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 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 char
separator
This is the character that the CSVParser will treat as the separator.-
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.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()
char
getSeparator()
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.-
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
-
separator
protected final char separator
This is the character that the CSVParser will treat as the separator.
-
quotechar
protected final char quotechar
This is the character that the CSVParser will treat as the quotation character.
-
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.
-
getQuotechar
public char getQuotechar()
- Specified by:
getQuotechar
in interfaceICSVParser
- Returns:
- The default quotation character for this parser.
-
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. NOTE: as of the 4.1 release this functionality is not considered production ready and has not been fully tested (and the ability to add a parser to the CSVWriter has not been implemented yet. I am adding this now because because I need to do the 4.1 release because of the number of defect fixes and I do not want to strip this out.- 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.
-
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 testedapplyQuotestoAll
- All values should be surrounded with quotes- Returns:
- String that will go into the CSV string
-
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.
-
-