Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 14 additions & 21 deletions jena-arq/src/main/java/org/apache/jena/sparql/function/CastXSD.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -440,25 +432,26 @@ 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) )
return nv2;
throw exception("Can not cast '"+lex+"' to a "+castType);
}

private static boolean isWhitespaceCollapsed(XSDDatatype castType) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From Andy's comment, it sounds like there's already a method in Jena to perform the white space removal. Is it possible to use that instead of this new method and isXMLWhitespace?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, switched to XML10Char.trim in d3b9b3d and removed trimXMLWhitespace and isXMLWhitespace from CastXSD. TestCastXSD and TestExpressions2 still pass.

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) ); }
Expand Down
Loading