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 2020/04/11 14:03:42 UTC

[groovy] branch master updated: Trivial refactoring: extract common code

This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new f885f10  Trivial refactoring: extract common code
f885f10 is described below

commit f885f10441a6fe666e03caeca5feb2d9a1eda4a6
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sat Apr 11 22:03:27 2020 +0800

    Trivial refactoring: extract common code
---
 .../src/main/groovy/groovy/xml/StreamingDOMBuilder.groovy     | 10 +---------
 .../src/main/groovy/groovy/xml/StreamingSAXBuilder.groovy     |  9 +--------
 .../streamingmarkupsupport/AbstractStreamingBuilder.groovy    | 11 +++++++++++
 3 files changed, 13 insertions(+), 17 deletions(-)

diff --git a/subprojects/groovy-xml/src/main/groovy/groovy/xml/StreamingDOMBuilder.groovy b/subprojects/groovy-xml/src/main/groovy/groovy/xml/StreamingDOMBuilder.groovy
index 8f61ec2..8441f7a 100644
--- a/subprojects/groovy-xml/src/main/groovy/groovy/xml/StreamingDOMBuilder.groovy
+++ b/subprojects/groovy-xml/src/main/groovy/groovy/xml/StreamingDOMBuilder.groovy
@@ -37,15 +37,7 @@ class StreamingDOMBuilder extends AbstractStreamingBuilder {
         attrs.each {target, instruction ->
             def pi = null
             if (instruction instanceof Map) {
-                def buf = new StringBuffer()
-                instruction.each { name, value ->
-                    if (value.toString().contains('"')) {
-                        buf.append(" $name='$value'")
-                    } else {
-                        buf.append(" $name=\"$value\"" )
-                    }
-                }
-                pi = dom.document.createProcessingInstruction(target, buf.toString())
+                pi = dom.document.createProcessingInstruction(target, toMapString(instruction))
             } else {
                 pi = dom.document.createProcessingInstruction(target, instruction)
             }
diff --git a/subprojects/groovy-xml/src/main/groovy/groovy/xml/StreamingSAXBuilder.groovy b/subprojects/groovy-xml/src/main/groovy/groovy/xml/StreamingSAXBuilder.groovy
index 05faf23..917d1ba 100644
--- a/subprojects/groovy-xml/src/main/groovy/groovy/xml/StreamingSAXBuilder.groovy
+++ b/subprojects/groovy-xml/src/main/groovy/groovy/xml/StreamingSAXBuilder.groovy
@@ -35,14 +35,7 @@ class StreamingSAXBuilder extends AbstractStreamingBuilder {
     def piClosure = {doc, pendingNamespaces, namespaces, namespaceSpecificTags, prefix, attrs, body, contentHandler ->
         attrs.each {target, instruction ->
             if (instruction instanceof Map) {
-                def buf = new StringBuffer()
-                instruction.each {name, value ->
-                    if (value.toString().contains('"'))
-                        buf.append(" $name='$value'")
-                    else
-                        buf.append(" $name=\"$value\"")
-                }
-                contentHandler.processingInstruction(target, buf.toString())
+                contentHandler.processingInstruction(target, toMapString(instruction))
             } else {
                 contentHandler.processingInstruction(target, instruction)
             }
diff --git a/subprojects/groovy-xml/src/main/groovy/groovy/xml/streamingmarkupsupport/AbstractStreamingBuilder.groovy b/subprojects/groovy-xml/src/main/groovy/groovy/xml/streamingmarkupsupport/AbstractStreamingBuilder.groovy
index a49d4fe..91a3057 100644
--- a/subprojects/groovy-xml/src/main/groovy/groovy/xml/streamingmarkupsupport/AbstractStreamingBuilder.groovy
+++ b/subprojects/groovy-xml/src/main/groovy/groovy/xml/streamingmarkupsupport/AbstractStreamingBuilder.groovy
@@ -62,6 +62,17 @@ class AbstractStreamingBuilder {
     }
     def getNamespaceClosure = {doc, pendingNamespaces, namespaces, Object[] rest -> [namespaces, pendingNamespaces]}
 
+    def toMapString = { Map instruction ->
+        def buf = new StringBuilder()
+        instruction.each { name, value ->
+            if (value.toString().contains('"'))
+                buf.append(" $name='$value'")
+            else
+                buf.append(" $name=\"$value\"")
+        }
+        return buf.toString()
+    }
+
     def specialTags = ['declareNamespace':namespaceSetupClosure,
                            'declareAlias':aliasSetupClosure,
                           'getNamespaces':getNamespaceClosure]