Adapt to deprecation

This commit is contained in:
Stéphane Nicoll
2025-02-27 09:46:59 +01:00
parent fb4ab827c5
commit 5a1e5028cb
38 changed files with 103 additions and 50 deletions

View File

@@ -15,6 +15,8 @@
*/
package org.springframework.xml.transform;
import java.lang.reflect.InvocationTargetException;
import javax.xml.XMLConstants;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
@@ -43,9 +45,9 @@ public class TransformerFactoryUtils {
*/
public static TransformerFactory newInstance(Class<? extends TransformerFactory> transformerFactoryClass) {
try {
return defaultSettings(transformerFactoryClass.newInstance());
return defaultSettings(transformerFactoryClass.getDeclaredConstructor().newInstance());
}
catch (InstantiationException | IllegalAccessException e) {
catch (InvocationTargetException | NoSuchMethodException | InstantiationException | IllegalAccessException e) {
throw new TransformerFactoryConfigurationError(e,
"Could not instantiate TransformerFactory [" + transformerFactoryClass + "]");
}

View File

@@ -15,9 +15,12 @@
*/
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.xml.sax.helpers.XMLReaderFactory;
/**
* @author Greg Turnquist
@@ -27,17 +30,27 @@ public class XMLReaderFactoryUtils {
/**
* Build a {@link XMLReader} and set properties to prevent external entity access.
* @see XMLReaderFactory#createXMLReader()
* @see SAXParser#getXMLReader()
*/
public static XMLReader createXMLReader() throws SAXException {
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
XMLReader xmlReader = 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;
}
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);
}
}
}

View File

@@ -20,6 +20,7 @@ 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;
@@ -27,7 +28,6 @@ import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import org.springframework.xml.DocumentBuilderFactoryUtils;
@@ -63,7 +63,9 @@ public class DomContentHandlerTest {
documentBuilderFactory.setNamespaceAware(true);
documentBuilder = documentBuilderFactory.newDocumentBuilder();
result = documentBuilder.newDocument();
xmlReader = XMLReaderFactory.createXMLReader();
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
parserFactory.setNamespaceAware(true);
xmlReader = parserFactory.newSAXParser().getXMLReader();
}
@Test

View File

@@ -28,6 +28,7 @@ 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;
@@ -52,7 +53,6 @@ import org.xml.sax.XMLReader;
import org.xml.sax.ext.DefaultHandler2;
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
import org.springframework.util.xml.StaxUtils;
import org.springframework.xml.DocumentBuilderFactoryUtils;
@@ -119,7 +119,9 @@ public class TraxUtilsTest {
@Test
public void testDoWithSaxSource() throws Exception {
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
parserFactory.setNamespaceAware(true);
XMLReader reader = parserFactory.newSAXParser().getXMLReader();
InputSource inputSource = new InputSource();
TraxUtils.SourceCallback mock = createMock(TraxUtils.SourceCallback.class);