Package org.eclipse.rdf4j.rio.turtle
Class TurtleParser
- java.lang.Object
-
- org.eclipse.rdf4j.rio.helpers.AbstractRDFParser
-
- org.eclipse.rdf4j.rio.turtle.TurtleParser
-
- All Implemented Interfaces:
RDFParser
- Direct Known Subclasses:
N3Parser,TriGParser,TurtleStarParser
public class TurtleParser extends AbstractRDFParser
RDF parser for RDF-1.1 Turtle files. This parser is not thread-safe, therefore its public methods are synchronized.- Normalization of integer, floating point and boolean values is dependent on the specified datatype handling. According to the specification, integers and booleans should be normalized, but floats don't.
- Comments can be used anywhere in the document, and extend to the end of the line. The Turtle grammar doesn't allow comments to be used inside triple constructs that extend over multiple lines, but the author's own parser deviates from this too.
-
-
Field Summary
Fields Modifier and Type Field Description private intlineNumberprotected Valueobjectprivate java.lang.StringBuilderparsingBuilderprotected IRIpredicateprivate StatementpreviousStatementThe most recently read complete statement.private java.io.PushbackReaderreaderprotected Resourcesubject-
Fields inherited from class org.eclipse.rdf4j.rio.helpers.AbstractRDFParser
rdfHandler, valueFactory
-
-
Constructor Summary
Constructors Constructor Description TurtleParser()Creates a new TurtleParser that will use aSimpleValueFactoryto create RDF model objects.TurtleParser(ValueFactory valueFactory)Creates a new TurtleParser that will use the supplied ValueFactory to create RDF model objects.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static voidappendCodepoint(java.lang.StringBuilder dst, int codePoint)Appends the characters from codepoint into the string builder.private java.lang.StringBuildergetBuilder()protected intgetLineNumber()RDFFormatgetRDFFormat()Gets the RDF format that this parser can parse.java.util.Collection<RioSetting<?>>getSupportedSettings()voidparse(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.voidparse(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 voidparseAnnotation()protected voidparseBase()protected ResourceparseCollection()Parses a collection, e.g.protected voidparseDirective(java.lang.String directive)protected ResourceparseImplicitBlank()Parses an implicit blank node.protected java.lang.StringparseLongString(int closingCharacter)Parses a """long string""".protected ResourceparseNodeID()Parses a blank node ID, e.g.protected LiteralparseNumber()protected voidparseObject()Parse an objectprotected voidparseObjectList()protected IRIparsePredicate()protected voidparsePredicateObjectList()protected voidparsePrefixID()protected ValueparseQNameOrBoolean()Parses qnames and boolean values, which have equivalent starting characters.protected LiteralparseQuotedLiteral()Parses a quoted string, optionally followed by a language tag or datatype.protected java.lang.StringparseQuotedString()Parses a quoted string, which is either a "normal string" or a """long string""".protected voidparseStatement()protected java.lang.StringparseString(int closingCharacter)Parses a "normal string".protected voidparseSubject()protected voidparseTriples()protected TripleparseTripleValue()Parser an RDF-star triple value and returns it.protected IRIparseURI()protected ValueparseValue()Parses an RDF value.protected intpeekCodePoint()Peeks at the next Unicode code point without advancing the reader, and returns its value.protected booleanpeekIsTripleValue()Peeks at the next two Unicode code points without advancing the reader and returns true if they indicate the start of an RDF-star triple value.protected voidprocessComment()Consumes characters from reader until the first EOL has been read.protected intreadCodePoint()Reads the next Unicode code point.private charreadLocalEscapedChar()protected voidreportError(java.lang.String msg, RioSetting<java.lang.Boolean> setting)OverridesAbstractRDFParser.reportError(String, RioSetting), adding line number information to the error.protected voidreportFatalError(java.lang.Exception e)OverridesAbstractRDFParser.reportFatalError(Exception), adding line number information to the error.protected voidreportFatalError(java.lang.String msg)OverridesAbstractRDFParser.reportFatalError(String), adding line number information to the error.protected voidreportLocation()protected voidreportStatement(Resource subj, IRI pred, Value obj)protected voidreportWarning(java.lang.String msg)OverridesAbstractRDFParser.reportWarning(String), adding line number information to the error.protected intskipWSC()Consumes any white space characters (space, tab, line feed, newline) and comments (#-style) from reader.protected voidthrowEOFException()protected voidunread(int codePoint)Pushes back a single code point by copying it to the front of the buffer.protected voidunread(java.lang.String string)Pushes back the supplied string by copying it to the front of the buffer.protected voidverifyCharacterOrFail(int codePoint, java.lang.String expected)Verifies that the supplied character code point codePoint is one of the expected characters specified in expected.-
Methods inherited from class org.eclipse.rdf4j.rio.helpers.AbstractRDFParser
clear, clearBNodeIDMap, createBNode, createBNode, createLiteral, createLiteral, createNode, createNode, createStatement, createStatement, createURI, getNamespace, getParseErrorListener, getParseLocationListener, getParserConfig, getRDFHandler, initializeNamespaceTableFromConfiguration, preserveBNodeIDs, reportError, reportError, reportError, reportError, reportFatalError, reportFatalError, reportFatalError, reportLocation, reportWarning, resolveURI, set, setBaseURI, setBaseURI, setNamespace, setParseErrorListener, setParseLocationListener, setParserConfig, setPreserveBNodeIDs, setRDFHandler, setValueFactory
-
-
-
-
Field Detail
-
reader
private java.io.PushbackReader reader
-
subject
protected Resource subject
-
predicate
protected IRI predicate
-
object
protected Value object
-
lineNumber
private int lineNumber
-
parsingBuilder
private final java.lang.StringBuilder parsingBuilder
-
previousStatement
private Statement previousStatement
The most recently read complete statement.
-
-
Constructor Detail
-
TurtleParser
public TurtleParser()
Creates a new TurtleParser that will use aSimpleValueFactoryto create RDF model objects.
-
TurtleParser
public TurtleParser(ValueFactory valueFactory)
Creates a new TurtleParser 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:RDFParserGets the RDF format that this parser can parse.
-
getSupportedSettings
public java.util.Collection<RioSetting<?>> getSupportedSettings()
- Specified by:
getSupportedSettingsin interfaceRDFParser- Overrides:
getSupportedSettingsin classAbstractRDFParser- Returns:
- A collection of
RioSettings that are supported by this RDFParser.
-
parse
public void parse(java.io.InputStream in, java.lang.String baseURI) throws java.io.IOException, RDFParseException, RDFHandlerExceptionDescription copied from interface:RDFParserParses 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, RDFHandlerExceptionDescription copied from interface:RDFParserParses 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 java.io.IOException, RDFParseException, RDFHandlerException- Throws:
java.io.IOExceptionRDFParseExceptionRDFHandlerException
-
parseDirective
protected void parseDirective(java.lang.String directive) throws java.io.IOException, RDFParseException, RDFHandlerException- Throws:
java.io.IOExceptionRDFParseExceptionRDFHandlerException
-
parsePrefixID
protected void parsePrefixID() throws java.io.IOException, RDFParseException, RDFHandlerException- Throws:
java.io.IOExceptionRDFParseExceptionRDFHandlerException
-
parseBase
protected void parseBase() throws java.io.IOException, RDFParseException, RDFHandlerException- Throws:
java.io.IOExceptionRDFParseExceptionRDFHandlerException
-
parseTriples
protected void parseTriples() throws java.io.IOException, RDFParseException, RDFHandlerException- Throws:
java.io.IOExceptionRDFParseExceptionRDFHandlerException
-
parsePredicateObjectList
protected void parsePredicateObjectList() throws java.io.IOException, RDFParseException, RDFHandlerException- Throws:
java.io.IOExceptionRDFParseExceptionRDFHandlerException
-
parseObjectList
protected void parseObjectList() throws java.io.IOException, RDFParseException, RDFHandlerException- Throws:
java.io.IOExceptionRDFParseExceptionRDFHandlerException
-
parseSubject
protected void parseSubject() throws java.io.IOException, RDFParseException, RDFHandlerException- Throws:
java.io.IOExceptionRDFParseExceptionRDFHandlerException
-
parsePredicate
protected IRI parsePredicate() throws java.io.IOException, RDFParseException, RDFHandlerException
- Throws:
java.io.IOExceptionRDFParseExceptionRDFHandlerException
-
parseObject
protected void parseObject() throws java.io.IOException, RDFParseException, RDFHandlerExceptionParse an object- Throws:
java.io.IOExceptionRDFParseExceptionRDFHandlerException
-
parseCollection
protected Resource parseCollection() throws java.io.IOException, RDFParseException, RDFHandlerException
Parses a collection, e.g. ( item1 item2 item3 ).- Throws:
java.io.IOExceptionRDFParseExceptionRDFHandlerException
-
parseImplicitBlank
protected Resource parseImplicitBlank() throws java.io.IOException, RDFParseException, RDFHandlerException
Parses an implicit blank node. This method parses the token [] and predicateObjectLists that are surrounded by square brackets.- Throws:
java.io.IOExceptionRDFParseExceptionRDFHandlerException
-
parseValue
protected Value parseValue() throws java.io.IOException, RDFParseException, RDFHandlerException
Parses an RDF value. This method parses uriref, qname, node ID, quoted literal, integer, double and boolean.- Throws:
java.io.IOExceptionRDFParseExceptionRDFHandlerException
-
parseQuotedLiteral
protected Literal parseQuotedLiteral() throws java.io.IOException, RDFParseException, RDFHandlerException
Parses a quoted string, optionally followed by a language tag or datatype.- Throws:
java.io.IOExceptionRDFParseExceptionRDFHandlerException
-
parseQuotedString
protected java.lang.String parseQuotedString() throws java.io.IOException, RDFParseExceptionParses a quoted string, which is either a "normal string" or a """long string""".- Returns:
- string
- Throws:
java.io.IOExceptionRDFParseException
-
parseString
protected java.lang.String parseString(int closingCharacter) throws java.io.IOException, RDFParseExceptionParses a "normal string". This method requires that the opening character has already been parsed.- Returns:
- parsed string
- Throws:
java.io.IOExceptionRDFParseException
-
parseLongString
protected java.lang.String parseLongString(int closingCharacter) throws java.io.IOException, RDFParseExceptionParses a """long string""". This method requires that the first three characters have already been parsed.- Throws:
java.io.IOExceptionRDFParseException
-
parseNumber
protected Literal parseNumber() throws java.io.IOException, RDFParseException
- Throws:
java.io.IOExceptionRDFParseException
-
parseURI
protected IRI parseURI() throws java.io.IOException, RDFParseException
- Throws:
java.io.IOExceptionRDFParseException
-
parseQNameOrBoolean
protected Value parseQNameOrBoolean() throws java.io.IOException, RDFParseException
Parses qnames and boolean values, which have equivalent starting characters.- Throws:
java.io.IOExceptionRDFParseException
-
readLocalEscapedChar
private char readLocalEscapedChar() throws RDFParseException, java.io.IOException- Throws:
RDFParseExceptionjava.io.IOException
-
parseNodeID
protected Resource parseNodeID() throws java.io.IOException, RDFParseException
Parses a blank node ID, e.g. _:node1.- Throws:
java.io.IOExceptionRDFParseException
-
reportStatement
protected void reportStatement(Resource subj, IRI pred, Value obj) throws RDFParseException, RDFHandlerException
- Throws:
RDFParseExceptionRDFHandlerException
-
verifyCharacterOrFail
protected void verifyCharacterOrFail(int codePoint, java.lang.String expected) throws RDFParseExceptionVerifies that the supplied character code point codePoint is one of the expected characters specified in expected. This method will throw a ParseException if this is not the case.- Throws:
RDFParseException
-
skipWSC
protected int skipWSC() throws java.io.IOException, RDFHandlerExceptionConsumes any white space characters (space, tab, line feed, newline) and comments (#-style) from reader. After this method has been called, the first character that is returned by reader is either a non-ignorable character, or EOF. For convenience, this character is also returned by this method.- Returns:
- The next character code point that will be returned by reader.
- Throws:
java.io.IOExceptionRDFHandlerException
-
processComment
protected void processComment() throws java.io.IOException, RDFHandlerExceptionConsumes characters from reader until the first EOL has been read. This line of text is then passed to theAbstractRDFParser.rdfHandleras a comment.- Throws:
java.io.IOExceptionRDFHandlerException
-
readCodePoint
protected int readCodePoint() throws java.io.IOExceptionReads the next Unicode code point.- Returns:
- the next Unicode code point, or -1 if the end of the stream has been reached.
- Throws:
java.io.IOException
-
unread
protected void unread(int codePoint) throws java.io.IOExceptionPushes back a single code point by copying it to the front of the buffer. After this method returns, a call toreadCodePoint()will return the same code point c again.- Parameters:
codePoint- a single Unicode code point.- Throws:
java.io.IOException
-
unread
protected void unread(java.lang.String string) throws java.io.IOExceptionPushes back the supplied string by copying it to the front of the buffer. After this method returns, successive calls toreadCodePoint()will return the code points in the supplied string again, starting at the first in the String..- Parameters:
string- the string to un-read.- Throws:
java.io.IOException
-
peekCodePoint
protected int peekCodePoint() throws java.io.IOExceptionPeeks at the next Unicode code point without advancing the reader, and returns its value.- Returns:
- the next Unicode code point, or -1 if the end of the stream has been reached.
- Throws:
java.io.IOException
-
reportLocation
protected void reportLocation()
-
reportWarning
protected void reportWarning(java.lang.String msg)
OverridesAbstractRDFParser.reportWarning(String), adding line number information to the error.- Overrides:
reportWarningin classAbstractRDFParser
-
reportError
protected void reportError(java.lang.String msg, RioSetting<java.lang.Boolean> setting) throws RDFParseExceptionOverridesAbstractRDFParser.reportError(String, RioSetting), adding line 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.
-
reportFatalError
protected void reportFatalError(java.lang.String msg) throws RDFParseExceptionOverridesAbstractRDFParser.reportFatalError(String), adding line number information to the error.- Overrides:
reportFatalErrorin classAbstractRDFParser- Throws:
RDFParseException
-
reportFatalError
protected void reportFatalError(java.lang.Exception e) throws RDFParseExceptionOverridesAbstractRDFParser.reportFatalError(Exception), adding line number information to the error.- Overrides:
reportFatalErrorin classAbstractRDFParser- Throws:
RDFParseException
-
throwEOFException
protected void throwEOFException() throws RDFParseException- Throws:
RDFParseException
-
getLineNumber
protected int getLineNumber()
-
getBuilder
private java.lang.StringBuilder getBuilder()
-
appendCodepoint
private static void appendCodepoint(java.lang.StringBuilder dst, int codePoint)Appends the characters from codepoint into the string builder. This is the same as Character#toChars but prevents the additional char array garbage for BMP codepoints.- Parameters:
dst- the destination in which to append the characterscodePoint- the codepoint to be appended
-
peekIsTripleValue
protected boolean peekIsTripleValue() throws java.io.IOExceptionPeeks at the next two Unicode code points without advancing the reader and returns true if they indicate the start of an RDF-star triple value. Such values start with '<<'.- Returns:
- true if the next code points indicate the beginning of an RDF-star triple value, false otherwise
- Throws:
java.io.IOException
-
parseTripleValue
protected Triple parseTripleValue() throws java.io.IOException
Parser an RDF-star triple value and returns it.- Returns:
- An RDF-star triple.
- Throws:
java.io.IOException
-
parseAnnotation
protected void parseAnnotation() throws java.io.IOException- Throws:
java.io.IOException
-
-