org.apache.tika.sax
Class TaggedContentHandler

java.lang.Object
  extended by org.xml.sax.helpers.DefaultHandler
      extended by org.apache.tika.sax.ContentHandlerDecorator
          extended by org.apache.tika.sax.TaggedContentHandler
All Implemented Interfaces:
org.xml.sax.ContentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler

public class TaggedContentHandler
extends ContentHandlerDecorator

A content handler decorator that tags potential exceptions so that the handler that caused the exception can easily be identified. This is done by using the TaggedSAXException class to wrap all thrown SAXExceptions. See below for an example of using this class.

 TaggedContentHandler handler = new TaggedContentHandler(...);
 try {
     // Processing that may throw an SAXException either from this handler
     // or from some other XML parsing activity
     processXML(handler);
 } catch (SAXException e) {
     if (handler.isCauseOf(e)) {
         // The exception was caused by this handler.
         // Use e.getCause() to get the original exception.
     } else {
         // The exception was caused by something else.
     }
 }
 

Alternatively, the throwIfCauseOf(Exception) method can be used to let higher levels of code handle the exception caused by this stream while other processing errors are being taken care of at this lower level.

 TaggedContentHandler handler = new TaggedContentHandler(...);
 try {
     processXML(handler);
 } catch (SAXException e) {
     stream.throwIfCauseOf(e);
     // ... or process the exception that was caused by something else
 }
 

See Also:
TaggedSAXException

Constructor Summary
TaggedContentHandler(org.xml.sax.ContentHandler proxy)
          Creates a tagging decorator for the given content handler.
 
Method Summary
protected  void handleException(org.xml.sax.SAXException e)
          Tags any SAXExceptions thrown, wrapping and re-throwing.
 boolean isCauseOf(org.xml.sax.SAXException exception)
          Tests if the given exception was caused by this handler.
 void throwIfCauseOf(java.lang.Exception exception)
          Re-throws the original exception thrown by this handler.
 
Methods inherited from class org.apache.tika.sax.ContentHandlerDecorator
characters, endDocument, endElement, endPrefixMapping, ignorableWhitespace, processingInstruction, setContentHandler, setDocumentLocator, skippedEntity, startDocument, startElement, startPrefixMapping, toString
 
Methods inherited from class org.xml.sax.helpers.DefaultHandler
error, fatalError, notationDecl, resolveEntity, unparsedEntityDecl, warning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TaggedContentHandler

public TaggedContentHandler(org.xml.sax.ContentHandler proxy)
Creates a tagging decorator for the given content handler.

Parameters:
proxy - content handler to be decorated
Method Detail

isCauseOf

public boolean isCauseOf(org.xml.sax.SAXException exception)
Tests if the given exception was caused by this handler.

Parameters:
exception - an exception
Returns:
true if the exception was thrown by this handler, false otherwise

throwIfCauseOf

public void throwIfCauseOf(java.lang.Exception exception)
                    throws org.xml.sax.SAXException
Re-throws the original exception thrown by this handler. This method first checks whether the given exception is a TaggedSAXException wrapper created by this decorator, and then unwraps and throws the original wrapped exception. Returns normally if the exception was not thrown by this handler.

Parameters:
exception - an exception
Throws:
org.xml.sax.SAXException - original exception, if any, thrown by this handler

handleException

protected void handleException(org.xml.sax.SAXException e)
                        throws org.xml.sax.SAXException
Tags any SAXExceptions thrown, wrapping and re-throwing.

Overrides:
handleException in class ContentHandlerDecorator
Parameters:
e - The SAXException thrown
Throws:
org.xml.sax.SAXException - if an XML error occurs


Copyright © 2007-2010 The Apache Software Foundation. All Rights Reserved.