| java.lang.Object org.cyberneko.html.filters.DefaultFilter org.cyberneko.html.filters.ElementRemover
ElementRemover | public class ElementRemover extends DefaultFilter (Code) | | 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.
author: Andy Clark version: $Id: ElementRemover.java,v 1.5 2005/02/14 03:56:54 andyc Exp $ |
Method Summary | |
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. | public void | characters(XMLString text, Augmentations augs) Characters. | public void | comment(XMLString text, Augmentations augs) Comment. | protected boolean | elementAccepted(String element) Returns true if the specified element is accepted. | protected boolean | elementRemoved(String element) Returns true if the specified element should be removed. | public void | emptyElement(QName element, XMLAttributes attributes, Augmentations augs) Empty element. | public void | endCDATA(Augmentations augs) End CDATA section. | public void | endElement(QName element, Augmentations augs) End element. | public void | endGeneralEntity(String name, Augmentations augs) End general entity. | public void | endPrefixMapping(String prefix, Augmentations augs) End prefix mapping. | protected boolean | handleOpenTag(QName element, XMLAttributes attributes) Handles an open tag. | public void | ignorableWhitespace(XMLString text, Augmentations augs) Ignorable whitespace. | public void | processingInstruction(String target, XMLString data, Augmentations augs) Processing instruction. | public void | removeElement(String element) Specifies that the given element should be completely removed. | public void | startCDATA(Augmentations augs) Start CDATA section. | public void | startDocument(XMLLocator locator, String encoding, NamespaceContext nscontext, Augmentations augs) Start document. | public void | startDocument(XMLLocator locator, String encoding, Augmentations augs) Start document. | public void | startElement(QName element, XMLAttributes attributes, Augmentations augs) Start element. | public void | startGeneralEntity(String name, XMLResourceIdentifier id, String encoding, Augmentations augs) Start general entity. | public void | startPrefixMapping(String prefix, String uri, Augmentations augs) Start prefix mapping. | public void | textDecl(String version, String encoding, Augmentations augs) Text declaration. |
NULL | final protected static Object NULL(Code) | | A "null" object.
|
fAcceptedElements | protected Hashtable fAcceptedElements(Code) | | Accepted elements.
|
fElementDepth | protected int fElementDepth(Code) | | The element depth.
|
fRemovalElementDepth | protected int fRemovalElementDepth(Code) | | The element depth at element removal.
|
fRemovedElements | protected Hashtable fRemovedElements(Code) | | Removed elements.
|
acceptElement | public void acceptElement(String element, String[] attributes)(Code) | | Specifies that the given element should be accepted and, optionally,
which attributes of that element should be kept.
Parameters: element - The element to accept. Parameters: attributes - The list of attributes to be kept or null if noattributes should be kept for this element.see #removeElement |
characters | public void characters(XMLString text, Augmentations augs) throws XNIException(Code) | | Characters.
|
comment | public void comment(XMLString text, Augmentations augs) throws XNIException(Code) | | Comment.
|
elementAccepted | protected boolean elementAccepted(String element)(Code) | | Returns true if the specified element is accepted.
|
elementRemoved | protected boolean elementRemoved(String element)(Code) | | Returns true if the specified element should be removed.
|
emptyElement | public void emptyElement(QName element, XMLAttributes attributes, Augmentations augs) throws XNIException(Code) | | Empty element.
|
endCDATA | public void endCDATA(Augmentations augs) throws XNIException(Code) | | End CDATA section.
|
endElement | public void endElement(QName element, Augmentations augs) throws XNIException(Code) | | End element.
|
endGeneralEntity | public void endGeneralEntity(String name, Augmentations augs) throws XNIException(Code) | | End general entity.
|
endPrefixMapping | public void endPrefixMapping(String prefix, Augmentations augs) throws XNIException(Code) | | End prefix mapping.
|
handleOpenTag | protected boolean handleOpenTag(QName element, XMLAttributes attributes)(Code) | | Handles an open tag.
|
ignorableWhitespace | public void ignorableWhitespace(XMLString text, Augmentations augs) throws XNIException(Code) | | Ignorable whitespace.
|
processingInstruction | public void processingInstruction(String target, XMLString data, Augmentations augs) throws XNIException(Code) | | Processing instruction.
|
removeElement | public void removeElement(String element)(Code) | | 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.
Parameters: element - The element to completely remove. |
startCDATA | public void startCDATA(Augmentations augs) throws XNIException(Code) | | Start CDATA section.
|
startDocument | public void startDocument(XMLLocator locator, String encoding, NamespaceContext nscontext, Augmentations augs) throws XNIException(Code) | | Start document.
|
startDocument | public void startDocument(XMLLocator locator, String encoding, Augmentations augs) throws XNIException(Code) | | Start document.
|
startElement | public void startElement(QName element, XMLAttributes attributes, Augmentations augs) throws XNIException(Code) | | Start element.
|
startGeneralEntity | public void startGeneralEntity(String name, XMLResourceIdentifier id, String encoding, Augmentations augs) throws XNIException(Code) | | Start general entity.
|
startPrefixMapping | public void startPrefixMapping(String prefix, String uri, Augmentations augs) throws XNIException(Code) | | Start prefix mapping.
|
textDecl | public void textDecl(String version, String encoding, Augmentations augs) throws XNIException(Code) | | Text declaration.
|
Fields inherited from org.cyberneko.html.filters.DefaultFilter | protected XMLDocumentHandler fDocumentHandler(Code)(Java Doc) protected XMLDocumentSource fDocumentSource(Code)(Java Doc)
|
Methods inherited from org.cyberneko.html.filters.DefaultFilter | public void characters(XMLString text, Augmentations augs) throws XNIException(Code)(Java Doc) public void comment(XMLString text, Augmentations augs) throws XNIException(Code)(Java Doc) public void doctypeDecl(String root, String publicId, String systemId, Augmentations augs) throws XNIException(Code)(Java Doc) public void emptyElement(QName element, XMLAttributes attributes, Augmentations augs) throws XNIException(Code)(Java Doc) public void endCDATA(Augmentations augs) throws XNIException(Code)(Java Doc) public void endDocument(Augmentations augs) throws XNIException(Code)(Java Doc) public void endElement(QName element, Augmentations augs) throws XNIException(Code)(Java Doc) public void endGeneralEntity(String name, Augmentations augs) throws XNIException(Code)(Java Doc) public void endPrefixMapping(String prefix, Augmentations augs) throws XNIException(Code)(Java Doc) public XMLDocumentHandler getDocumentHandler()(Code)(Java Doc) public XMLDocumentSource getDocumentSource()(Code)(Java Doc) public Boolean getFeatureDefault(String featureId)(Code)(Java Doc) public Object getPropertyDefault(String propertyId)(Code)(Java Doc) public String[] getRecognizedFeatures()(Code)(Java Doc) public String[] getRecognizedProperties()(Code)(Java Doc) public void ignorableWhitespace(XMLString text, Augmentations augs) throws XNIException(Code)(Java Doc) protected static String[] merge(String[] array1, String[] array2)(Code)(Java Doc) public void processingInstruction(String target, XMLString data, Augmentations augs) throws XNIException(Code)(Java Doc) public void reset(XMLComponentManager componentManager) throws XMLConfigurationException(Code)(Java Doc) public void setDocumentHandler(XMLDocumentHandler handler)(Code)(Java Doc) public void setDocumentSource(XMLDocumentSource source)(Code)(Java Doc) public void setFeature(String featureId, boolean state) throws XMLConfigurationException(Code)(Java Doc) public void setProperty(String propertyId, Object value) throws XMLConfigurationException(Code)(Java Doc) public void startCDATA(Augmentations augs) throws XNIException(Code)(Java Doc) public void startDocument(XMLLocator locator, String encoding, NamespaceContext nscontext, Augmentations augs) throws XNIException(Code)(Java Doc) public void startDocument(XMLLocator locator, String encoding, Augmentations augs) throws XNIException(Code)(Java Doc) public void startElement(QName element, XMLAttributes attributes, Augmentations augs) throws XNIException(Code)(Java Doc) public void startGeneralEntity(String name, XMLResourceIdentifier id, String encoding, Augmentations augs) throws XNIException(Code)(Java Doc) public void startPrefixMapping(String prefix, String uri, Augmentations augs) throws XNIException(Code)(Java Doc) public void textDecl(String version, String encoding, Augmentations augs) throws XNIException(Code)(Java Doc) public void xmlDecl(String version, String encoding, String standalone, Augmentations augs) throws XNIException(Code)(Java Doc)
|
|
|