001: package isql;
002:
003: import java.io.*;
004:
005: import org.xml.sax.*;
006: import org.xml.sax.helpers.DefaultHandler;
007:
008: import javax.xml.parsers.SAXParserFactory;
009: import javax.xml.parsers.ParserConfigurationException;
010: import javax.xml.parsers.SAXParser;
011: import java.util.Map;
012: import java.util.HashMap;
013: import java.util.Iterator;
014: import util.*;
015:
016: /** this program will do a simple parse of attributes and elements
017: * within an XML file using a ParseModel supplied by user. The
018: * ParseModel will have the actions to be taken upon encountering an
019: * element or attribute.
020: * @author $Author: rahul_kumar $
021: * $Id: SimpleParser.java,v 1.2 2004/01/01 06:59:04 rahul_kumar Exp $
022: */
023: public class SimpleParser {
024:
025: public static void main(String argv[]) {
026: if (argv.length != 1) {
027: System.err.println("Usage: cmd filename");
028: System.exit(1);
029: }
030: SimpleParser sp = new SimpleParser();
031: //ParseModel pm = createDefaultParseModel();
032: ParseModel pm = createDefaultParseModel2();
033: try {
034: sp.parse(argv[0], pm);
035: Map ht = (Map) pm.getObject();
036: System.out.println(ht.size());
037: System.out.println("Keys:");
038:
039: for (Iterator e = ht.keySet().iterator(); e.hasNext();) {
040: String s = (String) (e.next());
041: System.out.println(s + "=" + ht.get(s));
042: }
043: // System.out.println(ht.get("name"));
044: // System.out.println(ht.get("sql"));
045: // System.out.println(ht.get("details"));
046: //} catch (Exception exc) { System.err.println( "EXC36:"+ exc.toString()); }
047: } catch (Exception t) {
048: System.err.println(t.toString());
049: }
050: }
051:
052: /** parser instance */
053: SAXParser _saxParser;
054: SimpleHandler _handler;
055:
056: /** constructor that creates the parser */
057: public SimpleParser() {
058: // Use an instance of the SAX event handler
059: _handler = new SimpleHandler();
060: // Use the default (non-validating) parser
061: SAXParserFactory factory = SAXParserFactory.newInstance();
062: try {
063: // Parse the input
064: _saxParser = factory.newSAXParser();
065: } catch (Throwable t) {
066: t.printStackTrace();
067: }
068: }
069:
070: /** parses given file name along with implementatin of ParseModel
071: */
072: public void parse(String filename, ParseModel pm)
073: throws SAXException, IOException {
074: _handler.setModel(pm);
075: _saxParser.parse(new File(filename), _handler);
076: }
077:
078: /** a sample parse model implementation */
079: public static ParseModel createDefaultParseModel() {
080: return new ParseModel() {
081: Map ht = new HashMap();
082: String key;
083:
084: public void setModelElement(String ename, Attributes attrs) {
085: }
086:
087: public void setModelAttribute(int row, int total,
088: String aname, String value, String ename) {
089: if (aname.equals("name"))
090: key = value;
091: else if (aname.equals("sql"))
092: ht.put(key, value);
093: }
094:
095: public Object getObject() {
096: return ht;
097: }
098:
099: };
100: }
101:
102: public static ParseModel createDefaultParseModel2() {
103: return new ParseModel() {
104: Map ht = new HashMap();
105: String key;
106:
107: public void setModelElement(String ename, Attributes attrs) {
108: }
109:
110: public void setModelAttribute(int row, int total,
111: String aname, String value, String ename) {
112: ht.put(aname, value);
113: }
114:
115: public Object getObject() {
116: return ht;
117: }
118:
119: };
120: }
121: }
122:
123: /** Simple implementation of DefaultHandler that uses a ParseModel
124: */
125: class SimpleHandler extends DefaultHandler {
126: ParseModel _parsemodel;
127:
128: //===========================================================
129: // SAX DocumentHandler methods
130: //===========================================================
131:
132: public void startDocument() throws SAXException {
133: // emit("<?xml version='1.0' encoding='UTF-8'?>");
134: // nl();
135: }
136:
137: public void endDocument() throws SAXException {
138: // try {
139: // nl();
140: // out.flush();
141: // } catch (IOException e) {
142: // throw new SAXException("I/O error", e);
143: // }
144: }
145:
146: public void startElement(String namespaceURI, String lName, // local name
147: String qName, // qualified name
148: Attributes attrs) throws SAXException {
149: String eName = lName; // element name
150: if ("".equals(eName))
151: eName = qName; // namespaceAware = false
152: _parsemodel.setModelElement(eName, attrs);
153: if (attrs != null) {
154: for (int i = 0; i < attrs.getLength(); i++) {
155: String aName = attrs.getLocalName(i); // Attr name
156: if ("".equals(aName))
157: aName = attrs.getQName(i);
158: _parsemodel.setModelAttribute(i, attrs.getLength(),
159: aName, attrs.getValue(i), eName);
160: }
161: }
162: }
163:
164: public void endElement(String namespaceURI, String sName, // simple name
165: String qName // qualified name
166: ) throws SAXException {
167: //emit("</"+sName+">");
168: }
169:
170: public void characters(char buf[], int offset, int len)
171: throws SAXException {
172: // String s = new String(buf, offset, len);
173: // emit(s);
174: }
175:
176: //===========================================================
177: // Utility Methods ...
178: //===========================================================
179:
180: // Wrap I/O exceptions in SAX exceptions, to
181: // suit handler signature requirements
182: private void emit(String s) throws SAXException {
183: // try {
184: // out.write(s);
185: // out.flush();
186: // } catch (IOException e) {
187: // throw new SAXException("I/O error", e);
188: // }
189: }
190:
191: // Start a new line
192: private void nl() throws SAXException {
193: // String lineEnd = System.getProperty("line.separator");
194: // try {
195: // out.write(lineEnd);
196: // } catch (IOException e) {
197: // throw new SAXException("I/O error", e);
198: // }
199: }
200:
201: public void setModel(ParseModel pm) {
202: _parsemodel = pm;
203: }
204: }
|