KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > text > sax > Handler


1 package com.quadcap.text.sax;
2
3 /* Copyright 2000 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.BufferedReader JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.Reader JavaDoc;
44
45 import java.util.Hashtable JavaDoc;
46
47 import org.xml.sax.AttributeList JavaDoc;
48 import org.xml.sax.DocumentHandler JavaDoc;
49 import org.xml.sax.ErrorHandler JavaDoc;
50 import org.xml.sax.InputSource JavaDoc;
51 import org.xml.sax.Parser JavaDoc;
52 import org.xml.sax.Locator JavaDoc;
53 import org.xml.sax.SAXException JavaDoc;
54 import org.xml.sax.SAXParseException JavaDoc;
55
56 import org.xml.sax.helpers.ParserFactory JavaDoc;
57
58 import com.quadcap.util.Debug;
59 import com.quadcap.util.ConfigString;
60
61 /**
62  * General purpose sax handler class.
63  *
64  * @author Stan Bailes
65  */

66 public class Handler implements DocumentHandler JavaDoc, ErrorHandler JavaDoc {
67     protected Object JavaDoc context = null;
68     protected Hashtable JavaDoc env = new Hashtable JavaDoc();
69     protected StringBuffer JavaDoc data = new StringBuffer JavaDoc("");
70
71     Parser parser;
72     Locator locator;
73
74     /**
75      * Construct a new parser
76      */

77     public Handler() throws SAXException JavaDoc {
78         try {
79             ConfigString ps = ConfigString.find("xml.sax.parser",
80                                                 "com.quadcap.text.sax.Parser");
81             ClassLoader JavaDoc cl = ClassLoader.getSystemClassLoader();
82             this.parser =
83                 (Parser)(Class.forName(ps.toString(), true, cl).newInstance());
84             parser.setDocumentHandler(this);
85             parser.setErrorHandler(this);
86         } catch (Exception JavaDoc e) {
87             Debug.print(e);
88             throw new SAXException JavaDoc(e);
89         }
90     }
91
92     /**
93      * Parse the specified file the context of the
94      * specified web application.
95      *
96      * @param dd the inputstream containing the deployment descriptor
97      * @param app the web application being constructed
98      */

99     public void parse(Reader JavaDoc r, Object JavaDoc context)
100         throws SAXException JavaDoc
101     {
102         this.context = context;
103         try {
104             parser.parse(new InputSource JavaDoc(new BufferedReader JavaDoc(r)));
105         } catch (SAXException JavaDoc e) {
106             throw e;
107         } catch (Exception JavaDoc e) {
108             Debug.print(e);
109             throw new SAXException JavaDoc(e);
110         }
111     }
112
113     /**
114      * SAX parser callback to handle XML Parser errors.
115      * This implementation just prints them
116      * to System.err.
117      *
118      * @param exception the exception generated by the parser.
119      */

120     public void error(SAXParseException JavaDoc exception) {
121     System.err.println("error");
122     exception.printStackTrace(System.err);
123     }
124
125     /**
126      * SAX parser callback to handle XML Parser fatal errors.
127      * This implementation just prints them
128      * to System.err.
129      *
130      * @param exception the exception generated by the parser.
131      */

132     public void fatalError(SAXParseException JavaDoc exception) {
133     System.err.println("fatal error");
134     exception.printStackTrace(System.err);
135     }
136
137     /**
138      * SAX parser callback to handle XML Parser fatal errors.
139      * This implementation just prints them
140      * to System.err.
141      *
142      * @param exception the exception generated by the parser.
143      */

144     public void warning(SAXParseException JavaDoc exception) {
145     System.err.println("warning");
146     exception.printStackTrace(System.err);
147     }
148
149     /**
150      * SAX parser callback to handle character data found in the
151      * parsed document.
152      *
153      * @param ch the buffer containing the parsed characters.
154      * @param star the buffer position of the first character
155      * @param length the number of characters
156      *
157      * @exception SAXException may be thrown if this data represents
158      * a database value and there's a SQL exception thrown while
159      * trying to update the underlying resultset object with this
160      * data.
161      */

162     public void characters(char[] ch, int start, int length)
163     throws SAXException JavaDoc
164     {
165     data.append(ch, start, length);
166     }
167
168     /**
169      * SAX parser callback function that is called when the end of the
170      * document is reached. This implementation does nothing.
171      */

172     public void endDocument() {
173     }
174
175     /**
176      * SAX parser callback function called for the end of an element.
177      *
178      * @param name the name of this element
179      * @exception SAXException may be thrown
180      */

181     public void endElement(String JavaDoc name) throws SAXException JavaDoc {
182     }
183
184
185     protected final String JavaDoc consumeAny(String JavaDoc name) throws SAXException JavaDoc {
186         String JavaDoc ret = (String JavaDoc)env.get(name);
187         if (ret != null) {
188             env.remove(name);
189         }
190         return ret;
191     }
192
193     protected final String JavaDoc consume(String JavaDoc name) throws SAXException JavaDoc {
194         String JavaDoc ret = consumeAny(name);
195         if (ret == null) {
196             throw new SAXException JavaDoc("No value for <" + name + ">");
197         }
198         return ret;
199     }
200
201     protected final String JavaDoc consumeData() {
202         String JavaDoc ret = data.toString().trim();
203         data.setLength(0);
204         return ret;
205     }
206
207     /**
208      * SAX parser callback for ignorable whitespace. We just ignore it
209      *
210      * @param ch the buffer containing the parsed characters.
211      * @param star the buffer position of the first character
212      * @param length the number of characters
213      */

214     public void ignorableWhitespace(char[] ch, int start, int length) {
215     }
216
217     /**
218      * SAX parser callback for processing instructions. This implementation
219      * does nothing.
220      *
221      * @param target the processing instruction target.
222      * @param data the processing instruction data.
223      */

224     public void processingInstruction(String JavaDoc target, String JavaDoc data) {
225     }
226
227     /**
228      * SAX parser callback used to receive a document locator.
229      *
230      * @param locator the parser's locator object.
231      */

232     public void setDocumentLocator(Locator locator) {
233     this.locator = locator;
234     }
235
236     /**
237      * SAX parser callback for document start.
238      * This implementation does nothing.
239      */

240     public void startDocument() {
241     }
242
243     /**
244      * SAX parser callback for the start of an element.
245      *
246      * @param name the element name
247      * @param attrs the element's attributes
248      *
249      * @exception SAXException may be thrown
250      */

251     public void startElement(String JavaDoc name, AttributeList attrs)
252     throws SAXException JavaDoc
253     {
254     data.setLength(0);
255     }
256
257
258
259 }
260
Popular Tags