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.

Author:
  Arjohn Kampman, Peter Ansell

- - ### Nested Class Summary

    - ### Nested classes/interfaces inherited from interface org.eclipse.rdf4j.rio.RDFParser

      `RDFParser.DatatypeHandling`

  - ### Field Summary

    Fields
    Modifier and Type | Field | Description
    --- | --- | ---
    `protected Value` | `object` |
    `protected IRI` | `predicate` |
    `protected Resource` | `subject` |

    - ### 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 a `SimpleValueFactory` to 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 Instance Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `protected int` | `getLineNumber()` |
    `RDFFormat` | `getRDFFormat()` | Gets the RDF format that this parser can parse.
    `Collection<RioSetting<?>>` | `getSupportedSettings()` |
    `void` | `parse​(InputStream in, String baseURI)` | Parses the data from the supplied InputStream, using the supplied baseURI to resolve any relative URI references.
    `void` | `parse​(Reader reader, String baseURI)` | Parses the data from the supplied Reader, using the supplied baseURI to resolve any relative URI references.
    `protected void` | `parseAnnotation()` |
    `protected void` | `parseBase()` |
    `protected Resource` | `parseCollection()` | Parses a collection, e.g.
    `protected void` | `parseDirective​(String directive)` |
    `protected Resource` | `parseImplicitBlank()` | Parses an implicit blank node.
    `protected String` | `parseLongString​(int closingCharacter)` | Parses a """long string""".
    `protected Resource` | `parseNodeID()` | Parses a blank node ID, e.g.
    `protected Literal` | `parseNumber()` |
    `protected void` | `parseObject()` | Parse an object
    `protected void` | `parseObjectList()` |
    `protected IRI` | `parsePredicate()` |
    `protected void` | `parsePredicateObjectList()` |
    `protected void` | `parsePrefixID()` |
    `protected Value` | `parseQNameOrBoolean()` | Parses qnames and boolean values, which have equivalent starting characters.
    `protected Literal` | `parseQuotedLiteral()` | Parses a quoted string, optionally followed by a language tag or datatype.
    `protected String` | `parseQuotedString()` | Parses a quoted string, which is either a "normal string" or a """long string""".
    `protected void` | `parseStatement()` |
    `protected String` | `parseString​(int closingCharacter)` | Parses a "normal string".
    `protected void` | `parseSubject()` |
    `protected void` | `parseTriples()` |
    `protected Triple` | `parseTripleValue()` | Parser an RDF-star triple value and returns it.
    `protected IRI` | `parseURI()` |
    `protected Value` | `parseValue()` | Parses an RDF value.
    `protected int` | `peekCodePoint()` | Peeks at the next Unicode code point without advancing the reader, and returns its value.
    `protected boolean` | `peekIsTripleValue()` | 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 void` | `processComment()` | Consumes characters from reader until the first EOL has been read.
    `protected int` | `readCodePoint()` | Reads the next Unicode code point.
    `protected void` | `reportError​(String msg, RioSetting<Boolean> setting)` | Overrides `AbstractRDFParser.reportError(String, RioSetting)`, adding line number information to the error.
    `protected void` | `reportFatalError​(Exception e)` | Overrides `AbstractRDFParser.reportFatalError(Exception)`, adding line number information to the error.
    `protected void` | `reportFatalError​(String msg)` | Overrides `AbstractRDFParser.reportFatalError(String)`, adding line number information to the error.
    `protected void` | `reportLocation()` |
    `protected void` | `reportStatement​(Resource subj, IRI pred, Value obj)` |
    `protected void` | `reportWarning​(String msg)` | Overrides `AbstractRDFParser.reportWarning(String)`, adding line number information to the error.
    `protected int` | `skipWSC()` | Consumes any white space characters (space, tab, line feed, newline) and comments (#-style) from reader.
    `protected void` | `throwEOFException()` |
    `protected void` | `unread​(int codePoint)` | Pushes back a single code point by copying it to the front of the buffer.
    `protected void` | `unread​(String string)` | Pushes back the supplied string by copying it to the front of the buffer.
    `protected void` | `verifyCharacterOrFail​(int codePoint, 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, datatypeHandling, getNamespace, getParseErrorListener, getParseLocationListener, getParserConfig, getRDFHandler, initializeNamespaceTableFromConfiguration, preserveBNodeIDs, reportError, reportError, reportError, reportError, reportFatalError, reportFatalError, reportFatalError, reportLocation, reportWarning, resolveURI, set, setBaseURI, setBaseURI, setDatatypeHandling, setNamespace, setParseErrorListener, setParseLocationListener, setParserConfig, setPreserveBNodeIDs, setRDFHandler, setStopAtFirstError, setValueFactory, setVerifyData, stopAtFirstError, verifyData`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface org.eclipse.rdf4j.rio.RDFParser

      `parse, parse`

- - ### Field Detail

    - #### subject

          protected Resource subject

    - #### predicate

          protected IRI predicate

    - #### object

          protected Value object

  - ### Constructor Detail

    - #### TurtleParser

          public TurtleParser()

      Creates a new TurtleParser that will use a `SimpleValueFactory` to 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: `RDFParser`
      Gets the RDF format that this parser can parse.

    - #### getSupportedSettings

          public Collection<RioSetting<?>> getSupportedSettings()

      Specified by:
        `getSupportedSettings` in interface `RDFParser`
      Overrides:
        `getSupportedSettings` in class `AbstractRDFParser`
      Returns:
        A collection of `RioSetting`s that are supported by this RDFParser.

    - #### parse

          public void parse​(InputStream in,
                            String baseURI)
                     throws 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 be `null`. 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: `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 be `null`. 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.

    - #### parseStatement

          protected void parseStatement()
                                 throws IOException,
                                        RDFParseException,
                                        RDFHandlerException

      Throws:
        `IOException`
        `RDFParseException`
        `RDFHandlerException`

    - #### parseDirective

          protected void parseDirective​(String directive)
                                 throws IOException,
                                        RDFParseException,
                                        RDFHandlerException

      Throws:
        `IOException`
        `RDFParseException`
        `RDFHandlerException`

    - #### parsePrefixID

          protected void parsePrefixID()
                                throws IOException,
                                       RDFParseException,
                                       RDFHandlerException

      Throws:
        `IOException`
        `RDFParseException`
        `RDFHandlerException`

    - #### parseBase

          protected void parseBase()
                            throws IOException,
                                   RDFParseException,
                                   RDFHandlerException

      Throws:
        `IOException`
        `RDFParseException`
        `RDFHandlerException`

    - #### parseTriples

          protected void parseTriples()
                               throws IOException,
                                      RDFParseException,
                                      RDFHandlerException

      Throws:
        `IOException`
        `RDFParseException`
        `RDFHandlerException`

    - #### parsePredicateObjectList

          protected void parsePredicateObjectList()
                                           throws IOException,
                                                  RDFParseException,
                                                  RDFHandlerException

      Throws:
        `IOException`
        `RDFParseException`
        `RDFHandlerException`

    - #### parseObjectList

          protected void parseObjectList()
                                  throws IOException,
                                         RDFParseException,
                                         RDFHandlerException

      Throws:
        `IOException`
        `RDFParseException`
        `RDFHandlerException`

    - #### parseSubject

          protected void parseSubject()
                               throws IOException,
                                      RDFParseException,
                                      RDFHandlerException

      Throws:
        `IOException`
        `RDFParseException`
        `RDFHandlerException`

    - #### parsePredicate

          protected IRI parsePredicate()
                                throws IOException,
                                       RDFParseException,
                                       RDFHandlerException

      Throws:
        `IOException`
        `RDFParseException`
        `RDFHandlerException`

    - #### parseObject

          protected void parseObject()
                              throws IOException,
                                     RDFParseException,
                                     RDFHandlerException

      Parse an object
      Throws:
        `IOException`
        `RDFParseException`
        `RDFHandlerException`

    - #### parseCollection

          protected Resource parseCollection()
                                      throws IOException,
                                             RDFParseException,
                                             RDFHandlerException

      Parses a collection, e.g. ( item1 item2 item3 ).
      Throws:
        `IOException`
        `RDFParseException`
        `RDFHandlerException`

    - #### parseImplicitBlank

          protected Resource parseImplicitBlank()
                                         throws IOException,
                                                RDFParseException,
                                                RDFHandlerException

      Parses an implicit blank node. This method parses the token [] and predicateObjectLists that are surrounded by square brackets.
      Throws:
        `IOException`
        `RDFParseException`
        `RDFHandlerException`

    - #### parseValue

          protected Value parseValue()
                              throws IOException,
                                     RDFParseException,
                                     RDFHandlerException

      Parses an RDF value. This method parses uriref, qname, node ID, quoted literal, integer, double and boolean.
      Throws:
        `IOException`
        `RDFParseException`
        `RDFHandlerException`

    - #### parseQuotedLiteral

          protected Literal parseQuotedLiteral()
                                        throws IOException,
                                               RDFParseException,
                                               RDFHandlerException

      Parses a quoted string, optionally followed by a language tag or datatype.
      Throws:
        `IOException`
        `RDFParseException`
        `RDFHandlerException`

    - #### parseQuotedString

          protected String parseQuotedString()
                                      throws IOException,
                                             RDFParseException

      Parses a quoted string, which is either a "normal string" or a """long string""".
      Returns:
        string
      Throws:
        `IOException`
        `RDFParseException`

    - #### parseString

          protected String parseString​(int closingCharacter)
                                throws IOException,
                                       RDFParseException

      Parses a "normal string". This method requires that the opening character has already been parsed.
      Returns:
        parsed string
      Throws:
        `IOException`
        `RDFParseException`

    - #### parseLongString

          protected String parseLongString​(int closingCharacter)
                                    throws IOException,
                                           RDFParseException

      Parses a """long string""". This method requires that the first three characters have already been parsed.
      Throws:
        `IOException`
        `RDFParseException`

    - #### parseNumber

          protected Literal parseNumber()
                                 throws IOException,
                                        RDFParseException

      Throws:
        `IOException`
        `RDFParseException`

    - #### parseURI

          protected IRI parseURI()
                          throws IOException,
                                 RDFParseException

      Throws:
        `IOException`
        `RDFParseException`

    - #### parseQNameOrBoolean

          protected Value parseQNameOrBoolean()
                                       throws IOException,
                                              RDFParseException

      Parses qnames and boolean values, which have equivalent starting characters.
      Throws:
        `IOException`
        `RDFParseException`

    - #### parseNodeID

          protected Resource parseNodeID()
                                  throws IOException,
                                         RDFParseException

      Parses a blank node ID, e.g. _:node1.
      Throws:
        `IOException`
        `RDFParseException`

    - #### reportStatement

          protected void reportStatement​(Resource subj,
                                         IRI pred,
                                         Value obj)
                                  throws RDFParseException,
                                         RDFHandlerException

      Throws:
        `RDFParseException`
        `RDFHandlerException`

    - #### verifyCharacterOrFail

          protected void verifyCharacterOrFail​(int codePoint,
                                               String expected)
                                        throws RDFParseException

      Verifies 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 IOException,
                                RDFHandlerException

      Consumes 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:
        `IOException`
        `RDFHandlerException`

    - #### processComment

          protected void processComment()
                                 throws IOException,
                                        RDFHandlerException

      Consumes characters from reader until the first EOL has been read. This line of text is then passed to the `AbstractRDFParser.rdfHandler` as a comment.
      Throws:
        `IOException`
        `RDFHandlerException`

    - #### readCodePoint

          protected int readCodePoint()
                               throws IOException

      Reads the next Unicode code point.
      Returns:
        the next Unicode code point, or -1 if the end of the stream has been reached.
      Throws:
        `IOException`

    - #### unread

          protected void unread​(int codePoint)
                         throws IOException

      Pushes back a single code point by copying it to the front of the buffer. After this method returns, a call to `readCodePoint()` will return the same code point c again.
      Parameters:
        `codePoint` - a single Unicode code point.
      Throws:
        `IOException`

    - #### unread

          protected void unread​(String string)
                         throws IOException

      Pushes back the supplied string by copying it to the front of the buffer. After this method returns, successive calls to `readCodePoint()` 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:
        `IOException`

    - #### peekCodePoint

          protected int peekCodePoint()
                               throws IOException

      Peeks 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:
        `IOException`

    - #### reportLocation

          protected void reportLocation()

    - #### reportWarning

          protected void reportWarning​(String msg)

      Overrides `AbstractRDFParser.reportWarning(String)`, adding line number information to the error.
      Overrides:
        `reportWarning` in class `AbstractRDFParser`

    - #### reportError

          protected void reportError​(String msg,
                                     RioSetting<Boolean> setting)
                              throws RDFParseException

      Overrides `AbstractRDFParser.reportError(String, RioSetting)`, adding line number information to the error.
      Overrides:
        `reportError` in class `AbstractRDFParser`
      Parameters:
        `msg` - The message to use for `ParseErrorListener.error(String, long, long)` and for `RDFParseException(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 if `ParserConfig.isNonFatalError(RioSetting)` returns true an exception will be thrown.
      Throws:
        `RDFParseException` - If `RioConfig.get(RioSetting)` returns true, and `ParserConfig.isNonFatalError(RioSetting)` returns true for the given setting.

    - #### reportFatalError

          protected void reportFatalError​(String msg)
                                   throws RDFParseException

      Overrides `AbstractRDFParser.reportFatalError(String)`, adding line number information to the error.
      Overrides:
        `reportFatalError` in class `AbstractRDFParser`
      Throws:
        `RDFParseException`

    - #### reportFatalError

          protected void reportFatalError​(Exception e)
                                   throws RDFParseException

      Overrides `AbstractRDFParser.reportFatalError(Exception)`, adding line number information to the error.
      Overrides:
        `reportFatalError` in class `AbstractRDFParser`
      Throws:
        `RDFParseException`

    - #### throwEOFException

          protected void throwEOFException()
                                    throws RDFParseException

      Throws:
        `RDFParseException`

    - #### getLineNumber

          protected int getLineNumber()

    - #### peekIsTripleValue

          protected boolean peekIsTripleValue()
                                       throws IOException

      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. Such values start with '<<'.
      Returns:
        true if the next code points indicate the beginning of an RDF-star triple value, false otherwise
      Throws:
        `IOException`

    - #### parseTripleValue

          protected Triple parseTripleValue()
                                     throws IOException

      Parser an RDF-star triple value and returns it.
      Returns:
        An RDF-star triple.
      Throws:
        `IOException`

    - #### parseAnnotation

          protected void parseAnnotation()
                                  throws IOException

      Throws:
        `IOException`