Class LocationReader<D>

java.lang.Object
java.io.Reader
java.io.FilterReader
eu.bandm.tools.location.LocationReader<D>
Type Parameters:
D - the type of document identifiers
All Implemented Interfaces:
Closeable, AutoCloseable, Readable

public class LocationReader<D> extends FilterReader
A secondary Reader that tracks location information.

Characters are obtained from another Reader. For the consistency of the computed location information, it is required that there are no other, concurrent consumers of characters from that source.

For tracking line and column number information, the following ISO control characters are recognized:
'\n' not preceded by a '\r' advances the line number and resets the column number.
'\r' and the sequence "\r\n" are normalized to '\n' and do the same.
This corresponds to the behaviour of LineNumberReader.

'\f' is delivered as a normal character. This is required for processing Java source texts, see language specification.

'\t' advances the column number to the next tab stop. Tab stops are set at multiples of the given tabWidth.

Other ISO "group zero" control characters must not occur among the input code points, or a IllegalArgumentException is thrown.

See Also:
  • Constructor Details

    • LocationReader

      public LocationReader(Reader in, @Opt D documentId, int firstLine, int firstColumn)
      Constructor, which sets the tabulator width to one.
      Parameters:
      in - the underlying character stream.
      documentId - the document id for all locations to generate.
      firstLine - the numeric value >= 0 with which the line numbers start.
      firstColumn - the numeric value >= 0 with which the column numbers start.
    • LocationReader

      public LocationReader(Reader in, @Opt D documentId, int firstLine, int firstColumn, int tabWidth)
      Constructor with an arbitrary tabulator width >=1.
      Parameters:
      in - the underlying character stream.
      documentId - the document id for all locations to generate.
      firstLine - the numeric value >= 0 with which the line numbers start. Can be Location.UNKNOWN or Location.NOT_APPLICABLE.
      firstColumn - the numeric value >= 0 with which the column numbers start. Can be Location.UNKNOWN or Location.NOT_APPLICABLE.
      tabWidth - how to expand tabulator characters = \t = ^I. Must be >= 1.
  • Method Details

    • getPoint

      public Location<D> getPoint()
      Returns the position immediately before to next-to-read character.
      Returns:
      the current location.
    • setBeginPoint

      public void setBeginPoint()
      Memorize the current location as the start point of a region, later constructed by getRegion().
    • getRegion

      public Location<D> getRegion()
      Construct a region from the start point (memorized by the last call to setBeginPoint()) to the current point.
      Returns:
      the constructed region.
      Throws:
      IllegalStateException - if setBeginPoint() has not been called.
    • read

      public int read() throws IOException
      . Unifies the different CR/LF-strategies: returns "\n" for "\r" and "\r\n". This corresponds to the behaviour of LineNumberReader. Additionally the current location is updated according to the read characters.
      Overrides:
      read in class FilterReader
      Throws:
      IOException
    • read

      public int read(char[] cbuf, int off, int len) throws IOException
      . Unifies the different CR/LF-strategies: returns "\n" for "\r" and "\r\n". This corresponds to the behaviour of LineNumberReader. Additionally the current location is updated according to the read characters.
      Overrides:
      read in class FilterReader
      Throws:
      IOException
    • mark

      public void mark(int readAheadLimit) throws IOException
      . Additionally the current location is memorized for a later reset().
      Overrides:
      mark in class FilterReader
      Throws:
      IOException
    • reset

      public void reset() throws IOException
      . Additionally the current location is reset to the marked state.
      Overrides:
      reset in class FilterReader
      Throws:
      IOException