Add utility to create a org.xml.sax.XMLReader
Closes gh-1471
This commit is contained in:
@@ -20,9 +20,14 @@ import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.parsers.SAXParserFactory;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.XMLReader;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
@@ -36,6 +41,22 @@ public abstract class SaxUtils {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(SaxUtils.class);
|
||||
|
||||
/**
|
||||
* Create a default {@link XMLReader} that is
|
||||
* {@linkplain SAXParserFactory#setNamespaceAware(boolean) namespace aware}.
|
||||
* @return a new {@link XMLReader}
|
||||
*/
|
||||
public static XMLReader namespaceAwareXmlReader() throws SAXException {
|
||||
try {
|
||||
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
|
||||
parserFactory.setNamespaceAware(true);
|
||||
return parserFactory.newSAXParser().getXMLReader();
|
||||
}
|
||||
catch (ParserConfigurationException ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a SAX {@code InputSource} from the given resource. Sets the system
|
||||
* identifier to the resource's {@code URL}, if available.
|
||||
|
||||
@@ -27,6 +27,7 @@ import org.xml.sax.XMLReader;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.xml.sax.SaxUtils;
|
||||
import org.springframework.xml.transform.ResourceSource;
|
||||
|
||||
/**
|
||||
@@ -66,7 +67,7 @@ public abstract class SchemaLoaderUtils {
|
||||
Assert.notEmpty(resources, "No resources given");
|
||||
Assert.hasLength(schemaLanguage, "No schema language provided");
|
||||
Source[] schemaSources = new Source[resources.length];
|
||||
XMLReader xmlReader = XMLReaderFactoryUtils.createXMLReader();
|
||||
XMLReader xmlReader = offlinerXmlReader();
|
||||
xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
|
||||
for (int i = 0; i < resources.length; i++) {
|
||||
Assert.notNull(resources[i], "Resource is null");
|
||||
@@ -77,6 +78,15 @@ public abstract class SchemaLoaderUtils {
|
||||
return schemaFactory.newSchema(schemaSources);
|
||||
}
|
||||
|
||||
private static XMLReader offlinerXmlReader() throws SAXException {
|
||||
XMLReader xmlReader = SaxUtils.namespaceAwareXmlReader();
|
||||
xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
||||
xmlReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
|
||||
xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", false);
|
||||
xmlReader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
|
||||
return xmlReader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the URL from the given resource as System ID. Returns {@code null} if it
|
||||
* cannot be opened.
|
||||
|
||||
@@ -16,19 +16,21 @@
|
||||
|
||||
package org.springframework.xml.validation;
|
||||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.parsers.SAXParser;
|
||||
import javax.xml.parsers.SAXParserFactory;
|
||||
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.XMLReader;
|
||||
|
||||
import org.springframework.xml.sax.SaxUtils;
|
||||
|
||||
/**
|
||||
* General utilities to create an {@link XMLReader}.
|
||||
*
|
||||
* @author Greg Turnquist
|
||||
* @since 3.0.5
|
||||
* @deprecated since 4.0.12 in favor of {@link SaxUtils}
|
||||
*/
|
||||
@Deprecated(since = "4.0.12", forRemoval = true)
|
||||
public abstract class XMLReaderFactoryUtils {
|
||||
|
||||
/**
|
||||
@@ -36,7 +38,7 @@ public abstract class XMLReaderFactoryUtils {
|
||||
* @see SAXParser#getXMLReader()
|
||||
*/
|
||||
public static XMLReader createXMLReader() throws SAXException {
|
||||
XMLReader xmlReader = namespaceAwareXmlReader();
|
||||
XMLReader xmlReader = SaxUtils.namespaceAwareXmlReader();
|
||||
xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
||||
xmlReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
|
||||
xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", false);
|
||||
@@ -44,16 +46,4 @@ public abstract class XMLReaderFactoryUtils {
|
||||
return xmlReader;
|
||||
}
|
||||
|
||||
private static XMLReader namespaceAwareXmlReader() throws SAXException {
|
||||
try {
|
||||
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
|
||||
parserFactory.setNamespaceAware(true);
|
||||
return parserFactory.newSAXParser().getXMLReader();
|
||||
}
|
||||
catch (ParserConfigurationException ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,7 +20,6 @@ import java.io.StringReader;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.SAXParserFactory;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -30,6 +29,7 @@ import org.xml.sax.InputSource;
|
||||
import org.xml.sax.XMLReader;
|
||||
|
||||
import org.springframework.xml.DocumentBuilderFactoryUtils;
|
||||
import org.springframework.xml.sax.SaxUtils;
|
||||
|
||||
import static org.xmlunit.assertj.XmlAssert.assertThat;
|
||||
|
||||
@@ -63,9 +63,7 @@ public class DomContentHandlerTest {
|
||||
documentBuilderFactory.setNamespaceAware(true);
|
||||
this.documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
||||
this.result = this.documentBuilder.newDocument();
|
||||
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
|
||||
parserFactory.setNamespaceAware(true);
|
||||
this.xmlReader = parserFactory.newSAXParser().getXMLReader();
|
||||
this.xmlReader = SaxUtils.namespaceAwareXmlReader();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -28,7 +28,6 @@ import java.nio.charset.StandardCharsets;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.SAXParserFactory;
|
||||
import javax.xml.stream.XMLEventReader;
|
||||
import javax.xml.stream.XMLEventWriter;
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
@@ -57,6 +56,7 @@ import org.xml.sax.helpers.DefaultHandler;
|
||||
import org.springframework.util.xml.StaxUtils;
|
||||
import org.springframework.xml.DocumentBuilderFactoryUtils;
|
||||
import org.springframework.xml.XMLInputFactoryUtils;
|
||||
import org.springframework.xml.sax.SaxUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
@@ -119,9 +119,7 @@ public class TraxUtilsTest {
|
||||
@Test
|
||||
public void testDoWithSaxSource() throws Exception {
|
||||
|
||||
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
|
||||
parserFactory.setNamespaceAware(true);
|
||||
XMLReader reader = parserFactory.newSAXParser().getXMLReader();
|
||||
XMLReader reader = SaxUtils.namespaceAwareXmlReader();
|
||||
InputSource inputSource = new InputSource();
|
||||
|
||||
TraxUtils.SourceCallback mock = createMock(TraxUtils.SourceCallback.class);
|
||||
|
||||
Reference in New Issue
Block a user