public class BasicScanner extends Logging implements java.io.Closeable
scanAnyContent()
according to the expected token grammar.
BasicScanner provides the common methods for scanning name tokens, key
words, quoted strings and XML entity references, which are inherited by
all of its descendants.
BasicScanner is expected to buffer between 1-20 tokens on each call to
getToken()
, which allows parsers to work with the token
stream during read-cycles from the underlying input stream. BasicScanner
also implements Scanner's logging interface, so that the state of the
underlying stream can be queried by pull-parsers using this BasicScanner.
BasicScanner implements the Closeable interface, so may be used in
try-with-resources blocks (since JDK 1.7).
Modifier and Type | Field and Description |
---|---|
protected java.lang.String |
encoding
The name of the character set used when decoding characters from bytes.
|
protected java.io.LineNumberReader |
input
The input stream used by this BasicScanner.
|
private boolean |
isStandardInput
Secret flag to determine whether the wrapped stream is System.in,
used to block closing the standard input stream.
|
protected int |
lastChar
The lookahead character.
|
protected java.util.Deque<java.lang.String> |
symbolStack
The stack of where we are in the parse of tokens.
|
protected java.util.Deque<java.lang.String> |
textQueue
The queue of tokenised text.
|
protected java.util.Deque<java.lang.Integer> |
tokenQueue
The queue of symbolic tokens.
|
Constructor and Description |
---|
BasicScanner(java.io.File file,
java.lang.String encoding)
Creates a BasicScanner reading from a file, using a specified character
encoding.
|
BasicScanner(java.io.InputStream stream,
java.lang.String encoding)
Creates a BasicScanner reading from a basic InputStream, using a specified
character encoding.
|
BasicScanner(java.io.Reader reader,
java.lang.String encoding)
Creates a BasicScanner reading from a character Reader, using a specified
character encoding.
|
BasicScanner(java.net.URL url,
java.lang.String encoding)
Creates a BasicScanner reading from a URL, using a specified character
encoding.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes this BasicScanner.
|
boolean |
endOfStream()
Reports whether the end of the input stream has been reached.
|
java.lang.String |
getContext()
Returns the XML context in which a syntax error is detected.
|
java.lang.String |
getEncoding()
Reports the character encoding used by this BasicScanner.
|
int |
getLineNumber()
Returns the current line number reached by this BasicScanner.
|
java.lang.String |
getText()
Returns the first piece of segmented text at the head of the text
queue.
|
int |
getToken()
Returns the first token at the head of the token queue.
|
protected void |
scanAnyContent()
Scans the input stream for arbitrary XML or DTD content, populating
the token stream.
|
protected void |
scanEntityReference()
Parses an XML entity reference.
|
protected boolean |
scanExactly(java.lang.String token)
Checks that the input contains the exact token next on the stream.
|
protected void |
scanKeyword()
Scans a DTD keyword.
|
protected void |
scanNameToken()
Scans an XML name token.
|
protected void |
scanQuotedValue()
Scans a quoted value-string.
|
protected void |
skipSpace()
Skips whitespace characters.
|
encodingError, semanticError, syntaxError
private boolean isStandardInput
protected java.lang.String encoding
protected java.io.LineNumberReader input
protected java.util.Deque<java.lang.Integer> tokenQueue
protected java.util.Deque<java.lang.String> textQueue
protected java.util.Deque<java.lang.String> symbolStack
protected int lastChar
public BasicScanner(java.io.File file, java.lang.String encoding) throws java.io.FileNotFoundException, java.io.UnsupportedEncodingException
file
- the input file.encoding
- the name of the character encoding.java.io.FileNotFoundException
- if the file cannot be found or opened.java.io.UnsupportedEncodingException
- if the character encoding is not
supported.public BasicScanner(java.net.URL url, java.lang.String encoding) throws java.io.IOException, java.io.UnsupportedEncodingException
openStream()
method.url
- the absolute URL giving the location of the XML data.encoding
- the name of the character encoding.java.io.IOException
- if the attempt to connect to the URL and open an
input stream to read from it fails.java.io.UnsupportedEncodingException
- if the character encoding is not
supported.public BasicScanner(java.io.InputStream stream, java.lang.String encoding) throws java.io.UnsupportedEncodingException
stream
- the basic InputStream.encoding
- the name of the character encoding.java.io.UnsupportedEncodingException
- if the character encoding is not
supported.public BasicScanner(java.io.Reader reader, java.lang.String encoding)
reader
- the character Reader.encoding
- the name of the character encoding.public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
java.io.IOException
- if an I/O error occurs during closing.public java.lang.String getEncoding()
public int getLineNumber()
getLineNumber
in class Logging
public java.lang.String getContext()
getContext
in class Logging
public boolean endOfStream()
endOfStream
in class Logging
public int getToken() throws java.io.UnsupportedEncodingException, java.io.IOException, SyntaxError
java.io.UnsupportedEncodingException
- if a decoding fault occurs.java.io.IOException
- if an input stream error is detected.SyntaxError
- if an XML syntax error is detected.public java.lang.String getText()
protected void scanAnyContent() throws java.io.UnsupportedEncodingException, java.io.IOException, SyntaxError
java.io.UnsupportedEncodingException
- if a decoding fault occurs.java.io.IOException
- if an input stream error is detected.SyntaxError
- if a syntax error is detected.protected void scanNameToken() throws java.io.IOException, SyntaxError
java.io.IOException
- if an input stream error is detected.SyntaxError
- if the name token is malformed.protected void scanKeyword() throws java.io.IOException, SyntaxError
java.io.IOException
- if an input stream error is detected.SyntaxError
- if the plain token is malformed.protected void scanEntityReference() throws java.io.IOException
java.io.IOException
- if an input stream error is detected.java.io.EOFException
- if premature end of input is detected.SyntaxError
- if an XML syntax error is detected.protected void scanQuotedValue() throws java.io.IOException, SyntaxError
java.io.IOException
- if an input stream error is detected.SyntaxError
- if an XML syntax error is detected.protected boolean scanExactly(java.lang.String token) throws java.io.IOException
token
- the token to be matched.java.io.IOException
- if an input stream error is detected.protected void skipSpace() throws java.io.IOException
java.io.IOException
- if an input stream error is detected.