org.jrdf.parser.rdfxml
Class RdfXmlParser

java.lang.Object
  extended by org.jrdf.parser.rdfxml.RdfXmlParser
All Implemented Interfaces:
ConfigurableParser, Parser, ParserConfiguration, StatementHandlerConfiguration

public final class RdfXmlParser
extends Object
implements ConfigurableParser

A parser for XML-serialized RDF. This parser operates directly on the SAX events generated by a SAX-enabled XML parser. The XML parser should be compliant with SAX2. You should specify which SAX parser should be used by setting the org.xml.sax.driver property. This parser is not thread-safe, therefore it's public methods are synchronized.

To parse a document using this parser:

Example code:
 Graph jrdfGraph = new GraphImpl();
 Parser parser = new RdfXmlParser(jrdfGraph.getElementFactory());
 parser.setStatementHandler(myStatementHandler);
 parser.setParseErrorListener(myParseErrorListener);
 parser.setVerifyData(true);
 parser.setStopAtFirstError(false);
 

// Parse the data from inputStream, resolving any relative URIs against http://foo/bar: parser.parse(inputStream, "http://foo/bar");


Field Summary
 
Fields inherited from interface org.jrdf.parser.ParserConfiguration
DT_IGNORE, DT_NORMALIZE, DT_VERIFY
 
Constructor Summary
RdfXmlParser(GraphElementFactory graphElementFactory)
          Creates a new RdfXmlParser that will use the supplied GraphElementFactory to create objects for resources, bNodes and literals.
RdfXmlParser(GraphElementFactory graphElementFactory, MapFactory creator)
          Creates a new RdfXmlParser that will use the supplied BlankNodeFactoryCreator and create a new map to be used by the generic ParserBlankNodeFactoryImpl.
RdfXmlParser(GraphElementFactory valueFactory, ParserBlankNodeFactory parserBlankNodeFactory)
          Creates a new RdfXmlParser that will use the supplied GraphElementFactory to create objects for resources, bNodes and literals.
 
Method Summary
 boolean getParseStandAloneDocuments()
          Returns whether the parser is currently in a mode to parse stand-alone RDF documents.
 void parse(InputStream in, String baseURI)
          Parses the data from the supplied InputStream, using the supplied BASE_URI to resolve any relative URI references.
 void parse(Reader reader, String baseURI)
          Parses the data from the supplied Reader, using the supplied BASE_URI to resolve any relative URI references.
 void setDatatypeHandling(int datatypeHandling)
          Sets the datatype handling mode.
 void setNamespaceListener(NamespaceListener nl)
          Sets the NamespaceListener that will be notified of any namespace declarations that the parser finds during parsing.
 void setParseErrorListener(ParseErrorListener el)
          Sets the ParseErrorListener that will be notified of any errors that this parser finds during parsing.
 void setParseLocationListener(ParseLocationListener ll)
          Sets the ParseLocationListener that will be notified of the parser's progress during the parse process.
 void setParseStandAloneDocuments(boolean standAloneDocs)
          Sets the parser in a mode to parse stand-alone RDF documents.
 void setPreserveBNodeIds(boolean preserveBNodeIds)
          Set whether the parser should preserve bnode identifiers specified in the source (default is false).
 void setStatementHandler(StatementHandler sh)
          Sets the StatementHandler that will be notified of statements that are parsed by this parser.
 void setStopAtFirstError(boolean stopAtFirstError)
          Sets whether the parser should stop immediately if it finds an error in the data (default value is true).
 void setVerifyData(boolean verifyData)
          Sets whether the parser should verify the data it parses (default value is true).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RdfXmlParser

public RdfXmlParser(GraphElementFactory graphElementFactory)
             throws GraphException
Creates a new RdfXmlParser that will use the supplied GraphElementFactory to create objects for resources, bNodes and literals. This create an in memory blank node factory which may exhaust available memory.

Parameters:
graphElementFactory - A GraphElementFactory.
Throws:
GraphException

RdfXmlParser

public RdfXmlParser(GraphElementFactory graphElementFactory,
                    MapFactory creator)
             throws GraphException
Creates a new RdfXmlParser that will use the supplied BlankNodeFactoryCreator and create a new map to be used by the generic ParserBlankNodeFactoryImpl.

Parameters:
graphElementFactory - A GraphElementFactory.
creator - A BlankNodeFactoryCreator.
Throws:
GraphException

RdfXmlParser

public RdfXmlParser(GraphElementFactory valueFactory,
                    ParserBlankNodeFactory parserBlankNodeFactory)
             throws GraphException
Creates a new RdfXmlParser that will use the supplied GraphElementFactory to create objects for resources, bNodes and literals.

Parameters:
valueFactory - A GraphElementFactory.
parserBlankNodeFactory - A ParserBlankNodeFactory.
Throws:
GraphException
Method Detail

setStatementHandler

public void setStatementHandler(StatementHandler sh)
Description copied from interface: StatementHandlerConfiguration
Sets the StatementHandler that will be notified of statements that are parsed by this parser.

Specified by:
setStatementHandler in interface StatementHandlerConfiguration
Parameters:
sh - the StatementHandler.

setParseErrorListener

public void setParseErrorListener(ParseErrorListener el)
Description copied from interface: ParserConfiguration
Sets the ParseErrorListener that will be notified of any errors that this parser finds during parsing.

Specified by:
setParseErrorListener in interface ParserConfiguration
Parameters:
el - the error listener.

setParseLocationListener

public void setParseLocationListener(ParseLocationListener ll)
Description copied from interface: ParserConfiguration
Sets the ParseLocationListener that will be notified of the parser's progress during the parse process.

Specified by:
setParseLocationListener in interface ParserConfiguration
Parameters:
ll - the parser location listener.

setNamespaceListener

public void setNamespaceListener(NamespaceListener nl)
Description copied from interface: ParserConfiguration
Sets the NamespaceListener that will be notified of any namespace declarations that the parser finds during parsing.

Specified by:
setNamespaceListener in interface ParserConfiguration
Parameters:
nl - the namespace listener.

setVerifyData

public void setVerifyData(boolean verifyData)
Description copied from interface: ParserConfiguration
Sets whether the parser should verify the data it parses (default value is true).

Specified by:
setVerifyData in interface ParserConfiguration
Parameters:
verifyData - true to verify the data parsed in.

setPreserveBNodeIds

public void setPreserveBNodeIds(boolean preserveBNodeIds)
Description copied from interface: ParserConfiguration
Set whether the parser should preserve bnode identifiers specified in the source (default is false).

Specified by:
setPreserveBNodeIds in interface ParserConfiguration
Parameters:
preserveBNodeIds - true to presever blank node identifier.

setStopAtFirstError

public void setStopAtFirstError(boolean stopAtFirstError)
Description copied from interface: ParserConfiguration
Sets whether the parser should stop immediately if it finds an error in the data (default value is true).

Specified by:
setStopAtFirstError in interface ParserConfiguration
Parameters:
stopAtFirstError - true if an error should stop parsing.

setDatatypeHandling

public void setDatatypeHandling(int datatypeHandling)
Description copied from interface: ParserConfiguration
Sets the datatype handling mode. There are three modes for handling datatyped literals: ignore, verify and normalize. If set to ignore, no special action will be taken to handle datatyped literals. If set to verify (the default value), any literals with known (XML Schema built-in) datatypes are checked to see if their values are valid. If set to normalize, the literal values are not only checked, but also normalized to their canonical representation. The default value is verify.

Specified by:
setDatatypeHandling in interface ParserConfiguration
Parameters:
datatypeHandling - One of the constants DT_IGNORE, DT_VERIFY or DT_NORMALIZE.
See Also:
ParserConfiguration.DT_IGNORE, ParserConfiguration.DT_VERIFY, ParserConfiguration.DT_NORMALIZE

setParseStandAloneDocuments

public void setParseStandAloneDocuments(boolean standAloneDocs)
Description copied from interface: ParserConfiguration
Sets the parser in a mode to parse stand-alone RDF documents. In stand-alone RDF documents, the enclosing rdf:RDF root element is optional if this root element contains just one element (e.g. rdf:Description.

Specified by:
setParseStandAloneDocuments in interface ParserConfiguration
Parameters:
standAloneDocs - true in standad alone mode.

getParseStandAloneDocuments

public boolean getParseStandAloneDocuments()
Returns whether the parser is currently in a mode to parse stand-alone RDF documents.

See Also:
setParseStandAloneDocuments(boolean)

parse

public void parse(InputStream in,
                  String baseURI)
           throws IOException,
                  ParseException,
                  StatementHandlerException
Parses the data from the supplied InputStream, using the supplied BASE_URI to resolve any relative URI references.

Specified by:
parse in interface Parser
Parameters:
in - The InputStream from which to read the data.
baseURI - The URI associated with the data in the InputStream.
Throws:
IOException - If an I/O error occurred while data was read from the InputStream.
ParseException - If the parser has found an unrecoverable parse error.
StatementHandlerException - If the configured statement handler encountered an unrecoverable error.
IllegalArgumentException - If the supplied input stream or base URI is null.

parse

public void parse(Reader reader,
                  String baseURI)
           throws IOException,
                  ParseException,
                  StatementHandlerException
Parses the data from the supplied Reader, using the supplied BASE_URI to resolve any relative URI references.

Specified by:
parse in interface Parser
Parameters:
reader - The Reader from which to read the data.
baseURI - The URI associated with the data in the InputStream.
Throws:
IOException - If an I/O error occurred while data was read from the InputStream.
ParseException - If the parser has found an unrecoverable parse error.
StatementHandlerException - If the configured statement handler has encountered an unrecoverable error.
IllegalArgumentException - If the supplied reader or base URI is null.