HandlerPanel.java



import org.xml.sax.*;
import javax.swing.*;

/** to show handlers output */
public class HandlerPanel extends JPanel
             implements ContentHandler, ErrorHandler {

  /** the TextArea object */
    private JTextArea ta;

  /** new line string */
    private String nl;
    private void newLine() {
       ta.append(nl);
    }

  /** constructor */
    public HandlerPanel() {

        ta = new JTextArea( "", 20, 64 );
        ta.setEditable( false );
        nl = System.getProperties().getProperty( "line.separator" );
        add( ta );
    }

  /** clear text */
    public void clear() {
        ta.setText( "" );
    }

  /** start parsing */
    public void startDocument() throws SAXException {
    }

  /** end parsing */
    public void endDocument() throws SAXException {
    }

  /** element */
    public void startElement( String nameSpace, String localName,
                              String qName, Attributes attrs )
                              throws SAXException {

        if( nameSpace != null && localName != null
         && !nameSpace.equals("") && !localName.equals("") ) {
            ta.append( "<" + nameSpace + ":" + localName + ">" );
        }
        else {
            ta.append( "<" + qName + ">" );
        }
        printAttrs( attrs );
        newLine();
    }

  /** return attributes info */
    private String getAttrsInfo( Attributes attrs ) {

        if( attrs.getLength() <= 0 )
            return "";

        String info = "";
        for( int i=0; i<attrs.getLength(); i++ ) {
            String qName = attrs.getQName( i );
            String value = attrs.getValue( qName );
            info += ( " " + qName + "=" + value + " " );
        }
        return info;
    }

  /** print attributes */
    private void printAttrs( Attributes attrs ) {

        ta.append( getAttrsInfo( attrs) );
    }

  /** end element */
    public void endElement( String nameSpace, String localName,
                            String qName )
                            throws SAXException {

        if( nameSpace != null && localName != null
         && !nameSpace.equals("") && !localName.equals("") ) {
            ta.append( "</" + nameSpace + ":" + localName + ">" );
        }
        else {
            ta.append( "</" + qName + ">" );
        }
        newLine();
    }

  /** text */
    public void characters( char[] ch, int start, int length )
                            throws SAXException {
        String text = new String( ch, start, length );
        ta.append( text );
        newLine();
    }

  /** set Locator */
    public void setDocumentLocator ( Locator locator ) {
    }
    
  /** Start Name Space Mapping */
    public void startPrefixMapping ( String prefix, String uri )
                                     throws SAXException {
    }

  /** End Name Space Mapping */
    public void endPrefixMapping ( String prefix )
                                   throws SAXException {
    }
    
  /** white space */
    public void ignorableWhitespace ( char ch[], int start, int length )
                                      throws SAXException {
    }
    
  /** processing instruction */
    public void processingInstruction ( String target, String data )
                                        throws SAXException {
    }

  /** skip entity */
    public void skippedEntity ( String name )
                                throws SAXException {
    }

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

        ta.append( "Error line:"
                 + e.getLineNumber() + ":" + e.getMessage() );
    }

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

        ta.append( "Fatal Error line:"
                 + e.getLineNumber() + ":" + e.getMessage() );
    }

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

        ta.append( "Warning line:"
                 + e.getLineNumber() + ":" + e.getMessage() );
    }

  /** start here(for test) */
    public static void main( String[] argv ) {
        try {
            HandlerPanel panel = new HandlerPanel();
            OptionalParser parser
              = new OptionalParser( panel, panel, true, true );
            JFrame frame = new JFrame( "Handler Pane" );
            frame.getContentPane().add( panel, "Center" );
            frame.pack();
            frame.show();

            InputSource source = new InputSource( argv[0] );
            parser.parse( source );
        }
        catch( Exception e ) {
            System.err.println( e.toString() );
        }
    }
}