<commit_before>/***************************************************************************
 * Copyright 2010 Global Biodiversity Information Facility Secretariat
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 ***************************************************************************/

package org.gbif.io;

import org.gbif.dwca.io.ArchiveFile;
import org.gbif.dwca.io.UnkownCharsetException;
import org.gbif.dwca.io.UnkownDelimitersException;
import org.gbif.utils.file.CharsetDetection;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CSVReaderFactory {

  private static final Logger LOG = LoggerFactory.getLogger(CSVReaderFactory.class);
  private static final int ROWS_TO_INSPECT = 10;

  public static CSVReader build(ArchiveFile source) throws IOException {
    return new CSVReader(source.getLocationFile(), source.getEncoding(), source.getFieldsTerminatedBy(),
      source.getFieldsEnclosedBy(), source.getIgnoreHeaderLines());
  }

  public static CSVReader build(File source) throws IOException {
    String encoding = detectEncoding(source);
    ArchiveFile af = buildArchiveFile(source, encoding);
    return build(af);
  }

  public static CSVReader build(File source, String delimiter, boolean header) throws IOException {
    return new CSVReader(source, detectEncoding(source), delimiter, null, header ? 1 : 0);
  }

  public static CSVReader build(File source, String encoding, String delimiter, Character quotes, Integer headerRows)
    throws IOException {
    return new CSVReader(source, encoding, delimiter, quotes, headerRows);
  }

  public static CSVReader build(File source, String encoding, String delimiter, Integer headerRows) throws IOException {
    return new CSVReader(source, encoding, delimiter, '"', headerRows);
  }

  public static CSVReader build(InputStream stream, String encoding, String delimiter, Character quotes,
    Integer headerRows) throws IOException {
    return new CSVReader(stream, encoding, delimiter, quotes, headerRows);
  }

  private static ArchiveFile buildArchiveFile(File source, String encoding) throws UnkownDelimitersException {
    ArchiveFile dwcFile = new ArchiveFile();
    // add file
    dwcFile.addLocation(source.getAbsolutePath());

    // detect character encoding
    dwcFile.setEncoding(encoding);

    // set header row to 1
    dwcFile.setIgnoreHeaderLines(1);

    // try csv, tab and then other popular delimiters
    // keep number of resulting columns for comparisons
    int maxColumns = 0;
<commit_msg>Add tab reader factory methods<commit_after>/***************************************************************************
 * Copyright 2010 Global Biodiversity Information Facility Secretariat
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 ***************************************************************************/

package org.gbif.io;

import org.gbif.dwca.io.ArchiveFile;
import org.gbif.dwca.io.UnkownCharsetException;
import org.gbif.dwca.io.UnkownDelimitersException;
import org.gbif.utils.file.CharsetDetection;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CSVReaderFactory {

  private static final Logger LOG = LoggerFactory.getLogger(CSVReaderFactory.class);
  private static final int ROWS_TO_INSPECT = 10;

  public static CSVReader build(ArchiveFile source) throws IOException {
    return new CSVReader(source.getLocationFile(), source.getEncoding(), source.getFieldsTerminatedBy(),
      source.getFieldsEnclosedBy(), source.getIgnoreHeaderLines());
  }

  public static CSVReader build(File source) throws IOException {
    String encoding = detectEncoding(source);
    ArchiveFile af = buildArchiveFile(source, encoding);
    return build(af);
  }

  public static CSVReader build(File source, String delimiter, boolean header) throws IOException {
    return new CSVReader(source, detectEncoding(source), delimiter, null, header ? 1 : 0);
  }

  public static CSVReader build(File source, String encoding, String delimiter, Character quotes, Integer headerRows)
    throws IOException {
    return new CSVReader(source, encoding, delimiter, quotes, headerRows);
  }

  public static CSVReader build(File source, String encoding, String delimiter, Integer headerRows) throws IOException {
    return new CSVReader(source, encoding, delimiter, '"', headerRows);
  }

  public static CSVReader build(InputStream stream, String encoding, String delimiter, Character quotes,
    Integer headerRows) throws IOException {
    return new CSVReader(stream, encoding, delimiter, quotes, headerRows);
  }

  public static CSVReader buildTabReader(InputStream stream, String encoding, Integer headerRows) throws IOException {
    return new CSVReader(stream, encoding, "\t", null, headerRows);
  }

  public static CSVReader buildUtf8TabReader(InputStream stream) throws IOException {
    return buildTabReader(stream, "utf8", 0);
  }

  private static ArchiveFile buildArchiveFile(File source, String encoding) throws UnkownDelimitersException {
    ArchiveFile dwcFile = new ArchiveFile();
    // add file
    dwcFile.addLocation(source.getAbsolutePath());

    // detect character encoding
    dwcFile.setEncoding(encoding);

    // set header row to 1
    dwcFile.setIgnoreHeaderLines(1);

    // try csv, tab and then other popular delimiters
    // keep number of resulting columns for comparisons
    int maxColumns = 0;

{'fraction_non_alphanumeric': 0.12042274486454302, 'fraction_numerical': 0.0055075915451027095, 'mean_word_length': 4.8886941279579315, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6337891', 'n_tokens_mistral': 1797, 'n_tokens_neox': 1678, 'n_words': 601}