public class AncestorXMLFilter extends XMLFilterImpl2
Example
With the following XML:
<root> <a/> <b> <c/> </b> </root>
When the startElement SAXEvent has been called for the element <c/>
, the ancestor stack will look like:
This allows your code (which extends AncestorXMLFilter) to query the stack to find out if there are ancestors which meet given
criteria (for example am I a descendant of <b/>)
.
This saves you from having to store lots of status information in your filter (e.g. booleans like inA
or
inB
).
Use
To make use of this filter, extend
it (rather than extending XMLFilterImpl
). Elements
are added to the stack in the AncestorXMLFilter.startElement(String, String, String, Attributes)
methods and removed from the stack in
the AncestorXMLFilter.endElement(String, String, String)
methods.
If you need to push/pop an element before you make your super.startElement call, for example if you're buffering, then you can
use AncestorXMLFilter.pushAncestorStack(String, String, String, Attributes)
and AncestorXMLFilter.popAncestorStack(String, String, String)
to
control the stack. These should be used in conjunction with AncestorXMLFilter.outputStartElement(String, String, String, Attributes)
and
AncestorXMLFilter.outputEndElement(String, String, String)
which are the same as startElement and endElement, but don't push/pop the
stack.
Constructor and Description |
---|
AncestorXMLFilter()
Constructs a new AncestorXMLFilter.
|
Modifier and Type | Method and Description |
---|---|
void |
endDocument()
Overrides the default
endDocument method. |
void |
endElement(java.lang.String uri,
java.lang.String localName,
java.lang.String qName)
Overrides the default
endElement method. |
java.lang.String |
getClosestAttributeValueFromAncestor(java.lang.String elementURI,
java.lang.String elementLocalName,
java.lang.String attrURI,
java.lang.String attrLocalName,
int ancestorLimit)
Returns the value of the given attribute on the ancestor of the given type that is nearest the top of the ancestor stack
which has a value for the given attribute.
|
java.lang.String |
getClosestAttributeValueFromAnyAncestor(java.lang.String attrURI,
java.lang.String attrLocalName,
int ancestorLimit)
Returns the value of the given attribute on the ancestor nearest the top of the ancestor stack which has a value for the
given attribute.
|
java.lang.String |
getGrandParentLocalName()
Returns the localName of the grand parent element.
|
java.lang.String |
getParentLocalName()
Returns the localName of the parent element.
|
java.lang.String |
getParentQName()
Returns the qName of the parent element.
|
java.lang.Object |
getProperty(java.lang.String name)
Reports a current property setting.
|
boolean |
hasAncestor(java.lang.String uri,
java.lang.String localName)
Looks for matching ancestors along the entire depth of the ancestor stack.
|
boolean |
hasAncestor(java.lang.String uri,
java.lang.String localName,
int ancestorLimit)
Looks for ancestors.
|
boolean |
hasAncestorWithAttr(java.lang.String elementURI,
java.lang.String elementLocalName,
java.lang.String attrURI,
java.lang.String attrLocalName,
int ancestorLimit)
Returns whether there is an ancestor with the given attribute.
|
boolean |
hasAncestorWithAttrValue(java.lang.String elementURI,
java.lang.String elementLocalName,
java.lang.String attrURI,
java.lang.String attrLocalName,
java.lang.String attrValue,
boolean ignoreCase,
int ancestorLimit)
Returns whether there is an ancestor of the given element type that has an attribute that matches the supplied value.
|
boolean |
hasAncestorWithAttrValues(java.lang.String elementURI,
java.lang.String elementLocalName,
java.lang.String attrURI,
java.lang.String attrLocalName,
java.lang.String[] allowedValues,
boolean ignoreCase,
int ancestorLimit)
Returns whether there is an ancestor of the given element type that has an attribute that matches the supplied values.
|
void |
outputCharacters(char[] ch,
int start,
int length)
Identical to the
characters method. |
void |
outputEndElement(java.lang.String uri,
java.lang.String localName,
java.lang.String qName)
Similar to endElement.
|
void |
outputStartElement(java.lang.String uri,
java.lang.String localName,
java.lang.String qName,
org.xml.sax.Attributes atts)
Similar to startElement.
|
void |
popAncestorStack(java.lang.String uri,
java.lang.String localName,
java.lang.String qName)
Checks that the top element of the stack is the same as the supplied element, if it is then pops the top of the stack.
|
void |
pushAncestorStack(java.lang.String uri,
java.lang.String localName,
java.lang.String qName,
org.xml.sax.Attributes atts)
Pushes the supplied element onto the ancestor stack.
|
void |
setProperty(java.lang.String name,
java.lang.Object value)
Specifies a property used to configure the filter.
|
int |
stackDepth()
Returns the current depth of the ancestor stack.
|
void |
startDocument()
Overrides the default
startDocument method. |
void |
startElement(java.lang.String uri,
java.lang.String localName,
java.lang.String qName,
org.xml.sax.Attributes atts)
Overrides the default
startElement method. |
attributeDecl, comment, elementDecl, endCDATA, endDTD, endEntity, externalEntityDecl, internalEntityDecl, parse, parse, startCDATA, startDTD, startEntity
characters, endPrefixMapping, error, fatalError, getContentHandler, getDTDHandler, getEntityResolver, getErrorHandler, getFeature, getParent, ignorableWhitespace, notationDecl, processingInstruction, resolveEntity, setContentHandler, setDocumentLocator, setDTDHandler, setEntityResolver, setErrorHandler, setFeature, setParent, skippedEntity, startPrefixMapping, unparsedEntityDecl, warning
public void setProperty(java.lang.String name, java.lang.Object value) throws org.xml.sax.SAXNotRecognizedException, org.xml.sax.SAXNotSupportedException
setProperty
in interface org.xml.sax.XMLReader
setProperty
in class XMLFilterImpl2
name
- the name of the property to be assignedvalue
- the new value of the propertyorg.xml.sax.SAXNotRecognizedException
- if the feature name is not recognizedorg.xml.sax.SAXNotSupportedException
- if it is not possible to configure the feature with the specified valueXMLFilterImpl.setProperty(String, Object)
,
XMLReader.setProperty(String, Object)
,
SAX Property
documentationpublic java.lang.Object getProperty(java.lang.String name) throws org.xml.sax.SAXNotRecognizedException, org.xml.sax.SAXNotSupportedException
getProperty
in interface org.xml.sax.XMLReader
getProperty
in class XMLFilterImpl2
name
- the name of the property to be reportedorg.xml.sax.SAXNotRecognizedException
- if the property name is not recognizedorg.xml.sax.SAXNotSupportedException
- if it is not possible to determine the property's valueXMLFilterImpl.getProperty(String)
,
XMLReader.getProperty(String)
,
SAX Property
documentationpublic void startElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes atts) throws org.xml.sax.SAXException
startElement
method. It calls
AncestorXMLFilter.pushAncestorStack(String, String, String, Attributes)
, which pushes the supplied element onto the ancestor stack.startElement
in interface org.xml.sax.ContentHandler
startElement
in class org.xml.sax.helpers.XMLFilterImpl
uri
- the element's namespace URIlocalName
- the element's localnameqName
- the element's qualified nameatts
- the element's attributesorg.xml.sax.SAXException
- the superclass may throw an exception during processing.XMLFilterImpl.startElement(String, String, String, Attributes)
,
XMLFilterImpl.startElement(String, String, String, Attributes)
public void endElement(java.lang.String uri, java.lang.String localName, java.lang.String qName) throws org.xml.sax.SAXException
endElement
method. It calls AncestorXMLFilter.popAncestorStack(String, String, String)
, which pops
the supplied element off the ancestor stack.endElement
in interface org.xml.sax.ContentHandler
endElement
in class org.xml.sax.helpers.XMLFilterImpl
uri
- the element's namespace URIlocalName
- the element's localnameqName
- the element's qualified nameorg.xml.sax.SAXException
- the superclass may throw an exception during processing.XMLFilterImpl.endElement(String, String, String)
,
XMLFilterImpl.endElement(String, String, String)
public void pushAncestorStack(java.lang.String uri, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes atts)
uri
- The element's NS uri.localName
- The element's localNameqName
- The element's qNameatts
- The element's attributesStack.push(Object)
public void popAncestorStack(java.lang.String uri, java.lang.String localName, java.lang.String qName) throws java.lang.IllegalStateException
uri
- The element's NS uri.localName
- The element's localNameqName
- The element's qNamejava.lang.IllegalStateException
- is thrown when the element at the top of the stack is not the same as the supplied element's
details.Stack.pop()
public void startDocument() throws org.xml.sax.SAXException
startDocument
method. Clears the ancestor stack.startDocument
in interface org.xml.sax.ContentHandler
startDocument
in class org.xml.sax.helpers.XMLFilterImpl
org.xml.sax.SAXException
- the superclass may throw an exception during processing.XMLFilterImpl.startDocument()
,
XMLFilterImpl.startDocument()
,
Vector.clear()
public void endDocument() throws org.xml.sax.SAXException
endDocument
method. Clears the ancestor stack.endDocument
in interface org.xml.sax.ContentHandler
endDocument
in class org.xml.sax.helpers.XMLFilterImpl
org.xml.sax.SAXException
- the superclass may throw an exception during processing. Also thrown if the stack is not empty.XMLFilterImpl.endDocument()
,
XMLFilterImpl.endDocument()
,
Vector.clear()
public java.lang.String getParentLocalName() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- when called before the first startElementpublic java.lang.String getParentQName() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- when called before the first startElementpublic java.lang.String getGrandParentLocalName() throws java.lang.IllegalStateException, java.lang.UnsupportedOperationException
java.lang.IllegalStateException
- when called before the first startElementjava.lang.UnsupportedOperationException
- when there is not enough depth in the ancestor stackpublic boolean hasAncestor(java.lang.String uri, java.lang.String localName, int ancestorLimit) throws java.lang.IllegalArgumentException
uri
- the namespace uri of the ancestor element to look forlocalName
- the localname of the ancestor element to look forancestorLimit
- The number of ancestors to consider. If negative unlimited ancestors are considered, otherwise a number
representing the number to consider; to only consider parents should be 1, grandparents 2 etc. If zero then will not
consider anything and will throw an IllegalArgumentException.java.lang.IllegalArgumentException
- when the ancestorLimit parameter is zeropublic boolean hasAncestor(java.lang.String uri, java.lang.String localName)
uri
- the namespace URI of the ancestor element to test forlocalName
- the localname of the ancestor to test forAncestorXMLFilter.hasAncestor(String, String, int)
public boolean hasAncestorWithAttr(java.lang.String elementURI, java.lang.String elementLocalName, java.lang.String attrURI, java.lang.String attrLocalName, int ancestorLimit)
elementURI
- The namespace URI of the element to matchelementLocalName
- The local name of the element to matchattrURI
- The namespace URI of the attribute to matchattrLocalName
- The local name of the attribute to matchancestorLimit
- The number of ancestors to consider. If negative unlimited ancestors are considered, otherwise a number
representing the number to consider; to only consider parents should be 1, grandparents 2 etc. If zero then will not
consider anything and will throw an IllegalArgumentException.public boolean hasAncestorWithAttrValue(java.lang.String elementURI, java.lang.String elementLocalName, java.lang.String attrURI, java.lang.String attrLocalName, java.lang.String attrValue, boolean ignoreCase, int ancestorLimit)
elementURI
- The namespace URI of the element to matchelementLocalName
- The local name of the element to matchattrURI
- The namespace URI of the attribute to matchattrLocalName
- The local name of the attribute to matchattrValue
- The value allowed for the attribute's valueignoreCase
- Whether to ignore the case of the attribute's valueancestorLimit
- The number of ancestors to consider. If negative unlimited ancestors are considered, otherwise a number
representing the number to consider; to only consider parents should be 1, grandparents 2 etc. If zero then will not
consider anything and will throw an IllegalArgumentException.public boolean hasAncestorWithAttrValues(java.lang.String elementURI, java.lang.String elementLocalName, java.lang.String attrURI, java.lang.String attrLocalName, java.lang.String[] allowedValues, boolean ignoreCase, int ancestorLimit)
elementURI
- The namespace URI of the element to matchelementLocalName
- The local name of the element to matchattrURI
- The namespace URI of the attribute to matchattrLocalName
- The local name of the attribute to matchallowedValues
- The values allowed for the attribute's valueignoreCase
- Whether to ignore the case of the attribute's valueancestorLimit
- The number of ancestors to consider. If negative unlimited ancestors are considered, otherwise a number
representing the number to consider; to only consider parents should be 1, grandparents 2 etc. If zero then will not
consider anything and will throw an IllegalArgumentException.public java.lang.String getClosestAttributeValueFromAncestor(java.lang.String elementURI, java.lang.String elementLocalName, java.lang.String attrURI, java.lang.String attrLocalName, int ancestorLimit)
elementURI
- The namespace URI of the element to matchelementLocalName
- The local name of the element to matchattrURI
- The namespace URI of the attribute to matchattrLocalName
- The local name of the attribute to matchancestorLimit
- The number of ancestors to consider. If negative unlimited ancestors are considered, otherwise a number
representing the number to consider; to only consider parents should be 1, grandparents 2 etc. If zero then will not
consider anything and will throw an IllegalArgumentException.public java.lang.String getClosestAttributeValueFromAnyAncestor(java.lang.String attrURI, java.lang.String attrLocalName, int ancestorLimit)
attrURI
- the namespace URI of the requested attributeattrLocalName
- the localname of the requested attributeancestorLimit
- The number of ancestors to consider. If negative unlimited ancestors are considered, otherwise a number
representing the number to consider; to only consider parents should be 1, grandparents 2 etc. If zero then will not
consider anything and will throw an IllegalArgumentException.public void outputStartElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes atts) throws org.xml.sax.SAXException
uri
- the element's namespace URIlocalName
- the element's localnameqName
- the element's qualified nameatts
- the element's attributesorg.xml.sax.SAXException
- the supertype may throw a SAXException during processingXMLFilterImpl.startElement(String, String, String, Attributes)
,
XMLFilterImpl.startElement(String, String, String, Attributes)
public void outputCharacters(char[] ch, int start, int length) throws org.xml.sax.SAXException
characters
method.ch
- an array of charactersstart
- the starting position in the arraylength
- the nu,ber of characters to use from the arrayorg.xml.sax.SAXException
- the supertype may throw a SAXException during processingXMLFilterImpl.characters(char[], int, int)
,
XMLFilterImpl.characters(char[], int, int)
public void outputEndElement(java.lang.String uri, java.lang.String localName, java.lang.String qName) throws org.xml.sax.SAXException
uri
- the element's namespace URIlocalName
- the element's localnameqName
- the element's qualified nameorg.xml.sax.SAXException
- the supertype may throw a SAXException during processingXMLFilterImpl.endElement(String, String, String)
,
XMLFilterImpl.endElement(String, String, String)
public int stackDepth()
0
is returned when the stack is currently empty and
that you're processing the root node.