From f175bfe80e0636e9fef49a1f8e0f97ab7b99aeba Mon Sep 17 00:00:00 2001 From: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> Date: Mon, 26 Oct 2026 16:31:02 -0700 Subject: [PATCH 1/4] fix: honor XSD whitespace facets in numeric and boolean casts Fixes #4298 --- .../apache/jena/sparql/function/CastXSD.java | 25 +------- .../apache/jena/sparql/expr/TestCastXSD.java | 64 +++++++++++++++++++ 2 files changed, 66 insertions(+), 23 deletions(-) diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/function/CastXSD.java b/jena-arq/src/main/java/org/apache/jena/sparql/function/CastXSD.java index a2f0fb450d5..bb9148bda41 100644 --- a/jena-arq/src/main/java/org/apache/jena/sparql/function/CastXSD.java +++ b/jena-arq/src/main/java/org/apache/jena/sparql/function/CastXSD.java @@ -34,8 +34,6 @@ import org.apache.jena.datatypes.RDFDatatype; import org.apache.jena.datatypes.xsd.XSDDatatype; -import org.apache.jena.datatypes.xsd.impl.XSDAbstractDateTimeType; -import org.apache.jena.datatypes.xsd.impl.XSDBaseNumericType; import org.apache.jena.graph.Node; import org.apache.jena.graph.NodeFactory; import org.apache.jena.sparql.expr.ExprEvalException; @@ -284,17 +282,8 @@ private static NodeValue castToBoolean(NodeValue nv, XSDDatatype castType) { return NodeValue.TRUE; } if ( nv.isString() ) { - String str = nv.getString(); - switch (str) { - case "0": - case "false": - return NodeValue.FALSE; - case "1": - case "true": - return NodeValue.TRUE; - default: - throw exception(nv, castType); - } + boolean b = castByLex(nv, castType).getBoolean(); + return b ? NodeValue.TRUE : NodeValue.FALSE; } return castByLex(nv, castType); } @@ -442,16 +431,6 @@ private static NodeValue castByLex(NodeValue nv, XSDDatatype castType) { private static NodeValue castByLex(String lex, XSDDatatype castType) { if ( ! castType.isValid(lex) ) throw exception("Invalid lexical form: '"+lex+"' for "+castType.getURI()); - if ( castType instanceof XSDBaseNumericType || - castType.equals(XSDDatatype.XSDfloat) || - castType.equals(XSDDatatype.XSDdouble) || - castType.equals(XSDDatatype.XSDboolean) || - castType instanceof XSDAbstractDateTimeType ) // Includes durations, and Gregorian - { - // More helpful error message. - if ( lex.startsWith(" ") || lex.endsWith(" ") ) - throw exception("Not a valid literal form (has whitespace): '"+lex+"'"); - } NodeValue nv2 = NodeValue.makeNode(lex, castType); RDFDatatype dt = nv2.getNode().getLiteralDatatype(); if ( castType.equals(dt) ) diff --git a/jena-arq/src/test/java/org/apache/jena/sparql/expr/TestCastXSD.java b/jena-arq/src/test/java/org/apache/jena/sparql/expr/TestCastXSD.java index ac08136abd0..3139ccdb85c 100644 --- a/jena-arq/src/test/java/org/apache/jena/sparql/expr/TestCastXSD.java +++ b/jena-arq/src/test/java/org/apache/jena/sparql/expr/TestCastXSD.java @@ -22,6 +22,8 @@ package org.apache.jena.sparql.expr; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; import org.junit.jupiter.api.AfterAll; @@ -30,6 +32,8 @@ import org.apache.jena.graph.Node; import org.apache.jena.query.ARQ; +import org.apache.jena.query.QueryExecution; +import org.apache.jena.query.DatasetFactory; import org.apache.jena.sparql.ARQConstants; import org.apache.jena.sparql.engine.ExecutionContext; import org.apache.jena.sparql.function.FunctionEnv; @@ -140,6 +144,66 @@ public class TestCastXSD { @Test public void cast_to_gregorian_03() { testCast ("xsd:gMonthDay('2015-10-12'^^xsd:date)", "'--10-12'^^xsd:gMonthDay"); } @Test public void cast_to_gregorian_04() { testCast ("xsd:gYearMonth('2015-10-12'^^xsd:date)", "'2015-10'^^xsd:gYearMonth"); } + @Test public void cast_numeric_whitespace() { + for ( String type : new String[]{"integer", "decimal", "float", "double", "byte"} ) { + for ( String lex : new String[]{" 1", "1 ", " \\t\\r\\n1\\n\\r\\t "} ) { + String input = "xsd:"+type+"('"+lex+"')"; + NodeValue actual = cast(input); + NodeValue expected = cast("xsd:"+type+"('1')"); + assertTrue(NodeValue.sameValueAs(expected, actual), input); + assertEquals(expected.getDatatypeURI(), actual.getDatatypeURI(), input); + } + } + } + + @Test public void cast_boolean_whitespace() { + for ( String token : new String[]{"true", "false", "1", "0"} ) { + String expected = token.equals("true") || token.equals("1") ? "true" : "false"; + testCast("xsd:boolean(' "+token+"')", expected); + testCast("xsd:boolean('"+token+" ')", expected); + testCast("xsd:boolean(' \\t\\r\\n"+token+"\\n\\r\\t ')", expected); + } + } + + @Test public void cast_invalid_whitespace() { + for ( String type : new String[]{"integer", "decimal", "float", "double", "boolean"} ) { + for ( String lex : new String[]{"", " \\t\\r\\n", "1 2", "abc", "\u00A01\u00A0"} ) + testNoCast("xsd:"+type+"('"+lex+"')"); + } + testNoCast("xsd:boolean(' tr ue ')"); + testNoCast("xsd:boolean(' TRUE ')"); + testNoCast("xsd:byte(' 128 ')"); + testNoCast("xsd:unsignedInt(' -1 ')"); + } + + @Test public void cast_whitespace_controls() { + testCast("xsd:date(' 2015-10-12 ')", "' 2015-10-12 '^^xsd:date"); + testCast("xsd:string(' 1 ')", "' 1 '"); + } + + @Test public void cast_whitespace_select() { + String query = """ + PREFIX xsd: + SELECT (xsd:integer(' 1 ') AS ?integer) + (isNumeric(' 1 '^^xsd:integer) AS ?numeric) + (' 1 '^^xsd:integer + 0 AS ?sum) + (xsd:double(' 1 ') AS ?double) + (xsd:boolean(' true ') AS ?boolean) + WHERE {} + """; + try ( QueryExecution qExec = QueryExecution.create(query, DatasetFactory.create()) ) { + var results = qExec.execSelect(); + assertTrue(results.hasNext()); + var row = results.next(); + assertEquals(1, row.getLiteral("integer").getInt()); + assertTrue(row.getLiteral("numeric").getBoolean()); + assertEquals(1, row.getLiteral("sum").getInt()); + assertEquals(1.0, row.getLiteral("double").getDouble()); + assertTrue(row.getLiteral("boolean").getBoolean()); + assertFalse(results.hasNext()); + } + } + private void testNoCast(String input) { try { cast(input); From 6b3cd595ce8ec20f3ed609de7ad3417cdbbdbe87 Mon Sep 17 00:00:00 2001 From: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> Date: Sun, 8 Nov 2026 10:35:26 -0800 Subject: [PATCH 2/4] GH-4298: Trim whitespace in collapsed-whitespace casts Remove leading and trailing XML whitespace (#x9, #xA, #xD, #x20) before casting a string to a numeric, boolean or duration datatype, so the result has the canonical lexical form ("1"^^xsd:integer, not " 1 "^^xsd:integer). Update TestExpressions2 xsd_cast_10..13, which asserted the old rejection, add duration coverage, and drop the query-level test. --- .../apache/jena/sparql/function/CastXSD.java | 31 ++++++++++++ .../apache/jena/sparql/expr/TestCastXSD.java | 49 +++++-------------- .../jena/sparql/expr/TestExpressions2.java | 10 ++-- 3 files changed, 50 insertions(+), 40 deletions(-) diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/function/CastXSD.java b/jena-arq/src/main/java/org/apache/jena/sparql/function/CastXSD.java index bb9148bda41..eb1abbf345b 100644 --- a/jena-arq/src/main/java/org/apache/jena/sparql/function/CastXSD.java +++ b/jena-arq/src/main/java/org/apache/jena/sparql/function/CastXSD.java @@ -34,6 +34,8 @@ import org.apache.jena.datatypes.RDFDatatype; import org.apache.jena.datatypes.xsd.XSDDatatype; +import org.apache.jena.datatypes.xsd.impl.XSDAbstractDateTimeType; +import org.apache.jena.datatypes.xsd.impl.XSDBaseNumericType; import org.apache.jena.graph.Node; import org.apache.jena.graph.NodeFactory; import org.apache.jena.sparql.expr.ExprEvalException; @@ -429,6 +431,12 @@ private static NodeValue castByLex(NodeValue nv, XSDDatatype castType) { // Cast by lexical form with checking. private static NodeValue castByLex(String lex, XSDDatatype castType) { + if ( isWhitespaceCollapsed(castType) ) + // These datatypes have the whitespace facet "collapse" so leading + // and trailing XML whitespace is not part of the value. + // https://www.w3.org/TR/xpath-functions/#casting-from-strings + // Remove it so the result has a clean lexical form. + lex = trimXMLWhitespace(lex); if ( ! castType.isValid(lex) ) throw exception("Invalid lexical form: '"+lex+"' for "+castType.getURI()); NodeValue nv2 = NodeValue.makeNode(lex, castType); @@ -438,6 +446,29 @@ private static NodeValue castByLex(String lex, XSDDatatype castType) { throw exception("Can not cast '"+lex+"' to a "+castType); } + private static boolean isWhitespaceCollapsed(XSDDatatype castType) { + return castType instanceof XSDBaseNumericType || + castType.equals(XSDDatatype.XSDfloat) || + castType.equals(XSDDatatype.XSDdouble) || + castType.equals(XSDDatatype.XSDboolean) || + castType instanceof XSDAbstractDateTimeType; // Includes durations, and Gregorian + } + + private static boolean isXMLWhitespace(char ch) { + return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'; + } + + /** Remove leading and trailing XML whitespace (#x9, #xA, #xD, #x20). */ + private static String trimXMLWhitespace(String lex) { + int start = 0; + int end = lex.length(); + while ( start < end && isXMLWhitespace(lex.charAt(start)) ) + start++; + while ( end > start && isXMLWhitespace(lex.charAt(end-1)) ) + end--; + return lex.substring(start, end); + } + // Known to work casts. No checking. private static NodeValue cast$(String lex, XSDDatatype castType) { return NodeValue.makeNode(lex, castType); diff --git a/jena-arq/src/test/java/org/apache/jena/sparql/expr/TestCastXSD.java b/jena-arq/src/test/java/org/apache/jena/sparql/expr/TestCastXSD.java index 3139ccdb85c..a3e7ae69f44 100644 --- a/jena-arq/src/test/java/org/apache/jena/sparql/expr/TestCastXSD.java +++ b/jena-arq/src/test/java/org/apache/jena/sparql/expr/TestCastXSD.java @@ -22,8 +22,6 @@ package org.apache.jena.sparql.expr; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; import org.junit.jupiter.api.AfterAll; @@ -32,8 +30,6 @@ import org.apache.jena.graph.Node; import org.apache.jena.query.ARQ; -import org.apache.jena.query.QueryExecution; -import org.apache.jena.query.DatasetFactory; import org.apache.jena.sparql.ARQConstants; import org.apache.jena.sparql.engine.ExecutionContext; import org.apache.jena.sparql.function.FunctionEnv; @@ -144,15 +140,12 @@ public class TestCastXSD { @Test public void cast_to_gregorian_03() { testCast ("xsd:gMonthDay('2015-10-12'^^xsd:date)", "'--10-12'^^xsd:gMonthDay"); } @Test public void cast_to_gregorian_04() { testCast ("xsd:gYearMonth('2015-10-12'^^xsd:date)", "'2015-10'^^xsd:gYearMonth"); } + // Leading and trailing XML whitespace is removed for datatypes with whitespace facet "collapse". + // The result has the lexical form without the whitespace. @Test public void cast_numeric_whitespace() { for ( String type : new String[]{"integer", "decimal", "float", "double", "byte"} ) { - for ( String lex : new String[]{" 1", "1 ", " \\t\\r\\n1\\n\\r\\t "} ) { - String input = "xsd:"+type+"('"+lex+"')"; - NodeValue actual = cast(input); - NodeValue expected = cast("xsd:"+type+"('1')"); - assertTrue(NodeValue.sameValueAs(expected, actual), input); - assertEquals(expected.getDatatypeURI(), actual.getDatatypeURI(), input); - } + for ( String lex : new String[]{" 1", "1 ", " \\t\\r\\n1\\n\\r\\t "} ) + testCast("xsd:"+type+"('"+lex+"')", "'1'^^xsd:"+type); } } @@ -165,6 +158,13 @@ public class TestCastXSD { } } + @Test public void cast_duration_whitespace() { + testCast("xsd:duration(' P1D ')", "'P1D'^^xsd:duration"); + testCast("xsd:dayTimeDuration(' PT10S ')", "'PT10S'^^xsd:dayTimeDuration"); + testCast("xsd:yearMonthDuration(' P1Y2M ')", "'P1Y2M'^^xsd:yearMonthDuration"); + testNoCast("xsd:duration(' P 1D ')"); + } + @Test public void cast_invalid_whitespace() { for ( String type : new String[]{"integer", "decimal", "float", "double", "boolean"} ) { for ( String lex : new String[]{"", " \\t\\r\\n", "1 2", "abc", "\u00A01\u00A0"} ) @@ -176,34 +176,11 @@ public class TestCastXSD { testNoCast("xsd:unsignedInt(' -1 ')"); } - @Test public void cast_whitespace_controls() { - testCast("xsd:date(' 2015-10-12 ')", "' 2015-10-12 '^^xsd:date"); + // xsd:string keeps the whitespace. + @Test public void cast_string_whitespace() { testCast("xsd:string(' 1 ')", "' 1 '"); } - @Test public void cast_whitespace_select() { - String query = """ - PREFIX xsd: - SELECT (xsd:integer(' 1 ') AS ?integer) - (isNumeric(' 1 '^^xsd:integer) AS ?numeric) - (' 1 '^^xsd:integer + 0 AS ?sum) - (xsd:double(' 1 ') AS ?double) - (xsd:boolean(' true ') AS ?boolean) - WHERE {} - """; - try ( QueryExecution qExec = QueryExecution.create(query, DatasetFactory.create()) ) { - var results = qExec.execSelect(); - assertTrue(results.hasNext()); - var row = results.next(); - assertEquals(1, row.getLiteral("integer").getInt()); - assertTrue(row.getLiteral("numeric").getBoolean()); - assertEquals(1, row.getLiteral("sum").getInt()); - assertEquals(1.0, row.getLiteral("double").getDouble()); - assertTrue(row.getLiteral("boolean").getBoolean()); - assertFalse(results.hasNext()); - } - } - private void testNoCast(String input) { try { cast(input); diff --git a/jena-arq/src/test/java/org/apache/jena/sparql/expr/TestExpressions2.java b/jena-arq/src/test/java/org/apache/jena/sparql/expr/TestExpressions2.java index 6d637c278d2..48ccdf478e2 100644 --- a/jena-arq/src/test/java/org/apache/jena/sparql/expr/TestExpressions2.java +++ b/jena-arq/src/test/java/org/apache/jena/sparql/expr/TestExpressions2.java @@ -254,10 +254,12 @@ public void triple_term_cmp_04() @Test public void xsd_cast_03() { eval("sameTerm(xsd:double('1.0e0'),1.0e0)", true); } @Test public void xsd_cast_04() { eval("xsd:double('1') = 1", true); } - @Test public void xsd_cast_10() { assertThrows(ExprEvalException.class, ()-> eval("xsd:integer(' 1')") ); } - @Test public void xsd_cast_11() { assertThrows(ExprEvalException.class, ()-> eval("xsd:boolean(' 1')") ); } - @Test public void xsd_cast_12() { assertThrows(ExprEvalException.class, ()-> eval("xsd:double(' 1.0e0')") ); } - @Test public void xsd_cast_13() { assertThrows(ExprEvalException.class, ()-> eval("xsd:double(' 1.0e0')") ); } + // Leading and trailing whitespace is removed (whitespace facet "collapse"). + @Test public void xsd_cast_10() { eval("xsd:integer(' 1') = 1", true); } + @Test public void xsd_cast_11() { eval("xsd:boolean(' 1') = true", true); } + @Test public void xsd_cast_12() { eval("sameTerm(xsd:double(' 1.0e0'), 1.0e0)", true); } + @Test public void xsd_cast_13() { eval("sameTerm(xsd:integer(' 1 '), 1)", true); } + @Test public void xsd_cast_14() { assertThrows(ExprEvalException.class, ()-> eval("xsd:integer('1 2')") ); } // Dynamic Function Calls @Test public void dynamic_call_01() { assertThrows(QueryParseException.class, ()-> eval("CALL()", false) ); } From b4eda4b0d7c4d515f4eed708f6a6ee4889137cf9 Mon Sep 17 00:00:00 2001 From: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> Date: Sat, 14 Nov 2026 15:00:09 -0800 Subject: [PATCH 3/4] GH-4298: Simplify whitespace comment in castByLex --- .../main/java/org/apache/jena/sparql/function/CastXSD.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/function/CastXSD.java b/jena-arq/src/main/java/org/apache/jena/sparql/function/CastXSD.java index eb1abbf345b..45ff3afc3a4 100644 --- a/jena-arq/src/main/java/org/apache/jena/sparql/function/CastXSD.java +++ b/jena-arq/src/main/java/org/apache/jena/sparql/function/CastXSD.java @@ -432,10 +432,7 @@ private static NodeValue castByLex(NodeValue nv, XSDDatatype castType) { // Cast by lexical form with checking. private static NodeValue castByLex(String lex, XSDDatatype castType) { if ( isWhitespaceCollapsed(castType) ) - // These datatypes have the whitespace facet "collapse" so leading - // and trailing XML whitespace is not part of the value. - // https://www.w3.org/TR/xpath-functions/#casting-from-strings - // Remove it so the result has a clean lexical form. + // Remove empty white space around values lex = trimXMLWhitespace(lex); if ( ! castType.isValid(lex) ) throw exception("Invalid lexical form: '"+lex+"' for "+castType.getURI()); From d3b9b3d8dd15a45e793d30ce8389031a36501383 Mon Sep 17 00:00:00 2001 From: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:08:42 -0800 Subject: [PATCH 4/4] GH-4298: Use XML10Char.trim for whitespace collapse in casts Replace the private trimXMLWhitespace and isXMLWhitespace helpers in CastXSD with the existing XML10Char.trim, which trims the XML 1.0 S production characters. --- .../apache/jena/sparql/function/CastXSD.java | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/function/CastXSD.java b/jena-arq/src/main/java/org/apache/jena/sparql/function/CastXSD.java index 45ff3afc3a4..e8d319029bf 100644 --- a/jena-arq/src/main/java/org/apache/jena/sparql/function/CastXSD.java +++ b/jena-arq/src/main/java/org/apache/jena/sparql/function/CastXSD.java @@ -44,6 +44,7 @@ import org.apache.jena.sparql.expr.NodeValue; import org.apache.jena.sparql.expr.nodevalue.XSDFuncOp; import org.apache.jena.sparql.util.XSDNumUtils; +import org.apache.jena.util.XML10Char; /** * Code for all casting between XSD datatypes. @@ -433,7 +434,7 @@ private static NodeValue castByLex(NodeValue nv, XSDDatatype castType) { private static NodeValue castByLex(String lex, XSDDatatype castType) { if ( isWhitespaceCollapsed(castType) ) // Remove empty white space around values - lex = trimXMLWhitespace(lex); + lex = XML10Char.trim(lex); if ( ! castType.isValid(lex) ) throw exception("Invalid lexical form: '"+lex+"' for "+castType.getURI()); NodeValue nv2 = NodeValue.makeNode(lex, castType); @@ -451,21 +452,6 @@ private static boolean isWhitespaceCollapsed(XSDDatatype castType) { castType instanceof XSDAbstractDateTimeType; // Includes durations, and Gregorian } - private static boolean isXMLWhitespace(char ch) { - return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'; - } - - /** Remove leading and trailing XML whitespace (#x9, #xA, #xD, #x20). */ - private static String trimXMLWhitespace(String lex) { - int start = 0; - int end = lex.length(); - while ( start < end && isXMLWhitespace(lex.charAt(start)) ) - start++; - while ( end > start && isXMLWhitespace(lex.charAt(end-1)) ) - end--; - return lex.substring(start, end); - } - // Known to work casts. No checking. private static NodeValue cast$(String lex, XSDDatatype castType) { return NodeValue.makeNode(lex, castType);