CSVParserBuilder.java

  1. /*
  2.  Copyright 2005 Bytecode Pty Ltd.

  3.  Licensed under the Apache License, Version 2.0 (the "License");
  4.  you may not use this file except in compliance with the License.
  5.  You may obtain a copy of the License at

  6.  http://www.apache.org/licenses/LICENSE-2.0

  7.  Unless required by applicable law or agreed to in writing, software
  8.  distributed under the License is distributed on an "AS IS" BASIS,
  9.  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10.  See the License for the specific language governing permissions and
  11.  limitations under the License.
  12.  */
  13. package com.opencsv;


  14. import com.opencsv.enums.CSVReaderNullFieldIndicator;
  15. import org.apache.commons.lang3.ObjectUtils;

  16. import java.util.Locale;

  17. /**
  18.  * Builder for creating a CSVParser.
  19.  * <p>Example code for using this class:<br><br>
  20.  * <code>
  21.  * final CSVParser parser =<br>
  22.  * new CSVParserBuilder()<br>
  23.  * .withSeparator('\t')<br>
  24.  * .withIgnoreQuotations(true)<br>
  25.  * .build();<br>
  26.  * </code></p>
  27.  *
  28.  * @see CSVParser
  29.  */
  30. public class CSVParserBuilder {

  31.     private char separator = ICSVParser.DEFAULT_SEPARATOR;
  32.     private char quoteChar = ICSVParser.DEFAULT_QUOTE_CHARACTER;
  33.     private char escapeChar = ICSVParser.DEFAULT_ESCAPE_CHARACTER;
  34.     private boolean strictQuotes = ICSVParser.DEFAULT_STRICT_QUOTES;
  35.     private boolean ignoreLeadingWhiteSpace = ICSVParser.DEFAULT_IGNORE_LEADING_WHITESPACE;
  36.     private boolean ignoreQuotations = ICSVParser.DEFAULT_IGNORE_QUOTATIONS;
  37.     private CSVReaderNullFieldIndicator nullFieldIndicator = CSVReaderNullFieldIndicator.NEITHER;
  38.     private Locale errorLocale = Locale.getDefault();


  39.     /**
  40.      * Default constructor.
  41.      */
  42.     public CSVParserBuilder() {
  43.     }

  44.     /**
  45.      * Sets the delimiter to use for separating entries.
  46.      *
  47.      * @param separator The delimiter to use for separating entries
  48.      * @return The CSVParserBuilder
  49.      */
  50.     public CSVParserBuilder withSeparator(
  51.             final char separator) {
  52.         this.separator = separator;
  53.         return this;
  54.     }


  55.     /**
  56.      * Sets the character to use for quoted elements.
  57.      *
  58.      * @param quoteChar The character to use for quoted element.
  59.      * @return The CSVParserBuilder
  60.      */
  61.     public CSVParserBuilder withQuoteChar(
  62.             final char quoteChar) {
  63.         this.quoteChar = quoteChar;
  64.         return this;
  65.     }


  66.     /**
  67.      * Sets the character to use for escaping a separator or quote.
  68.      *
  69.      * @param escapeChar The character to use for escaping a separator or quote.
  70.      * @return The CSVParserBuilder
  71.      */
  72.     public CSVParserBuilder withEscapeChar(
  73.             final char escapeChar) {
  74.         this.escapeChar = escapeChar;
  75.         return this;
  76.     }


  77.     /**
  78.      * Sets the strict quotes setting - if true, characters
  79.      * outside the quotes are ignored.
  80.      *
  81.      * @param strictQuotes If true, characters outside the quotes are ignored
  82.      * @return The CSVParserBuilder
  83.      */
  84.     public CSVParserBuilder withStrictQuotes(
  85.             final boolean strictQuotes) {
  86.         this.strictQuotes = strictQuotes;
  87.         return this;
  88.     }

  89.     /**
  90.      * Sets the ignore leading whitespace setting - if true, white space
  91.      * in front of a quote in a field is ignored.
  92.      *
  93.      * @param ignoreLeadingWhiteSpace If true, white space in front of a quote in a field is ignored
  94.      * @return The CSVParserBuilder
  95.      */
  96.     public CSVParserBuilder withIgnoreLeadingWhiteSpace(
  97.             final boolean ignoreLeadingWhiteSpace) {
  98.         this.ignoreLeadingWhiteSpace = ignoreLeadingWhiteSpace;
  99.         return this;
  100.     }

  101.     /**
  102.      * Sets the ignore quotations mode - if true, quotations are ignored.
  103.      *
  104.      * @param ignoreQuotations If true, quotations are ignored
  105.      * @return The CSVParserBuilder
  106.      */
  107.     public CSVParserBuilder withIgnoreQuotations(
  108.             final boolean ignoreQuotations) {
  109.         this.ignoreQuotations = ignoreQuotations;
  110.         return this;
  111.     }

  112.     /**
  113.      * Constructs CSVParser.
  114.      *
  115.      * @return A new CSVParser with defined settings.
  116.      */
  117.     public CSVParser build() {

  118.         return new CSVParser(
  119.                 separator,
  120.                 quoteChar,
  121.                 escapeChar,
  122.                 strictQuotes,
  123.                 ignoreLeadingWhiteSpace,
  124.                 ignoreQuotations,
  125.                 nullFieldIndicator,
  126.                 errorLocale);
  127.     }

  128.     /**
  129.      * @return The defined separator.
  130.      */
  131.     public char getSeparator() {
  132.         return separator;
  133.     }

  134.     /**
  135.      * @return The defined quotation character.
  136.      */
  137.     public char getQuoteChar() {
  138.         return quoteChar;
  139.     }

  140.     /**
  141.      * @return The defined escape character.
  142.      */
  143.     public char getEscapeChar() {
  144.         return escapeChar;
  145.     }

  146.     /**
  147.      * @return The defined strict quotation setting.
  148.      */
  149.     public boolean isStrictQuotes() {
  150.         return strictQuotes;
  151.     }

  152.     /**
  153.      * @return The defined ignoreLeadingWhiteSpace setting.
  154.      */
  155.     public boolean isIgnoreLeadingWhiteSpace() {
  156.         return ignoreLeadingWhiteSpace;
  157.     }

  158.     /**
  159.      * @return The defined ignoreQuotation setting.
  160.      */
  161.     public boolean isIgnoreQuotations() {
  162.         return ignoreQuotations;
  163.     }

  164.     /**
  165.      * Sets the NullFieldIndicator.
  166.      *
  167.      * @param fieldIndicator CSVReaderNullFieldIndicator set to what should be considered a null field.
  168.      * @return The CSVParserBuilder
  169.      */
  170.     public CSVParserBuilder withFieldAsNull(final CSVReaderNullFieldIndicator fieldIndicator) {
  171.         this.nullFieldIndicator = fieldIndicator;
  172.         return this;
  173.     }
  174.    
  175.     /**
  176.      * Sets the locale for all error messages.
  177.      *
  178.      * @param errorLocale Locale for error messages
  179.      * @return {@code this}
  180.      * @since 4.0
  181.      */
  182.     public CSVParserBuilder withErrorLocale(Locale errorLocale) {
  183.         this.errorLocale = ObjectUtils.defaultIfNull(errorLocale, Locale.getDefault());
  184.         return this;
  185.     }
  186.    
  187.     /**
  188.      * @return The null field indicator.
  189.      */
  190.     public CSVReaderNullFieldIndicator nullFieldIndicator() {
  191.         return nullFieldIndicator;
  192.     }
  193.    
  194. }