org.qedeq.base.io
Class TextInput

java.lang.Object
  extended by java.io.InputStream
      extended by org.qedeq.base.io.TextInput
All Implemented Interfaces:
java.io.Closeable
Direct Known Subclasses:
SubTextInput

public class TextInput
extends java.io.InputStream

This class provides convenient methods for parsing input.

Author:
Michael Meyling

Field Summary
static char CR
          Char marking end of input line.
static int EOF
          Char marking end of data.
 
Constructor Summary
TextInput(java.io.File file, java.lang.String encoding)
          Constructor using FILE source.
TextInput(java.io.Reader reader)
          Constructor using Reader source.
TextInput(java.lang.String source)
          Constructor using String source.
TextInput(java.lang.StringBuffer source)
          Constructor using StringBuffer source.
 
Method Summary
 void addColumn(int number)
          Add the following columns.
 void addPosition(SourcePosition delta)
          Adds a given position to the current one and changes the row and column number accordingly.
 void addRow(int number)
          Add the following rows and reset column (if number == 0).
 java.lang.String asString()
          Get complete input source.
 void forward(int number)
          Skips a given amount of characters and increments the reading position accordingly.
 boolean forward(java.lang.String search)
          Skips until a given keyword is reached.
 int getChar()
          Reads a single character and does not change the reading position.
 int getChar(int skip)
          Reads a single character and does not change the reading position.
 int getColumn()
          Returns the current column number.
 java.lang.String getLine()
          Returns the current line.
 int getMaximumPosition()
          Returns the highest position number possible.
 int getPosition()
          Returns the current position.
 SourcePosition getPosition(int find)
          Get given byte position as SourcePosition.
 int getPosition(SourcePosition position)
          Get given byte position as SourcePosition.
 int getRow()
          Returns the current line number.
 java.lang.String getSourceArea(SourceArea area)
          Get source area as string.
 SourcePosition getSourcePosition()
          Returns the current position.
 java.lang.String getSubstring(int from, int to)
          Reads a substring.
 boolean isEmpty()
          Is there no data left for reading?
 boolean isEmpty(int skip)
          Is there no data left for reading after skipping?
 int read()
          Reads a single character and increments the reading position by one.
 java.lang.String readCounter()
          Reads the next (big) integer, leading whitespace is skipped.
 int readInverse()
          Decrements the reading position by one and reads a single character.
 java.lang.String readLetterDigitString()
          Reads the next string containing only letters or digits, leading whitespace is skipped.
 java.lang.String readNextAttributeValue()
          Reads attribute value out of XML stream.
 java.lang.String readNextXmlName()
          Reads tag or attribute name out of XML stream.
 int readNonNegativeInt()
          Reads the next integer, leading whitespace is skipped.
 java.lang.String readQuoted()
          Reads the next quoted string, leading whitespace is skipped.
 java.lang.String readString(int number)
          Reads a given amount of characters and increments the reading position accordingly.
 java.lang.String readStringTilWhitespace()
          Reads the next string until whitespace occurs, leading whitespace is skipped.
 void replace(int from, int to, java.lang.String replacement)
          Replace given interval with given string.
 void setColumn(int column)
          Sets the current column position (and indirectly the position).
 void setPosition(int position)
          Sets the current position (and indirectly the row and column number).
 void setPosition(SourcePosition position)
          Sets the current position (and indirectly the row and column number).
 void setRow(int row)
          Sets the current line number (and indirectly the position).
 java.lang.String showLinePosition()
          Show reading position.
 void skipBackToBeginOfXmlTag()
          Skip current position back to beginning of an XML tag.
 void skipForwardToEndOfXmlTag()
          Skip current position forward to end of an XML tag.
 void skipToEndOfLine()
          Skip forward to end of line.
 void skipWhiteSpace()
          Skips white space, beginning from reading position.
 void skipWhiteSpaceInverse()
          Skips white space, beginning from reading position.
 
Methods inherited from class java.io.InputStream
available, close, mark, markSupported, read, read, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EOF

public static final int EOF
Char marking end of data.

See Also:
Constant Field Values

CR

public static final char CR
Char marking end of input line.

See Also:
Constant Field Values
Constructor Detail

TextInput

public TextInput(java.io.Reader reader)
          throws java.io.IOException
Constructor using Reader source.

Parameters:
reader - Data source. This reader will be closed (even if reading fails).
Throws:
java.io.IOException - Reading failed.
java.lang.NullPointerException - Argument was a null pointer.

TextInput

public TextInput(java.lang.StringBuffer source)
Constructor using StringBuffer source.

Parameters:
source - data source
Throws:
java.lang.NullPointerException - Argument was a null pointer.

TextInput

public TextInput(java.lang.String source)
Constructor using String source.

Parameters:
source - data source
Throws:
java.lang.NullPointerException - Argument was a null pointer.

TextInput

public TextInput(java.io.File file,
                 java.lang.String encoding)
          throws java.io.IOException
Constructor using FILE source.

Parameters:
file - Data source.
encoding - Take this encoding for file.
Throws:
java.io.IOException - File reading failed.
java.lang.NullPointerException - One argument was a null pointer.
Method Detail

read

public final int read()
Reads a single character and increments the reading position by one. If no characters are left, -1 is returned. Otherwise a cast to char gives the character read.

Specified by:
read in class java.io.InputStream
Returns:
Character read, if there are no more chars -1 is returned.

readInverse

public final int readInverse()
Decrements the reading position by one and reads a single character. If no characters are left, -1 is returned. Otherwise a cast to char gives the character read.

Returns:
Character read, if there are no more chars -1 is returned.

readString

public final java.lang.String readString(int number)
Reads a given amount of characters and increments the reading position accordingly.

Parameters:
number - amount of characters to read
Returns:
string read

forward

public final void forward(int number)
Skips a given amount of characters and increments the reading position accordingly.

Parameters:
number - Amount of characters to read. Must be positive - if not nothing happens.

forward

public final boolean forward(java.lang.String search)
Skips until a given keyword is reached. The position afterwards is at the start of the keyword or at the end of the text (if the keyword is not found).

Parameters:
search - Look for this keyword.
Returns:
Was the keyword found?

getChar

public final int getChar()
Reads a single character and does not change the reading position. If no characters are left, -1 is returned. Otherwise a cast to char gives the character read.

Returns:
Character read at current position, if there are no more chars -1 is returned

getChar

public final int getChar(int skip)
Reads a single character and does not change the reading position. If offset addition leads out of the source, -1 is returned. Otherwise a cast to char gives the character read.

Parameters:
skip - Offset from current reading position. Maybe negative.
Returns:
Character read, if position is out of scope -1 is returned.

getSubstring

public final java.lang.String getSubstring(int from,
                                           int to)
Reads a substring. Doesn't change reading position. Throws never an Exception.

Parameters:
from - Absolute reading position.
to - Read to this position.
Returns:
Resulting string.

asString

public final java.lang.String asString()
Get complete input source. Doesn't change reading position.

Returns:
Complete input string.

replace

public final void replace(int from,
                          int to,
                          java.lang.String replacement)
Replace given interval with given string. If the current reading position is in the interval it is set to the end of the interval.

Parameters:
from - Absolute reading position.
to - Read to this position.
replacement - Replacement for interval.

skipWhiteSpace

public final void skipWhiteSpace()
Skips white space, beginning from reading position. Changes reading position to next non white space character.


skipWhiteSpaceInverse

public final void skipWhiteSpaceInverse()
Skips white space, beginning from reading position. Changes reading position to next non white space character.


skipBackToBeginOfXmlTag

public final void skipBackToBeginOfXmlTag()
Skip current position back to beginning of an XML tag. This is mainly something like <tagName.

Throws:
java.lang.IllegalArgumentException - No begin of XML tag found.

skipToEndOfLine

public final void skipToEndOfLine()
Skip forward to end of line.


skipForwardToEndOfXmlTag

public final void skipForwardToEndOfXmlTag()
Skip current position forward to end of an XML tag. It is assumed the current position is within the the XML tag. Now we search for >. Quoted data is skipped.

Throws:
java.lang.IllegalArgumentException - No end of XML tag found.

readNextXmlName

public final java.lang.String readNextXmlName()
Reads tag or attribute name out of XML stream. Whitespace is skipped and characters are read till "=" or ">" or "< or whitespace is found. We must be within the tag, so we can not start with something like <.

Returns:
Name of tag or attribute.
Throws:
java.lang.IllegalArgumentException - Next non white space character is "=" or ">".

readNextAttributeValue

public final java.lang.String readNextAttributeValue()
Reads attribute value out of XML stream. Whitespace is skipped and an "=" is expected to follow. Again whitespace is skipped. If no quotation mark follows characters are read till whitespace or ">" occurs. Otherwise data is read till an ending quotation mark comes.

Returns:
Value read.
Throws:
java.lang.IllegalArgumentException - Following had not one of the following forms:
   = "value"
 
   = value
 

isEmpty

public final boolean isEmpty()
Is there no data left for reading?

Returns:
is all data read?

isEmpty

public final boolean isEmpty(int skip)
Is there no data left for reading after skipping?

Parameters:
skip - Add this number to current position.
Returns:
Is data empty at that new position?

readLetterDigitString

public final java.lang.String readLetterDigitString()
Reads the next string containing only letters or digits, leading whitespace is skipped. Changes reading position.

Returns:
read string
Throws:
java.lang.IllegalArgumentException - if no such characters could be found

readStringTilWhitespace

public final java.lang.String readStringTilWhitespace()
Reads the next string until whitespace occurs, leading whitespace is skipped. Changes (probably) reading position.

Returns:
read string

readNonNegativeInt

public final int readNonNegativeInt()
Reads the next integer, leading whitespace is skipped. Signs like - or + are not accepted. Resulting integer Changes reading position.

Returns:
Read integer.
Throws:
java.lang.IllegalArgumentException - if no digits where found or the number was to big for an int

readCounter

public final java.lang.String readCounter()
Reads the next (big) integer, leading whitespace is skipped. The first character might be a minus sign, the rest must be digits. Leading zero digits are not allowed, also "-0" is not accepted.

Changes reading position.

Returns:
read integer
Throws:
java.lang.IllegalArgumentException - if no digits where found.

readQuoted

public final java.lang.String readQuoted()
Reads the next quoted string, leading whitespace is skipped. A correctly quoted string could be created by adding a leading and a trailing quote character and doubling each other quote character. The resulting string is dequoted. Changes reading position.

Returns:
Dequoted string read.
Throws:
java.lang.IllegalArgumentException - No correctly quoted string was found.

getRow

public final int getRow()
Returns the current line number.

Returns:
Current line number (starting with line 1).

getColumn

public final int getColumn()
Returns the current column number.

Returns:
Current column number (starting with line 1).

getLine

public final java.lang.String getLine()
Returns the current line.

Returns:
Current line.

getPosition

public final int getPosition()
Returns the current position. Starting with 0. This is the number of characters from the beginning.

Returns:
Current position.

getSourcePosition

public final SourcePosition getSourcePosition()
Returns the current position.

Returns:
Current position.

getMaximumPosition

public final int getMaximumPosition()
Returns the highest position number possible. This is equal to the length of the source.

Returns:
Maximum position.

setPosition

public final void setPosition(int position)
Sets the current position (and indirectly the row and column number).

Parameters:
position - Set current position to this value.

setPosition

public final void setPosition(SourcePosition position)
Sets the current position (and indirectly the row and column number).

Parameters:
position - Set current position to this value.

addPosition

public final void addPosition(SourcePosition delta)
Adds a given position to the current one and changes the row and column number accordingly. A delta position with one row and one column doesn't change the current position.

Parameters:
delta - Add this position to current one.

setRow

public final void setRow(int row)
Sets the current line number (and indirectly the position).

Parameters:
row - Move to this line number.

getPosition

public final SourcePosition getPosition(int find)
Get given byte position as SourcePosition.

Parameters:
find - Get row and column information for this byte position.
Returns:
Row and column information.

getPosition

public final int getPosition(SourcePosition position)
Get given byte position as SourcePosition.

Parameters:
position - Get row and column information for this byte position.
Returns:
Row and column information.

getSourceArea

public final java.lang.String getSourceArea(SourceArea area)
Get source area as string.

Parameters:
area - Get this area as string.
Returns:
Area itself.

addRow

public final void addRow(int number)
Add the following rows and reset column (if number == 0).

Parameters:
number - Add this number of rows.

setColumn

public final void setColumn(int column)
Sets the current column position (and indirectly the position). If column is out of range the minimum value (1) or the maximum possible column value is taken.

Parameters:
column - Move to this column. First column has the number one.

addColumn

public final void addColumn(int number)
Add the following columns.

Parameters:
number - Add this number of columns.

showLinePosition

public final java.lang.String showLinePosition()
Show reading position.

Returns:
current line with mark at current reading position


Copyright © 2014. All Rights Reserved.