Skip to content

Commit

Permalink
Fix #780: Validate all IRIs in AbstractRDFParser by default
Browse files Browse the repository at this point in the history
Signed-off-by: James Leigh <james.leigh@ontotext.com>
  • Loading branch information
James Leigh committed May 31, 2017
1 parent c38e52f commit 828be54
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class RDFJSONParserCustomTest {
public void testSupportedSettings()
throws Exception
{
assertEquals(17, Rio.createParser(RDFFormat.RDFJSON).getSupportedSettings().size());
assertEquals(18, Rio.createParser(RDFFormat.RDFJSON).getSupportedSettings().size());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,6 @@ public void testParseCommentAtStart()
public void testSupportedSettings()
throws Exception
{
assertEquals(21, Rio.createParser(RDFFormat.RDFXML).getSupportedSettings().size());
assertEquals(22, Rio.createParser(RDFFormat.RDFXML).getSupportedSettings().size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public void testBadPname02()
public void testSupportedSettings()
throws Exception
{
assertEquals(12, Rio.createParser(RDFFormat.TRIG).getSupportedSettings().size());
assertEquals(13, Rio.createParser(RDFFormat.TRIG).getSupportedSettings().size());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,25 @@
*******************************************************************************/
package org.eclipse.rdf4j.rio.turtle;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.Collections;
import java.util.function.Consumer;

import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Literal;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.Namespace;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.model.impl.LinkedHashModel;
import org.eclipse.rdf4j.model.impl.NamespaceImpl;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
import org.eclipse.rdf4j.model.impl.ValueFactoryImpl;
import org.eclipse.rdf4j.model.util.Models;
import org.eclipse.rdf4j.model.vocabulary.RDF;
Expand Down Expand Up @@ -206,7 +212,7 @@ public void testLiteralWithNewlines()
public void testSupportedSettings()
throws Exception
{
assertEquals(12, parser.getSupportedSettings().size());
assertEquals(13, parser.getSupportedSettings().size());
}

@Test
Expand Down Expand Up @@ -507,4 +513,24 @@ public void testParsingNamespacesWithOverride()
assertTrue(model.contains(vf.createIRI("urn:a"), vf.createIRI("urn:not_skos:broader"),
vf.createIRI("urn:b")));
}

@Test
public void test780IRISpace()
throws Exception
{
String ttl = "_:b25978837 a <http://purl.bioontology.org/ontology/UATC/\\u0020SERINE\\u0020\\u0020> .";
try {
Rio.parse(new StringReader(ttl), "", RDFFormat.TURTLE);
fail();
}
catch (RDFParseException e) {
// Invalid IRI
}
Model model = Rio.parse(new StringReader(ttl), "", RDFFormat.TURTLE,
new ParserConfig().set(BasicParserSettings.VERIFY_URI_SYNTAX, false),
SimpleValueFactory.getInstance(), new ParseErrorLogger());
assertEquals(1, model.size());
model.filter(null, RDF.TYPE, null).objects().forEach(obj -> assertEquals(
"http://purl.bioontology.org/ontology/UATC/%20SERINE%20%20", obj.stringValue()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
*******************************************************************************/
package org.eclipse.rdf4j.rio.turtle;

import junit.framework.Test;

import org.eclipse.rdf4j.rio.RDFParser;
import org.eclipse.rdf4j.rio.helpers.TurtleParserSettings;
import org.eclipse.rdf4j.rio.helpers.BasicParserSettings;
import org.eclipse.rdf4j.rio.ntriples.NTriplesParser;
import org.eclipse.rdf4j.rio.turtle.TurtleParser;
import org.eclipse.rdf4j.rio.turtle.TurtleParserTestCase;

import junit.framework.Test;

/**
* JUnit test for the Turtle parser that uses the tests that are available
Expand All @@ -30,11 +28,16 @@ public static Test suite()
@Override
protected RDFParser createTurtleParser() {
RDFParser result = new TurtleParser();
// localName_with_assigned_nfc_PN_CHARS_BASE_character_boundaries contains illegal trailing character
result.set(BasicParserSettings.VERIFY_URI_SYNTAX, false);
return result;
}

@Override
protected RDFParser createNTriplesParser() {
return new NTriplesParser();
NTriplesParser result = new NTriplesParser();
// localName_with_assigned_nfc_PN_CHARS_BASE_character_boundaries contains illegal trailing character
result.set(BasicParserSettings.VERIFY_URI_SYNTAX, false);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ public Collection<RioSetting<?>> getSupportedSettings() {
// Supported in RDFParserBase.resolveURI
result.add(BasicParserSettings.VERIFY_RELATIVE_URIS);

// Supported in createURI
result.add(BasicParserSettings.VERIFY_URI_SYNTAX);

// Supported in RDFParserBase.createBNode(String)
result.add(BasicParserSettings.PRESERVE_BNODE_IDS);

Expand Down Expand Up @@ -376,7 +379,15 @@ protected IRI resolveURI(String uriSpec)
throws RDFParseException
{
// Resolve relative URIs against base URI
ParsedIRI uri = ParsedIRI.create(uriSpec);
ParsedIRI uri;
try {
uri = new ParsedIRI(uriSpec);
}
catch (URISyntaxException e) {
reportError("Invalid IRI '" + uriSpec,
BasicParserSettings.VERIFY_URI_SYNTAX);
uri = ParsedIRI.create(uriSpec);
}

if (!uri.isAbsolute()) {
if (baseURI == null) {
Expand All @@ -403,6 +414,15 @@ protected IRI resolveURI(String uriSpec)
protected IRI createURI(String uri)
throws RDFParseException
{
if (getParserConfig().get(BasicParserSettings.VERIFY_URI_SYNTAX)) {
try {
new ParsedIRI(uri);
}
catch (URISyntaxException e) {
reportError(e.getMessage(),
BasicParserSettings.VERIFY_URI_SYNTAX);
}
}
try {
return valueFactory.createIRI(uri);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ public void assertHandler(int expected) {
public void testSupportedSettings()
throws Exception
{
assertEquals(12, parser.getSupportedSettings().size());
assertEquals(13, parser.getSupportedSettings().size());
}

protected abstract RDFParser createRDFParser();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ public void testBlankNodeIdentifiersRDF11()
public void testSupportedSettings()
throws Exception
{
assertEquals(12, createRDFParser().getSupportedSettings().size());
assertEquals(13, createRDFParser().getSupportedSettings().size());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<:_about> <http://purl.org/dc/terms/creator> <http://example.org/anna> .
<:_anna> <http://xmlns.com/foaf/0.1/name> "Anna" .
_:about <http://purl.org/dc/terms/creator> <http://example.org/anna> .
_:anna <http://xmlns.com/foaf/0.1/name> "Anna" .
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
":_about" : {
"_:about" : {
"http://purl.org/dc/terms/creator" : [ { "value" : "http://example.org/anna",
"type" : "uri" } ]
},
":_anna" : {
"_:anna" : {
"http://xmlns.com/foaf/0.1/name" : [ { "value" : "Anna",
"type" : "literal" } ]
}
}
}

0 comments on commit 828be54

Please sign in to comment.