Active content filtering: Custom HTML filters

You can develop your own custom filter. Using the steps described in this article, you can create and use a new filter.

You can develop a filter and then add filter rules to the ACF configuration file.

Developing the filter

Develop the filter by extending the com.ibm.trl.acf.api.html.Filter class. The filter class defines the following abstract methods that you need to implement:

  • startElement2
  • endElement2

These methods have the same semantics as startElement and endElement in the org.xm.sax.ContentHandler but they also require the scope declared in the configuration. You also need to implement the remaining methods in the org.xm.sax.ContentHandler. For example, to create a custom filter called MyFilter, use the following steps:

  1. Create a MyFilter class by extending the Filter class.
  2. Call the constructor of the super class in the constructor.
  3. Implement the actual process when each method is called.
    • Implement almost the same interface as org.xm.sax.ContentHandler except, instead of startElement and endElement, implement startElement2 and endElement2. The startElement2 method has the additional applicableRules argument. A list of applicable rules specified in the configuration file is passed to the argument. Lists of rules not in the current scope are not passed.
    • If you have nothing special to do in a method, you must propagate the events to the next handler.

Add filter rules to the ACF configuration file

See the Active content filtering article for more information about the configuration file. After configuration, you can access these values through the applicableRules attribute of startElement2.

The following example shows coding for the ACF configuration:

public class MyFilter extends com.ibm.trl.acf.api.Filter {
        
        public MyFilter(
        org.xml.sax.ContentHandler next, com.ibm.trl.acf.api.html.FilterRule filterRule,
        Boolean isProcess
        ) {
                // Call the constructor of the super class
                super(next, filterRule, isProcess);
        }
        
        public void startElement2(
        String uri, String localName, String qName, org.xml.sax.Attributes attributeList,
        java.util.List applicableRules
        ) throws org.xml.sax.SAXException {
                // Write the actual process for the start element.
                // The following example shows how to pass the SAX event to the next handler.
                next.startElement(uri, localName, qName, newAttributeList);
        }
        
        public void endElement2(
        String uri, String localName, String qName
        ) throws org.xml.sax.SAXException {
                // Write the actual process for the start element.
                // The following example shows how to pass the SAX event to the next handler.
                next.endElement(uri, localName, qName);
        }
        
        public void characters(
        char[] ch, int offset, int length
        ) throws org.xml.sax.SAXException {
                // Write the actual process for the start element.
                // The following example shows how to pass the SAX event to the next handler.
                next.characters(ch, offset, length);
        }
        
        // Need to implement the other methods.
        
}

Version 1.1.0.0.21442