Merge pull request #27239 from Frederick888

* pr/27239:
  Update copyright year of changed files
  Replace XMLReaderFactory with SAXParserFactory

Closes gh-27239
This commit is contained in:
Stephane Nicoll 2021-12-02 11:33:50 +01:00
commit e0979d0e74
8 changed files with 74 additions and 37 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -21,6 +21,8 @@ import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamException;
import javax.xml.transform.Result; import javax.xml.transform.Result;
import javax.xml.transform.dom.DOMResult; import javax.xml.transform.dom.DOMResult;
@ -63,9 +65,11 @@ abstract class AbstractStaxHandlerTests {
@BeforeEach @BeforeEach
@SuppressWarnings("deprecation") // on JDK 9
void createXMLReader() throws Exception { void createXMLReader() throws Exception {
xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader(); SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setNamespaceAware(true);
SAXParser saxParser = saxParserFactory.newSAXParser();
xmlReader = saxParser.getXMLReader();
xmlReader.setEntityResolver((publicId, systemId) -> new InputSource(new StringReader(""))); xmlReader.setEntityResolver((publicId, systemId) -> new InputSource(new StringReader("")));
} }

View File

@ -19,6 +19,8 @@ package org.springframework.util.xml;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.InputStream; import java.io.InputStream;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamException;
import javax.xml.transform.Transformer; import javax.xml.transform.Transformer;
@ -64,10 +66,12 @@ abstract class AbstractStaxXMLReaderTests {
@BeforeEach @BeforeEach
@SuppressWarnings("deprecation") // on JDK 9
void setUp() throws Exception { void setUp() throws Exception {
inputFactory = XMLInputFactory.newInstance(); inputFactory = XMLInputFactory.newInstance();
standardReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader(); SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setNamespaceAware(true);
SAXParser saxParser = saxParserFactory.newSAXParser();
standardReader = saxParser.getXMLReader();
standardContentHandler = mockContentHandler(); standardContentHandler = mockContentHandler();
standardReader.setContentHandler(standardContentHandler); standardReader.setContentHandler(standardContentHandler);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -20,6 +20,8 @@ import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -62,13 +64,15 @@ class DomContentHandlerTests {
@BeforeEach @BeforeEach
@SuppressWarnings("deprecation") // on JDK 9
void setUp() throws Exception { void setUp() throws Exception {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true); documentBuilderFactory.setNamespaceAware(true);
documentBuilder = documentBuilderFactory.newDocumentBuilder(); documentBuilder = documentBuilderFactory.newDocumentBuilder();
result = documentBuilder.newDocument(); result = documentBuilder.newDocument();
xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader(); SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setNamespaceAware(true);
SAXParser saxParser = saxParserFactory.newSAXParser();
xmlReader = saxParser.getXMLReader();
} }

View File

@ -42,6 +42,9 @@ import javax.xml.XMLConstants;
import javax.xml.datatype.Duration; import javax.xml.datatype.Duration;
import javax.xml.datatype.XMLGregorianCalendar; import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName; import javax.xml.namespace.QName;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter; import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamReader;
@ -575,8 +578,7 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
} }
} }
@SuppressWarnings("deprecation") private Schema loadSchema(Resource[] resources, String schemaLanguage) throws IOException, SAXException, ParserConfigurationException {
private Schema loadSchema(Resource[] resources, String schemaLanguage) throws IOException, SAXException {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("Setting validation schema to " + logger.debug("Setting validation schema to " +
StringUtils.arrayToCommaDelimitedString(this.schemaResources)); StringUtils.arrayToCommaDelimitedString(this.schemaResources));
@ -584,8 +586,11 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
Assert.notEmpty(resources, "No resources given"); Assert.notEmpty(resources, "No resources given");
Assert.hasLength(schemaLanguage, "No schema language provided"); Assert.hasLength(schemaLanguage, "No schema language provided");
Source[] schemaSources = new Source[resources.length]; Source[] schemaSources = new Source[resources.length];
XMLReader xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader(); SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true); saxParserFactory.setNamespaceAware(true);
saxParserFactory.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
SAXParser saxParser = saxParserFactory.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
for (int i = 0; i < resources.length; i++) { for (int i = 0; i < resources.length; i++) {
Resource resource = resources[i]; Resource resource = resources[i];
Assert.isTrue(resource != null && resource.exists(), () -> "Resource does not exist: " + resource); Assert.isTrue(resource != null && resource.exists(), () -> "Resource does not exist: " + resource);
@ -854,7 +859,6 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
} }
} }
@SuppressWarnings("deprecation")
private Source processSource(Source source) { private Source processSource(Source source) {
if (StaxUtils.isStaxSource(source) || source instanceof DOMSource) { if (StaxUtils.isStaxSource(source) || source instanceof DOMSource) {
return source; return source;
@ -881,17 +885,20 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
try { try {
if (xmlReader == null) { if (xmlReader == null) {
xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader(); SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
} saxParserFactory.setNamespaceAware(true);
xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd()); saxParserFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd());
String name = "http://xml.org/sax/features/external-general-entities"; String name = "http://xml.org/sax/features/external-general-entities";
xmlReader.setFeature(name, isProcessExternalEntities()); saxParserFactory.setFeature(name, isProcessExternalEntities());
SAXParser saxParser = saxParserFactory.newSAXParser();
xmlReader = saxParser.getXMLReader();
}
if (!isProcessExternalEntities()) { if (!isProcessExternalEntities()) {
xmlReader.setEntityResolver(NO_OP_ENTITY_RESOLVER); xmlReader.setEntityResolver(NO_OP_ENTITY_RESOLVER);
} }
return new SAXSource(xmlReader, inputSource); return new SAXSource(xmlReader, inputSource);
} }
catch (SAXException ex) { catch (SAXException | ParserConfigurationException ex) {
logger.info("Processing of external entities could not be disabled", ex); logger.info("Processing of external entities could not be disabled", ex);
return source; return source;
} }

View File

@ -26,6 +26,8 @@ import java.io.Writer;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter; import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamReader;
@ -188,12 +190,15 @@ public abstract class AbstractMarshaller implements Marshaller, Unmarshaller {
* Create an {@code XMLReader} that this marshaller will when passed an empty {@code SAXSource}. * Create an {@code XMLReader} that this marshaller will when passed an empty {@code SAXSource}.
* @return the XMLReader * @return the XMLReader
* @throws SAXException if thrown by JAXP methods * @throws SAXException if thrown by JAXP methods
* @throws ParserConfigurationException if thrown by JAXP methods
*/ */
@SuppressWarnings("deprecation") protected XMLReader createXmlReader() throws SAXException, ParserConfigurationException {
protected XMLReader createXmlReader() throws SAXException { SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
XMLReader xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader(); saxParserFactory.setNamespaceAware(true);
xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd()); saxParserFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd());
xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", isProcessExternalEntities()); saxParserFactory.setFeature("http://xml.org/sax/features/external-general-entities", isProcessExternalEntities());
SAXParser saxParser = saxParserFactory.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
if (!isProcessExternalEntities()) { if (!isProcessExternalEntities()) {
xmlReader.setEntityResolver(NO_OP_ENTITY_RESOLVER); xmlReader.setEntityResolver(NO_OP_ENTITY_RESOLVER);
} }
@ -431,7 +436,7 @@ public abstract class AbstractMarshaller implements Marshaller, Unmarshaller {
try { try {
saxSource.setXMLReader(createXmlReader()); saxSource.setXMLReader(createXmlReader());
} }
catch (SAXException ex) { catch (SAXException | ParserConfigurationException ex) {
throw new UnmarshallingFailureException("Could not create XMLReader for SAXSource", ex); throw new UnmarshallingFailureException("Could not create XMLReader for SAXSource", ex);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,6 +22,8 @@ import java.io.StringReader;
import javax.xml.namespace.QName; import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamReader;
@ -98,9 +100,11 @@ public abstract class AbstractUnmarshallerTests<U extends Unmarshaller> {
} }
@Test @Test
@SuppressWarnings("deprecation") // on JDK 9
public void unmarshalSAXSource() throws Exception { public void unmarshalSAXSource() throws Exception {
XMLReader reader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader(); SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setNamespaceAware(true);
SAXParser saxParser = saxParserFactory.newSAXParser();
XMLReader reader = saxParser.getXMLReader();
SAXSource source = new SAXSource(reader, new InputSource(new StringReader(INPUT_STRING))); SAXSource source = new SAXSource(reader, new InputSource(new StringReader(INPUT_STRING)));
Object flights = unmarshaller.unmarshal(source); Object flights = unmarshaller.unmarshal(source);
testFlights(flights); testFlights(flights);

View File

@ -18,6 +18,9 @@ package org.springframework.http.converter.xml;
import java.io.StringReader; import java.io.StringReader;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.Result; import javax.xml.transform.Result;
import javax.xml.transform.Source; import javax.xml.transform.Source;
import javax.xml.transform.sax.SAXSource; import javax.xml.transform.sax.SAXSource;
@ -149,21 +152,23 @@ public class Jaxb2RootElementHttpMessageConverter extends AbstractJaxb2HttpMessa
} }
} }
@SuppressWarnings("deprecation")
protected Source processSource(Source source) { protected Source processSource(Source source) {
if (source instanceof StreamSource streamSource) { if (source instanceof StreamSource streamSource) {
InputSource inputSource = new InputSource(streamSource.getInputStream()); InputSource inputSource = new InputSource(streamSource.getInputStream());
try { try {
XMLReader xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader(); SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd()); saxParserFactory.setNamespaceAware(true);
saxParserFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd());
String featureName = "http://xml.org/sax/features/external-general-entities"; String featureName = "http://xml.org/sax/features/external-general-entities";
xmlReader.setFeature(featureName, isProcessExternalEntities()); saxParserFactory.setFeature(featureName, isProcessExternalEntities());
SAXParser saxParser = saxParserFactory.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
if (!isProcessExternalEntities()) { if (!isProcessExternalEntities()) {
xmlReader.setEntityResolver(NO_OP_ENTITY_RESOLVER); xmlReader.setEntityResolver(NO_OP_ENTITY_RESOLVER);
} }
return new SAXSource(xmlReader, inputSource); return new SAXSource(xmlReader, inputSource);
} }
catch (SAXException ex) { catch (SAXException | ParserConfigurationException ex) {
logger.warn("Processing of external entities could not be disabled", ex); logger.warn("Processing of external entities could not be disabled", ex);
return source; return source;
} }

View File

@ -27,6 +27,8 @@ import java.util.Set;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLResolver; import javax.xml.stream.XMLResolver;
import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamException;
@ -197,19 +199,21 @@ public class SourceHttpMessageConverter<T extends Source> extends AbstractHttpMe
} }
} }
@SuppressWarnings("deprecation")
private SAXSource readSAXSource(InputStream body, HttpInputMessage inputMessage) throws IOException { private SAXSource readSAXSource(InputStream body, HttpInputMessage inputMessage) throws IOException {
try { try {
XMLReader xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader(); SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd()); saxParserFactory.setNamespaceAware(true);
xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", isProcessExternalEntities()); saxParserFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd());
saxParserFactory.setFeature("http://xml.org/sax/features/external-general-entities", isProcessExternalEntities());
SAXParser saxParser = saxParserFactory.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
if (!isProcessExternalEntities()) { if (!isProcessExternalEntities()) {
xmlReader.setEntityResolver(NO_OP_ENTITY_RESOLVER); xmlReader.setEntityResolver(NO_OP_ENTITY_RESOLVER);
} }
byte[] bytes = StreamUtils.copyToByteArray(body); byte[] bytes = StreamUtils.copyToByteArray(body);
return new SAXSource(xmlReader, new InputSource(new ByteArrayInputStream(bytes))); return new SAXSource(xmlReader, new InputSource(new ByteArrayInputStream(bytes)));
} }
catch (SAXException ex) { catch (SAXException | ParserConfigurationException ex) {
throw new HttpMessageNotReadableException( throw new HttpMessageNotReadableException(
"Could not parse document: " + ex.getMessage(), ex, inputMessage); "Could not parse document: " + ex.getMessage(), ex, inputMessage);
} }