Class DocumentBuilderFactoryBuilder
- java.lang.Object
-
- com.adaptris.core.util.DocumentBuilderFactoryBuilder
-
public class DocumentBuilderFactoryBuilder extends java.lang.Object
Allows simple configuration of aDocumentBuilderFactory
.For security reasons the default behaviour is to mitigate against XXE attacks and the like. It is still possible to explicitly configure whatever behaviour is required, but we no longer rely on the underlying
DocumentBuilderFactory
defaults. As a result the vanilla configuration for aDocumentBuilderFactory
that is created by this class will have the following defaults:- expandEntityReferences will be set to false if not otherwise specified
- The feature "http://apache.org/xml/features/disallow-doctype-decl" will be automatically set to true if not otherwise specified
Note that the static convenience methods have also been modified to reflect this behaviour. There are the two new
newLenientInstance()
andnewLenientInstanceIfNull(DocumentBuilderFactoryBuilder)
methods if the more secure default is not appropriate.In the adapter configuration file this class is aliased as xml-document-builder-configuration which is the preferred alternative to the fully qualified classname when building your configuration.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
DISABLE_DOCTYPE
-
Constructor Summary
Constructors Constructor Description DocumentBuilderFactoryBuilder()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description DocumentBuilderFactoryBuilder
addFeature(java.lang.String featureName, java.lang.Boolean value)
javax.xml.parsers.DocumentBuilderFactory
build()
Create aDocumentBuilderFactory
.javax.xml.parsers.DocumentBuilder
configure(javax.xml.parsers.DocumentBuilder db)
Configure a document builder.javax.xml.parsers.DocumentBuilderFactory
configure(javax.xml.parsers.DocumentBuilderFactory f)
Configure a document builder factoryjava.lang.Boolean
getCoalescing()
CallsDocumentBuilderFactory.setCoalescing(boolean)
if non-nullorg.xml.sax.EntityResolver
getEntityResolver()
CallsDocumentBuilder.setEntityResolver(EntityResolver)
if non-null.java.lang.Boolean
getExpandEntityReferences()
WrapsDocumentBuilderFactory.setExpandEntityReferences(boolean)
.@NonNull KeyValuePairSet
getFeatures()
CallsDocumentBuilderFactory.setFeature(String, boolean)
for each value defined.java.lang.Boolean
getIgnoreComments()
CallsDocumentBuilderFactory.setIgnoringComments(boolean)
if non-nulljava.lang.Boolean
getIgnoreWhitespace()
CallsDocumentBuilderFactory.setIgnoringElementContentWhitespace(boolean)
if non-nulljava.lang.Boolean
getNamespaceAware()
CallsDocumentBuilderFactory.setNamespaceAware(boolean)
if non nulljava.lang.Boolean
getValidating()
CallsDocumentBuilderFactory.setValidating(boolean)
if non-nulljava.lang.Boolean
getXincludeAware()
CallsDocumentBuilderFactory.setXIncludeAware(boolean)
if non-nulljavax.xml.parsers.DocumentBuilder
newDocumentBuilder(javax.xml.parsers.DocumentBuilderFactory f)
Convenience to create a newDocumentBuilder
instance.static DocumentBuilderFactoryBuilder
newInstance()
Create a new instance that is namespace aware.static DocumentBuilderFactoryBuilder
newInstanceIfNull(DocumentBuilderFactoryBuilder b)
Convenient method to create a new default instance if required.static DocumentBuilderFactoryBuilder
newInstanceIfNull(DocumentBuilderFactoryBuilder b, javax.xml.namespace.NamespaceContext ctx)
static DocumentBuilderFactoryBuilder
newLenientInstance()
Return a DocumentBuilderFactoryBuilder instance that explicitly does not mitigate against XXE and is also namespace aware.static DocumentBuilderFactoryBuilder
newLenientInstanceIfNull(DocumentBuilderFactoryBuilder b)
Convenient method to create a new default instance if required.static DocumentBuilderFactoryBuilder
newRestrictedInstance()
Create a New instance that disables Entityrefs and also mitigates against XXE viahttp://apache.org/xml/features/disallow-doctype-decl = true
.static DocumentBuilderFactoryBuilder
newRestrictedInstanceIfNull(DocumentBuilderFactoryBuilder b)
Convenient method to create a new default instance if required.void
setCoalescing(java.lang.Boolean coalescing)
CallsDocumentBuilderFactory.setCoalescing(boolean)
if non-nullvoid
setEntityResolver(org.xml.sax.EntityResolver entityResolver)
CallsDocumentBuilder.setEntityResolver(EntityResolver)
if non-null.void
setExpandEntityReferences(java.lang.Boolean expandEntityReferences)
WrapsDocumentBuilderFactory.setExpandEntityReferences(boolean)
.void
setFeatures(@NonNull KeyValuePairSet features)
CallsDocumentBuilderFactory.setFeature(String, boolean)
for each value defined.void
setIgnoreComments(java.lang.Boolean ignoreComments)
CallsDocumentBuilderFactory.setIgnoringComments(boolean)
if non-nullvoid
setIgnoreWhitespace(java.lang.Boolean ignoreWhitespace)
CallsDocumentBuilderFactory.setIgnoringElementContentWhitespace(boolean)
if non-nullvoid
setNamespaceAware(java.lang.Boolean namespaceAware)
CallsDocumentBuilderFactory.setNamespaceAware(boolean)
if non nullvoid
setValidating(java.lang.Boolean validating)
CallsDocumentBuilderFactory.setValidating(boolean)
if non-nullvoid
setXincludeAware(java.lang.Boolean xincludeAware)
CallsDocumentBuilderFactory.setXIncludeAware(boolean)
if non-nullDocumentBuilderFactoryBuilder
withCoalescing(java.lang.Boolean b)
DocumentBuilderFactoryBuilder
withEntityResolver(org.xml.sax.EntityResolver e)
DocumentBuilderFactoryBuilder
withExpandEntityReferences(java.lang.Boolean b)
DocumentBuilderFactoryBuilder
withFeatures(KeyValuePairSet v)
DocumentBuilderFactoryBuilder
withFeatures(java.util.Map<java.lang.String,java.lang.Boolean> f)
DocumentBuilderFactoryBuilder
withIgnoreComments(java.lang.Boolean b)
DocumentBuilderFactoryBuilder
withIgnoreWhitespace(java.lang.Boolean b)
DocumentBuilderFactoryBuilder
withNamespaceAware(java.lang.Boolean b)
DocumentBuilderFactoryBuilder
withNamespaceAware(javax.xml.namespace.NamespaceContext b)
DocumentBuilderFactoryBuilder
withValidating(java.lang.Boolean b)
DocumentBuilderFactoryBuilder
withXIncludeAware(java.lang.Boolean b)
-
-
-
Field Detail
-
DISABLE_DOCTYPE
public static final java.lang.String DISABLE_DOCTYPE
- See Also:
- Constant Field Values
-
-
Method Detail
-
newInstance
public static final DocumentBuilderFactoryBuilder newInstance()
Create a new instance that is namespace aware.- Returns:
- a new instance which is basically the same as
newRestrictedInstance()
.
-
newRestrictedInstance
public static final DocumentBuilderFactoryBuilder newRestrictedInstance()
Create a New instance that disables Entityrefs and also mitigates against XXE viahttp://apache.org/xml/features/disallow-doctype-decl = true
. This is added as a convenience so you don't have to keep configuring it if XXE is a bit of a bother for you.- Returns:
- a new instance.
-
newLenientInstance
public static final DocumentBuilderFactoryBuilder newLenientInstance()
Return a DocumentBuilderFactoryBuilder instance that explicitly does not mitigate against XXE and is also namespace aware.This is included for completeness, it's not expected that you'll want to use this, however if you were using
newInstance()
and you don't like the new defaults then well, here it is.- Returns:
- a new instance
-
newInstanceIfNull
public static final DocumentBuilderFactoryBuilder newInstanceIfNull(DocumentBuilderFactoryBuilder b)
Convenient method to create a new default instance if required.- Parameters:
b
- an existing configured DocumentBuilderFactoryBuilder, if null, thennewInstance()
is used.- Returns:
- a DocumentBuilderFactoryBuilder instance.
-
newRestrictedInstanceIfNull
public static final DocumentBuilderFactoryBuilder newRestrictedInstanceIfNull(DocumentBuilderFactoryBuilder b)
Convenient method to create a new default instance if required.- Parameters:
b
- an existing configured DocumentBuilderFactoryBuilder, if null, thennewRestrictedInstance()
is used.- Returns:
- a DocumentBuilderFactoryBuilder instance.
-
newLenientInstanceIfNull
public static final DocumentBuilderFactoryBuilder newLenientInstanceIfNull(DocumentBuilderFactoryBuilder b)
Convenient method to create a new default instance if required.- Parameters:
b
- an existing configured DocumentBuilderFactoryBuilder, if null, thennewLenientInstance()
is used.- Returns:
- a DocumentBuilderFactoryBuilder instance.
-
newInstanceIfNull
public static final DocumentBuilderFactoryBuilder newInstanceIfNull(DocumentBuilderFactoryBuilder b, javax.xml.namespace.NamespaceContext ctx)
-
configure
public javax.xml.parsers.DocumentBuilderFactory configure(javax.xml.parsers.DocumentBuilderFactory f) throws javax.xml.parsers.ParserConfigurationException
Configure a document builder factory- Parameters:
f
-- Returns:
- a reconfigured document builder factory
- Throws:
javax.xml.parsers.ParserConfigurationException
-
configure
public javax.xml.parsers.DocumentBuilder configure(javax.xml.parsers.DocumentBuilder db)
Configure a document builder.- Parameters:
db
-- Returns:
- a reconfigured document builder
-
newDocumentBuilder
public javax.xml.parsers.DocumentBuilder newDocumentBuilder(javax.xml.parsers.DocumentBuilderFactory f) throws javax.xml.parsers.ParserConfigurationException
Convenience to create a newDocumentBuilder
instance.- Parameters:
f
- a DocumentBuilderFactory- Returns:
- a configured DocumentBuilder
- Throws:
javax.xml.parsers.ParserConfigurationException
- See Also:
configure(DocumentBuilder)
,configure(DocumentBuilderFactory)
-
build
public javax.xml.parsers.DocumentBuilderFactory build() throws javax.xml.parsers.ParserConfigurationException
Create aDocumentBuilderFactory
.If all you're doing is creating a
DocumentBuilder
straight after calling this method, don't forget to callconfigure(DocumentBuilder)
to make sure you configure the underlyingDocumentBuilder
with any configuredgetEntityResolver()
.- Throws:
javax.xml.parsers.ParserConfigurationException
-
withFeatures
public DocumentBuilderFactoryBuilder withFeatures(java.util.Map<java.lang.String,java.lang.Boolean> f)
-
withFeatures
public DocumentBuilderFactoryBuilder withFeatures(KeyValuePairSet v)
-
addFeature
public DocumentBuilderFactoryBuilder addFeature(java.lang.String featureName, java.lang.Boolean value)
-
withValidating
public DocumentBuilderFactoryBuilder withValidating(java.lang.Boolean b)
-
withNamespaceAware
public DocumentBuilderFactoryBuilder withNamespaceAware(java.lang.Boolean b)
-
withNamespaceAware
public DocumentBuilderFactoryBuilder withNamespaceAware(javax.xml.namespace.NamespaceContext b)
-
withIgnoreWhitespace
public DocumentBuilderFactoryBuilder withIgnoreWhitespace(java.lang.Boolean b)
-
withExpandEntityReferences
public DocumentBuilderFactoryBuilder withExpandEntityReferences(java.lang.Boolean b)
-
withIgnoreComments
public DocumentBuilderFactoryBuilder withIgnoreComments(java.lang.Boolean b)
-
withCoalescing
public DocumentBuilderFactoryBuilder withCoalescing(java.lang.Boolean b)
-
withXIncludeAware
public DocumentBuilderFactoryBuilder withXIncludeAware(java.lang.Boolean b)
-
withEntityResolver
public DocumentBuilderFactoryBuilder withEntityResolver(org.xml.sax.EntityResolver e)
-
getFeatures
@NonNull public @NonNull KeyValuePairSet getFeatures()
CallsDocumentBuilderFactory.setFeature(String, boolean)
for each value defined.No validation of the features is done and are passed as-is through to the underlying DocumentBuilderFactory.
- Since:
- 4.0 By default the XML Feature "http://apache.org/xml/features/disallow-doctype-decl" will be set to true to disable doctype declarations.
-
setFeatures
public void setFeatures(@NonNull @NonNull KeyValuePairSet features)
CallsDocumentBuilderFactory.setFeature(String, boolean)
for each value defined.No validation of the features is done and are passed as-is through to the underlying DocumentBuilderFactory.
- Since:
- 4.0 By default the XML Feature "http://apache.org/xml/features/disallow-doctype-decl" will be set to true to disable doctype declarations.
-
getValidating
public java.lang.Boolean getValidating()
CallsDocumentBuilderFactory.setValidating(boolean)
if non-null
-
setValidating
public void setValidating(java.lang.Boolean validating)
CallsDocumentBuilderFactory.setValidating(boolean)
if non-null
-
getNamespaceAware
public java.lang.Boolean getNamespaceAware()
CallsDocumentBuilderFactory.setNamespaceAware(boolean)
if non null
-
setNamespaceAware
public void setNamespaceAware(java.lang.Boolean namespaceAware)
CallsDocumentBuilderFactory.setNamespaceAware(boolean)
if non null
-
getIgnoreWhitespace
public java.lang.Boolean getIgnoreWhitespace()
CallsDocumentBuilderFactory.setIgnoringElementContentWhitespace(boolean)
if non-null
-
setIgnoreWhitespace
public void setIgnoreWhitespace(java.lang.Boolean ignoreWhitespace)
CallsDocumentBuilderFactory.setIgnoringElementContentWhitespace(boolean)
if non-null
-
getExpandEntityReferences
public java.lang.Boolean getExpandEntityReferences()
WrapsDocumentBuilderFactory.setExpandEntityReferences(boolean)
.- Since:
- 4.0 If not specified, then the default is 'false' so that we mitigate against XXE attacks when parsing XML.
-
setExpandEntityReferences
public void setExpandEntityReferences(java.lang.Boolean expandEntityReferences)
WrapsDocumentBuilderFactory.setExpandEntityReferences(boolean)
.- Since:
- 4.0 If not specified, then the default is 'false' so that we mitigate against XXE attacks when parsing XML.
-
getIgnoreComments
public java.lang.Boolean getIgnoreComments()
CallsDocumentBuilderFactory.setIgnoringComments(boolean)
if non-null
-
setIgnoreComments
public void setIgnoreComments(java.lang.Boolean ignoreComments)
CallsDocumentBuilderFactory.setIgnoringComments(boolean)
if non-null
-
getCoalescing
public java.lang.Boolean getCoalescing()
CallsDocumentBuilderFactory.setCoalescing(boolean)
if non-null
-
setCoalescing
public void setCoalescing(java.lang.Boolean coalescing)
CallsDocumentBuilderFactory.setCoalescing(boolean)
if non-null
-
getXincludeAware
public java.lang.Boolean getXincludeAware()
CallsDocumentBuilderFactory.setXIncludeAware(boolean)
if non-null
-
setXincludeAware
public void setXincludeAware(java.lang.Boolean xincludeAware)
CallsDocumentBuilderFactory.setXIncludeAware(boolean)
if non-null
-
getEntityResolver
public org.xml.sax.EntityResolver getEntityResolver()
CallsDocumentBuilder.setEntityResolver(EntityResolver)
if non-null.
-
setEntityResolver
public void setEntityResolver(org.xml.sax.EntityResolver entityResolver)
CallsDocumentBuilder.setEntityResolver(EntityResolver)
if non-null.
-
-