Skip to content

Commit 57e3880

Browse files
committed
Add utility to create a org.xml.sax.XMLReader
Closes gh-1471
1 parent 51964f9 commit 57e3880

File tree

9 files changed

+49
-42
lines changed

9 files changed

+49
-42
lines changed

spring-ws-core/src/main/java/org/springframework/ws/server/endpoint/interceptor/PayloadTransformingInterceptor.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.io.ByteArrayInputStream;
2020
import java.io.ByteArrayOutputStream;
2121

22-
import javax.xml.parsers.SAXParserFactory;
2322
import javax.xml.transform.Source;
2423
import javax.xml.transform.Templates;
2524
import javax.xml.transform.Transformer;
@@ -38,6 +37,7 @@
3837
import org.springframework.ws.WebServiceMessage;
3938
import org.springframework.ws.context.MessageContext;
4039
import org.springframework.ws.server.EndpointInterceptor;
40+
import org.springframework.xml.sax.SaxUtils;
4141
import org.springframework.xml.transform.ResourceSource;
4242
import org.springframework.xml.transform.TransformerObjectSupport;
4343

@@ -139,9 +139,7 @@ public void afterPropertiesSet() throws Exception {
139139
throw new IllegalArgumentException("Setting either 'requestXslt' or 'responseXslt' is required");
140140
}
141141
TransformerFactory transformerFactory = getTransformerFactory();
142-
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
143-
parserFactory.setNamespaceAware(true);
144-
XMLReader xmlReader = parserFactory.newSAXParser().getXMLReader();
142+
XMLReader xmlReader = SaxUtils.namespaceAwareXmlReader();
145143
xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
146144
if (this.requestXslt != null) {
147145
Assert.isTrue(this.requestXslt.exists(), "requestXslt \"" + this.requestXslt + "\" does not exit");

spring-ws-core/src/main/java/org/springframework/ws/wsdl/wsdl11/SimpleWsdl11Definition.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
import java.io.IOException;
2020

21-
import javax.xml.parsers.ParserConfigurationException;
22-
import javax.xml.parsers.SAXParserFactory;
2321
import javax.xml.transform.Source;
2422

2523
import org.xml.sax.SAXException;
@@ -29,6 +27,7 @@
2927
import org.springframework.core.io.Resource;
3028
import org.springframework.util.Assert;
3129
import org.springframework.ws.wsdl.WsdlDefinitionException;
30+
import org.springframework.xml.sax.SaxUtils;
3231
import org.springframework.xml.transform.ResourceSource;
3332

3433
/**
@@ -73,13 +72,11 @@ public void afterPropertiesSet() throws Exception {
7372
@Override
7473
public Source getSource() {
7574
try {
76-
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
77-
parserFactory.setNamespaceAware(true);
78-
XMLReader xmlReader = parserFactory.newSAXParser().getXMLReader();
75+
XMLReader xmlReader = SaxUtils.namespaceAwareXmlReader();
7976
xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
8077
return new ResourceSource(xmlReader, this.wsdlResource);
8178
}
82-
catch (SAXException | ParserConfigurationException ex) {
79+
catch (SAXException ex) {
8380
throw new WsdlDefinitionException("Could not create XMLReader", ex);
8481
}
8582
catch (IOException ex) {

spring-ws-core/src/test/java/org/springframework/ws/AbstractWebServiceMessageTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
import javax.xml.parsers.DocumentBuilder;
2828
import javax.xml.parsers.DocumentBuilderFactory;
29-
import javax.xml.parsers.SAXParserFactory;
3029
import javax.xml.stream.XMLEventReader;
3130
import javax.xml.stream.XMLEventWriter;
3231
import javax.xml.stream.XMLInputFactory;
@@ -188,9 +187,7 @@ public void testStreamReaderPayload() throws Exception {
188187
}
189188

190189
private void validateMessage() throws Exception {
191-
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
192-
parserFactory.setNamespaceAware(true);
193-
XMLReader xmlReader = parserFactory.newSAXParser().getXMLReader();
190+
XMLReader xmlReader = SaxUtils.namespaceAwareXmlReader();
194191
xmlReader.setContentHandler(new DefaultHandler());
195192
ByteArrayOutputStream os = new ByteArrayOutputStream();
196193
this.webServiceMessage.writeTo(os);

spring-ws-core/src/test/java/org/springframework/ws/server/endpoint/AbstractEndpointTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import javax.xml.parsers.DocumentBuilder;
2626
import javax.xml.parsers.DocumentBuilderFactory;
27-
import javax.xml.parsers.SAXParserFactory;
2827
import javax.xml.stream.XMLEventReader;
2928
import javax.xml.stream.XMLInputFactory;
3029
import javax.xml.stream.XMLStreamReader;
@@ -41,6 +40,7 @@
4140
import org.springframework.util.xml.StaxUtils;
4241
import org.springframework.xml.DocumentBuilderFactoryUtils;
4342
import org.springframework.xml.XMLInputFactoryUtils;
43+
import org.springframework.xml.sax.SaxUtils;
4444

4545
@SuppressWarnings("Since15")
4646
public abstract class AbstractEndpointTest {
@@ -67,9 +67,7 @@ public void testDomSource() throws Exception {
6767

6868
@Test
6969
public void testSaxSource() throws Exception {
70-
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
71-
parserFactory.setNamespaceAware(true);
72-
XMLReader reader = parserFactory.newSAXParser().getXMLReader();
70+
XMLReader reader = SaxUtils.namespaceAwareXmlReader();
7371
InputSource inputSource = new InputSource(new StringReader(REQUEST));
7472
testSource(new SAXSource(reader, inputSource));
7573
}

spring-xml/src/main/java/org/springframework/xml/sax/SaxUtils.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@
2020
import java.net.URI;
2121
import java.net.URISyntaxException;
2222

23+
import javax.xml.parsers.ParserConfigurationException;
24+
import javax.xml.parsers.SAXParserFactory;
25+
2326
import org.apache.commons.logging.Log;
2427
import org.apache.commons.logging.LogFactory;
2528
import org.xml.sax.InputSource;
29+
import org.xml.sax.SAXException;
30+
import org.xml.sax.XMLReader;
2631

2732
import org.springframework.core.io.Resource;
2833

@@ -36,6 +41,22 @@ public abstract class SaxUtils {
3641

3742
private static final Log logger = LogFactory.getLog(SaxUtils.class);
3843

44+
/**
45+
* Create a default {@link XMLReader} that is
46+
* {@linkplain SAXParserFactory#setNamespaceAware(boolean) namespace aware}.
47+
* @return a new {@link XMLReader}
48+
*/
49+
public static XMLReader namespaceAwareXmlReader() throws SAXException {
50+
try {
51+
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
52+
parserFactory.setNamespaceAware(true);
53+
return parserFactory.newSAXParser().getXMLReader();
54+
}
55+
catch (ParserConfigurationException ex) {
56+
throw new IllegalStateException(ex);
57+
}
58+
}
59+
3960
/**
4061
* Creates a SAX {@code InputSource} from the given resource. Sets the system
4162
* identifier to the resource's {@code URL}, if available.

spring-xml/src/main/java/org/springframework/xml/validation/SchemaLoaderUtils.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import org.springframework.core.io.Resource;
2929
import org.springframework.util.Assert;
30+
import org.springframework.xml.sax.SaxUtils;
3031
import org.springframework.xml.transform.ResourceSource;
3132

3233
/**
@@ -66,7 +67,7 @@ public static Schema loadSchema(Resource[] resources, String schemaLanguage) thr
6667
Assert.notEmpty(resources, "No resources given");
6768
Assert.hasLength(schemaLanguage, "No schema language provided");
6869
Source[] schemaSources = new Source[resources.length];
69-
XMLReader xmlReader = XMLReaderFactoryUtils.createXMLReader();
70+
XMLReader xmlReader = offlinerXmlReader();
7071
xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
7172
for (int i = 0; i < resources.length; i++) {
7273
Assert.notNull(resources[i], "Resource is null");
@@ -77,6 +78,15 @@ public static Schema loadSchema(Resource[] resources, String schemaLanguage) thr
7778
return schemaFactory.newSchema(schemaSources);
7879
}
7980

81+
private static XMLReader offlinerXmlReader() throws SAXException {
82+
XMLReader xmlReader = SaxUtils.namespaceAwareXmlReader();
83+
xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
84+
xmlReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
85+
xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", false);
86+
xmlReader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
87+
return xmlReader;
88+
}
89+
8090
/**
8191
* Retrieves the URL from the given resource as System ID. Returns {@code null} if it
8292
* cannot be opened.

spring-xml/src/main/java/org/springframework/xml/validation/XMLReaderFactoryUtils.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,34 @@
1616

1717
package org.springframework.xml.validation;
1818

19-
import javax.xml.parsers.ParserConfigurationException;
2019
import javax.xml.parsers.SAXParser;
21-
import javax.xml.parsers.SAXParserFactory;
2220

2321
import org.xml.sax.SAXException;
2422
import org.xml.sax.XMLReader;
2523

24+
import org.springframework.xml.sax.SaxUtils;
25+
2626
/**
2727
* General utilities to create an {@link XMLReader}.
2828
*
2929
* @author Greg Turnquist
3030
* @since 3.0.5
31+
* @deprecated since 4.0.12 in favor of {@link SaxUtils}
3132
*/
33+
@Deprecated(since = "4.0.12", forRemoval = true)
3234
public abstract class XMLReaderFactoryUtils {
3335

3436
/**
3537
* Build a {@link XMLReader} and set properties to prevent external entity access.
3638
* @see SAXParser#getXMLReader()
3739
*/
3840
public static XMLReader createXMLReader() throws SAXException {
39-
XMLReader xmlReader = namespaceAwareXmlReader();
41+
XMLReader xmlReader = SaxUtils.namespaceAwareXmlReader();
4042
xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
4143
xmlReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
4244
xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", false);
4345
xmlReader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
4446
return xmlReader;
4547
}
4648

47-
private static XMLReader namespaceAwareXmlReader() throws SAXException {
48-
try {
49-
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
50-
parserFactory.setNamespaceAware(true);
51-
return parserFactory.newSAXParser().getXMLReader();
52-
}
53-
catch (ParserConfigurationException ex) {
54-
throw new IllegalStateException(ex);
55-
}
56-
57-
}
58-
5949
}

spring-xml/src/test/java/org/springframework/xml/dom/DomContentHandlerTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import javax.xml.parsers.DocumentBuilder;
2222
import javax.xml.parsers.DocumentBuilderFactory;
23-
import javax.xml.parsers.SAXParserFactory;
2423

2524
import org.junit.jupiter.api.BeforeEach;
2625
import org.junit.jupiter.api.Test;
@@ -30,6 +29,7 @@
3029
import org.xml.sax.XMLReader;
3130

3231
import org.springframework.xml.DocumentBuilderFactoryUtils;
32+
import org.springframework.xml.sax.SaxUtils;
3333

3434
import static org.xmlunit.assertj.XmlAssert.assertThat;
3535

@@ -63,9 +63,7 @@ public void setUp() throws Exception {
6363
documentBuilderFactory.setNamespaceAware(true);
6464
this.documentBuilder = documentBuilderFactory.newDocumentBuilder();
6565
this.result = this.documentBuilder.newDocument();
66-
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
67-
parserFactory.setNamespaceAware(true);
68-
this.xmlReader = parserFactory.newSAXParser().getXMLReader();
66+
this.xmlReader = SaxUtils.namespaceAwareXmlReader();
6967
}
7068

7169
@Test

spring-xml/src/test/java/org/springframework/xml/transform/TraxUtilsTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
import javax.xml.parsers.DocumentBuilder;
3030
import javax.xml.parsers.DocumentBuilderFactory;
31-
import javax.xml.parsers.SAXParserFactory;
3231
import javax.xml.stream.XMLEventReader;
3332
import javax.xml.stream.XMLEventWriter;
3433
import javax.xml.stream.XMLInputFactory;
@@ -57,6 +56,7 @@
5756
import org.springframework.util.xml.StaxUtils;
5857
import org.springframework.xml.DocumentBuilderFactoryUtils;
5958
import org.springframework.xml.XMLInputFactoryUtils;
59+
import org.springframework.xml.sax.SaxUtils;
6060

6161
import static org.assertj.core.api.Assertions.assertThat;
6262
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
@@ -119,9 +119,7 @@ public void testDoWithDomResult() throws Exception {
119119
@Test
120120
public void testDoWithSaxSource() throws Exception {
121121

122-
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
123-
parserFactory.setNamespaceAware(true);
124-
XMLReader reader = parserFactory.newSAXParser().getXMLReader();
122+
XMLReader reader = SaxUtils.namespaceAwareXmlReader();
125123
InputSource inputSource = new InputSource();
126124

127125
TraxUtils.SourceCallback mock = createMock(TraxUtils.SourceCallback.class);

0 commit comments

Comments
 (0)