From commits-return-5850-archive-asf-public=cust-asf.ponee.io@groovy.apache.org Wed Mar 14 16:10:31 2018 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id 39C4F180654 for ; Wed, 14 Mar 2018 16:10:30 +0100 (CET) Received: (qmail 84572 invoked by uid 500); 14 Mar 2018 15:10:29 -0000 Mailing-List: contact commits-help@groovy.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@groovy.apache.org Delivered-To: mailing list commits@groovy.apache.org Received: (qmail 84558 invoked by uid 99); 14 Mar 2018 15:10:29 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 14 Mar 2018 15:10:29 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 1DAA7E96E4; Wed, 14 Mar 2018 15:10:29 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: sunlan@apache.org To: commits@groovy.apache.org Date: Wed, 14 Mar 2018 15:10:29 -0000 Message-Id: <9db6798fa9384fc098a1579a397ab1aa@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [1/2] groovy git commit: Safe number parsing methods for more convenient XML parsing(closes #493) 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 Authored: Thu Feb 9 19:25:02 2017 +0800 Committer: danielsun1106 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 Integer + */ + 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 Long + */ + 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 Float + */ + 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 Double + */ + 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 BigDecimal + */ + 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 BigInteger + */ + 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 = ''' + + 123.4 + + 123 + + 123.4 + + 123.4 + + 123 + + + ''' + 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 + } +}