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 9a1e6d4679d8f51661b27005cb45ad9545eda7d5 Author: Andy Seaborne <a...@apache.org> AuthorDate: Fri Aug 30 10:55:29 2024 +0100 GH-2669: Test for rdf:datatype with incompatible RDF attributes --- .../jena/riot/lang/rdfxml/rrx/ParserRRX_SAX.java | 12 ++++++++++-- .../lang/rdfxml/rrx_stax_ev/ParserRRX_StAX_EV.java | 19 ++++++++++++++----- .../lang/rdfxml/rrx_stax_sr/ParserRRX_StAX_SR.java | 19 ++++++++++++++----- .../org/apache/jena/riot/lang/rdfxml/rrx/TestRRX.java | 12 ++++++++++++ jena-arq/testing/RIOT/rrx-files/bad-object-type-1.rdf | 12 ++++++++++++ jena-arq/testing/RIOT/rrx-files/bad-object-type-2.rdf | 12 ++++++++++++ jena-arq/testing/RIOT/rrx-files/bad-object-type-3.rdf | 12 ++++++++++++ 7 files changed, 86 insertions(+), 12 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 f1bf90887f..d6aa5c68e1 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 @@ -765,11 +765,19 @@ class ParserRRX_SAX String rdfResourceStr = attributes.getValue(rdfNS, rdfResource); // Checked if the blank node is created. String objBlankNodeLabel = attributes.getValue(rdfNS, rdfNodeID); - String parseTypeStr = attributes.getValue(rdfNS, rdfParseType); - Node resourceObj = null; + // Better error messages. + if ( dt != null ) { + if ( parseTypeStr != null ) + throw RDFXMLparseError("rdf:datatype can not be used with rdf:parseType.", position); + if ( rdfResourceStr != null ) + throw RDFXMLparseError("rdf:datatype can not be used with rdf:resource.", position); + if ( objBlankNodeLabel != null ) + throw RDFXMLparseError("rdf:datatype can not be used with rdf:NodeId.", position); + } + if ( rdfResourceStr != null && objBlankNodeLabel != null ) throw RDFXMLparseError("Both rdf:resource and rdf:NodeId on a property element. Only one allowed", 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 cbd3fd68e3..eaefd06559 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 @@ -538,19 +538,28 @@ class ParserRRX_StAX_EV { // If there is a blank node label, the element must be empty, // Check NCName if blank node created - String objBlanklNodeLabel = attribute(startElt, rdfNodeID); + String objBlankNodeLabel = attribute(startElt, rdfNodeID); String rdfResourceStr = attribute(startElt, rdfResource); String datatype = attribute(startElt, rdfDatatype); String parseType = objectParseType(startElt); // Checking - if ( rdfResourceStr != null && objBlanklNodeLabel != null ) + if ( datatype != null ) { + if ( parseType != null && parseType != parseTypePlain ) + throw RDFXMLparseError("rdf:datatype can not be used with rdf:parseType.", startElt); + if ( rdfResourceStr != null ) + throw RDFXMLparseError("rdf:datatype can not be used with rdf:resource.", startElt); + if ( objBlankNodeLabel != null ) + throw RDFXMLparseError("rdf:datatype can not be used with rdf:NodeId.", startElt); + } + + if ( rdfResourceStr != null && objBlankNodeLabel != null ) throw RDFXMLparseError("Can't have both rdf:nodeId and rdf:resource on a property element", startElt); if ( rdfResourceStr != null && parseType != parseTypePlain ) throw RDFXMLparseError("Both rdf:resource and rdf:ParseType on a property element. Only one allowed", startElt); - if ( objBlanklNodeLabel != null && parseType != parseTypePlain ) + if ( objBlankNodeLabel != null && parseType != parseTypePlain ) throw RDFXMLparseError("Both rdf:NodeId and rdf:ParseType on a property element. Only one allowed", startElt); Node resourceObj = null; @@ -558,8 +567,8 @@ class ParserRRX_StAX_EV { if ( rdfResourceStr != null ) resourceObj = iriResolve(rdfResourceStr, location); - if ( objBlanklNodeLabel != null ) - resourceObj = blankNode(objBlanklNodeLabel, location); + if ( objBlankNodeLabel != null ) + resourceObj = blankNode(objBlankNodeLabel, location); Node innerSubject = processPropertyAttributes(resourceObj, startElt, true, location); if ( resourceObj == null && innerSubject != null ) { 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 32c8eaf971..9f11b5954f 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 @@ -512,19 +512,28 @@ class ParserRRX_StAX_SR { // If there is a blank node label, the element must be empty, // Check NCName if blank node created - String objBlanklNodeLabel = attribute(rdfNodeID); + String objBlankNodeLabel = attribute(rdfNodeID); String rdfResourceStr = attribute(rdfResource); String datatype = attribute(rdfDatatype); String parseType = objectParseType(); // Checking - if ( rdfResourceStr != null && objBlanklNodeLabel != null ) + if ( datatype != null ) { + if ( parseType != null && parseType != parseTypePlain ) + throw RDFXMLparseError("rdf:datatype can not be used with rdf:parseType."); + if ( rdfResourceStr != null ) + throw RDFXMLparseError("rdf:datatype can not be used with rdf:resource."); + if ( objBlankNodeLabel != null ) + throw RDFXMLparseError("rdf:datatype can not be used with rdf:NodeId."); + } + + if ( rdfResourceStr != null && objBlankNodeLabel != null ) throw RDFXMLparseError("Can't have both rdf:nodeId and rdf:resource on a property element"); if ( rdfResourceStr != null && parseType != parseTypePlain ) throw RDFXMLparseError("Both rdf:resource and rdf:ParseType on a property element. Only one allowed"); - if ( objBlanklNodeLabel != null && parseType != parseTypePlain ) + if ( objBlankNodeLabel != null && parseType != parseTypePlain ) throw RDFXMLparseError("Both rdf:NodeId and rdf:ParseType on a property element. Only one allowed"); Node resourceObj = null; @@ -532,8 +541,8 @@ class ParserRRX_StAX_SR { if ( rdfResourceStr != null ) resourceObj = iriResolve(rdfResourceStr, location); - if ( objBlanklNodeLabel != null ) - resourceObj = blankNode(objBlanklNodeLabel, location); + if ( objBlankNodeLabel != null ) + resourceObj = blankNode(objBlankNodeLabel, location); Node innerSubject = processPropertyAttributes(resourceObj, qName, true, location); if ( resourceObj == null && innerSubject != 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 887ae1dfea..032c6e1743 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 @@ -193,6 +193,18 @@ public class TestRRX { checkForError("bad-unqualified-class.rdf", false); } + @Test public void bad_property_object_1() { + checkForError("bad-object-type-1.rdf", false); + } + + @Test public void bad_property_object_2() { + checkForError("bad-object-type-2.rdf", false); + } + + @Test public void bad_property_object_3() { + checkForError("bad-object-type-3.rdf", false); + } + /** Parse with no base set by the parser */ private void noBase(String filename) { trackFilename(filename); diff --git a/jena-arq/testing/RIOT/rrx-files/bad-object-type-1.rdf b/jena-arq/testing/RIOT/rrx-files/bad-object-type-1.rdf new file mode 100644 index 0000000000..5bceb260c8 --- /dev/null +++ b/jena-arq/testing/RIOT/rrx-files/bad-object-type-1.rdf @@ -0,0 +1,12 @@ +<rdf:RDF + xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" + xml:base="http://base/" + xmlns:xsd="http://www.w3.org/2001/XMLSchema#" + xmlns:ex="http://example/"> + + <rdf:Description rdf:about="s"> + <ex:property rdf:parseType="Literal" rdf:datatype="http://www.w3.org/2001/XMLSchema#integer"> + 123 + </ex:property> + </rdf:Description> +</rdf:RDF> diff --git a/jena-arq/testing/RIOT/rrx-files/bad-object-type-2.rdf b/jena-arq/testing/RIOT/rrx-files/bad-object-type-2.rdf new file mode 100644 index 0000000000..b913f13f51 --- /dev/null +++ b/jena-arq/testing/RIOT/rrx-files/bad-object-type-2.rdf @@ -0,0 +1,12 @@ +<rdf:RDF + xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" + xml:base="http://base/" + xmlns:xsd="http://www.w3.org/2001/XMLSchema#" + xmlns:ex="http://example/"> + + <rdf:Description rdf:about="s"> + <ex:property rdf:parseType="Literal" rdf:resource="http://example/r"> + 123 + </ex:property> + </rdf:Description> +</rdf:RDF> diff --git a/jena-arq/testing/RIOT/rrx-files/bad-object-type-3.rdf b/jena-arq/testing/RIOT/rrx-files/bad-object-type-3.rdf new file mode 100644 index 0000000000..c8260dfaf4 --- /dev/null +++ b/jena-arq/testing/RIOT/rrx-files/bad-object-type-3.rdf @@ -0,0 +1,12 @@ +<rdf:RDF + xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" + xml:base="http://base/" + xmlns:xsd="http://www.w3.org/2001/XMLSchema#" + xmlns:ex="http://example/"> + + <rdf:Description rdf:about="s"> + <ex:property rdf:parseType="Literal" rdf:nodeID="b1234"> + 123 + </ex:property> + </rdf:Description> +</rdf:RDF>