Class SecureDocumentBuilderFactory
DocumentBuilderFactory instances.
Beyond the three universal guarantees on org.apache.commons.xml.secure, XInclude resolution is denied by default. When
setXIncludeAware(true) is called on the returned factory, the parser will process xi:include
elements but every external resource lookup is rejected. To permit specific trusted resources, install an EntityResolver
on the DocumentBuilder that allow-lists them; any href the resolver does not explicitly allow stays blocked.
This class is not itself a DocumentBuilderFactory, so it inherits none of the static JAXP factory methods. A caller therefore cannot obtain an
unsecured factory through this class by calling a method such as newDefaultInstance(). The secure factories are instances of a nested, non-public
wrapper class.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic DocumentBuilderFactoryReturns a new, secureDocumentBuilderFactoryof the system-default implementation.static DocumentBuilderFactoryReturns a new, secure, namespace-awareDocumentBuilderFactoryof the system-default implementation, enabling namespace awareness onnewDefaultInstance(), the behaviorDocumentBuilderFactory.newDefaultNSInstance()(Java 13 or later) is specified to have.static DocumentBuilderFactoryReturns a new, secureDocumentBuilderFactory.static DocumentBuilderFactorynewInstance(String factoryClassName, ClassLoader classLoader) Returns a new, secureDocumentBuilderFactoryof the given implementation class.static DocumentBuilderCreates a new, secure, namespace-awareDocumentBuilderfromnewNSInstance().static DocumentBuilderFactoryReturns a new, secure, namespace-awareDocumentBuilderFactory, enabling namespace awareness onnewInstance(), the behaviorDocumentBuilderFactory.newNSInstance()(Java 13 or later) is specified to have.static DocumentBuilderFactorynewNSInstance(String factoryClassName, ClassLoader classLoader) Returns a new, secure, namespace-awareDocumentBuilderFactoryof the given implementation class, enabling namespace awareness onnewInstance(String, ClassLoader), the behaviorDocumentBuilderFactory.newNSInstance(String, ClassLoader)(Java 13 or later) is specified to have.
-
Method Details
-
newDefaultInstance
Returns a new, secureDocumentBuilderFactoryof the system-default implementation.Obtained from
DocumentBuilderFactory.newDefaultInstance()where the platform provides it (Java 9 or later), by instantiating the JDK's built-in implementation directly on Java 8, and by the standardnewInstance()lookup where the platform provides neither (for example, Android, whose lookup is itself pinned to the platform implementation).- Returns:
- A secure factory.
- Throws:
IllegalStateException- Thrown if a required secure setting cannot be applied to the underlying implementation.FactoryConfigurationError- Thrown from thenewInstance()lookup this method falls back to on a platform that provides neithernewDefaultInstance()nor the JDK's built-in implementation (for example, Android).
-
newDefaultNSInstance
Returns a new, secure, namespace-awareDocumentBuilderFactoryof the system-default implementation, enabling namespace awareness onnewDefaultInstance(), the behaviorDocumentBuilderFactory.newDefaultNSInstance()(Java 13 or later) is specified to have.- Returns:
- A secure, namespace-aware factory.
- Throws:
IllegalStateException- Thrown if a required secure setting cannot be applied to the underlying implementation.FactoryConfigurationError- Thrown from thenewInstance()lookupnewDefaultInstance()falls back to on a platform that provides neithernewDefaultInstance()nor the JDK's built-in implementation (for example, Android).
-
newInstance
Returns a new, secureDocumentBuilderFactory.- Returns:
- A secure factory.
- Throws:
IllegalStateException- Thrown if a required secure setting cannot be applied to the underlying implementation.IllegalStateException- Thrown if a (non-Android) factory cannot support the secure processing featureXMLConstants.FEATURE_SECURE_PROCESSING.FactoryConfigurationError- Thrown from a factory in case of aservice configuration erroror if the implementation is not available or cannot be instantiated.
-
newInstance
Returns a new, secureDocumentBuilderFactoryof the given implementation class.- Parameters:
factoryClassName- The fully qualified class name of theDocumentBuilderFactoryimplementation.classLoader- The class loader used to load the factory class;nullmeans the current thread's context class loader.- Returns:
- A secure factory.
- Throws:
IllegalStateException- Thrown if a required secure setting cannot be applied to the underlying implementation.IllegalStateException- Thrown if a (non-Android) factory cannot support the secure processing featureXMLConstants.FEATURE_SECURE_PROCESSING.FactoryConfigurationError- Thrown iffactoryClassNameisnullor the factory class cannot be loaded or instantiated.
-
newNSDocumentBuilder
Creates a new, secure, namespace-awareDocumentBuilderfromnewNSInstance().No factory is cached: each call configures a fresh one. To parse many documents, keep the returned builder and call
DocumentBuilder.reset()between documents. Reusing the builder saves more than caching the factory would, andreset()costs next to nothing while keeping handler state from leaking between parses. A builder is not thread-safe, so reuse it within one thread.- Returns:
- A secure, namespace-aware builder.
- Throws:
IllegalStateException- Thrown if a required secure setting cannot be applied to the underlying implementation, or if the implementation cannot create a builder.FactoryConfigurationError- Thrown from a factory in case of aservice configuration erroror if the implementation is not available or cannot be instantiated.- Since:
- 1.1.0
-
newNSInstance
Returns a new, secure, namespace-awareDocumentBuilderFactory, enabling namespace awareness onnewInstance(), the behaviorDocumentBuilderFactory.newNSInstance()(Java 13 or later) is specified to have.- Returns:
- A secure, namespace-aware factory.
- Throws:
IllegalStateException- Thrown if a required secure setting cannot be applied to the underlying implementation.FactoryConfigurationError- Thrown from a factory in case of aservice configuration erroror if the implementation is not available or cannot be instantiated.
-
newNSInstance
public static DocumentBuilderFactory newNSInstance(String factoryClassName, ClassLoader classLoader) Returns a new, secure, namespace-awareDocumentBuilderFactoryof the given implementation class, enabling namespace awareness onnewInstance(String, ClassLoader), the behaviorDocumentBuilderFactory.newNSInstance(String, ClassLoader)(Java 13 or later) is specified to have.- Parameters:
factoryClassName- The fully qualified class name of theDocumentBuilderFactoryimplementation.classLoader- The class loader used to load the factory class;nullmeans the current thread's context class loader.- Returns:
- A secure, namespace-aware factory.
- Throws:
IllegalStateException- Thrown if a required secure setting cannot be applied to the underlying implementation.FactoryConfigurationError- Thrown iffactoryClassNameisnullor the factory class cannot be loaded or instantiated.
-