Package org.eclipse.rdf4j.rio.ntriples
Class NTriplesParser
- java.lang.Object
-
- org.eclipse.rdf4j.rio.helpers.AbstractRDFParser
-
- org.eclipse.rdf4j.rio.ntriples.NTriplesParser
-
- All Implemented Interfaces:
RDFParser
- Direct Known Subclasses:
NQuadsParser
public class NTriplesParser extends AbstractRDFParser
RDF parser for N-Triples files. A specification of NTriples can be found in this section of the RDF Test Cases document. This parser is not thread-safe, therefore its public methods are synchronized.
-
-
Field Summary
Fields Modifier and Type Field Description protected int
currentIndex
protected char[]
lineChars
protected long
lineNo
protected Value
object
protected IRI
predicate
protected java.io.BufferedReader
reader
protected Resource
subject
-
Fields inherited from class org.eclipse.rdf4j.rio.helpers.AbstractRDFParser
rdfHandler, valueFactory
-
-
Constructor Summary
Constructors Constructor Description NTriplesParser()
Creates a new NTriplesParser that will use aSimpleValueFactory
to create object for resources, bNodes and literals.NTriplesParser(ValueFactory valueFactory)
Creates a new NTriplesParser that will use the supplied ValueFactory to create RDF model objects.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
assertLineTerminates()
Verifies that there is only whitespace or comments until the end of the line.protected void
clear()
Clears any information that has been collected while parsing.protected IRI
createURI(java.lang.String uri)
Creates aIRI
object for the specified URI-string.RDFFormat
getRDFFormat()
Gets the RDF format that this parser can parse.java.util.Collection<RioSetting<?>>
getSupportedSettings()
protected void
handleStatement(boolean ignoredAnError)
private void
incrementIndexOrThrowEOF()
Increments the current line index position and asserts EOF is not reached.private void
moveToBNodeEndIndex()
Moves the current line index position to the end of the BNode ID.private void
moveToIRIEndIndex()
Moves the current line index position to the end of the IRI.void
parse(java.io.InputStream in, java.lang.String baseURI)
Parses the data from the supplied InputStream, using the supplied baseURI to resolve any relative URI references.void
parse(java.io.Reader reader, java.lang.String baseURI)
Parses the data from the supplied Reader, using the supplied baseURI to resolve any relative URI references.protected IRI
parseIRI()
private java.lang.String
parseLabel()
private void
parseLangLiteral(java.lang.String label)
private void
parseLiteral()
private void
parseLiteralWithDatatype(java.lang.String label)
protected Resource
parseNode()
protected void
parseObject()
protected void
parsePredicate()
protected void
parseStatement()
protected void
parseSubject()
private boolean
readLine()
Attempts to read the next line from the buffered reader.protected void
reportError(java.lang.Exception e, RioSetting<java.lang.Boolean> setting)
Reports an error with associated line- and column number to the registered ParseErrorListener, if the given setting has been set to true.protected void
reportError(java.lang.String msg, RioSetting<java.lang.Boolean> setting)
OverridesAbstractRDFParser.reportError(String, RioSetting)
, adding line number information to the error.protected void
reportFatalError(java.lang.Exception e)
OverridesAbstractRDFParser.reportFatalError(Exception)
, adding line number information to the error.protected void
reportFatalError(java.lang.String msg)
OverridesAbstractRDFParser.reportFatalError(String)
, adding line number information to the error.protected void
reportWarning(java.lang.String msg)
OverridesAbstractRDFParser.reportWarning(String)
, adding line number information to the error.protected boolean
shouldParseLine()
protected void
skipWhitespace(boolean throwEOF)
protected void
throwEOFException()
-
Methods inherited from class org.eclipse.rdf4j.rio.helpers.AbstractRDFParser
clearBNodeIDMap, createBNode, createBNode, createLiteral, createLiteral, createNode, createNode, createStatement, createStatement, getNamespace, getParseErrorListener, getParseLocationListener, getParserConfig, getRDFHandler, initializeNamespaceTableFromConfiguration, preserveBNodeIDs, reportError, reportError, reportError, reportFatalError, reportFatalError, reportFatalError, reportLocation, reportWarning, resolveURI, set, setBaseURI, setBaseURI, setNamespace, setParseErrorListener, setParseLocationListener, setParserConfig, setPreserveBNodeIDs, setRDFHandler, setValueFactory
-
-
-
-
Constructor Detail
-
NTriplesParser
public NTriplesParser()
Creates a new NTriplesParser that will use aSimpleValueFactory
to create object for resources, bNodes and literals.
-
NTriplesParser
public NTriplesParser(ValueFactory valueFactory)
Creates a new NTriplesParser that will use the supplied ValueFactory to create RDF model objects.- Parameters:
valueFactory
- A ValueFactory.
-
-
Method Detail
-
getRDFFormat
public RDFFormat getRDFFormat()
Description copied from interface:RDFParser
Gets the RDF format that this parser can parse.
-
parse
public void parse(java.io.InputStream in, java.lang.String baseURI) throws java.io.IOException, RDFParseException, RDFHandlerException
Description copied from interface:RDFParser
Parses the data from the supplied InputStream, using the supplied baseURI to resolve any relative URI references.- 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:
java.io.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(java.io.Reader reader, java.lang.String baseURI) throws java.io.IOException, RDFParseException, RDFHandlerException
Description copied from interface:RDFParser
Parses the data from the supplied Reader, using the supplied baseURI to resolve any relative URI references.- 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:
java.io.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.
-
parseStatement
protected void parseStatement() throws RDFParseException, RDFHandlerException
- Throws:
RDFParseException
RDFHandlerException
-
skipWhitespace
protected void skipWhitespace(boolean throwEOF)
-
shouldParseLine
protected boolean shouldParseLine()
-
parseSubject
protected void parseSubject()
-
parsePredicate
protected void parsePredicate()
-
parseObject
protected void parseObject()
-
assertLineTerminates
protected void assertLineTerminates() throws RDFParseException
Verifies that there is only whitespace or comments until the end of the line.- Throws:
RDFParseException
-
handleStatement
protected void handleStatement(boolean ignoredAnError)
-
parseIRI
protected IRI parseIRI()
-
parseNode
protected Resource parseNode()
-
parseLiteral
private void parseLiteral()
-
parseLabel
private java.lang.String parseLabel()
-
parseLiteralWithDatatype
private void parseLiteralWithDatatype(java.lang.String label)
-
parseLangLiteral
private void parseLangLiteral(java.lang.String label)
-
moveToIRIEndIndex
private void moveToIRIEndIndex() throws RDFParseException
Moves the current line index position to the end of the IRI.- Throws:
RDFParseException
-
moveToBNodeEndIndex
private void moveToBNodeEndIndex() throws RDFParseException
Moves the current line index position to the end of the BNode ID.- Throws:
RDFParseException
-
incrementIndexOrThrowEOF
private void incrementIndexOrThrowEOF()
Increments the current line index position and asserts EOF is not reached.
-
readLine
private boolean readLine() throws java.io.IOException
Attempts to read the next line from the buffered reader.- Throws:
java.io.IOException
-
createURI
protected IRI createURI(java.lang.String uri) throws RDFParseException
Description copied from class:AbstractRDFParser
Creates aIRI
object for the specified URI-string.- Overrides:
createURI
in classAbstractRDFParser
- Throws:
RDFParseException
-
reportWarning
protected void reportWarning(java.lang.String msg)
OverridesAbstractRDFParser.reportWarning(String)
, adding line number information to the error.- Overrides:
reportWarning
in classAbstractRDFParser
-
reportError
protected void reportError(java.lang.String msg, RioSetting<java.lang.Boolean> setting) throws RDFParseException
OverridesAbstractRDFParser.reportError(String, RioSetting)
, adding line number information to the error.- Overrides:
reportError
in 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
protected void reportError(java.lang.Exception e, RioSetting<java.lang.Boolean> setting) throws RDFParseException
Description copied from class:AbstractRDFParser
Reports 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
RDFParseException
when the given setting has been set to true and it is not a nonFatalError.- Overrides:
reportError
in 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
protected void reportFatalError(java.lang.String msg) throws RDFParseException
OverridesAbstractRDFParser.reportFatalError(String)
, adding line number information to the error.- Overrides:
reportFatalError
in classAbstractRDFParser
- Throws:
RDFParseException
-
reportFatalError
protected void reportFatalError(java.lang.Exception e) throws RDFParseException
OverridesAbstractRDFParser.reportFatalError(Exception)
, adding line number information to the error.- Overrides:
reportFatalError
in classAbstractRDFParser
- Throws:
RDFParseException
-
throwEOFException
protected void throwEOFException() throws RDFParseException
- Throws:
RDFParseException
-
clear
protected void clear()
Description copied from class:AbstractRDFParser
Clears any information that has been collected while parsing. This method must be called by subclasses when finishing the parse process.- Overrides:
clear
in classAbstractRDFParser
-
getSupportedSettings
public java.util.Collection<RioSetting<?>> getSupportedSettings()
- Specified by:
getSupportedSettings
in interfaceRDFParser
- Overrides:
getSupportedSettings
in classAbstractRDFParser
- Returns:
- A collection of
RioSetting
s that are supported by this RDFParser.
-
-