SimpleParser.java :  » Database-Client » SQLMinus » isql » Java Open Source

Java Open Source » Database Client » SQLMinus 
SQLMinus » isql » SimpleParser.java
package isql;
import java.io.*;

import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import util.*;

/** this program will do a simple parse of attributes and elements
 * within an XML file using a ParseModel supplied by user. The
 * ParseModel will have the actions to be taken upon encountering an
 * element or attribute.
 * @author $Author: rahul_kumar $
 * $Id: SimpleParser.java,v 1.2 2004/01/01 06:59:04 rahul_kumar Exp $
 */
public class SimpleParser {

    public static void main(String argv[])
    {
        if (argv.length != 1) {
            System.err.println("Usage: cmd filename");
            System.exit(1);
        }
        SimpleParser sp = new SimpleParser();
        //ParseModel pm = createDefaultParseModel();
        ParseModel pm = createDefaultParseModel2();
        try {
        sp.parse(argv[0], pm);
        Map ht = (Map) pm.getObject();
        System.out.println(ht.size());
        System.out.println("Keys:");

for (Iterator e = ht.keySet().iterator(); e.hasNext ();)
  {
    String s =(String) (e.next());
        System.out.println(s+"="+ht.get(s));
  }
 //       System.out.println(ht.get("name"));
 //       System.out.println(ht.get("sql"));
 //       System.out.println(ht.get("details"));
        //} catch (Exception exc) { System.err.println( "EXC36:"+ exc.toString()); } 
        } catch (Exception t ) { System.err.println(t.toString())  ; }
    }

    /** parser instance */
    SAXParser _saxParser;
    SimpleHandler _handler;

    /** constructor that creates the parser */
    public SimpleParser () {
        // Use an instance of the SAX event handler
        _handler = new SimpleHandler();
        // Use the default (non-validating) parser
        SAXParserFactory factory = SAXParserFactory.newInstance();
        try {
            // Parse the input
            _saxParser = factory.newSAXParser();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
    /** parses given file name along with implementatin of ParseModel
     */
    public void parse (String filename, ParseModel pm) 
        throws SAXException , IOException{
        _handler.setModel(pm);
        _saxParser.parse( new File(filename), _handler);
    }

    /** a sample parse model implementation */
    public static ParseModel createDefaultParseModel(){
        return new ParseModel(){
            Map ht = new HashMap();
            String key;
            public void setModelElement(String ename, Attributes attrs){
            }
            public void setModelAttribute (int row, int total, String aname, 
                    String value, String ename){
                if (aname.equals("name"))
                    key = value;
                else
                    if (aname.equals("sql")) 
                        ht.put(key, value);
            }

            public Object getObject (){
                return ht;
            }

        };
    }
    public static ParseModel createDefaultParseModel2(){
        return new ParseModel(){
            Map ht = new HashMap();
            String key;
            public void setModelElement(String ename, Attributes attrs){
            }
            public void setModelAttribute (int row, int total, String aname, 
                    String value, String ename){
                        ht.put(aname, value);
            }

            public Object getObject (){
                return ht;
            }

        };
    }
}
/** Simple implementation of DefaultHandler that uses a ParseModel
 */
class SimpleHandler extends DefaultHandler
{
    ParseModel _parsemodel;

    //===========================================================
    // SAX DocumentHandler methods
    //===========================================================

    public void startDocument()
        throws SAXException
        {
            //        emit("<?xml version='1.0' encoding='UTF-8'?>");
            //        nl();
        }

    public void endDocument()
        throws SAXException
        {
            //        try {
            //            nl();
            //            out.flush();
            //        } catch (IOException e) {
            //            throw new SAXException("I/O error", e);
            //        }
        }

    public void startElement(String namespaceURI,
            String lName, // local name
            String qName, // qualified name
            Attributes attrs)
        throws SAXException
        {
            String eName = lName; // element name
            if ("".equals(eName)) eName = qName; // namespaceAware = false
            _parsemodel.setModelElement(eName, attrs);
            if (attrs != null) {
                for (int i = 0; i < attrs.getLength(); i++) {
                    String aName = attrs.getLocalName(i); // Attr name 
                    if ("".equals(aName)) aName = attrs.getQName(i);
                    _parsemodel.setModelAttribute(i, attrs.getLength(), aName, attrs.getValue(i), eName);
                }
            }
        }

    public void endElement(String namespaceURI,
            String sName, // simple name
            String qName  // qualified name
                          )
        throws SAXException
        {
            //emit("</"+sName+">");
        }

    public void characters(char buf[], int offset, int len)
        throws SAXException
        {
            //        String s = new String(buf, offset, len);
            //        emit(s);
        }

    //===========================================================
    // Utility Methods ...
    //===========================================================

    // Wrap I/O exceptions in SAX exceptions, to
    // suit handler signature requirements
    private void emit(String s)
        throws SAXException
        {
            //        try {
            //            out.write(s);
            //            out.flush();
            //        } catch (IOException e) {
            //            throw new SAXException("I/O error", e);
            //        }
        }

    // Start a new line
    private void nl()
        throws SAXException
        {
            //        String lineEnd =  System.getProperty("line.separator");
            //        try {
            //            out.write(lineEnd);
            //        } catch (IOException e) {
            //            throw new SAXException("I/O error", e);
            //        }
        }
    public void setModel (ParseModel pm){
        _parsemodel = pm;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.