Class RDFXMLParser
java.lang.Object
org.eclipse.rdf4j.rio.helpers.AbstractRDFParser
org.eclipse.rdf4j.rio.helpers.XMLReaderBasedParser
org.eclipse.rdf4j.rio.rdfxml.RDFXMLParser
- All Implemented Interfaces:
RDFParser, ErrorHandler
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:
- Create an instance of RDFXMLParser, optionally supplying it with your own ValueFactory.
- Set the RDFHandler.
- Optionally, set the ParseErrorListener and/or ParseLocationListener.
- Optionally, specify whether the parser should verify the data it parses and whether it should stop immediately when it finds an error in the data (both default to true).
- Call the parse method.
// Use the SAX2-compliant Xerces parser:
System.setProperty("org.xml.sax.driver", "org.apache.xerces.parsers.SAXParser");
RDFParser parser = new RDFXMLParser();
parser.setRDFHandler(myRDFHandler);
parser.setParseErrorListener(myParseErrorListener);
parser.setVerifyData(true);
parser.stopAtFirstError(false);
// Parse the data from inputStream, resolving any
// relative URIs against http://foo/bar:
parser.parse(inputStream, "http://foo/bar");
Note that JAXP entity expansion limits may apply. Check the documentation on limits and using the jaxp.properties file if you get one of the following errors:
JAXP00010001: The parser has encountered more than "64000" entity expansions in this document JAXP00010004: The accumulated size of entities is ... that exceeded the "50,000,000" limit
As a work-around, try passing -Djdk.xml.totalEntitySizeLimit=0 -DentityExpansionLimit=0 to the JVM.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static class(package private) static class -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate StringThe base URI of the document.A stack of node- and property elements.private final SAXFilterA filter filtering calls to SAX methods specifically for this parser.A set containing URIs that have been generated as a result of rdf:ID attributes.private StringThe language of literal values as can be specified using xml:lang attributes.Fields inherited from class AbstractRDFParser
rdfHandler, valueFactory -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new RDFXMLParser that will use aSimpleValueFactoryto create RDF model objects.RDFXMLParser(ValueFactory valueFactory) Creates a new RDFXMLParser that will use the supplied ValueFactory to create RDF model objects. -
Method Summary
Modifier and TypeMethodDescriptionprivate IRIbuildResourceFromLocalName(String localName) Builds a Resource from a non-qualified localname.private IRIbuildURIFromID(String id) Builds a Resource from the value of an rdf:ID attribute.private voidcheckNodeEltName(String namespaceURI, String localName, String qName) Checks whether the node element name is from the RDF namespace and, if so, if it is allowed to be used in a node element.private voidcheckNoMoreAtts(Atts atts) Checks whether 'atts' is empty.private voidcheckPropertyEltName(String namespaceURI, String localName, String qName, RioSetting<Boolean> setting) Checks whether the property element name is from the RDF namespace and, if so, if it is allowed to be used in a property element.private voidcheckRDFAtts(Atts atts) Checks whether 'atts' contains attributes from the RDF namespace that are not allowed as attributes.protected LiteralcreateLiteral(String label, String lang, IRI datatype) Creates aLiteralobject with the supplied parameters.protected ResourcecreateNode(String nodeID) (package private) voidemptyElement(String namespaceURI, String localName, String qName, Atts atts) (package private) void(package private) voidendElement(String namespaceURI, String localName, String qName) voiderror(SAXParseException exception) Implementation of SAX ErrorHandler.errorvoidfatalError(SAXParseException exception) Implementation of SAX ErrorHandler.fatalErrorprivate ResourcegetNodeResource(Atts atts) Retrieves the resource of a node element (subject or object) using relevant attributes (rdf:ID, rdf:about and rdf:nodeID) from its attributes list.booleanReturns whether the parser is currently in a mode to parse stand-alone RDF documents.private ResourcegetPropertyResource(Atts atts) Retrieves the object resource of a property element using relevant attributes (rdf:resource and rdf:nodeID) from its attributes list.final RDFFormatGets the RDF format that this parser can parse.getSAXResult(String baseURI) Collection<RioSetting<?>> private voidhandleReification(Value value) voidparse(InputStream in, String baseURI) Parses the data from the supplied InputStream, using the supplied baseURI to resolve any relative URI references.voidParses the data from the supplied Reader, using the supplied baseURI to resolve any relative URI references.private voidparse(InputSource inputSource) private ObjectpeekStack(int distFromTop) private voidprocessNodeElt(String namespaceURI, String localName, String qName, Atts atts, boolean isEmptyElt) private voidprocessPropertyElt(String namespaceURI, String localName, String qName, Atts atts, boolean isEmptyElt) private voidprocessSubjectAtts(RDFXMLParser.NodeElement nodeElt, Atts atts) processes subject attributes.private voidreifyStatement(Resource reifNode, Resource subj, IRI pred, Value obj) protected voidreportError(Exception e, RioSetting<Boolean> setting) OverridesAbstractRDFParser.reportError(String, RioSetting), adding line- and column number information to the error.protected voidreportError(String msg, RioSetting<Boolean> setting) OverridesAbstractRDFParser.reportError(String, RioSetting), adding line- and column number information to the error.protected voidOverridesAbstractRDFParser.reportFatalError(Exception), adding line- and column number information to the error.protected voidreportFatalError(String msg) OverridesAbstractRDFParser.reportFatalError(String), adding line- and column number information to the error.private voidreportStatement(Resource subject, IRI predicate, Value object) Reports a stament to the configured RDFHandlerException.protected voidreportWarning(String msg) OverridesAbstractRDFParser.reportWarning(String), adding line- and column number information to the error.protected voidsetBaseURI(String baseURI) Parses the supplied URI-string and sets it as the base URI for resolving relative URIs.protected voidsetBaseURI(ParsedIRI baseURI) Sets the base URI for resolving relative URIs.voidsetParseStandAloneDocuments(boolean standAloneDocs) Sets the parser in a mode to parse stand-alone RDF documents.(package private) voidsetXMLLang(String xmlLang) (package private) void(package private) voidstartElement(String namespaceURI, String localName, String qName, Atts atts) (package private) voidprivate booleanvoidwarning(SAXParseException exception) Implementation of SAX ErrorHandler.warningMethods inherited from class XMLReaderBasedParser
getCompulsoryXmlFeatureSettings, getCompulsoryXmlPropertySettings, getOptionalXmlFeatureSettings, getOptionalXmlPropertySettings, getXMLReaderMethods inherited from class AbstractRDFParser
clear, clearBNodeIDMap, createBNode, createBNode, createLiteral, createNode, createStatement, createStatement, createURI, getNamespace, getParseErrorListener, getParseLocationListener, getParserConfig, getRDFHandler, initializeNamespaceTableFromConfiguration, preserveBNodeIDs, reportError, reportError, reportError, reportFatalError, reportFatalError, reportFatalError, reportLocation, reportWarning, resolveURI, set, setNamespace, setParseErrorListener, setParseLocationListener, setParserConfig, setPreserveBNodeIDs, setRDFHandler, setValueFactory
-
Field Details
-
saxFilter
A filter filtering calls to SAX methods specifically for this parser. -
documentURI
The base URI of the document. This variable is set when parse(inputStream, baseURI) is called and will not be changed during parsing. -
xmlLang
The language of literal values as can be specified using xml:lang attributes. This variable is set/modified by the SAXFilter during parsing such that it always represents the language of the context in which elements are reported. -
elementStack
-
usedIDs
-
-
Constructor Details
-
RDFXMLParser
public RDFXMLParser()Creates a new RDFXMLParser that will use aSimpleValueFactoryto create RDF model objects. -
RDFXMLParser
Creates a new RDFXMLParser that will use the supplied ValueFactory to create RDF model objects.- Parameters:
valueFactory- A ValueFactory.
-
-
Method Details
-
getRDFFormat
Description copied from interface:RDFParserGets the RDF format that this parser can parse.- Specified by:
getRDFFormatin interfaceRDFParser
-
setParseStandAloneDocuments
public void setParseStandAloneDocuments(boolean standAloneDocs) 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. -
getParseStandAloneDocuments
public boolean getParseStandAloneDocuments()Returns whether the parser is currently in a mode to parse stand-alone RDF documents.- See Also:
-
parse
public void parse(InputStream in, String baseURI) throws IOException, RDFParseException, RDFHandlerException Description copied from interface:RDFParserParses the data from the supplied InputStream, using the supplied baseURI to resolve any relative URI references.- Specified by:
parsein interfaceRDFParser- Parameters:
in- The InputStream from which to read the data.baseURI- The URI associated with the data in the InputStream. May benull. Parsers for syntax formats that do not support relative URIs will ignore this argument.Note that if the data contains an embedded base URI, that embedded base URI will overrule the value supplied here (see RFC 3986 section 5.1 for details).
- Throws:
IOException- If an I/O error occurred while data was read from the InputStream.RDFParseException- If the parser has found an unrecoverable parse error.RDFHandlerException- If the configured statement handler has encountered an unrecoverable error.
-
parse
public void parse(Reader reader, String baseURI) throws IOException, RDFParseException, RDFHandlerException Description copied from interface:RDFParserParses the data from the supplied Reader, using the supplied baseURI to resolve any relative URI references.- Specified by:
parsein interfaceRDFParser- Parameters:
reader- The Reader from which to read the data.baseURI- The URI associated with the data in the InputStream. May benull. Parsers for syntax formats that do not support relative URIs will ignore this argument.Note that if the data contains an embedded base URI, that embedded base URI will overrule the value supplied here (see RFC 3986 section 5.1 for details).
- Throws:
IOException- If an I/O error occurred while data was read from the InputStream.RDFParseException- If the parser has found an unrecoverable parse error.RDFHandlerException- If the configured statement handler has encountered an unrecoverable error.
-
parse
private void parse(InputSource inputSource) throws IOException, RDFParseException, RDFHandlerException -
getSupportedSettings
- Specified by:
getSupportedSettingsin interfaceRDFParser- Overrides:
getSupportedSettingsin classAbstractRDFParser- Returns:
- A collection of
RioSettings that are supported by this RDFParser.
-
getSAXResult
-
startDocument
- Throws:
RDFParseExceptionRDFHandlerException
-
endDocument
- Throws:
RDFParseExceptionRDFHandlerException
-
setBaseURI
Description copied from class:AbstractRDFParserSets the base URI for resolving relative URIs.- Overrides:
setBaseURIin classAbstractRDFParser
-
setBaseURI
Description copied from class:AbstractRDFParserParses the supplied URI-string and sets it as the base URI for resolving relative URIs.- Overrides:
setBaseURIin classAbstractRDFParser
-
setXMLLang
-
startElement
void startElement(String namespaceURI, String localName, String qName, Atts atts) throws RDFParseException, RDFHandlerException - Throws:
RDFParseExceptionRDFHandlerException
-
endElement
void endElement(String namespaceURI, String localName, String qName) throws RDFParseException, RDFHandlerException - Throws:
RDFParseExceptionRDFHandlerException
-
emptyElement
void emptyElement(String namespaceURI, String localName, String qName, Atts atts) throws RDFParseException, RDFHandlerException - Throws:
RDFParseExceptionRDFHandlerException
-
text
- Throws:
RDFParseExceptionRDFHandlerException
-
processNodeElt
private void processNodeElt(String namespaceURI, String localName, String qName, Atts atts, boolean isEmptyElt) throws RDFParseException, RDFHandlerException - Throws:
RDFParseExceptionRDFHandlerException
-
getNodeResource
Retrieves the resource of a node element (subject or object) using relevant attributes (rdf:ID, rdf:about and rdf:nodeID) from its attributes list.- Returns:
- a resource or a bNode.
- Throws:
RDFParseException
-
processSubjectAtts
private void processSubjectAtts(RDFXMLParser.NodeElement nodeElt, Atts atts) throws RDFParseException, RDFHandlerException processes subject attributes.- Throws:
RDFParseExceptionRDFHandlerException
-
processPropertyElt
private void processPropertyElt(String namespaceURI, String localName, String qName, Atts atts, boolean isEmptyElt) throws RDFParseException, RDFHandlerException - Throws:
RDFParseExceptionRDFHandlerException
-
getPropertyResource
Retrieves the object resource of a property element using relevant attributes (rdf:resource and rdf:nodeID) from its attributes list.- Returns:
- a resource or a bNode.
- Throws:
RDFParseException
-
handleReification
- Throws:
RDFParseExceptionRDFHandlerException
-
reifyStatement
private void reifyStatement(Resource reifNode, Resource subj, IRI pred, Value obj) throws RDFParseException, RDFHandlerException - Throws:
RDFParseExceptionRDFHandlerException
-
buildResourceFromLocalName
Builds a Resource from a non-qualified localname.- Throws:
RDFParseException
-
buildURIFromID
Builds a Resource from the value of an rdf:ID attribute.- Throws:
RDFParseException
-
createNode
Description copied from class:AbstractRDFParser- Overrides:
createNodein classAbstractRDFParser- Parameters:
nodeID- node identifier- Returns:
- blank node or skolem IRI
- Throws:
RDFParseException
-
peekStack
-
topIsProperty
private boolean topIsProperty() -
checkNodeEltName
private void checkNodeEltName(String namespaceURI, String localName, String qName) throws RDFParseException Checks whether the node element name is from the RDF namespace and, if so, if it is allowed to be used in a node element. If the name is equal to one of the disallowed names (RDF, ID, about, parseType, resource, nodeID, datatype and li), an error is generated. If the name is not defined in the RDF namespace, but it claims that it is from this namespace, a warning is generated.- Throws:
RDFParseException
-
checkPropertyEltName
private void checkPropertyEltName(String namespaceURI, String localName, String qName, RioSetting<Boolean> setting) throws RDFParseException Checks whether the property element name is from the RDF namespace and, if so, if it is allowed to be used in a property element. If the name is equal to one of the disallowed names (RDF, ID, about, parseType, resource and li), an error is generated. If the name is not defined in the RDF namespace, but it claims that it is from this namespace, a warning is generated.- Parameters:
setting-- Throws:
RDFParseException
-
checkRDFAtts
Checks whether 'atts' contains attributes from the RDF namespace that are not allowed as attributes. If such an attribute is found, an error is generated and the attribute is removed from 'atts'. If the attribute is not defined in the RDF namespace, but it claims that it is from this namespace, a warning is generated.- Throws:
RDFParseException
-
checkNoMoreAtts
Checks whether 'atts' is empty. If this is not the case, a warning is generated for each attribute that is still present.- Throws:
RDFParseException
-
reportStatement
private void reportStatement(Resource subject, IRI predicate, Value object) throws RDFParseException, RDFHandlerException Reports a stament to the configured RDFHandlerException.- Parameters:
subject- The statement's subject.predicate- The statement's predicate.object- The statement's object.- Throws:
RDFHandlerException- If the configured RDFHandlerException throws an RDFHandlerException.RDFParseException
-
createLiteral
Description copied from class:AbstractRDFParserCreates aLiteralobject with the supplied parameters.- Overrides:
createLiteralin classAbstractRDFParser- Throws:
RDFParseException
-
reportWarning
OverridesAbstractRDFParser.reportWarning(String), adding line- and column number information to the error.- Overrides:
reportWarningin classAbstractRDFParser
-
reportError
OverridesAbstractRDFParser.reportError(String, RioSetting), adding line- and column number information to the error.- Overrides:
reportErrorin classAbstractRDFParser- Parameters:
msg- The message to use forParseErrorListener.error(String, long, long)and forRDFParseException(String, long, long).setting- The boolean setting that will be checked to determine if this is an issue that we need to look at at all. If this setting is true, then the error listener will receive the error, and ifParserConfig.isNonFatalError(RioSetting)returns true an exception will be thrown.- Throws:
RDFParseException- IfRioConfig.get(RioSetting)returns true, andParserConfig.isNonFatalError(RioSetting)returns true for the given setting.
-
reportError
OverridesAbstractRDFParser.reportError(String, RioSetting), adding line- and column number information to the error.- Overrides:
reportErrorin classAbstractRDFParser- Parameters:
e- The exception whose message will be used forParseErrorListener.error(String, long, long)and forRDFParseException(String, long, long).setting- The boolean setting that will be checked to determine if this is an issue that we need to look at at all. If this setting is true, then the error listener will receive the error, and ifParserConfig.isNonFatalError(RioSetting)returns true an exception will be thrown.- Throws:
RDFParseException- IfRioConfig.get(RioSetting)returns true, andParserConfig.isNonFatalError(RioSetting)returns true for the given setting.
-
reportFatalError
OverridesAbstractRDFParser.reportFatalError(String), adding line- and column number information to the error.- Overrides:
reportFatalErrorin classAbstractRDFParser- Throws:
RDFParseException
-
reportFatalError
OverridesAbstractRDFParser.reportFatalError(Exception), adding line- and column number information to the error.- Overrides:
reportFatalErrorin classAbstractRDFParser- Throws:
RDFParseException
-
warning
Implementation of SAX ErrorHandler.warning- Specified by:
warningin interfaceErrorHandler- Throws:
SAXException
-
error
Implementation of SAX ErrorHandler.error- Specified by:
errorin interfaceErrorHandler- Throws:
SAXException
-
fatalError
Implementation of SAX ErrorHandler.fatalError- Specified by:
fatalErrorin interfaceErrorHandler- Throws:
SAXException
-