Package org.eclipse.rdf4j.common.xml
Class SimpleSAXParser
- java.lang.Object
-
- org.eclipse.rdf4j.common.xml.SimpleSAXParser
-
public class SimpleSAXParser extends java.lang.Object
An XML parser that generates "simple" SAX-like events from a limited subset of XML documents. The SimpleSAXParser can parse simple XML documents; it doesn't support processing instructions or elements that contain both sub-element and character data; character data is only supported in the "leaves" of the XML element tree.Example:
Parsing the following XML:
<?xml version='1.0' encoding='UTF-8'?> <xml-doc> <foo a="1" b="2&3"/> <bar>Hello World!</bar> </xml-doc>
will result in the following method calls to the SimpleSAXListener:
startDocument() startTag("xml-doc", emptyMap, "") startTag("foo", a_b_Map, "") endTag("foo") startTag("bar", emptyMap, "Hello World!") endTag("bar") endTag("xml-doc") endDocument()
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) class
SimpleSAXParser.SimpleSAXDefaultHandler
-
Field Summary
Fields Modifier and Type Field Description private SimpleSAXListener
listener
The listener to report the events to.private org.xml.sax.Locator
locator
A Locator indicating a position in the text that is currently being parsed by the SAX parser.private boolean
preserveWhitespace
Flag indicating whether leading and trailing whitespace in text elements should be preserved.private org.xml.sax.XMLReader
xmlReader
The XMLReader to use for parsing the XML.
-
Constructor Summary
Constructors Constructor Description SimpleSAXParser()
Creates a new SimpleSAXParser that will try to create a new XMLReader using info.aduna.xml.XMLReaderFactory for parsing the XML.SimpleSAXParser(org.xml.sax.XMLReader xmlReader)
Creates a new SimpleSAXParser that will use the supplied XMLReader for parsing the XML.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description SimpleSAXListener
getListener()
Gets the listener that currently will receive any events from this parser.org.xml.sax.Locator
getLocator()
boolean
isPreserveWhitespace()
Checks whether leading and trailing whitespace characters in text elements are preserved.void
parse(java.io.File file)
Parses the content of the supplied File as XML.void
parse(java.io.InputStream in)
Parses the content of the supplied InputStream as XML.void
parse(java.io.Reader reader)
Parses the content of the supplied Reader as XML.void
parse(org.xml.sax.InputSource inputSource)
Parses the content of the supplied InputSource as XML.void
setListener(SimpleSAXListener listener)
Sets the (new) listener that should receive any events from this parser.void
setPreserveWhitespace(boolean preserveWhitespace)
Sets whether leading and trailing whitespace characters in text elements should be preserved.
-
-
-
Field Detail
-
xmlReader
private final org.xml.sax.XMLReader xmlReader
The XMLReader to use for parsing the XML.
-
listener
private SimpleSAXListener listener
The listener to report the events to.
-
preserveWhitespace
private boolean preserveWhitespace
Flag indicating whether leading and trailing whitespace in text elements should be preserved.
-
locator
private org.xml.sax.Locator locator
A Locator indicating a position in the text that is currently being parsed by the SAX parser.
-
-
Constructor Detail
-
SimpleSAXParser
public SimpleSAXParser(org.xml.sax.XMLReader xmlReader)
Creates a new SimpleSAXParser that will use the supplied XMLReader for parsing the XML. One must set a SimpleSAXListener on this object before calling one of the parse() methods.- Parameters:
xmlReader
- The XMLReader to use for parsing.- See Also:
setListener(org.eclipse.rdf4j.common.xml.SimpleSAXListener)
-
SimpleSAXParser
public SimpleSAXParser() throws org.xml.sax.SAXException
Creates a new SimpleSAXParser that will try to create a new XMLReader using info.aduna.xml.XMLReaderFactory for parsing the XML. One must set a SimpleSAXListener on this object before calling one of the parse() methods.- Throws:
org.xml.sax.SAXException
- If the SimpleSAXParser was unable to create an XMLReader.- See Also:
setListener(org.eclipse.rdf4j.common.xml.SimpleSAXListener)
,XMLReader
,XMLReaderFactory
-
-
Method Detail
-
setListener
public void setListener(SimpleSAXListener listener)
Sets the (new) listener that should receive any events from this parser. This listener will replace any previously set listener.- Parameters:
listener
- The (new) listener for events from this parser.
-
getListener
public SimpleSAXListener getListener()
Gets the listener that currently will receive any events from this parser.- Returns:
- The listener for events from this parser.
-
getLocator
public org.xml.sax.Locator getLocator()
-
setPreserveWhitespace
public void setPreserveWhitespace(boolean preserveWhitespace)
Sets whether leading and trailing whitespace characters in text elements should be preserved. Such whitespace characters are discarded by default.
-
isPreserveWhitespace
public boolean isPreserveWhitespace()
Checks whether leading and trailing whitespace characters in text elements are preserved. Defaults to false.
-
parse
public void parse(java.io.File file) throws org.xml.sax.SAXException, java.io.IOException
Parses the content of the supplied File as XML.- Parameters:
file
- The file containing the XML to parse.- Throws:
org.xml.sax.SAXException
java.io.IOException
-
parse
public void parse(java.io.InputStream in) throws org.xml.sax.SAXException, java.io.IOException
Parses the content of the supplied InputStream as XML.- Parameters:
in
- An InputStream containing XML data.- Throws:
org.xml.sax.SAXException
java.io.IOException
-
parse
public void parse(java.io.Reader reader) throws org.xml.sax.SAXException, java.io.IOException
Parses the content of the supplied Reader as XML.- Parameters:
reader
- A Reader containing XML data.- Throws:
org.xml.sax.SAXException
java.io.IOException
-
parse
public void parse(org.xml.sax.InputSource inputSource) throws org.xml.sax.SAXException, java.io.IOException
Parses the content of the supplied InputSource as XML.- Parameters:
inputSource
- An InputSource containing XML data.- Throws:
org.xml.sax.SAXException
java.io.IOException
-
-