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 14:47:34 UTC
[2/2] groovy git commit: Safe number parsing methods for more
convenient XML parsing with GPathResult(closes #494)
Safe number parsing methods for more convenient XML parsing with GPathResult(closes #494)
(cherry picked from commit 746e501)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/d35d6b86
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/d35d6b86
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/d35d6b86
Branch: refs/heads/GROOVY_2_6_X
Commit: d35d6b8623e44e91f4f77a0c6f92c5290d73180b
Parents: 178a13b
Author: Morten Holm Søby <mh...@nineconsult.dk>
Authored: Thu Feb 9 20:03:55 2017 +0800
Committer: danielsun1106 <re...@hotmail.com>
Committed: Wed Mar 14 22:47:29 2018 +0800
----------------------------------------------------------------------
.../groovy/util/slurpersupport/GPathResult.java | 22 +++++++++++
.../groovy/util/SafeNumberParsingTest.groovy | 39 ++++++++++++++++++++
2 files changed, 61 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/d35d6b86/subprojects/groovy-xml/src/main/java/groovy/util/slurpersupport/GPathResult.java
----------------------------------------------------------------------
diff --git a/subprojects/groovy-xml/src/main/java/groovy/util/slurpersupport/GPathResult.java b/subprojects/groovy-xml/src/main/java/groovy/util/slurpersupport/GPathResult.java
index 47dec63..8a0f08c 100644
--- a/subprojects/groovy-xml/src/main/java/groovy/util/slurpersupport/GPathResult.java
+++ b/subprojects/groovy-xml/src/main/java/groovy/util/slurpersupport/GPathResult.java
@@ -282,6 +282,9 @@ public abstract class GPathResult extends GroovyObjectSupport implements Writabl
* @return the GPathResult, converted to a <code>Integer</code>
*/
public Integer toInteger() {
+ if(textIsEmptyOrNull()){
+ return null;
+ }
return StringGroovyMethods.toInteger(text());
}
@@ -291,6 +294,9 @@ public abstract class GPathResult extends GroovyObjectSupport implements Writabl
* @return the GPathResult, converted to a <code>Long</code>
*/
public Long toLong() {
+ if(textIsEmptyOrNull()){
+ return null;
+ }
return StringGroovyMethods.toLong(text());
}
@@ -300,6 +306,9 @@ public abstract class GPathResult extends GroovyObjectSupport implements Writabl
* @return the GPathResult, converted to a <code>Float</code>
*/
public Float toFloat() {
+ if(textIsEmptyOrNull()){
+ return null;
+ }
return StringGroovyMethods.toFloat(text());
}
@@ -309,6 +318,9 @@ public abstract class GPathResult extends GroovyObjectSupport implements Writabl
* @return the GPathResult, converted to a <code>Double</code>
*/
public Double toDouble() {
+ if(textIsEmptyOrNull()){
+ return null;
+ }
return StringGroovyMethods.toDouble(text());
}
@@ -318,6 +330,9 @@ public abstract class GPathResult extends GroovyObjectSupport implements Writabl
* @return the GPathResult, converted to a <code>BigDecimal</code>
*/
public BigDecimal toBigDecimal() {
+ if(textIsEmptyOrNull()){
+ return null;
+ }
return StringGroovyMethods.toBigDecimal(text());
}
@@ -327,9 +342,16 @@ public abstract class GPathResult extends GroovyObjectSupport implements Writabl
* @return the GPathResult, converted to a <code>BigInteger</code>
*/
public BigInteger toBigInteger() {
+ if(textIsEmptyOrNull()){
+ return null;
+ }
return StringGroovyMethods.toBigInteger(text());
}
+ private boolean textIsEmptyOrNull() {
+ return text() == null || text().equals("");
+ }
+
/**
* Converts the text of this GPathResult to a URL object.
*
http://git-wip-us.apache.org/repos/asf/groovy/blob/d35d6b86/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
new file mode 100644
index 0000000..b69cd5b
--- /dev/null
+++ b/subprojects/groovy-xml/src/test/groovy/groovy/util/SafeNumberParsingTest.groovy
@@ -0,0 +1,39 @@
+package groovy.util
+
+
+class SafeNumberParsingTest 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 XmlSlurper().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
+ }
+}