TurtleParser extends AbstractRDFParser { @Override public synchronized void parse(InputStream in, String baseURI) throws IOException, RDFParseException, RDFHandlerException { if (in == null) { throw new IllegalArgumentException("Input stream must not be 'null'"); } try { parse(new InputStreamReader(new BOMInputStream(in, false), StandardCharsets.UTF_8), baseURI); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } TurtleParser();  TurtleParser(ValueFactory valueFactory); @Override RDFFormat getRDFFormat(); @Override Collection<RioSetting<?>> getSupportedSettings(); @Override synchronized void parse(InputStream in, String baseURI); @Override synchronized void parse(Reader reader, String baseURI);  }
@Test public void testParseDots() throws IOException { String data = prefixes + " ex:foo.bar ex:\\~foo.bar ex:foobar. "; parser.parse(new StringReader(data), baseURI); assertTrue(errorCollector.getWarnings().isEmpty()); assertTrue(errorCollector.getErrors().isEmpty()); assertTrue(errorCollector.getFatalErrors().isEmpty()); assertFalse(statementCollector.getStatements().isEmpty()); assertEquals(1, statementCollector.getStatements().size()); }
@Test public void testParseIllegalURIFatal() throws IOException { String data = " <urn:foo_bar\\r> <urn:foo> <urn:bar> ; <urn:foo2> <urn:bar2> . <urn:foobar> <urn:food> <urn:barf> . "; try { parser.parse(new StringReader(data), baseURI); fail("default config should result in fatal error / parse exception"); } catch (RDFParseException e) { } }
@Test public void testParseIllegalURINonFatal() throws IOException { String data = " <urn:foo_bar\\r> <urn:foo> <urn:bar> ; <urn:foo2> <urn:bar2> . <urn:foobar> <urn:food> <urn:barf> . "; parser.getParserConfig().addNonFatalError(BasicParserSettings.VERIFY_URI_SYNTAX); parser.parse(new StringReader(data), baseURI); assertThat(errorCollector.getErrors()).hasSize(1); assertThat(errorCollector.getFatalErrors()).isEmpty(); assertThat(statementCollector.getStatements()).isNotEmpty(); assertThat(statementCollector.getStatements()).hasSize(1) .overridingErrorMessage("only syntactically legal triples should have been reported"); }
@Test public void testParseIllegalURINoVerify() throws IOException { String data = " <urn:foo_bar\\r> <urn:foo> <urn:bar> ; <urn:foo2> <urn:bar2> . <urn:foobar> <urn:food> <urn:barf> . "; parser.getParserConfig().set(BasicParserSettings.VERIFY_URI_SYNTAX, false); parser.parse(new StringReader(data), baseURI); assertThat(errorCollector.getErrors()).isEmpty(); assertThat(errorCollector.getFatalErrors()).isEmpty(); assertThat(statementCollector.getStatements()).isNotEmpty(); assertThat(statementCollector.getStatements()).hasSize(3) .overridingErrorMessage("all triples should have been reported"); }
@Test public void testParseIllegalDatatypeURIFatal() throws IOException { String data = " <urn:foo_bar> <urn:foo> \"a\"^^<urn:foo bar> ; <urn:foo2> <urn:bar2> . <urn:foobar> <urn:food> <urn:barf> . "; try { parser.parse(new StringReader(data), baseURI); fail("default config should result in fatal error / parse exception"); } catch (RDFParseException e) { } }
@Test public void testParseIllegalDatatypeValueFatalIRI() throws IOException { String data = " <urn:foo_bar> <urn:foo> \"a\"^^\"b\" ; <urn:foo2> <urn:bar2> . <urn:foobar> <urn:food> <urn:barf> . "; try { parser.parse(new StringReader(data), baseURI); fail("default config should result in fatal error / parse exception"); } catch (RDFParseException e) { } }
@Test public void testParseIllegalDatatypeURINonFatal() throws IOException { String data = " <urn:foo_bar> <urn:foo> \"a\"^^<urn:foo bar> ; <urn:foo2> <urn:bar2> . <urn:foobar> <urn:food> <urn:barf> . "; parser.getParserConfig().addNonFatalError(BasicParserSettings.VERIFY_URI_SYNTAX); parser.parse(new StringReader(data), baseURI); assertThat(errorCollector.getErrors()).hasSize(2); assertThat(errorCollector.getFatalErrors()).isEmpty(); assertThat(statementCollector.getStatements()).isNotEmpty(); assertThat(statementCollector.getStatements()).hasSize(2) .overridingErrorMessage("only syntactically legal triples should have been reported"); }
@Test public void testParseIllegalDatatypValueINonFatalIRI() throws IOException { String data = " <urn:foo_bar> <urn:foo> \"a\"^^\"b\" ; <urn:foo2> <urn:bar2> . <urn:foobar> <urn:food> <urn:barf> . "; try { parser.getParserConfig().addNonFatalError(BasicParserSettings.VERIFY_URI_SYNTAX); parser.parse(new StringReader(data), baseURI); fail("literal as datatype should result in fatal error / parse exception"); } catch (RDFParseException e) { } }
@Test public void testParseIllegalDatatypeURINoVerify() throws IOException { String data = " <urn:foo_bar> <urn:foo> \"a\"^^<urn:foo bar> ; <urn:foo2> <urn:bar2> . <urn:foobar> <urn:food> <urn:barf> . "; parser.getParserConfig().set(BasicParserSettings.VERIFY_URI_SYNTAX, false); parser.parse(new StringReader(data), baseURI); assertThat(errorCollector.getErrors()).isEmpty(); assertThat(errorCollector.getFatalErrors()).isEmpty(); assertThat(statementCollector.getStatements()).isNotEmpty(); assertThat(statementCollector.getStatements()).hasSize(3) .overridingErrorMessage("all triples should have been reported"); }
@Test public void testParseIllegalDatatypValueINoVerify() throws IOException { String data = " <urn:foo_bar> <urn:foo> \"a\"^^\"b\" ; <urn:foo2> <urn:bar2> . <urn:foobar> <urn:food> <urn:barf> . "; try { parser.getParserConfig().set(BasicParserSettings.VERIFY_URI_SYNTAX, false); parser.parse(new StringReader(data), baseURI); fail("literal as datatype should result in fatal error / parse exception"); } catch (RDFParseException e) { } }
@Test public void testUnparsableIRIFatal() throws IOException { String data = " <http: try { parser.parse(new StringReader(data), baseURI); fail("default config should result in fatal error / parse exception"); } catch (RDFParseException e) { } }
@Test public void testUnparsableIRINonFatal() throws IOException { String data = " <http: parser.getParserConfig().addNonFatalError(BasicParserSettings.VERIFY_URI_SYNTAX); parser.parse(new StringReader(data), baseURI); assertThat(errorCollector.getErrors()).hasSize(1); assertThat(errorCollector.getFatalErrors()).isEmpty(); assertThat(statementCollector.getStatements()).isNotEmpty(); assertThat(statementCollector.getStatements()).hasSize(1) .overridingErrorMessage("only syntactically legal triples should have been reported"); }
@Test public void testUnparsableIRINoVerify() throws IOException { String data = " <http: parser.getParserConfig().set(BasicParserSettings.VERIFY_URI_SYNTAX, false); parser.parse(new StringReader(data), baseURI); assertThat(errorCollector.getErrors()).isEmpty(); assertThat(errorCollector.getFatalErrors()).isEmpty(); assertThat(statementCollector.getStatements()).isNotEmpty(); assertThat(statementCollector.getStatements()).hasSize(2) .overridingErrorMessage("all triples should have been reported"); }
@Test public void testParseBNodes() throws IOException { String data = prefixes + " [ :p :o1,:2 ] . "; parser.parse(new StringReader(data), baseURI); assertTrue(errorCollector.getWarnings().isEmpty()); assertTrue(errorCollector.getErrors().isEmpty()); assertTrue(errorCollector.getFatalErrors().isEmpty()); assertFalse(statementCollector.getStatements().isEmpty()); assertEquals(2, statementCollector.getStatements().size()); }
@Test public void testLineNumberReporting() throws IOException { InputStream in = this.getClass().getResourceAsStream("/test-newlines.ttl"); try { parser.parse(in, baseURI); fail("expected to fail parsing input file"); } catch (RDFParseException e) { assertFalse(errorCollector.getFatalErrors().isEmpty()); final String error = errorCollector.getFatalErrors().get(0); assertTrue(error.contains("(9,")); assertEquals(9, locationListener.getLineNo()); assertEquals(-1, locationListener.getColumnNo()); } }
@Test public void testLineNumberReportingNoErrorsSingleLine() throws IOException { assertEquals(0, locationListener.getLineNo()); assertEquals(0, locationListener.getColumnNo()); Reader in = new StringReader("<urn:a> <urn:b> <urn:c>."); parser.parse(in, baseURI); assertEquals(1, locationListener.getLineNo()); assertEquals(-1, locationListener.getColumnNo()); }
@Test public void testLineNumberReportingNoErrorsSingleLineEndNewline() throws IOException { assertEquals(0, locationListener.getLineNo()); assertEquals(0, locationListener.getColumnNo()); Reader in = new StringReader("<urn:a> <urn:b> <urn:c>.\n"); parser.parse(in, baseURI); assertEquals(2, locationListener.getLineNo()); assertEquals(-1, locationListener.getColumnNo()); }
@Test public void testLineNumberReportingNoErrorsMultipleLinesNoEndNewline() throws IOException { assertEquals(0, locationListener.getLineNo()); assertEquals(0, locationListener.getColumnNo()); Reader in = new StringReader("<urn:a> <urn:b> <urn:c>.\n<urn:a> <urn:b> <urn:d>."); parser.parse(in, baseURI); assertEquals(2, locationListener.getLineNo()); assertEquals(-1, locationListener.getColumnNo()); }
@Test public void testLineNumberReportingNoErrorsMultipleLinesEndNewline() throws IOException { assertEquals(0, locationListener.getLineNo()); assertEquals(0, locationListener.getColumnNo()); Reader in = new StringReader("<urn:a> <urn:b> <urn:c>.\n<urn:a> <urn:b> <urn:d>.\n"); parser.parse(in, baseURI); assertEquals(3, locationListener.getLineNo()); assertEquals(-1, locationListener.getColumnNo()); }
@Test public void testLineNumberReportingOnlySingleCommentNoEndline() throws IOException { assertEquals(0, locationListener.getLineNo()); assertEquals(0, locationListener.getColumnNo()); Reader in = new StringReader("# This is just a comment"); parser.parse(in, baseURI); assertEquals(1, locationListener.getLineNo()); assertEquals(-1, locationListener.getColumnNo()); }
@Test public void testLineNumberReportingOnlySingleCommentEndline() throws IOException { assertEquals(0, locationListener.getLineNo()); assertEquals(0, locationListener.getColumnNo()); Reader in = new StringReader("# This is just a comment\n"); parser.parse(in, baseURI); assertEquals(2, locationListener.getLineNo()); assertEquals(-1, locationListener.getColumnNo()); }
@Test public void testLineNumberReportingOnlySingleCommentCarriageReturn() throws IOException { assertEquals(0, locationListener.getLineNo()); assertEquals(0, locationListener.getColumnNo()); Reader in = new StringReader("# This is just a comment\r"); parser.parse(in, baseURI); assertEquals(2, locationListener.getLineNo()); assertEquals(-1, locationListener.getColumnNo()); }
@Test public void testLineNumberReportingOnlySingleCommentCarriageReturnNewline() throws IOException { assertEquals(0, locationListener.getLineNo()); assertEquals(0, locationListener.getColumnNo()); Reader in = new StringReader("# This is just a comment\r\n"); parser.parse(in, baseURI); assertEquals(2, locationListener.getLineNo()); assertEquals(-1, locationListener.getColumnNo()); }
@Test public void testParseBooleanLiteralComma() throws IOException { String data = "<urn:a> <urn:b> true, false ."; Reader r = new StringReader(data); try { parser.parse(r, baseURI); assertTrue(statementCollector.getStatements().size() == 2); } catch (RDFParseException e) { fail("parse error on correct data: " + e.getMessage()); } }
@Test public void testParseBooleanLiteralWhitespaceComma() throws IOException { String data = "<urn:a> <urn:b> true , false ."; Reader r = new StringReader(data); try { parser.parse(r, baseURI); assertTrue(statementCollector.getStatements().size() == 2); } catch (RDFParseException e) { fail("parse error on correct data: " + e.getMessage()); } }
@Test public void testParseBooleanLiteralSemicolumn() throws IOException { String data = "<urn:a> <urn:b> true; <urn:c> false ."; Reader r = new StringReader(data); try { parser.parse(r, baseURI); assertTrue(statementCollector.getStatements().size() == 2); } catch (RDFParseException e) { fail("parse error on correct data: " + e.getMessage()); } }
@Test public void testParseBooleanLiteralWhitespaceSemicolumn() throws IOException { String data = "<urn:a> <urn:b> true ; <urn:c> false ."; Reader r = new StringReader(data); try { parser.parse(r, baseURI); assertTrue(statementCollector.getStatements().size() == 2); } catch (RDFParseException e) { fail("parse error on correct data: " + e.getMessage()); } }
@Test public void rdfXmlLoadedFromInsideAJarResolvesRelativeUris() throws IOException { URL zipfileUrl = TurtleParserTest.class.getResource("sample-with-turtle-data.zip"); assertNotNull("The sample-with-turtle-data.zip file must be present for this test", zipfileUrl); String url = "jar:" + zipfileUrl + "!/index.ttl"; RDFParser parser = new TurtleParser(); StatementCollector sc = new StatementCollector(); parser.setRDFHandler(sc); try (InputStream in = new URL(url).openStream()) { parser.parse(in, url); } Collection<Statement> stmts = sc.getStatements(); assertThat(stmts).hasSize(2); Iterator<Statement> iter = stmts.iterator(); Statement stmt1 = iter.next(), stmt2 = iter.next(); assertEquals(vf.createIRI("http: assertEquals(vf.createIRI("http: Resource res = (Resource) stmt1.getObject(); String resourceUrl = res.stringValue(); assertThat(resourceUrl).startsWith("jar:" + zipfileUrl + "!"); URL javaUrl = new URL(resourceUrl); assertEquals("jar", javaUrl.getProtocol()); try (InputStream uc = javaUrl.openStream()) { assertEquals("The resource stream should be empty", -1, uc.read()); } assertEquals(res, stmt2.getSubject()); assertEquals(DC.TITLE, stmt2.getPredicate()); assertEquals(vf.createLiteral("Empty File"), stmt2.getObject()); }
@Test public void testIllegalNewlineInQuotedObjectLiteral() throws IOException { String data = "<urn:a> <urn:b> \"not\nallowed\" ."; Reader r = new StringReader(data); try { parser.parse(r, baseURI); fail("Did not catch illegal new line"); } catch (RDFParseException e) { assertThat(e.getMessage().startsWith("Illegal carriage return or new line in literal")); } }
@Test public void testLegalNewlineInTripleQuotedObjectLiteral() throws IOException { String data = "<urn:a> <urn:b> \"\"\"is\nallowed\"\"\" ."; Reader r = new StringReader(data); try { parser.parse(r, baseURI); assertTrue(statementCollector.getStatements().size() == 1); } catch (RDFParseException e) { fail("New line is legal inside triple quoted literal"); } }
@Test public void testLegalUnicodeInTripleSubject() throws IOException { String data = "@prefix xsd: <http: Reader r = new StringReader(data); try { parser.parse(r, baseURI); assertTrue(statementCollector.getStatements().size() == 1); } catch (RDFParseException e) { fail("Complex unicode characters should be parsed correctly (" + e.getMessage() + ")"); } }
@Test public void testOverflowingUnicodeInTripleSubject() throws IOException { String data = "@prefix xsd: <http: Reader r = new StringReader(data); try { parser.parse(r, baseURI); assertTrue(statementCollector.getStatements().size() == 1); } catch (RDFParseException e) { fail("Complex unicode characters should be parsed correctly (" + e.getMessage() + ")"); } }
@Test public void testParseRDFStarData() throws IOException { IRI bob = vf.createIRI("http: IRI alice = vf.createIRI("http: IRI book = vf.createIRI("http: IRI otherbook = vf.createIRI("http: IRI bobshomepage = vf.createIRI("http: IRI a = vf.createIRI("http: IRI b = vf.createIRI("http: IRI c = vf.createIRI("http: IRI valid = vf.createIRI("http: Literal abcDate = vf.createLiteral("1999-08-16", XSD.DATE); Literal birthDate = vf.createLiteral("1908-03-18", XSD.DATE); Literal titleEn = vf.createLiteral("Example book", "en"); Literal titleDe = vf.createLiteral("Beispielbuch", "de"); Literal titleEnUs = vf.createLiteral("Example Book", "en-US"); Triple bobCreatedBook = vf.createTriple(bob, DCTERMS.CREATED, book); Triple aliceKnowsBobCreatedBook = vf.createTriple(alice, FOAF.KNOWS, bobCreatedBook); Triple bobCreatedBookKnowsAlice = vf.createTriple(bobCreatedBook, FOAF.KNOWS, alice); Triple bookCreatorAlice = vf.createTriple(book, DCTERMS.CREATOR, alice); Triple aliceCreatedBook = vf.createTriple(alice, DCTERMS.CREATED, book); Triple abc = vf.createTriple(a, b, c); Triple bobBirthdayDate = vf.createTriple(bob, FOAF.BIRTHDAY, birthDate); Triple bookTitleEn = vf.createTriple(book, DCTERMS.TITLE, titleEn); Triple bookTitleDe = vf.createTriple(book, DCTERMS.TITLE, titleDe); Triple bookTitleEnUs = vf.createTriple(book, DCTERMS.TITLE, titleEnUs); try (InputStream in = this.getClass().getResourceAsStream("/test-rdfstar.ttls")) { parser.parse(in, baseURI); Collection<Statement> stmts = statementCollector.getStatements(); assertEquals(10, stmts.size()); assertTrue(stmts.contains(vf.createStatement(bob, FOAF.KNOWS, aliceKnowsBobCreatedBook))); assertTrue(stmts.contains(vf.createStatement(bobCreatedBookKnowsAlice, DCTERMS.SOURCE, otherbook))); assertTrue(stmts.contains(vf.createStatement(bobshomepage, DCTERMS.SOURCE, bookCreatorAlice))); assertTrue(stmts.contains(vf.createStatement(bookCreatorAlice, DCTERMS.SOURCE, bobshomepage))); assertTrue(stmts.contains(vf.createStatement(bookCreatorAlice, DCTERMS.REQUIRES, aliceCreatedBook))); assertTrue(stmts.contains(vf.createStatement(abc, valid, abcDate))); assertTrue(stmts.contains(vf.createStatement(bobBirthdayDate, DCTERMS.SOURCE, bobshomepage))); assertTrue(stmts.contains(vf.createStatement(bookTitleEn, DCTERMS.SOURCE, bobshomepage))); assertTrue(stmts.contains(vf.createStatement(bookTitleDe, DCTERMS.SOURCE, bobshomepage))); assertTrue(stmts.contains(vf.createStatement(bookTitleEnUs, DCTERMS.SOURCE, bobshomepage))); } }
@Test(expected = RDFParseException.class) public void testParseRDFStar_TurtleStarDisabled() throws IOException { parser.getParserConfig().set(TurtleParserSettings.ACCEPT_TURTLESTAR, false); try (InputStream in = this.getClass().getResourceAsStream("/test-rdfstar.ttls")) { parser.parse(in, baseURI); } }