ElementRemover (NekoHTML 1.9.20 Implementation)
Date: 2014-02-13

org.cyberneko.html.filters

## Class ElementRemover

- java.lang.Object
- - org.cyberneko.html.filters.DefaultFilter
  - - org.cyberneko.html.filters.ElementRemover

- All Implemented Interfaces:
    HTMLComponent

      public class ElementRemover
      extends DefaultFilter

  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.

  Version:
    $Id: ElementRemover.java,v 1.5 2005/02/14 03:56:54 andyc Exp $
  Author:
    Andy Clark

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected java.util.Hashtable` | `fAcceptedElements` Accepted elements.
    `protected int` | `fElementDepth` The element depth.
    `protected int` | `fRemovalElementDepth` The element depth at element removal.
    `protected java.util.Hashtable` | `fRemovedElements` Removed elements.
    `protected static java.lang.Object` | `NULL` A "null" object.

    - ### Fields inherited from class org.cyberneko.html.filters.DefaultFilter

      `fDocumentHandler, fDocumentSource`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `ElementRemover()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `acceptElement(java.lang.String element, java.lang.String[] attributes)` Specifies that the given element should be accepted and, optionally, which attributes of that element should be kept.
    `void` | `characters(XMLString text, Augmentations augs)` Characters.
    `void` | `comment(XMLString text, Augmentations augs)` Comment.
    `protected boolean` | `elementAccepted(java.lang.String element)` Returns true if the specified element is accepted.
    `protected boolean` | `elementRemoved(java.lang.String element)` Returns true if the specified element should be removed.
    `void` | `emptyElement(QName element, XMLAttributes attributes, Augmentations augs)` Empty element.
    `void` | `endCDATA(Augmentations augs)` End CDATA section.
    `void` | `endElement(QName element, Augmentations augs)` End element.
    `void` | `endGeneralEntity(java.lang.String name, Augmentations augs)` End general entity.
    `void` | `endPrefixMapping(java.lang.String prefix, Augmentations augs)` End prefix mapping.
    `protected boolean` | `handleOpenTag(QName element, XMLAttributes attributes)` Handles an open tag.
    `void` | `ignorableWhitespace(XMLString text, Augmentations augs)` Ignorable whitespace.
    `void` | `processingInstruction(java.lang.String target, XMLString data, Augmentations augs)` Processing instruction.
    `void` | `removeElement(java.lang.String element)` Specifies that the given element should be completely removed.
    `void` | `startCDATA(Augmentations augs)` Start CDATA section.
    `void` | `startDocument(XMLLocator locator, java.lang.String encoding, Augmentations augs)` Start document.
    `void` | `startDocument(XMLLocator locator, java.lang.String encoding, NamespaceContext nscontext, Augmentations augs)` Start document.
    `void` | `startElement(QName element, XMLAttributes attributes, Augmentations augs)` Start element.
    `void` | `startGeneralEntity(java.lang.String name, XMLResourceIdentifier id, java.lang.String encoding, Augmentations augs)` Start general entity.
    `void` | `startPrefixMapping(java.lang.String prefix, java.lang.String uri, Augmentations augs)` Start prefix mapping.
    `void` | `textDecl(java.lang.String version, java.lang.String encoding, Augmentations augs)` Text declaration.

    - ### Methods inherited from class org.cyberneko.html.filters.DefaultFilter

      `doctypeDecl, endDocument, getDocumentHandler, getDocumentSource, getFeatureDefault, getPropertyDefault, getRecognizedFeatures, getRecognizedProperties, merge, reset, setDocumentHandler, setDocumentSource, setFeature, setProperty, xmlDecl`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### NULL

          protected static final java.lang.Object NULL

      A "null" object.

    - #### fAcceptedElements

          protected java.util.Hashtable fAcceptedElements

      Accepted elements.

    - #### fRemovedElements

          protected java.util.Hashtable fRemovedElements

      Removed elements.

    - #### fElementDepth

          protected int fElementDepth

      The element depth.

    - #### fRemovalElementDepth

          protected int fRemovalElementDepth

      The element depth at element removal.

  - ### Constructor Detail

    - #### ElementRemover

          public ElementRemover()

  - ### Method Detail

    - #### acceptElement

          public void acceptElement(java.lang.String element,
                           java.lang.String[] attributes)

      Specifies that the given element should be accepted and, optionally, which attributes of that element should be kept.
      Parameters:
        `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

    - #### removeElement

          public void removeElement(java.lang.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.
      Parameters:
        `element` - The element to completely remove.

    - #### startDocument

          public void startDocument(XMLLocator locator,
                           java.lang.String encoding,
                           NamespaceContext nscontext,
                           Augmentations augs)
                             throws XNIException

      Start document.
      Overrides:
        `startDocument` in class `DefaultFilter`
      Throws:
        `XNIException`

    - #### startDocument

          public void startDocument(XMLLocator locator,
                           java.lang.String encoding,
                           Augmentations augs)
                             throws XNIException

      Start document.
      Overrides:
        `startDocument` in class `DefaultFilter`
      Throws:
        `XNIException`

    - #### startPrefixMapping

          public void startPrefixMapping(java.lang.String prefix,
                                java.lang.String uri,
                                Augmentations augs)
                                  throws XNIException

      Start prefix mapping.
      Overrides:
        `startPrefixMapping` in class `DefaultFilter`
      Throws:
        `XNIException`

    - #### startElement

          public void startElement(QName element,
                          XMLAttributes attributes,
                          Augmentations augs)
                            throws XNIException

      Start element.
      Overrides:
        `startElement` in class `DefaultFilter`
      Throws:
        `XNIException`

    - #### emptyElement

          public void emptyElement(QName element,
                          XMLAttributes attributes,
                          Augmentations augs)
                            throws XNIException

      Empty element.
      Overrides:
        `emptyElement` in class `DefaultFilter`
      Throws:
        `XNIException`

    - #### comment

          public void comment(XMLString text,
                     Augmentations augs)
                       throws XNIException

      Comment.
      Overrides:
        `comment` in class `DefaultFilter`
      Throws:
        `XNIException`

    - #### processingInstruction

          public void processingInstruction(java.lang.String target,
                                   XMLString data,
                                   Augmentations augs)
                                     throws XNIException

      Processing instruction.
      Overrides:
        `processingInstruction` in class `DefaultFilter`
      Throws:
        `XNIException`

    - #### characters

          public void characters(XMLString text,
                        Augmentations augs)
                          throws XNIException

      Characters.
      Overrides:
        `characters` in class `DefaultFilter`
      Throws:
        `XNIException`

    - #### ignorableWhitespace

          public void ignorableWhitespace(XMLString text,
                                 Augmentations augs)
                                   throws XNIException

      Ignorable whitespace.
      Overrides:
        `ignorableWhitespace` in class `DefaultFilter`
      Throws:
        `XNIException`

    - #### startGeneralEntity

          public void startGeneralEntity(java.lang.String name,
                                XMLResourceIdentifier id,
                                java.lang.String encoding,
                                Augmentations augs)
                                  throws XNIException

      Start general entity.
      Overrides:
        `startGeneralEntity` in class `DefaultFilter`
      Throws:
        `XNIException`

    - #### textDecl

          public void textDecl(java.lang.String version,
                      java.lang.String encoding,
                      Augmentations augs)
                        throws XNIException

      Text declaration.
      Overrides:
        `textDecl` in class `DefaultFilter`
      Throws:
        `XNIException`

    - #### endGeneralEntity

          public void endGeneralEntity(java.lang.String name,
                              Augmentations augs)
                                throws XNIException

      End general entity.
      Overrides:
        `endGeneralEntity` in class `DefaultFilter`
      Throws:
        `XNIException`

    - #### startCDATA

          public void startCDATA(Augmentations augs)
                          throws XNIException

      Start CDATA section.
      Overrides:
        `startCDATA` in class `DefaultFilter`
      Throws:
        `XNIException`

    - #### endCDATA

          public void endCDATA(Augmentations augs)
                        throws XNIException

      End CDATA section.
      Overrides:
        `endCDATA` in class `DefaultFilter`
      Throws:
        `XNIException`

    - #### endElement

          public void endElement(QName element,
                        Augmentations augs)
                          throws XNIException

      End element.
      Overrides:
        `endElement` in class `DefaultFilter`
      Throws:
        `XNIException`

    - #### endPrefixMapping

          public void endPrefixMapping(java.lang.String prefix,
                              Augmentations augs)
                                throws XNIException

      End prefix mapping.
      Overrides:
        `endPrefixMapping` in class `DefaultFilter`
      Throws:
        `XNIException`

    - #### elementAccepted

          protected boolean elementAccepted(java.lang.String element)

      Returns true if the specified element is accepted.

    - #### elementRemoved

          protected boolean elementRemoved(java.lang.String element)

      Returns true if the specified element should be removed.

    - #### handleOpenTag

          protected boolean handleOpenTag(QName element,
                              XMLAttributes attributes)

      Handles an open tag.

(C) Copyright 2002-2014, Andy Clark, Marc Guillemot. All rights reserved.