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..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. @@ -284,17 +285,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); } @@ -440,18 +432,11 @@ 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) ) + // Remove empty white space around values + lex = XML10Char.trim(lex); 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) ) @@ -459,6 +444,14 @@ 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 + } + // 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 ac08136abd0..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 @@ -140,6 +140,47 @@ 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 "} ) + testCast("xsd:"+type+"('"+lex+"')", "'1'^^xsd:"+type); + } + } + + @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_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"} ) + testNoCast("xsd:"+type+"('"+lex+"')"); + } + testNoCast("xsd:boolean(' tr ue ')"); + testNoCast("xsd:boolean(' TRUE ')"); + testNoCast("xsd:byte(' 128 ')"); + testNoCast("xsd:unsignedInt(' -1 ')"); + } + + // xsd:string keeps the whitespace. + @Test public void cast_string_whitespace() { + testCast("xsd:string(' 1 ')", "' 1 '"); + } + 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) ); }