com.univocity.parsers.csv.CsvParserSettings Maven / Gradle / Ivy
Show all versions of univocity-parsers Show documentation
/*******************************************************************************
* Copyright 2014 uniVocity Software Pty Ltd
*
* 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 com.univocity.parsers.csv;
import java.util.*;
import com.univocity.parsers.common.*;
import com.univocity.parsers.common.input.*;
/**
* This is the configuration class used by the CSV parser ({@link CsvParser})
*
* In addition to the configuration options provided by {@link CommonParserSettings}, the CSVParserSettings include:
*
*
* - emptyValue (defaults to null): Defines a replacement string to signify an empty value (which is not a null value)
*
When reading, if the parser does not read any character from the input, and the input is within quotes, the empty is used instead of an empty string
*
*
* @see com.univocity.parsers.csv.CsvParser
* @see com.univocity.parsers.csv.CsvFormat
* @see com.univocity.parsers.common.CommonParserSettings
*
* @author uniVocity Software Pty Ltd - [email protected]
*
*/
public class CsvParserSettings extends CommonParserSettings {
private String emptyValue = null;
private boolean parseUnescapedQuotes = true;
/**
* Returns the String representation of an empty value (defaults to null)
*
* When reading, if the parser does not read any character from the input, and the input is within quotes, the empty is used instead of an empty string
*
* @return the String representation of an empty value
*/
public String getEmptyValue() {
return emptyValue;
}
/**
* Sets the String representation of an empty value (defaults to null)
*
*
When reading, if the parser does not read any character from the input, and the input is within quotes, the empty is used instead of an empty string
*
* @param emptyValue the String representation of an empty value
*/
public void setEmptyValue(String emptyValue) {
this.emptyValue = emptyValue;
}
/**
* Returns an instance of CharAppender with the configured limit of maximum characters per column and the default value used to represent an empty value (when the String parsed from the input, within quotes, is empty)
*
*
This overrides the parent's version because the CSV parser does not rely on the appender to identify null values, but on the other hand, the appender is required to identify empty values.
*
* @return an instance of CharAppender with the configured limit of maximum characters per column and the default value used to represent an empty value (when the String parsed from the input, within quotes, is empty)
*/
@Override
protected CharAppender newCharAppender() {
return new DefaultCharAppender(getMaxCharsPerColumn(), emptyValue);
}
/**
* Returns the default CsvFormat configured to handle CSV inputs compliant to the RFC4180 standard.
* @return and instance of CsvFormat configured to handle CSV inputs compliant to the RFC4180 standard.
*/
@Override
protected CsvFormat createDefaultFormat() {
return new CsvFormat();
}
/**
* Indicates whether the CSV parser should accept unescaped quotes inside quoted values and parse them normally. Defaults to {@code true}.
* @return a flag indicating whether or not the CSV parser should accept unescaped quotes inside quoted values.
*/
public boolean isParseUnescapedQuotes() {
return parseUnescapedQuotes;
}
/**
* Configures how to handle unescaped quotes inside quoted values. If set to {@code true}, the parser will parse the quote normally as part of the value.
* If set the {@code false}, a {@link TextParsingException} will be thrown. Defaults to {@code true}.
* @param parseUnescapedQuotes indicates whether or not the CSV parser should accept unescaped quotes inside quoted values.
*/
public void setParseUnescapedQuotes(boolean parseUnescapedQuotes) {
this.parseUnescapedQuotes = parseUnescapedQuotes;
}
@Override
protected void addConfiguration(Map out) {
super.addConfiguration(out);
out.put("Empty value", emptyValue);
out.put("Parse unescaped quotes", parseUnescapedQuotes);
}
}