public class PhoneExtractingContentHandler extends ContentHandlerDecorator
Every time a document is parsed in Tika, the content is split into SAX events. Those SAX events are handled by a ContentHandler. You can think of these events as marking a tag in an HTML file. Once you're finished parsing, you can call handler.toString(), for example, to get the text contents of the file. On the other hand, any of the metadata of the file will be added to the Metadata object passed in during the parse() call. So, the Parser class sends metadata to the Metadata object and content to the ContentHandler.
This class is an example of how to combine a ContentHandler and a Metadata. As content is passed to the handler, we first check to see if it matches a textual pattern for a phone number. If the extracted content is a phone number, we add it to the metadata under the key "phonenumbers". So, if you used this ContentHandler when you parsed a document, then called metadata.getValues("phonenumbers"), you would get an array of Strings of phone numbers found in the document.
Please see the PhoneExtractingContentHandlerTest for an example of how to use this class.
Modifier | Constructor and Description |
---|---|
protected |
PhoneExtractingContentHandler()
Creates a decorator that by default forwards incoming SAX events to
a dummy content handler that simply ignores all the events.
|
|
PhoneExtractingContentHandler(ContentHandler handler,
Metadata metadata)
Creates a decorator for the given SAX event handler and Metadata object.
|
Modifier and Type | Method and Description |
---|---|
void |
characters(char[] ch,
int start,
int length)
The characters method is called whenever a Parser wants to pass raw...
|
void |
endDocument()
This method is called whenever the Parser is done parsing the file.
|
endElement, endPrefixMapping, handleException, ignorableWhitespace, processingInstruction, setContentHandler, setDocumentLocator, skippedEntity, startDocument, startElement, startPrefixMapping, toString
error, fatalError, notationDecl, resolveEntity, unparsedEntityDecl, warning
public PhoneExtractingContentHandler(ContentHandler handler, Metadata metadata)
handler
- SAX event handler to be decoratedprotected PhoneExtractingContentHandler()
ContentHandlerDecorator.setContentHandler(ContentHandler)
method to
switch to a more usable underlying content handler.
Also creates a dummy Metadata object to store phone numbers in.public void characters(char[] ch, int start, int length) throws SAXException
characters
in interface ContentHandler
characters
in class ContentHandlerDecorator
SAXException
public void endDocument() throws SAXException
endDocument
in interface ContentHandler
endDocument
in class ContentHandlerDecorator
SAXException
Copyright © 2007–2022 The Apache Software Foundation. All rights reserved.