Package org.apache.tika.sax
Class PhoneExtractingContentHandler
- java.lang.Object
-
- org.xml.sax.helpers.DefaultHandler
-
- org.apache.tika.sax.ContentHandlerDecorator
-
- org.apache.tika.sax.PhoneExtractingContentHandler
-
- All Implemented Interfaces:
org.xml.sax.ContentHandler
,org.xml.sax.DTDHandler
,org.xml.sax.EntityResolver
,org.xml.sax.ErrorHandler
public class PhoneExtractingContentHandler extends ContentHandlerDecorator
Class used to extract phone numbers while parsing. 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.
-
-
Constructor Summary
Constructors Constructor Description PhoneExtractingContentHandler(org.xml.sax.ContentHandler handler, Metadata metadata)
Creates a decorator for the given SAX event handler and Metadata object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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.-
Methods inherited from class org.apache.tika.sax.ContentHandlerDecorator
endElement, endPrefixMapping, ignorableWhitespace, processingInstruction, setDocumentLocator, skippedEntity, startDocument, startElement, startPrefixMapping, toString
-
-
-
-
Constructor Detail
-
PhoneExtractingContentHandler
public PhoneExtractingContentHandler(org.xml.sax.ContentHandler handler, Metadata metadata)
Creates a decorator for the given SAX event handler and Metadata object.- Parameters:
handler
- SAX event handler to be decorated
-
-
Method Detail
-
characters
public void characters(char[] ch, int start, int length) throws org.xml.sax.SAXException
The characters method is called whenever a Parser wants to pass raw... characters to the ContentHandler. But, sometimes, phone numbers are split accross different calls to characters, depending on the specific Parser used. So, we simply add all characters to a StringBuilder and analyze it once the document is finished.- Specified by:
characters
in interfaceorg.xml.sax.ContentHandler
- Overrides:
characters
in classContentHandlerDecorator
- Throws:
org.xml.sax.SAXException
-
endDocument
public void endDocument() throws org.xml.sax.SAXException
This method is called whenever the Parser is done parsing the file. So, we check the output for any phone numbers.- Specified by:
endDocument
in interfaceorg.xml.sax.ContentHandler
- Overrides:
endDocument
in classContentHandlerDecorator
- Throws:
org.xml.sax.SAXException
-
-