You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2024/08/14 18:11:37 UTC
(jena) branch main updated (9b7e5ad584 -> 452816cf97)
This is an automated email from the ASF dual-hosted git repository.
andy pushed a change to branch main
in repository https://gitbox.apache.org/repos/asf/jena.git
from 9b7e5ad584 GH-2638: Configure jena-benchmark-jmh for annotation processing
new 4ea2c55285 RRX: Fix xml:base handling
new e5a71d6956 RRX: Check for bad use of rdf:resource
new 978891e87a RRX: Consistent behaviour for relative base URIs
new e36f93c9d0 GH-2629: Check for unqualified elements
new 452816cf97 GH-2630: Handle unqualified attributes
The 5 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../apache/jena/riot/lang/rdfxml/package-info.java | 9 +-
.../{ParserRDFXML_SAX.java => ParserRRX_SAX.java} | 387 ++++++++++++---------
.../riot/lang/rdfxml/rrx/ReaderRDFXML_SAX.java | 2 +-
...rRDFXML_StAX_EV.java => ParserRRX_StAX_EV.java} | 318 +++++++++++------
.../rdfxml/rrx_stax_ev/ReaderRDFXML_StAX_EV.java | 2 +-
...rRDFXML_StAX_SR.java => ParserRRX_StAX_SR.java} | 316 +++++++++++------
.../rdfxml/rrx_stax_sr/ReaderRDFXML_StAX_SR.java | 2 +-
.../jena/riot/lang/rdfxml/rrx/RunTestRDFXML.java | 16 +-
.../apache/jena/riot/lang/rdfxml/rrx/TestRRX.java | 169 +++++++--
.../testing/RIOT/rrx-files/bad-rdf-id-node.rdf | 14 +
.../RIOT/rrx-files/bad-rdf-resource-node.rdf | 14 +
.../RIOT/rrx-files/bad-unqualified-attribute1.rdf | 6 +
.../RIOT/rrx-files/bad-unqualified-attribute2.rdf | 9 +
.../RIOT/rrx-files/bad-unqualified-attribute3.rdf | 9 +
.../RIOT/rrx-files/bad-unqualified-attribute4.rdf | 9 +
.../RIOT/rrx-files/bad-unqualified-attribute5.rdf | 9 +
.../RIOT/rrx-files/bad-unqualified-class.rdf | 6 +
.../RIOT/rrx-files/bad-unqualified-property.rdf | 8 +
...-external-base.rdf => base-external-needed.rdf} | 3 +
jena-arq/testing/RIOT/rrx-files/base-inner.rdf | 12 +
.../rrx-files/{file-no-base.rdf => base-none.rdf} | 3 +
.../rrx-files/{warn01.rdf => parseType-warn.rdf} | 0
.../src/main/java/org/apache/jena/irix/IRIx.java | 1 -
23 files changed, 892 insertions(+), 432 deletions(-)
rename jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx/{ParserRDFXML_SAX.java => ParserRRX_SAX.java} (86%)
rename jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_ev/{ParserRDFXML_StAX_EV.java => ParserRRX_StAX_EV.java} (88%)
rename jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_sr/{ParserRDFXML_StAX_SR.java => ParserRRX_StAX_SR.java} (89%)
create mode 100644 jena-arq/testing/RIOT/rrx-files/bad-rdf-id-node.rdf
create mode 100644 jena-arq/testing/RIOT/rrx-files/bad-rdf-resource-node.rdf
create mode 100644 jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute1.rdf
create mode 100644 jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute2.rdf
create mode 100644 jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute3.rdf
create mode 100644 jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute4.rdf
create mode 100644 jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute5.rdf
create mode 100644 jena-arq/testing/RIOT/rrx-files/bad-unqualified-class.rdf
create mode 100644 jena-arq/testing/RIOT/rrx-files/bad-unqualified-property.rdf
rename jena-arq/testing/RIOT/rrx-files/{file-external-base.rdf => base-external-needed.rdf} (69%)
create mode 100644 jena-arq/testing/RIOT/rrx-files/base-inner.rdf
rename jena-arq/testing/RIOT/rrx-files/{file-no-base.rdf => base-none.rdf} (70%)
rename jena-arq/testing/RIOT/rrx-files/{warn01.rdf => parseType-warn.rdf} (100%)
(jena) 04/05: GH-2629: Check for unqualified elements
Posted by an...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
andy pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/jena.git
commit e36f93c9d04866f8892c0b563bba0809399e76e1
Author: Andy Seaborne <an...@apache.org>
AuthorDate: Sat Aug 10 21:41:34 2024 +0100
GH-2629: Check for unqualified elements
---
.../jena/riot/lang/rdfxml/rrx/ParserRRX_SAX.java | 17 +++--
.../lang/rdfxml/rrx_stax_ev/ParserRRX_StAX_EV.java | 12 ++-
.../lang/rdfxml/rrx_stax_sr/ParserRRX_StAX_SR.java | 19 ++---
.../jena/riot/lang/rdfxml/rrx/RunTestRDFXML.java | 2 +-
.../apache/jena/riot/lang/rdfxml/rrx/TestRRX.java | 88 ++++++++++++++++++++--
.../{rdf-resource-node.rdf => bad-rdf-id-node.rdf} | 4 +-
...resource-node.rdf => bad-rdf-resource-node.rdf} | 0
.../RIOT/rrx-files/bad-unqualified-attribute1.rdf | 6 ++
.../RIOT/rrx-files/bad-unqualified-attribute2.rdf | 9 +++
.../RIOT/rrx-files/bad-unqualified-class.rdf | 6 ++
.../RIOT/rrx-files/bad-unqualified-property.rdf | 8 ++
11 files changed, 135 insertions(+), 36 deletions(-)
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx/ParserRRX_SAX.java b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx/ParserRRX_SAX.java
index aca5212fcb..9f34f9cd2e 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx/ParserRRX_SAX.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx/ParserRRX_SAX.java
@@ -696,7 +696,7 @@ class ParserRRX_SAX
if ( isNotRecognizedRDFtype(namespaceURI, localName) )
RDFXMLparseWarning(qName+" is not a recognized RDF term for a type", position);
}
- Node object = qNameToIRI(namespaceURI, localName, position);
+ Node object = qNameToIRI(namespaceURI, localName, position, "typed node element");
emit(currentSubject, RDF.Nodes.type, object, position);
}
@@ -724,8 +724,10 @@ class ParserRRX_SAX
int i = containerPropertyCounter.value++;
String p = rdfNS+"_"+i;
currentProperty = iri(p, position);
- } else
- currentProperty = qNameToIRI(namespaceURI, localName, position);
+ } else {
+ // The empty string namespace does not apply to XML attributes.
+ currentProperty = qNameToIRI(namespaceURI, localName, position, "property element");
+ }
if ( ReaderRDFXML_SAX.TRACE )
trace.printf("Property = %s\n", str(currentProperty));
@@ -1026,7 +1028,7 @@ class ParserRRX_SAX
return;
}
}
- Node property = qNameToIRI(namespaceURI, localName, position);
+ Node property = qNameToIRI(namespaceURI, localName, position, "property attribute");
String lex = value;
Node object = literal(lex, currentLang, position);
emit(subject, property, object, position);
@@ -1269,10 +1271,9 @@ class ParserRRX_SAX
// ---- Creating terms.
- private Node qNameToIRI(String namespaceURI, String localName, Position position) {
- if ( StringUtils.isBlank(namespaceURI) ) {
- RDFXMLparseWarning("Unqualified typed nodes are not allowed: <"+localName+">", position);
- }
+ private Node qNameToIRI(String namespaceURI, String localName, Position position, String usage) {
+ if ( StringUtils.isBlank(namespaceURI) )
+ throw RDFXMLparseError("Unqualified "+usage+" not allowed: <"+localName+">", position);
String uriStr = qNameToIRI(namespaceURI, localName);
return iri(uriStr, position);
}
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_ev/ParserRRX_StAX_EV.java b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_ev/ParserRRX_StAX_EV.java
index fd354eb49b..84e97ead58 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_ev/ParserRRX_StAX_EV.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_ev/ParserRRX_StAX_EV.java
@@ -448,7 +448,7 @@ class ParserRRX_StAX_EV {
RDFXMLparseWarning(str(qName)+" is not a recognized RDF term for a type", location);
}
- Node object = qNameToIRI(qName, location);
+ Node object = qNameToIRI(qName, location, "typed node element");
emit(subject, NodeConst.nodeRDFType, object, location);
}
@@ -489,7 +489,7 @@ class ParserRRX_StAX_EV {
emit(subject, RDF.Nodes.type, type, location);
continue;
}
- Node property = qNameToIRI(attribute.getName(), location);
+ Node property = qNameToIRI(attribute.getName(), location, "property attribute");
Node object = literal(attribute.getValue(), currentLang, location);
emit(subject, property, object, location);
}
@@ -512,8 +512,6 @@ class ParserRRX_StAX_EV {
String namespace = qName.getNamespaceURI();
String localName = qName.getLocalPart();
if ( namespace == null || namespace.isEmpty() ) {
- // Note about XML: The empty string namespace does not apply to XML attributes,
- // only XML elements. ":attr" is not legal XML.
//RDFXMLparseError("XML attribute '"+localName+"' used for RDF property attribute (no namespace)", event);
if ( outputWarnings )
RDFXMLparseWarning("XML attribute '"+localName+"' used for RDF property attribute - ignored", event);
@@ -589,7 +587,7 @@ class ParserRRX_StAX_EV {
if ( qNameMatches(rdfContainerItem, startElt.getName()) )
property = iriDirect(rdfNS+"_"+Integer.toString(listElementCounter.value++), location);
else
- property = qNameToIRI(startElt.getName(), location);
+ property = qNameToIRI(startElt.getName(), location, "property element");
Node reify = reifyStatement(startElt);
Emitter emitter = (reify==null) ? this::emit : (s,p,o,loc)->emitReify(reify, s, p, o, loc);
@@ -1128,9 +1126,9 @@ class ParserRRX_StAX_EV {
parserProfile.setBaseIRI(n.getURI());
}
- private Node qNameToIRI(QName qName, Location location) {
+ private Node qNameToIRI(QName qName, Location location, String usage) {
if ( StringUtils.isBlank(qName.getNamespaceURI()) )
- RDFXMLparseWarning("Unqualified typed nodes are not allowed: <"+qName.getLocalPart()+">", location);
+ throw RDFXMLparseError("Unqualified "+usage+" not allowed: <"+qName.getLocalPart()+">", location);
String uriStr = qNameToIRI(qName);
return iriDirect(uriStr, location);
}
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_sr/ParserRRX_StAX_SR.java b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_sr/ParserRRX_StAX_SR.java
index 9ed1672a2d..a8eaa8402e 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_sr/ParserRRX_StAX_SR.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_sr/ParserRRX_StAX_SR.java
@@ -428,7 +428,7 @@ class ParserRRX_StAX_SR {
RDFXMLparseWarning(str(qName)+" is not a recognized RDF term for a type");
}
- Node object = qNameToIRI(qName, location);
+ Node object = qNameToIRI(qName, location, "type node element");
emit(subject, NodeConst.nodeRDFType, object, location);
}
@@ -462,7 +462,7 @@ class ParserRRX_StAX_SR {
emit(subject, RDF.Nodes.type, type, location);
continue;
}
- Node property = qNameToIRI(qName, location);
+ Node property = qNameToIRI(qName, location, "property attribute");
String lexicalForm = xmlSource.getAttributeValue(i);
Node object = literal(lexicalForm, currentLang, location);
emit(subject, property, object, location);
@@ -486,15 +486,13 @@ class ParserRRX_StAX_SR {
* @param qName
*/
private boolean checkPropertyAttribute(QName qName, boolean outputWarnings) {
- String namespace = qName.getNamespaceURI();
- if ( namespace == null || namespace.isEmpty() ) {
- // Note about XML: The empty string namespace does not apply to XML attributes,
- // only XML elements. ":attr" is not legal XML.
- //RDFXMLparseError("XML attribute '"+qName.getLocalPart()+"' used for RDF property attribute (no namespace)", event);
+ if ( StringUtils.isBlank(qName.getNamespaceURI()) ) {
+ //RDFXMLparseError("XML attribute '"+localName+"' used for RDF property attribute (no namespace)", event);
if ( outputWarnings )
RDFXMLparseWarning("XML attribute '"+qName.getLocalPart()+"' used for RDF property attribute - ignored");
return false;
}
+
if ( isSyntaxAttribute(qName) )
return false;
@@ -564,7 +562,7 @@ class ParserRRX_StAX_SR {
if ( qNameMatches(rdfContainerItem, qName) )
property = iriDirect(rdfNS+"_"+Integer.toString(listElementCounter.value++), location());
else
- property = qNameToIRI(qName, location);
+ property = qNameToIRI(qName, location, "property element");
Node reify = reifyStatement(location);
Emitter emitter = (reify==null) ? this::emit : (s,p,o,loc)->emitReify(reify, s, p, o, loc);
@@ -1084,10 +1082,9 @@ class ParserRRX_StAX_SR {
}
/** This is the RDF rule for creating an IRI from a QName. */
- private Node qNameToIRI(QName qName, Location location) {
+ private Node qNameToIRI(QName qName, Location location, String usage) {
if ( StringUtils.isBlank(qName.getNamespaceURI()) )
- RDFXMLparseWarning("Unqualified typed nodes are not allowed: <"+qName.getLocalPart()+">", location);
-
+ throw RDFXMLparseError("Unqualified "+usage+" not allowed: <"+qName.getLocalPart()+">", location);
String uriStr = strQNameToIRI(qName);
return iriDirect(uriStr, location);
}
diff --git a/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/RunTestRDFXML.java b/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/RunTestRDFXML.java
index 64712ff262..2f12d64e41 100644
--- a/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/RunTestRDFXML.java
+++ b/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/RunTestRDFXML.java
@@ -329,7 +329,7 @@ public class RunTestRDFXML {
ErrorHandlerCollector actualErrorHandler = new ErrorHandlerCollector();
assertThrows(RiotException.class, ()->{
parseFile(testSubjectFactory, actualErrorHandler, filename);
- output.printf("## Expected RiotExpection : %-4s : %s : %s", subjectLabel, testLabel, filename);
+ //output.printf("## Expected RiotExpection : %-4s : %s : %s\n", subjectLabel, testLabel, filename);
});
checkErrorHandler(testLabel, actualErrorHandler, -1, 1, 0);
}
diff --git a/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/TestRRX.java b/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/TestRRX.java
index cd4e4a8c24..44235fb102 100644
--- a/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/TestRRX.java
+++ b/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/TestRRX.java
@@ -20,15 +20,22 @@ package org.apache.jena.riot.lang.rdfxml.rrx;
import java.io.IOException;
import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.ArrayList;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+import org.apache.commons.collections4.SetUtils;
import org.apache.jena.atlas.io.IO;
import org.apache.jena.atlas.io.IOX;
import org.apache.jena.riot.*;
import org.apache.jena.riot.lang.rdfxml.RRX;
import org.apache.jena.riot.lang.rdfxml.rrx.RunTestRDFXML.ErrorHandlerCollector;
import org.apache.jena.riot.system.*;
+import org.junit.AfterClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@@ -65,17 +72,41 @@ public class TestRRX {
this.lang = lang;
}
- // Test2 for more than one object in RDF/XML striping.
+ // XXX Track files!
+
+ private static Set<String> processedFiles = new HashSet<>();
+ private void trackFilename(String filename) {
+ processedFiles.add(filename);
+ }
+
+ /** Check all files in the were touched */
+
+ @AfterClass public static void checkFiles() {
+ // This can break when running single tests.
+ Set<String> fsFiles = localTestFiles();
+ if ( fsFiles.size() != processedFiles.size()) {
+ System.out.flush();
+ System.err.flush();
+ Set<String> missed = SetUtils.difference(fsFiles, processedFiles);
+ System.err.println("Missed files: ");
+ missed.forEach(x->System.err.printf(" %s\n",x));
+ System.out.flush();
+ System.err.flush();
+ //Assert.fail();
+ }
+ }
+
+ // Test for more than one object in RDF/XML striping.
@Test public void error_multiple_objects_lex_node() {
- checkForError("multiple_objects_lex_node.rdf");
+ checkForErrorCompare("multiple_objects_lex_node.rdf");
}
@Test public void error_multiple_objects_node_lex() {
- checkForError("multiple_objects_node_lex.rdf");
+ checkForErrorCompare("multiple_objects_node_lex.rdf");
}
@Test public void error_multiple_objects_node_node() {
- checkForError("multiple_objects_node_node.rdf");
+ checkForErrorCompare("multiple_objects_node_node.rdf");
}
// Check that the "one object" parse state does not impact deeper structures.
@@ -128,12 +159,26 @@ public class TestRRX {
warningTest("cim_statements01.rdf", 2);
}
- @Test public void rdfResourceBad() {
- checkForError("rdf-resource-node.rdf");
+ @Test public void element_node_rdf_resource_bad() {
+ checkForErrorCompare("bad-rdf-resource-node.rdf");
+ }
+
+ @Test public void element_node_rdf_id_bad() {
+ checkForErrorCompare("bad-rdf-id-node.rdf");
+ }
+
+ @Test public void bad_unqualified_property() {
+ checkForError("bad-unqualified-property.rdf", false);
+ }
+
+ @Test public void bad_unqualified_class() {
+ checkForError("bad-unqualified-class.rdf", false);
}
/** Parse with no base set by the parser */
private void noBase(String filename) {
+ trackFilename(filename);
+
ReaderRIOTFactory factory = RDFParserRegistry.getFactory(lang);
String fn = DIR+filename;
ErrorHandlerCollector errorHandler = new ErrorHandlerCollector();
@@ -152,6 +197,7 @@ public class TestRRX {
}
private void goodTest(String filename) {
+ trackFilename(filename);
ReaderRIOTFactory factory = RDFParserRegistry.getFactory(lang);
String fn = DIR+filename;
RunTestRDFXML.runTestPlain(filename, factory, label, fn);
@@ -159,6 +205,7 @@ public class TestRRX {
}
private void warningTest(String filename, int warnings) {
+ trackFilename(filename);
ReaderRIOTFactory factory = RDFParserRegistry.getFactory(lang);
String fn = DIR+filename;
RunTestRDFXML.runTestExpectWarning(filename, factory, label, warnings, fn);
@@ -167,20 +214,47 @@ public class TestRRX {
/**
* Run test, expecting an error.
+ * Compare to running ARP.
*/
- private void checkForError(String filename) {
+ private void checkForErrorCompare(String filename) {
checkForError(filename, true);
}
+ /**
+ * Run test, expecting an error.
+ * Dop not compare to running ARP.
+ */
+ private void checkForErroNoCompare(String filename) {
+ checkForError(filename, false);
+ }
+
+
/**
* Run test, expecting an error. If the second argument is true, also Compare to
* make sure it is the same as ARP.
*/
private void checkForError(String filename, boolean compare) {
+ trackFilename(filename);
ReaderRIOTFactory factory = RDFParserRegistry.getFactory(lang);
String fn = DIR+filename;
RunTestRDFXML.runTestExpectFailure(filename, factory, label, fn);
if ( compare )
RunTestRDFXML.runTestCompareARP(fn, factory, label, fn);
}
+
+ static Set<String> localTestFiles() {
+ Path LOCAL_DIR = Path.of(DIR);
+ Set<String> found;
+ try {
+ found = Files
+ .list(LOCAL_DIR)
+ // Directory relative name.
+ .map(path->path.getFileName().toString())
+ .filter(fn->fn.endsWith(".rdf"))
+ .collect(Collectors.toSet());
+ } catch (IOException e) {
+ throw IOX.exception(e);
+ }
+ return found;
+ }
}
diff --git a/jena-arq/testing/RIOT/rrx-files/rdf-resource-node.rdf b/jena-arq/testing/RIOT/rrx-files/bad-rdf-id-node.rdf
similarity index 70%
copy from jena-arq/testing/RIOT/rrx-files/rdf-resource-node.rdf
copy to jena-arq/testing/RIOT/rrx-files/bad-rdf-id-node.rdf
index ee3bbab131..db19f039ff 100644
--- a/jena-arq/testing/RIOT/rrx-files/rdf-resource-node.rdf
+++ b/jena-arq/testing/RIOT/rrx-files/bad-rdf-id-node.rdf
@@ -7,8 +7,8 @@
xmlns="http://local/"
xmlns:ex="http://example/" >
- <!-- rdf:resource on rdf:Description -->
- <rdf:Description rdf:about="http://example/s" rdf:resource="http://example/foo">
+ <!-- rdf:ID on rdf:Description -->
+ <rdf:Description rdf:about="http://example/s" rdf:ID="foo">
<ex:property>text</ex:property>
</rdf:Description>
</rdf:RDF>
\ No newline at end of file
diff --git a/jena-arq/testing/RIOT/rrx-files/rdf-resource-node.rdf b/jena-arq/testing/RIOT/rrx-files/bad-rdf-resource-node.rdf
similarity index 100%
rename from jena-arq/testing/RIOT/rrx-files/rdf-resource-node.rdf
rename to jena-arq/testing/RIOT/rrx-files/bad-rdf-resource-node.rdf
diff --git a/jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute1.rdf b/jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute1.rdf
new file mode 100644
index 0000000000..dd1db8e7c3
--- /dev/null
+++ b/jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute1.rdf
@@ -0,0 +1,6 @@
+<?xml version='1.0'?>
+<!-- Licensed under the terms of https://www.apache.org/licenses/LICENSE-2.0 -->
+
+<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
+ <rdf:Description name="NAME"/>
+</rdf:RDF>
\ No newline at end of file
diff --git a/jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute2.rdf b/jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute2.rdf
new file mode 100644
index 0000000000..21940b5d63
--- /dev/null
+++ b/jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute2.rdf
@@ -0,0 +1,9 @@
+<?xml version='1.0'?>
+<!-- Licensed under the terms of https://www.apache.org/licenses/LICENSE-2.0 -->
+
+<rdf:RDF
+ xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+ xmlns="http://example/">
+ <!-- qnames do not apply to attributes unless ex:attr -->
+ <rdf:Description name="NAME"/>
+</rdf:RDF>
\ No newline at end of file
diff --git a/jena-arq/testing/RIOT/rrx-files/bad-unqualified-class.rdf b/jena-arq/testing/RIOT/rrx-files/bad-unqualified-class.rdf
new file mode 100644
index 0000000000..aa59cef963
--- /dev/null
+++ b/jena-arq/testing/RIOT/rrx-files/bad-unqualified-class.rdf
@@ -0,0 +1,6 @@
+<?xml version='1.0'?>
+<!-- Licensed under the terms of https://www.apache.org/licenses/LICENSE-2.0 -->
+
+<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
+ <Class/>
+</rdf:RDF>
diff --git a/jena-arq/testing/RIOT/rrx-files/bad-unqualified-property.rdf b/jena-arq/testing/RIOT/rrx-files/bad-unqualified-property.rdf
new file mode 100644
index 0000000000..46a054784e
--- /dev/null
+++ b/jena-arq/testing/RIOT/rrx-files/bad-unqualified-property.rdf
@@ -0,0 +1,8 @@
+<?xml version='1.0'?>
+<!-- Licensed under the terms of https://www.apache.org/licenses/LICENSE-2.0 -->
+
+<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
+ <rdf:Description rdf:about="http://example/s">
+ <property></property>
+ </rdf:Description>
+</rdf:RDF>
\ No newline at end of file
(jena) 03/05: RRX: Consistent behaviour for relative base URIs
Posted by an...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
andy pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/jena.git
commit 978891e87aaf15e989416e7907dfb9c2f8d66b02
Author: Andy Seaborne <an...@apache.org>
AuthorDate: Sat Aug 10 15:18:59 2024 +0100
RRX: Consistent behaviour for relative base URIs
---
.../{ParserRDFXML_SAX.java => ParserRRX_SAX.java} | 133 +++++++++++----------
.../riot/lang/rdfxml/rrx/ReaderRDFXML_SAX.java | 2 +-
...rRDFXML_StAX_EV.java => ParserRRX_StAX_EV.java} | 85 ++++++++-----
.../rdfxml/rrx_stax_ev/ReaderRDFXML_StAX_EV.java | 2 +-
...rRDFXML_StAX_SR.java => ParserRRX_StAX_SR.java} | 93 ++++++++------
.../rdfxml/rrx_stax_sr/ReaderRDFXML_StAX_SR.java | 2 +-
.../jena/riot/lang/rdfxml/rrx/RunTestRDFXML.java | 4 +-
.../apache/jena/riot/lang/rdfxml/rrx/TestRRX.java | 62 ++++++----
...-external-base.rdf => base-external-needed.rdf} | 0
.../{file-external-base.rdf => base-inner.rdf} | 5 +-
.../rrx-files/{file-no-base.rdf => base-none.rdf} | 0
.../src/main/java/org/apache/jena/irix/IRIx.java | 1 -
12 files changed, 234 insertions(+), 155 deletions(-)
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx/ParserRDFXML_SAX.java b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx/ParserRRX_SAX.java
similarity index 95%
rename from jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx/ParserRDFXML_SAX.java
rename to jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx/ParserRRX_SAX.java
index c52d843727..aca5212fcb 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx/ParserRDFXML_SAX.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx/ParserRRX_SAX.java
@@ -27,6 +27,7 @@ import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
import javax.xml.namespace.QName;
+import org.apache.commons.lang3.StringUtils;
import org.apache.jena.atlas.io.IndentedWriter;
import org.apache.jena.atlas.lib.EscapeStr;
import org.apache.jena.datatypes.RDFDatatype;
@@ -35,7 +36,6 @@ import org.apache.jena.graph.Node;
import org.apache.jena.graph.NodeFactory;
import org.apache.jena.graph.Triple;
import org.apache.jena.irix.IRIException;
-import org.apache.jena.irix.IRIs;
import org.apache.jena.irix.IRIx;
import org.apache.jena.riot.RiotException;
import org.apache.jena.riot.lang.rdfxml.RDFXMLParseException;
@@ -52,7 +52,7 @@ import org.xml.sax.ext.DeclHandler;
import org.xml.sax.ext.EntityResolver2;
import org.xml.sax.ext.LexicalHandler;
-class ParserRDFXML_SAX
+class ParserRRX_SAX
implements
ContentHandler,
ErrorHandler,
@@ -426,7 +426,7 @@ class ParserRDFXML_SAX
// // Forming objects.
// private ParseType parseType = null;
- ParserRDFXML_SAX(String xmlBase, ParserProfile parserProfile, StreamRDF destination, Context context) {
+ ParserRRX_SAX(String xmlBase, ParserProfile parserProfile, StreamRDF destination, Context context) {
// Debug
if ( ReaderRDFXML_SAX.TRACE ) {
IndentedWriter out1 = IndentedWriter.stdout.clone();
@@ -665,8 +665,7 @@ class ParserRDFXML_SAX
* ++ nodeElementURIs anyURI - ( coreSyntaxTerms | rdf:li | oldTerms ) */
private void startNodeElement(String namespaceURI, String localName, String qName, Attributes attributes, Position position) {
- // Subject - maybe inside rdf:RDF
-
+ // Subject
String rdfResourceStr = attributes.getValue(rdfNS, rdfResource);
if ( rdfResourceStr != null )
throw RDFXMLparseError("rdf:resource not allowed as attribute here: "+qName, position);
@@ -934,14 +933,21 @@ class ParserRDFXML_SAX
}
private void processBaseAndLang(Attributes attributes, Position position) {
- // Too early.
- IRIx base = xmlBase(attributes, position);
- if ( base != null ) {
- currentBase = base;
+ //resolves.
+ IRIx xmlBase = xmlBase(attributes, position);
+ String xmlLang = xmlLang(attributes, position);
+ if ( ReaderRDFXML_SAX.TRACE ) {
+ if ( xmlBase != null )
+ trace.printf("+ BASE <%s>\n", xmlBase);
+ if ( xmlLang != null )
+ trace.printf("+ LANG @%s\n", xmlLang);
+ }
+ if ( xmlBase != null ) {
+ currentBase = xmlBase;// resolve.
}
- String lang = xmlLang(attributes, position);
- if ( lang != null )
- currentLang = lang;
+
+ if ( xmlLang != null )
+ currentLang = xmlLang;
}
// Property attributes.
@@ -951,9 +957,6 @@ class ParserRDFXML_SAX
boolean isPropertyAttribute = checkPropertyAttribute(attributes, i, false, position);
if ( ! isPropertyAttribute )
continue;
- String namespace = attributes.getURI(i);
- String localName = attributes.getLocalName(i);
- String qName = attributes.getQName(i);
propertyAttribute(subject, attributes, i, position);
}
}
@@ -972,32 +975,34 @@ class ParserRDFXML_SAX
/** Return true if this is a property attribute. */
private boolean checkPropertyAttribute(Attributes attributes, int index, boolean outputWarnings, Position position) {
- String namespace = attributes.getURI(index);
+ String namespaceURI = attributes.getURI(index);
String localName = attributes.getLocalName(index);
String qName = attributes.getQName(index);
- if ( namespace == null || namespace.isEmpty() ) {
- // In SAX, xmlns: is qname, but namespace and local name are "".
+ if ( StringUtils.isBlank(namespaceURI) ) {
+ // Note about XML: The empty string namespace does not apply to XML attributes,
+ // only XML elements. ":attr" is not legal XML.
//RDFXMLparseError("XML attribute '"+qName+"' used for RDF property attribute (no namespace)", position);
+
if ( outputWarnings ){
if ( ! localName.isEmpty() ) // Skip XML namespace declarations.
RDFXMLparseWarning("XML attribute '"+qName+"' used for RDF property attribute - ignored", position);
}
return false;
}
- if ( isSyntaxAttribute(namespace, localName) )
+ if ( isSyntaxAttribute(namespaceURI, localName) )
return false;
- if ( ! allowedPropertyAttributeURIs(namespace, localName) )
+ if ( ! allowedPropertyAttributeURIs(namespaceURI, localName) )
throw RDFXMLparseError("Not allowed as a property attribute: '"+attributes.getQName(index)+"'", position);
- if ( outputWarnings && isNotRecognizedRDFproperty(namespace, localName) )
+ if ( outputWarnings && isNotRecognizedRDFproperty(namespaceURI, localName) )
RDFXMLparseWarning(qName+" is not a recognized RDF term for a property attribute", position);
- if ( isXMLQName(namespace, localName) )
+ if ( isXMLQName(namespaceURI, localName) )
return false;
- if ( isXMLNamespace(namespace) ) {
+ if ( isXMLNamespace(namespaceURI) ) {
// Unrecognized qnames in the XMLnamespace are a warning and are ignored.
RDFXMLparseWarning("Unrecognized XML attribute: '"+attributes.getQName(index)+"'", position);
return false;
@@ -1010,48 +1015,48 @@ class ParserRDFXML_SAX
/** Output for a property attribute (already checked) */
private void propertyAttribute(Node subject, Attributes attributes, int index, Position position) {
- String namespace = attributes.getURI(index);
+ String namespaceURI = attributes.getURI(index);
String localName = attributes.getLocalName(index);
String value = attributes.getValue(index);
- if ( rdfNS.equals(namespace) ) {
+ if ( rdfNS.equals(namespaceURI) ) {
if ( rdfType.equals(localName) ) {
Node type = iriResolve(value, position);
emit(subject, Nodes.type, type, position);
return;
}
}
- Node property = qNameToIRI(namespace, localName, position);
+ Node property = qNameToIRI(namespaceURI, localName, position);
String lex = value;
Node object = literal(lex, currentLang, position);
emit(subject, property, object, position);
}
+ /**
+ * Generate a new base IRIx.
+ * If this is relative, issue a warning.
+ * It it is an error to use it and the error is generated at the point of use.
+ */
private IRIx xmlBase(Attributes attributes, Position position) {
String baseStr = attributes.getValue(xmlNS, xmlBaseLN);
- return xmlBase(baseStr);
- }
-
- private IRIx xmlBase(String baseStr) {
if ( baseStr == null )
return null;
- if ( currentBase == null )
- return null;
- return currentBase.resolve(baseStr);
- }
-
- private String xmlBaseStr(Attributes attributes, Position position) {
- String baseStr = attributes.getValue(xmlNS, xmlBaseLN);
- if ( baseStr == null )
- return null;
- return IRIs.resolve(currentBase, baseStr);
+ IRIx irix = resolveIRIxAny(baseStr, position);
+ if ( irix.isRelative() )
+ //throw RDFXMLparseError("BANG", position);
+ RDFXMLparseWarning("Relative URI for base: <"+baseStr+">", position);
+ return irix;
}
+ /**
+ * Determine the xml:lang.
+ */
private String xmlLang(Attributes attributes, Position position) {
+ // We use null for "no language" so that explicit
+ // xml:lang="" is different.
String langStr = attributes.getValue(xmlNS, xmlLangLN);
if ( langStr == null )
- return null; // We use null for "no language" so that explicit
- // xml:lang="" is different.
+ return null;
return langStr;
}
@@ -1264,14 +1269,17 @@ class ParserRDFXML_SAX
// ---- Creating terms.
- private Node qNameToIRI(String namespace, String localName, Position position) {
- String uriStr = qNameToIRI(namespace, localName);
+ private Node qNameToIRI(String namespaceURI, String localName, Position position) {
+ if ( StringUtils.isBlank(namespaceURI) ) {
+ RDFXMLparseWarning("Unqualified typed nodes are not allowed: <"+localName+">", position);
+ }
+ String uriStr = qNameToIRI(namespaceURI, localName);
return iri(uriStr, position);
}
- /** This is the RDF rule for creating an IRI from QName. */
- private String qNameToIRI(String namespace, String localName) {
- String iriStr = namespace + localName;
+ /** This is the RDF rule for creating an IRI from a QName. */
+ private String qNameToIRI(String namespaceURI, String localName) {
+ String iriStr = namespaceURI + localName;
return iriStr;
}
@@ -1294,26 +1302,31 @@ class ParserRDFXML_SAX
private Node iriResolve(String uriStr, Position position) {
Objects.requireNonNull(uriStr);
Objects.requireNonNull(position);
- int line = position.line();
- int col = position.column();
- String resolved = resolveIRI(uriStr, position);
- return createURI(resolved, position);
- }
-
- private String resolveIRI(String uriStr, Position position) {
if ( uriStr.startsWith("_:") )
// <_:label> syntax. Handled by the FactoryRDF via the parser profile.
- return uriStr;
- return resolveIRIx(uriStr, position).str();
+ return createURI(uriStr, position);
+ String resolved = resolveIRIx(uriStr, position).str();
+ return createURI(resolved, position);
}
private IRIx resolveIRIx(String uriStr, Position position) {
try {
- if ( currentBase != null )
- return currentBase.resolve(uriStr);
- IRIx iri = IRIx.create(uriStr);
+
+ IRIx iri = resolveIRIxAny(uriStr, position);
if ( iri.isRelative() )
- throw RDFXMLparseError("Base URI is null, but there are relative URIs to resolve" , position);
+ throw RDFXMLparseError("Relative URI encountered: <"+iri.str()+">" , position);
+ return iri;
+ } catch (IRIException ex) {
+ throw RDFXMLparseError(ex.getMessage(), position);
+ }
+ }
+
+ /** String to IRIx, no opinion */
+ private IRIx resolveIRIxAny(String uriStr, Position position) {
+ try {
+ IRIx iri = ( currentBase != null )
+ ? currentBase.resolve(uriStr)
+ : IRIx.create(uriStr);
return iri;
} catch (IRIException ex) {
throw RDFXMLparseError(ex.getMessage(), position);
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx/ReaderRDFXML_SAX.java b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx/ReaderRDFXML_SAX.java
index c5597f0423..b7ff1a3624 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx/ReaderRDFXML_SAX.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx/ReaderRDFXML_SAX.java
@@ -66,7 +66,7 @@ public class ReaderRDFXML_SAX implements ReaderRIOT
}
private void parse(InputSource inputSource, String xmlBase, ContentType ct, StreamRDF destination, Context context) {
- ParserRDFXML_SAX sax2rdf = new ParserRDFXML_SAX(xmlBase, parserProfile, destination, RIOT.getContext().copy());
+ ParserRRX_SAX sax2rdf = new ParserRRX_SAX(xmlBase, parserProfile, destination, RIOT.getContext().copy());
// Configured to avoid XXE
XMLReader xmlReader;
try {
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_ev/ParserRDFXML_StAX_EV.java b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_ev/ParserRRX_StAX_EV.java
similarity index 96%
rename from jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_ev/ParserRDFXML_StAX_EV.java
rename to jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_ev/ParserRRX_StAX_EV.java
index 7ee9b26a88..fd354eb49b 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_ev/ParserRDFXML_StAX_EV.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_ev/ParserRRX_StAX_EV.java
@@ -48,10 +48,9 @@ import org.apache.jena.sparql.graph.NodeConst;
import org.apache.jena.sparql.util.Context;
import org.apache.jena.util.XML11Char;
import org.apache.jena.vocabulary.RDF;
-import org.apache.jena.vocabulary.RDF.Nodes;
/** StAX events */
-class ParserRDFXML_StAX_EV {
+class ParserRRX_StAX_EV {
private static boolean EVENTS = false;
private final IndentedWriter trace;
@@ -147,7 +146,7 @@ class ParserRDFXML_StAX_EV {
/** Integer holder for rdf:li */
private static class Counter { int value = 1; }
- ParserRDFXML_StAX_EV(XMLEventReader reader, String xmlBase, ParserProfile parserProfile, StreamRDF destination, Context context) {
+ ParserRRX_StAX_EV(XMLEventReader reader, String xmlBase, ParserProfile parserProfile, StreamRDF destination, Context context) {
// Debug
IndentedWriter out = IndentedWriter.stdout.clone();
out.setFlushOnNewline(true);
@@ -165,7 +164,7 @@ class ParserRDFXML_StAX_EV {
this.initialXmlLang = "";
if ( xmlBase != null ) {
this.currentBase = IRIx.create(xmlBase);
- parserProfile.setBaseIRI(currentBase.str());
+ //parserProfile.setBaseIRI(currentBase.str());
} else {
this.currentBase = null;
}
@@ -449,7 +448,7 @@ class ParserRDFXML_StAX_EV {
RDFXMLparseWarning(str(qName)+" is not a recognized RDF term for a type", location);
}
- Node object = qNameToURI(qName, location);
+ Node object = qNameToIRI(qName, location);
emit(subject, NodeConst.nodeRDFType, object, location);
}
@@ -490,7 +489,7 @@ class ParserRDFXML_StAX_EV {
emit(subject, RDF.Nodes.type, type, location);
continue;
}
- Node property = qNameToURI(attribute.getName(), location);
+ Node property = qNameToIRI(attribute.getName(), location);
Node object = literal(attribute.getValue(), currentLang, location);
emit(subject, property, object, location);
}
@@ -513,9 +512,8 @@ class ParserRDFXML_StAX_EV {
String namespace = qName.getNamespaceURI();
String localName = qName.getLocalPart();
if ( namespace == null || namespace.isEmpty() ) {
- // SAX passes xmlns as attributes with namespace and local name of "". The qname is "xmlns:"/"xmlns"
- // StAX, does not pass namespaces.
-
+ // Note about XML: The empty string namespace does not apply to XML attributes,
+ // only XML elements. ":attr" is not legal XML.
//RDFXMLparseError("XML attribute '"+localName+"' used for RDF property attribute (no namespace)", event);
if ( outputWarnings )
RDFXMLparseWarning("XML attribute '"+localName+"' used for RDF property attribute - ignored", event);
@@ -591,7 +589,7 @@ class ParserRDFXML_StAX_EV {
if ( qNameMatches(rdfContainerItem, startElt.getName()) )
property = iriDirect(rdfNS+"_"+Integer.toString(listElementCounter.value++), location);
else
- property = qNameToURI(startElt.getName(), location);
+ property = qNameToIRI(startElt.getName(), location);
Node reify = reifyStatement(startElt);
Emitter emitter = (reify==null) ? this::emit : (s,p,o,loc)->emitReify(reify, s, p, o, loc);
@@ -1130,13 +1128,15 @@ class ParserRDFXML_StAX_EV {
parserProfile.setBaseIRI(n.getURI());
}
- private Node qNameToURI(QName qName, Location location) {
- String uriStr = qNameToURI(qName);
+ private Node qNameToIRI(QName qName, Location location) {
+ if ( StringUtils.isBlank(qName.getNamespaceURI()) )
+ RDFXMLparseWarning("Unqualified typed nodes are not allowed: <"+qName.getLocalPart()+">", location);
+ String uriStr = qNameToIRI(qName);
return iriDirect(uriStr, location);
}
- /** RDF rule */
- private String qNameToURI(QName qName) {
+ /** This is the RDF rule for creating an IRI from a QName. */
+ private String qNameToIRI(QName qName) {
return qName.getNamespaceURI()+qName.getLocalPart();
}
@@ -1303,8 +1303,8 @@ class ParserRDFXML_StAX_EV {
}
private boolean processBaseAndLang(StartElement startElt) {
- String xmlBase = attribute(startElt, xmlQNameBase);
- String xmlLang = attribute(startElt, xmlQNameLang);
+ IRIx xmlBase = xmlBase(startElt);
+ String xmlLang = xmlLang(startElt);
if ( ReaderRDFXML_StAX_EV.TRACE ) {
if ( xmlBase != null )
trace.printf("+ BASE <%s>\n", xmlBase);
@@ -1314,11 +1314,8 @@ class ParserRDFXML_StAX_EV {
boolean hasFrame = (xmlBase != null || xmlLang != null);
if ( hasFrame ) {
pushFrame(currentBase, currentLang);
- if ( xmlBase != null ) {
- currentBase = (currentBase != null)
- ? currentBase.resolve(xmlBase)
- : IRIx.create(xmlBase);
- }
+ if ( xmlBase != null )
+ currentBase = xmlBase;
if ( xmlLang != null )
currentLang = xmlLang;
}
@@ -1356,10 +1353,10 @@ class ParserRDFXML_StAX_EV {
private void emitReify(Node reify, Node subject, Node property, Node object, Location location) {
emit(subject, property, object, location);
if ( reify != null ) {
- emit(reify, NodeConst.nodeRDFType, Nodes.Statement, location);
- emit(reify, Nodes.subject, subject, location);
- emit(reify, Nodes.predicate, property, location);
- emit(reify, Nodes.object, object, location);
+ emit(reify, NodeConst.nodeRDFType, RDF.Nodes.Statement, location);
+ emit(reify, RDF.Nodes.subject, subject, location);
+ emit(reify, RDF.Nodes.predicate, property, location);
+ emit(reify, RDF.Nodes.object, object, location);
}
}
@@ -1371,6 +1368,27 @@ class ParserRDFXML_StAX_EV {
destination.prefix(prefix, iriStr);
}
+ /**
+ * Generate a new base IRIx.
+ * If this is relative, issue a warning.
+ * It is an error to use it and the error is generated
+ * sin {@link #resolveIRIx}.
+ */
+ private IRIx xmlBase(StartElement startElt) {
+ String baseStr = attribute(startElt, xmlQNameBase);
+ if ( baseStr == null )
+ return null;
+ Location location = startElt.getLocation();
+ IRIx irix = resolveIRIxNoWarning(baseStr, location);
+ if ( irix.isRelative() )
+ RDFXMLparseWarning("Relative URI for base: <"+baseStr+">", location);
+ return irix;
+ }
+
+ private String xmlLang(StartElement startElt) {
+ return attribute(startElt, xmlQNameLang);
+ }
+
// ---- RDF Terms (Nodes)
private Node iriFromID(String idStr, Location location) {
@@ -1412,11 +1430,20 @@ class ParserRDFXML_StAX_EV {
private IRIx resolveIRIx(String uriStr, Location location) {
// This does not use the parser profile because the base stacks and unstacks in RDF/XML.
try {
- if ( currentBase != null )
- return currentBase.resolve(uriStr);
- IRIx iri = IRIx.create(uriStr);
+ IRIx iri = resolveIRIxNoWarning(uriStr, location);
if ( iri.isRelative() )
- throw RDFXMLparseError("Base URI is null, but there are relative URIs to resolve" , location);
+ throw RDFXMLparseError("Relative URI encountered: <"+iri.str()+">" , location);
+ return iri;
+ } catch (IRIException ex) {
+ throw RDFXMLparseError(ex.getMessage(), location);
+ }
+ }
+
+ private IRIx resolveIRIxNoWarning(String uriStr, Location location) {
+ try {
+ IRIx iri = ( currentBase != null )
+ ? currentBase.resolve(uriStr)
+ : IRIx.create(uriStr);
return iri;
} catch (IRIException ex) {
throw RDFXMLparseError(ex.getMessage(), location);
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_ev/ReaderRDFXML_StAX_EV.java b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_ev/ReaderRDFXML_StAX_EV.java
index 0741ac28de..592545efde 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_ev/ReaderRDFXML_StAX_EV.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_ev/ReaderRDFXML_StAX_EV.java
@@ -78,7 +78,7 @@ public class ReaderRDFXML_StAX_EV implements ReaderRIOT
}
private void parse(XMLEventReader xmlEventReader, String xmlBase, ContentType ct, StreamRDF destination, Context context) {
- ParserRDFXML_StAX_EV parser = new ParserRDFXML_StAX_EV(xmlEventReader, xmlBase, parserProfile, destination, context);
+ ParserRRX_StAX_EV parser = new ParserRRX_StAX_EV(xmlEventReader, xmlBase, parserProfile, destination, context);
destination.start();
try {
parser.parse();
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_sr/ParserRDFXML_StAX_SR.java b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_sr/ParserRRX_StAX_SR.java
similarity index 96%
rename from jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_sr/ParserRDFXML_StAX_SR.java
rename to jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_sr/ParserRRX_StAX_SR.java
index 6c9487fc3c..9ed1672a2d 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_sr/ParserRDFXML_StAX_SR.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_sr/ParserRRX_StAX_SR.java
@@ -29,8 +29,6 @@ import javax.xml.namespace.QName;
import javax.xml.stream.Location;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
-import javax.xml.stream.events.Attribute;
-import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import org.apache.commons.lang3.StringUtils;
@@ -55,7 +53,7 @@ import org.apache.jena.vocabulary.RDF;
import org.apache.jena.vocabulary.RDF.Nodes;
/* StAX - stream reader */
-class ParserRDFXML_StAX_SR {
+class ParserRRX_StAX_SR {
private static boolean EVENTS = false;
private final IndentedWriter trace;
@@ -148,7 +146,7 @@ class ParserRDFXML_StAX_SR {
/** Integer holder for rdf:li */
private static class Counter { int value = 1; }
- ParserRDFXML_StAX_SR(XMLStreamReader reader, String xmlBase, ParserProfile parserProfile, StreamRDF destination, Context context) {
+ ParserRRX_StAX_SR(XMLStreamReader reader, String xmlBase, ParserProfile parserProfile, StreamRDF destination, Context context) {
// Debug
IndentedWriter out = IndentedWriter.stdout.clone();
out.setFlushOnNewline(true);
@@ -430,7 +428,7 @@ class ParserRDFXML_StAX_SR {
RDFXMLparseWarning(str(qName)+" is not a recognized RDF term for a type");
}
- Node object = qNameToURI(qName, location);
+ Node object = qNameToIRI(qName, location);
emit(subject, NodeConst.nodeRDFType, object, location);
}
@@ -464,7 +462,7 @@ class ParserRDFXML_StAX_SR {
emit(subject, RDF.Nodes.type, type, location);
continue;
}
- Node property = qNameToURI(qName, location);
+ Node property = qNameToIRI(qName, location);
String lexicalForm = xmlSource.getAttributeValue(i);
Node object = literal(lexicalForm, currentLang, location);
emit(subject, property, object, location);
@@ -490,8 +488,8 @@ class ParserRDFXML_StAX_SR {
private boolean checkPropertyAttribute(QName qName, boolean outputWarnings) {
String namespace = qName.getNamespaceURI();
if ( namespace == null || namespace.isEmpty() ) {
- // SAX passes xmlns as attributes with namespace and local name of "". The qname is "xmlns:"/"xmlns"
- // StAX, does not pass namespaces.
+ // Note about XML: The empty string namespace does not apply to XML attributes,
+ // only XML elements. ":attr" is not legal XML.
//RDFXMLparseError("XML attribute '"+qName.getLocalPart()+"' used for RDF property attribute (no namespace)", event);
if ( outputWarnings )
RDFXMLparseWarning("XML attribute '"+qName.getLocalPart()+"' used for RDF property attribute - ignored");
@@ -566,7 +564,7 @@ class ParserRDFXML_StAX_SR {
if ( qNameMatches(rdfContainerItem, qName) )
property = iriDirect(rdfNS+"_"+Integer.toString(listElementCounter.value++), location());
else
- property = qNameToURI(qName, location);
+ property = qNameToIRI(qName, location);
Node reify = reifyStatement(location);
Emitter emitter = (reify==null) ? this::emit : (s,p,o,loc)->emitReify(reify, s, p, o, loc);
@@ -1022,7 +1020,7 @@ class ParserRDFXML_StAX_SR {
// subject property INNER
emitter.emit(subject, property, subjectInner, location());
- // Process as a node element, having decied the subject.
+ // Process as a node element, having decided the subject.
nodeElement(subjectInner);
// End property tag.
@@ -1078,13 +1076,6 @@ class ParserRDFXML_StAX_SR {
return blankNode(location);
}
- private String attribute(StartElement startElt, QName attrName) {
- Attribute attr = startElt.getAttributeByName(attrName);
- if ( attr == null )
- return null;
- return attr.getValue();
- }
-
// ---- Nodes
private void setBase(String uriStr, Location location) {
@@ -1092,13 +1083,17 @@ class ParserRDFXML_StAX_SR {
parserProfile.setBaseIRI(n.getURI());
}
- private Node qNameToURI(QName qName, Location location) {
- String uriStr = strQNameToURI(qName);
+ /** This is the RDF rule for creating an IRI from a QName. */
+ private Node qNameToIRI(QName qName, Location location) {
+ if ( StringUtils.isBlank(qName.getNamespaceURI()) )
+ RDFXMLparseWarning("Unqualified typed nodes are not allowed: <"+qName.getLocalPart()+">", location);
+
+ String uriStr = strQNameToIRI(qName);
return iriDirect(uriStr, location);
}
- /** RDF rule */
- private String strQNameToURI(QName qName) {
+ /** This is the RDF rule for creating an IRI from a QName. */
+ private String strQNameToIRI(QName qName) {
return qName.getNamespaceURI()+qName.getLocalPart();
}
@@ -1264,8 +1259,9 @@ class ParserRDFXML_StAX_SR {
}
private boolean processBaseAndLang() {
- String xmlBase = attribute(xmlQNameBase);
- String xmlLang = attribute(xmlQNameLang);
+ IRIx xmlBase = xmlBase();
+ String xmlLang = xmlLang();
+
if ( ReaderRDFXML_StAX_SR.TRACE ) {
if ( xmlBase != null )
trace.printf("+ BASE <%s>\n", xmlBase);
@@ -1275,11 +1271,8 @@ class ParserRDFXML_StAX_SR {
boolean hasFrame = (xmlBase != null || xmlLang != null);
if ( hasFrame ) {
pushFrame(currentBase, currentLang);
- if ( xmlBase != null ) {
- currentBase = (currentBase != null)
- ? currentBase.resolve(xmlBase)
- : IRIx.create(xmlBase);
- }
+ if ( xmlBase != null )
+ currentBase = xmlBase;
if ( xmlLang != null )
currentLang = xmlLang;
}
@@ -1351,6 +1344,27 @@ class ParserRDFXML_StAX_SR {
destination.prefix(prefix, iriStr);
}
+ /**
+ * Generate a new base IRIx.
+ * If this is relative, issue a warning.
+ * It is an error to use it and the error is generated
+ * sin {@link #resolveIRIx}.
+ */
+ private IRIx xmlBase() {
+ String baseStr = attribute(xmlQNameBase);
+ if ( baseStr == null )
+ return null;
+ Location location = location();
+ IRIx irix = resolveIRIxAny(baseStr, location);
+ if ( irix.isRelative() )
+ RDFXMLparseWarning("Relative URI for base: <"+baseStr+">", location);
+ return irix;
+ }
+
+ private String xmlLang() {
+ return attribute(xmlQNameLang);
+ }
+
// ---- RDF Terms (Nodes)
private Node iriFromID(String idStr, Location location) {
@@ -1371,13 +1385,15 @@ class ParserRDFXML_StAX_SR {
return parserProfile.createURI(uriStr, line, col);
}
- /** Create a URI. The IRI is resolved by this operation. */
+ /**
+ * Create a URI. The IRI is resolved by this operation.
+ */
private Node iriResolve(String uriStr, Location location) {
Objects.requireNonNull(uriStr);
Objects.requireNonNull(location);
+ String resolved = resolveIRI(uriStr, location);
int line = location.getLineNumber();
int col = location.getColumnNumber();
- String resolved = resolveIRI(uriStr, location);
return parserProfile.createURI(resolved, line, col);
}
@@ -1392,11 +1408,20 @@ class ParserRDFXML_StAX_SR {
private IRIx resolveIRIx(String uriStr, Location location) {
// This does not use the parser profile because the base stacks and unstacks in RDF/XML.
try {
- if ( currentBase != null )
- return currentBase.resolve(uriStr);
- IRIx iri = IRIx.create(uriStr);
+ IRIx iri = resolveIRIxAny(uriStr, location);
if ( iri.isRelative() )
- throw RDFXMLparseError("Base URI is null, but there are relative URIs to resolve" , location);
+ throw RDFXMLparseError("Relative URI encountered: <"+iri.str()+">" , location);
+ return iri;
+ } catch (IRIException ex) {
+ throw RDFXMLparseError(ex.getMessage(), location);
+ }
+ }
+
+ private IRIx resolveIRIxAny(String uriStr, Location location) {
+ try {
+ IRIx iri = ( currentBase != null )
+ ? currentBase.resolve(uriStr)
+ : IRIx.create(uriStr);
return iri;
} catch (IRIException ex) {
throw RDFXMLparseError(ex.getMessage(), location);
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_sr/ReaderRDFXML_StAX_SR.java b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_sr/ReaderRDFXML_StAX_SR.java
index 272b2cd6a4..ba12f54d37 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_sr/ReaderRDFXML_StAX_SR.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_sr/ReaderRDFXML_StAX_SR.java
@@ -78,7 +78,7 @@ public class ReaderRDFXML_StAX_SR implements ReaderRIOT
}
private void parse(XMLStreamReader xmlStreamReader, String xmlBase, ContentType ct, StreamRDF destination, Context context) {
- ParserRDFXML_StAX_SR parser = new ParserRDFXML_StAX_SR(xmlStreamReader, xmlBase, parserProfile, destination, context);
+ ParserRRX_StAX_SR parser = new ParserRRX_StAX_SR(xmlStreamReader, xmlBase, parserProfile, destination, context);
destination.start();
try {
parser.parse();
diff --git a/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/RunTestRDFXML.java b/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/RunTestRDFXML.java
index f9133a433e..64712ff262 100644
--- a/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/RunTestRDFXML.java
+++ b/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/RunTestRDFXML.java
@@ -71,6 +71,8 @@ public class RunTestRDFXML {
}
}
+ private static String ParserBase = "http://external/base#";
+
/**
* Manifest-like in that the test files are run in a specific order.
* The local files cover all the features of RDF/XML parsing
@@ -463,7 +465,7 @@ public class RunTestRDFXML {
Graph graph = GraphFactory.createDefaultGraph();
StreamRDF dest = StreamRDFLib.graph(graph);
try ( InputStream in = IO.openFile(filename) ) {
- reader.read(in, "http://external/base", WebContent.ctRDFXML, dest, RIOT.getContext().copy());
+ reader.read(in, ParserBase, WebContent.ctRDFXML, dest, RIOT.getContext().copy());
} catch (IOException ex) {
throw IOX.exception(ex);
}
diff --git a/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/TestRRX.java b/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/TestRRX.java
index e88689c058..cd4e4a8c24 100644
--- a/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/TestRRX.java
+++ b/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/TestRRX.java
@@ -25,15 +25,10 @@ import java.util.List;
import org.apache.jena.atlas.io.IO;
import org.apache.jena.atlas.io.IOX;
-import org.apache.jena.graph.Graph;
import org.apache.jena.riot.*;
import org.apache.jena.riot.lang.rdfxml.RRX;
import org.apache.jena.riot.lang.rdfxml.rrx.RunTestRDFXML.ErrorHandlerCollector;
-import org.apache.jena.riot.system.ParserProfile;
-import org.apache.jena.riot.system.RiotLib;
-import org.apache.jena.riot.system.StreamRDF;
-import org.apache.jena.riot.system.StreamRDFLib;
-import org.apache.jena.sparql.graph.GraphFactory;
+import org.apache.jena.riot.system.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@@ -72,15 +67,15 @@ public class TestRRX {
// Test2 for more than one object in RDF/XML striping.
@Test public void error_multiple_objects_lex_node() {
- errorTest("multiple_objects_lex_node.rdf");
+ checkForError("multiple_objects_lex_node.rdf");
}
@Test public void error_multiple_objects_node_lex() {
- errorTest("multiple_objects_node_lex.rdf");
+ checkForError("multiple_objects_node_lex.rdf");
}
@Test public void error_multiple_objects_node_node() {
- errorTest("multiple_objects_node_node.rdf");
+ checkForError("multiple_objects_node_node.rdf");
}
// Check that the "one object" parse state does not impact deeper structures.
@@ -91,7 +86,7 @@ public class TestRRX {
// rdf:parserType=
@Test public void error_parseType_unknown() {
// This is only a warning in ARP.
- errorTest("parseType-unknown.rdf", false);
+ checkForError("parseType-unknown.rdf", false);
}
@Test public void warn_parseType_extension_1() {
@@ -102,28 +97,39 @@ public class TestRRX {
}
// misc
- @Test public void no_base_not_requiredBase01() {
+ @Test public void base_not_needed() {
// Call with no base; no base needed.
- noBase("file-no-base.rdf");
+ noBase("base-none.rdf");
}
@Test(expected=RiotException.class)
- public void noBase_but_needed() {
+ public void bare_needed() {
// Call with no base; a base is needed => exception.
- noBase("file-external-base.rdf");
+ noBase("base-external-needed.rdf");
+ }
+
+ @Test(expected=RiotException.class)
+ public void base_inner_1() {
+ // Call with no base; xml:base is relative in the data.
+ noBase("base-inner.rdf");
+ }
+
+ public void base_inner_2() {
+ // Called external base
+ goodTest("base-inner.rdf");
}
// CIM
@Test public void cim_statements01() {
// parseType="Statements"
- // because ARP behaved that way.
- //errorTest("error02.rdf");
+ // This is an extension to support CIM XML data.
+ // ARP behaved this way.
// Warning issued.
warningTest("cim_statements01.rdf", 2);
}
@Test public void rdfResourceBad() {
- errorTest("rdf-resource-node.rdf");
+ checkForError("rdf-resource-node.rdf");
}
/** Parse with no base set by the parser */
@@ -133,10 +139,13 @@ public class TestRRX {
ErrorHandlerCollector errorHandler = new ErrorHandlerCollector();
ParserProfile parserProfile = RiotLib.createParserProfile(RiotLib.factoryRDF(), errorHandler, true);
ReaderRIOT reader = factory.create(lang, parserProfile);
- Graph graph = GraphFactory.createDefaultGraph();
- StreamRDF dest = StreamRDFLib.graph(graph);
+ StreamRDF dest = false
+ ? StreamRDFWriter.getWriterStream(System.out, RDFFormat.TURTLE_FLAT)
+ : StreamRDFWriter.getWriterStream(System.out, RDFFormat.RDFNULL);
try ( InputStream in = IO.openFile(fn) ) {
reader.read(in, null/* No base*/, WebContent.ctRDFXML, dest, RIOT.getContext().copy());
+ } catch (RiotException ex) {
+ throw ex;
} catch (IOException ex) {
throw IOX.exception(ex);
}
@@ -156,13 +165,18 @@ public class TestRRX {
RunTestRDFXML.runTestCompareARP(fn, factory, label, fn);
}
- // Run test, expecting an error.
- // This is checked by error handler.
- private void errorTest(String filename) {
- errorTest(filename, true);
+ /**
+ * Run test, expecting an error.
+ */
+ private void checkForError(String filename) {
+ checkForError(filename, true);
}
- private void errorTest(String filename, boolean compare) {
+ /**
+ * Run test, expecting an error. If the second argument is true, also Compare to
+ * make sure it is the same as ARP.
+ */
+ private void checkForError(String filename, boolean compare) {
ReaderRIOTFactory factory = RDFParserRegistry.getFactory(lang);
String fn = DIR+filename;
RunTestRDFXML.runTestExpectFailure(filename, factory, label, fn);
diff --git a/jena-arq/testing/RIOT/rrx-files/file-external-base.rdf b/jena-arq/testing/RIOT/rrx-files/base-external-needed.rdf
similarity index 100%
copy from jena-arq/testing/RIOT/rrx-files/file-external-base.rdf
copy to jena-arq/testing/RIOT/rrx-files/base-external-needed.rdf
diff --git a/jena-arq/testing/RIOT/rrx-files/file-external-base.rdf b/jena-arq/testing/RIOT/rrx-files/base-inner.rdf
similarity index 74%
rename from jena-arq/testing/RIOT/rrx-files/file-external-base.rdf
rename to jena-arq/testing/RIOT/rrx-files/base-inner.rdf
index e13b8cbd50..682248a36f 100644
--- a/jena-arq/testing/RIOT/rrx-files/file-external-base.rdf
+++ b/jena-arq/testing/RIOT/rrx-files/base-inner.rdf
@@ -6,8 +6,7 @@
xmlns="http://local/"
xmlns:ex="http://example/" >
- <rdf:Description rdf:about="relative">
- <ex:property1>ABC</ex:property1>
+ <rdf:Description rdf:about="inner" xml:base="relative">
+ <ex:property>ABC</ex:property>
</rdf:Description>
-
</rdf:RDF>
diff --git a/jena-arq/testing/RIOT/rrx-files/file-no-base.rdf b/jena-arq/testing/RIOT/rrx-files/base-none.rdf
similarity index 100%
rename from jena-arq/testing/RIOT/rrx-files/file-no-base.rdf
rename to jena-arq/testing/RIOT/rrx-files/base-none.rdf
diff --git a/jena-core/src/main/java/org/apache/jena/irix/IRIx.java b/jena-core/src/main/java/org/apache/jena/irix/IRIx.java
index 7b2c5377f1..36ae005e38 100644
--- a/jena-core/src/main/java/org/apache/jena/irix/IRIx.java
+++ b/jena-core/src/main/java/org/apache/jena/irix/IRIx.java
@@ -185,7 +185,6 @@ public abstract class IRIx {
*/
public abstract IRIx normalize();
-
/**
* Return (if possible), an IRI that is relative to the base argument.
* If this IRI is a relative path, this is returned unchanged.
(jena) 02/05: RRX: Check for bad use of rdf:resource
Posted by an...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
andy pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/jena.git
commit e5a71d6956d5b40a3fee44cd52c154aeae000029
Author: Andy Seaborne <an...@apache.org>
AuthorDate: Fri Aug 9 19:27:30 2024 +0100
RRX: Check for bad use of rdf:resource
---
.../riot/lang/rdfxml/rrx/ParserRDFXML_SAX.java | 7 ++++-
.../rdfxml/rrx_stax_ev/ParserRDFXML_StAX_EV.java | 5 ++++
.../rdfxml/rrx_stax_sr/ParserRDFXML_StAX_SR.java | 6 +++++
.../jena/riot/lang/rdfxml/rrx/RunTestRDFXML.java | 4 ++-
.../apache/jena/riot/lang/rdfxml/rrx/TestRRX.java | 31 +++++++++++++---------
.../testing/RIOT/rrx-files/file-external-base.rdf | 3 +++
jena-arq/testing/RIOT/rrx-files/file-no-base.rdf | 3 +++
.../rrx-files/{warn01.rdf => parseType-warn.rdf} | 0
.../testing/RIOT/rrx-files/rdf-resource-node.rdf | 14 ++++++++++
9 files changed, 59 insertions(+), 14 deletions(-)
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx/ParserRDFXML_SAX.java b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx/ParserRDFXML_SAX.java
index e34ccc4329..c52d843727 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx/ParserRDFXML_SAX.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx/ParserRDFXML_SAX.java
@@ -665,7 +665,12 @@ class ParserRDFXML_SAX
* ++ nodeElementURIs anyURI - ( coreSyntaxTerms | rdf:li | oldTerms ) */
private void startNodeElement(String namespaceURI, String localName, String qName, Attributes attributes, Position position) {
- // Top level object - maybe inside rdf:RDF
+ // Subject - maybe inside rdf:RDF
+
+ String rdfResourceStr = attributes.getValue(rdfNS, rdfResource);
+ if ( rdfResourceStr != null )
+ throw RDFXMLparseError("rdf:resource not allowed as attribute here: "+qName, position);
+
Node thisSubject = attributesToSubjectNode(attributes, position);
startNodeElementWithSubject(thisSubject, namespaceURI, localName, qName, attributes, position);
}
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_ev/ParserRDFXML_StAX_EV.java b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_ev/ParserRDFXML_StAX_EV.java
index d64a501028..7ee9b26a88 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_ev/ParserRDFXML_StAX_EV.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_ev/ParserRDFXML_StAX_EV.java
@@ -419,6 +419,11 @@ class ParserRDFXML_StAX_EV {
if ( ! allowedNodeElementURIs(startElt.getName()) )
throw RDFXMLparseError("Not allowed as a node element tag: '"+str(startElt.getName())+"'", startElt);
+ // rdf:resource not allowed on a node element
+ Attribute attrRdfResource= startElt.getAttributeByName(rdfResource);
+ if ( attrRdfResource != null )
+ throw RDFXMLparseError("rdf:resource not allowed as attribute here: "+str(startElt.getName()), startElt);
+
incIndent();
boolean hasFrame = startElement(startElt);
if ( subject == null )
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_sr/ParserRDFXML_StAX_SR.java b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_sr/ParserRDFXML_StAX_SR.java
index 65bc006b25..6c9487fc3c 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_sr/ParserRDFXML_StAX_SR.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_sr/ParserRDFXML_StAX_SR.java
@@ -399,6 +399,12 @@ class ParserRDFXML_StAX_SR {
if ( ! allowedNodeElementURIs(qName) )
throw RDFXMLparseError("Not allowed as a node element tag: '"+str(qName)+"'");
+
+ // rdf:resource not allowed on a node element
+ String rdfResourceStr = attribute(rdfResource);
+ if ( rdfResourceStr != null )
+ throw RDFXMLparseError("rdf:resource not allowed as attribute here: "+str(qName));
+
incIndent();
boolean hasFrame = startElement();
if ( subject == null )
diff --git a/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/RunTestRDFXML.java b/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/RunTestRDFXML.java
index da6648a05b..f9133a433e 100644
--- a/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/RunTestRDFXML.java
+++ b/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/RunTestRDFXML.java
@@ -95,7 +95,9 @@ public class RunTestRDFXML {
// For a better order ...
List<String> testfiles = List.of(
- "xml.rdf", "xml10.rdf", "xml11.rdf",
+ "xml.rdf",
+ "xml10.rdf",
+ "xml11.rdf",
"basic01.rdf",
"basic02.rdf",
diff --git a/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/TestRRX.java b/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/TestRRX.java
index 8c16eec860..e88689c058 100644
--- a/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/TestRRX.java
+++ b/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/TestRRX.java
@@ -98,7 +98,19 @@ public class TestRRX {
// Now valid. parseType="literal" -> parseType="Literal"
// because ARP behaved that way.
// Warning issued.
- warningTest("warn01.rdf", 1);
+ warningTest("parseType-warn.rdf", 1);
+ }
+
+ // misc
+ @Test public void no_base_not_requiredBase01() {
+ // Call with no base; no base needed.
+ noBase("file-no-base.rdf");
+ }
+
+ @Test(expected=RiotException.class)
+ public void noBase_but_needed() {
+ // Call with no base; a base is needed => exception.
+ noBase("file-external-base.rdf");
}
// CIM
@@ -110,18 +122,11 @@ public class TestRRX {
warningTest("cim_statements01.rdf", 2);
}
- // misc
- @Test public void noBase01() {
- // Call with no base; no base needed.
- noBase("file-no-base.rdf");
- }
-
- @Test(expected=RiotException.class)
- public void noBase02() {
- // Call with no base; a base is needed => exception.
- noBase("file-external-base.rdf");
+ @Test public void rdfResourceBad() {
+ errorTest("rdf-resource-node.rdf");
}
+ /** Parse with no base set by the parser */
private void noBase(String filename) {
ReaderRIOTFactory factory = RDFParserRegistry.getFactory(lang);
String fn = DIR+filename;
@@ -131,7 +136,7 @@ public class TestRRX {
Graph graph = GraphFactory.createDefaultGraph();
StreamRDF dest = StreamRDFLib.graph(graph);
try ( InputStream in = IO.openFile(fn) ) {
- reader.read(in, null, WebContent.ctRDFXML, dest, RIOT.getContext().copy());
+ reader.read(in, null/* No base*/, WebContent.ctRDFXML, dest, RIOT.getContext().copy());
} catch (IOException ex) {
throw IOX.exception(ex);
}
@@ -151,6 +156,8 @@ public class TestRRX {
RunTestRDFXML.runTestCompareARP(fn, factory, label, fn);
}
+ // Run test, expecting an error.
+ // This is checked by error handler.
private void errorTest(String filename) {
errorTest(filename, true);
}
diff --git a/jena-arq/testing/RIOT/rrx-files/file-external-base.rdf b/jena-arq/testing/RIOT/rrx-files/file-external-base.rdf
index 40509769ef..e13b8cbd50 100644
--- a/jena-arq/testing/RIOT/rrx-files/file-external-base.rdf
+++ b/jena-arq/testing/RIOT/rrx-files/file-external-base.rdf
@@ -1,3 +1,6 @@
+<?xml version='1.0'?>
+<!-- Licensed under the terms of https://www.apache.org/licenses/LICENSE-2.0 -->
+
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns="http://local/"
diff --git a/jena-arq/testing/RIOT/rrx-files/file-no-base.rdf b/jena-arq/testing/RIOT/rrx-files/file-no-base.rdf
index b49b5a3fd0..789ef6dbfb 100644
--- a/jena-arq/testing/RIOT/rrx-files/file-no-base.rdf
+++ b/jena-arq/testing/RIOT/rrx-files/file-no-base.rdf
@@ -1,3 +1,6 @@
+<?xml version='1.0'?>
+<!-- Licensed under the terms of https://www.apache.org/licenses/LICENSE-2.0 -->
+
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns="http://local/"
diff --git a/jena-arq/testing/RIOT/rrx-files/warn01.rdf b/jena-arq/testing/RIOT/rrx-files/parseType-warn.rdf
similarity index 100%
rename from jena-arq/testing/RIOT/rrx-files/warn01.rdf
rename to jena-arq/testing/RIOT/rrx-files/parseType-warn.rdf
diff --git a/jena-arq/testing/RIOT/rrx-files/rdf-resource-node.rdf b/jena-arq/testing/RIOT/rrx-files/rdf-resource-node.rdf
new file mode 100644
index 0000000000..ee3bbab131
--- /dev/null
+++ b/jena-arq/testing/RIOT/rrx-files/rdf-resource-node.rdf
@@ -0,0 +1,14 @@
+<?xml version='1.0'?>
+<!-- Licensed under the terms of https://www.apache.org/licenses/LICENSE-2.0 -->
+
+<!-- Out of place rdf:resource -->
+<rdf:RDF
+ xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+ xmlns="http://local/"
+ xmlns:ex="http://example/" >
+
+ <!-- rdf:resource on rdf:Description -->
+ <rdf:Description rdf:about="http://example/s" rdf:resource="http://example/foo">
+ <ex:property>text</ex:property>
+ </rdf:Description>
+</rdf:RDF>
\ No newline at end of file
(jena) 01/05: RRX: Fix xml:base handling
Posted by an...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
andy pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/jena.git
commit 4ea2c55285ba688d87853f38dc67c957c28ff028
Author: Andy Seaborne <an...@apache.org>
AuthorDate: Fri Aug 9 18:46:13 2024 +0100
RRX: Fix xml:base handling
---
.../riot/lang/rdfxml/rrx_stax_ev/ParserRDFXML_StAX_EV.java | 11 +++++++++--
.../riot/lang/rdfxml/rrx_stax_sr/ParserRDFXML_StAX_SR.java | 7 +++++--
2 files changed, 14 insertions(+), 4 deletions(-)
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_ev/ParserRDFXML_StAX_EV.java b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_ev/ParserRDFXML_StAX_EV.java
index cbbaac3b34..d64a501028 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_ev/ParserRDFXML_StAX_EV.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_ev/ParserRDFXML_StAX_EV.java
@@ -1293,7 +1293,11 @@ class ParserRDFXML_StAX_EV {
*/
private boolean startElement(StartElement startElt) {
processNamespaces(startElt);
+ boolean hasFrame = processBaseAndLang(startElt);
+ return hasFrame;
+ }
+ private boolean processBaseAndLang(StartElement startElt) {
String xmlBase = attribute(startElt, xmlQNameBase);
String xmlLang = attribute(startElt, xmlQNameLang);
if ( ReaderRDFXML_StAX_EV.TRACE ) {
@@ -1305,8 +1309,11 @@ class ParserRDFXML_StAX_EV {
boolean hasFrame = (xmlBase != null || xmlLang != null);
if ( hasFrame ) {
pushFrame(currentBase, currentLang);
- if ( xmlBase != null )
- currentBase = currentBase.resolve(xmlBase);
+ if ( xmlBase != null ) {
+ currentBase = (currentBase != null)
+ ? currentBase.resolve(xmlBase)
+ : IRIx.create(xmlBase);
+ }
if ( xmlLang != null )
currentLang = xmlLang;
}
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_sr/ParserRDFXML_StAX_SR.java b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_sr/ParserRDFXML_StAX_SR.java
index 32b1024056..65bc006b25 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_sr/ParserRDFXML_StAX_SR.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_sr/ParserRDFXML_StAX_SR.java
@@ -1269,8 +1269,11 @@ class ParserRDFXML_StAX_SR {
boolean hasFrame = (xmlBase != null || xmlLang != null);
if ( hasFrame ) {
pushFrame(currentBase, currentLang);
- if ( xmlBase != null )
- currentBase = currentBase.resolve(xmlBase);
+ if ( xmlBase != null ) {
+ currentBase = (currentBase != null)
+ ? currentBase.resolve(xmlBase)
+ : IRIx.create(xmlBase);
+ }
if ( xmlLang != null )
currentLang = xmlLang;
}
(jena) 05/05: GH-2630: Handle unqualified attributes
Posted by an...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
andy pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/jena.git
commit 452816cf97d8dba621d1c25b4b0032a24c0b9c7b
Author: Andy Seaborne <an...@apache.org>
AuthorDate: Sun Aug 11 12:31:32 2024 +0100
GH-2630: Handle unqualified attributes
---
.../apache/jena/riot/lang/rdfxml/package-info.java | 9 +-
.../jena/riot/lang/rdfxml/rrx/ParserRRX_SAX.java | 274 ++++++++++++---------
.../lang/rdfxml/rrx_stax_ev/ParserRRX_StAX_EV.java | 245 +++++++++++-------
.../lang/rdfxml/rrx_stax_sr/ParserRRX_StAX_SR.java | 235 +++++++++++-------
.../jena/riot/lang/rdfxml/rrx/RunTestRDFXML.java | 6 +-
.../apache/jena/riot/lang/rdfxml/rrx/TestRRX.java | 22 +-
.../testing/RIOT/rrx-files/bad-rdf-id-node.rdf | 2 +-
.../RIOT/rrx-files/bad-rdf-resource-node.rdf | 2 +-
.../RIOT/rrx-files/bad-unqualified-attribute1.rdf | 2 +-
.../RIOT/rrx-files/bad-unqualified-attribute2.rdf | 2 +-
...tribute2.rdf => bad-unqualified-attribute3.rdf} | 6 +-
...tribute2.rdf => bad-unqualified-attribute4.rdf} | 6 +-
...tribute2.rdf => bad-unqualified-attribute5.rdf} | 6 +-
13 files changed, 495 insertions(+), 322 deletions(-)
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/package-info.java b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/package-info.java
index 1fad47ab40..348641f1be 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/package-info.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/package-info.java
@@ -22,16 +22,19 @@
* that is the RIOT reader interface implementation and a {@code RDFXMLParser-???}
* class which is the parser engine.
* <ul>
- * <li>SAX based {@linkplain org.apache.jena.riot.lang.rdfxml.rrx.ParserRDFXML_SAX ParserRDFXML_SAX}. This is the default parser for Jena5 onwards for
+ * <li>SAX based {@linkplain org.apache.jena.riot.lang.rdfxml.rrx.ParserRRX_SAX ParserRRX_SAX}. This is the default parser for Jena5 onwards for
* RDF/XML in RIOT, i.e it is the registration for {@linkplain org.apache.jena.riot.Lang#RDFXML Lang.RDFXML} and it is the fastest.
* <li>A StAX-based parser using {@link javax.xml.stream.XMLStreamReader XMLStreamReader}.
* <li>A StAX-based parser using {@link javax.xml.stream.XMLEventReader XMLEventReader}.
* </ul>
- * In addition Jena (in jena-core) has the original ARP parser in package {@code org.apach.jena-rdfxml.rdfxml0} referred to as "ARP0".
- * This was the RDF/XML parser up to Jena 4.6.1. ARP1 uses package jena-iri directly to handle IRIs.
+ * In addition, Jena (in jena-core) has the original ARP parser.
+ * <p>
+ * "ARP0" in package {@code org.apach.jena-rdfxml.rdfxml0}
+ * This was the RDF/XML parser up to Jena 4.6.1. It handled IRIs directly using jena-iri.
* <p>
* "ARP1" is in package {@code org.apach.jena-rdfxml.rdfxml1} is derived from ARP0.
* It uses the {@link org.apache.jena.irix.IRIx IRIx} abstraction to handle IRIs.
+ * It has error reporting aligned with RIOT.
* <br/>
* </p>
* <table border=1 style="border-width: 2px ; border-style: solid; border-collapse: collapse">
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx/ParserRRX_SAX.java b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx/ParserRRX_SAX.java
index 9f34f9cd2e..6d3e768ef5 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx/ParserRRX_SAX.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx/ParserRRX_SAX.java
@@ -108,6 +108,7 @@ class ParserRRX_SAX
private static Set<String> $coreSyntaxTerms =
Set.of(rdfRDF, rdfID, rdfAbout, rdfParseType, rdfResource, rdfNodeID, rdfDatatype);
+ // Not used.
// 6.2.3 Production syntaxTerms
// coreSyntaxTerms | rdf:Description | rdf:li
private static Set<String> $syntaxTerms =
@@ -118,9 +119,16 @@ class ParserRRX_SAX
// rdf:aboutEach | rdf:aboutEachPrefix | rdf:bagID
private static Set<String> $oldTerms = Set.of(rdfAboutEach, rdfAboutEachPrefix, rdfBagID);
- private static Set<String> disallowedPropertyAttributeTerms =
- Set.of(rdfRDF, rdfID, rdfAbout, rdfParseType, rdfResource, rdfNodeID, rdfDatatype,
- rdfDescription, rdfContainerItem, "aboutEach", "aboutEachPrefix", "bagID");
+ // 6.1.4 Attribute Event
+ // The (old form) qualified named allowed where property attributes expected.
+ // Parses "MAY" warn about their use
+ private static Set<String> $allowedUnqualified = Set.of(rdfAbout, rdfID, rdfResource, rdfParseType, rdfType);
+
+ private boolean coreSyntaxTerm(String namespace, String localName) {
+ if ( ! rdfNS.equals(namespace) )
+ return false;
+ return $coreSyntaxTerms.contains(localName);
+ }
// 6.2.5 Production nodeElementURIs
// anyURI - ( coreSyntaxTerms | rdf:li | oldTerms )
@@ -166,11 +174,20 @@ class ParserRRX_SAX
return true;
}
+ private static boolean allowedUnqualifiedTerm(String localName) {
+ return $allowedUnqualified.contains(localName);
+ }
+
+ /** The attributes that guide the RDF/XML parser. */
+ // Production: CodeSyntaxterms
+ // rdf:RDF | rdf:ID | rdf:about | rdf:parseType | rdf:resource | rdf:nodeID | rdf:datatype
+
/** The attributes that guide the RDF/XML parser. */
private static Set<String> $rdfSyntaxAttributes =
Set.of(rdfRDF, rdfAbout, rdfNodeID, rdfID, rdfParseType, rdfDatatype, rdfResource);
- private boolean isSyntaxAttribute(String namespace, String localName) {
- if ( ! rdfNS.equals(namespace) )
+
+ private static boolean isSyntaxAttribute(String namespace, String localName) {
+ if ( ! isXMLNamespace(namespace) )
return false;
return $rdfSyntaxAttributes.contains(localName);
}
@@ -250,6 +267,13 @@ class ParserRRX_SAX
ObjectNode
}
+ /** Mark the usage of a QName */
+ private enum QNameUsage {
+ TypedNodeElement("typed node element"), PropertyElement("property element");
+ final String msg;
+ private QNameUsage(String msg) { this.msg = msg; }
+ }
+
/** Integer holder for rdf:li */
private static class Counter { int value = 1; }
@@ -424,8 +448,6 @@ class ParserRRX_SAX
this.parserMode = parserMode;
}
-// // Forming objects.
-// private ParseType parseType = null;
ParserRRX_SAX(String xmlBase, ParserProfile parserProfile, StreamRDF destination, Context context) {
// Debug
if ( ReaderRDFXML_SAX.TRACE ) {
@@ -696,12 +718,12 @@ class ParserRRX_SAX
if ( isNotRecognizedRDFtype(namespaceURI, localName) )
RDFXMLparseWarning(qName+" is not a recognized RDF term for a type", position);
}
- Node object = qNameToIRI(namespaceURI, localName, position, "typed node element");
+ Node object = qNameToIRI(namespaceURI, localName, QNameUsage.TypedNodeElement, position);
emit(currentSubject, RDF.Nodes.type, object, position);
}
- if ( hasPropertyAttributes(attributes, position) )
- processPropertyAttributes(currentSubject, attributes, position);
+ processPropertyAttributes(currentSubject, qName, attributes, false, position);
+
parserMode(ParserMode.PropertyElement);
}
@@ -726,7 +748,7 @@ class ParserRRX_SAX
currentProperty = iri(p, position);
} else {
// The empty string namespace does not apply to XML attributes.
- currentProperty = qNameToIRI(namespaceURI, localName, position, "property element");
+ currentProperty = qNameToIRI(namespaceURI, localName, QNameUsage.PropertyElement, position);
}
if ( ReaderRDFXML_SAX.TRACE )
@@ -763,14 +785,9 @@ class ParserRRX_SAX
if ( objBlankNodeLabel != null )
resourceObj = blankNode(objBlankNodeLabel, position);
- if ( hasPropertyAttributes(attributes, position) ) {
- if ( parseTypeStr != null ) {
- // rdf:parseType found.
- throw RDFXMLparseError("The attribute rdf:parseType is not permitted with property attributes on a property element: "+qName, position);
- }
+ Node innerSubject = processPropertyAttributes(resourceObj, qName, attributes, true, position);
+ if (resourceObj == null && innerSubject != null ) {
// AND must be empty tag
- Node innerSubject = (resourceObj==null) ? blankNode(position) : resourceObj;
- processPropertyAttributes(innerSubject, attributes, position);
currentEmitter.emit(currentSubject, currentProperty, innerSubject, position);
return;
}
@@ -828,34 +845,27 @@ class ParserRRX_SAX
return currentProperty == null;
}
-// private String xmlBaseStr(Attributes attributes, Position position) {
-// String baseStr = attributes.getValue(xmlNS, xmlBaseLN);
-// if ( baseStr == null )
-// return null;
-// return IRIs.resolve(currentBase, baseStr);
-// }
-
- // Start element encountered when expecting a ObjectCollection
- private void startCollectionItem(String namespaceURI, String localName, String qName, Attributes attributes, Position position) {
- // Finish last list cell, start new one.
- if ( ReaderRDFXML_SAX.TRACE )
- trace.println("Generate list cell");
- // Preceding cell in list.
- Node previousCollectionNode = collectionNode.node;
- Node thisCollectionNode = blankNode(position);
- // New cell in list.
- // Either link up to the origin or fixup previous cell.
- if ( previousCollectionNode == null )
- currentEmitter.emit(currentSubject, currentProperty, thisCollectionNode, position);
- else
- emit(previousCollectionNode, Nodes.rest, thisCollectionNode, position);
- collectionNode.node = thisCollectionNode;
+ // Start element encountered when expecting a ObjectCollection
+ private void startCollectionItem(String namespaceURI, String localName, String qName, Attributes attributes, Position position) {
+ // Finish last list cell, start new one.
+ if ( ReaderRDFXML_SAX.TRACE )
+ trace.println("Generate list cell");
+ // Preceding cell in list.
+ Node previousCollectionNode = collectionNode.node;
+ Node thisCollectionNode = blankNode(position);
+ // New cell in list.
+ // Either link up to the origin or fixup previous cell.
+ if ( previousCollectionNode == null )
+ currentEmitter.emit(currentSubject, currentProperty, thisCollectionNode, position);
+ else
+ emit(previousCollectionNode, Nodes.rest, thisCollectionNode, position);
+ collectionNode.node = thisCollectionNode;
- // Start the item.
- Node itemSubject = attributesToSubjectNode(attributes, position);
- emit(thisCollectionNode, RDF.Nodes.first, itemSubject, position);
- startNodeElementWithSubject(itemSubject, namespaceURI, localName, qName, attributes, position);
- }
+ // Start the item.
+ Node itemSubject = attributesToSubjectNode(attributes, position);
+ emit(thisCollectionNode, RDF.Nodes.first, itemSubject, position);
+ startNodeElementWithSubject(itemSubject, namespaceURI, localName, qName, attributes, position);
+ }
private void endCollectionItem(Position position) {
if ( ReaderRDFXML_SAX.TRACE )
@@ -952,86 +962,122 @@ class ParserRRX_SAX
currentLang = xmlLang;
}
- // Property attributes.
- // The checking is done by the call to hasPropertyAttributes.
- private void processPropertyAttributes(Node subject, Attributes attributes, Position position) {
- for ( int i = 0 ; i < attributes.getLength() ; i++ ) {
- boolean isPropertyAttribute = checkPropertyAttribute(attributes, i, false, position);
- if ( ! isPropertyAttribute )
- continue;
- propertyAttribute(subject, attributes, i, position);
+ // Process property attributes - return null for nothing to do.
+ private Node processPropertyAttributes(Node resourceObj, String qName, Attributes attributes, boolean isPropertyElement, Position position) {
+ // Subject may not yet be decided.
+ List<Integer> indexes = gatherPropertyAttributes(attributes, position);
+ if ( indexes.isEmpty() )
+ return null;
+ if ( isPropertyElement ) {
+ String parseTypeStr = attributes.getValue(rdfNS, rdfParseType);
+ if ( parseTypeStr != null ) {
+ // rdf:parseType found.
+ throw RDFXMLparseError("The attribute rdf:parseType is not permitted with property attributes on a property element: "+qName, position);
+ }
}
+
+ Node innerSubject = (resourceObj==null) ? blankNode(position) : resourceObj;
+ outputPropertyAttributes(innerSubject, indexes, attributes, position);
+ return innerSubject;
}
- // Early abort! But also used to avoid creating a Node for
- // processPropertyAttributes which has no work to do.
- private boolean hasPropertyAttributes(Attributes attributes, Position position) {
- for ( int i = 0 ; i < attributes.getLength() ; i++ ) {
- boolean isPropertyAttribute = checkPropertyAttribute(attributes, i, true, position);
- if ( ! isPropertyAttribute )
- continue;
- return true;
+ private List<Integer> gatherPropertyAttributes(Attributes attributes, Position position) {
+ if ( attributes.getLength() == 0 )
+ return List.of();
+ // Indexes
+ List<Integer> attributeIdx = new ArrayList<>(attributes.getLength());
+ for ( int idx = 0 ; idx < attributes.getLength() ; idx++ ) {
+ boolean isPropertyAttribute = checkPropertyAttribute(attributes, idx, position);
+ if ( isPropertyAttribute )
+ attributeIdx.add(idx);
+ }
+ return attributeIdx;
+ }
+
+ private void outputPropertyAttributes(Node subject, List<Integer> indexes, Attributes attributes, Position position) {
+ for ( int index : indexes ) {
+ String namespaceURI = attributes.getURI(index);
+ String localName = attributes.getLocalName(index);
+ String value = attributes.getValue(index);
+
+ if ( rdfNS.equals(namespaceURI) ) {
+ if ( rdfType.equals(localName) ) {
+ Node type = iriResolve(value, position);
+ emit(subject, Nodes.type, type, position);
+ return;
+ }
+ }
+ Node property = attributeToIRI(namespaceURI, localName, position);
+ String lex = value;
+ Node object = literal(lex, currentLang, position);
+ emit(subject, property, object, position);
}
- return false;
}
- /** Return true if this is a property attribute. */
- private boolean checkPropertyAttribute(Attributes attributes, int index, boolean outputWarnings, Position position) {
+ // 6.1.2 Element Event - attributes
+ /** Return true if this is an acceptable property attribute. */
+ private boolean checkPropertyAttribute(Attributes attributes, int index, Position position) {
String namespaceURI = attributes.getURI(index);
String localName = attributes.getLocalName(index);
String qName = attributes.getQName(index);
- if ( StringUtils.isBlank(namespaceURI) ) {
- // Note about XML: The empty string namespace does not apply to XML attributes,
- // only XML elements. ":attr" is not legal XML.
- //RDFXMLparseError("XML attribute '"+qName+"' used for RDF property attribute (no namespace)", position);
-
- if ( outputWarnings ){
- if ( ! localName.isEmpty() ) // Skip XML namespace declarations.
- RDFXMLparseWarning("XML attribute '"+qName+"' used for RDF property attribute - ignored", position);
- }
- return false;
- }
if ( isSyntaxAttribute(namespaceURI, localName) )
return false;
+ // The default namespace (i.e. prefix "") does not apply to attributes.
+
+ // 6.1.2 Element Event - attributes
+
+ if ( coreSyntaxTerm(namespaceURI, localName) )
+ return false;
+
if ( ! allowedPropertyAttributeURIs(namespaceURI, localName) )
- throw RDFXMLparseError("Not allowed as a property attribute: '"+attributes.getQName(index)+"'", position);
+ throw RDFXMLparseError("Not allowed as a property attribute '"+attributes.getQName(index)+"'", position);
- if ( outputWarnings && isNotRecognizedRDFproperty(namespaceURI, localName) )
+ if ( isNotRecognizedRDFproperty(namespaceURI, localName) )
RDFXMLparseWarning(qName+" is not a recognized RDF term for a property attribute", position);
+ // xml:lang, xml:base, xml:space (if these get here).
if ( isXMLQName(namespaceURI, localName) )
return false;
+ // 6.1.2 Element Event
+ // "All remaining reserved XML Names (see Name in XML 1.0) are now removed from the set."
if ( isXMLNamespace(namespaceURI) ) {
// Unrecognized qnames in the XMLnamespace are a warning and are ignored.
- RDFXMLparseWarning("Unrecognized XML attribute: '"+attributes.getQName(index)+"'", position);
+ RDFXMLparseWarning("Unrecognized XML attribute '"+qName+"' - ignored", position);
return false;
}
- if ( isXMLNamespaceQName(qName) )
+ if ( isXMLNamespaceQName(qName) ) {
+ // xmlns
return false;
+ }
+
+ if ( StringUtils.isBlank(namespaceURI) ) {
+ boolean valid = checkPropertyAttributeUnqualifiedTerm(localName, position);
+ return valid;
+ }
return true;
}
- /** Output for a property attribute (already checked) */
- private void propertyAttribute(Node subject, Attributes attributes, int index, Position position) {
- String namespaceURI = attributes.getURI(index);
- String localName = attributes.getLocalName(index);
- String value = attributes.getValue(index);
-
- if ( rdfNS.equals(namespaceURI) ) {
- if ( rdfType.equals(localName) ) {
- Node type = iriResolve(value, position);
- emit(subject, Nodes.type, type, position);
- return;
+ private boolean checkPropertyAttributeUnqualifiedTerm(String localName, Position position) {
+ if ( allowedUnqualifiedTerm(localName) )
+ return true;
+ if ( localName.length() >= 3 ) {
+ String chars3 = localName.substring(0, 3);
+ if ( chars3.equalsIgnoreCase("xml") ) {
+ // 6.1.2 Element Event
+ // "all attribute information items with [prefix] property having no value and which
+ // have [local name] beginning with xml (case independent comparison)"
+ // Test: unrecognised-xml-attributes/test002.rdf
+ RDFXMLparseWarning("Unrecognized XML non-namespaced attribute '"+localName+"' - ignored", position);
+ return false;
}
}
- Node property = qNameToIRI(namespaceURI, localName, position, "property attribute");
- String lex = value;
- Node object = literal(lex, currentLang, position);
- emit(subject, property, object, position);
+ // 6.1.4 Attribute Event
+ // "Other non-namespaced ·local-name· accessor values are forbidden."
+ throw RDFXMLparseError("Non-namespaced attribute not allowed as a property attribute: '"+localName+"'", position);
}
/**
@@ -1095,13 +1141,6 @@ class ParserRRX_SAX
return (s, p, o, loc) -> emitReify(reify, s, p, o, loc);
}
-// private String xmlBaseStr(Attributes attributes, Position position) {
-// String baseStr = attributes.getValue(xmlNS, xmlBaseLN);
-// if ( baseStr == null )
-// return null;
-// return IRIs.resolve(currentBase, baseStr);
-// }
-
private Node generateLiteral(Position position) {
String lex = accCharacters.toString();
if ( datatype != null )
@@ -1271,19 +1310,33 @@ class ParserRRX_SAX
// ---- Creating terms.
- private Node qNameToIRI(String namespaceURI, String localName, Position position, String usage) {
+ private Node qNameToIRI(String namespaceURI, String localName, QNameUsage usage, Position position) {
if ( StringUtils.isBlank(namespaceURI) )
- throw RDFXMLparseError("Unqualified "+usage+" not allowed: <"+localName+">", position);
- String uriStr = qNameToIRI(namespaceURI, localName);
+ // Default namespace, "" not defined.
+ throw RDFXMLparseError("Unqualified "+usage.msg+" not allowed: <"+localName+">", position);
+ String uriStr = strQNameToIRI(namespaceURI, localName);
return iri(uriStr, position);
}
/** This is the RDF rule for creating an IRI from a QName. */
- private String qNameToIRI(String namespaceURI, String localName) {
+ private String strQNameToIRI(String namespaceURI, String localName) {
String iriStr = namespaceURI + localName;
return iriStr;
}
+ private Node attributeToIRI(String namespaceURI, String localName, Position position) {
+ String ns = namespaceURI;
+ if ( StringUtils.isBlank(namespaceURI) ) {
+ if ( allowedUnqualifiedTerm(localName) )
+ ns = rdfNS;
+ else
+ // else rejected in checkPropertyAttribute
+ throw RDFXMLparseError("Unqualified property attribute not allowed: '"+localName+"'", position);
+ }
+ String uriStr = strQNameToIRI(namespaceURI, localName);
+ return iri(uriStr, position);
+ }
+
private Node iri(String uriStr, Position position) {
Objects.requireNonNull(uriStr);
Objects.requireNonNull(position);
@@ -1342,17 +1395,6 @@ class ParserRRX_SAX
return parserProfile.createURI(iriStr, line, col);
}
-// private Node iriDirect(String uriStr, Position position) {
-// Objects.requireNonNull(uriStr);
-// // No checking.
-// return createURIdirect(uriStr, position);
-// }
-//
-// /** Always done without checking. */
-// private Node createURIdirect(String uriStr, Position position) {
-// return factory.createURI(uriStr);
-// }
-
private Node blankNode(Position position) {
Objects.requireNonNull(position);
int line = position.line();
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_ev/ParserRRX_StAX_EV.java b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_ev/ParserRRX_StAX_EV.java
index 84e97ead58..f0706cdcd4 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_ev/ParserRRX_StAX_EV.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_ev/ParserRRX_StAX_EV.java
@@ -91,6 +91,13 @@ class ParserRRX_StAX_EV {
currentLang = frame.lang;
}
+ /** Mark the usage of a QName */
+ private enum QNameUsage {
+ TypedNodeElement("typed node element"), PropertyElement("property element");
+ final String msg;
+ private QNameUsage(String msg) { this.msg = msg; }
+ }
+
// ---- Error handlers
private RiotException RDFXMLparseError(String message, XMLEvent event) {
@@ -212,19 +219,25 @@ class ParserRRX_StAX_EV {
private static Set<QName> $coreSyntaxTerms =
Set.of(rdfRDF, rdfID, rdfAbout, rdfParseType, rdfResource, rdfNodeID, rdfDatatype);
- // 6.2.3 Production syntaxTerms
- // coreSyntaxTerms | rdf:Description | rdf:li
- private static Set<QName> $syntaxTerms =
- Set.of(rdfRDF, rdfID, rdfAbout, rdfParseType, rdfResource, rdfNodeID, rdfDatatype,
- rdfDescription, rdfContainerItem);
+// // 6.2.3 Production syntaxTerms
+// // coreSyntaxTerms | rdf:Description | rdf:li
+// private static Set<QName> $syntaxTerms =
+// Set.of(rdfRDF, rdfID, rdfAbout, rdfParseType, rdfResource, rdfNodeID, rdfDatatype,
+// rdfDescription, rdfContainerItem);
// 6.2.4 Production oldTerms
// rdf:aboutEach | rdf:aboutEachPrefix | rdf:bagID
private static Set<QName> $oldTerms = Set.of(rdfAboutEach, rdfAboutEachPrefix, rdfBagID);
- private static Set<QName> disallowedPropertyAttributeTerms =
- Set.of(rdfRDF, rdfID, rdfAbout, rdfParseType, rdfResource, rdfNodeID, rdfDatatype,
- rdfDescription, rdfContainerItem, rdfAboutEach, rdfAboutEachPrefix, rdfBagID);
+ private static Set<String> $allowedUnqualified =
+ Set.of(rdfAbout.getLocalPart(), rdfID.getLocalPart(), rdfResource.getLocalPart(),
+ rdfParseType.getLocalPart(), rdfType.getLocalPart());
+
+ private boolean coreSyntaxTerm(QName qName) {
+ if ( ! rdfNS.equals(qName.getNamespaceURI()) )
+ return false;
+ return $coreSyntaxTerms.contains(qName);
+ }
// 6.2.5 Production nodeElementURIs
// anyURI - ( coreSyntaxTerms | rdf:li | oldTerms )
@@ -270,6 +283,10 @@ class ParserRRX_StAX_EV {
return true;
}
+ private static boolean allowedUnqualifiedTerm(String localName) {
+ return $allowedUnqualified.contains(localName);
+ }
+
/** The attributes that guide the RDF/XML parser. */
private static Set<QName> $rdfSyntaxAttributes =
Set.of(rdfRDF, rdfAbout, rdfNodeID, rdfID, rdfParseType, rdfDatatype, rdfResource);
@@ -374,8 +391,6 @@ class ParserRRX_StAX_EV {
// ---- Node Loop - sequence of zero or more RDF/XML node elements.
// generic move over multiple elements
while ( event != null ) {
-// if ( VERBOSE )
-// out.println("-- node loop: "+str(event));
if ( !event.isStartElement() )
break;
StartElement startElt = event.asStartElement();
@@ -387,8 +402,6 @@ class ParserRRX_StAX_EV {
/** Top level single node element. (no <rdf:RDF>, </rdf:RDF>) */
private XMLEvent nodeElementSingle(XMLEvent event) {
-// if ( VERBOSE )
-// out.println("-- single node element: "+str(event));
if ( !event.isStartElement() )
// Protective "not recognized"
return event;
@@ -448,15 +461,14 @@ class ParserRRX_StAX_EV {
RDFXMLparseWarning(str(qName)+" is not a recognized RDF term for a type", location);
}
- Node object = qNameToIRI(qName, location, "typed node element");
+ Node object = qNameToIRI(qName, QNameUsage.TypedNodeElement, location);
emit(subject, NodeConst.nodeRDFType, object, location);
}
// Other attributes are properties.
// rdf:type is special.
- if ( hasAttributeProperties(startElt) )
- processPropertyAttributes(subject, startElt, location);
+ processPropertyAttributes(subject, startElt, true, location);
// Finished with the node start tag.
XMLEvent event = nextEventTag();
@@ -471,79 +483,11 @@ class ParserRRX_StAX_EV {
return event.asEndElement();
}
- // Property attributes.
- // The checking is done by the call to hasPropertyAttributes.
- private void processPropertyAttributes(Node subject, StartElement startElt, Location location) {
- // This both nodes and properties. There is an implicit bnode in when used with properties.
- // attributes don't support location :: attribute.getLocation();
- Iterator<Attribute> x = startElt.getAttributes();
- while (x.hasNext()) {
- Attribute attribute = x.next();
- boolean isPropertyAttribute = checkPropertyAttribute(attribute.getName(), startElt, false);
- if ( ! isPropertyAttribute )
- continue;
- QName qName = attribute.getName();
- if ( rdfType.equals(qName) ) {
- String iriStr = attribute.getValue();
- Node type = iriResolve(iriStr, location);
- emit(subject, RDF.Nodes.type, type, location);
- continue;
- }
- Node property = qNameToIRI(attribute.getName(), location, "property attribute");
- Node object = literal(attribute.getValue(), currentLang, location);
- emit(subject, property, object, location);
- }
- }
-
- private boolean hasAttributeProperties(StartElement startElt) {
- Iterator<Attribute> x = startElt.getAttributes();
- while (x.hasNext()) {
- Attribute attribute = x.next();
- boolean isPropertyAttribute = checkPropertyAttribute(attribute.getName(), startElt, true);
- if ( ! isPropertyAttribute )
- continue;
- return true;
- }
- return false;
- }
-
- /** Return true if this is a property attribute. */
- private boolean checkPropertyAttribute(QName qName, XMLEvent event, boolean outputWarnings) {
- String namespace = qName.getNamespaceURI();
- String localName = qName.getLocalPart();
- if ( namespace == null || namespace.isEmpty() ) {
- //RDFXMLparseError("XML attribute '"+localName+"' used for RDF property attribute (no namespace)", event);
- if ( outputWarnings )
- RDFXMLparseWarning("XML attribute '"+localName+"' used for RDF property attribute - ignored", event);
- return false;
- }
- if ( isSyntaxAttribute(qName) )
- return false;
-
- if ( ! allowedPropertyAttributeURIs(qName) )
- throw RDFXMLparseError("Not allowed as a property attribute: '"+str(qName)+"'", event);
-
- if ( outputWarnings && isNotRecognizedRDFproperty(qName) )
- RDFXMLparseWarning(str(qName)+" is not a recognized RDF term for a property attribute", event);
-
- if ( isXMLQName(qName) )
- return false;
-
- if ( isXMLNamespace(qName) ) {
- // Unrecognized qnames in the XMLnamespace are a warning and are ignored.
- RDFXMLparseWarning("Unrecognized XML attribute: '"+str(qName)+"'", event);
- return false;
- }
- return true;
- }
-
// ---- Property elements
private XMLEvent propertyElementLoop(Node subject, XMLEvent event) {
Counter listElementCounter = new Counter();
while (event != null) {
-// if ( VERBOSE )
-// out.println("-- property loop: "+str(event));
if ( ! event.isStartElement() )
break;
propertyElement(subject, event.asStartElement(), listElementCounter);
@@ -587,7 +531,7 @@ class ParserRRX_StAX_EV {
if ( qNameMatches(rdfContainerItem, startElt.getName()) )
property = iriDirect(rdfNS+"_"+Integer.toString(listElementCounter.value++), location);
else
- property = qNameToIRI(startElt.getName(), location, "property element");
+ property = qNameToIRI(startElt.getName(), QNameUsage.PropertyElement, location);
Node reify = reifyStatement(startElt);
Emitter emitter = (reify==null) ? this::emit : (s,p,o,loc)->emitReify(reify, s, p, o, loc);
@@ -617,13 +561,8 @@ class ParserRRX_StAX_EV {
if ( objBlanklNodeLabel != null )
resourceObj = blankNode(objBlanklNodeLabel, location);
- if ( hasAttributeProperties(startElt) ) {
- if ( parseType != parseTypePlain ) {
- // rdf:parseType found.
- throw RDFXMLparseError("The attribute rdf:parseType is not permitted with property attributes on a property element: "+startElt.getName(), startElt);
- }
- Node innerSubject = (resourceObj==null) ? blankNode(location) : resourceObj;
- processPropertyAttributes(innerSubject, startElt, location);
+ Node innerSubject = processPropertyAttributes(resourceObj, startElt, true, location);
+ if ( resourceObj == null && innerSubject != null ) {
emitter.emit(subject, property, innerSubject, location);
XMLEvent event = nextEventAny();
if ( !event.isEndElement() )
@@ -731,6 +670,108 @@ class ParserRRX_StAX_EV {
return event;
}
+ private Node processPropertyAttributes(Node resourceObj, StartElement startElt, boolean isPropertyElement, Location location) {
+ // Subject may not yet be decided.
+ List<Attribute> propertyAttributes = gatherPropertyAttributes(startElt, location);
+ if ( propertyAttributes.isEmpty() )
+ return null;
+ if ( isPropertyElement ) {
+ String parseTypeStr = objectParseType(startElt);
+ if ( parseTypeStr != parseTypePlain ) {
+ // rdf:parseType found.
+ throw RDFXMLparseError("The attribute rdf:parseType is not permitted with property attributes on a property element: "+str(startElt.getName()), location);
+ }
+ }
+
+ Node innerSubject = (resourceObj==null) ? blankNode(location) : resourceObj;
+ outputPropertyAttributes(innerSubject, propertyAttributes, location);
+ return innerSubject;
+ }
+
+ private List<Attribute> gatherPropertyAttributes(StartElement startElt, Location location) {
+ Iterator<Attribute> x = startElt.getAttributes();
+ if ( ! x.hasNext() )
+ return List.of();
+ List<Attribute> propertyAttributes = new ArrayList<>();
+ while(x.hasNext()) {
+ Attribute attribute = x.next();
+ boolean isPropertyAttribute = checkPropertyAttribute(attribute.getName(), location);
+ if ( isPropertyAttribute )
+ propertyAttributes.add(attribute);
+ }
+ return propertyAttributes;
+ }
+
+ private void outputPropertyAttributes(Node subject, List<Attribute> propertyAttributes, Location location) {
+ for ( Attribute attribute : propertyAttributes ) {
+ QName qName = attribute.getName();
+ if ( rdfType.equals(qName) ) {
+ String iriStr = attribute.getValue();
+ Node type = iriResolve(iriStr, location);
+ emit(subject, RDF.Nodes.type, type, location);
+ return;
+ }
+ Node property = attributeToIRI(qName, location);
+ String lexicalForm = attribute.getValue();
+ Node object = literal(lexicalForm, currentLang, location);
+ emit(subject, property, object, location);
+ }
+ }
+
+ /** Return true if this is a property attribute. */
+ private boolean checkPropertyAttribute(QName qName, Location location) {
+ if ( isSyntaxAttribute(qName) )
+ return false;
+
+ // The default namespace (i.e. prefix "") does not apply to attributes.
+
+ // 6.1.2 Element Event - attributes
+
+ if (coreSyntaxTerm(qName) )
+ return false;
+
+ if ( ! allowedPropertyAttributeURIs(qName) )
+ throw RDFXMLparseError("Not allowed as a property attribute: '"+str(qName)+"'", location);
+
+ if ( isNotRecognizedRDFproperty(qName) )
+ RDFXMLparseWarning(str(qName)+" is not a recognized RDF term for a property attribute", location);
+
+ if ( isXMLQName(qName) )
+ return false;
+
+ if ( isXMLNamespace(qName) ) {
+ // Unrecognized qnames in the XMLnamespace are a warning and are ignored.
+ RDFXMLparseWarning("Unrecognized XML attribute: '"+str(qName)+"'", location);
+ return false;
+ }
+
+ if ( StringUtils.isBlank(qName.getNamespaceURI()) ) {
+ String localName = qName.getLocalPart();
+ boolean valid = checkPropertyAttributeUnqualifiedTerm(localName, location);
+ return valid;
+ }
+ return true;
+ }
+
+ private boolean checkPropertyAttributeUnqualifiedTerm(String localName, Location location) {
+ if ( allowedUnqualifiedTerm(localName) )
+ return true;
+ if ( localName.length() >= 3 ) {
+ String chars3 = localName.substring(0, 3);
+ if ( chars3.equalsIgnoreCase("xml") ) {
+ // 6.1.2 Element Event
+ // "all attribute information items with [prefix] property having no value and which
+ // have [local name] beginning with xml (case independent comparison)"
+ // Test: unrecognised-xml-attributes/test002.rdf
+ RDFXMLparseWarning("Unrecognized XML non-namespaced attribute '"+localName+"' - ignored", location);
+ return false;
+ }
+ }
+ // 6.1.4 Attribute Event
+ // "Other non-namespaced ·local-name· accessor values are forbidden."
+ throw RDFXMLparseError("Non-namespaced attribute not allowed as a property attribute: '"+localName+"'", location);
+ }
+
private String objectParseType(StartElement startElt) {
String parseTypeStr = attribute(startElt, rdfParseType);
return ( parseTypeStr != null ) ? parseTypeStr : parseTypePlain;
@@ -1126,18 +1167,32 @@ class ParserRRX_StAX_EV {
parserProfile.setBaseIRI(n.getURI());
}
- private Node qNameToIRI(QName qName, Location location, String usage) {
+ private Node qNameToIRI(QName qName, QNameUsage usage, Location location) {
if ( StringUtils.isBlank(qName.getNamespaceURI()) )
- throw RDFXMLparseError("Unqualified "+usage+" not allowed: <"+qName.getLocalPart()+">", location);
- String uriStr = qNameToIRI(qName);
+ throw RDFXMLparseError("Unqualified "+usage.msg+" not allowed: <"+qName.getLocalPart()+">", location);
+ String uriStr = strQNameToIRI(qName);
return iriDirect(uriStr, location);
}
/** This is the RDF rule for creating an IRI from a QName. */
- private String qNameToIRI(QName qName) {
+ private String strQNameToIRI(QName qName) {
return qName.getNamespaceURI()+qName.getLocalPart();
}
+ private Node attributeToIRI(QName qName, Location location) {
+ String namespaceURI = qName.getNamespaceURI();
+ String localName = qName.getLocalPart();
+ if ( StringUtils.isBlank(namespaceURI) ) {
+ if ( allowedUnqualifiedTerm(localName) )
+ namespaceURI = rdfNS;
+ else
+ // else rejected in checkPropertyAttribute
+ throw RDFXMLparseError("Unqualified property attribute not allowed: '"+localName+"'", location);
+ }
+ String uriStr = namespaceURI+localName;
+ return iriDirect(uriStr, location);
+ }
+
// ---- Reading XMLEvents
/** XML parsing error. */
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_sr/ParserRRX_StAX_SR.java b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_sr/ParserRRX_StAX_SR.java
index a8eaa8402e..d6df70f8af 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_sr/ParserRRX_StAX_SR.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/lang/rdfxml/rrx_stax_sr/ParserRRX_StAX_SR.java
@@ -94,6 +94,13 @@ class ParserRRX_StAX_SR {
currentLang = frame.lang;
}
+ /** Mark the usage of a QName */
+ private enum QNameUsage {
+ TypedNodeElement("typed node element"), PropertyElement("property element");
+ final String msg;
+ private QNameUsage(String msg) { this.msg = msg; }
+ }
+
// ---- Error handlers
private RiotException RDFXMLparseError(String message) {
@@ -222,9 +229,16 @@ class ParserRRX_StAX_SR {
// rdf:aboutEach | rdf:aboutEachPrefix | rdf:bagID
private static Set<QName> $oldTerms = Set.of(rdfAboutEach, rdfAboutEachPrefix, rdfBagID);
- private static Set<QName> disallowedPropertyAttributeTerms =
- Set.of(rdfRDF, rdfID, rdfAbout, rdfParseType, rdfResource, rdfNodeID, rdfDatatype,
- rdfDescription, rdfContainerItem, rdfAboutEach, rdfAboutEachPrefix, rdfBagID);
+ private static Set<String> $allowedUnqualified =
+ Set.of(rdfAbout.getLocalPart(), rdfID.getLocalPart(), rdfResource.getLocalPart(),
+ rdfParseType.getLocalPart(), rdfType.getLocalPart());
+
+ private boolean coreSyntaxTerm(QName qName) {
+ if ( ! rdfNS.equals(qName.getNamespaceURI()) )
+ return false;
+ return $coreSyntaxTerms.contains(qName);
+ }
+
// 6.2.5 Production nodeElementURIs
// anyURI - ( coreSyntaxTerms | rdf:li | oldTerms )
@@ -270,6 +284,10 @@ class ParserRRX_StAX_SR {
return true;
}
+ private static boolean allowedUnqualifiedTerm(String localName) {
+ return $allowedUnqualified.contains(localName);
+ }
+
/** The attributes that guide the RDF/XML parser. */
private static Set<QName> $rdfSyntaxAttributes =
Set.of(rdfRDF, rdfAbout, rdfNodeID, rdfID, rdfParseType, rdfDatatype, rdfResource);
@@ -368,8 +386,6 @@ class ParserRRX_StAX_SR {
/** Top level single node element. (no <rdf:RDF>, </rdf:RDF>) */
private void nodeElementSingle(int eventType) {
-// if ( VERBOSE )
-// out.println("-- single node element: "+str(event));
if ( ! lookingAt(eventType, START_ELEMENT) )
return ;
nodeElement();
@@ -428,15 +444,11 @@ class ParserRRX_StAX_SR {
RDFXMLparseWarning(str(qName)+" is not a recognized RDF term for a type");
}
- Node object = qNameToIRI(qName, location, "type node element");
+ Node object = qNameToIRI(qName, QNameUsage.TypedNodeElement, location);
emit(subject, NodeConst.nodeRDFType, object, location);
}
- // Other attributes are properties.
- // rdf:type is special.
-
- if ( hasAttributeProperties() )
- processPropertyAttributes(subject, location);
+ processPropertyAttributes(subject, qName, false, location);
// Finished with the node start tag.
int event = nextEventTag();
@@ -446,80 +458,11 @@ class ParserRRX_StAX_SR {
throw RDFXMLparseError("Expected end element for "+qName());
}
- // Property attributes.
- // The checking is done by the call to hasPropertyAttributes.
- private void processPropertyAttributes(Node subject, Location location) {
- // This both nodes and properties. There is an implicit bnode in when used with properties.
- int N = xmlSource.getAttributeCount();
- for ( int i = 0 ; i < N ; i++ ) {
- QName qName = xmlSource.getAttributeName(i);
- boolean isPropertyAttribute = checkPropertyAttribute(qName, false);
- if ( ! isPropertyAttribute )
- continue;
- if ( rdfType.equals(qName) ) {
- String iriStr = xmlSource.getAttributeValue(i);
- Node type = iriResolve(iriStr, location);
- emit(subject, RDF.Nodes.type, type, location);
- continue;
- }
- Node property = qNameToIRI(qName, location, "property attribute");
- String lexicalForm = xmlSource.getAttributeValue(i);
- Node object = literal(lexicalForm, currentLang, location);
- emit(subject, property, object, location);
- }
- }
-
- private boolean hasAttributeProperties() {
- int N = xmlSource.getAttributeCount();
- for ( int i = 0 ; i < N ; i++ ) {
- QName qName = xmlSource.getAttributeName(i);
- boolean isPropertyAttribute = checkPropertyAttribute(qName, true);
- if ( ! isPropertyAttribute )
- continue;
- return true;
- }
- return false;
- }
-
- /**
- * Return true if this is a property attribute.
- * @param qName
- */
- private boolean checkPropertyAttribute(QName qName, boolean outputWarnings) {
- if ( StringUtils.isBlank(qName.getNamespaceURI()) ) {
- //RDFXMLparseError("XML attribute '"+localName+"' used for RDF property attribute (no namespace)", event);
- if ( outputWarnings )
- RDFXMLparseWarning("XML attribute '"+qName.getLocalPart()+"' used for RDF property attribute - ignored");
- return false;
- }
-
- if ( isSyntaxAttribute(qName) )
- return false;
-
- if ( ! allowedPropertyAttributeURIs(qName) )
- throw RDFXMLparseError("Not allowed as a property attribute: '"+str(qName)+"'");
-
- if ( outputWarnings && isNotRecognizedRDFproperty(qName) )
- RDFXMLparseWarning(str(qName)+" is not a recognized RDF term for a property attribute");
-
- if ( isXMLQName(qName) )
- return false;
-
- if ( isXMLNamespace(qName) ) {
- // Unrecognized qnames in the XMLnamespace are a warning and are ignored.
- RDFXMLparseWarning("Unrecognized XML attribute: '"+str(qName)+"'");
- return false;
- }
- return true;
- }
-
// ---- Property elements
private int propertyElementlLoop(Node subject, int event) {
Counter listElementCounter = new Counter();
while (true) {
-// if ( VERBOSE )
-// out.println("-- property loop: "+str(event));
if ( ! lookingAt(event, START_ELEMENT) )
break;
propertyElement(subject, listElementCounter, location());
@@ -562,7 +505,7 @@ class ParserRRX_StAX_SR {
if ( qNameMatches(rdfContainerItem, qName) )
property = iriDirect(rdfNS+"_"+Integer.toString(listElementCounter.value++), location());
else
- property = qNameToIRI(qName, location, "property element");
+ property = qNameToIRI(qName, QNameUsage.PropertyElement, location);
Node reify = reifyStatement(location);
Emitter emitter = (reify==null) ? this::emit : (s,p,o,loc)->emitReify(reify, s, p, o, loc);
@@ -592,13 +535,8 @@ class ParserRRX_StAX_SR {
if ( objBlanklNodeLabel != null )
resourceObj = blankNode(objBlanklNodeLabel, location);
- if ( hasAttributeProperties() ) {
- if ( parseType != parseTypePlain ) {
- // rdf:parseType found.
- throw RDFXMLparseError("The attribute rdf:parseType is not permitted with property attributes on a property element: "+str(qName));
- }
- Node innerSubject = (resourceObj==null) ? blankNode(location) : resourceObj;
- processPropertyAttributes(innerSubject, location);
+ Node innerSubject = processPropertyAttributes(resourceObj, qName, true, location);
+ if ( resourceObj == null && innerSubject != null ) {
emitter.emit(subject, property, innerSubject, location);
int event = nextEventAny();
if ( ! lookingAt(event, END_ELEMENT) )
@@ -704,6 +642,109 @@ class ParserRRX_StAX_SR {
return event;
}
+ private Node processPropertyAttributes(Node resourceObj, QName qName, boolean isPropertyElement, Location location) {
+ // Subject may not yet be decided.
+ List<Integer> indexes = gatherPropertyAttributes(location);
+ if ( indexes.isEmpty() )
+ return null;
+ if ( isPropertyElement ) {
+ String parseTypeStr = objectParseType();
+ if ( parseTypeStr != parseTypePlain ) {
+ // rdf:parseType found.
+ throw RDFXMLparseError("The attribute rdf:parseType is not permitted with property attributes on a property element: "+str(qName), location);
+ }
+ }
+
+ Node innerSubject = (resourceObj==null) ? blankNode(location) : resourceObj;
+ outputPropertyAttributes(innerSubject, indexes, location);
+ return innerSubject;
+ }
+
+ private List<Integer> gatherPropertyAttributes(Location location) {
+ int N = xmlSource.getAttributeCount();
+ if ( N == 0 )
+ return List.of();
+ List<Integer> attributeIdx = new ArrayList<>(N);
+ for ( int idx = 0 ; idx < N ; idx++ ) {
+ QName qName = xmlSource.getAttributeName(idx);
+ boolean isPropertyAttribute = checkPropertyAttribute(qName, location);
+ if ( isPropertyAttribute )
+ attributeIdx.add(idx);
+ }
+ return attributeIdx;
+ }
+
+ private void outputPropertyAttributes(Node subject, List<Integer> indexes, Location location) {
+ for ( int index : indexes ) {
+ QName qName = xmlSource.getAttributeName(index);
+ if ( rdfType.equals(qName) ) {
+ String iriStr = xmlSource.getAttributeValue(index);
+ Node type = iriResolve(iriStr, location);
+ emit(subject, RDF.Nodes.type, type, location);
+ return;
+ }
+ Node property = attributeToIRI(qName, location);
+ String lexicalForm = xmlSource.getAttributeValue(index);
+ Node object = literal(lexicalForm, currentLang, location);
+ emit(subject, property, object, location);
+ }
+ }
+
+ /** Return true if this is a property attribute. */
+ private boolean checkPropertyAttribute(QName qName, Location location) {
+ if ( isSyntaxAttribute(qName) )
+ return false;
+
+ // The default namespace (i.e. prefix "") does not apply to attributes.
+
+ // 6.1.2 Element Event - attributes
+
+ if (coreSyntaxTerm(qName) )
+ return false;
+
+ if ( ! allowedPropertyAttributeURIs(qName) )
+ throw RDFXMLparseError("Not allowed as a property attribute: '"+str(qName)+"'");
+
+ if ( isNotRecognizedRDFproperty(qName) )
+ RDFXMLparseWarning(str(qName)+" is not a recognized RDF term for a property attribute");
+
+ if ( isXMLQName(qName) )
+ return false;
+
+ if ( isXMLNamespace(qName) ) {
+ // Unrecognized qnames in the XMLnamespace are a warning and are ignored.
+ RDFXMLparseWarning("Unrecognized XML attribute: '"+str(qName)+"'");
+ return false;
+ }
+
+ if ( StringUtils.isBlank(qName.getNamespaceURI()) ) {
+ String localName = qName.getLocalPart();
+ boolean valid = checkPropertyAttributeUnqualifiedTerm(localName, location);
+ return valid;
+ }
+ return true;
+ }
+
+ private boolean checkPropertyAttributeUnqualifiedTerm(String localName, Location location) {
+ if ( allowedUnqualifiedTerm(localName) )
+ return true;
+ if ( localName.length() >= 3 ) {
+ String chars3 = localName.substring(0, 3);
+ if ( chars3.equalsIgnoreCase("xml") ) {
+ // 6.1.2 Element Event
+ // "all attribute information items with [prefix] property having no value and which
+ // have [local name] beginning with xml (case independent comparison)"
+ // Test: unrecognised-xml-attributes/test002.rdf
+ RDFXMLparseWarning("Unrecognized XML non-namespaced attribute '"+localName+"' - ignored", location);
+ return false;
+ }
+ }
+ // 6.1.4 Attribute Event
+ // "Other non-namespaced ·local-name· accessor values are forbidden."
+ throw RDFXMLparseError("Non-namespaced attribute not allowed as a property attribute: '"+localName+"'", location);
+ }
+
+
private String objectParseType() {
String parseTypeStr = attribute(rdfParseType);
return ( parseTypeStr != null ) ? parseTypeStr : parseTypePlain;
@@ -1082,9 +1123,9 @@ class ParserRRX_StAX_SR {
}
/** This is the RDF rule for creating an IRI from a QName. */
- private Node qNameToIRI(QName qName, Location location, String usage) {
+ private Node qNameToIRI(QName qName, QNameUsage usage, Location location) {
if ( StringUtils.isBlank(qName.getNamespaceURI()) )
- throw RDFXMLparseError("Unqualified "+usage+" not allowed: <"+qName.getLocalPart()+">", location);
+ throw RDFXMLparseError("Unqualified "+usage.msg+" not allowed: <"+qName.getLocalPart()+">", location);
String uriStr = strQNameToIRI(qName);
return iriDirect(uriStr, location);
}
@@ -1094,6 +1135,20 @@ class ParserRRX_StAX_SR {
return qName.getNamespaceURI()+qName.getLocalPart();
}
+ private Node attributeToIRI(QName qName, Location location) {
+ String namespaceURI = qName.getNamespaceURI();
+ String localName = qName.getLocalPart();
+ if ( StringUtils.isBlank(namespaceURI) ) {
+ if ( allowedUnqualifiedTerm(localName) )
+ namespaceURI = rdfNS;
+ else
+ // else rejected in checkPropertyAttribute
+ throw RDFXMLparseError("Unqualified property attribute not allowed: '"+localName+"'", location);
+ }
+ String uriStr = namespaceURI+localName;
+ return iriDirect(uriStr, location);
+ }
+
// ---- Reading XML
/** Get the string value for an attribute by QName */
diff --git a/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/RunTestRDFXML.java b/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/RunTestRDFXML.java
index 2f12d64e41..a66196e947 100644
--- a/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/RunTestRDFXML.java
+++ b/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/RunTestRDFXML.java
@@ -372,8 +372,8 @@ public class RunTestRDFXML {
checkErrorHandler(testLabel, expectedErrorHandler, errorHandlerTest);
return;
} catch(RiotException ex) {
- output.println("## "+testLabel);
- ex.printStackTrace();
+// output.println("## "+testLabel);
+// ex.printStackTrace();
fail("Unexpected parse error: "+ex.getMessage());
}
}
@@ -407,7 +407,7 @@ public class RunTestRDFXML {
ErrorHandlerCollector actualErrorHandler = new ErrorHandlerCollector();
assertThrows(RiotException.class, ()->{
parseFile(testSubjectFactory, actualErrorHandler, filename);
- output.printf("## Expected RiotExpection : %-4s : %s : %s", subjectLabel, testLabel, filename);
+ output.printf("## Expected RiotExpection : %-4s : %s : %s\n", subjectLabel, testLabel, filename);
});
if ( expectedErrorHandler != null )
diff --git a/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/TestRRX.java b/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/TestRRX.java
index 44235fb102..887ae1dfea 100644
--- a/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/TestRRX.java
+++ b/jena-arq/src/test/java/org/apache/jena/riot/lang/rdfxml/rrx/TestRRX.java
@@ -72,8 +72,6 @@ public class TestRRX {
this.lang = lang;
}
- // XXX Track files!
-
private static Set<String> processedFiles = new HashSet<>();
private void trackFilename(String filename) {
processedFiles.add(filename);
@@ -171,6 +169,26 @@ public class TestRRX {
checkForError("bad-unqualified-property.rdf", false);
}
+ @Test public void bad_unqualified_attribute1() {
+ checkForError("bad-unqualified-attribute1.rdf", false);
+ }
+
+ @Test public void bad_unqualified_attribute2() {
+ checkForError("bad-unqualified-attribute2.rdf", false);
+ }
+
+ @Test public void bad_unqualified_attribute3() {
+ checkForError("bad-unqualified-attribute3.rdf", false);
+ }
+
+ @Test public void bad_unqualified_attribute4() {
+ warningTest("bad-unqualified-attribute4.rdf", 1);
+ }
+
+ @Test public void bad_unqualified_attribute5() {
+ warningTest("bad-unqualified-attribute5.rdf", 1);
+ }
+
@Test public void bad_unqualified_class() {
checkForError("bad-unqualified-class.rdf", false);
}
diff --git a/jena-arq/testing/RIOT/rrx-files/bad-rdf-id-node.rdf b/jena-arq/testing/RIOT/rrx-files/bad-rdf-id-node.rdf
index db19f039ff..071ac9b2ed 100644
--- a/jena-arq/testing/RIOT/rrx-files/bad-rdf-id-node.rdf
+++ b/jena-arq/testing/RIOT/rrx-files/bad-rdf-id-node.rdf
@@ -11,4 +11,4 @@
<rdf:Description rdf:about="http://example/s" rdf:ID="foo">
<ex:property>text</ex:property>
</rdf:Description>
-</rdf:RDF>
\ No newline at end of file
+</rdf:RDF>
diff --git a/jena-arq/testing/RIOT/rrx-files/bad-rdf-resource-node.rdf b/jena-arq/testing/RIOT/rrx-files/bad-rdf-resource-node.rdf
index ee3bbab131..b29db442a2 100644
--- a/jena-arq/testing/RIOT/rrx-files/bad-rdf-resource-node.rdf
+++ b/jena-arq/testing/RIOT/rrx-files/bad-rdf-resource-node.rdf
@@ -11,4 +11,4 @@
<rdf:Description rdf:about="http://example/s" rdf:resource="http://example/foo">
<ex:property>text</ex:property>
</rdf:Description>
-</rdf:RDF>
\ No newline at end of file
+</rdf:RDF>
diff --git a/jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute1.rdf b/jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute1.rdf
index dd1db8e7c3..5957021ab9 100644
--- a/jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute1.rdf
+++ b/jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute1.rdf
@@ -3,4 +3,4 @@
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description name="NAME"/>
-</rdf:RDF>
\ No newline at end of file
+</rdf:RDF>
diff --git a/jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute2.rdf b/jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute2.rdf
index 21940b5d63..9a0bf21edf 100644
--- a/jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute2.rdf
+++ b/jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute2.rdf
@@ -6,4 +6,4 @@
xmlns="http://example/">
<!-- qnames do not apply to attributes unless ex:attr -->
<rdf:Description name="NAME"/>
-</rdf:RDF>
\ No newline at end of file
+</rdf:RDF>
diff --git a/jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute2.rdf b/jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute3.rdf
similarity index 66%
copy from jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute2.rdf
copy to jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute3.rdf
index 21940b5d63..be9dbd3fce 100644
--- a/jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute2.rdf
+++ b/jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute3.rdf
@@ -4,6 +4,6 @@
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns="http://example/">
- <!-- qnames do not apply to attributes unless ex:attr -->
- <rdf:Description name="NAME"/>
-</rdf:RDF>
\ No newline at end of file
+ <!-- less that 3 characters attribute -->
+ <rdf:Description n="NAME"/>
+</rdf:RDF>
diff --git a/jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute2.rdf b/jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute4.rdf
similarity index 66%
copy from jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute2.rdf
copy to jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute4.rdf
index 21940b5d63..400c1db00e 100644
--- a/jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute2.rdf
+++ b/jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute4.rdf
@@ -4,6 +4,6 @@
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns="http://example/">
- <!-- qnames do not apply to attributes unless ex:attr -->
- <rdf:Description name="NAME"/>
-</rdf:RDF>
\ No newline at end of file
+ <!-- xml : warning -->
+ <rdf:Description xml:n="NAME"/>
+</rdf:RDF>
diff --git a/jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute2.rdf b/jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute5.rdf
similarity index 63%
copy from jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute2.rdf
copy to jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute5.rdf
index 21940b5d63..70da6bddb0 100644
--- a/jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute2.rdf
+++ b/jena-arq/testing/RIOT/rrx-files/bad-unqualified-attribute5.rdf
@@ -4,6 +4,6 @@
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns="http://example/">
- <!-- qnames do not apply to attributes unless ex:attr -->
- <rdf:Description name="NAME"/>
-</rdf:RDF>
\ No newline at end of file
+ <!-- RDF/XML calls out plain attributes startin "xml" : warning -->
+ <rdf:Description XmLname="NAME"/>
+</rdf:RDF>