Package com.opencsv
Class AbstractCSVParser
- java.lang.Object
-
- com.opencsv.AbstractCSVParser
-
- All Implemented Interfaces:
ICSVParser
- Direct Known Subclasses:
CSVParser
,RFC4180Parser
public abstract class AbstractCSVParser extends java.lang.Object implements ICSVParser
The purpose of the AbstractCSVParser is to consolidate the duplicate code amongst the parsers.
-
-
Field Summary
Fields Modifier and Type Field Description protected static java.lang.StringBuilder
EMPTY_STRINGBUILDER
Empty StringBuilderprotected CSVReaderNullFieldIndicator
nullFieldIndicator
Determines the handling of null fields.protected java.lang.String
pending
Value to be appended to string to process.protected char
quotechar
This is the character that the CSVParser will treat as the quotation character.protected java.lang.String
quotecharAsString
This is the quotechar in String form to reduce the number of calls to toString.protected java.lang.String
quoteDoubledAsString
This is quotecharAsString+quotecharAsString - used in replaceAll to reduce the number of strings being created.protected java.util.regex.Pattern
quoteMatcherPattern
pattern created to match quotechars - optimizaion of the String.replaceAll.protected char
separator
This is the character that the CSVParser will treat as the separator.protected java.lang.String
separatorAsString
This is the separator in Stirng form to reduce the number of calls to toString.protected static java.util.regex.Pattern
SPECIAL_REGEX_CHARS
This is needed by the split command in case the separator character is a regex special character.-
Fields inherited from interface com.opencsv.ICSVParser
DEFAULT_BUNDLE_NAME, DEFAULT_ESCAPE_CHARACTER, DEFAULT_IGNORE_LEADING_WHITESPACE, DEFAULT_IGNORE_QUOTATIONS, DEFAULT_NULL_FIELD_INDICATOR, DEFAULT_QUOTE_CHARACTER, DEFAULT_SEPARATOR, DEFAULT_STRICT_QUOTES, INITIAL_READ_SIZE, MAX_SIZE_FOR_EMPTY_FIELD, NEWLINE, NULL_CHARACTER, READ_BUFFER_SIZE
-
-
Constructor Summary
Constructors Constructor Description AbstractCSVParser(char separator, char quotechar, CSVReaderNullFieldIndicator nullFieldIndicator)
Common constructor.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract java.lang.String
convertToCsvValue(java.lang.String value, boolean applyQuotestoAll)
Used when reverse parsing an array of strings to a single string.protected void
convertToCsvValue(java.lang.String value, boolean applyQuotesToAll, java.lang.Appendable appendable)
Used when reverse parsing an array of strings to a single string.java.lang.String
getPendingText()
If a parser is in the middle of parsing a multiline field, this will return the text collected so far.char
getQuotechar()
java.lang.String
getQuotecharAsString()
char
getSeparator()
java.lang.String
getSeparatorAsString()
boolean
isPending()
protected boolean
isSurroundWithQuotes(java.lang.String value, boolean forceSurround)
Used by reverse parsing to determine if a value should be surrounded by quote characters.CSVReaderNullFieldIndicator
nullFieldIndicator()
java.lang.String[]
parseLine(java.lang.String nextLine)
Parses an incoming String and returns an array of elements.protected abstract java.lang.String[]
parseLine(java.lang.String nextLine, boolean multi)
Parses an incomingString
and returns an array of elements.java.lang.String[]
parseLineMulti(java.lang.String nextLine)
Parses an incoming String and returns an array of elements.java.lang.String
parseToLine(java.lang.String[] values, boolean applyQuotesToAll)
Essentially a "Reverse parse" where an array of values are concatenating to a csv delimited string.void
parseToLine(java.lang.String[] values, boolean applyQuotesToAll, java.lang.Appendable appendable)
Essentially a "Reverse parse" where an array of values are concatenating to a csv delimited string.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.opencsv.ICSVParser
setErrorLocale
-
-
-
-
Field Detail
-
SPECIAL_REGEX_CHARS
protected static final java.util.regex.Pattern SPECIAL_REGEX_CHARS
This is needed by the split command in case the separator character is a regex special character.
-
EMPTY_STRINGBUILDER
protected static final java.lang.StringBuilder EMPTY_STRINGBUILDER
Empty StringBuilder
-
separator
protected final char separator
This is the character that the CSVParser will treat as the separator.
-
separatorAsString
protected final java.lang.String separatorAsString
This is the separator in Stirng form to reduce the number of calls to toString.
-
quotechar
protected final char quotechar
This is the character that the CSVParser will treat as the quotation character.
-
quotecharAsString
protected final java.lang.String quotecharAsString
This is the quotechar in String form to reduce the number of calls to toString.
-
quoteDoubledAsString
protected final java.lang.String quoteDoubledAsString
This is quotecharAsString+quotecharAsString - used in replaceAll to reduce the number of strings being created.
-
quoteMatcherPattern
protected final java.util.regex.Pattern quoteMatcherPattern
pattern created to match quotechars - optimizaion of the String.replaceAll.
-
nullFieldIndicator
protected final CSVReaderNullFieldIndicator nullFieldIndicator
Determines the handling of null fields.- See Also:
CSVReaderNullFieldIndicator
-
pending
protected java.lang.String pending
Value to be appended to string to process.
-
-
Constructor Detail
-
AbstractCSVParser
public AbstractCSVParser(char separator, char quotechar, CSVReaderNullFieldIndicator nullFieldIndicator)
Common constructor.- Parameters:
separator
- The delimiter to use for separating entriesquotechar
- The character to use for quoted elementsnullFieldIndicator
- Indicate what should be considered null
-
-
Method Detail
-
getSeparator
public char getSeparator()
- Specified by:
getSeparator
in interfaceICSVParser
- Returns:
- The default separator for this parser.
-
getSeparatorAsString
public java.lang.String getSeparatorAsString()
- Returns:
- String version of separator to reduce number of calls to toString.
-
getQuotechar
public char getQuotechar()
- Specified by:
getQuotechar
in interfaceICSVParser
- Returns:
- The default quotation character for this parser.
-
getQuotecharAsString
public java.lang.String getQuotecharAsString()
- Returns:
- String version of quotechar to reduce the number of calls to toString.
-
isPending
public boolean isPending()
- Specified by:
isPending
in interfaceICSVParser
- Returns:
- True if something was left over from last call(s)
-
parseLineMulti
public java.lang.String[] parseLineMulti(java.lang.String nextLine) throws java.io.IOException
Description copied from interface:ICSVParser
Parses an incoming String and returns an array of elements. This method is used when the data spans multiple lines.- Specified by:
parseLineMulti
in interfaceICSVParser
- Parameters:
nextLine
- Current line to be processed- Returns:
- The comma-tokenized list of elements, or null if nextLine is null
- Throws:
java.io.IOException
- If bad things happen during the read
-
parseLine
public java.lang.String[] parseLine(java.lang.String nextLine) throws java.io.IOException
Description copied from interface:ICSVParser
Parses an incoming String and returns an array of elements. This method is used when all data is contained in a single line.- Specified by:
parseLine
in interfaceICSVParser
- Parameters:
nextLine
- Line to be parsed.- Returns:
- The list of elements, or null if nextLine is null
- Throws:
java.io.IOException
- If bad things happen during the read
-
parseToLine
public java.lang.String parseToLine(java.lang.String[] values, boolean applyQuotesToAll)
Description copied from interface:ICSVParser
Essentially a "Reverse parse" where an array of values are concatenating to a csv delimited string.- Specified by:
parseToLine
in interfaceICSVParser
- Parameters:
values
- List of elements to parse.applyQuotesToAll
- - If true all strings in the array will have quotes if it needs it or not. If false then it will only have quotes if it needs it (i.e. contains a quote character).- Returns:
- CSV formatted string representing the values in the array.
-
parseToLine
public void parseToLine(java.lang.String[] values, boolean applyQuotesToAll, java.lang.Appendable appendable) throws java.io.IOException
Description copied from interface:ICSVParser
Essentially a "Reverse parse" where an array of values are concatenating to a csv delimited string.NOTE: This functionality is for testing only in the 5.7.2 release in an effort to optimize the number of strings created when parsing large files.
- Specified by:
parseToLine
in interfaceICSVParser
- Parameters:
values
- List of elements to parse.applyQuotesToAll
- - If true all strings in the array will have quotes if it needs it or not. If false then it will only have quotes if it needs it (i.e. contains a quote character).appendable
- The Appendable that the parsed values will be appended to- Throws:
java.io.IOException
-
convertToCsvValue
protected abstract java.lang.String convertToCsvValue(java.lang.String value, boolean applyQuotestoAll)
Used when reverse parsing an array of strings to a single string. Handles the application of quotes around the string and handling any quotes within the string.- Parameters:
value
- String to be convertedapplyQuotestoAll
- All values should be surrounded with quotes- Returns:
- String that will go into the CSV string
-
convertToCsvValue
protected void convertToCsvValue(java.lang.String value, boolean applyQuotesToAll, java.lang.Appendable appendable) throws java.io.IOException
Used when reverse parsing an array of strings to a single string. Handles the application of quotes around the string and handling any quotes within the string.NOTE: as of 5.7.2 most objects will be inheriting a solution that calls the existing convertToCsvValue and thus will not receive much benefit.
- Parameters:
value
- String to be convertedapplyQuotesToAll
- All values should be surrounded with quotesappendable
- Appendable object that the converted values are added to.- Throws:
java.io.IOException
-
isSurroundWithQuotes
protected boolean isSurroundWithQuotes(java.lang.String value, boolean forceSurround)
Used by reverse parsing to determine if a value should be surrounded by quote characters.- Parameters:
value
- String to be testedforceSurround
- If the value is notnull
it will be surrounded with quotes- Returns:
- True if the string should be surrounded with quotes, false otherwise
-
parseLine
protected abstract java.lang.String[] parseLine(java.lang.String nextLine, boolean multi) throws java.io.IOException
Parses an incomingString
and returns an array of elements.- Parameters:
nextLine
- The string to parsemulti
- Whether it takes multiple lines to form a single record- Returns:
- The list of elements, or
null
ifnextLine
isnull
- Throws:
java.io.IOException
- If bad things happen during the read
-
nullFieldIndicator
public CSVReaderNullFieldIndicator nullFieldIndicator()
- Specified by:
nullFieldIndicator
in interfaceICSVParser
- Returns:
- The null field indicator.
-
getPendingText
public java.lang.String getPendingText()
Description copied from interface:ICSVParser
If a parser is in the middle of parsing a multiline field, this will return the text collected so far.- Specified by:
getPendingText
in interfaceICSVParser
- Returns:
- The incomplete text for a multiline field. If there is no pending text, this returns an empty string.
-
-