This class is a document filter capable of removing specified
elements from the processing stream. There are two options for
processing document elements:
- specifying those elements which should be accepted and,
optionally, which attributes of that element should be
kept; and
- specifying those elements whose tags and content should be
completely removed from the event stream.
The first option allows the application to specify which elements
appearing in the event stream should be accepted and, therefore,
passed on to the next stage in the pipeline. All elements
not in the list of acceptable elements have their start
and end tags stripped from the event stream
unless those
elements appear in the list of elements to be removed.
The second option allows the application to specify which elements
should be completely removed from the event stream. When an element
appears that is to be removed, the element's start and end tag as
well as all of that element's content is removed from the event
stream.
A common use of this filter would be to only allow rich-text
and linking elements as well as the character content to pass
through the filter — all other elements would be stripped.
The following code shows how to configure this filter to perform
this task:
ElementRemover remover = new ElementRemover();
remover.acceptElement("b", null);
remover.acceptElement("i", null);
remover.acceptElement("u", null);
remover.acceptElement("a", new String[] { "href" });
However, this would still allow the text content of other
elements to pass through, which may not be desirable. In order
to further "clean" the input, the
removeElement
option can be used. The following piece of code adds the ability
to completely remove any <SCRIPT> tags and content
from the stream.
remover.removeElement("script");
Note:
All text and accepted element children of a stripped element is
retained. To completely remove an element's content, use the
removeElement
method.
Note:
Care should be taken when using this filter because the output
may not be a well-balanced tree. Specifically, if the application
removes the <HTML> element (with or without retaining its
children), the resulting document event stream will no longer be
well-formed.
acceptElement
public void acceptElement(String element,
String[] attributes)
Specifies that the given element should be accepted and, optionally,
which attributes of that element should be kept.
element
- The element to accept.attributes
- The list of attributes to be kept or null if no
attributes should be kept for this element.
see #removeElement
characters
public void characters(XMLString text,
Augmentations augs)
throws XNIException
Characters.
- characters in interface DefaultFilter
comment
public void comment(XMLString text,
Augmentations augs)
throws XNIException
Comment.
- comment in interface DefaultFilter
elementAccepted
protected boolean elementAccepted(String element)
Returns true if the specified element is accepted.
elementRemoved
protected boolean elementRemoved(String element)
Returns true if the specified element should be removed.
emptyElement
public void emptyElement(QName element,
XMLAttributes attributes,
Augmentations augs)
throws XNIException
Empty element.
- emptyElement in interface DefaultFilter
endCDATA
public void endCDATA(Augmentations augs)
throws XNIException
End CDATA section.
- endCDATA in interface DefaultFilter
endElement
public void endElement(QName element,
Augmentations augs)
throws XNIException
End element.
- endElement in interface DefaultFilter
endGeneralEntity
public void endGeneralEntity(String name,
Augmentations augs)
throws XNIException
End general entity.
- endGeneralEntity in interface DefaultFilter
endPrefixMapping
public void endPrefixMapping(String prefix,
Augmentations augs)
throws XNIException
End prefix mapping.
- endPrefixMapping in interface DefaultFilter
handleOpenTag
protected boolean handleOpenTag(QName element,
XMLAttributes attributes)
Handles an open tag.
ignorableWhitespace
public void ignorableWhitespace(XMLString text,
Augmentations augs)
throws XNIException
Ignorable whitespace.
- ignorableWhitespace in interface DefaultFilter
processingInstruction
public void processingInstruction(String target,
XMLString data,
Augmentations augs)
throws XNIException
Processing instruction.
- processingInstruction in interface DefaultFilter
removeElement
public void removeElement(String element)
Specifies that the given element should be completely removed. If an
element is encountered during processing that is on the remove list,
the element's start and end tags as well as all of content contained
within the element will be removed from the processing stream.
element
- The element to completely remove.
startCDATA
public void startCDATA(Augmentations augs)
throws XNIException
Start CDATA section.
- startCDATA in interface DefaultFilter
startDocument
public void startDocument(XMLLocator locator,
String encoding,
Augmentations augs)
throws XNIException
Start document.
- startDocument in interface DefaultFilter
startDocument
public void startDocument(XMLLocator locator,
String encoding,
NamespaceContext nscontext,
Augmentations augs)
throws XNIException
Start document.
- startDocument in interface DefaultFilter
startElement
public void startElement(QName element,
XMLAttributes attributes,
Augmentations augs)
throws XNIException
Start element.
- startElement in interface DefaultFilter
startGeneralEntity
public void startGeneralEntity(String name,
XMLResourceIdentifier id,
String encoding,
Augmentations augs)
throws XNIException
Start general entity.
- startGeneralEntity in interface DefaultFilter
startPrefixMapping
public void startPrefixMapping(String prefix,
String uri,
Augmentations augs)
throws XNIException
Start prefix mapping.
- startPrefixMapping in interface DefaultFilter
textDecl
public void textDecl(String version,
String encoding,
Augmentations augs)
throws XNIException
Text declaration.
- textDecl in interface DefaultFilter