You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2011/10/31 14:30:21 UTC
svn commit: r1195452 - in /incubator/jena/Jena2/ARQ/trunk/src:
main/java/com/hp/hpl/jena/sparql/expr/nodevalue/XSDFuncOp.java
test/java/com/hp/hpl/jena/sparql/expr/TestFunctions.java
Author: andy
Date: Mon Oct 31 13:30:21 2011
New Revision: 1195452
URL: http://svn.apache.org/viewvc?rev=1195452&view=rev
Log:
Align to SPARQL 1.1 spec (STRAFTER, STRBEFORE)
Modified:
incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/expr/nodevalue/XSDFuncOp.java
incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/expr/TestFunctions.java
Modified: incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/expr/nodevalue/XSDFuncOp.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/expr/nodevalue/XSDFuncOp.java?rev=1195452&r1=1195451&r2=1195452&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/expr/nodevalue/XSDFuncOp.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/expr/nodevalue/XSDFuncOp.java Mon Oct 31 13:30:21 2011
@@ -364,9 +364,7 @@ public class XSDFuncOp
}
// @@ To NodeFunctions.
- /** String operations - se NodeFunctions beause we have to cope with
- * simple literals, literals with language and xsd;string
- * @param subLabel
+ /** check and get a string (may be a simple literal, literal with language tag or an XSD string).
*/
private static Node checkAndGetString(String label, NodeValue nv)
@@ -431,9 +429,9 @@ public class XSDFuncOp
String n = checkAndGetString("replace", nvStr).getLiteralLexicalForm() ;
String rep = checkAndGetString("replace", nvReplacement).getLiteralLexicalForm() ;
String x = pattern.matcher(n).replaceAll(rep) ;
- return NodeValue.makeString(x) ;
+ //return NodeValue.makeString(x) ;
+ return calcReturn(x, nvStr.asNode()) ;
}
-
public static NodeValue strReplace(NodeValue nvStr, NodeValue nvPattern, NodeValue nvReplacement)
{
@@ -524,7 +522,7 @@ public class XSDFuncOp
}
throw new ExprEvalException("Not a number:"+nv) ;
}
-
+
// Check for string operations with primary first arg and second second arg (e.g. CONTAINS)
private static void check2(String label, NodeValue arg1, NodeValue arg2)
{
@@ -577,20 +575,37 @@ public class XSDFuncOp
return NodeValue.booleanReturn(lex1.endsWith(lex2)) ;
}
+ private static NodeValue calcReturn(String result, Node arg)
+ {
+ if ( arg.getLiteralDatatype() != null )
+ {
+ if ( arg.getLiteralDatatype() != XSDDatatype.XSDstring )
+ throw new ARQInternalErrorException("Excepted only xsd:string: "+arg) ;
+ // Must be xsd:string
+ return NodeValue.makeNode(result, XSDDatatype.XSDstring ) ;
+ }
+ String lang = arg.getLiteralLanguage() ;
+ if ( lang == null ) lang = "" ;
+ if ( lang.equals("") ) return NodeValue.makeString(result) ;
+ return NodeValue.makeNode(result, lang, (String)null) ;
+ }
+
public static NodeValue strBefore(NodeValue string, NodeValue match)
{
check2("strBefore", string, match) ;
String lex1 = string.asNode().getLiteralLexicalForm() ;
String lex2 = match.asNode().getLiteralLexicalForm() ;
+ Node mainArg = string.asNode() ;
+
if ( lex2.length() == 0 )
- return NodeValue.nvEmptyString ;
+ return calcReturn("", mainArg) ;
int i = lex1.indexOf(lex2) ;
if ( i < 0 )
- return NodeValue.nvEmptyString ;
+ return calcReturn("", mainArg) ;
String s = lex1.substring(0, i) ;
- return NodeValue.makeString(s) ;
+ return calcReturn(s, string.asNode()) ;
}
public static NodeValue strAfter(NodeValue string, NodeValue match)
@@ -598,15 +613,17 @@ public class XSDFuncOp
check2("strAfter", string, match) ;
String lex1 = string.asNode().getLiteralLexicalForm() ;
String lex2 = match.asNode().getLiteralLexicalForm() ;
+ Node mainArg = string.asNode() ;
+
if ( lex2.length() == 0 )
- return NodeValue.nvEmptyString ;
+ return calcReturn("", mainArg) ;
int i = lex1.indexOf(lex2) ;
if ( i < 0 )
- return NodeValue.nvEmptyString ;
+ return calcReturn("", mainArg) ;
i += lex2.length() ;
String s = lex1.substring(i) ;
- return NodeValue.makeString(s) ;
+ return calcReturn(s, string.asNode()) ;
}
public static NodeValue strLowerCase(NodeValue string)
Modified: incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/expr/TestFunctions.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/expr/TestFunctions.java?rev=1195452&r1=1195451&r2=1195452&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/expr/TestFunctions.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/expr/TestFunctions.java Mon Oct 31 13:30:21 2011
@@ -70,12 +70,56 @@ public class TestFunctions
@Test public void exprStrStart3() { test("fn:starts-with('abc', 'abc')", TRUE) ; }
@Test public void exprStrStart4() { test("fn:starts-with('abc', 'abcd')", FALSE) ; }
+ @Test public void exprStrStart10() { test("STRSTARTS('abc', 'abcd')", FALSE) ; }
+ @Test public void exprStrStart11() { test("STRSTARTS('abc'@en, 'ab')", TRUE) ; }
+ @Test public void exprStrStart12() { test("STRSTARTS('abc'^^xsd:string, 'ab')", TRUE) ; }
+ @Test public void exprStrStart13() { test("STRSTARTS('abc'^^xsd:string, 'ab'^^xsd:string)", TRUE) ; }
+ @Test public void exprStrStart14() { test("STRSTARTS('abc', 'ab'^^xsd:string)", TRUE) ; }
+ @Test public void exprStrStart15() { test("STRSTARTS('abc'@en, 'ab'@en)", TRUE) ; }
+
+ @Test public void exprStrStart16() { testEvalException("STRSTARTS('ab'@en, 'ab'@fr)") ; }
+ @Test public void exprStrStart17() { testEvalException("STRSTARTS(123, 'ab'@fr)") ; }
+ @Test public void exprStrStart18() { testEvalException("STRSTARTS('123'^^xsd:string, 12.3)") ; }
+
+ @Test public void exprStrBefore0() { test("STRBEFORE('abc', 'abcd')", NodeValue.nvEmptyString) ; }
+ @Test public void exprStrBefore1() { test("STRBEFORE('abc'@en, 'b')", NodeValue.makeNode("a", "en", (String)null)) ; }
+ @Test public void exprStrBefore2() { test("STRBEFORE('abc'^^xsd:string, 'c')", NodeValue.makeNode("ab", XSDDatatype.XSDstring)) ; }
+ @Test public void exprStrBefore3() { test("STRBEFORE('abc'^^xsd:string, ''^^xsd:string)", NodeValue.makeNode("", XSDDatatype.XSDstring)) ; }
+ @Test public void exprStrBefore4() { test("STRBEFORE('abc', 'ab'^^xsd:string)", NodeValue.nvEmptyString) ; }
+ @Test public void exprStrBefore5() { test("STRBEFORE('abc'@en, 'b'@en)", NodeValue.makeNode("a", "en", (String)null)) ; }
+
+ @Test public void exprStrBefore6() { testEvalException("STRBEFORE('ab'@en, 'ab'@fr)") ; }
+ @Test public void exprStrBefore7() { testEvalException("STRBEFORE(123, 'ab'@fr)") ; }
+ @Test public void exprStrBefore8() { testEvalException("STRBEFORE('123'^^xsd:string, 12.3)") ; }
+
+ @Test public void exprStrAfter0() { test("STRAFTER('abc', 'abcd')", NodeValue.nvEmptyString) ; }
+ @Test public void exprStrAfter1() { test("STRAFTER('abc'@en, 'b')", NodeValue.makeNode("c", "en", (String)null)) ; }
+ @Test public void exprStrAfter2() { test("STRAFTER('abc'^^xsd:string, 'a')", NodeValue.makeNode("bc", XSDDatatype.XSDstring)) ; }
+ @Test public void exprStrAfter3() { test("STRAFTER('abc'^^xsd:string, ''^^xsd:string)", NodeValue.makeNode("", XSDDatatype.XSDstring)) ; }
+ @Test public void exprStrAfter4() { test("STRAFTER('abc', 'bc'^^xsd:string)", NodeValue.nvEmptyString) ; }
+ @Test public void exprStrAfter5() { test("STRAFTER('abc'@en, 'b'@en)", NodeValue.makeNode("c", "en", (String)null)) ; }
+
+ @Test public void exprStrAfter6() { testEvalException("STRAFTER('ab'@en, 'ab'@fr)") ; }
+ @Test public void exprStrAfter7() { testEvalException("STRAFTER(123, 'ab'@fr)") ; }
+ @Test public void exprStrAfter8() { testEvalException("STRAFTER('123'^^xsd:string, 12.3)") ; }
+
@Test public void exprStrEnds0() { test("fn:ends-with('abc', '')", TRUE) ; }
@Test public void exprStrEnds1() { test("fn:ends-with('abc', 'c')", TRUE) ; }
@Test public void exprStrEnds2() { test("fn:ends-with('abc', 'bc')", TRUE) ; }
@Test public void exprStrEnds3() { test("fn:ends-with('abc', 'abc')", TRUE) ; }
@Test public void exprStrEnds4() { test("fn:ends-with('abc', 'zabc')", FALSE) ; }
-
+
+ @Test public void exprStrEnds10() { test("STRENDS('abc', 'abcd')", FALSE) ; }
+ @Test public void exprStrEnds11() { test("STRENDS('abc'@en, 'bc')", TRUE) ; }
+ @Test public void exprStrEnds12() { test("STRENDS('abc'^^xsd:string, 'c')", TRUE) ; }
+ @Test public void exprStrEnds13() { test("STRENDS('abc'^^xsd:string, 'c'^^xsd:string)", TRUE) ; }
+ @Test public void exprStrEnds14() { test("STRENDS('abc', 'ab'^^xsd:string)", FALSE) ; }
+ @Test public void exprStrEnds15() { test("STRENDS('abc'@en, 'abc'@en)", TRUE) ; }
+
+ @Test public void exprStrEnds16() { testEvalException("STRENDS('ab'@en, 'ab'@fr)") ; }
+ @Test public void exprStrEnds17() { testEvalException("STRENDS(123, 'ab'@fr)") ; }
+ @Test public void exprStrEnds18() { testEvalException("STRENDS('123'^^xsd:string, 12.3)") ; }
+
@Test public void exprStrCase1() { test("fn:lower-case('aBc')", NodeValue.makeString("abc")) ; }
@Test public void exprStrCase2() { test("fn:lower-case('abc')", NodeValue.makeString("abc")) ; }
@Test public void exprStrCase3() { test("fn:upper-case('abc')", NodeValue.makeString("ABC")) ; }
@@ -91,7 +135,26 @@ public class TestFunctions
@Test public void exprStrContains5() { test("fn:contains('abc', 'bc')", TRUE) ; }
@Test public void exprStrContains6() { test("fn:contains('abc', 'abc')", TRUE) ; }
@Test public void exprStrContains7() { test("fn:contains('abc', 'Xc')", FALSE) ; }
- @Test public void exprStrContains8() { test("fn:contains('abc', 'Xa')", FALSE) ; }
+ @Test public void exprStrContains8() { test("fn:contains('abc', 'Xa')", FALSE) ; }
+
+ @Test public void exprContains10() { test("Contains('abc', 'abcd')", FALSE) ; }
+ @Test public void exprContains11() { test("Contains('abc'@en, 'bc')", TRUE) ; }
+ @Test public void exprContains12() { test("Contains('abc'^^xsd:string, 'c')", TRUE) ; }
+ @Test public void exprContains13() { test("Contains('abc'^^xsd:string, 'c'^^xsd:string)", TRUE) ; }
+ @Test public void exprContains14() { test("Contains('abc', 'z'^^xsd:string)", FALSE) ; }
+ @Test public void exprContains15() { test("Contains('abc'@en, 'abc'@en)", TRUE) ; }
+
+ @Test public void exprContains16() { testEvalException("Contains('ab'@en, 'ab'@fr)") ; }
+ @Test public void exprContains17() { testEvalException("Contains(123, 'ab'@fr)") ; }
+ @Test public void exprContains18() { testEvalException("STRENDS('123'^^xsd:string, 12.3)") ; }
+
+ @Test public void exprReplace1() { test("REPLACE('abc', 'b', 'Z')", NodeValue.makeString("aZc")) ; }
+ @Test public void exprReplace2() { test("REPLACE('abc', 'b.', 'Z')", NodeValue.makeString("aZ")) ; }
+ @Test public void exprReplace3() { test("REPLACE('abcbd', 'b.', 'Z')", NodeValue.makeString("aZZ")) ; }
+
+ @Test public void exprReplace4() { test("REPLACE('abcbd'^^xsd:string, 'b.', 'Z')", NodeValue.makeNode("aZZ", XSDDatatype.XSDstring)) ; }
+ @Test public void exprReplace5() { test("REPLACE('abcbd'@en, 'b.', 'Z')", NodeValue.makeNode("aZZ", "en", (String)null)) ; }
+ @Test public void exprReplace6() { test("REPLACE('abcbd', 'B.', 'Z', 'i')", NodeValue.makeString("aZZ")) ; }
@Test public void exprBoolean1() { test("fn:boolean('')", FALSE) ; }
@Test public void exprBoolean2() { test("fn:boolean(0)", FALSE) ; }