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:25 UTC

[groovy] branch GROOVY_4_0_X updated (09b1226 -> 258d40f)

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

paulk pushed a change to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git.


    from 09b1226  GROOVY-10557: add test case
     new e17ed9cc minor refactor: remove Sonar warning
     new c9e3e8e  minor refactor: remove Sonar warning
     new 258d40f  GROOVY-10560: Provide additional XmlUtil variants for more options when disabling doctypes

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../src/main/java/groovy/xml/DOMBuilder.java       |  11 +-
 .../src/main/java/groovy/xml/XmlParser.java        |  13 +-
 .../src/main/java/groovy/xml/XmlSlurper.java       |  12 +-
 .../src/main/java/groovy/xml/XmlUtil.java          | 150 ++++++++++++++++++---
 .../org/apache/groovy/xml/tools/DomToGroovy.java   |  68 +++++-----
 5 files changed, 179 insertions(+), 75 deletions(-)

[groovy] 03/03: GROOVY-10560: Provide additional XmlUtil variants for more options when disabling doctypes

Posted by pa...@apache.org.
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 258d40f3b851a2b68d98e0ecd12c5a9d1b261366
Author: Paul King <pa...@asert.com.au>
AuthorDate: Wed Mar 30 15:06:57 2022 +1000

    GROOVY-10560: Provide additional XmlUtil variants for more options when disabling doctypes
---
 .../src/main/java/groovy/xml/XmlUtil.java          | 97 +++++++++++++++++++---
 1 file changed, 86 insertions(+), 11 deletions(-)

diff --git a/subprojects/groovy-xml/src/main/java/groovy/xml/XmlUtil.java b/subprojects/groovy-xml/src/main/java/groovy/xml/XmlUtil.java
index 4a4d6f1..7492915 100644
--- a/subprojects/groovy-xml/src/main/java/groovy/xml/XmlUtil.java
+++ b/subprojects/groovy-xml/src/main/java/groovy/xml/XmlUtil.java
@@ -39,6 +39,7 @@ import javax.xml.parsers.SAXParserFactory;
 import javax.xml.transform.OutputKeys;
 import javax.xml.transform.Source;
 import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
 import javax.xml.transform.TransformerException;
 import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.dom.DOMSource;
@@ -67,8 +68,19 @@ public class XmlUtil {
      * @return the pretty String representation of the Element
      */
     public static String serialize(Element element) {
+        return serialize(element, false);
+    }
+
+    /**
+     * Return a pretty String version of the Element.
+     *
+     * @param element                 the Element to serialize
+     * @param allowDocTypeDeclaration whether to allow doctype processing
+     * @return the pretty String representation of the Element
+     */
+    public static String serialize(Element element, boolean allowDocTypeDeclaration) {
         Writer sw = new StringBuilderWriter();
-        serialize(new DOMSource(element), sw);
+        serialize(new DOMSource(element), sw, allowDocTypeDeclaration);
         return sw.toString();
     }
 
@@ -79,8 +91,19 @@ public class XmlUtil {
      * @param os      the OutputStream to write to
      */
     public static void serialize(Element element, OutputStream os) {
+        serialize(element, os, false);
+    }
+
+    /**
+     * Write a pretty version of the Element to the OutputStream.
+     *
+     * @param element                 the Element to serialize
+     * @param os                      the OutputStream to write to
+     * @param allowDocTypeDeclaration whether to allow doctype processing
+     */
+    public static void serialize(Element element, OutputStream os, boolean allowDocTypeDeclaration) {
         Source source = new DOMSource(element);
-        serialize(source, os);
+        serialize(source, os, allowDocTypeDeclaration);
     }
 
     /**
@@ -90,8 +113,19 @@ public class XmlUtil {
      * @param w       the Writer to write to
      */
     public static void serialize(Element element, Writer w) {
+        serialize(element, w, false);
+    }
+
+    /**
+     * Write a pretty version of the Element to the Writer.
+     *
+     * @param element                 the Element to serialize
+     * @param w                       the Writer to write to
+     * @param allowDocTypeDeclaration whether to allow doctype processing
+     */
+    public static void serialize(Element element, Writer w, boolean allowDocTypeDeclaration) {
         Source source = new DOMSource(element);
-        serialize(source, w);
+        serialize(source, w, allowDocTypeDeclaration);
     }
 
     /**
@@ -191,8 +225,19 @@ public class XmlUtil {
      * @return the pretty String representation of the original content
      */
     public static String serialize(String xmlString) {
+        return serialize(xmlString, false);
+    }
+
+    /**
+     * Return a pretty version of the XML content contained in the given String.
+     *
+     * @param xmlString               the String to serialize
+     * @param allowDocTypeDeclaration whether to allow doctype processing
+     * @return the pretty String representation of the original content
+     */
+    public static String serialize(String xmlString, boolean allowDocTypeDeclaration) {
         Writer sw = new StringBuilderWriter();
-        serialize(asStreamSource(xmlString), sw);
+        serialize(asStreamSource(xmlString), sw, allowDocTypeDeclaration);
         return sw.toString();
     }
 
@@ -203,7 +248,18 @@ public class XmlUtil {
      * @param os        the OutputStream to write to
      */
     public static void serialize(String xmlString, OutputStream os) {
-        serialize(asStreamSource(xmlString), os);
+        serialize(xmlString, os, false);
+    }
+
+    /**
+     * Write a pretty version of the given XML string to the OutputStream.
+     *
+     * @param xmlString               the String to serialize
+     * @param allowDocTypeDeclaration whether to allow doctype processing
+     * @param os                      the OutputStream to write to
+     */
+    public static void serialize(String xmlString, OutputStream os, boolean allowDocTypeDeclaration) {
+        serialize(asStreamSource(xmlString), os, allowDocTypeDeclaration);
     }
 
     /**
@@ -213,7 +269,18 @@ public class XmlUtil {
      * @param w         the Writer to write to
      */
     public static void serialize(String xmlString, Writer w) {
-        serialize(asStreamSource(xmlString), w);
+        serialize(xmlString, w, false);
+    }
+
+    /**
+     * Write a pretty version of the given XML string to the Writer.
+     *
+     * @param xmlString               the String to serialize
+     * @param allowDocTypeDeclaration whether to allow doctype processing
+     * @param w                       the Writer to write to
+     */
+    public static void serialize(String xmlString, Writer w, boolean allowDocTypeDeclaration) {
+        serialize(asStreamSource(xmlString), w, allowDocTypeDeclaration);
     }
 
     /**
@@ -463,16 +530,17 @@ public class XmlUtil {
         return new StreamSource(new StringReader(xmlString));
     }
 
-    private static void serialize(Source source, OutputStream os) {
-        serialize(source, new StreamResult(new OutputStreamWriter(os, StandardCharsets.UTF_8)));
+    private static void serialize(Source source, OutputStream os, boolean allowDocTypeDeclaration) {
+        serialize(source, new StreamResult(new OutputStreamWriter(os, StandardCharsets.UTF_8)), allowDocTypeDeclaration);
     }
 
-    private static void serialize(Source source, Writer w) {
-        serialize(source, new StreamResult(w));
+    private static void serialize(Source source, Writer w, boolean allowDocTypeDeclaration) {
+        serialize(source, new StreamResult(w), allowDocTypeDeclaration);
     }
 
-    private static void serialize(Source source, StreamResult target) {
+    private static void serialize(Source source, StreamResult target, boolean allowDocTypeDeclaration) {
         TransformerFactory factory = TransformerFactory.newInstance();
+        setFeatureQuietly(factory, "http://apache.org/xml/features/disallow-doctype-decl", !allowDocTypeDeclaration);
         setIndent(factory, 2);
         try {
             Transformer transformer = factory.newTransformer();
@@ -495,6 +563,13 @@ public class XmlUtil {
         }
     }
 
+    public static void setFeatureQuietly(TransformerFactory factory, String feature, boolean value) {
+        try {
+            factory.setFeature(feature, value);
+        }
+        catch (TransformerConfigurationException ignored) { }
+    }
+
     public static void setFeatureQuietly(DocumentBuilderFactory factory, String feature, boolean value) {
         try {
             factory.setFeature(feature, value);

[groovy] 02/03: minor refactor: remove Sonar warning

Posted by pa...@apache.org.
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 c9e3e8ea22b884207d0f11804c19f4da55d717da
Author: Paul King <pa...@asert.com.au>
AuthorDate: Fri Mar 25 20:50:27 2022 +1000

    minor refactor: remove Sonar warning
---
 .../src/main/java/groovy/xml/DOMBuilder.java       | 11 +----
 .../src/main/java/groovy/xml/XmlParser.java        | 13 ++----
 .../src/main/java/groovy/xml/XmlSlurper.java       | 12 ++---
 .../src/main/java/groovy/xml/XmlUtil.java          | 53 +++++++++++++++++++---
 4 files changed, 56 insertions(+), 33 deletions(-)

diff --git a/subprojects/groovy-xml/src/main/java/groovy/xml/DOMBuilder.java b/subprojects/groovy-xml/src/main/java/groovy/xml/DOMBuilder.java
index 1c96ff3..3a818f5 100644
--- a/subprojects/groovy-xml/src/main/java/groovy/xml/DOMBuilder.java
+++ b/subprojects/groovy-xml/src/main/java/groovy/xml/DOMBuilder.java
@@ -111,19 +111,12 @@ public class DOMBuilder extends BuilderSupport {
         DocumentBuilderFactory factory = FactorySupport.createDocumentBuilderFactory();
         factory.setNamespaceAware(namespaceAware);
         factory.setValidating(validating);
-        setQuietly(factory, XMLConstants.FEATURE_SECURE_PROCESSING, true);
-        setQuietly(factory, "http://apache.org/xml/features/disallow-doctype-decl", !allowDocTypeDeclaration);
+        XmlUtil.setFeatureQuietly(factory, XMLConstants.FEATURE_SECURE_PROCESSING, true);
+        XmlUtil.setFeatureQuietly(factory, "http://apache.org/xml/features/disallow-doctype-decl", !allowDocTypeDeclaration);
         DocumentBuilder documentBuilder = factory.newDocumentBuilder();
         return documentBuilder.parse(new InputSource(reader));
     }
 
-    private static void setQuietly(DocumentBuilderFactory factory, String feature, boolean value) {
-        try {
-            factory.setFeature(feature, value);
-        }
-        catch (ParserConfigurationException ignored) { }
-    }
-
     /**
      * A helper method to parse the given text as XML.
      *
diff --git a/subprojects/groovy-xml/src/main/java/groovy/xml/XmlParser.java b/subprojects/groovy-xml/src/main/java/groovy/xml/XmlParser.java
index ebc3a2e..976ad0f 100644
--- a/subprojects/groovy-xml/src/main/java/groovy/xml/XmlParser.java
+++ b/subprojects/groovy-xml/src/main/java/groovy/xml/XmlParser.java
@@ -47,6 +47,8 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
+import static groovy.xml.XmlUtil.setFeatureQuietly;
+
 /**
  * A helper class for parsing XML into a tree of Node instances for a
  * simple way of processing XML. This parser does not preserve the XML
@@ -115,8 +117,8 @@ public class XmlParser implements ContentHandler {
         factory.setNamespaceAware(namespaceAware);
         this.namespaceAware = namespaceAware;
         factory.setValidating(validating);
-        setQuietly(factory, XMLConstants.FEATURE_SECURE_PROCESSING, true);
-        setQuietly(factory, "http://apache.org/xml/features/disallow-doctype-decl", !allowDocTypeDeclaration);
+        setFeatureQuietly(factory, XMLConstants.FEATURE_SECURE_PROCESSING, true);
+        setFeatureQuietly(factory, "http://apache.org/xml/features/disallow-doctype-decl", !allowDocTypeDeclaration);
         reader = factory.newSAXParser().getXMLReader();
     }
 
@@ -128,13 +130,6 @@ public class XmlParser implements ContentHandler {
         reader = parser.getXMLReader();
     }
 
-    private static void setQuietly(SAXParserFactory factory, String feature, boolean value) {
-        try {
-            factory.setFeature(feature, value);
-        }
-        catch (ParserConfigurationException | SAXNotSupportedException | SAXNotRecognizedException ignored) { }
-    }
-
     /**
      * Returns the current trim whitespace setting.
      *
diff --git a/subprojects/groovy-xml/src/main/java/groovy/xml/XmlSlurper.java b/subprojects/groovy-xml/src/main/java/groovy/xml/XmlSlurper.java
index a87a1ca..c233cad 100644
--- a/subprojects/groovy-xml/src/main/java/groovy/xml/XmlSlurper.java
+++ b/subprojects/groovy-xml/src/main/java/groovy/xml/XmlSlurper.java
@@ -51,6 +51,7 @@ import java.util.HashMap;
 import java.util.Map;
 import java.util.Stack;
 
+import static groovy.xml.XmlUtil.setFeatureQuietly;
 
 /**
  * Parse XML into a document tree that may be traversed similar to XPath
@@ -129,8 +130,8 @@ public class XmlSlurper extends DefaultHandler {
         factory.setNamespaceAware(namespaceAware);
         this.namespaceAware = namespaceAware;
         factory.setValidating(validating);
-        setQuietly(factory, XMLConstants.FEATURE_SECURE_PROCESSING, true);
-        setQuietly(factory, "http://apache.org/xml/features/disallow-doctype-decl", !allowDocTypeDeclaration);
+        setFeatureQuietly(factory, XMLConstants.FEATURE_SECURE_PROCESSING, true);
+        setFeatureQuietly(factory, "http://apache.org/xml/features/disallow-doctype-decl", !allowDocTypeDeclaration);
         reader = factory.newSAXParser().getXMLReader();
     }
 
@@ -142,13 +143,6 @@ public class XmlSlurper extends DefaultHandler {
         this(parser.getXMLReader());
     }
 
-    private static void setQuietly(SAXParserFactory factory, String feature, boolean value) {
-        try {
-            factory.setFeature(feature, value);
-        }
-        catch (ParserConfigurationException | SAXNotSupportedException | SAXNotRecognizedException ignored) { }
-    }
-
     /**
      * @deprecated use setKeepIgnorableWhitespace
      * @param keepWhitespace If true then whitespace before elements is kept.
diff --git a/subprojects/groovy-xml/src/main/java/groovy/xml/XmlUtil.java b/subprojects/groovy-xml/src/main/java/groovy/xml/XmlUtil.java
index db8d8bb..4a4d6f1 100644
--- a/subprojects/groovy-xml/src/main/java/groovy/xml/XmlUtil.java
+++ b/subprojects/groovy-xml/src/main/java/groovy/xml/XmlUtil.java
@@ -28,7 +28,11 @@ import org.codehaus.groovy.runtime.InvokerHelper;
 import org.codehaus.groovy.runtime.StringGroovyMethods;
 import org.w3c.dom.Element;
 import org.xml.sax.SAXException;
+import org.xml.sax.SAXNotRecognizedException;
+import org.xml.sax.SAXNotSupportedException;
 
+import javax.xml.XMLConstants;
+import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.parsers.SAXParser;
 import javax.xml.parsers.SAXParserFactory;
@@ -243,9 +247,25 @@ public class XmlUtil {
      * @since 1.8.7
      */
     public static SAXParser newSAXParser(String schemaLanguage, boolean namespaceAware, boolean validating, Source... schemas) throws SAXException, ParserConfigurationException {
-        SAXParserFactory factory = SAXParserFactory.newInstance();
-        factory.setValidating(validating);
-        factory.setNamespaceAware(namespaceAware);
+        return newSAXParser(schemaLanguage, namespaceAware, validating, false, schemas);
+    }
+
+    /**
+     * Factory method to create a SAXParser configured to validate according to a particular schema language and
+     * optionally providing the schema sources to validate with.
+     *
+     * @param schemaLanguage   the schema language used, e.g. XML Schema or RelaxNG (as per the String representation in javax.xml.XMLConstants)
+     * @param namespaceAware   will the parser be namespace aware
+     * @param validating       will the parser also validate against DTDs
+     * @param allowDoctypeDecl whether to allow doctype declarations (potentially insecure)
+     * @param schemas          the schemas to validate against
+     * @return the created SAXParser
+     * @throws SAXException
+     * @throws ParserConfigurationException
+     * @since 3.0.11
+     */
+    public static SAXParser newSAXParser(String schemaLanguage, boolean namespaceAware, boolean validating, boolean allowDoctypeDecl, Source... schemas) throws SAXException, ParserConfigurationException {
+        SAXParserFactory factory = newFactoryInstance(namespaceAware, validating, allowDoctypeDecl);
         if (schemas.length != 0) {
             SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage);
             factory.setSchema(schemaFactory.newSchema(schemas));
@@ -257,6 +277,15 @@ public class XmlUtil {
         return saxParser;
     }
 
+    private static SAXParserFactory newFactoryInstance(boolean namespaceAware, boolean validating, boolean allowDoctypeDecl) {
+        SAXParserFactory factory = SAXParserFactory.newInstance();
+        factory.setValidating(validating);
+        factory.setNamespaceAware(namespaceAware);
+        setFeatureQuietly(factory, XMLConstants.FEATURE_SECURE_PROCESSING, true);
+        setFeatureQuietly(factory, "http://apache.org/xml/features/disallow-doctype-decl", !allowDoctypeDecl);
+        return factory;
+    }
+
     /**
      * Factory method to create a SAXParser configured to validate according to a particular schema language and
      * a File containing the schema to validate against.
@@ -389,9 +418,7 @@ public class XmlUtil {
     }
 
     private static SAXParser newSAXParser(boolean namespaceAware, boolean validating, Schema schema1) throws ParserConfigurationException, SAXException {
-        SAXParserFactory factory = SAXParserFactory.newInstance();
-        factory.setValidating(validating);
-        factory.setNamespaceAware(namespaceAware);
+        SAXParserFactory factory = newFactoryInstance(namespaceAware, validating, false);
         factory.setSchema(schema1);
         return factory.newSAXParser();
     }
@@ -467,4 +494,18 @@ public class XmlUtil {
             // ignore for factories that don't support this
         }
     }
+
+    public static void setFeatureQuietly(DocumentBuilderFactory factory, String feature, boolean value) {
+        try {
+            factory.setFeature(feature, value);
+        }
+        catch (ParserConfigurationException ignored) { }
+    }
+
+    public static void setFeatureQuietly(SAXParserFactory factory, String feature, boolean value) {
+        try {
+            factory.setFeature(feature, value);
+        }
+        catch (ParserConfigurationException | SAXNotSupportedException | SAXNotRecognizedException ignored) { }
+    }
 }
\ No newline at end of file

[groovy] 01/03: minor refactor: remove Sonar warning

Posted by pa...@apache.org.
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);
         }
     }