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

[groovy] 02/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 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