ListParser.java



package xmlshare;

import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

import java.util.ArrayList;
import java.io.File;

/** Parse list.xml */
public class ListParser extends DefaultHandler {

  /** Reader */
    private XMLReader reader = null;

  /** Constructor */
    public ListParser() {
        try {
            SAXParserFactory spf = SAXParserFactory.newInstance();
         // do validate
            spf.setValidating( true );
            SAXParser parser = spf.newSAXParser();
            reader = parser.getXMLReader();
            reader.setContentHandler( this );
            reader.setErrorHandler( this );
        }
        catch( Exception e ) {
            System.err.println( e.toString() );
        }
    }

  /** public parse method */
    public void parse( InputSource source ) {
        try {
            names = new ArrayList();
            reader.parse( source );
        }
        catch( Exception e ) {
            System.err.println( e.toString() );
        }
    }

  /** name list */
    private ArrayList names;

  /** public method to access names */
    public String[] getListedName() {

        String[] array = new String[ names.size() ];
        for( int i=0; i<array.length; i++ ) {
            array[i] = (String)names.get(i);
        }
        return array;
    }

  /** only process name char */
    private boolean inName = false;

  /** element */
    public void startElement( String nameSpace, String localName,
                              String qName, Attributes attrs )
                              throws SAXException {
        if( qName.equals( "name" ) )
            inName = true;
    }

  /** end element */
    public void endElement( String nameSpace, String localName,
                            String qName )
                            throws SAXException {
        if( qName.equals( "name" ) )
            inName = false;
    }

  /** text */
    public void characters( char[] ch, int start, int length )
                            throws SAXException {
        if( inName ) {
            int b = start;
            while( ignoreChar( ch[b] ) ) {
                b++; length--;
                if( length <= 0 ) return;
            }
            int e = b + length - 1;
            while( ignoreChar( ch[e] ) ) {
                e--;  length--;
                if( length <= 0 ) return;
            }
            String text = new String( ch, b, length );
            if( text.endsWith( ".xml" ) ||
                text.endsWith( ".XML" ) ) {
                names.add( text );
            }
        }
    }

  /** to remove white space */
    private boolean ignoreChar( char c ) {

        boolean flag;
        switch(c) {
             case ' ':
             case '\t':
             case '\r':
             case '\n':
                      flag = true;
                      break;
             default: flag = false; 
                      break;
        }
        return flag;
    }

  /** error */
    public void error( SAXParseException e ) throws SAXException {
        throw e;
    }

  /** fatal error */
    public void fatalError( SAXParseException e ) throws SAXException {
        throw e;
    }

  /** warning */
    public void warning( SAXParseException e ) throws SAXException {
        throw e;
    }

  /** main (start here) */
    public static void main( String[] argv ) {
        try {
            ListParser test = new ListParser();
            InputSource source = new InputSource( "public"
                                   + File.separator + "list.xml" );
            test.parse( source );
            String[] a = test.getListedName();
            for( int i=0; i<a.length; i++ )
              System.out.println( a[i] );
        }
        catch( Exception e ) {
            System.err.println( e.toString() );
        }
    }
}