Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ public static String guessLocalName(String iri) {
if (matcher == null || !matcher.matches()) {
return iri;
} else if (matcher.matches()) {
if (matcher.group("fragment") != null) { // If the IRI has a fragment
return matcher.group("fragment");
if (matcher.group("anchor") != null) {
return matcher.group("fragment") != null ? matcher.group("fragment") : "";
} else if (matcher.group(FINAL_PATH_GROUP) != null) { // If the IRI has no fragment but does not end with a slash
return matcher.group(FINAL_PATH_GROUP);
} else { // If the URI ends with a slash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public class RDFaParser extends AbstractRDFParser {

private String baseIri = IOConstants.getDefaultBaseURI();

private SAXParser saxParser;
private SAXParserFactory saxParserFactory;
private final SAXParser saxParser;
private final SAXParserFactory saxParserFactory;

/**
* An index of IRI prefixes
Expand Down Expand Up @@ -249,7 +249,13 @@ && isAttributePresent(RDFaAttributes.HREF)) {
throw new ParsingException("Prefix '" + prefixName + "' contains underscore character which is not allowed in xmlns declaration");
}

IRI prefixNamespace = getValueFactory().createIRI(attributeValue, "");
IRI prefixNamespace;
if (IRIUtils.isStandardIRI(attributeValue)) {
prefixNamespace = getValueFactory().createIRI(attributeValue);
} else {
String baseIriString = currentProcessingContext().getEvaluationContext().getBaseIri().stringValue();
prefixNamespace = getValueFactory().createIRI(baseIriString + attributeValue);
}
this.addIriMapping(prefixName, prefixNamespace);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ public String resolveIRI(String raw) {
} else if (isAbsoluteIRI(raw)) {
return raw;
} else {
if (prefix.isEmpty()) {
throw new ParsingException(
"Undeclared prefix: '' (empty prefix). " +
"Use '@prefix : <namespace> .' to declare it.");
}
throw new ParsingException("Undeclared prefix: " + prefix);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,32 @@ void test429ErrorOnDTD() {
assertTrue(testModel.contains(subject, RDF.type.getIRI(), object));
}

@Test
void parseXmlnsWithRelativeUri() {
String xhtml = """
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ex="vocab#"
version="XHTML+RDFa 1.1">
<body>
<p about="#me" typeof="ex:Person" property="ex:name">Alice</p>
</body>
</html>""";

Model model = createTestModel();
RDFParser parser = parserFactory.createRDFParser(RDFFormat.RDFA, model, valueFactory);
parser.parse(new ByteArrayInputStream(xhtml.getBytes()), "http://example.org/test/");

IRI subject = valueFactory.createIRI("http://example.org/test/#me");
IRI type = RDF.type.getIRI();
IRI personClass = valueFactory.createIRI("http://example.org/test/vocab#Person");
IRI namePred = valueFactory.createIRI("http://example.org/test/vocab#name");

assertTrue(model.contains(subject, type, personClass));
assertTrue(model.contains(subject, namePred, valueFactory.createLiteral("Alice")));
}

private static void logModelContent(Model model) {
StringWriter outWriter = new StringWriter();
RDFSerializer serializer = (new DefaultRDFSerializerFactory()).createSerializer(RDFFormat.TURTLE, model);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,17 @@ void testRdfaManifest() {
assertEquals(refModel.size(), model.size());
}

@Test
void testUndeclaredEmptyPrefixThrowsParsingException() {
String turtle = ":subject :predicate :object .";
Model model = createTestModel();
RDFParser parser = new TurtleParser(model, factory);

fr.inria.corese.core.next.data.api.exception.ParsingException ex = org.junit.jupiter.api.Assertions.assertThrows(
fr.inria.corese.core.next.data.api.exception.ParsingException.class,
() -> parser.parse(new StringReader(turtle))
);
assertTrue(ex.getMessage().contains("Undeclared prefix: '' (empty prefix)"));
}

}
Loading