com.aliasi.xml
Class ElementStackFilter

java.lang.Object
  extended by org.xml.sax.helpers.DefaultHandler
      extended by com.aliasi.xml.SimpleElementHandler
          extended by com.aliasi.xml.SAXFilterHandler
              extended by com.aliasi.xml.ElementStackFilter
All Implemented Interfaces:
ContentHandler, DTDHandler, EntityResolver, ErrorHandler
Direct Known Subclasses:
TextContentFilter, WellFormedFilter

public class ElementStackFilter
extends SAXFilterHandler

Provides a SAX filter that maintains a stack of elements visited. The qualified name of the current element and attributes are available through currentElement() and currentAttributes() respectively.

The implementation uses Attributes2Impl to copy the attributes from each element. This defends against the policy of some SAX parsers to re-use elements.

Since:
LingPipe1.0
Version:
3.8
Author:
Bob Carpenter

Field Summary
 
Fields inherited from class com.aliasi.xml.SAXFilterHandler
mHandler
 
Fields inherited from class com.aliasi.xml.SimpleElementHandler
CDATA_ATTS_TYPE, EMPTY_ATTS, NO_OP_DEFAULT_HANDLER
 
Constructor Summary
ElementStackFilter()
          Construct an element stack filter without a specified handler to which to delegate events.
ElementStackFilter(DefaultHandler handler)
          Construct an element stack filter that delegates events to the specified handler.
 
Method Summary
 Attributes currentAttributes()
          Returns the attributes as specified for the current containing element.
 String currentElement()
          Returns the qualified name of the current containing element's qualified name.
 void endElement(String namespaceURI, String localName, String qName)
          End the specified element, popping its qualified name and attributes off the stack, and delegating the SAX event to the contained handler.
 List<Attributes> getAttributesStack()
          Returns an unmodifiable view of the stack of attributes associated with the stack of elements.
 List<String> getElementStack()
          Returns an unmodifiable view of the stack of qualified names of elements.
 boolean noElement()
          Returns true if there are no elements in the stack.
 void startDocument()
          Start the document, clearing the element stack and delegating the start document even to the contained handler.
 void startElement(String namespaceURI, String localName, String qName, Attributes atts)
          Start the specified element, adding its qualified name and attributes to the stack, and delegating the SAX event to the contained handler.
 
Methods inherited from class com.aliasi.xml.SAXFilterHandler
characters, endDocument, endPrefixMapping, error, fatalError, ignorableWhitespace, notationDecl, processingInstruction, resolveEntity, setDocumentLocator, setHandler, skippedEntity, startPrefixMapping, unparsedEntityDecl, warning
 
Methods inherited from class com.aliasi.xml.SimpleElementHandler
addSimpleAttribute, characters, characters, characters, characters, createAttributes, createAttributes, createAttributes, createAttributes, createAttributes, createAttributes, endSimpleElement, endSimpleElement, startEndSimpleElement, startEndSimpleElement, startSimpleElement, startSimpleElement, startSimpleElement, startSimpleElement, startSimpleElement, startSimpleElement, startSimpleElement
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ElementStackFilter

public ElementStackFilter(DefaultHandler handler)
Construct an element stack filter that delegates events to the specified handler. The handler may be reset with SAXFilterHandler.setHandler(DefaultHandler).

Parameters:
handler - Handler to which events are delegated.

ElementStackFilter

public ElementStackFilter()
Construct an element stack filter without a specified handler to which to delegate events. Set the handler with SAXFilterHandler.setHandler(DefaultHandler).

Method Detail

getElementStack

public List<String> getElementStack()
Returns an unmodifiable view of the stack of qualified names of elements. The view will change as the underlying stack changes.

The elements are indexed from the bottom of the stack to the top. So getElementStack().size()-1 is the index of the top of a non-empty stack, and 0 is the index of the bottom of a non-empty stack.

See getAttributesStack() for a method to return the corresponding attributes for the elements.

Returns:
The stack of elements.

getAttributesStack

public List<Attributes> getAttributesStack()
Returns an unmodifiable view of the stack of attributes associated with the stack of elements. The view will change as the underlying stack changes.

The members of this stack are implementations of the XML SAX Attributes interface.

The elements are indexed from the bottom of the stack to the top. So getAttributesStack().size()-1 is the index of the top of a non-empty stack, and 0 is the index of the bottom of a non-empty stack.

See getElementStack() to get a parallel stack of qualified element names.

Returns:
The stack of attributes.

startDocument

public void startDocument()
                   throws SAXException
Start the document, clearing the element stack and delegating the start document even to the contained handler.

Specified by:
startDocument in interface ContentHandler
Overrides:
startDocument in class SAXFilterHandler
Throws:
SAXException - If there is an exception raised by the contained handler.

startElement

public void startElement(String namespaceURI,
                         String localName,
                         String qName,
                         Attributes atts)
                  throws SAXException
Start the specified element, adding its qualified name and attributes to the stack, and delegating the SAX event to the contained handler.

Specified by:
startElement in interface ContentHandler
Overrides:
startElement in class SAXFilterHandler
Parameters:
namespaceURI - The URI of the namespace for this element.
localName - The local name (without prefix) for this element.
qName - The qualified name (with prefix, if any) for this element.
atts - The attributes for this element.
Throws:
SAXException - If there is an exception raised by the contained handler.

endElement

public void endElement(String namespaceURI,
                       String localName,
                       String qName)
                throws SAXException
End the specified element, popping its qualified name and attributes off the stack, and delegating the SAX event to the contained handler.

Specified by:
endElement in interface ContentHandler
Overrides:
endElement in class SAXFilterHandler
Parameters:
namespaceURI - The URI of the namespace for this element.
localName - The local name (without prefix) for this element.
qName - The qualified name (with prefix, if any) for this element.
Throws:
SAXException - If there is an exception raised by the contained handler.

noElement

public boolean noElement()
Returns true if there are no elements in the stack. Should only return true outside of the top-level element.

Returns:
true if there are no elements in the stack.

currentElement

public String currentElement()
Returns the qualified name of the current containing element's qualified name. Should only be called when there is an element.

Returns:
Qualified name of current element.
Throws:
EmptyStackException - If there is no containing element.

currentAttributes

public Attributes currentAttributes()
Returns the attributes as specified for the current containing element. Should only be called when there is an element.

Returns:
Qualified name of current element.
Throws:
EmptyStackException - If there is no containing element.