groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [1/2] groovy git commit: Safe number parsing methods for more convenient XML parsing(closes #493)
Date Wed, 14 Mar 2018 15:10:29 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_5_X 0a4bd5e99 -> 0c82e1ced


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

(cherry picked from commit 61fd8d9)


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

Branch: refs/heads/GROOVY_2_5_X
Commit: b3503a790ec96a2adcf0b64436413b0c5ac1e0e6
Parents: 0a4bd5e
Author: Morten Holm Søby <mhs@nineconsult.dk>
Authored: Thu Feb 9 19:25:02 2017 +0800
Committer: danielsun1106 <realbluesun@hotmail.com>
Committed: Wed Mar 14 23:10:16 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/b3503a79/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/b3503a79/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
+    }
+}


Mime
View raw message