Added: dev/commons/csv/1.8-RC2/site/jacoco/org.apache.commons.csv/CSVFormat.java.html ============================================================================== --- dev/commons/csv/1.8-RC2/site/jacoco/org.apache.commons.csv/CSVFormat.java.html (added) +++ dev/commons/csv/1.8-RC2/site/jacoco/org.apache.commons.csv/CSVFormat.java.html Sun Feb 2 01:23:05 2020 @@ -0,0 +1,2330 @@ +<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>CSVFormat.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Apache Commons CSV</a> > <a href="index.source.html" class="el_package">org.apache.commons.csv</a> > <span class="el_source">CSVFormat.ja va</span></div><h1>CSVFormat.java</h1><pre class="source lang-java linenums">/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.commons.csv; + +import static org.apache.commons.csv.Constants.BACKSLASH; +import static org.apache.commons.csv.Constants.COMMA; +import static org.apache.commons.csv.Constants.COMMENT; +import static org.apache.commons.csv.Constants.CR; +import static org.apache.commons.csv.Constants.CRLF; +import static org.apache.commons.csv.Constants.DOUBLE_QUOTE_CHAR; +import static org.apache.commons.csv.Constants.EMPTY; +import static org.apache.commons.csv.Constants.LF; +import static org.apache.commons.csv.Constants.PIPE; +import static org.apache.commons.csv.Constants.SP; +import static org.apache.commons.csv.Constants.TAB; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Reader; +import java.io.Serializable; +import java.io.StringWriter; +import java.io.Writer; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +/** + * Specifies the format of a CSV file and parses input. + * + * <h2>Using predefined formats</h2> + * + * <p> + * You can use one of the predefined formats: + * </p> + * + * <ul> + * <li>{@link #DEFAULT}</li> + * <li>{@link #EXCEL}</li> + * <li>{@link #INFORMIX_UNLOAD}</li> + * <li>{@link #INFORMIX_UNLOAD_CSV}</li> + * <li>{@link #MYSQL}</li> + * <li>{@link #RFC4180}</li> + * <li>{@link #ORACLE}</li> + * <li>{@link #POSTGRESQL_CSV}</li> + * <li>{@link #POSTGRESQL_TEXT}</li> + * <li>{@link #TDF}</li> + * </ul> + * + * <p> + * For example: + * </p> + * + * <pre> + * CSVParser parser = CSVFormat.EXCEL.parse(reader); + * </pre> + * + * <p> + * The {@link CSVParser} provides static methods to parse other input types, for example: + * </p> + * + * <pre> + * CSVParser parser = CSVParser.parse(file, StandardCharsets.US_ASCII, CSVFormat.EXCEL); + * </pre> + * + * <h2>Defining formats</h2> + * + * <p> + * You can extend a format by calling the {@code with} methods. For example: + * </p> + * + * <pre> + * CSVFormat.EXCEL.withNullString(&quot;N/A&quot;).withIgnoreSurroundingSpaces(true); + * </pre> + * + * <h2>Defining column names</h2> + * + * <p> + * To define the column names you want to use to access records, write: + * </p> + * + * <pre> + * CSVFormat.EXCEL.withHeader(&quot;Col1&quot;, &quot;Col2&quot;, &quot;Col3&quot;); + * </pre> + * + * <p> + * Calling {@link #withHeader(String...)} let's you use the given names to address values in a {@link CSVRecord}, and + * assumes that your CSV source does not contain a first record that also defines column names. + * + * If it does, then you are overriding this metadata with your names and you should skip the first record by calling + * {@link #withSkipHeaderRecord(boolean)} with {@code true}. + * </p> + * + * <h2>Parsing</h2> + * + * <p> + * You can use a format directly to parse a reader. For example, to parse an Excel file with columns header, write: + * </p> + * + * <pre> + * Reader in = ...; + * CSVFormat.EXCEL.withHeader(&quot;Col1&quot;, &quot;Col2&quot;, &quot;Col3&quot;).parse(in); + * </pre> + * + * <p> + * For other input types, like resources, files, and URLs, use the static methods on {@link CSVParser}. + * </p> + * + * <h2>Referencing columns safely</h2> + * + * <p> + * If your source contains a header record, you can simplify your code and safely reference columns, by using + * {@link #withHeader(String...)} with no arguments: + * </p> + * + * <pre> + * CSVFormat.EXCEL.withHeader(); + * </pre> + * + * <p> + * This causes the parser to read the first record and use its values as column names. + * + * Then, call one of the {@link CSVRecord} get method that takes a String column name argument: + * </p> + * + * <pre> + * String value = record.get(&quot;Col1&quot;); + * </pre> + * + * <p> + * This makes your code impervious to changes in column order in the CSV file. + * </p> + * + * <h2>Notes</h2> + * + * <p> + * This class is immutable. + * </p> + */ +public final class CSVFormat implements Serializable { + + /** + * Predefines formats. + * + * @since 1.2 + */ +<span class="fc" id="L169"> public enum Predefined {</span> + + /** + * @see CSVFormat#DEFAULT + */ +<span class="fc" id="L174"> Default(CSVFormat.DEFAULT),</span> + + /** + * @see CSVFormat#EXCEL + */ +<span class="fc" id="L179"> Excel(CSVFormat.EXCEL),</span> + + /** + * @see CSVFormat#INFORMIX_UNLOAD + * @since 1.3 + */ +<span class="fc" id="L185"> InformixUnload(CSVFormat.INFORMIX_UNLOAD),</span> + + /** + * @see CSVFormat#INFORMIX_UNLOAD_CSV + * @since 1.3 + */ +<span class="fc" id="L191"> InformixUnloadCsv(CSVFormat.INFORMIX_UNLOAD_CSV),</span> + + /** + * @see CSVFormat#MONGODB_CSV + * @since 1.7 + */ +<span class="fc" id="L197"> MongoDBCsv(CSVFormat.MONGODB_CSV),</span> + + /** + * @see CSVFormat#MONGODB_TSV + * @since 1.7 + */ +<span class="fc" id="L203"> MongoDBTsv(CSVFormat.MONGODB_TSV),</span> + + /** + * @see CSVFormat#MYSQL + */ +<span class="fc" id="L208"> MySQL(CSVFormat.MYSQL),</span> + + /** + * @see CSVFormat#ORACLE + */ +<span class="fc" id="L213"> Oracle(CSVFormat.ORACLE),</span> + + /** + * @see CSVFormat#POSTGRESQL_CSV + * @since 1.5 + */ +<span class="fc" id="L219"> PostgreSQLCsv(CSVFormat.POSTGRESQL_CSV),</span> + + /** + * @see CSVFormat#POSTGRESQL_CSV + */ +<span class="fc" id="L224"> PostgreSQLText(CSVFormat.POSTGRESQL_TEXT),</span> + + /** + * @see CSVFormat#RFC4180 + */ +<span class="fc" id="L229"> RFC4180(CSVFormat.RFC4180),</span> + + /** + * @see CSVFormat#TDF + */ +<span class="fc" id="L234"> TDF(CSVFormat.TDF);</span> + + private final CSVFormat format; + +<span class="fc" id="L238"> Predefined(final CSVFormat format) {</span> +<span class="fc" id="L239"> this.format = format;</span> +<span class="fc" id="L240"> }</span> + + /** + * Gets the format. + * + * @return the format. + */ + public CSVFormat getFormat() { +<span class="fc" id="L248"> return format;</span> + } + } + + /** + * Standard Comma Separated Value format, as for {@link #RFC4180} but allowing empty lines. + * + * <p> + * Settings are: + * </p> + * <ul> + * <li>{@code withDelimiter(',')}</li> + * <li>{@code withQuote('"')}</li> + * <li>{@code withRecordSeparator("\r\n")}</li> + * <li>{@code withIgnoreEmptyLines(true)}</li> + * <li>{@code withAllowDuplicateHeaderNames(true)}</li> + * </ul> + * + * @see Predefined#Default + */ +<span class="fc" id="L268"> public static final CSVFormat DEFAULT = new CSVFormat(COMMA, DOUBLE_QUOTE_CHAR, null, null, null, false, true, CRLF,</span> + null, null, null, false, false, false, false, false, false, true); + + /** + * Excel file format (using a comma as the value delimiter). Note that the actual value delimiter used by Excel is + * locale dependent, it might be necessary to customize this format to accommodate to your regional settings. + * + * <p> + * For example for parsing or generating a CSV file on a French system the following format will be used: + * </p> + * + * <pre> + * CSVFormat fmt = CSVFormat.EXCEL.withDelimiter(';'); + * </pre> + * + * <p> + * Settings are: + * </p> + * <ul> + * <li>{@code {@link #withDelimiter(char) withDelimiter(',')}}</li> + * <li>{@code {@link #withQuote(char) withQuote('"')}}</li> + * <li>{@code {@link #withRecordSeparator(String) withRecordSeparator("\r\n")}}</li> + * <li>{@code {@link #withIgnoreEmptyLines(boolean) withIgnoreEmptyLines(false)}}</li> + * <li>{@code {@link #withAllowMissingColumnNames(boolean) withAllowMissingColumnNames(true)}}</li> + * <li>{@code {@link #withAllowDuplicateHeaderNames(boolean) withAllowDuplicateHeaderNames(true)}}</li> + * </ul> + * <p> + * Note: This is currently like {@link #RFC4180} plus {@link #withAllowMissingColumnNames(boolean) + * withAllowMissingColumnNames(true)} and {@link #withIgnoreEmptyLines(boolean) withIgnoreEmptyLines(false)}. + * </p> + * + * @see Predefined#Excel + */ + // @formatter:off +<span class="fc" id="L302"> public static final CSVFormat EXCEL = DEFAULT</span> +<span class="fc" id="L303"> .withIgnoreEmptyLines(false)</span> +<span class="fc" id="L304"> .withAllowMissingColumnNames();</span> + // @formatter:on + + /** + * Default Informix CSV UNLOAD format used by the {@code UNLOAD TO file_name} operation. + * + * <p> + * This is a comma-delimited format with a LF character as the line separator. Values are not quoted and special + * characters are escaped with {@code '\'}. The default NULL string is {@code "\\N"}. + * </p> + * + * <p> + * Settings are: + * </p> + * <ul> + * <li>{@code withDelimiter(',')}</li> + * <li>{@code withEscape('\\')}</li> + * <li>{@code withQuote("\"")}</li> + * <li>{@code withRecordSeparator('\n')}</li> + * </ul> + * + * @see Predefined#MySQL + * @see <a href= + * "http://www.ibm.com/support/knowledgecenter/SSBJG3_2.5.0/com.ibm.gen_busug.doc/c_fgl_InOutSql_UNLOAD.htm"> + * http://www.ibm.com/support/knowledgecenter/SSBJG3_2.5.0/com.ibm.gen_busug.doc/c_fgl_InOutSql_UNLOAD.htm</a> + * @since 1.3 + */ + // @formatter:off +<span class="fc" id="L332"> public static final CSVFormat INFORMIX_UNLOAD = DEFAULT</span> +<span class="fc" id="L333"> .withDelimiter(PIPE)</span> +<span class="fc" id="L334"> .withEscape(BACKSLASH)</span> +<span class="fc" id="L335"> .withQuote(DOUBLE_QUOTE_CHAR)</span> +<span class="fc" id="L336"> .withRecordSeparator(LF);</span> + // @formatter:on + + /** + * Default Informix CSV UNLOAD format used by the {@code UNLOAD TO file_name} operation (escaping is disabled.) + * + * <p> + * This is a comma-delimited format with a LF character as the line separator. Values are not quoted and special + * characters are escaped with {@code '\'}. The default NULL string is {@code "\\N"}. + * </p> + * + * <p> + * Settings are: + * </p> + * <ul> + * <li>{@code withDelimiter(',')}</li> + * <li>{@code withQuote("\"")}</li> + * <li>{@code withRecordSeparator('\n')}</li> + * </ul> + * + * @see Predefined#MySQL + * @see <a href= + * "http://www.ibm.com/support/knowledgecenter/SSBJG3_2.5.0/com.ibm.gen_busug.doc/c_fgl_InOutSql_UNLOAD.htm"> + * http://www.ibm.com/support/knowledgecenter/SSBJG3_2.5.0/com.ibm.gen_busug.doc/c_fgl_InOutSql_UNLOAD.htm</a> + * @since 1.3 + */ + // @formatter:off +<span class="fc" id="L363"> public static final CSVFormat INFORMIX_UNLOAD_CSV = DEFAULT</span> +<span class="fc" id="L364"> .withDelimiter(COMMA)</span> +<span class="fc" id="L365"> .withQuote(DOUBLE_QUOTE_CHAR)</span> +<span class="fc" id="L366"> .withRecordSeparator(LF);</span> + // @formatter:on + + /** + * Default MongoDB CSV format used by the {@code mongoexport} operation. + * <p> + * <b>Parsing is not supported yet.</b> + * </p> + * + * <p> + * This is a comma-delimited format. Values are double quoted only if needed and special characters are escaped with + * {@code '"'}. A header line with field names is expected. + * </p> + * + * <p> + * Settings are: + * </p> + * <ul> + * <li>{@code withDelimiter(',')}</li> + * <li>{@code withEscape('"')}</li> + * <li>{@code withQuote('"')}</li> + * <li>{@code withQuoteMode(QuoteMode.ALL_NON_NULL)}</li> + * <li>{@code withSkipHeaderRecord(false)}</li> + * </ul> + * + * @see Predefined#MongoDBCsv + * @see <a href="https://docs.mongodb.com/manual/reference/program/mongoexport/">MongoDB mongoexport command + * documentation</a> + * @since 1.7 + */ + // @formatter:off +<span class="fc" id="L397"> public static final CSVFormat MONGODB_CSV = DEFAULT</span> +<span class="fc" id="L398"> .withDelimiter(COMMA)</span> +<span class="fc" id="L399"> .withEscape(DOUBLE_QUOTE_CHAR)</span> +<span class="fc" id="L400"> .withQuote(DOUBLE_QUOTE_CHAR)</span> +<span class="fc" id="L401"> .withQuoteMode(QuoteMode.MINIMAL)</span> +<span class="fc" id="L402"> .withSkipHeaderRecord(false);</span> + // @formatter:off + + /** + * Default MongoDB TSV format used by the {@code mongoexport} operation. + * <p> + * <b>Parsing is not supported yet.</b> + * </p> + * + * <p> + * This is a tab-delimited format. Values are double quoted only if needed and special + * characters are escaped with {@code '"'}. A header line with field names is expected. + * </p> + * + * <p> + * Settings are: + * </p> + * <ul> + * <li>{@code withDelimiter('\t')}</li> + * <li>{@code withEscape('"')}</li> + * <li>{@code withQuote('"')}</li> + * <li>{@code withQuoteMode(QuoteMode.ALL_NON_NULL)}</li> + * <li>{@code withSkipHeaderRecord(false)}</li> + * </ul> + * + * @see Predefined#MongoDBCsv + * @see <a href="https://docs.mongodb.com/manual/reference/program/mongoexport/">MongoDB mongoexport command + * documentation</a> + * @since 1.7 + */ + // @formatter:off +<span class="fc" id="L433"> public static final CSVFormat MONGODB_TSV = DEFAULT</span> +<span class="fc" id="L434"> .withDelimiter(TAB)</span> +<span class="fc" id="L435"> .withEscape(DOUBLE_QUOTE_CHAR)</span> +<span class="fc" id="L436"> .withQuote(DOUBLE_QUOTE_CHAR)</span> +<span class="fc" id="L437"> .withQuoteMode(QuoteMode.MINIMAL)</span> +<span class="fc" id="L438"> .withSkipHeaderRecord(false);</span> + // @formatter:off + + /** + * Default MySQL format used by the {@code SELECT INTO OUTFILE} and {@code LOAD DATA INFILE} operations. + * + * <p> + * This is a tab-delimited format with a LF character as the line separator. Values are not quoted and special + * characters are escaped with {@code '\'}. The default NULL string is {@code "\\N"}. + * </p> + * + * <p> + * Settings are: + * </p> + * <ul> + * <li>{@code withDelimiter('\t')}</li> + * <li>{@code withEscape('\\')}</li> + * <li>{@code withIgnoreEmptyLines(false)}</li> + * <li>{@code withQuote(null)}</li> + * <li>{@code withRecordSeparator('\n')}</li> + * <li>{@code withNullString("\\N")}</li> + * <li>{@code withQuoteMode(QuoteMode.ALL_NON_NULL)}</li> + * </ul> + * + * @see Predefined#MySQL + * @see <a href="http://dev.mysql.com/doc/refman/5.1/en/load-data.html"> http://dev.mysql.com/doc/refman/5.1/en/load + * -data.html</a> + */ + // @formatter:off +<span class="fc" id="L467"> public static final CSVFormat MYSQL = DEFAULT</span> +<span class="fc" id="L468"> .withDelimiter(TAB)</span> +<span class="fc" id="L469"> .withEscape(BACKSLASH)</span> +<span class="fc" id="L470"> .withIgnoreEmptyLines(false)</span> +<span class="fc" id="L471"> .withQuote(null)</span> +<span class="fc" id="L472"> .withRecordSeparator(LF)</span> +<span class="fc" id="L473"> .withNullString("\\N")</span> +<span class="fc" id="L474"> .withQuoteMode(QuoteMode.ALL_NON_NULL);</span> + // @formatter:off + + /** + * Default Oracle format used by the SQL*Loader utility. + * + * <p> + * This is a comma-delimited format with the system line separator character as the record separator.Values are + * double quoted when needed and special characters are escaped with {@code '"'}. The default NULL string is + * {@code ""}. Values are trimmed. + * </p> + * + * <p> + * Settings are: + * </p> + * <ul> + * <li>{@code withDelimiter(',') // default is {@code FIELDS TERMINATED BY ','}}</li> + * <li>{@code withEscape('\\')}</li> + * <li>{@code withIgnoreEmptyLines(false)}</li> + * <li>{@code withQuote('"') // default is {@code OPTIONALLY ENCLOSED BY '"'}}</li> + * <li>{@code withNullString("\\N")}</li> + * <li>{@code withTrim()}</li> + * <li>{@code withSystemRecordSeparator()}</li> + * <li>{@code withQuoteMode(QuoteMode.MINIMAL)}</li> + * </ul> + * + * @see Predefined#Oracle + * @see <a href="https://s.apache.org/CGXG">Oracle CSV Format Specification</a> + * @since 1.6 + */ + // @formatter:off +<span class="fc" id="L505"> public static final CSVFormat ORACLE = DEFAULT</span> +<span class="fc" id="L506"> .withDelimiter(COMMA)</span> +<span class="fc" id="L507"> .withEscape(BACKSLASH)</span> +<span class="fc" id="L508"> .withIgnoreEmptyLines(false)</span> +<span class="fc" id="L509"> .withQuote(DOUBLE_QUOTE_CHAR)</span> +<span class="fc" id="L510"> .withNullString("\\N")</span> +<span class="fc" id="L511"> .withTrim()</span> +<span class="fc" id="L512"> .withSystemRecordSeparator()</span> +<span class="fc" id="L513"> .withQuoteMode(QuoteMode.MINIMAL);</span> + // @formatter:off + + /** + * Default PostgreSQL CSV format used by the {@code COPY} operation. + * + * <p> + * This is a comma-delimited format with a LF character as the line separator. Values are double quoted and special + * characters are escaped with {@code '"'}. The default NULL string is {@code ""}. + * </p> + * + * <p> + * Settings are: + * </p> + * <ul> + * <li>{@code withDelimiter(',')}</li> + * <li>{@code withEscape('"')}</li> + * <li>{@code withIgnoreEmptyLines(false)}</li> + * <li>{@code withQuote('"')}</li> + * <li>{@code withRecordSeparator('\n')}</li> + * <li>{@code withNullString("")}</li> + * <li>{@code withQuoteMode(QuoteMode.ALL_NON_NULL)}</li> + * </ul> + * + * @see Predefined#MySQL + * @see <a href="https://www.postgresql.org/docs/current/static/sql-copy.html">PostgreSQL COPY command + * documentation</a> + * @since 1.5 + */ + // @formatter:off +<span class="fc" id="L543"> public static final CSVFormat POSTGRESQL_CSV = DEFAULT</span> +<span class="fc" id="L544"> .withDelimiter(COMMA)</span> +<span class="fc" id="L545"> .withEscape(DOUBLE_QUOTE_CHAR)</span> +<span class="fc" id="L546"> .withIgnoreEmptyLines(false)</span> +<span class="fc" id="L547"> .withQuote(DOUBLE_QUOTE_CHAR)</span> +<span class="fc" id="L548"> .withRecordSeparator(LF)</span> +<span class="fc" id="L549"> .withNullString(EMPTY)</span> +<span class="fc" id="L550"> .withQuoteMode(QuoteMode.ALL_NON_NULL);</span> + // @formatter:off + + /** + * Default PostgreSQL text format used by the {@code COPY} operation. + * + * <p> + * This is a tab-delimited format with a LF character as the line separator. Values are double quoted and special + * characters are escaped with {@code '"'}. The default NULL string is {@code "\\N"}. + * </p> + * + * <p> + * Settings are: + * </p> + * <ul> + * <li>{@code withDelimiter('\t')}</li> + * <li>{@code withEscape('\\')}</li> + * <li>{@code withIgnoreEmptyLines(false)}</li> + * <li>{@code withQuote('"')}</li> + * <li>{@code withRecordSeparator('\n')}</li> + * <li>{@code withNullString("\\N")}</li> + * <li>{@code withQuoteMode(QuoteMode.ALL_NON_NULL)}</li> + * </ul> + * + * @see Predefined#MySQL + * @see <a href="https://www.postgresql.org/docs/current/static/sql-copy.html">PostgreSQL COPY command + * documentation</a> + * @since 1.5 + */ + // @formatter:off +<span class="fc" id="L580"> public static final CSVFormat POSTGRESQL_TEXT = DEFAULT</span> +<span class="fc" id="L581"> .withDelimiter(TAB)</span> +<span class="fc" id="L582"> .withEscape(BACKSLASH)</span> +<span class="fc" id="L583"> .withIgnoreEmptyLines(false)</span> +<span class="fc" id="L584"> .withQuote(DOUBLE_QUOTE_CHAR)</span> +<span class="fc" id="L585"> .withRecordSeparator(LF)</span> +<span class="fc" id="L586"> .withNullString("\\N")</span> +<span class="fc" id="L587"> .withQuoteMode(QuoteMode.ALL_NON_NULL);</span> + // @formatter:off + + /** + * Comma separated format as defined by <a href="http://tools.ietf.org/html/rfc4180">RFC 4180</a>. + * + * <p> + * Settings are: + * </p> + * <ul> + * <li>{@code withDelimiter(',')}</li> + * <li>{@code withQuote('"')}</li> + * <li>{@code withRecordSeparator("\r\n")}</li> + * <li>{@code withIgnoreEmptyLines(false)}</li> + * </ul> + * + * @see Predefined#RFC4180 + */ +<span class="fc" id="L605"> public static final CSVFormat RFC4180 = DEFAULT.withIgnoreEmptyLines(false);</span> + + private static final long serialVersionUID = 1L; + + /** + * Tab-delimited format. + * + * <p> + * Settings are: + * </p> + * <ul> + * <li>{@code withDelimiter('\t')}</li> + * <li>{@code withQuote('"')}</li> + * <li>{@code withRecordSeparator("\r\n")}</li> + * <li>{@code withIgnoreSurroundingSpaces(true)}</li> + * </ul> + * + * @see Predefined#TDF + */ + // @formatter:off +<span class="fc" id="L625"> public static final CSVFormat TDF = DEFAULT</span> +<span class="fc" id="L626"> .withDelimiter(TAB)</span> +<span class="fc" id="L627"> .withIgnoreSurroundingSpaces();</span> + // @formatter:on + + /** + * Returns true if the given character is a line break character. + * + * @param c + * the character to check + * + * @return true if {@code c} is a line break character + */ + private static boolean isLineBreak(final char c) { +<span class="fc bfc" id="L639" title="All 4 branches covered."> return c == LF || c == CR;</span> + } + + /** + * Returns true if the given character is a line break character. + * + * @param c + * the character to check, may be null + * + * @return true if {@code c} is a line break character (and not null) + */ + private static boolean isLineBreak(final Character c) { +<span class="fc bfc" id="L651" title="All 4 branches covered."> return c != null && isLineBreak(c.charValue());</span> + } + + /** + * Creates a new CSV format with the specified delimiter. + * + * <p> + * Use this method if you want to create a CSVFormat from scratch. All fields but the delimiter will be initialized + * with null/false. + * </p> + * + * @param delimiter + * the char used for value separation, must not be a line break character + * @return a new CSV format. + * @throws IllegalArgumentException + * if the delimiter is a line break character + * + * @see #DEFAULT + * @see #RFC4180 + * @see #MYSQL + * @see #EXCEL + * @see #TDF + */ + public static CSVFormat newFormat(final char delimiter) { +<span class="fc" id="L675"> return new CSVFormat(delimiter, null, null, null, null, false, false, null, null, null, null, false, false,</span> + false, false, false, false, true); + } + + /** + * Gets one of the predefined formats from {@link CSVFormat.Predefined}. + * + * @param format + * name + * @return one of the predefined formats + * @since 1.2 + */ + public static CSVFormat valueOf(final String format) { +<span class="fc" id="L688"> return CSVFormat.Predefined.valueOf(format).getFormat();</span> + } + + private final boolean allowDuplicateHeaderNames; + + private final boolean allowMissingColumnNames; + + private final boolean autoFlush; + + private final Character commentMarker; // null if commenting is disabled + + private final char delimiter; + + private final Character escapeCharacter; // null if escaping is disabled + + private final String[] header; // array of header column names + + private final String[] headerComments; // array of header comment lines + + private final boolean ignoreEmptyLines; + + private final boolean ignoreHeaderCase; // should ignore header names case + + private final boolean ignoreSurroundingSpaces; // Should leading/trailing spaces be ignored around values? + + private final String nullString; // the string to be used for null values + + private final Character quoteCharacter; // null if quoting is disabled + + private final String quotedNullString; + + private final QuoteMode quoteMode; + + private final String recordSeparator; // for outputs + + private final boolean skipHeaderRecord; + + private final boolean trailingDelimiter; + + private final boolean trim; + + /** + * Creates a customized CSV format. + * + * @param delimiter + * the char used for value separation, must not be a line break character + * @param quoteChar + * the Character used as value encapsulation marker, may be {@code null} to disable + * @param quoteMode + * the quote mode + * @param commentStart + * the Character used for comment identification, may be {@code null} to disable + * @param escape + * the Character used to escape special characters in values, may be {@code null} to disable + * @param ignoreSurroundingSpaces + * {@code true} when whitespaces enclosing values should be ignored + * @param ignoreEmptyLines + * {@code true} when the parser should skip empty lines + * @param recordSeparator + * the line separator to use for output + * @param nullString + * the line separator to use for output + * @param headerComments + * the comments to be printed by the Printer before the actual CSV data + * @param header + * the header + * @param skipHeaderRecord + * TODO + * @param allowMissingColumnNames + * TODO + * @param ignoreHeaderCase + * TODO + * @param trim + * TODO + * @param trailingDelimiter + * TODO + * @param autoFlush + * @throws IllegalArgumentException + * if the delimiter is a line break character + */ + private CSVFormat(final char delimiter, final Character quoteChar, final QuoteMode quoteMode, + final Character commentStart, final Character escape, final boolean ignoreSurroundingSpaces, + final boolean ignoreEmptyLines, final String recordSeparator, final String nullString, + final Object[] headerComments, final String[] header, final boolean skipHeaderRecord, + final boolean allowMissingColumnNames, final boolean ignoreHeaderCase, final boolean trim, +<span class="fc" id="L773"> final boolean trailingDelimiter, final boolean autoFlush, final boolean allowDuplicateHeaderNames) {</span> +<span class="fc" id="L774"> this.delimiter = delimiter;</span> +<span class="fc" id="L775"> this.quoteCharacter = quoteChar;</span> +<span class="fc" id="L776"> this.quoteMode = quoteMode;</span> +<span class="fc" id="L777"> this.commentMarker = commentStart;</span> +<span class="fc" id="L778"> this.escapeCharacter = escape;</span> +<span class="fc" id="L779"> this.ignoreSurroundingSpaces = ignoreSurroundingSpaces;</span> +<span class="fc" id="L780"> this.allowMissingColumnNames = allowMissingColumnNames;</span> +<span class="fc" id="L781"> this.ignoreEmptyLines = ignoreEmptyLines;</span> +<span class="fc" id="L782"> this.recordSeparator = recordSeparator;</span> +<span class="fc" id="L783"> this.nullString = nullString;</span> +<span class="fc" id="L784"> this.headerComments = toStringArray(headerComments);</span> +<span class="fc bfc" id="L785" title="All 2 branches covered."> this.header = header == null ? null : header.clone();</span> +<span class="fc" id="L786"> this.skipHeaderRecord = skipHeaderRecord;</span> +<span class="fc" id="L787"> this.ignoreHeaderCase = ignoreHeaderCase;</span> +<span class="fc" id="L788"> this.trailingDelimiter = trailingDelimiter;</span> +<span class="fc" id="L789"> this.trim = trim;</span> +<span class="fc" id="L790"> this.autoFlush = autoFlush;</span> +<span class="fc" id="L791"> this.quotedNullString = quoteCharacter + nullString + quoteCharacter;</span> +<span class="fc" id="L792"> this.allowDuplicateHeaderNames = allowDuplicateHeaderNames;</span> +<span class="fc" id="L793"> validate();</span> +<span class="fc" id="L794"> }</span> + + @Override + public boolean equals(final Object obj) { +<span class="fc bfc" id="L798" title="All 2 branches covered."> if (this == obj) {</span> +<span class="fc" id="L799"> return true;</span> + } +<span class="fc bfc" id="L801" title="All 2 branches covered."> if (obj == null) {</span> +<span class="fc" id="L802"> return false;</span> + } +<span class="fc bfc" id="L804" title="All 2 branches covered."> if (getClass() != obj.getClass()) {</span> +<span class="fc" id="L805"> return false;</span> + } + +<span class="fc" id="L808"> final CSVFormat other = (CSVFormat) obj;</span> +<span class="fc bfc" id="L809" title="All 2 branches covered."> if (delimiter != other.delimiter) {</span> +<span class="fc" id="L810"> return false;</span> + } +<span class="fc bfc" id="L812" title="All 2 branches covered."> if (trailingDelimiter != other.trailingDelimiter) {</span> +<span class="fc" id="L813"> return false;</span> + } +<span class="fc bfc" id="L815" title="All 2 branches covered."> if (autoFlush != other.autoFlush) {</span> +<span class="fc" id="L816"> return false;</span> + } +<span class="fc bfc" id="L818" title="All 2 branches covered."> if (trim != other.trim) {</span> +<span class="fc" id="L819"> return false;</span> + } +<span class="fc bfc" id="L821" title="All 2 branches covered."> if (allowMissingColumnNames != other.allowMissingColumnNames) {</span> +<span class="fc" id="L822"> return false;</span> + } +<span class="fc bfc" id="L824" title="All 2 branches covered."> if (allowDuplicateHeaderNames != other.allowDuplicateHeaderNames) {</span> +<span class="fc" id="L825"> return false;</span> + } +<span class="fc bfc" id="L827" title="All 2 branches covered."> if (ignoreHeaderCase != other.ignoreHeaderCase) {</span> +<span class="fc" id="L828"> return false;</span> + } +<span class="fc bfc" id="L830" title="All 2 branches covered."> if (quoteMode != other.quoteMode) {</span> +<span class="fc" id="L831"> return false;</span> + } +<span class="fc bfc" id="L833" title="All 2 branches covered."> if (quoteCharacter == null) {</span> +<span class="fc bfc" id="L834" title="All 2 branches covered."> if (other.quoteCharacter != null) {</span> +<span class="fc" id="L835"> return false;</span> + } +<span class="fc bfc" id="L837" title="All 2 branches covered."> } else if (!quoteCharacter.equals(other.quoteCharacter)) {</span> +<span class="fc" id="L838"> return false;</span> + } +<span class="fc bfc" id="L840" title="All 2 branches covered."> if (commentMarker == null) {</span> +<span class="fc bfc" id="L841" title="All 2 branches covered."> if (other.commentMarker != null) {</span> +<span class="fc" id="L842"> return false;</span> + } +<span class="fc bfc" id="L844" title="All 2 branches covered."> } else if (!commentMarker.equals(other.commentMarker)) {</span> +<span class="fc" id="L845"> return false;</span> + } +<span class="fc bfc" id="L847" title="All 2 branches covered."> if (escapeCharacter == null) {</span> +<span class="fc bfc" id="L848" title="All 2 branches covered."> if (other.escapeCharacter != null) {</span> +<span class="fc" id="L849"> return false;</span> + } +<span class="fc bfc" id="L851" title="All 2 branches covered."> } else if (!escapeCharacter.equals(other.escapeCharacter)) {</span> +<span class="fc" id="L852"> return false;</span> + } +<span class="fc bfc" id="L854" title="All 2 branches covered."> if (nullString == null) {</span> +<span class="fc bfc" id="L855" title="All 2 branches covered."> if (other.nullString != null) {</span> +<span class="fc" id="L856"> return false;</span> + } +<span class="fc bfc" id="L858" title="All 2 branches covered."> } else if (!nullString.equals(other.nullString)) {</span> +<span class="fc" id="L859"> return false;</span> + } +<span class="fc bfc" id="L861" title="All 2 branches covered."> if (!Arrays.equals(header, other.header)) {</span> +<span class="fc" id="L862"> return false;</span> + } +<span class="fc bfc" id="L864" title="All 2 branches covered."> if (ignoreSurroundingSpaces != other.ignoreSurroundingSpaces) {</span> +<span class="fc" id="L865"> return false;</span> + } +<span class="fc bfc" id="L867" title="All 2 branches covered."> if (ignoreEmptyLines != other.ignoreEmptyLines) {</span> +<span class="fc" id="L868"> return false;</span> + } +<span class="fc bfc" id="L870" title="All 2 branches covered."> if (skipHeaderRecord != other.skipHeaderRecord) {</span> +<span class="fc" id="L871"> return false;</span> + } +<span class="fc bfc" id="L873" title="All 2 branches covered."> if (recordSeparator == null) {</span> +<span class="fc bfc" id="L874" title="All 2 branches covered."> if (other.recordSeparator != null) {</span> +<span class="fc" id="L875"> return false;</span> + } +<span class="fc bfc" id="L877" title="All 2 branches covered."> } else if (!recordSeparator.equals(other.recordSeparator)) {</span> +<span class="fc" id="L878"> return false;</span> + } +<span class="fc bfc" id="L880" title="All 2 branches covered."> if (!Arrays.equals(headerComments, other.headerComments)) {</span> +<span class="fc" id="L881"> return false;</span> + } +<span class="fc" id="L883"> return true;</span> + } + + /** + * Formats the specified values. + * + * @param values + * the values to format + * @return the formatted values + */ + public String format(final Object... values) { +<span class="fc" id="L894"> final StringWriter out = new StringWriter();</span> +<span class="fc" id="L895"> try (CSVPrinter csvPrinter = new CSVPrinter(out, this)) {</span> +<span class="fc" id="L896"> csvPrinter.printRecord(values);</span> +<span class="fc" id="L897"> return out.toString().trim();</span> +<span class="nc" id="L898"> } catch (final IOException e) {</span> + // should not happen because a StringWriter does not do IO. +<span class="nc" id="L900"> throw new IllegalStateException(e);</span> + } + } + + /** + * Returns true if and only if duplicate names are allowed in the headers. + * + * @return whether duplicate header names are allowed + * @since 1.7 + */ + public boolean getAllowDuplicateHeaderNames() { +<span class="fc" id="L911"> return allowDuplicateHeaderNames;</span> + } + + /** + * Specifies whether missing column names are allowed when parsing the header line. + * + * @return {@code true} if missing column names are allowed when parsing the header line, {@code false} to throw an + * {@link IllegalArgumentException}. + */ + public boolean getAllowMissingColumnNames() { +<span class="fc" id="L921"> return allowMissingColumnNames;</span> + } + + /** + * Returns whether to flush on close. + * + * @return whether to flush on close. + * @since 1.6 + */ + public boolean getAutoFlush() { +<span class="fc" id="L931"> return autoFlush;</span> + } + + /** + * Returns the character marking the start of a line comment. + * + * @return the comment start marker, may be {@code null} + */ + public Character getCommentMarker() { +<span class="fc" id="L940"> return commentMarker;</span> + } + + /** + * Returns the character delimiting the values (typically ';', ',' or '\t'). + * + * @return the delimiter character + */ + public char getDelimiter() { +<span class="fc" id="L949"> return delimiter;</span> + } + + /** + * Returns the escape character. + * + * @return the escape character, may be {@code null} + */ + public Character getEscapeCharacter() { +<span class="fc" id="L958"> return escapeCharacter;</span> + } + + /** + * Returns a copy of the header array. + * + * @return a copy of the header array; {@code null} if disabled, the empty array if to be read from the file + */ + public String[] getHeader() { +<span class="fc bfc" id="L967" title="All 2 branches covered."> return header != null ? header.clone() : null;</span> + } + + /** + * Returns a copy of the header comment array. + * + * @return a copy of the header comment array; {@code null} if disabled. + */ + public String[] getHeaderComments() { +<span class="fc bfc" id="L976" title="All 2 branches covered."> return headerComments != null ? headerComments.clone() : null;</span> + } + + /** + * Specifies whether empty lines between records are ignored when parsing input. + * + * @return {@code true} if empty lines between records are ignored, {@code false} if they are turned into empty + * records. + */ + public boolean getIgnoreEmptyLines() { +<span class="fc" id="L986"> return ignoreEmptyLines;</span> + } + + /** + * Specifies whether header names will be accessed ignoring case. + * + * @return {@code true} if header names cases are ignored, {@code false} if they are case sensitive. + * @since 1.3 + */ + public boolean getIgnoreHeaderCase() { +<span class="fc" id="L996"> return ignoreHeaderCase;</span> + } + + /** + * Specifies whether spaces around values are ignored when parsing input. + * + * @return {@code true} if spaces around values are ignored, {@code false} if they are treated as part of the value. + */ + public boolean getIgnoreSurroundingSpaces() { +<span class="fc" id="L1005"> return ignoreSurroundingSpaces;</span> + } + + /** + * Gets the String to convert to and from {@code null}. + * <ul> + * <li><strong>Reading:</strong> Converts strings equal to the given {@code nullString} to {@code null} when reading + * records.</li> + * <li><strong>Writing:</strong> Writes {@code null} as the given {@code nullString} when writing records.</li> + * </ul> + * + * @return the String to convert to and from {@code null}. No substitution occurs if {@code null} + */ + public String getNullString() { +<span class="fc" id="L1019"> return nullString;</span> + } + + /** + * Returns the character used to encapsulate values containing special characters. + * + * @return the quoteChar character, may be {@code null} + */ + public Character getQuoteCharacter() { +<span class="fc" id="L1028"> return quoteCharacter;</span> + } + + /** + * Returns the quote policy output fields. + * + * @return the quote policy + */ + public QuoteMode getQuoteMode() { +<span class="fc" id="L1037"> return quoteMode;</span> + } + + /** + * Returns the record separator delimiting output records. + * + * @return the record separator + */ + public String getRecordSeparator() { +<span class="fc" id="L1046"> return recordSeparator;</span> + } + + /** + * Returns whether to skip the header record. + * + * @return whether to skip the header record. + */ + public boolean getSkipHeaderRecord() { +<span class="fc" id="L1055"> return skipHeaderRecord;</span> + } + + /** + * Returns whether to add a trailing delimiter. + * + * @return whether to add a trailing delimiter. + * @since 1.3 + */ + public boolean getTrailingDelimiter() { +<span class="fc" id="L1065"> return trailingDelimiter;</span> + } + + /** + * Returns whether to trim leading and trailing blanks. + * This is used by {@link #print(Object, Appendable, boolean)} + * Also by {@link CSVParser#addRecordValue(boolean)} + * + * @return whether to trim leading and trailing blanks. + */ + public boolean getTrim() { +<span class="fc" id="L1076"> return trim;</span> + } + + @Override + public int hashCode() { +<span class="fc" id="L1081"> final int prime = 31;</span> +<span class="fc" id="L1082"> int result = 1;</span> + +<span class="fc" id="L1084"> result = prime * result + delimiter;</span> +<span class="fc bfc" id="L1085" title="All 2 branches covered."> result = prime * result + ((quoteMode == null) ? 0 : quoteMode.hashCode());</span> +<span class="fc bfc" id="L1086" title="All 2 branches covered."> result = prime * result + ((quoteCharacter == null) ? 0 : quoteCharacter.hashCode());</span> +<span class="fc bfc" id="L1087" title="All 2 branches covered."> result = prime * result + ((commentMarker == null) ? 0 : commentMarker.hashCode());</span> +<span class="fc bfc" id="L1088" title="All 2 branches covered."> result = prime * result + ((escapeCharacter == null) ? 0 : escapeCharacter.hashCode());</span> +<span class="fc bfc" id="L1089" title="All 2 branches covered."> result = prime * result + ((nullString == null) ? 0 : nullString.hashCode());</span> +<span class="fc bfc" id="L1090" title="All 2 branches covered."> result = prime * result + (ignoreSurroundingSpaces ? 1231 : 1237);</span> +<span class="fc bfc" id="L1091" title="All 2 branches covered."> result = prime * result + (ignoreHeaderCase ? 1231 : 1237);</span> +<span class="fc bfc" id="L1092" title="All 2 branches covered."> result = prime * result + (ignoreEmptyLines ? 1231 : 1237);</span> +<span class="fc bfc" id="L1093" title="All 2 branches covered."> result = prime * result + (skipHeaderRecord ? 1231 : 1237);</span> +<span class="fc bfc" id="L1094" title="All 2 branches covered."> result = prime * result + (allowDuplicateHeaderNames ? 1231 : 1237);</span> +<span class="fc bfc" id="L1095" title="All 2 branches covered."> result = prime * result + (trim ? 1231 : 1237);</span> +<span class="fc bfc" id="L1096" title="All 2 branches covered."> result = prime * result + (autoFlush ? 1231 : 1237);</span> +<span class="fc bfc" id="L1097" title="All 2 branches covered."> result = prime * result + (trailingDelimiter ? 1231 : 1237);</span> +<span class="fc bfc" id="L1098" title="All 2 branches covered."> result = prime * result + (allowMissingColumnNames ? 1231 : 1237);</span> +<span class="fc bfc" id="L1099" title="All 2 branches covered."> result = prime * result + ((recordSeparator == null) ? 0 : recordSeparator.hashCode());</span> +<span class="fc" id="L1100"> result = prime * result + Arrays.hashCode(header);</span> +<span class="fc" id="L1101"> result = prime * result + Arrays.hashCode(headerComments);</span> +<span class="fc" id="L1102"> return result;</span> + } + + /** + * Specifies whether comments are supported by this format. + * + * Note that the comment introducer character is only recognized at the start of a line. + * + * @return {@code true} is comments are supported, {@code false} otherwise + */ + public boolean isCommentMarkerSet() { +<span class="fc bfc" id="L1113" title="All 2 branches covered."> return commentMarker != null;</span> + } + + /** + * Returns whether escape are being processed. + * + * @return {@code true} if escapes are processed + */ + public boolean isEscapeCharacterSet() { +<span class="fc bfc" id="L1122" title="All 2 branches covered."> return escapeCharacter != null;</span> + } + + /** + * Returns whether a nullString has been defined. + * + * @return {@code true} if a nullString is defined + */ + public boolean isNullStringSet() { +<span class="fc bfc" id="L1131" title="All 2 branches covered."> return nullString != null;</span> + } + + /** + * Returns whether a quoteChar has been defined. + * + * @return {@code true} if a quoteChar is defined + */ + public boolean isQuoteCharacterSet() { +<span class="fc bfc" id="L1140" title="All 2 branches covered."> return quoteCharacter != null;</span> + } + + /** + * Parses the specified content. + * + * <p> + * See also the various static parse methods on {@link CSVParser}. + * </p> + * + * @param in + * the input stream + * @return a parser over a stream of {@link CSVRecord}s. + * @throws IOException + * If an I/O error occurs + */ + public CSVParser parse(final Reader in) throws IOException { +<span class="fc" id="L1157"> return new CSVParser(in, this);</span> + } + + /** + * Prints to the specified output. + * + * <p> + * See also {@link CSVPrinter}. + * </p> + * + * @param out + * the output. + * @return a printer to an output. + * @throws IOException + * thrown if the optional header cannot be printed. + */ + public CSVPrinter print(final Appendable out) throws IOException { +<span class="fc" id="L1174"> return new CSVPrinter(out, this);</span> + } + + /** + * Prints to the specified output. + * + * <p> + * See also {@link CSVPrinter}. + * </p> + * + * @param out + * the output. + * @param charset + * A charset. + * @return a printer to an output. + * @throws IOException + * thrown if the optional header cannot be printed. + * @since 1.5 + */ + @SuppressWarnings("resource") + public CSVPrinter print(final File out, final Charset charset) throws IOException { + // The writer will be closed when close() is called. +<span class="fc" id="L1196"> return new CSVPrinter(new OutputStreamWriter(new FileOutputStream(out), charset), this);</span> + } + + /** + * Prints the {@code value} as the next value on the line to {@code out}. The value will be escaped or encapsulated + * as needed. Useful when one wants to avoid creating CSVPrinters. + * Trims the value if {@link #getTrim()} is true + * @param value + * value to output. + * @param out + * where to print the value. + * @param newRecord + * if this a new record. + * @throws IOException + * If an I/O error occurs. + * @since 1.4 + */ + public void print(final Object value, final Appendable out, final boolean newRecord) throws IOException { + // null values are considered empty + // Only call CharSequence.toString() if you have to, helps GC-free use cases. + CharSequence charSequence; +<span class="fc bfc" id="L1217" title="All 2 branches covered."> if (value == null) {</span> + // https://issues.apache.org/jira/browse/CSV-203 +<span class="fc bfc" id="L1219" title="All 2 branches covered."> if (null == nullString) {</span> +<span class="fc" id="L1220"> charSequence = EMPTY;</span> + } else { +<span class="fc bfc" id="L1222" title="All 2 branches covered."> if (QuoteMode.ALL == quoteMode) {</span> +<span class="fc" id="L1223"> charSequence = quotedNullString;</span> + } else { +<span class="fc" id="L1225"> charSequence = nullString;</span> + } + } + } else { +<span class="fc bfc" id="L1229" title="All 2 branches covered."> if (value instanceof CharSequence) {</span> +<span class="fc" id="L1230"> charSequence = (CharSequence) value;</span> +<span class="fc bfc" id="L1231" title="All 2 branches covered."> } else if (value instanceof Reader) {</span> +<span class="fc" id="L1232"> print((Reader) value, out, newRecord);</span> +<span class="fc" id="L1233"> return;</span> + } else { +<span class="fc" id="L1235"> charSequence = value.toString();</span> + } + } +<span class="fc bfc" id="L1238" title="All 2 branches covered."> charSequence = getTrim() ? trim(charSequence) : charSequence;</span> +<span class="fc" id="L1239"> print(value, charSequence, out, newRecord);</span> +<span class="fc" id="L1240"> }</span> + + private void print(final Object object, final CharSequence value, final Appendable out, final boolean newRecord) + throws IOException { +<span class="fc" id="L1244"> final int offset = 0;</span> +<span class="fc" id="L1245"> final int len = value.length();</span> +<span class="fc bfc" id="L1246" title="All 2 branches covered."> if (!newRecord) {</span> +<span class="fc" id="L1247"> out.append(getDelimiter());</span> + } +<span class="fc bfc" id="L1249" title="All 2 branches covered."> if (object == null) {</span> +<span class="fc" id="L1250"> out.append(value);</span> +<span class="fc bfc" id="L1251" title="All 2 branches covered."> } else if (isQuoteCharacterSet()) {</span> + // the original object is needed so can check for Number +<span class="fc" id="L1253"> printWithQuotes(object, value, out, newRecord);</span> +<span class="fc bfc" id="L1254" title="All 2 branches covered."> } else if (isEscapeCharacterSet()) {</span> +<span class="fc" id="L1255"> printWithEscapes(value, out);</span> + } else { +<span class="fc" id="L1257"> out.append(value, offset, len);</span> + } +<span class="fc" id="L1259"> }</span> + + /** + * Prints to the specified output. + * + * <p> + * See also {@link CSVPrinter}. + * </p> + * + * @param out + * the output. + * @param charset + * A charset. + * @return a printer to an output. + * @throws IOException + * thrown if the optional header cannot be printed. + * @since 1.5 + */ + public CSVPrinter print(final Path out, final Charset charset) throws IOException { +<span class="fc" id="L1278"> return print(Files.newBufferedWriter(out, charset));</span> + } + + private void print(final Reader reader, final Appendable out, final boolean newRecord) throws IOException { + // Reader is never null +<span class="fc bfc" id="L1283" title="All 2 branches covered."> if (!newRecord) {</span> +<span class="fc" id="L1284"> out.append(getDelimiter());</span> + } +<span class="fc bfc" id="L1286" title="All 2 branches covered."> if (isQuoteCharacterSet()) {</span> +<span class="fc" id="L1287"> printWithQuotes(reader, out);</span> +<span class="pc bpc" id="L1288" title="1 of 2 branches missed."> } else if (isEscapeCharacterSet()) {</span> +<span class="nc" id="L1289"> printWithEscapes(reader, out);</span> +<span class="fc bfc" id="L1290" title="All 2 branches covered."> } else if (out instanceof Writer) {</span> +<span class="fc" id="L1291"> IOUtils.copyLarge(reader, (Writer) out);</span> + } else { +<span class="fc" id="L1293"> IOUtils.copy(reader, out);</span> + } + +<span class="fc" id="L1296"> }</span> + + /** + * Prints to the {@link System#out}. + * + * <p> + * See also {@link CSVPrinter}. + * </p> + * + * @return a printer to {@link System#out}. + * @throws IOException + * thrown if the optional header cannot be printed. + * @since 1.5 + */ + public CSVPrinter printer() throws IOException { +<span class="fc" id="L1311"> return new CSVPrinter(System.out, this);</span> + } + + /** + * Outputs the trailing delimiter (if set) followed by the record separator (if set). + * + * @param out + * where to write + * @throws IOException + * If an I/O error occurs + * @since 1.4 + */ + public void println(final Appendable out) throws IOException { +<span class="fc bfc" id="L1324" title="All 2 branches covered."> if (getTrailingDelimiter()) {</span> +<span class="fc" id="L1325"> out.append(getDelimiter());</span> + } +<span class="fc bfc" id="L1327" title="All 2 branches covered."> if (recordSeparator != null) {</span> +<span class="fc" id="L1328"> out.append(recordSeparator);</span> + } +<span class="fc" id="L1330"> }</span> + + /** + * Prints the given {@code values} to {@code out} as a single record of delimiter separated values followed by the + * record separator. + * + * <p> + * The values will be quoted if needed. Quotes and new-line characters will be escaped. This method adds the record + * separator to the output after printing the record, so there is no need to call {@link #println(Appendable)}. + * </p> + * + * @param out + * where to write. + * @param values + * values to output. + * @throws IOException + * If an I/O error occurs. + * @since 1.4 + */ + public void printRecord(final Appendable out, final Object... values) throws IOException { +<span class="fc bfc" id="L1350" title="All 2 branches covered."> for (int i = 0; i < values.length; i++) {</span> +<span class="fc bfc" id="L1351" title="All 2 branches covered."> print(values[i], out, i == 0);</span> + } +<span class="fc" id="L1353"> println(out);</span> +<span class="fc" id="L1354"> }</span> + + /* + * Note: must only be called if escaping is enabled, otherwise will generate NPE + */ + private void printWithEscapes(final CharSequence value, final Appendable out) throws IOException { +<span class="fc" id="L1360"> int start = 0;</span> +<span class="fc" id="L1361"> int pos = 0;</span> +<span class="fc" id="L1362"> final int len = value.length();</span> +<span class="fc" id="L1363"> final int end = len;</span> + +<span class="fc" id="L1365"> final char delim = getDelimiter();</span> +<span class="fc" id="L1366"> final char escape = getEscapeCharacter().charValue();</span> + +<span class="fc bfc" id="L1368" title="All 2 branches covered."> while (pos < end) {</span> +<span class="fc" id="L1369"> char c = value.charAt(pos);</span> +<span class="fc bfc" id="L1370" title="All 8 branches covered."> if (c == CR || c == LF || c == delim || c == escape) {</span> + // write out segment up until this char +<span class="fc bfc" id="L1372" title="All 2 branches covered."> if (pos > start) {</span> +<span class="fc" id="L1373"> out.append(value, start, pos);</span> + } +<span class="fc bfc" id="L1375" title="All 2 branches covered."> if (c == LF) {</span> +<span class="fc" id="L1376"> c = 'n';</span> +<span class="fc bfc" id="L1377" title="All 2 branches covered."> } else if (c == CR) {</span> +<span class="fc" id="L1378"> c = 'r';</span> + } + +<span class="fc" id="L1381"> out.append(escape);</span> +<span class="fc" id="L1382"> out.append(c);</span> + +<span class="fc" id="L1384"> start = pos + 1; // start on the current char after this one</span> + } +<span class="fc" id="L1386"> pos++;</span> +<span class="fc" id="L1387"> }</span> + + // write last segment +<span class="fc bfc" id="L1390" title="All 2 branches covered."> if (pos > start) {</span> +<span class="fc" id="L1391"> out.append(value, start, pos);</span> + } +<span class="fc" id="L1393"> }</span> + + private void printWithEscapes(final Reader reader, final Appendable out) throws IOException { +<span class="nc" id="L1396"> int start = 0;</span> +<span class="nc" id="L1397"> int pos = 0;</span> + +<span class="nc" id="L1399"> final char delim = getDelimiter();</span> +<span class="nc" id="L1400"> final char escape = getEscapeCharacter().charValue();</span> +<span class="nc" id="L1401"> final StringBuilder builder = new StringBuilder(IOUtils.DEFAULT_BUFFER_SIZE);</span> + + int c; +<span class="nc bnc" id="L1404" title="All 2 branches missed."> while (-1 != (c = reader.read())) {</span> +<span class="nc" id="L1405"> builder.append((char) c);</span> +<span class="nc bnc" id="L1406" title="All 8 branches missed."> if (c == CR || c == LF || c == delim || c == escape) {</span> + // write out segment up until this char +<span class="nc bnc" id="L1408" title="All 2 branches missed."> if (pos > start) {</span> +<span class="nc" id="L1409"> out.append(builder.substring(start, pos));</span> +<span class="nc" id="L1410"> builder.setLength(0);</span> + } +<span class="nc bnc" id="L1412" title="All 2 branches missed."> if (c == LF) {</span> +<span class="nc" id="L1413"> c = 'n';</span> +<span class="nc bnc" id="L1414" title="All 2 branches missed."> } else if (c == CR) {</span> +<span class="nc" id="L1415"> c = 'r';</span> + } + +<span class="nc" id="L1418"> out.append(escape);</span> +<span class="nc" id="L1419"> out.append((char) c);</span> + +<span class="nc" id="L1421"> start = pos + 1; // start on the current char after this one</span> + } +<span class="nc" id="L1423"> pos++;</span> + } + + // write last segment +<span class="nc bnc" id="L1427" title="All 2 branches missed."> if (pos > start) {</span> +<span class="nc" id="L1428"> out.append(builder.substring(start, pos));</span> + } +<span class="nc" id="L1430"> }</span> + + /* + * Note: must only be called if quoting is enabled, otherwise will generate NPE + */ + // the original object is needed so can check for Number + private void printWithQuotes(final Object object, final CharSequence value, final Appendable out, + final boolean newRecord) throws IOException { +<span class="fc" id="L1438"> boolean quote = false;</span> +<span class="fc" id="L1439"> int start = 0;</span> +<span class="fc" id="L1440"> int pos = 0;</span> +<span class="fc" id="L1441"> final int len = value.length();</span> +<span class="fc" id="L1442"> final int end = len;</span> + +<span class="fc" id="L1444"> final char delimChar = getDelimiter();</span> +<span class="fc" id="L1445"> final char quoteChar = getQuoteCharacter().charValue();</span> + // If escape char not specified, default to the quote char + // This avoids having to keep checking whether there is an escape character + // at the cost of checking against quote twice +<span class="fc bfc" id="L1449" title="All 2 branches covered."> final char escapeChar = isEscapeCharacterSet() ? getEscapeCharacter().charValue() : quoteChar;</span> + +<span class="fc" id="L1451"> QuoteMode quoteModePolicy = getQuoteMode();</span> +<span class="fc bfc" id="L1452" title="All 2 branches covered."> if (quoteModePolicy == null) {</span> +<span class="fc" id="L1453"> quoteModePolicy = QuoteMode.MINIMAL;</span> + } +<span class="pc bpc" id="L1455" title="1 of 5 branches missed."> switch (quoteModePolicy) {</span> + case ALL: + case ALL_NON_NULL: +<span class="fc" id="L1458"> quote = true;</span> +<span class="fc" id="L1459"> break;</span> + case NON_NUMERIC: +<span class="fc bfc" id="L1461" title="All 2 branches covered."> quote = !(object instanceof Number);</span> +<span class="fc" id="L1462"> break;</span> + case NONE: + // Use the existing escaping code +<span class="fc" id="L1465"> printWithEscapes(value, out);</span> +<span class="fc" id="L1466"> return;</span> + case MINIMAL: +<span class="fc bfc" id="L1468" title="All 2 branches covered."> if (len <= 0) {</span> + // always quote an empty token that is the first + // on the line, as it may be the only thing on the + // line. If it were not quoted in that case, + // an empty line has no tokens. +<span class="fc bfc" id="L1473" title="All 2 branches covered."> if (newRecord) {</span> +<span class="fc" id="L1474"> quote = true;</span> + } + } else { +<span class="fc" id="L1477"> char c = value.charAt(pos);</span> + +<span class="fc bfc" id="L1479" title="All 2 branches covered."> if (c <= COMMENT) {</span> + // Some other chars at the start of a value caused the parser to fail, so for now + // encapsulate if we start in anything less than '#'. We are being conservative + // by including the default comment char too. +<span class="fc" id="L1483"> quote = true;</span> + } else { +<span class="fc bfc" id="L1485" title="All 2 branches covered."> while (pos < end) {</span> +<span class="fc" id="L1486"> c = value.charAt(pos);</span> +<span class="fc bfc" id="L1487" title="All 10 branches covered."> if (c == LF || c == CR || c == quoteChar || c == delimChar || c == escapeChar) {</span> +<span class="fc" id="L1488"> quote = true;</span> +<span class="fc" id="L1489"> break;</span> + } +<span class="fc" id="L1491"> pos++;</span> + } + +<span class="fc bfc" id="L1494" title="All 2 branches covered."> if (!quote) {</span> +<span class="fc" id="L1495"> pos = end - 1;</span> +<span class="fc" id="L1496"> c = value.charAt(pos);</span> + // Some other chars at the end caused the parser to fail, so for now + // encapsulate if we end in anything less than ' ' +<span class="fc bfc" id="L1499" title="All 2 branches covered."> if (c <= SP) {</span> +<span class="fc" id="L1500"> quote = true;</span> + } + } + } + } + +<span class="fc bfc" id="L1506" title="All 2 branches covered."> if (!quote) {</span> + // no encapsulation needed - write out the original value +<span class="fc" id="L1508"> out.append(value, start, end);</span> +<span class="fc" id="L1509"> return;</span> + } + break; + default: +<span class="nc" id="L1513"> throw new IllegalStateException("Unexpected Quote value: " + quoteModePolicy);</span> + } + +<span class="fc bfc" id="L1516" title="All 2 branches covered."> if (!quote) {</span> + // no encapsulation needed - write out the original value +<span class="fc" id="L1518"> out.append(value, start, end);</span> +<span class="fc" id="L1519"> return;</span> + } + + // we hit something that needed encapsulation +<span class="fc" id="L1523"> out.append(quoteChar);</span> + + // Pick up where we left off: pos should be positioned on the first character that caused + // the need for encapsulation. +<span class="fc bfc" id="L1527" title="All 2 branches covered."> while (pos < end) {</span> +<span class="fc" id="L1528"> final char c = value.charAt(pos);</span> +<span class="fc bfc" id="L1529" title="All 4 branches covered."> if (c == quoteChar || c == escapeChar) {</span> + // write out the chunk up until this point +<span class="fc" id="L1531"> out.append(value, start, pos);</span> +<span class="fc" id="L1532"> out.append(escapeChar); // now output the escape</span> +<span class="fc" id="L1533"> start = pos; // and restart with the matched char</span> + } +<span class="fc" id="L1535"> pos++;</span> +<span class="fc" id="L1536"> }</span> + + // write the last segment +<span class="fc" id="L1539"> out.append(value, start, pos);</span> +<span class="fc" id="L1540"> out.append(quoteChar);</span> +<span class="fc" id="L1541"> }</span> + + /** + * Always use quotes unless QuoteMode is NONE, so we not have to look ahead. + * + * @throws IOException + */ + private void printWithQuotes(final Reader reader, final Appendable out) throws IOException { + +<span class="pc bpc" id="L1550" title="1 of 2 branches missed."> if (getQuoteMode() == QuoteMode.NONE) {</span> +<span class="nc" id="L1551"> printWithEscapes(reader, out);</span> +<span class="nc" id="L1552"> return;</span> + } + +<span class="fc" id="L1555"> int pos = 0;</span> + +<span class="fc" id="L1557"> final char quote = getQuoteCharacter().charValue();</span> +<span class="fc" id="L1558"> final StringBuilder builder = new StringBuilder(IOUtils.DEFAULT_BUFFER_SIZE);</span> + +<span class="fc" id="L1560"> out.append(quote);</span> + + int c; +<span class="fc bfc" id="L1563" title="All 2 branches covered."> while (-1 != (c = reader.read())) {</span> +<span class="fc" id="L1564"> builder.append((char) c);</span> +<span class="fc bfc" id="L1565" title="All 2 branches covered."> if (c == quote) {</span> + // write out segment up until this char +<span class="pc bpc" id="L1567" title="1 of 2 branches missed."> if (pos > 0) {</span> +<span class="fc" id="L1568"> out.append(builder.substring(0, pos));</span> +<span class="fc" id="L1569"> builder.setLength(0);</span> +<span class="fc" id="L1570"> pos = -1;</span> + } + +<span class="fc" id="L1573"> out.append(quote);</span> +<span class="fc" id="L1574"> out.append((char) c);</span> + } +<span class="fc" id="L1576"> pos++;</span> + } + + // write last segment +<span class="pc bpc" id="L1580" title="1 of 2 branches missed."> if (pos > 0) {</span> +<span class="fc" id="L1581"> out.append(builder.substring(0, pos));</span> + } + +<span class="fc" id="L1584"> out.append(quote);</span> +<span class="fc" id="L1585"> }</span> + + @Override + public String toString() { +<span class="fc" id="L1589"> final StringBuilder sb = new StringBuilder();</span> +<span class="fc" id="L1590"> sb.append("Delimiter=<").append(delimiter).append('>');</span> +<span class="fc bfc" id="L1591" title="All 2 branches covered."> if (isEscapeCharacterSet()) {</span> +<span class="fc" id="L1592"> sb.append(' ');</span> +<span class="fc" id="L1593"> sb.append("Escape=<").append(escapeCharacter).append('>');</span> + } +<span class="pc bpc" id="L1595" title="1 of 2 branches missed."> if (isQuoteCharacterSet()) {</span> +<span class="fc" id="L1596"> sb.append(' ');</span> +<span class="fc" id="L1597"> sb.append("QuoteChar=<").append(quoteCharacter).append('>');</span> + } +<span class="pc bpc" id="L1599" title="1 of 2 branches missed."> if (quoteMode != null) {</span> +<span class="nc" id="L1600"> sb.append(' ');</span> +<span class="nc" id="L1601"> sb.append("QuoteMode=<").append(quoteMode).append('>');</span> + } +<span class="fc bfc" id="L1603" title="All 2 branches covered."> if (isCommentMarkerSet()) {</span> +<span class="fc" id="L1604"> sb.append(' ');</span> +<span class="fc" id="L1605"> sb.append("CommentStart=<").append(commentMarker).append('>');</span> + } +<span class="pc bpc" id="L1607" title="1 of 2 branches missed."> if (isNullStringSet()) {</span> +<span class="nc" id="L1608"> sb.append(' ');</span> +<span class="nc" id="L1609"> sb.append("NullString=<").append(nullString).append('>');</span> + } +<span class="fc bfc" id="L1611" title="All 2 branches covered."> if (recordSeparator != null) {</span> +<span class="fc" id="L1612"> sb.append(' ');</span> +<span class="fc" id="L1613"> sb.append("RecordSeparator=<").append(recordSeparator).append('>');</span> + } +<span class="fc bfc" id="L1615" title="All 2 branches covered."> if (getIgnoreEmptyLines()) {</span> +<span class="fc" id="L1616"> sb.append(" EmptyLines:ignored");</span> + } +<span class="fc bfc" id="L1618" title="All 2 branches covered."> if (getIgnoreSurroundingSpaces()) {</span> +<span class="fc" id="L1619"> sb.append(" SurroundingSpaces:ignored");</span> + } +<span class="pc bpc" id="L1621" title="1 of 2 branches missed."> if (getIgnoreHeaderCase()) {</span> +<span class="nc" id="L1622"> sb.append(" IgnoreHeaderCase:ignored");</span> + } +<span class="fc" id="L1624"> sb.append(" SkipHeaderRecord:").append(skipHeaderRecord);</span> +<span class="pc bpc" id="L1625" title="1 of 2 branches missed."> if (headerComments != null) {</span> +<span class="nc" id="L1626"> sb.append(' ');</span> +<span class="nc" id="L1627"> sb.append("HeaderComments:").append(Arrays.toString(headerComments));</span> + } +<span class="pc bpc" id="L1629" title="1 of 2 branches missed."> if (header != null) {</span> +<span class="nc" id="L1630"> sb.append(' ');</span> +<span class="nc" id="L1631"> sb.append("Header:").append(Arrays.toString(header));</span> + } +<span class="fc" id="L1633"> return sb.toString();</span> + } + + private String[] toStringArray(final Object[] values) { +<span class="fc bfc" id="L1637" title="All 2 branches covered."> if (values == null) {</span> +<span class="fc" id="L1638"> return null;</span> + } +<span class="fc" id="L1640"> final String[] strings = new String[values.length];</span> +<span class="fc bfc" id="L1641" title="All 2 branches covered."> for (int i = 0; i < values.length; i++) {</span> +<span class="fc" id="L1642"> final Object value = values[i];</span> +<span class="fc bfc" id="L1643" title="All 2 branches covered."> strings[i] = value == null ? null : value.toString();</span> + } +<span class="fc" id="L1645"> return strings;</span> + } + + private CharSequence trim(final CharSequence charSequence) { +<span class="pc bpc" id="L1649" title="1 of 2 branches missed."> if (charSequence instanceof String) {</span> +<span class="fc" id="L1650"> return ((String) charSequence).trim();</span> + } +<span class="nc" id="L1652"> final int count = charSequence.length();</span> +<span class="nc" id="L1653"> int len = count;</span> +<span class="nc" id="L1654"> int pos = 0;</span> + +<span class="nc bnc" id="L1656" title="All 4 branches missed."> while (pos < len && charSequence.charAt(pos) <= SP) {</span> +<span class="nc" id="L1657"> pos++;</span> + } +<span class="nc bnc" id="L1659" title="All 4 branches missed."> while (pos < len && charSequence.charAt(len - 1) <= SP) {</span> +<span class="nc" id="L1660"> len--;</span> + } +<span class="nc bnc" id="L1662" title="All 4 branches missed."> return pos > 0 || len < count ? charSequence.subSequence(pos, len) : charSequence;</span> + } + + /** + * Verifies the consistency of the parameters and throws an IllegalArgumentException if necessary. + * + * @throws IllegalArgumentException + */ + private void validate() throws IllegalArgumentException { +<span class="pc bpc" id="L1671" title="1 of 2 branches missed."> if (isLineBreak(delimiter)) {</span> +<span class="nc" id="L1672"> throw new IllegalArgumentException("The delimiter cannot be a line break");</span> + } + +<span class="fc bfc" id="L1675" title="All 4 branches covered."> if (quoteCharacter != null && delimiter == quoteCharacter.charValue()) {</span> +<span class="fc" id="L1676"> throw new IllegalArgumentException(</span> + "The quoteChar character and the delimiter cannot be the same ('" + quoteCharacter + "')"); + } + +<span class="fc bfc" id="L1680" title="All 4 branches covered."> if (escapeCharacter != null && delimiter == escapeCharacter.charValue()) {</span> +<span class="fc" id="L1681"> throw new IllegalArgumentException(</span> + "The escape character and the delimiter cannot be the same ('" + escapeCharacter + "')"); + } + +<span class="fc bfc" id="L1685" title="All 4 branches covered."> if (commentMarker != null && delimiter == commentMarker.charValue()) {</span> +<span class="fc" id="L1686"> throw new IllegalArgumentException(</span> + "The comment start character and the delimiter cannot be the same ('" + commentMarker + "')"); + } + +<span class="fc bfc" id="L1690" title="All 4 branches covered."> if (quoteCharacter != null && quoteCharacter.equals(commentMarker)) {</span> +<span class="fc" id="L1691"> throw new IllegalArgumentException(</span> + "The comment start character and the quoteChar cannot be the same ('" + commentMarker + "')"); + } + +<span class="fc bfc" id="L1695" title="All 4 branches covered."> if (escapeCharacter != null && escapeCharacter.equals(commentMarker)) {</span> +<span class="fc" id="L1696"> throw new IllegalArgumentException(</span> + "The comment start and the escape character cannot be the same ('" + commentMarker + "')"); + } + +<span class="fc bfc" id="L1700" title="All 4 branches covered."> if (escapeCharacter == null && quoteMode == QuoteMode.NONE) {</span> +<span class="fc" id="L1701"> throw new IllegalArgumentException("No quotes mode set but no escape character is set");</span> + } + + // validate header +<span class="fc bfc" id="L1705" title="All 4 branches covered."> if (header != null && !allowDuplicateHeaderNames) {</span> +<span class="fc" id="L1706"> final Set<String> dupCheck = new HashSet<>();</span> +<span class="fc bfc" id="L1707" title="All 2 branches covered."> for (final String hdr : header) {</span> +<span class="fc bfc" id="L1708" title="All 2 branches covered."> if (!dupCheck.add(hdr)) {</span> +<span class="fc" id="L1709"> throw new IllegalArgumentException(</span> +<span class="fc" id="L1710"> "The header contains a duplicate entry: '" + hdr + "' in " + Arrays.toString(header));</span> + } + } + } +<span class="fc" id="L1714"> }</span> + + /** + * Returns a new {@code CSVFormat} that allows duplicate header names. + * + * @return a new {@code CSVFormat} that allows duplicate header names + * @since 1.7 + */ + public CSVFormat withAllowDuplicateHeaderNames() { +<span class="fc" id="L1723"> return withAllowDuplicateHeaderNames(true);</span> + } + + /** + * Returns a new {@code CSVFormat} with duplicate header names behavior set to the given value. + * + * @param allowDuplicateHeaderNames the duplicate header names behavior, true to allow, false to disallow. + * @return a new {@code CSVFormat} with duplicate header names behavior set to the given value. + * @since 1.7 + */ + public CSVFormat withAllowDuplicateHeaderNames(final boolean allowDuplicateHeaderNames) { +<span class="fc" id="L1734"> return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,</span> + ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header, + skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush, + allowDuplicateHeaderNames); + } + + /** + * Returns a new {@code CSVFormat} with the missing column names behavior of the format set to {@code true} + * + * @return A new CSVFormat that is equal to this but with the specified missing column names behavior. + * @see #withAllowMissingColumnNames(boolean) + * @since 1.1 + */ + public CSVFormat withAllowMissingColumnNames() { +<span class="fc" id="L1748"> return this.withAllowMissingColumnNames(true);</span> + } + + /** + * Returns a new {@code CSVFormat} with the missing column names behavior of the format set to the given value. + * + * @param allowMissingColumnNames + * the missing column names behavior, {@code true} to allow missing column names in the header line, + * {@code false} to cause an {@link IllegalArgumentException} to be thrown. + * @return A new CSVFormat that is equal to this but with the specified missing column names behavior. + */ + public CSVFormat withAllowMissingColumnNames(final boolean allowMissingColumnNames) { +<span class="fc" id="L1760"> return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,</span> + ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header, + skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush, + allowDuplicateHeaderNames); + } + + /** + * Returns a new {@code CSVFormat} with whether to flush on close. + * + * @param autoFlush + * whether to flush on close. + * + * @return A new CSVFormat that is equal to this but with the specified autoFlush setting. + * @since 1.6 + */ + public CSVFormat withAutoFlush(final boolean autoFlush) { +<span class="fc" id="L1776"> return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,</span> + ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header, + skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush, + allowDuplicateHeaderNames); + } + + /** + * Returns a new {@code CSVFormat} with the comment start marker of the format set to the specified character. + * + * Note that the comment start character is only recognized at the start of a line. + * + * @param commentMarker + * the comment start marker + * @return A new CSVFormat that is equal to this one but with the specified character as the comment start marker + * @throws IllegalArgumentException + * thrown if the specified character is a line break + */ + public CSVFormat withCommentMarker(final char commentMarker) { +<span class="fc" id="L1794"> return withCommentMarker(Character.valueOf(commentMarker));</span> + } + + /** + * Returns a new {@code CSVFormat} with the comment start marker of the format set to the specified character. + * + * Note that the comment start character is only recognized at the start of a line. + * + * @param commentMarker + * the comment start marker, use {@code null} to disable + * @return A new CSVFormat that is equal to this one but with the specified character as the comment start marker + * @throws IllegalArgumentException + * thrown if the specified character is a line break + */ + public CSVFormat withCommentMarker(final Character commentMarker) { +<span class="fc bfc" id="L1809" title="All 2 branches covered."> if (isLineBreak(commentMarker)) {</span> +<span class="fc" id="L1810"> throw new IllegalArgumentException("The comment start marker character cannot be a line break");</span> + } +<span class="fc" id="L1812"> return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,</span> + ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header, + skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush, + allowDuplicateHeaderNames); + } + + /** + * Returns a new {@code CSVFormat} with the delimiter of the format set to the specified character. + * + * @param delimiter + * the delimiter character + * @return A new CSVFormat that is equal to this with the specified character as delimiter + * @throws IllegalArgumentException + * thrown if the specified character is a line break + */ + public CSVFormat withDelimiter(final char delimiter) { +<span class="fc bfc" id="L1828" title="All 2 branches covered."> if (isLineBreak(delimiter)) {</span> +<span class="fc" id="L1829"> throw new IllegalArgumentException("The delimiter cannot be a line break");</span> + } +<span class="fc" id="L1831"> return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,</span> + ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header, + skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush, + allowDuplicateHeaderNames); + } + + /** + * Returns a new {@code CSVFormat} with the escape character of the format set to the specified character. + * + * @param escape + * the escape character + * @return A new CSVFormat that is equal to his but with the specified character as the escape character + * @throws IllegalArgumentException + * thrown if the specified character is a line break + */ + public CSVFormat withEscape(final char escape) {
[... 486 lines stripped ...]