Class LineParser

java.lang.Object
com.northconcepts.datapipeline.core.LineParser
All Implemented Interfaces:
IParser

public class LineParser extends Object implements IParser
An IParser that reads its input one line at a time; lookahead and matching only see the current line until cacheNextLine() moves to the next one.
  • Field Summary

    Fields inherited from interface com.northconcepts.datapipeline.core.IParser

    EOF
  • Constructor Summary

    Constructors
    Constructor
    Description
    LineParser(Reader reader, char[][] lineSeparators)
    Creates a parser that ends lines at the given separators, checked in array order, or like BufferedReader.readLine() if they are null.
    LineParser(String text, char[][] lineSeparators)
    Creates a parser that ends lines at the given separators, checked in array order, or like BufferedReader.readLine() if they are null.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
     
    void
     
    void
     
    void
    consume(int count)
     
    void
     
    void
    consumeWhitespace(char[] exception)
    Skips whitespace at the current position, stopping where the given string (such as a field separator) begins.
    void
    consumeWhitespace(int exception)
    Skips spaces and tabs at the current position, stopping early at the given character.
    void
    Skips whitespace at the current position, stopping where the given string (such as a field separator) begins.
    exception(Throwable exception)
    Wraps the exception in a DataException and adds the parser's buffer and position as properties.
    long
    Returns the number of characters consumed so far (within the current line for LineParser).
    char[]
     
    long
    Returns the 0-based number of the current line or -1 before the first line is read.
    Returns the current line's text, without its separator, or null before the first line is read.
    Returns the unconsumed rest of the current line, an empty string if it is fully consumed, or null before the first line is read.
    void
    match(char expectedChar)
    Consumes the next character, throwing a DataException if it is not the expected one.
    void
    match(char[] expectedString)
    Consumes the expected string, throwing a DataException at the first character that differs.
    void
    match(char[] expectedString, String failureMessage)
    Consumes the expected string, throwing a DataException prefixed with failureMessage at the first character that differs.
    void
    match(char expectedChar, String failureMessage)
    Consumes the next character, throwing a DataException prefixed with failureMessage if it is not the expected one.
    void
    match(CharSequence expectedString)
    Consumes the expected string, throwing a DataException at the first character that differs.
    void
    match(CharSequence expectedString, String failureMessage)
    Consumes the expected string, throwing a DataException prefixed with failureMessage at the first character that differs.
    matchUntil(char terminatingChar)
    Consumes and returns the text before the terminating character, then consumes the terminator; throws a DataException if the input ends first.
    matchUntil(char terminatingChar, String failureMessage)
    Consumes and returns the text before the terminating character, then consumes the terminator; throws a DataException prefixed with failureMessage if the input ends first.
    Consumes and returns all remaining characters (of the current line for LineParser).
    void
    Moves to the next line, throwing a DataException if the input has ended.
    void
    nextLine(String failureMessage)
    Moves to the next line, throwing a DataException prefixed with failureMessage if the input has ended.
    int
    peek(int index)
    Returns the character at the given 0-based offset ahead without consuming it, or IParser.EOF past the end of the input.
    peekSequence(int offset, int length)
    Returns up to length characters starting at the given lookahead offset without consuming them; fewer if the input ends first.
    boolean
    peekString(char[] lookahead)
    Indicates if the upcoming characters match the given string, without consuming them.
    boolean
    Indicates if the upcoming characters match the given string, without consuming them.
    int
    peekStringArray(char[][] lookahead)
    Returns the index of the first string that matches the upcoming characters or -1 if none do, without consuming them.
    int
    peekStringArray(char[][] lookahead, int offset)
    Returns the index of the first string that matches the characters at the given lookahead offset or -1 if none do.
    int
    Returns the index of the first string that matches the upcoming characters or -1 if none do, without consuming them.
    int
    peekStringArray(CharSequence[] lookahead, int offset)
    Returns the index of the first string that matches the characters at the given lookahead offset or -1 if none do.
    boolean
    peekStringOffset(char[] lookahead, int offset)
    Indicates if the characters starting at the given lookahead offset match the given string, without consuming them.
    boolean
    peekStringOffset(CharSequence lookahead, int offset)
    Indicates if the characters starting at the given lookahead offset match the given string, without consuming them.
    int
    Returns the number of consecutive whitespace characters starting at the given 0-based lookahead offset.
    int
    pop()
    Consumes and returns the next character, or IParser.EOF at the end of the input.
    boolean
     
    setLineSeparators(char[][] lineSeparators)
    Sets the separators that end a line, checked in array order; null ends lines like BufferedReader.readLine().
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • LineParser

      public LineParser(Reader reader, char[][] lineSeparators)
      Creates a parser that ends lines at the given separators, checked in array order, or like BufferedReader.readLine() if they are null.
    • LineParser

      public LineParser(String text, char[][] lineSeparators)
      Creates a parser that ends lines at the given separators, checked in array order, or like BufferedReader.readLine() if they are null.
  • Method Details

    • exception

      public DataException exception(Throwable exception)
      Description copied from interface: IParser
      Wraps the exception in a DataException and adds the parser's buffer and position as properties.
      Specified by:
      exception in interface IParser
    • setLineSeparators

      public LineParser setLineSeparators(char[][] lineSeparators)
      Sets the separators that end a line, checked in array order; null ends lines like BufferedReader.readLine().
    • getLastLineSeparator

      public char[] getLastLineSeparator()
    • cacheNextLine

      public boolean cacheNextLine()
    • ready

      public boolean ready() throws IOException
      Specified by:
      ready in interface IParser
      Throws:
      IOException
    • getLine

      public long getLine()
      Returns the 0-based number of the current line or -1 before the first line is read.
    • getColumn

      public long getColumn()
      Description copied from interface: IParser
      Returns the number of characters consumed so far (within the current line for LineParser).
      Specified by:
      getColumn in interface IParser
    • getLineText

      public String getLineText()
      Returns the current line's text, without its separator, or null before the first line is read.
    • getRemainingLineText

      public String getRemainingLineText()
      Returns the unconsumed rest of the current line, an empty string if it is fully consumed, or null before the first line is read.
    • peek

      public int peek(int index)
      Description copied from interface: IParser
      Returns the character at the given 0-based offset ahead without consuming it, or IParser.EOF past the end of the input.
      Specified by:
      peek in interface IParser
    • peekWhitespaceLength

      public int peekWhitespaceLength(int index)
      Description copied from interface: IParser
      Returns the number of consecutive whitespace characters starting at the given 0-based lookahead offset.
      Specified by:
      peekWhitespaceLength in interface IParser
    • peekSequence

      public CharSequence peekSequence(int offset, int length)
      Description copied from interface: IParser
      Returns up to length characters starting at the given lookahead offset without consuming them; fewer if the input ends first.
      Specified by:
      peekSequence in interface IParser
    • peekString

      public boolean peekString(CharSequence lookahead)
      Description copied from interface: IParser
      Indicates if the upcoming characters match the given string, without consuming them.
      Specified by:
      peekString in interface IParser
    • peekString

      public boolean peekString(char[] lookahead)
      Description copied from interface: IParser
      Indicates if the upcoming characters match the given string, without consuming them.
      Specified by:
      peekString in interface IParser
    • peekStringOffset

      public boolean peekStringOffset(CharSequence lookahead, int offset)
      Description copied from interface: IParser
      Indicates if the characters starting at the given lookahead offset match the given string, without consuming them.
      Specified by:
      peekStringOffset in interface IParser
    • peekStringOffset

      public boolean peekStringOffset(char[] lookahead, int offset)
      Description copied from interface: IParser
      Indicates if the characters starting at the given lookahead offset match the given string, without consuming them.
      Specified by:
      peekStringOffset in interface IParser
    • peekStringArray

      public int peekStringArray(CharSequence... lookahead)
      Description copied from interface: IParser
      Returns the index of the first string that matches the upcoming characters or -1 if none do, without consuming them.
      Specified by:
      peekStringArray in interface IParser
    • peekStringArray

      public int peekStringArray(CharSequence[] lookahead, int offset)
      Description copied from interface: IParser
      Returns the index of the first string that matches the characters at the given lookahead offset or -1 if none do.
      Specified by:
      peekStringArray in interface IParser
    • peekStringArray

      public int peekStringArray(char[][] lookahead)
      Description copied from interface: IParser
      Returns the index of the first string that matches the upcoming characters or -1 if none do, without consuming them.
      Specified by:
      peekStringArray in interface IParser
    • peekStringArray

      public int peekStringArray(char[][] lookahead, int offset)
      Description copied from interface: IParser
      Returns the index of the first string that matches the characters at the given lookahead offset or -1 if none do.
      Specified by:
      peekStringArray in interface IParser
    • pop

      public int pop()
      Description copied from interface: IParser
      Consumes and returns the next character, or IParser.EOF at the end of the input.
      Specified by:
      pop in interface IParser
    • nextLine

      public void nextLine()
      Moves to the next line, throwing a DataException if the input has ended.
    • nextLine

      public void nextLine(String failureMessage)
      Moves to the next line, throwing a DataException prefixed with failureMessage if the input has ended.
    • match

      public void match(char expectedChar, String failureMessage)
      Description copied from interface: IParser
      Consumes the next character, throwing a DataException prefixed with failureMessage if it is not the expected one.
      Specified by:
      match in interface IParser
    • match

      public void match(char expectedChar)
      Description copied from interface: IParser
      Consumes the next character, throwing a DataException if it is not the expected one.
      Specified by:
      match in interface IParser
    • match

      public void match(CharSequence expectedString, String failureMessage)
      Description copied from interface: IParser
      Consumes the expected string, throwing a DataException prefixed with failureMessage at the first character that differs.
      Specified by:
      match in interface IParser
    • match

      public void match(char[] expectedString, String failureMessage)
      Description copied from interface: IParser
      Consumes the expected string, throwing a DataException prefixed with failureMessage at the first character that differs.
      Specified by:
      match in interface IParser
    • match

      public void match(CharSequence expectedString)
      Description copied from interface: IParser
      Consumes the expected string, throwing a DataException at the first character that differs.
      Specified by:
      match in interface IParser
    • match

      public void match(char[] expectedString)
      Description copied from interface: IParser
      Consumes the expected string, throwing a DataException at the first character that differs.
      Specified by:
      match in interface IParser
    • matchUntil

      public StringBuilder matchUntil(char terminatingChar)
      Description copied from interface: IParser
      Consumes and returns the text before the terminating character, then consumes the terminator; throws a DataException if the input ends first.
      Specified by:
      matchUntil in interface IParser
    • matchUntil

      public StringBuilder matchUntil(char terminatingChar, String failureMessage)
      Description copied from interface: IParser
      Consumes and returns the text before the terminating character, then consumes the terminator; throws a DataException prefixed with failureMessage if the input ends first.
      Specified by:
      matchUntil in interface IParser
    • matchUntilEof

      public StringBuilder matchUntilEof()
      Description copied from interface: IParser
      Consumes and returns all remaining characters (of the current line for LineParser).
      Specified by:
      matchUntilEof in interface IParser
    • consume

      public void consume()
      Specified by:
      consume in interface IParser
    • consume

      public void consume(int count)
      Specified by:
      consume in interface IParser
    • consumeWhitespace

      public void consumeWhitespace()
      Specified by:
      consumeWhitespace in interface IParser
    • consumeWhitespace

      public void consumeWhitespace(int exception)
      Description copied from interface: IParser
      Skips spaces and tabs at the current position, stopping early at the given character.
      Specified by:
      consumeWhitespace in interface IParser
    • consumeWhitespace

      public void consumeWhitespace(CharSequence exception)
      Description copied from interface: IParser
      Skips whitespace at the current position, stopping where the given string (such as a field separator) begins.
      Specified by:
      consumeWhitespace in interface IParser
    • consumeWhitespace

      public void consumeWhitespace(char[] exception)
      Description copied from interface: IParser
      Skips whitespace at the current position, stopping where the given string (such as a field separator) begins.
      Specified by:
      consumeWhitespace in interface IParser
    • close

      public void close()
      Specified by:
      close in interface IParser
    • toString

      public String toString()
      Overrides:
      toString in class Object