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 Object implements ICSVParser
The purpose of the AbstractCSVParser is to consolidate the duplicate code amongst the parsers.
  • Field Details

    • SPECIAL_REGEX_CHARS

      protected static final 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 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 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 String quotecharAsString
      This is the quotechar in String form to reduce the number of calls to toString.
    • quoteDoubledAsString

      protected final String quoteDoubledAsString
      This is quotecharAsString+quotecharAsString - used in replaceAll to reduce the number of strings being created.
    • quoteMatcherPattern

      protected final 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:
    • pending

      protected String pending
      Value to be appended to string to process.
  • Constructor Details

    • AbstractCSVParser

      public AbstractCSVParser(char separator, char quotechar, CSVReaderNullFieldIndicator nullFieldIndicator)
      Common constructor.
      Parameters:
      separator - The delimiter to use for separating entries
      quotechar - The character to use for quoted elements
      nullFieldIndicator - Indicate what should be considered null
  • Method Details

    • getSeparator

      public char getSeparator()
      Specified by:
      getSeparator in interface ICSVParser
      Returns:
      The default separator for this parser.
    • getSeparatorAsString

      public String getSeparatorAsString()
      Returns:
      String version of separator to reduce number of calls to toString.
    • getQuotechar

      public char getQuotechar()
      Specified by:
      getQuotechar in interface ICSVParser
      Returns:
      The default quotation character for this parser.
    • getQuotecharAsString

      public String getQuotecharAsString()
      Returns:
      String version of quotechar to reduce the number of calls to toString.
    • isPending

      public boolean isPending()
      Specified by:
      isPending in interface ICSVParser
      Returns:
      True if something was left over from last call(s)
    • parseLineMulti

      public String[] parseLineMulti(String nextLine) throws 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 interface ICSVParser
      Parameters:
      nextLine - Current line to be processed
      Returns:
      The comma-tokenized list of elements, or null if nextLine is null
      Throws:
      IOException - If bad things happen during the read
    • parseLine

      public String[] parseLine(String nextLine) throws 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 interface ICSVParser
      Parameters:
      nextLine - Line to be parsed.
      Returns:
      The list of elements, or null if nextLine is null
      Throws:
      IOException - If bad things happen during the read
    • parseToLine

      public String parseToLine(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 interface ICSVParser
      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(String[] values, boolean applyQuotesToAll, Appendable appendable) throws 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 interface ICSVParser
      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:
      IOException
    • convertToCsvValue

      protected abstract String convertToCsvValue(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 converted
      applyQuotestoAll - All values should be surrounded with quotes
      Returns:
      String that will go into the CSV string
    • convertToCsvValue

      protected void convertToCsvValue(String value, boolean applyQuotesToAll, Appendable appendable) throws 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 converted
      applyQuotesToAll - All values should be surrounded with quotes
      appendable - Appendable object that the converted values are added to.
      Throws:
      IOException
    • isSurroundWithQuotes

      protected boolean isSurroundWithQuotes(String value, boolean forceSurround)
      Used by reverse parsing to determine if a value should be surrounded by quote characters.
      Parameters:
      value - String to be tested
      forceSurround - If the value is not null it will be surrounded with quotes
      Returns:
      True if the string should be surrounded with quotes, false otherwise
    • parseLine

      protected abstract String[] parseLine(String nextLine, boolean multi) throws IOException
      Parses an incoming String and returns an array of elements.
      Parameters:
      nextLine - The string to parse
      multi - Whether it takes multiple lines to form a single record
      Returns:
      The list of elements, or null if nextLine is null
      Throws:
      IOException - If bad things happen during the read
    • nullFieldIndicator

      public CSVReaderNullFieldIndicator nullFieldIndicator()
      Specified by:
      nullFieldIndicator in interface ICSVParser
      Returns:
      The null field indicator.
    • getPendingText

      public 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 interface ICSVParser
      Returns:
      The incomplete text for a multiline field. If there is no pending text, this returns an empty string.