Skip to content

Commit e0979d0

Browse files
committed
Merge pull request #27239 from Frederick888
* pr/27239: Update copyright year of changed files Replace XMLReaderFactory with SAXParserFactory Closes gh-27239
2 parents 32359c5 + e986ff8 commit e0979d0

File tree

8 files changed

+74
-37
lines changed

8 files changed

+74
-37
lines changed

spring-core/src/test/java/org/springframework/util/xml/AbstractStaxHandlerTests.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,8 @@
2121

2222
import javax.xml.parsers.DocumentBuilder;
2323
import javax.xml.parsers.DocumentBuilderFactory;
24+
import javax.xml.parsers.SAXParser;
25+
import javax.xml.parsers.SAXParserFactory;
2426
import javax.xml.stream.XMLStreamException;
2527
import javax.xml.transform.Result;
2628
import javax.xml.transform.dom.DOMResult;
@@ -63,9 +65,11 @@ abstract class AbstractStaxHandlerTests {
6365

6466

6567
@BeforeEach
66-
@SuppressWarnings("deprecation") // on JDK 9
6768
void createXMLReader() throws Exception {
68-
xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
69+
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
70+
saxParserFactory.setNamespaceAware(true);
71+
SAXParser saxParser = saxParserFactory.newSAXParser();
72+
xmlReader = saxParser.getXMLReader();
6973
xmlReader.setEntityResolver((publicId, systemId) -> new InputSource(new StringReader("")));
7074
}
7175

spring-core/src/test/java/org/springframework/util/xml/AbstractStaxXMLReaderTests.java

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

22+
import javax.xml.parsers.SAXParser;
23+
import javax.xml.parsers.SAXParserFactory;
2224
import javax.xml.stream.XMLInputFactory;
2325
import javax.xml.stream.XMLStreamException;
2426
import javax.xml.transform.Transformer;
@@ -64,10 +66,12 @@ abstract class AbstractStaxXMLReaderTests {
6466

6567

6668
@BeforeEach
67-
@SuppressWarnings("deprecation") // on JDK 9
6869
void setUp() throws Exception {
6970
inputFactory = XMLInputFactory.newInstance();
70-
standardReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
71+
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
72+
saxParserFactory.setNamespaceAware(true);
73+
SAXParser saxParser = saxParserFactory.newSAXParser();
74+
standardReader = saxParser.getXMLReader();
7175
standardContentHandler = mockContentHandler();
7276
standardReader.setContentHandler(standardContentHandler);
7377
}

spring-core/src/test/java/org/springframework/util/xml/DomContentHandlerTests.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,8 @@
2020

2121
import javax.xml.parsers.DocumentBuilder;
2222
import javax.xml.parsers.DocumentBuilderFactory;
23+
import javax.xml.parsers.SAXParser;
24+
import javax.xml.parsers.SAXParserFactory;
2325

2426
import org.junit.jupiter.api.BeforeEach;
2527
import org.junit.jupiter.api.Test;
@@ -62,13 +64,15 @@ class DomContentHandlerTests {
6264

6365

6466
@BeforeEach
65-
@SuppressWarnings("deprecation") // on JDK 9
6667
void setUp() throws Exception {
6768
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
6869
documentBuilderFactory.setNamespaceAware(true);
6970
documentBuilder = documentBuilderFactory.newDocumentBuilder();
7071
result = documentBuilder.newDocument();
71-
xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
72+
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
73+
saxParserFactory.setNamespaceAware(true);
74+
SAXParser saxParser = saxParserFactory.newSAXParser();
75+
xmlReader = saxParser.getXMLReader();
7276
}
7377

7478

spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
import javax.xml.datatype.Duration;
4343
import javax.xml.datatype.XMLGregorianCalendar;
4444
import javax.xml.namespace.QName;
45+
import javax.xml.parsers.ParserConfigurationException;
46+
import javax.xml.parsers.SAXParser;
47+
import javax.xml.parsers.SAXParserFactory;
4548
import javax.xml.stream.XMLEventReader;
4649
import javax.xml.stream.XMLEventWriter;
4750
import javax.xml.stream.XMLStreamReader;
@@ -575,17 +578,19 @@ private JAXBContext createJaxbContextFromPackages(String... packagesToScan) thro
575578
}
576579
}
577580

578-
@SuppressWarnings("deprecation")
579-
private Schema loadSchema(Resource[] resources, String schemaLanguage) throws IOException, SAXException {
581+
private Schema loadSchema(Resource[] resources, String schemaLanguage) throws IOException, SAXException, ParserConfigurationException {
580582
if (logger.isDebugEnabled()) {
581583
logger.debug("Setting validation schema to " +
582584
StringUtils.arrayToCommaDelimitedString(this.schemaResources));
583585
}
584586
Assert.notEmpty(resources, "No resources given");
585587
Assert.hasLength(schemaLanguage, "No schema language provided");
586588
Source[] schemaSources = new Source[resources.length];
587-
XMLReader xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
588-
xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
589+
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
590+
saxParserFactory.setNamespaceAware(true);
591+
saxParserFactory.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
592+
SAXParser saxParser = saxParserFactory.newSAXParser();
593+
XMLReader xmlReader = saxParser.getXMLReader();
589594
for (int i = 0; i < resources.length; i++) {
590595
Resource resource = resources[i];
591596
Assert.isTrue(resource != null && resource.exists(), () -> "Resource does not exist: " + resource);
@@ -854,7 +859,6 @@ protected Object unmarshalStaxSource(Unmarshaller jaxbUnmarshaller, Source staxS
854859
}
855860
}
856861

857-
@SuppressWarnings("deprecation")
858862
private Source processSource(Source source) {
859863
if (StaxUtils.isStaxSource(source) || source instanceof DOMSource) {
860864
return source;
@@ -881,17 +885,20 @@ else if (streamSource.getReader() != null) {
881885

882886
try {
883887
if (xmlReader == null) {
884-
xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
888+
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
889+
saxParserFactory.setNamespaceAware(true);
890+
saxParserFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd());
891+
String name = "http://xml.org/sax/features/external-general-entities";
892+
saxParserFactory.setFeature(name, isProcessExternalEntities());
893+
SAXParser saxParser = saxParserFactory.newSAXParser();
894+
xmlReader = saxParser.getXMLReader();
885895
}
886-
xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd());
887-
String name = "http://xml.org/sax/features/external-general-entities";
888-
xmlReader.setFeature(name, isProcessExternalEntities());
889896
if (!isProcessExternalEntities()) {
890897
xmlReader.setEntityResolver(NO_OP_ENTITY_RESOLVER);
891898
}
892899
return new SAXSource(xmlReader, inputSource);
893900
}
894-
catch (SAXException ex) {
901+
catch (SAXException | ParserConfigurationException ex) {
895902
logger.info("Processing of external entities could not be disabled", ex);
896903
return source;
897904
}

spring-oxm/src/main/java/org/springframework/oxm/support/AbstractMarshaller.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import javax.xml.parsers.DocumentBuilder;
2727
import javax.xml.parsers.DocumentBuilderFactory;
2828
import javax.xml.parsers.ParserConfigurationException;
29+
import javax.xml.parsers.SAXParser;
30+
import javax.xml.parsers.SAXParserFactory;
2931
import javax.xml.stream.XMLEventReader;
3032
import javax.xml.stream.XMLEventWriter;
3133
import javax.xml.stream.XMLStreamReader;
@@ -188,12 +190,15 @@ protected DocumentBuilder createDocumentBuilder(DocumentBuilderFactory factory)
188190
* Create an {@code XMLReader} that this marshaller will when passed an empty {@code SAXSource}.
189191
* @return the XMLReader
190192
* @throws SAXException if thrown by JAXP methods
193+
* @throws ParserConfigurationException if thrown by JAXP methods
191194
*/
192-
@SuppressWarnings("deprecation")
193-
protected XMLReader createXmlReader() throws SAXException {
194-
XMLReader xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
195-
xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd());
196-
xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", isProcessExternalEntities());
195+
protected XMLReader createXmlReader() throws SAXException, ParserConfigurationException {
196+
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
197+
saxParserFactory.setNamespaceAware(true);
198+
saxParserFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd());
199+
saxParserFactory.setFeature("http://xml.org/sax/features/external-general-entities", isProcessExternalEntities());
200+
SAXParser saxParser = saxParserFactory.newSAXParser();
201+
XMLReader xmlReader = saxParser.getXMLReader();
197202
if (!isProcessExternalEntities()) {
198203
xmlReader.setEntityResolver(NO_OP_ENTITY_RESOLVER);
199204
}
@@ -431,7 +436,7 @@ protected Object unmarshalSaxSource(SAXSource saxSource) throws XmlMappingExcept
431436
try {
432437
saxSource.setXMLReader(createXmlReader());
433438
}
434-
catch (SAXException ex) {
439+
catch (SAXException | ParserConfigurationException ex) {
435440
throw new UnmarshallingFailureException("Could not create XMLReader for SAXSource", ex);
436441
}
437442
}

spring-oxm/src/test/java/org/springframework/oxm/AbstractUnmarshallerTests.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,8 @@
2222
import javax.xml.namespace.QName;
2323
import javax.xml.parsers.DocumentBuilder;
2424
import javax.xml.parsers.DocumentBuilderFactory;
25+
import javax.xml.parsers.SAXParser;
26+
import javax.xml.parsers.SAXParserFactory;
2527
import javax.xml.stream.XMLEventReader;
2628
import javax.xml.stream.XMLInputFactory;
2729
import javax.xml.stream.XMLStreamReader;
@@ -98,9 +100,11 @@ public void unmarshalStreamSourceInputStream() throws Exception {
98100
}
99101

100102
@Test
101-
@SuppressWarnings("deprecation") // on JDK 9
102103
public void unmarshalSAXSource() throws Exception {
103-
XMLReader reader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
104+
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
105+
saxParserFactory.setNamespaceAware(true);
106+
SAXParser saxParser = saxParserFactory.newSAXParser();
107+
XMLReader reader = saxParser.getXMLReader();
104108
SAXSource source = new SAXSource(reader, new InputSource(new StringReader(INPUT_STRING)));
105109
Object flights = unmarshaller.unmarshal(source);
106110
testFlights(flights);

spring-web/src/main/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverter.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
import java.io.StringReader;
2020

21+
import javax.xml.parsers.ParserConfigurationException;
22+
import javax.xml.parsers.SAXParser;
23+
import javax.xml.parsers.SAXParserFactory;
2124
import javax.xml.transform.Result;
2225
import javax.xml.transform.Source;
2326
import javax.xml.transform.sax.SAXSource;
@@ -149,21 +152,23 @@ protected Object readFromSource(Class<?> clazz, HttpHeaders headers, Source sour
149152
}
150153
}
151154

152-
@SuppressWarnings("deprecation")
153155
protected Source processSource(Source source) {
154156
if (source instanceof StreamSource streamSource) {
155157
InputSource inputSource = new InputSource(streamSource.getInputStream());
156158
try {
157-
XMLReader xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
158-
xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd());
159+
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
160+
saxParserFactory.setNamespaceAware(true);
161+
saxParserFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd());
159162
String featureName = "http://xml.org/sax/features/external-general-entities";
160-
xmlReader.setFeature(featureName, isProcessExternalEntities());
163+
saxParserFactory.setFeature(featureName, isProcessExternalEntities());
164+
SAXParser saxParser = saxParserFactory.newSAXParser();
165+
XMLReader xmlReader = saxParser.getXMLReader();
161166
if (!isProcessExternalEntities()) {
162167
xmlReader.setEntityResolver(NO_OP_ENTITY_RESOLVER);
163168
}
164169
return new SAXSource(xmlReader, inputSource);
165170
}
166-
catch (SAXException ex) {
171+
catch (SAXException | ParserConfigurationException ex) {
167172
logger.warn("Processing of external entities could not be disabled", ex);
168173
return source;
169174
}

spring-web/src/main/java/org/springframework/http/converter/xml/SourceHttpMessageConverter.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import javax.xml.parsers.DocumentBuilder;
2828
import javax.xml.parsers.DocumentBuilderFactory;
2929
import javax.xml.parsers.ParserConfigurationException;
30+
import javax.xml.parsers.SAXParser;
31+
import javax.xml.parsers.SAXParserFactory;
3032
import javax.xml.stream.XMLInputFactory;
3133
import javax.xml.stream.XMLResolver;
3234
import javax.xml.stream.XMLStreamException;
@@ -197,19 +199,21 @@ private DOMSource readDOMSource(InputStream body, HttpInputMessage inputMessage)
197199
}
198200
}
199201

200-
@SuppressWarnings("deprecation")
201202
private SAXSource readSAXSource(InputStream body, HttpInputMessage inputMessage) throws IOException {
202203
try {
203-
XMLReader xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
204-
xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd());
205-
xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", isProcessExternalEntities());
204+
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
205+
saxParserFactory.setNamespaceAware(true);
206+
saxParserFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd());
207+
saxParserFactory.setFeature("http://xml.org/sax/features/external-general-entities", isProcessExternalEntities());
208+
SAXParser saxParser = saxParserFactory.newSAXParser();
209+
XMLReader xmlReader = saxParser.getXMLReader();
206210
if (!isProcessExternalEntities()) {
207211
xmlReader.setEntityResolver(NO_OP_ENTITY_RESOLVER);
208212
}
209213
byte[] bytes = StreamUtils.copyToByteArray(body);
210214
return new SAXSource(xmlReader, new InputSource(new ByteArrayInputStream(bytes)));
211215
}
212-
catch (SAXException ex) {
216+
catch (SAXException | ParserConfigurationException ex) {
213217
throw new HttpMessageNotReadableException(
214218
"Could not parse document: " + ex.getMessage(), ex, inputMessage);
215219
}

0 commit comments

Comments
 (0)