Skip to content
Permalink
Browse files
minor refactor: remove Sonar warning
  • Loading branch information
paulk-asert committed Mar 25, 2022
1 parent 240d330 commit 72481efdaa46794e1bf644a072f257a881f746b8
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 33 deletions.
@@ -111,19 +111,12 @@ public static Document parse(Reader reader, boolean validating, boolean namespac
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.
*
@@ -47,6 +47,8 @@
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 XmlParser(boolean validating, boolean namespaceAware, boolean allowDocTyp
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 XmlParser(SAXParser parser) throws SAXException {
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.
*
@@ -51,6 +51,7 @@
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 XmlSlurper(final boolean validating, final boolean namespaceAware, boolea
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 XmlSlurper(final SAXParser parser) throws SAXException {
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.
@@ -28,7 +28,11 @@
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 static SAXParser newSAXParser(String schemaLanguage, Source... schemas) t
* @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 static SAXParser newSAXParser(String schemaLanguage, boolean namespaceAwa
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 String doCall(Character arg) {
}

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 @@ private static void setIndent(TransformerFactory factory, int indent) {
// 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) { }
}
}

0 comments on commit 72481ef

Please sign in to comment.