Package com.opencsv

Class 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 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.
      • 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 entries
        quotechar - The character to use for quoted elements
        nullFieldIndicator - Indicate what should be considered null
    • Method Detail

      • getSeparator

        public char getSeparator()
        Specified by:
        getSeparator in interface ICSVParser
        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 interface ICSVParser
        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 interface ICSVParser
        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 interface ICSVParser
        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 interface ICSVParser
        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 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​(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 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:
        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 converted
        applyQuotestoAll - 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 converted
        applyQuotesToAll - All values should be surrounded with quotes
        appendable - 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 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 java.lang.String[] parseLine​(java.lang.String nextLine,
                                                        boolean multi)
                                                 throws java.io.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:
        java.io.IOException - If bad things happen during the read
      • 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 interface ICSVParser
        Returns:
        The incomplete text for a multiline field. If there is no pending text, this returns an empty string.