You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2022/03/25 10:14:52 UTC

[groovy] branch master updated: minor refactor: remove Sonar warning

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

paulk 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 240d330  minor refactor: remove Sonar warning
240d330 is described below

commit 240d33057243a6d7a03fdc502b1e72e0c0c59bd6
Author: Paul King <pa...@asert.com.au>
AuthorDate: Fri Mar 25 20:14:43 2022 +1000

    minor refactor: remove Sonar warning
---
 .../org/apache/groovy/xml/tools/DomToGroovy.java   | 68 ++++++++++++----------
 1 file changed, 37 insertions(+), 31 deletions(-)

diff --git a/subprojects/groovy-xml/src/main/java/org/apache/groovy/xml/tools/DomToGroovy.java b/subprojects/groovy-xml/src/main/java/org/apache/groovy/xml/tools/DomToGroovy.java
index b703b18..6ed377a 100644
--- a/subprojects/groovy-xml/src/main/java/org/apache/groovy/xml/tools/DomToGroovy.java
+++ b/subprojects/groovy-xml/src/main/java/org/apache/groovy/xml/tools/DomToGroovy.java
@@ -114,17 +114,19 @@ public class DomToGroovy {
     }
 
     public static Document parse(final Reader input) throws Exception {
-        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
-        factory.setNamespaceAware(true);
-        DocumentBuilder builder = factory.newDocumentBuilder();
-        return builder.parse(new InputSource(input));
+        return parse(new InputSource(input));
     }
 
     public static Document parse(final InputStream input) throws Exception {
+        return parse(new InputSource(input));
+    }
+
+    private static Document parse(InputSource is) throws Exception {
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
         factory.setNamespaceAware(true);
+        factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
         DocumentBuilder builder = factory.newDocumentBuilder();
-        return builder.parse(new InputSource(input));
+        return builder.parse(is);
     }
 
     protected void print(Node node, Map namespaces, boolean endWithComma) {
@@ -170,33 +172,37 @@ public class DomToGroovy {
         if (length == 0) {
             printEnd(")", endWithComma);
         } else {
-            Node node = list.item(0);
-            if (length == 1 && node instanceof Text) {
-                Text textNode = (Text) node;
-                String text = getTextNodeData(textNode);
-                if (hasAttributes) print(", ");
-                printQuoted(text);
-                printEnd(")", endWithComma);
-            } else if (mixedContent(list)) {
-                println(") {");
-                out.incrementIndent();
-                boolean oldInMixed = inMixed;
-                inMixed = true;
-                for (node = element.getFirstChild(); node != null; node = node.getNextSibling()) {
-                    print(node, namespaces, false);
-                }
-                inMixed = oldInMixed;
-                out.decrementIndent();
-                printIndent();
-                printEnd("}", endWithComma);
-            } else {
-                println(") {");
-                out.incrementIndent();
-                printChildren(element, namespaces);
-                out.decrementIndent();
-                printIndent();
-                printEnd("}", endWithComma);
+            printChildren(element, namespaces, endWithComma, hasAttributes, list, length);
+        }
+    }
+
+    private void printChildren(Element element, Map namespaces, boolean endWithComma, boolean hasAttributes, NodeList list, int length) {
+        Node node = list.item(0);
+        if (length == 1 && node instanceof Text) {
+            Text textNode = (Text) node;
+            String text = getTextNodeData(textNode);
+            if (hasAttributes) print(", ");
+            printQuoted(text);
+            printEnd(")", endWithComma);
+        } else if (mixedContent(list)) {
+            println(") {");
+            out.incrementIndent();
+            boolean oldInMixed = inMixed;
+            inMixed = true;
+            for (node = element.getFirstChild(); node != null; node = node.getNextSibling()) {
+                print(node, namespaces, false);
             }
+            inMixed = oldInMixed;
+            out.decrementIndent();
+            printIndent();
+            printEnd("}", endWithComma);
+        } else {
+            println(") {");
+            out.incrementIndent();
+            printChildren(element, namespaces);
+            out.decrementIndent();
+            printIndent();
+            printEnd("}", endWithComma);
         }
     }