diff --git a/src/main/java/org/apache/commons/xml/FallbackIgnoreLSResourceResolver.java b/src/main/java/org/apache/commons/xml/FallbackIgnoreLSResourceResolver.java index 3d99d641..d225b07e 100644 --- a/src/main/java/org/apache/commons/xml/FallbackIgnoreLSResourceResolver.java +++ b/src/main/java/org/apache/commons/xml/FallbackIgnoreLSResourceResolver.java @@ -37,13 +37,13 @@ final class FallbackIgnoreLSResourceResolver implements LSResourceResolver { /** DOM Level 3 Load/Save implementation used to build the empty input for unresolved lookups. */ - private static final DOMImplementationLS DOM_LS = domImplementationLS(); + private static final DOMImplementationLS DOM_LS; - private static DOMImplementationLS domImplementationLS() { + static { try { - return (DOMImplementationLS) DOMImplementationRegistry.newInstance().getDOMImplementation("LS"); - } catch (final ClassNotFoundException | InstantiationException | IllegalAccessException e) { - throw new HardeningException("No DOM Level 3 Load/Save implementation available to build the empty schema input", e); + DOM_LS = (DOMImplementationLS) DOMImplementationRegistry.newInstance().getDOMImplementation("LS"); + } catch (final ReflectiveOperationException e) { + throw new ExceptionInInitializerError(e); } } diff --git a/src/main/java/org/apache/commons/xml/FallbackIgnoreURIResolver.java b/src/main/java/org/apache/commons/xml/FallbackIgnoreURIResolver.java index debd391b..ee263407 100644 --- a/src/main/java/org/apache/commons/xml/FallbackIgnoreURIResolver.java +++ b/src/main/java/org/apache/commons/xml/FallbackIgnoreURIResolver.java @@ -20,7 +20,6 @@ import java.util.function.Supplier; import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.FactoryConfigurationError; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Source; import javax.xml.transform.TransformerException; @@ -53,11 +52,10 @@ final class FallbackIgnoreURIResolver implements URIResolver { * Backing for the default ignore outcome. Consumers parse the resolved {@link Source}, and an empty character stream is not a well-formed XML document * (XSLTC rejects it for {@code document()} and for an ignored {@code xsl:include}/{@code xsl:import}), so the default supplier answers with a well-formed * empty document that evaluates to no content. It is never mutated, so one instance serves every resolution. - * - * @see #newEmptyDocument() */ - private static final Document EMPTY_DOCUMENT = newEmptyDocument(); + private static final Document EMPTY_DOCUMENT; + static { /** * Creates a new empty document. * @@ -68,9 +66,10 @@ final class FallbackIgnoreURIResolver implements URIResolver { */ private static Document newEmptyDocument() { try { - return DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); + EMPTY_DOCUMENT = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); } catch (final ParserConfigurationException e) { - throw new HardeningException("A DocumentBuilder cannot be created which satisfies the configuration requested.", e); + // Checked exceptions cannot escape static initialization; a FactoryConfigurationError from newInstance() already can and propagates as-is. + throw new ExceptionInInitializerError(e); } }