Code Listings

Listing 1: VerboseContentHandler
import org.xml.sax.SAXException;
import org.xml.sax.Attributes;

public class VerboseContentHandler 
       implements org.xml.sax.ContentHandler {

    public void startDocument( ) throws SAXException
    {
        System.out.println( "\n---- startDocument() called." );
    }

    public void endDocument() throws SAXException
    {
        System.out.println( "\n---- endDocument() called." );
    }

    public void startPrefixMapping( java.lang.String prefix,  
                                    java.lang.String uri )
                                    throws SAXException
    { 
        System.out.println( "\n---- startPrefixMapping:" + 
                            " prefix = " + prefix + 
                            " uri = " + uri );
    }

    public void endPrefixMapping( java.lang.String prefix )
                                  throws SAXException
    {
        System.out.println( "\n ---- endPrefixMapping: prefix = " 
                            + prefix );
    }

    public void startElement( java.lang.String uri, 
                              java.lang.String localName,  
                              java.lang.String qName, 
                              Attributes attrs )
                              throws SAXException
    {   
        System.out.println("\n---- startElement: " );
        System.out.println("      uri =  '" + uri + "'");
        System.out.println("localName =  '" + localName + "'" );
        System.out.println("    qName =  '" + qName + "'“ );
        if (attrs.getLength() > 0) {
            System.out.println(" Attributes:" );
            System.out.println("    LocalName \tQName \tType  \tURI \tValue" );

            for (int i = 0; i < attrs.getLength(); i++) 
            {   System.out.println("    " + attrs.getLocalName(i) + "\t" +
                           attrs.getQName(i) + "\t" +
                           attrs.getType(i)  + "\t" +
                           attrs.getURI(i)   + "\t" +
                           attrs.getValue(i) );
            }
        }
    }

    public void endElement( java.lang.String uri, 
                            java.lang.String localName,
                            java.lang.String qName )
                            throws SAXException
    {   
        System.out.println("\n---- endElement" );
        System.out.println("      uri =  '" + uri + "'" );
        System.out.println("localName =  '" + localName + "'");
        System.out.println("    qName =  '" + qName + "'");
    }

    public void characters( char[] ch, int start, int length )
                            throws SAXException
    {   
        if (length > 0) {
            String str = new String( ch, start, length );
            System.out.println( "\n---- characters = '" + str + "'");
        }
    }

    public void ignorableWhitespace( char[] ch, int start, int length ) 
                                     throws SAXException
    {   
        System.out.println( "\n---- " + length + 
                            " ignorableWhitespace characters " +
                            "starting at " + start );
    }


    public void processingInstruction( java.lang.String target, 
                                       java.lang.String data )
                                       throws SAXException
    {   
        System.out.println( "\n---- processingInstruction: " + 
                            "target = " + target + 
                            " data = " + data );
    }


    public void skippedEntity( java.lang.String name ) throws SAXException
    {   
        System.out.println( "\n---- skippedEntity: name = " + name  );
    }

    public void setDocumentLocator( org.xml.sax.Locator locator ) 
    {
        System.out.println( "\n---- setDocumentLocator: locator = " 
                             + locator );
    }
}

Listing 2: VerboseContentHandler test program:
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import org.apache.xerces.parsers.SAXParser; 

import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ContentHandlerPrinter 
{
    public static void main( String[] args ) 
    {
        if (args.length == 0)
        {
            System.out.println( "Usage: java ContentHandlerPrinter " );
            return;
        }

        try 
        {
            // Open the file using a FileReader
            FileReader fr = new java.io.FileReader( args[0] );

            // create a SAX InputSource from the reader
            InputSource xmlSource = new InputSource( fr );

            // instantiate Xerces parser
            SAXParser sp = new SAXParser( );

            // set the content handler to our chatty one
            sp.setContentHandler( new VerboseContentHandler( ) );

           // let ‘er rip.
           sp.parse( xmlSource );
        }
        catch (java.io.FileNotFoundException fnf )
        {
            System.out.println( args[0] + “ Not Found!” );
        }
        catch (java.io.IOException io )
        {
            System.out.println( “IOException!: “ + io );
        }
        catch (org.xml.sax.SAXException se )
        {
            System.out.println( “SAXException!: “ + se );
        }
    }
}

Listing 3: ProgrammableSAXFilter demo:
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.raritech.xml.sax.Filter.ProgrammableSAXFilter;
import com.raritech.xml.sax.Filter.TagComparator;
import com.raritech.xml.sax.Filter.ElementModifier;
import com.raritech.xml.sax.Filter.AttributeIncrementer;
import com.raritech.xml.sax.Filter.AnyElementComparator;
import com.raritech.xml.sax.Filter.CDataEditor;

import com.raritech.xml.sax.Filter.SAXOutputter;

import org.apache.xerces.parsers.SAXParser;

public class ProgSAXFilterDemo  
{

    public static void main( String[] args )
    {
        try 
        {   // construct an InputSource from a file
            java.io.FileReader fr = new java.io.FileReader( args[0] );
            org.xml.sax.InputSource xmlInputSource =
                  new org.xml.sax.InputSource( fr );

            // construct a sax filter using a Xerces SAX parser
            ProgrammableSAXFilter psf 
                 = new ProgrammableSAXFilter(  new SAXParser( ) );

            // add an attribute incrementer for "ChildTag" elements
            psf.addElementModifier( new ElementModifier(
                                    new TagComparator( "ChildTag" ),
                                    new AttributeIncrementer(  "", "Number", 
                                                "Number", "CDATA", 0 ) ) );

            // get rid of "Dog" tags.
            psf.addElementRemover( new TagComparator( "Dog" ) );

            // replace "nuculer" with "nuclear"
            psf.addElementModifier( new ElementModifier(
                                new AnyElementComparator( ),
                                new CDataEditor( "nuculer", "nuclear" ) ) );

            // use a SAXOutputter to print results to System.out
            psf.setContentHandler( new SAXOutputter( System.out ) );

            // parse the input
            psf.parse( xmlInputSource );
        }
        catch (FileNotFoundException fnf )
        {
            System.out.println( "Couldn't find the file " + args[0] );
        }
        catch (IOException ioe )
        {
            System.out.println( "IOException! " + ioe );
        }
        catch (SAXException e )
        {
            System.out.println( "SAXException! " + e );
        }
    }
}

Listing 4: NameList program to extract Name= attributes from Author elements
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.raritech.xml.sax.Filter.TagComparator;
import com.raritech.xml.sax.Filter.ProgrammableSAXFilter;
import com.raritech.xml.sax.Filter.ElementModifier;
import com.raritech.xml.sax.Filter.AttributeObjectPropertySetter;

import org.apache.xerces.parsers.SAXParser;

public class NameList {

    java.util.ArrayList nameList = new java.util.ArrayList( );

    public void addName ( String name )
    {
        nameList.add( name );
    }

    public java.util.List getNames( ) 
    {
        return this.nameList;
    }

    public static void main( String[] args ) 
    {
        // construct an InputSource from a file
        try 
        {
            java.io.FileReader fr = new java.io.FileReader( args[0] );
            org.xml.sax.InputSource xmlInputSource =
                  new org.xml.sax.InputSource( fr );

            // construct a sax filter using a Xerces SAX parser
            ProgrammableSAXFilter psf 
                 = new ProgrammableSAXFilter(  new SAXParser( ) );

            // construct our callback object and add a notifier to the filter
            NameList nameList = new NameList( );
            psf.addElementModifier( new ElementModifier(
                           new TagComparator( "Author" ),
                           new AttributeObjectPropertySetter(  "", "name", 
                                       nameList, "addName" ) ) );
            // parse the input
            psf.parse( xmlInputSource );

            // the NameList object should now have a list of names from 
            // tags of the form 
            for (java.util.Iterator it 
                   = nameList.getNames().iterator(); it.hasNext(); )
            {    System.out.println( "Author is " + (String)it.next() );
            }
        }
        catch (FileNotFoundException fnf )
        {
            System.out.println( "Couldn't find the file " + args[0] );
        }
        catch (IOException ioe )
        {
            System.out.println( "IOException! " + ioe );
        }
        catch (SAXException e )
        {
            System.out.println( "SAXException! " + e );
        }
    }
}