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

      • 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.
      • 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.
      • getQuotechar

        public char getQuotechar()
        Specified by:
        getQuotechar in interface ICSVParser
        Returns:
        The default quotation character for this parser.
      • 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. 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 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.
      • 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 tested
        applyQuotestoAll - 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 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.