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/31 08:27:26 UTC
[groovy] 01/03: minor refactor: remove Sonar warning
This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit e17ed9cce269f39f43293a7453d3927a9d0534fd
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);
}
}