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
The following commit(s) were added to refs/heads/main by this push:
new c84f8df37f Fix query serialization round-trip issue (#2595)
c84f8df37f is described below
commit c84f8df37f8523c185ab678f7deec00abaa0f133
Author: Alex Tucker <[email protected]>
AuthorDate: Tue Aug 20 16:28:26 2024 +0100
Fix query serialization round-trip issue (#2595)
* Add test case to catch invalid local pname, :-x
* Re-do PName validation to follow
https://www.w3.org/TR/sparql11-query#PN_LOCAL production
* Re-use RiotChars.isPNChar*.
---
.../java/org/apache/jena/sparql/util/FmtUtils.java | 41 +++++++++++++---------
.../jena/sparql/syntax/TestSerialization.java | 3 ++
2 files changed, 27 insertions(+), 17 deletions(-)
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/util/FmtUtils.java
b/jena-arq/src/main/java/org/apache/jena/sparql/util/FmtUtils.java
index 6c171820ab..cd40a017ab 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/util/FmtUtils.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/util/FmtUtils.java
@@ -33,6 +33,7 @@ import org.apache.jena.rdf.model.Model ;
import org.apache.jena.rdf.model.RDFNode ;
import org.apache.jena.rdf.model.Resource ;
import org.apache.jena.riot.out.NodeFmtLib;
+import org.apache.jena.riot.system.RiotChars;
import org.apache.jena.shared.PrefixMapping ;
import org.apache.jena.sparql.ARQConstants ;
import org.apache.jena.sparql.ARQInternalErrorException ;
@@ -476,28 +477,34 @@ public class FmtUtils {
return true;
for ( int idx = 0 ; idx < localname.length() ; idx++ ) {
- char ch = localname.charAt(idx);
- if ( !validPNameChar(ch) )
- return false;
+ int ch = localname.codePointAt(idx);
+ if ( idx == 0 ) {
+ if ( !validFirstPNameChar(ch) )
+ return false;
+ } else if ( idx == localname.length() - 1 ) {
+ if ( !validEndPNameChar(ch) )
+ return false;
+ } else {
+ if ( !validMidPNameChar(ch) )
+ return false;
+ }
}
+ return true;
+ }
- // Test start and end - at least one character in the name.
-
- if ( localname.endsWith(".") )
- return false;
- if ( localname.startsWith(".") )
- return false;
+ // first char as per options in first bracketed production in 169:
https://www.w3.org/TR/sparql11-query/#rPN_LOCAL
+ private static boolean validFirstPNameChar(int ch) {
+ return RiotChars.isPNChars_U_N(ch) || ( ch == '%' ) ;
+ }
- return true;
+ // neither first char nor last in middle repeated production in 169:
https://www.w3.org/TR/sparql11-query/#rPN_LOCAL
+ private static boolean validMidPNameChar(int ch) {
+ return RiotChars.isPNChars(ch) || ( ch == '.' ) || ( ch == ':' ) || (
ch == '%' ) ;
}
- private static boolean validPNameChar(char ch) {
- if ( Character.isLetterOrDigit(ch) ) return true ;
- if ( ch == '.' ) return true ;
- if ( ch == ':' ) return true ;
- if ( ch == '-' ) return true ;
- if ( ch == '_' ) return true ;
- return false ;
+ // last char, but not first, in last production in 169:
https://www.w3.org/TR/sparql11-query/#rPN_LOCAL
+ private static boolean validEndPNameChar(int ch) {
+ return RiotChars.isPNChars(ch) || ( ch == ':' ) || ( ch == '%' ) ;
}
static boolean applyUnicodeEscapes = false ;
diff --git
a/jena-arq/src/test/java/org/apache/jena/sparql/syntax/TestSerialization.java
b/jena-arq/src/test/java/org/apache/jena/sparql/syntax/TestSerialization.java
index 08c3fd708f..6efb1eaef4 100644
---
a/jena-arq/src/test/java/org/apache/jena/sparql/syntax/TestSerialization.java
+++
b/jena-arq/src/test/java/org/apache/jena/sparql/syntax/TestSerialization.java
@@ -118,6 +118,9 @@ public class TestSerialization
@Test public void test_PName_Bad_7()
{ fmtURI_Prefix("http://example/x.", "<http://example/x.>", pmap1) ; }
+ @Test public void test_PName_Bad_8()
+ { fmtURI_Prefix("http://default/-x", "<http://default/-x>", pmap1) ; }
+
// Dots
@Test public void test_Dots_1() // Internal DOT
{ fmtURI_Prefix("http://example/x#a.b", "ex:a.b", pmap1) ; }