Package org.eclipse.rdf4j.rio.helpers
Class AbstractRDFParser
- java.lang.Object
-
- org.eclipse.rdf4j.rio.helpers.AbstractRDFParser
-
- All Implemented Interfaces:
RDFParser
- Direct Known Subclasses:
BinaryRDFParser,HDTParser,JSONLDParser,NTriplesParser,RDFJSONParser,TurtleParser,XMLReaderBasedParser
public abstract class AbstractRDFParser extends java.lang.Object implements RDFParser
Base class forRDFParsers offering common functionality for RDF parsers.
-
-
Field Summary
Fields Modifier and Type Field Description private ParsedIRIbaseURIThe base URI for resolving relative URIs.private ParseErrorListenererrListenerAn optional ParseErrorListener to report parse errors to.private ParseLocationListenerlocationListenerAn optional ParseLocationListener to report parse progress in the form of line- and column numbers to.private java.security.MessageDigestmd5private java.util.Map<java.lang.String,java.lang.String>namespaceTableMapping from namespace prefixes to namespace names.private java.lang.StringnextBNodePrefixEnables a consistent global mapping of blank node identifiers without using a map, but concatenating this as a prefix for the blank node identifiers supplied by the parser.private ValueFactoryoriginalValueFactoryprivate ParsedIRIparsedSkolemOriginprivate ParserConfigparserConfigA collection of configuration options for this parser.protected RDFHandlerrdfHandlerThe RDFHandler that will handle the parsed RDF.private java.lang.StringskolemOriginThe base URI for skolemizing IRIs.private static java.lang.StringuniqueIdPrefixprivate static java.util.concurrent.atomic.AtomicLonguniqueIdSuffixprotected ValueFactoryvalueFactoryThe ValueFactory to use for creating RDF model objects.
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractRDFParser()Creates a new RDFParserBase that will use aSimpleValueFactoryto create RDF model objects.protectedAbstractRDFParser(ValueFactory valueFactory)Creates a new RDFParserBase that will use the supplied ValueFactory to create RDF model objects.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected voidclear()Clears any information that has been collected while parsing.protected voidclearBNodeIDMap()Deprecated.Map is no longer used, callclear()instead.protected BNodecreateBNode()Deprecated.protected BNodecreateBNode(java.lang.String nodeID)Deprecated.protected LiteralcreateLiteral(java.lang.String label, java.lang.String lang, IRI datatype)Creates aLiteralobject with the supplied parameters.protected LiteralcreateLiteral(java.lang.String label, java.lang.String lang, IRI datatype, long lineNo, long columnNo)Creates aLiteralobject with the supplied parameters, using the lineNo and columnNo to enhance error messages or exceptions that may be generated during the creation of the literal.protected ResourcecreateNode()protected ResourcecreateNode(java.lang.String nodeID)protected StatementcreateStatement(Resource subj, IRI pred, Value obj)Creates a newStatementobject with the supplied components.protected StatementcreateStatement(Resource subj, IRI pred, Value obj, Resource context)Creates a newStatementobject with the supplied components.private java.lang.StringcreateUniqueBNodePrefix()protected IRIcreateURI(java.lang.String uri)Creates aIRIobject for the specified URI-string.private ParsedIRIgetCachedSkolemOrigin()Parse skolem origin, if setprotected java.lang.StringgetNamespace(java.lang.String prefix)Gets the namespace that is associated with the specified prefix or throws anRDFParseException.ParseErrorListenergetParseErrorListener()ParseLocationListenergetParseLocationListener()ParserConfiggetParserConfig()Retrieves the current parser configuration as a single object.RDFHandlergetRDFHandler()java.util.Collection<RioSetting<?>>getSupportedSettings()protected voidinitializeNamespaceTableFromConfiguration()booleanpreserveBNodeIDs()protected voidreportError(java.lang.Exception e, long lineNo, long columnNo, RioSetting<java.lang.Boolean> relevantSetting)Reports an error with associated line- and column number to the registered ParseErrorListener, if the given setting has been set to true.protected voidreportError(java.lang.Exception e, RioSetting<java.lang.Boolean> relevantSetting)Reports an error with associated line- and column number to the registered ParseErrorListener, if the given setting has been set to true.protected voidreportError(java.lang.String msg, long lineNo, long columnNo, RioSetting<java.lang.Boolean> relevantSetting)Reports an error with associated line- and column number to the registered ParseErrorListener, if the given setting has been set to true.protected voidreportError(java.lang.String msg, java.lang.Exception e, long lineNo, long columnNo, RioSetting<java.lang.Boolean> relevantSetting)Reports an error with associated line- and column number to the registered ParseErrorListener, if the given setting has been set to true.protected voidreportError(java.lang.String msg, RioSetting<java.lang.Boolean> relevantSetting)Reports an error with associated line- and column number to the registered ParseErrorListener, if the given setting has been set to true.protected voidreportFatalError(java.lang.Exception e)Reports a fatal error to the registered ParseErrorListener, if any, and throws a ParseException afterwards.protected voidreportFatalError(java.lang.Exception e, long lineNo, long columnNo)Reports a fatal error with associated line- and column number to the registered ParseErrorListener, if any, and throws a ParseException wrapped the supplied exception afterwards.protected voidreportFatalError(java.lang.String msg)Reports a fatal error to the registered ParseErrorListener, if any, and throws a ParseException afterwards.protected voidreportFatalError(java.lang.String msg, long lineNo, long columnNo)Reports a fatal error with associated line- and column number to the registered ParseErrorListener, if any, and throws a ParseException afterwards.protected voidreportFatalError(java.lang.String message, java.lang.Exception e, long lineNo, long columnNo)Reports a fatal error with associated line- and column number to the registered ParseErrorListener, if any, and throws a ParseException wrapped the supplied exception afterwards.protected voidreportLocation(long lineNo, long columnNo)Reports the specified line- and column number to the registeredParseLocationListener, if any.protected voidreportWarning(java.lang.String msg)Reports a warning to the registered ParseErrorListener, if any.protected voidreportWarning(java.lang.String msg, long lineNo, long columnNo)Reports a warning with associated line- and column number to the registered ParseErrorListener, if any.protected IRIresolveURI(java.lang.String uriSpec)Resolves a URI-string against the base URI and creates aIRIobject for it.<T> RDFParserset(RioSetting<T> setting, T value)Set a setting on the parser, and return this parser object to allow chaining.protected voidsetBaseURI(java.lang.String uriSpec)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.protected voidsetNamespace(java.lang.String prefix, java.lang.String namespace)Associates the specified prefix to the specified namespace.RDFParsersetParseErrorListener(ParseErrorListener el)Sets the ParseErrorListener that will be notified of any errors that this parser finds during parsing.RDFParsersetParseLocationListener(ParseLocationListener el)Sets the ParseLocationListener that will be notified of the parser's progress during the parse process.RDFParsersetParserConfig(ParserConfig config)Sets all supplied parser configuration options.voidsetPreserveBNodeIDs(boolean preserveBNodeIDs)Set whether the parser should preserve bnode identifiers specified in the source (default is false).RDFParsersetRDFHandler(RDFHandler handler)Sets the RDFHandler that will handle the parsed RDF data.RDFParsersetValueFactory(ValueFactory valueFactory)Sets the ValueFactory that the parser will use to create Value objects for the parsed RDF data.
-
-
-
Field Detail
-
uniqueIdPrefix
private static final java.lang.String uniqueIdPrefix
-
uniqueIdSuffix
private static final java.util.concurrent.atomic.AtomicLong uniqueIdSuffix
-
md5
private final java.security.MessageDigest md5
-
rdfHandler
protected RDFHandler rdfHandler
The RDFHandler that will handle the parsed RDF.
-
errListener
private ParseErrorListener errListener
An optional ParseErrorListener to report parse errors to.
-
locationListener
private ParseLocationListener locationListener
An optional ParseLocationListener to report parse progress in the form of line- and column numbers to.
-
valueFactory
protected ValueFactory valueFactory
The ValueFactory to use for creating RDF model objects.
-
originalValueFactory
private ValueFactory originalValueFactory
-
baseURI
private ParsedIRI baseURI
The base URI for resolving relative URIs.
-
skolemOrigin
private java.lang.String skolemOrigin
The base URI for skolemizing IRIs.
-
parsedSkolemOrigin
private ParsedIRI parsedSkolemOrigin
-
nextBNodePrefix
private java.lang.String nextBNodePrefix
Enables a consistent global mapping of blank node identifiers without using a map, but concatenating this as a prefix for the blank node identifiers supplied by the parser.
-
namespaceTable
private final java.util.Map<java.lang.String,java.lang.String> namespaceTable
Mapping from namespace prefixes to namespace names.
-
parserConfig
private ParserConfig parserConfig
A collection of configuration options for this parser.
-
-
Constructor Detail
-
AbstractRDFParser
protected AbstractRDFParser()
Creates a new RDFParserBase that will use aSimpleValueFactoryto create RDF model objects.
-
AbstractRDFParser
protected AbstractRDFParser(ValueFactory valueFactory)
Creates a new RDFParserBase that will use the supplied ValueFactory to create RDF model objects.- Parameters:
valueFactory- A ValueFactory.
-
-
Method Detail
-
setValueFactory
public RDFParser setValueFactory(ValueFactory valueFactory)
Description copied from interface:RDFParserSets the ValueFactory that the parser will use to create Value objects for the parsed RDF data.- Specified by:
setValueFactoryin interfaceRDFParser- Parameters:
valueFactory- The value factory that the parser should use.- Returns:
- Either a copy of this parser, if it is immutable, or this object, to allow chaining of method calls.
-
setRDFHandler
public RDFParser setRDFHandler(RDFHandler handler)
Description copied from interface:RDFParserSets the RDFHandler that will handle the parsed RDF data.- Specified by:
setRDFHandlerin interfaceRDFParser- Parameters:
handler- The RDFHandler to handle the parsed data.- Returns:
- Either a copy of this parser, if it is immutable, or this object, to allow chaining of method calls.
-
getRDFHandler
public RDFHandler getRDFHandler()
-
setParseErrorListener
public RDFParser setParseErrorListener(ParseErrorListener el)
Description copied from interface:RDFParserSets the ParseErrorListener that will be notified of any errors that this parser finds during parsing.- Specified by:
setParseErrorListenerin interfaceRDFParser- Parameters:
el- The ParseErrorListener that will be notified of errors or warnings.- Returns:
- Either a copy of this parser, if it is immutable, or this object, to allow chaining of method calls.
-
getParseErrorListener
public ParseErrorListener getParseErrorListener()
-
setParseLocationListener
public RDFParser setParseLocationListener(ParseLocationListener el)
Description copied from interface:RDFParserSets the ParseLocationListener that will be notified of the parser's progress during the parse process.- Specified by:
setParseLocationListenerin interfaceRDFParser- Parameters:
el- The ParseLocationListener that will be notified of the parser's progress.- Returns:
- Either a copy of this parser, if it is immutable, or this object, to allow chaining of method calls.
-
getParseLocationListener
public ParseLocationListener getParseLocationListener()
-
setParserConfig
public RDFParser setParserConfig(ParserConfig config)
Description copied from interface:RDFParserSets all supplied parser configuration options.- Specified by:
setParserConfigin interfaceRDFParser- Parameters:
config- a parser configuration object.- Returns:
- Either a copy of this parser, if it is immutable, or this object, to allow chaining of method calls.
-
getParserConfig
public ParserConfig getParserConfig()
Description copied from interface:RDFParserRetrieves the current parser configuration as a single object.- Specified by:
getParserConfigin interfaceRDFParser- Returns:
- a parser configuration object representing the current configuration of the parser.
-
getSupportedSettings
public java.util.Collection<RioSetting<?>> getSupportedSettings()
- Specified by:
getSupportedSettingsin interfaceRDFParser- Returns:
- A collection of
RioSettings that are supported by this RDFParser.
-
set
public <T> RDFParser set(RioSetting<T> setting, T value)
Description copied from interface:RDFParserSet a setting on the parser, and return this parser object to allow chaining.
-
setPreserveBNodeIDs
public void setPreserveBNodeIDs(boolean preserveBNodeIDs)
Description copied from interface:RDFParserSet whether the parser should preserve bnode identifiers specified in the source (default is false).- Specified by:
setPreserveBNodeIDsin interfaceRDFParser
-
preserveBNodeIDs
public boolean preserveBNodeIDs()
-
setBaseURI
protected void setBaseURI(java.lang.String uriSpec)
Parses the supplied URI-string and sets it as the base URI for resolving relative URIs.
-
setBaseURI
protected void setBaseURI(ParsedIRI baseURI)
Sets the base URI for resolving relative URIs.
-
setNamespace
protected void setNamespace(java.lang.String prefix, java.lang.String namespace)Associates the specified prefix to the specified namespace.
-
getNamespace
protected java.lang.String getNamespace(java.lang.String prefix) throws RDFParseExceptionGets the namespace that is associated with the specified prefix or throws anRDFParseException.- Throws:
RDFParseException- if no namespace is associated with this prefix
-
clear
protected void clear()
Clears any information that has been collected while parsing. This method must be called by subclasses when finishing the parse process.
-
initializeNamespaceTableFromConfiguration
protected void initializeNamespaceTableFromConfiguration()
-
clearBNodeIDMap
@Deprecated protected void clearBNodeIDMap()
Deprecated.Map is no longer used, callclear()instead.Clears the map that keeps track of blank nodes that have been parsed. Normally, this map is clear when the document has been parsed completely, but subclasses can clear the map at other moments too, for example when a bnode scope ends.
-
resolveURI
protected IRI resolveURI(java.lang.String uriSpec) throws RDFParseException
Resolves a URI-string against the base URI and creates aIRIobject for it.- Throws:
RDFParseException
-
createURI
protected IRI createURI(java.lang.String uri) throws RDFParseException
Creates aIRIobject for the specified URI-string.- Throws:
RDFParseException
-
createNode
protected Resource createNode() throws RDFParseException
- Returns:
- blank node or skolem IRI
- Throws:
RDFParseException
-
createNode
protected Resource createNode(java.lang.String nodeID) throws RDFParseException
- Parameters:
nodeID- node identifier- Returns:
- blank node or skolem IRI
- Throws:
RDFParseException
-
createBNode
@Deprecated protected BNode createBNode() throws RDFParseException
Deprecated.Creates a newBNodeobject.- Throws:
RDFParseException
-
createBNode
@Deprecated protected BNode createBNode(java.lang.String nodeID) throws RDFParseException
Deprecated.Creates aBNodeobject for the specified identifier.- Throws:
RDFParseException
-
createLiteral
protected Literal createLiteral(java.lang.String label, java.lang.String lang, IRI datatype) throws RDFParseException
Creates aLiteralobject with the supplied parameters.- Throws:
RDFParseException
-
createLiteral
protected Literal createLiteral(java.lang.String label, java.lang.String lang, IRI datatype, long lineNo, long columnNo) throws RDFParseException
Creates aLiteralobject with the supplied parameters, using the lineNo and columnNo to enhance error messages or exceptions that may be generated during the creation of the literal.
-
createStatement
protected Statement createStatement(Resource subj, IRI pred, Value obj) throws RDFParseException
Creates a newStatementobject with the supplied components.- Throws:
RDFParseException
-
createStatement
protected Statement createStatement(Resource subj, IRI pred, Value obj, Resource context) throws RDFParseException
Creates a newStatementobject with the supplied components.- Throws:
RDFParseException
-
reportLocation
protected void reportLocation(long lineNo, long columnNo)Reports the specified line- and column number to the registeredParseLocationListener, if any.
-
reportWarning
protected void reportWarning(java.lang.String msg)
Reports a warning to the registered ParseErrorListener, if any. This method simply callsreportWarning(String, long, long)supplying -1 for the line- and column number.
-
reportWarning
protected void reportWarning(java.lang.String msg, long lineNo, long columnNo)Reports a warning with associated line- and column number to the registered ParseErrorListener, if any.
-
reportError
protected void reportError(java.lang.String msg, RioSetting<java.lang.Boolean> relevantSetting) throws RDFParseExceptionReports an error with associated line- and column number to the registered ParseErrorListener, if the given setting has been set to true.This method also throws an
RDFParseExceptionwhen the given setting has been set to true and it is not a nonFatalError.- Parameters:
msg- The message to use forParseErrorListener.error(String, long, long)and forRDFParseException(String, long, long).relevantSetting- 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
protected void reportError(java.lang.String msg, long lineNo, long columnNo, RioSetting<java.lang.Boolean> relevantSetting) throws RDFParseExceptionReports an error with associated line- and column number to the registered ParseErrorListener, if the given setting has been set to true.This method also throws an
RDFParseExceptionwhen the given setting has been set to true and it is not a nonFatalError.- Parameters:
msg- The message to use forParseErrorListener.error(String, long, long)and forRDFParseException(String, long, long).lineNo- Optional line number, should default to setting this as -1 if not known. Used forParseErrorListener.error(String, long, long)and forRDFParseException(String, long, long).columnNo- Optional column number, should default to setting this as -1 if not known. Used forParseErrorListener.error(String, long, long)and forRDFParseException(String, long, long).relevantSetting- 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
protected void reportError(java.lang.Exception e, RioSetting<java.lang.Boolean> relevantSetting) throws RDFParseExceptionReports an error with associated line- and column number to the registered ParseErrorListener, if the given setting has been set to true.This method also throws an
RDFParseExceptionwhen the given setting has been set to true and it is not a nonFatalError.- Parameters:
e- The exception whose message will be used forParseErrorListener.error(String, long, long)and forRDFParseException(String, long, long).relevantSetting- 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
protected void reportError(java.lang.Exception e, long lineNo, long columnNo, RioSetting<java.lang.Boolean> relevantSetting) throws RDFParseExceptionReports an error with associated line- and column number to the registered ParseErrorListener, if the given setting has been set to true.This method also throws an
RDFParseExceptionwhen the given setting has been set to true and it is not a nonFatalError.- Parameters:
e- The exception whose message will be used forParseErrorListener.error(String, long, long)and forRDFParseException(String, long, long).lineNo- Optional line number, should default to setting this as -1 if not known. Used forParseErrorListener.error(String, long, long)and forRDFParseException(String, long, long).columnNo- Optional column number, should default to setting this as -1 if not known. Used forParseErrorListener.error(String, long, long)and forRDFParseException(String, long, long).relevantSetting- 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
protected void reportError(java.lang.String msg, java.lang.Exception e, long lineNo, long columnNo, RioSetting<java.lang.Boolean> relevantSetting) throws RDFParseExceptionReports an error with associated line- and column number to the registered ParseErrorListener, if the given setting has been set to true.This method also throws an
RDFParseExceptionwhen the given setting has been set to true and it is not a nonFatalError.- Parameters:
msg- The message to use forParseErrorListener.error(String, long, long)and forRDFParseException(String, long, long).e- The exception whose message will be used forParseErrorListener.error(String, long, long)and forRDFParseException(String, long, long).lineNo- Optional line number, should default to setting this as -1 if not known. Used forParseErrorListener.error(String, long, long)and forRDFParseException(String, long, long).columnNo- Optional column number, should default to setting this as -1 if not known. Used forParseErrorListener.error(String, long, long)and forRDFParseException(String, long, long).relevantSetting- 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
protected void reportFatalError(java.lang.String msg) throws RDFParseExceptionReports a fatal error to the registered ParseErrorListener, if any, and throws a ParseException afterwards. This method simply callsreportFatalError(String, long, long)supplying -1 for the line- and column number.- Throws:
RDFParseException
-
reportFatalError
protected void reportFatalError(java.lang.String msg, long lineNo, long columnNo) throws RDFParseExceptionReports a fatal error with associated line- and column number to the registered ParseErrorListener, if any, and throws a ParseException afterwards.- Throws:
RDFParseException
-
reportFatalError
protected void reportFatalError(java.lang.Exception e) throws RDFParseExceptionReports a fatal error to the registered ParseErrorListener, if any, and throws a ParseException afterwards. An exception is made for the case where the supplied exception is aRDFParseException; in that case the supplied exception is not wrapped in another ParseException and the error message is not reported to the ParseErrorListener, assuming that it has already been reported when the original ParseException was thrown.This method simply calls
reportFatalError(Exception, long, long)supplying -1 for the line- and column number.- Throws:
RDFParseException
-
reportFatalError
protected void reportFatalError(java.lang.Exception e, long lineNo, long columnNo) throws RDFParseExceptionReports a fatal error with associated line- and column number to the registered ParseErrorListener, if any, and throws a ParseException wrapped the supplied exception afterwards. An exception is made for the case where the supplied exception is aRDFParseException; in that case the supplied exception is not wrapped in another ParseException and the error message is not reported to the ParseErrorListener, assuming that it has already been reported when the original ParseException was thrown.- Throws:
RDFParseException
-
reportFatalError
protected void reportFatalError(java.lang.String message, java.lang.Exception e, long lineNo, long columnNo) throws RDFParseExceptionReports a fatal error with associated line- and column number to the registered ParseErrorListener, if any, and throws a ParseException wrapped the supplied exception afterwards. An exception is made for the case where the supplied exception is aRDFParseException; in that case the supplied exception is not wrapped in another ParseException and the error message is not reported to the ParseErrorListener, assuming that it has already been reported when the original ParseException was thrown.- Throws:
RDFParseException
-
createUniqueBNodePrefix
private java.lang.String createUniqueBNodePrefix()
-
getCachedSkolemOrigin
private ParsedIRI getCachedSkolemOrigin()
Parse skolem origin, if set- Returns:
- skolem origin or null
-
-