You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2018/03/14 15:07:54 UTC

[1/2] groovy git commit: Safe number parsing methods for more convenient XML parsing(closes #493)

Repository: groovy
Updated Branches:
  refs/heads/master 500f613cd -> d28df8f6e


Safe number parsing methods for more convenient XML parsing(closes #493)


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

Branch: refs/heads/master
Commit: 61fd8d97d366f21d7919e84684f65c83c16c6ffe
Parents: 500f613
Author: Morten Holm Søby <mh...@nineconsult.dk>
Authored: Thu Feb 9 12:25:02 2017 +0100
Committer: danielsun1106 <re...@hotmail.com>
Committed: Wed Mar 14 23:06:21 2018 +0800

----------------------------------------------------------------------
 src/main/groovy/groovy/util/Node.java           | 81 ++++++++++++++++++++
 .../util/SafeNumberParsingNodeTest.groovy       | 38 +++++++++
 2 files changed, 119 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/61fd8d97/src/main/groovy/groovy/util/Node.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/groovy/util/Node.java b/src/main/groovy/groovy/util/Node.java
index 76fdcca..8a53aa2 100644
--- a/src/main/groovy/groovy/util/Node.java
+++ b/src/main/groovy/groovy/util/Node.java
@@ -26,10 +26,13 @@ import groovy.lang.Tuple2;
 import groovy.xml.QName;
 import org.codehaus.groovy.runtime.DefaultGroovyMethods;
 import org.codehaus.groovy.runtime.InvokerHelper;
+import org.codehaus.groovy.runtime.StringGroovyMethods;
 import org.codehaus.groovy.util.ListHashMap;
 
 import java.io.PrintWriter;
 import java.io.Serializable;
+import java.math.BigDecimal;
+import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
@@ -784,4 +787,82 @@ public class Node implements Serializable, Cloneable {
     public void print(PrintWriter out) {
         new NodePrinter(out).print(this);
     }
+
+
+    /**
+     * Converts the text of this GPathResult to a Integer object.
+     *
+     * @return the GPathResult, converted to a <code>Integer</code>
+     */
+    public Integer toInteger() {
+        if(textIsEmptyOrNull()){
+            return null;
+        }
+        return StringGroovyMethods.toInteger(text());
+    }
+
+    /**
+     * Converts the text of this GPathResult to a Long object.
+     *
+     * @return the GPathResult, converted to a <code>Long</code>
+     */
+    public Long toLong() {
+        if(textIsEmptyOrNull()){
+            return null;
+        }
+        return StringGroovyMethods.toLong(text());
+    }
+
+    /**
+     * Converts the text of this GPathResult to a Float object.
+     *
+     * @return the GPathResult, converted to a <code>Float</code>
+     */
+    public Float toFloat() {
+        if(textIsEmptyOrNull()){
+            return null;
+        }
+        return StringGroovyMethods.toFloat(text());
+    }
+
+    /**
+     * Converts the text of this GPathResult to a Double object.
+     *
+     * @return the GPathResult, converted to a <code>Double</code>
+     */
+    public Double toDouble() {
+        if(textIsEmptyOrNull()){
+            return null;
+        }
+        return StringGroovyMethods.toDouble(text());
+    }
+
+    /**
+     * Converts the text of this GPathResult to a BigDecimal object.
+     *
+     * @return the GPathResult, converted to a <code>BigDecimal</code>
+     */
+    public BigDecimal toBigDecimal() {
+        if(textIsEmptyOrNull()){
+            return null;
+        }
+        return StringGroovyMethods.toBigDecimal(text());
+    }
+
+    /**
+     * Converts the text of this GPathResult to a BigInteger object.
+     *
+     * @return the GPathResult, converted to a <code>BigInteger</code>
+     */
+    public BigInteger toBigInteger() {
+        if(textIsEmptyOrNull()){
+            return null;
+        }
+        return StringGroovyMethods.toBigInteger(text());
+    }
+
+    private boolean textIsEmptyOrNull() {
+        String t = text();
+        return null == t || 0 == t.length();
+    }
 }

http://git-wip-us.apache.org/repos/asf/groovy/blob/61fd8d97/subprojects/groovy-xml/src/test/groovy/util/SafeNumberParsingNodeTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-xml/src/test/groovy/util/SafeNumberParsingNodeTest.groovy b/subprojects/groovy-xml/src/test/groovy/util/SafeNumberParsingNodeTest.groovy
new file mode 100644
index 0000000..6504479
--- /dev/null
+++ b/subprojects/groovy-xml/src/test/groovy/util/SafeNumberParsingNodeTest.groovy
@@ -0,0 +1,38 @@
+package util
+
+class SafeNumberParsingNodeTest extends GroovyTestCase {
+
+    void testSafetyWhenConvertingToNumbers() {
+        def xmlText = '''
+                <someNumberValues>
+                <someBigDecimal>123.4</someBigDecimal>
+                <someEmptyBigDecimal></someEmptyBigDecimal>
+                <someLong>123</someLong>
+                <someEmptyLong></someEmptyLong>
+                <someFloat>123.4</someFloat>
+                <someEmptyFloat></someEmptyFloat>
+                <someDouble>123.4</someDouble>
+                <someEmptyDouble></someEmptyDouble>
+                <someInteger>123</someInteger>
+                <someEmptyInteger></someEmptyInteger>
+            </someNumberValues>
+                '''
+        def xml = new XmlParser().parseText(xmlText)
+
+        assert xml.'**'.find { it.name() == 'someBigDecimal' }.toBigDecimal() == 123.4
+        assert xml.'**'.find { it.name() == 'someEmptyBigDecimal' }.toBigDecimal() == null
+        assert xml.'**'.find { it.name() == 'someMissingBigDecimal' }?.toBigDecimal() == null
+        assert xml.'**'.find { it.name() == 'someLong' }.toLong() == 123
+        assert xml.'**'.find { it.name() == 'someEmptyLong' }.toLong() == null
+        assert xml.'**'.find { it.name() == 'someMissingLong' }?.toLong() == null
+        assert xml.'**'.find { it.name() == 'someFloat' }.toFloat() == 123.4.toFloat()
+        assert xml.'**'.find { it.name() == 'someEmptyFloat' }.toFloat() == null
+        assert xml.'**'.find { it.name() == 'someMissingFloat' }?.toFloat() == null
+        assert xml.'**'.find { it.name() == 'someDouble' }.toDouble() == 123.4.toDouble()
+        assert xml.'**'.find { it.name() == 'someEmptyDouble' }.toDouble() == null
+        assert xml.'**'.find { it.name() == 'someMissingDouble' }?.toDouble() == null
+        assert xml.'**'.find { it.name() == 'someInteger' }.toInteger() == 123
+        assert xml.'**'.find { it.name() == 'someEmptyInteger' }.toInteger() == null
+        assert xml.'**'.find { it.name() == 'someMissingInteger' }?.toInteger() == null
+    }
+}


[2/2] groovy git commit: Add license header

Posted by su...@apache.org.
Add license header


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

Branch: refs/heads/master
Commit: d28df8f6e41a5abb35ed0e543035d121cd2edb10
Parents: 61fd8d9
Author: danielsun1106 <re...@hotmail.com>
Authored: Wed Mar 14 23:07:39 2018 +0800
Committer: danielsun1106 <re...@hotmail.com>
Committed: Wed Mar 14 23:07:39 2018 +0800

----------------------------------------------------------------------
 .../groovy/util/SafeNumberParsingTest.groovy     | 19 +++++++++++++++++++
 .../groovy/util/SafeNumberParsingNodeTest.groovy | 19 +++++++++++++++++++
 2 files changed, 38 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/d28df8f6/subprojects/groovy-xml/src/test/groovy/groovy/util/SafeNumberParsingTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-xml/src/test/groovy/groovy/util/SafeNumberParsingTest.groovy b/subprojects/groovy-xml/src/test/groovy/groovy/util/SafeNumberParsingTest.groovy
index b69cd5b..40e5f14 100644
--- a/subprojects/groovy-xml/src/test/groovy/groovy/util/SafeNumberParsingTest.groovy
+++ b/subprojects/groovy-xml/src/test/groovy/groovy/util/SafeNumberParsingTest.groovy
@@ -1,3 +1,22 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
 package groovy.util
 
 

http://git-wip-us.apache.org/repos/asf/groovy/blob/d28df8f6/subprojects/groovy-xml/src/test/groovy/util/SafeNumberParsingNodeTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-xml/src/test/groovy/util/SafeNumberParsingNodeTest.groovy b/subprojects/groovy-xml/src/test/groovy/util/SafeNumberParsingNodeTest.groovy
index 6504479..520cc5f 100644
--- a/subprojects/groovy-xml/src/test/groovy/util/SafeNumberParsingNodeTest.groovy
+++ b/subprojects/groovy-xml/src/test/groovy/util/SafeNumberParsingNodeTest.groovy
@@ -1,3 +1,22 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
 package util
 
 class SafeNumberParsingNodeTest extends GroovyTestCase {