Class StreamParser


  • public class StreamParser
    extends java.lang.Object
    A class which enables reading and re-reading of an InputStream. Various methods of reading are supported, including Fixed Length files, simple Separated Files, Escaped Separated Files and Quoted Separated Files.
    • Constructor Summary

      Constructors 
      Constructor Description
      StreamParser​(java.io.InputStream stream, java.lang.String charset)  
      StreamParser​(java.lang.String string, java.lang.String charset)
      Creates a new instance of StreamParser which will read the contents of the supplied String
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String getCharset()
      Creates a new instance of StreamParser which will read the contents of the supplied Reader
      java.lang.String getContent()
      returns the data extracted by the last readElement() call
      void readElement()
      reads the next elemement from the source of this StreamParser according to the specified parse rule
      void rewindElement​(java.lang.String string)
      Puts the supplied String back on the source for re-reading
      void setParseRule​(int type, char sep)
      sets the parse rule and separator character of the element to be parsed.
      void setParseRule​(int type, char sep, char esc)
      sets the parse rule, separator character and escape / quote character for the element to be parsed.
      void setParseRule​(int type, int length)
      Sets the parse rule and the length of the element to be parsed.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • StreamParser

        public StreamParser​(java.lang.String string,
                            java.lang.String charset)
        Creates a new instance of StreamParser which will read the contents of the supplied String
      • StreamParser

        public StreamParser​(java.io.InputStream stream,
                            java.lang.String charset)
    • Method Detail

      • getCharset

        public java.lang.String getCharset()
        Creates a new instance of StreamParser which will read the contents of the supplied Reader
      • setParseRule

        public void setParseRule​(int type,
                                 int length)
        Sets the parse rule and the length of the element to be parsed. This will be called only for Fixed Length files
        Parameters:
        type - should be StreamParser.FIXED_LENGTH
        length - the number of characters to be read
      • setParseRule

        public void setParseRule​(int type,
                                 char sep)
        sets the parse rule and separator character of the element to be parsed. This is only called for simple Separated files
        Parameters:
        type - should be StreamParser.SEPARATED_STRING
        sep - the separator character
      • setParseRule

        public void setParseRule​(int type,
                                 char sep,
                                 char esc)
        sets the parse rule, separator character and escape / quote character for the element to be parsed.
        e.g.
        setParseRule(ESCAPED_STRING, ',', '?') would enable parsing of [[to be?, or not to be,]]
        setParseRule(QUOTED_STRING, ',', '"') would enable parsing of [["to be, or not to be",]]

        Parameters:
        type - can be either StreamParser.ESCAPED_STRING or StreamParser.QUOTED_STRING
        sep - the separator character of this element
        esc - either the escape char of this element or the quote char
      • readElement

        public void readElement()
                         throws java.lang.Exception
        reads the next elemement from the source of this StreamParser according to the specified parse rule
        Throws:
        java.lang.Exception
      • getContent

        public java.lang.String getContent()
        returns the data extracted by the last readElement() call
      • rewindElement

        public void rewindElement​(java.lang.String string)
                           throws java.io.IOException
        Puts the supplied String back on the source for re-reading
        Throws:
        java.io.IOException