public abstract class XMLParser extends Object
The class proposes several helper functions used to load and interpret a document; IO operations use XMLIO
methods. It parses the root node of the document to determine the version of the XSD schema used to format the
document; then it delegates the interpretation of XML element to subclasses.
parseRootElement(org.w3c.dom.Element)
Modifier and Type | Class and Description |
---|---|
protected static interface |
XMLParser.ElementVisitor
A visitor class that may raise a
XMLParser.ParserException . |
static class |
XMLParser.ParserException
Exception raised whenever an error occurs while interpreting the XML document.
|
Modifier | Constructor and Description |
---|---|
protected |
XMLParser(boolean validateDocument) |
Modifier and Type | Method and Description |
---|---|
String |
getVersion()
Gets the version of the interpreted document.
|
protected static void |
mapElementChildrenOf(Node parent,
XMLParser.ElementVisitor v)
Applies a visitor function to all children elements of a node.
|
void |
parse(Document doc)
Interprets a JBotSim XML document.
|
void |
parse(InputStream input)
Load and parses the document from the given
input stream . |
void |
parseFromString(String input)
Loads and parses the document from the given
string . |
abstract void |
parseRootElement(Element element)
Interprets the root element of the document.
|
public String getVersion()
document
.public void parse(InputStream input) throws XMLParser.ParserException
input stream
.input
- the stream from which the document is read.XMLParser.ParserException
- raised if an IO error occurs or if the XML document is malformed.public void parseFromString(String input) throws XMLParser.ParserException
string
.input
- the string containing the documentXMLParser.ParserException
- raised if an IO error occurs or if the XML document is malformed.public void parse(Document doc) throws XMLParser.ParserException
First the document
is validated wrt the JBotSim schema and then it is interpreted to build
JBotSim objects. The methods checks if the root element of the document labelled with the jbotsim
keyword and if it has a version
attribute in which case its value is stored (mainly) for subclasses
usage.
jbotsim
element is passed to parseRootElement(Element)
method
that must be implemented by subclasses.doc
- the document
that is interpretedXMLParser.ParserException
- raised if an IO error occurs or if the XML document is malformed.public abstract void parseRootElement(Element element) throws XMLParser.ParserException
version
attribute of the root element.element
- the element to be interpreted.XMLParser.ParserException
- raised if an IO error occurs or if the XML document is malformed.protected static void mapElementChildrenOf(Node parent, XMLParser.ElementVisitor v) throws XMLParser.ParserException
parent
- the node for which the children are visited.v
- the visitor function.XMLParser.ParserException
- raised if an IO error occurs or if the XML document is malformed.