public class RecursiveParserWrapper extends java.lang.Object implements Parser
After parsing a document, call getMetadata() to retrieve a list of Metadata objects, one for each embedded resource. The first item in the list will contain the Metadata for the outer container file.
Content can also be extracted and stored in the TIKA_CONTENT
field
of a Metadata object. Select the type of content to be stored
at initialization.
If a WriteLimitReachedException is encountered, the wrapper will stop processing the current resource, and it will not process any of the child resources for the given resource. However, it will try to parse as much as it can. If a WLRE is reached in the parent document, no child resources will be parsed.
The implementation is based on Jukka's RecursiveMetadataParser and Nick's additions. See: RecursiveMetadataParser.
Note that this wrapper holds all data in memory and is not appropriate for files with content too large to be held in memory.
Note, too, that this wrapper is not thread safe because it stores state.
The client must initialize a new wrapper for each thread, and the client
is responsible for calling reset()
after each parse.
The unit tests for this class are in the tika-parsers module.
Modifier and Type | Class and Description |
---|---|
private class |
RecursiveParserWrapper.EmbeddedParserDecorator |
Modifier and Type | Field and Description |
---|---|
private boolean |
catchEmbeddedExceptions |
private ContentHandlerFactory |
contentHandlerFactory |
static Property |
EMBEDDED_EXCEPTION |
static Property |
EMBEDDED_RESOURCE_LIMIT_REACHED |
static Property |
EMBEDDED_RESOURCE_PATH |
private boolean |
hitMaxEmbeddedResources |
private int |
maxEmbeddedResources |
private java.util.List<Metadata> |
metadatas |
static Property |
PARSE_TIME_MILLIS |
private static long |
serialVersionUID
Generated serial version
|
static Property |
TIKA_CONTENT |
private int |
unknownCount |
private Parser |
wrappedParser |
static Property |
WRITE_LIMIT_REACHED |
Constructor and Description |
---|
RecursiveParserWrapper(Parser wrappedParser,
ContentHandlerFactory contentHandlerFactory)
Initialize the wrapper with
catchEmbeddedExceptions set
to true as default. |
RecursiveParserWrapper(Parser wrappedParser,
ContentHandlerFactory contentHandlerFactory,
boolean catchEmbeddedExceptions)
Initialize the wrapper.
|
Modifier and Type | Method and Description |
---|---|
private void |
addContent(org.xml.sax.ContentHandler handler,
Metadata metadata) |
private Metadata |
deepCopy(Metadata m) |
java.util.List<Metadata> |
getMetadata()
The first element in the returned list represents the
data from the outer container file.
|
private java.lang.String |
getResourceName(Metadata metadata) |
java.util.Set<MediaType> |
getSupportedTypes(ParseContext context)
Returns the set of media types supported by this parser when used
with the given parse context.
|
private boolean |
isWriteLimitReached(java.lang.Throwable t)
Copied/modified from WriteOutContentHandler.
|
void |
parse(java.io.InputStream stream,
org.xml.sax.ContentHandler ignore,
Metadata metadata,
ParseContext context)
Acts like a regular parser except it ignores the ContentHandler
and it automatically sets/overwrites the embedded Parser in the
ParseContext object.
|
void |
reset()
This clears the metadata list and resets
unknownCount and
hitMaxEmbeddedResources |
void |
setMaxEmbeddedResources(int max)
Set the maximum number of embedded resources to store.
|
private static final long serialVersionUID
public static final Property TIKA_CONTENT
public static final Property PARSE_TIME_MILLIS
public static final Property WRITE_LIMIT_REACHED
public static final Property EMBEDDED_RESOURCE_LIMIT_REACHED
public static final Property EMBEDDED_EXCEPTION
public static final Property EMBEDDED_RESOURCE_PATH
private final Parser wrappedParser
private final ContentHandlerFactory contentHandlerFactory
private final java.util.List<Metadata> metadatas
private final boolean catchEmbeddedExceptions
private int unknownCount
private int maxEmbeddedResources
private boolean hitMaxEmbeddedResources
public RecursiveParserWrapper(Parser wrappedParser, ContentHandlerFactory contentHandlerFactory)
catchEmbeddedExceptions
set
to true
as default.wrappedParser
- parser to use for the container documents and the embedded documentscontentHandlerFactory
- factory to use to generate a new content handler for
the container document and each embedded documentpublic RecursiveParserWrapper(Parser wrappedParser, ContentHandlerFactory contentHandlerFactory, boolean catchEmbeddedExceptions)
wrappedParser
- parser to use for the container documents and the embedded documentscontentHandlerFactory
- factory to use to generate a new content handler for
the container document and each embedded documentcatchEmbeddedExceptions
- whether or not to catch the embedded exceptions.
If set to true
, the stack traces will be stored in
the metadata object with key: EMBEDDED_EXCEPTION
.public java.util.Set<MediaType> getSupportedTypes(ParseContext context)
Parser
getSupportedTypes
in interface Parser
context
- parse contextpublic void parse(java.io.InputStream stream, org.xml.sax.ContentHandler ignore, Metadata metadata, ParseContext context) throws java.io.IOException, org.xml.sax.SAXException, TikaException
To retrieve the results of the parse, use getMetadata()
.
Make sure to call reset()
after each parse.
parse
in interface Parser
stream
- the document stream (input)ignore
- handler for the XHTML SAX events (output)metadata
- document metadata (input and output)context
- parse contextjava.io.IOException
- if the document stream could not be readorg.xml.sax.SAXException
- if the SAX events could not be processedTikaException
- if the document could not be parsedpublic java.util.List<Metadata> getMetadata()
public void setMaxEmbeddedResources(int max)
EMBEDDED_RESOURCE_LIMIT_REACHED
property will be added to the container document's Metadata.
If this value is < 0 (the default), the wrapper will store all Metadata.
max
- maximum number of embedded resources to storepublic void reset()
unknownCount
and
hitMaxEmbeddedResources
private boolean isWriteLimitReached(java.lang.Throwable t)
t
- private java.lang.String getResourceName(Metadata metadata)
private void addContent(org.xml.sax.ContentHandler handler, Metadata metadata)