You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jena.apache.org by ales004 <gi...@git.apache.org> on 2016/04/01 08:19:27 UTC

[GitHub] jena pull request: afn:Sprintf implementation (JENA-967)

GitHub user ales004 opened a pull request:

    https://github.com/apache/jena/pull/132

    afn:Sprintf implementation (JENA-967)

    Hi,
    I implemented the afn:sprintf function. In order to implement it, I had to:
    
    - create a sprintf.java class in the sparql function part.
    
    - implement in the XSDFuncOp.java a function dealing with making sprintf
    
    - create a series of tests checking that the function is working correctly.
    
    While implementing, I realized that the example described in the bug is just one of the possibilities (printing numbers) as by the Java specification, String.Format works with all types. I thus implemented the function to work with all the type that made sense in my opinion (we can discuss about this).
    
    There is still one problem that I am not sure how to solve:
    Test 04 and 07 and 08 depend on the user locale (it will be printed as 1.2.. or 1,2.. or with date the day or month depend on the locale). In order to solve this, we will need to show to String.Format the current locale but I could not find a way to do this from the XSDFuncOp class.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/ales004/jena master

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/jena/pull/132.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #132
    
----
commit e2b7c9a9f1c922f8290c704465483e30f9178ef9
Author: ales004 <ci...@hotmail.com>
Date:   2016-03-17T20:09:47Z

    Correct a small typo in the TestFunctions2 file.

commit a0cea2c45a4bc9ed2cfb6601c253e702d9ff5751
Author: ales004 <ci...@hotmail.com>
Date:   2016-03-17T20:07:25Z

    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.

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] jena pull request: JENA-967: afn:Sprintf implementation

Posted by ales004 <gi...@git.apache.org>.
Github user ales004 commented on the pull request:

    https://github.com/apache/jena/pull/132#issuecomment-205996315
  
    Yes I think that the idea about the list is very good. I will try to implement it like this.
    
    I am still wondering what to do with the String.Format for Double, Float and Datetime that depend on the Locale. What do you think about this?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] jena pull request: JENA-967: afn:Sprintf implementation

Posted by ales004 <gi...@git.apache.org>.
Github user ales004 commented on a diff in the pull request:

    https://github.com/apache/jena/pull/132#discussion_r58619008
  
    --- Diff: jena-arq/src/main/java/org/apache/jena/sparql/expr/nodevalue/XSDFuncOp.java ---
    @@ -352,6 +352,60 @@ public static NodeValue javaSubstring(NodeValue nvString, NodeValue nvStart, Nod
             }
         }
     
    +    // 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)) ;
    --- End diff --
    
    Depending on the Locale: 1.23 can be printed as 1.23 or 1,23


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] jena pull request: JENA-967: afn:Sprintf implementation

Posted by ales004 <gi...@git.apache.org>.
Github user ales004 commented on the pull request:

    https://github.com/apache/jena/pull/132#issuecomment-208077486
  
    I think that commit bf14c982b8ae3eb8f040f55c28f8262efb5dad01 should do what you asked:
    
    - now also tests are Locale dependent.
    - sprintf accepts any number of arguments.
    - unkwnown classes are translated to string using the function you pointed out.
    
    What do you think?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] jena pull request: JENA-967: afn:Sprintf implementation

Posted by afs <gi...@git.apache.org>.
Github user afs commented on the pull request:

    https://github.com/apache/jena/pull/132#issuecomment-208295483
  
    Looks good and I've merged it into master. Thanks!
    
    (I've added t the documentation for the function library.)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] jena pull request: JENA-967: afn:Sprintf implementation

Posted by afs <gi...@git.apache.org>.
Github user afs commented on the pull request:

    https://github.com/apache/jena/pull/132#issuecomment-205935091
  
    Hi - looks good 
    
    One suggestion : Java printf can take a variable number of arguments from zero upwards.
    
    It would be nice if `afn:sprintf("%d != %d", 1, 2)` worked.  Could the arguments (args 1 on) go into a list, and javaSprintf proces them into a a Object[] for String.format?



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] jena pull request: JENA-967: afn:Sprintf implementation

Posted by afs <gi...@git.apache.org>.
Github user afs commented on the pull request:

    https://github.com/apache/jena/pull/132#issuecomment-205936352
  
    >  These cases for the moment are not supported. Maybe we could treat them all like strings.
    
    Yes - `NodeFunction.str(NodeValue)` can be used to get the lexical form.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] jena pull request: JENA-967: afn:Sprintf implementation

Posted by afs <gi...@git.apache.org>.
Github user afs commented on the pull request:

    https://github.com/apache/jena/pull/132#issuecomment-206374357
  
    For now, I think that "local locale" is the best single choice because if the user wants the XSD standard form "1.23" they can workaround it with a call `str()` and print a string. Not perfect but gives them a way.
    
    XSD F&O v3.1 has "picture strings" : https://www.w3.org/TR/xpath-functions-3/#dt-picture-string : that looks like Java DecimalFormat, not printf style.
    
    I think most people are familiar with printf and things like padding strings and alignment are covered. We can refine the capabilities after the machinery is in.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] jena pull request: JENA-967: afn:Sprintf implementation

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/jena/pull/132


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---