@ThreadSafe public class BasicParserPool extends AbstractInitializableComponent implements ParserPool
DocumentBuilders.
This is a pool implementation of the caching factory variety, and as such imposes no upper bound on the number of
DocumentBuilders allowed to be concurrently checked out and in use. It does however impose a limit on the size of the
internal cache of idle builder instances via the value configured via setMaxPoolSize(int).
Builders retrieved from this pool may (but are not required to) be returned to the pool with the method
returnBuilder(DocumentBuilder).
References to builders are kept by way of SoftReference so that the garbage collector may reap the builders
if the system is running out of memory.
This implementation of ParserPool does not allow its properties to be modified once it has been initialized.| Modifier and Type | Class and Description |
|---|---|
protected class |
BasicParserPool.DocumentBuilderProxy
A proxy that prevents the manages document builders retrieved from the parser pool.
|
| Modifier and Type | Field and Description |
|---|---|
private java.util.Map<java.lang.String,java.lang.Object> |
builderAttributes
Builder attributes.
|
private javax.xml.parsers.DocumentBuilderFactory |
builderFactory
Factory used to create new builders.
|
private java.util.Map<java.lang.String,java.lang.Boolean> |
builderFeatures
Builder features.
|
private java.util.Stack<java.lang.ref.SoftReference<javax.xml.parsers.DocumentBuilder>> |
builderPool
Cache of document builders.
|
private boolean |
coalescing
Whether the builders are coalescing.
|
private boolean |
dtdValidating
Whether the builder should validate.
|
private org.xml.sax.EntityResolver |
entityResolver
Entity resolver used by builders.
|
private org.xml.sax.ErrorHandler |
errorHandler
Error handler used by builders.
|
private boolean |
expandEntityReferences
Whether the builders expand entity references.
|
private boolean |
ignoreComments
Whether the builders ignore comments.
|
private boolean |
ignoreElementContentWhitespace
Whether the builders ignore element content whitespace.
|
private org.slf4j.Logger |
log
Class logger.
|
private int |
maxPoolSize
Max number of builders allowed in the pool.
|
private boolean |
namespaceAware
Whether the builders are namespace aware.
|
private javax.xml.validation.Schema |
schema
Schema used to validate parsed content.
|
private boolean |
xincludeAware
Whether the builders are XInclude aware.
|
| Constructor and Description |
|---|
BasicParserPool()
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
protected java.util.Map<java.lang.String,java.lang.Boolean> |
buildDefaultFeatures()
Build the default set of parser features to use.
|
private void |
checkInitializedNotDestroyed()
Helper method to test class state.
|
private void |
checkNotInitializedNotDestroyed()
Helper method to test class state.
|
protected javax.xml.parsers.DocumentBuilder |
createBuilder()
Creates a new document builder.
|
protected void |
doDestroy()
Performs component specific destruction logic.
|
protected void |
doInitialize()
Initialize the pool.
|
javax.xml.parsers.DocumentBuilder |
getBuilder()
Gets a builder from the pool.
|
java.util.Map<java.lang.String,java.lang.Object> |
getBuilderAttributes()
Gets the builder attributes used when creating builders.
|
java.util.Map<java.lang.String,java.lang.Boolean> |
getBuilderFeatures()
Gets the builders' features.
|
int |
getMaxPoolSize()
Gets the max number of builders the pool will hold.
|
protected int |
getPoolSize()
Gets the size of the current pool storage.
|
javax.xml.validation.Schema |
getSchema()
Gets the schema used to validate the XML document during the parsing process.
|
boolean |
isCoalescing()
Gets whether the builders are coalescing.
|
boolean |
isDTDValidating()
Gets whether the builders are validating.
|
boolean |
isExpandEntityReferences()
Gets whether builders expand entity references.
|
boolean |
isIgnoreComments()
Gets whether the builders ignore comments.
|
boolean |
isIgnoreElementContentWhitespace()
Get whether the builders ignore element content whitespace.
|
boolean |
isNamespaceAware()
Gets whether the builders are namespace aware.
|
boolean |
isXincludeAware()
Gets whether the builders are XInclude aware.
|
org.w3c.dom.Document |
newDocument()
Convenience method for creating a new document with a pooled builder.
|
org.w3c.dom.Document |
parse(java.io.InputStream input)
Convenience method for parsing an XML file using a pooled builder.
|
org.w3c.dom.Document |
parse(java.io.Reader input)
Convenience method for parsing an XML file using a pooled builder.
|
private void |
prepareBuilder(javax.xml.parsers.DocumentBuilder builder)
Prepare a document builder instance for use, before returning it from a checkout call.
|
void |
returnBuilder(javax.xml.parsers.DocumentBuilder builder)
Returns a builder to the pool.
|
void |
setBuilderAttributes(java.util.Map<java.lang.String,java.lang.Object> newAttributes)
Sets the builder attributes used when creating builders.
|
void |
setBuilderFeatures(java.util.Map<java.lang.String,java.lang.Boolean> newFeatures)
Sets the the builders' features.
|
void |
setCoalescing(boolean isCoalescing)
Sets whether the builders are coalescing.
|
void |
setDTDValidating(boolean isValidating)
Sets whether the builders are validating.
|
void |
setExpandEntityReferences(boolean expand)
Sets whether builders expand entity references.
|
void |
setIgnoreComments(boolean ignore)
Sets whether the builders ignore comments.
|
void |
setIgnoreElementContentWhitespace(boolean ignore)
Sets whether the builders ignore element content whitespace.
|
void |
setMaxPoolSize(int newSize)
Sets the max number of builders the pool will hold.
|
void |
setNamespaceAware(boolean isNamespaceAware)
Sets whether the builders are namespace aware.
|
void |
setSchema(javax.xml.validation.Schema newSchema)
Sets the schema used to validate the XML document during the parsing process.
|
void |
setXincludeAware(boolean isXIncludeAware)
Sets whether the builders are XInclude aware.
|
destroy, initialize, isDestroyed, isInitializedprivate final org.slf4j.Logger log
private javax.xml.parsers.DocumentBuilderFactory builderFactory
private final java.util.Stack<java.lang.ref.SoftReference<javax.xml.parsers.DocumentBuilder>> builderPool
private int maxPoolSize
private java.util.Map<java.lang.String,java.lang.Object> builderAttributes
private boolean coalescing
private boolean expandEntityReferences
private java.util.Map<java.lang.String,java.lang.Boolean> builderFeatures
private boolean ignoreComments
private boolean ignoreElementContentWhitespace
private boolean namespaceAware
private javax.xml.validation.Schema schema
private boolean dtdValidating
private boolean xincludeAware
private org.xml.sax.EntityResolver entityResolver
private final org.xml.sax.ErrorHandler errorHandler
@Nonnull
public javax.xml.parsers.DocumentBuilder getBuilder()
throws XMLParserException
getBuilder in interface ParserPoolXMLParserException - thrown if the document builder factory is misconfiguredpublic void returnBuilder(@Nullable
javax.xml.parsers.DocumentBuilder builder)
returnBuilder in interface ParserPoolbuilder - the builder to return@Nonnull
public org.w3c.dom.Document newDocument()
throws XMLParserException
newDocument in interface ParserPoolXMLParserException - thrown if there is a problem retrieving a builder@Nonnull
public org.w3c.dom.Document parse(@Nonnull
java.io.InputStream input)
throws XMLParserException
parse in interface ParserPoolinput - XML to parseXMLParserException - thrown if there is a problem retrieving a builder, the input stream can not be read,
or the XML was invalid@Nonnull
public org.w3c.dom.Document parse(@Nonnull
java.io.Reader input)
throws XMLParserException
parse in interface ParserPoolinput - XML to parseXMLParserException - thrown if there is a problem retrieving a builder, the input stream can not be read,
or the XML was invalidpublic int getMaxPoolSize()
public void setMaxPoolSize(int newSize)
newSize - max number of builders the pool will hold@Nonnull @NonnullElements public java.util.Map<java.lang.String,java.lang.Object> getBuilderAttributes()
public void setBuilderAttributes(@Nullable @NullableElements java.util.Map<java.lang.String,java.lang.Object> newAttributes)
newAttributes - builder attributes used when creating builderspublic boolean isCoalescing()
public void setCoalescing(boolean isCoalescing)
isCoalescing - whether the builders are coalescingpublic boolean isExpandEntityReferences()
public void setExpandEntityReferences(boolean expand)
expand - whether builders expand entity references@Nonnull @NonnullElements @Unmodifiable public java.util.Map<java.lang.String,java.lang.Boolean> getBuilderFeatures()
public void setBuilderFeatures(@Nullable @NullableElements java.util.Map<java.lang.String,java.lang.Boolean> newFeatures)
newFeatures - the builders' featurespublic boolean isIgnoreComments()
public void setIgnoreComments(boolean ignore)
ignore - The ignoreComments to set.public boolean isIgnoreElementContentWhitespace()
public void setIgnoreElementContentWhitespace(boolean ignore)
ignore - whether the builders ignore element content whitespacepublic boolean isNamespaceAware()
public void setNamespaceAware(boolean isNamespaceAware)
isNamespaceAware - whether the builders are namespace aware@Nullable public javax.xml.validation.Schema getSchema()
public void setSchema(@Nullable
javax.xml.validation.Schema newSchema)
newSchema - schema used to validate the XML document during the parsing processpublic boolean isDTDValidating()
public void setDTDValidating(boolean isValidating)
isValidating - whether the builders are validatingpublic boolean isXincludeAware()
public void setXincludeAware(boolean isXIncludeAware)
isXIncludeAware - whether the builders are XInclude awareprotected int getPoolSize()
@Nonnull
protected javax.xml.parsers.DocumentBuilder createBuilder()
throws XMLParserException
XMLParserException - thrown if their is a configuration error with the builder factoryprivate void prepareBuilder(@Nonnull
javax.xml.parsers.DocumentBuilder builder)
builder - the document builder to prepareprotected void doInitialize()
throws ComponentInitializationException
doInitialize in class AbstractInitializableComponentComponentInitializationException - thrown if pool can not be initialized, or if it is already initialized
thrown if there is a problem initializing the componentprotected void doDestroy()
doDestroy in class AbstractInitializableComponentprivate void checkInitializedNotDestroyed()
private void checkNotInitializedNotDestroyed()
protected java.util.Map<java.lang.String,java.lang.Boolean> buildDefaultFeatures()
These will be overriden by a call to setBuilderFeatures(Map).
The default features set are:
XMLConstants.FEATURE_SECURE_PROCESSING = true