public abstract class FileSourceBase extends SourceBase implements FileSource
This class is a piece of crap. However it is still used in many places... :-( TODO use a parser generator to replace it.
This class provides parsing utilities to help the creation of new graph readers/parsers. It handles a stack of input files that allow to easily implements "includes" (that is interrupting the parsing of a file to input another one). It wraps stream tokenizers allowing to eat or get specific token types easily.
It is well suited for graph formats using text (not binary), but not for XML based files where a real XML parser would probably be better.
Modifier and Type | Class and Description |
---|---|
protected static class |
FileSourceBase.CurrentFile
Currently processed file.
|
SourceBase.ElementType
Modifier and Type | Field and Description |
---|---|
protected HashMap<String,String> |
attribute_classes
Map of unknown attributes to corresponding classes.
|
protected int |
COMMENT_CHAR
The comment character.
|
protected boolean |
eol_is_significant
Is EOL significant?.
|
protected String |
filename
Current file name.
|
protected int |
QUOTE_CHAR
The quote character.
|
protected StreamTokenizer |
st
Current tokenizer.
|
protected ArrayList<FileSourceBase.CurrentFile> |
tok_stack
Stack of tokenizers/filenames.
|
attrSinks, eltsSinks, eventProcessing, eventQueue, sourceId, sourceTime
Modifier | Constructor and Description |
---|---|
protected |
FileSourceBase()
No-op constructor.
|
protected |
FileSourceBase(boolean eol_is_significant)
Setup the reader End-Of-Line policy.
|
protected |
FileSourceBase(boolean eol_is_significant,
int commentChar,
int quoteChar)
Setup the reader End-Of-Line policy and specific comment and quote
characters.
|
Modifier and Type | Method and Description |
---|---|
void |
addAttributeClass(String attribute,
String attribute_class)
Declare that when
attribute is found, the corresponding
attribute_class must be instantiated and inserted in the
current element being parsed. |
void |
begin(InputStream stream)
Begin reading the file stopping as soon as possible.
|
void |
begin(Reader reader)
Begin reading the file stopping as soon as possible.
|
void |
begin(String filename)
Begin reading the file stopping as soon as possible.
|
void |
begin(URL url)
Begin reading the file stopping as soon as possible.
|
protected void |
configureTokenizer(StreamTokenizer tok)
Method to override to configure the tokenizer behaviour.
|
protected abstract void |
continueParsingInInclude()
Must be implemented to read the content of an include.
|
protected Reader |
createReaderFrom(InputStream stream)
Create a stream that can be read by the tokenizer.
|
protected Reader |
createReaderFrom(String file)
Create a reader for by the tokenizer.
|
private StreamTokenizer |
createTokenizer(Reader reader)
Create a tokenizer from an input source.
|
protected void |
eatAllEols()
Eat all availables EOLs.
|
protected void |
eatAllUntilEol()
Eat all until an EOL is found.
|
protected void |
eatEof()
Read EOF or report garbage at end of file.
|
protected void |
eatEol()
Read EOL.
|
protected int |
eatEolOrEof()
Read EOL or EOF.
|
protected String |
eatOneOfTwoWords(String word1,
String word2)
Eat either a word or another, and return the eated one.
|
protected void |
eatSymbol(char symbol)
Eat the expected symbol or generate a parse error.
|
protected void |
eatSymbolOrPushback(char symbol)
Eat the expected
symbol or push back what was read so that
it can be read anew. |
protected int |
eatSymbols(String symbols)
Eat one of the list of expected
symbols or generate a parse
error none of symbols can be found. |
protected void |
eatWord(String word)
Read an expected
word token or generate a parse error. |
protected void |
eatWordOrPushbak(String word)
Eat the expected
word or push back what was read so that it
can be read anew. |
protected void |
eatWords(String... words)
Read an expected word among the given word list or generate a parse
error.
|
void |
end()
Finish the reading process (even if
FileSource.nextEvents() or
FileSource.nextStep() did not returned false). |
protected String |
getAllExceptedEof()
Read a word or symbol or string or number or generate a parse error.
|
protected boolean |
getBoolean(String value)
Uses
isTrue(String) and isFalse(String) to determine if
value is a truth value and return the corresponding boolean. |
protected long |
getInteger(String value)
Try to transform
value into a long. |
protected double |
getNumber()
Read a number or generate a parse error.
|
protected double |
getNumberExp()
Read a number (possibly with an exponent) or generate a parse error.
|
protected Point3 |
getPoint3(String value)
Get a number triplet with numbers separated by comas and return a new
point for it.
|
protected double |
getReal(String value)
Try to transform
value into a double. |
protected String |
getString()
Read a string constant (between quotes) or generate a parse error.
|
protected String |
getStringOrNumber()
Read a string or number or generate a parse error.
|
protected String |
getStringOrWordOrNumber()
Read a string or number or generate a parse error.
|
protected Object |
getStringOrWordOrNumberO()
Read a string or number or generate a parse error.
|
protected String |
getStringOrWordOrNumberOrPushback()
Read a string or number or pushback and return null.
|
protected Object |
getStringOrWordOrSymbolOrNumberO()
Read a string or number or generate a parse error.
|
protected char |
getSymbol()
Get a symbol.
|
protected char |
getSymbolOrPushback()
Get a symbol or push back what was read so that it can be read anew.
|
protected String |
getWord()
Read a word or generate a parse error.
|
protected String |
getWordOrNumber()
Read a word or number or generate a parse error.
|
protected String |
getWordOrNumberOrStringOrEolOrEof()
Read a word or number or string or EOL/EOF or generate a parse error.
|
protected String |
getWordOrString()
Read a word or string or generate a parse error.
|
protected String |
getWordOrStringOrEolOrEof()
Read a word or string or EOL/EOF or generate a parse error.
|
protected String |
getWordOrSymbol()
Read a word or symbol or generate a parse error.
|
protected String |
getWordOrSymbolOrEof()
Read a word, a symbol or EOF, or generate a parse error.
|
protected String |
getWordOrSymbolOrNumberOrStringOrEolOrEof()
Read a word or number or string or EOL/EOF or generate a parse error.
|
protected String |
getWordOrSymbolOrPushback()
Read a word or symbol or push back the read thing so that it is readable
anew.
|
protected String |
getWordOrSymbolOrString()
Read a word or symbol or string or generate a parse error.
|
protected String |
getWordOrSymbolOrStringOrEolOrEof()
Read a word or symbol or string or EOL/EOF or generate a parse error.
|
protected String |
gotWhat(int token)
Return a string containing "got " then the content of the current
token . |
protected void |
include(String file)
Include the content of a
file . |
protected boolean |
isFalse(String string)
True if the
string represents a false statement ("0",
"false", "no", "off"). |
protected boolean |
isTrue(String string)
True if the
string represents a truth statement ("1",
"true", "yes", "on"). |
abstract boolean |
nextEvents()
Try to process one graph event, or as few as possible, if more must be
read at once.
|
protected void |
parseError(String message)
Generate a parse error.
|
protected void |
popTokenizer()
Remove the current tokenizer from the stack and restore the previous one
(if any).
|
protected void |
pushBack()
Push back the last read thing, so that it can be read anew.
|
protected void |
pushTokenizer(InputStream stream)
Push a tokenizer created from a stream on the file stack and make it
current.
|
protected void |
pushTokenizer(InputStream stream,
String name)
Push a tokenizer created from a stream on the file stack and make it
current.
|
protected void |
pushTokenizer(Reader reader)
Push a tokenizer created from a reader on the file stack and make it
current.
|
protected void |
pushTokenizer(String file)
Push a tokenizer created from a file name on the file stack and make it
current.
|
protected void |
pushTokenizer(URL url)
Push a tokenizer created from a stream on the file stack and make it
current.
|
void |
readAll(InputStream stream)
Read the whole file in one big non-interruptible operation.
|
void |
readAll(Reader reader)
Read the whole file in one big non-interruptible operation.
|
void |
readAll(String filename)
Read the whole file in one big non-interruptible operation.
|
void |
readAll(URL url)
Read the whole file in one big non-interruptible operation.
|
addAttributeSink, addElementSink, addSink, attributeSinks, clearAttributeSinks, clearElementSinks, clearSinks, elementSinks, manageEvents, removeAttributeSink, removeElementSink, removeSink, sendAttributeChangedEvent, sendAttributeChangedEvent, sendEdgeAdded, sendEdgeAdded, sendEdgeAttributeAdded, sendEdgeAttributeAdded, sendEdgeAttributeChanged, sendEdgeAttributeChanged, sendEdgeAttributeRemoved, sendEdgeAttributeRemoved, sendEdgeRemoved, sendEdgeRemoved, sendGraphAttributeAdded, sendGraphAttributeAdded, sendGraphAttributeChanged, sendGraphAttributeChanged, sendGraphAttributeRemoved, sendGraphAttributeRemoved, sendGraphCleared, sendGraphCleared, sendNodeAdded, sendNodeAdded, sendNodeAttributeAdded, sendNodeAttributeAdded, sendNodeAttributeChanged, sendNodeAttributeChanged, sendNodeAttributeRemoved, sendNodeAttributeRemoved, sendNodeRemoved, sendNodeRemoved, sendStepBegins, sendStepBegins
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
nextStep
addAttributeSink, addElementSink, addSink, clearAttributeSinks, clearElementSinks, clearSinks, removeAttributeSink, removeElementSink, removeSink
protected int QUOTE_CHAR
protected int COMMENT_CHAR
protected boolean eol_is_significant
protected ArrayList<FileSourceBase.CurrentFile> tok_stack
protected StreamTokenizer st
protected HashMap<String,String> attribute_classes
protected FileSourceBase()
protected FileSourceBase(boolean eol_is_significant)
eol_is_significant
- If true EOL will be returned as a token, else it is ignored.protected FileSourceBase(boolean eol_is_significant, int commentChar, int quoteChar)
eol_is_significant
- If true EOL will be returned as a token, else it is ignored.commentChar
- Character used for one line comments.quoteChar
- Character used to enclose quotations.public void readAll(String filename) throws IOException
FileSource
readAll
in interface FileSource
filename
- Name of the file to read.IOException
- If an I/O error occurs while reading.public void readAll(URL url) throws IOException
FileSource
readAll
in interface FileSource
url
- The URL of the file to read.IOException
- If an I/O error occurs while reading.public void readAll(InputStream stream) throws IOException
FileSource
readAll
in interface FileSource
stream
- The input stream to use for reading.IOException
- If an I/O error occurs while reading.public void readAll(Reader reader) throws IOException
FileSource
readAll
in interface FileSource
reader
- The reader to use.IOException
- If an I/O error occurs while reading.public void begin(String filename) throws IOException
FileSource
FileSource.nextEvents()
or
FileSource.nextStep()
. Once begin() has been called, you must finish the
reading process using FileSource.end()
. You cannot call begin() twice
without having called FileSource.end()
in between.begin
in interface FileSource
filename
- Name of the file to read.IOException
- If an I/O error occurs while reading.public void begin(InputStream stream) throws IOException
FileSource
FileSource.nextEvents()
or
FileSource.nextStep()
. Once begin() has been called, you must finish the
reading process using FileSource.end()
. You cannot call begin() twice
without having called FileSource.end()
in between.begin
in interface FileSource
stream
- The input stream to use for reading.IOException
- If an I/O error occurs while reading.public void begin(URL url) throws IOException
FileSource
FileSource.nextEvents()
or
FileSource.nextStep()
. Once begin() has been called, you must finish the
reading process using FileSource.end()
. You cannot call begin() twice
without having called FileSource.end()
in between.begin
in interface FileSource
url
- The URL of the file to read.IOException
- If an I/O error occurs while reading.public void begin(Reader reader) throws IOException
FileSource
FileSource.nextEvents()
or
FileSource.nextStep()
. Once begin() has been called, you must finish the
reading process using FileSource.end()
. You cannot call begin() twice
without having called FileSource.end()
in between.begin
in interface FileSource
reader
- The file reader to use.IOException
- If an I/O error occurs while reading.public abstract boolean nextEvents() throws IOException
FileSource
FileSource.begin(InputStream)
or FileSource.begin(String)
before. This method
return true while there are still events to read.nextEvents
in interface FileSource
IOException
- If an I/O error occurs while reading.public void end() throws IOException
FileSource
FileSource.nextEvents()
or
FileSource.nextStep()
did not returned false). You must call this method
after reading.end
in interface FileSource
IOException
- If an I/O error occurs while closing the file.public void addAttributeClass(String attribute, String attribute_class)
attribute
is found, the corresponding
attribute_class
must be instantiated and inserted in the
current element being parsed. This is equivalent to the "map" keyword of
the GML file. An attribute appears in a GML file as a name followed by a
"[...]" block. The contents of this block defines sub-attributes that
must map to public fields of the attribute. Only attributes that are not
handled specifically by this parser can be added.attribute
- must name the attribute.attribute_class
- must be the complete name of a Java class that will represent
the attribute.protected void include(String file) throws IOException
file
. This pushes a new tokenizer
on the input stack, calls the continueParsingInInclude()
method
(that must be implemented to read the include contents) and when finished
pops the tokenizer of the input stack.IOException
protected abstract void continueParsingInInclude() throws IOException
IOException
protected void pushTokenizer(String file) throws IOException
file
- Name of the file used as source for the tokenizer.IOException
protected Reader createReaderFrom(String file) throws FileNotFoundException
file
- File name to be opened.FileNotFoundException
- If the given file does not exist or un readable.protected Reader createReaderFrom(InputStream stream)
stream
- Input stream to be open as a reader.protected void pushTokenizer(URL url) throws IOException
url
- The URL used as source for the tokenizer.IOException
protected void pushTokenizer(InputStream stream) throws IOException
stream
- The stream used as source for the tokenizer.IOException
protected void pushTokenizer(InputStream stream, String name) throws IOException
stream
- The stream used as source for the tokenizer.name
- The name of the input stream.IOException
protected void pushTokenizer(Reader reader) throws IOException
reader
- The reader used as source for the tokenizer.IOException
private StreamTokenizer createTokenizer(Reader reader) throws IOException
reader
- The reader.IOException
- For any I/O error.protected void configureTokenizer(StreamTokenizer tok) throws IOException
IOException
protected void popTokenizer() throws IOException
IOException
protected void pushBack()
protected void eatEof() throws IOException
IOException
protected void eatEol() throws IOException
IOException
protected int eatEolOrEof() throws IOException
IOException
protected void eatWord(String word) throws IOException
word
token or generate a parse error.IOException
protected void eatWords(String... words) throws IOException
words
- The expected words.IOException
protected String eatOneOfTwoWords(String word1, String word2) throws IOException
word1
- The first word to eat.word2
- The alternative word to eat.IOException
protected void eatSymbol(char symbol) throws IOException
IOException
protected int eatSymbols(String symbols) throws IOException
symbols
or generate a parse
error none of symbols
can be found.IOException
protected void eatWordOrPushbak(String word) throws IOException
word
or push back what was read so that it
can be read anew.IOException
protected void eatSymbolOrPushback(char symbol) throws IOException
symbol
or push back what was read so that
it can be read anew.IOException
protected void eatAllUntilEol() throws IOException
IOException
protected void eatAllEols() throws IOException
IOException
protected String getWord() throws IOException
IOException
protected char getSymbol() throws IOException
IOException
protected char getSymbolOrPushback() throws IOException
IOException
protected String getString() throws IOException
IOException
protected String getWordOrNumber() throws IOException
IOException
protected String getStringOrNumber() throws IOException
IOException
protected String getStringOrWordOrNumberOrPushback() throws IOException
IOException
protected String getStringOrWordOrNumber() throws IOException
IOException
protected Object getStringOrWordOrNumberO() throws IOException
IOException
protected Object getStringOrWordOrSymbolOrNumberO() throws IOException
IOException
protected String getWordOrString() throws IOException
IOException
protected String getWordOrSymbol() throws IOException
IOException
protected String getWordOrSymbolOrPushback() throws IOException
IOException
protected String getWordOrSymbolOrString() throws IOException
IOException
protected String getAllExceptedEof() throws IOException
IOException
protected String getWordOrSymbolOrEof() throws IOException
IOException
protected String getWordOrSymbolOrStringOrEolOrEof() throws IOException
IOException
protected String getWordOrNumberOrStringOrEolOrEof() throws IOException
IOException
protected String getWordOrStringOrEolOrEof() throws IOException
IOException
protected String getWordOrSymbolOrNumberOrStringOrEolOrEof() throws IOException
IOException
protected double getNumber() throws IOException
IOException
protected double getNumberExp() throws IOException
IOException
protected String gotWhat(int token)
token
.protected void parseError(String message) throws IOException
IOException
protected boolean isTrue(String string)
string
represents a truth statement ("1",
"true", "yes", "on").protected boolean isFalse(String string)
string
represents a false statement ("0",
"false", "no", "off").protected boolean getBoolean(String value) throws NumberFormatException
isTrue(String)
and isFalse(String)
to determine if
value
is a truth value and return the corresponding boolean.NumberFormatException
- if the value
is not a truth value.protected double getReal(String value) throws NumberFormatException
value
into a double.NumberFormatException
- if the value
is not a double.protected long getInteger(String value) throws NumberFormatException
value
into a long.NumberFormatException
- if the value
is not a long.protected Point3 getPoint3(String value) throws NumberFormatException
NumberFormatException
WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses