jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject [2/5] jena git commit: Implemented the afn:sprintf function that uses the String.format Java function to format an input value to string. In the current implementation, the input value can be a number, a string, a date or a datetime value. Other types ar
Date Mon, 11 Apr 2016 11:19:20 GMT
Implemented the afn:sprintf function that uses the String.format Java function to format an
input value to string. In the current implementation, the input value can be a number, a string,
a date or a datetime value. Other types are not accepted.


Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/a0cea2c4
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/a0cea2c4
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/a0cea2c4

Branch: refs/heads/master
Commit: a0cea2c45a4bc9ed2cfb6601c253e702d9ff5751
Parents: e2b7c9a
Author: ales004 <cicerone54@hotmail.com>
Authored: Thu Mar 17 21:07:25 2016 +0100
Committer: ales004 <cicerone54@hotmail.com>
Committed: Fri Apr 1 08:09:10 2016 +0200

----------------------------------------------------------------------
 .../jena/sparql/expr/nodevalue/XSDFuncOp.java   | 54 ++++++++++++++++++++
 .../jena/sparql/function/library/sprintf.java   | 37 ++++++++++++++
 .../apache/jena/sparql/expr/TestFunctions.java  | 18 ++++++-
 3 files changed, 108 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/a0cea2c4/jena-arq/src/main/java/org/apache/jena/sparql/expr/nodevalue/XSDFuncOp.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/expr/nodevalue/XSDFuncOp.java b/jena-arq/src/main/java/org/apache/jena/sparql/expr/nodevalue/XSDFuncOp.java
index 7d95c12..f3ddbde 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/expr/nodevalue/XSDFuncOp.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/expr/nodevalue/XSDFuncOp.java
@@ -352,6 +352,60 @@ public class XSDFuncOp
         }
     }
 
+    // expecting nvString = format | nvStart = value (int,float, string,....)
+    public static NodeValue javaSprintf(NodeValue nvFormat, NodeValue nvValue) {
+        try {
+            String formatForOutput = nvFormat.getString() ;
+            ValueSpaceClassification vlSpClass = nvValue.getValueSpace();
+            switch(vlSpClass){
+                case VSPACE_NUM:
+                    NumericType type = classifyNumeric("javaSprintf",nvValue);
+                    switch(type) {
+                        case OP_DECIMAL:
+                            BigDecimal decimalValue = nvValue.getDecimal();
+                            return NodeValue.makeString(String.format(formatForOutput,decimalValue))
;
+                        case OP_INTEGER:
+                            BigInteger integerValue = nvValue.getInteger();
+                            return NodeValue.makeString(String.format(formatForOutput,integerValue))
;
+                        case OP_DOUBLE:
+                            Double doubValue = nvValue.getDouble();
+                            return NodeValue.makeString(String.format(formatForOutput,doubValue))
;
+                        case OP_FLOAT:
+                            Float floatValue = nvValue.getFloat();
+                            return NodeValue.makeString(String.format(formatForOutput,floatValue))
;
+                    }
+                case VSPACE_DATE:
+                case VSPACE_DATETIME:
+                    XMLGregorianCalendar gregorianCalendarValue = nvValue.getDateTime();
+                    return NodeValue.makeString(String.format(formatForOutput,gregorianCalendarValue.toGregorianCalendar().getTime()))
;
+                case VSPACE_STRING:
+                    String strValue = nvValue.getString();
+                    return NodeValue.makeString(String.format(formatForOutput,strValue));
+                case VSPACE_BOOLEAN:
+                    Boolean boolValue = nvValue.getBoolean();
+                    return NodeValue.makeString(String.format(formatForOutput,boolValue));
+                case VSPACE_LANG:
+                    String langValue = nvValue.getLang();
+                    return NodeValue.makeString(String.format(formatForOutput,langValue));
+                default:
+/*              These cases for the moment are not supported. Maybe we could treat them all
like strings.
+                case VSPACE_NODE:
+                case VSPACE_TIME:
+                case VSPACE_G_DAY:
+                case VSPACE_G_MONTH:
+                case VSPACE_G_MONTHDAY:
+                case VSPACE_G_YEAR:
+                case VSPACE_G_YEARMONTH:
+                case VSPACE_DURATION:
+                case VSPACE_UNKNOWN:
+*/
+                    throw new ARQInternalErrorException("Unrecognized sprintf type, value:
"+nvValue);
+            }
+        } catch (IndexOutOfBoundsException ex) {
+            throw new ExprEvalException("IndexOutOfBounds", ex) ;
+        }
+    }
+
     public static NodeValue strlen(NodeValue nvString) {
         Node n = checkAndGetStringLiteral("strlen", nvString) ;
         String str = n.getLiteralLexicalForm();

http://git-wip-us.apache.org/repos/asf/jena/blob/a0cea2c4/jena-arq/src/main/java/org/apache/jena/sparql/function/library/sprintf.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/function/library/sprintf.java b/jena-arq/src/main/java/org/apache/jena/sparql/function/library/sprintf.java
new file mode 100644
index 0000000..9a68149
--- /dev/null
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/function/library/sprintf.java
@@ -0,0 +1,37 @@
+package org.apache.jena.sparql.function.library;
+
+import org.apache.jena.atlas.lib.Lib;
+import org.apache.jena.query.QueryBuildException;
+import org.apache.jena.sparql.expr.ExprEvalException;
+import org.apache.jena.sparql.expr.ExprList;
+import org.apache.jena.sparql.expr.NodeValue;
+import org.apache.jena.sparql.expr.nodevalue.XSDFuncOp;
+import org.apache.jena.sparql.function.FunctionBase;
+
+import java.util.List;
+
+/** sprintf(string,string) - Java style */
+
+public class sprintf extends FunctionBase
+{
+    public sprintf() { super() ; }
+
+    @Override
+    public void checkBuild(String uri, ExprList args) {
+        if ( args.size() != 2)
+            throw new QueryBuildException("Function '"+ Lib.className(this)+"' takes two
or three arguments") ;
+    }
+
+    @Override
+    public NodeValue exec(List<NodeValue> args) {
+        if ( args.size() != 2 )
+            throw new ExprEvalException(Lib.className(this)+": Wrong number of arguments:
"+
+                    args.size()+" : [wanted 2]") ;
+
+        NodeValue v1 = args.get(0) ;
+        NodeValue v2 = args.get(1) ;
+
+        return XSDFuncOp.javaSprintf(v1, v2) ;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/jena/blob/a0cea2c4/jena-arq/src/test/java/org/apache/jena/sparql/expr/TestFunctions.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/test/java/org/apache/jena/sparql/expr/TestFunctions.java b/jena-arq/src/test/java/org/apache/jena/sparql/expr/TestFunctions.java
index db23201..535ea5f 100644
--- a/jena-arq/src/test/java/org/apache/jena/sparql/expr/TestFunctions.java
+++ b/jena-arq/src/test/java/org/apache/jena/sparql/expr/TestFunctions.java
@@ -71,7 +71,23 @@ public class TestFunctions
     @Test public void exprJavaSubstring4() { test("<"+ARQConstants.ARQFunctionLibrary+"substr>('abc',0,1)",
NodeValue.makeString("a")) ; }
     // Test from JENA-785
     @Test public void exprJavaSubstring5() { test("afn:substr('𐐈𐑌𐐻𐐪𐑉𐐿𐐻𐐮𐐿𐐲',
0, 1)", NodeValue.makeString("𐐈")) ; }
-    
+
+    // SPRINTF
+    @Test public void exprSprintf_01()      { test("afn:sprintf('%06d', 11)",NodeValue.makeString("000011"))
; }
+    @Test public void exprSprintf_02()      { test("afn:sprintf('%s', 'abcdefghi')",NodeValue.makeString("abcdefghi"))
; }
+    @Test public void exprSprintf_03()      { test("afn:sprintf('sometext %s', 'abcdefghi')",NodeValue.makeString("sometext
abcdefghi")) ; }
+    @Test public void exprSprintf_04()      { test("afn:sprintf('%1$tm %1$te,%1$tY', '2016-03-17'^^xsd:date)",NodeValue.makeString("03
17,2016")) ; }
+    @Test public void exprSprintf_05()      {
+            String nodeStr = NodeValue.makeDateTime("2005-10-14T13:09:43Z").toString();
+            test("afn:sprintf('%1$tm %1$te,%1$tY', "+nodeStr+")",NodeValue.makeString("10
14,2005")) ;
+    }
+    @Test public void exprSprintf_06()      { test("afn:sprintf('this is %s', 'false'^^xsd:boolean)",NodeValue.makeString("this
is false")) ; }
+    @Test public void exprSprintf_07()      { test("afn:sprintf('this number is equal to
%.2f', '11.22'^^xsd:decimal)",NodeValue.makeString("this number is equal to 11.22")) ; }
+    @Test public void exprSprintf_08()      { test("afn:sprintf('%.3f', '1.23456789'^^xsd:float)",NodeValue.makeString("1.235"))
; }
+    @Test public void exprSprintf_09()      { test("afn:sprintf('this number is equal to
%o in the octal system', '11'^^xsd:integer)",NodeValue.makeString("this number is equal to
13 in the octal system")) ; }
+    @Test public void exprSprintf_10()      { test("afn:sprintf('this number is equal to
%.5f', '1.23456789'^^xsd:double)",NodeValue.makeString("this number is equal to 1.23457"))
; }
+
+
     @Test public void exprStrStart0() { test("fn:starts-with('abc', '')", TRUE) ; }
     @Test public void exprStrStart1() { test("fn:starts-with('abc', 'a')", TRUE) ; }
     @Test public void exprStrStart2() { test("fn:starts-with('abc', 'ab')", TRUE) ; }


Mime
View raw message