001: package com.quadcap.text.sax;
002:
003: /* Copyright 2000 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.BufferedReader;
042: import java.io.IOException;
043: import java.io.Reader;
044:
045: import java.util.Hashtable;
046:
047: import org.xml.sax.AttributeList;
048: import org.xml.sax.DocumentHandler;
049: import org.xml.sax.ErrorHandler;
050: import org.xml.sax.InputSource;
051: import org.xml.sax.Parser;
052: import org.xml.sax.Locator;
053: import org.xml.sax.SAXException;
054: import org.xml.sax.SAXParseException;
055:
056: import org.xml.sax.helpers.ParserFactory;
057:
058: import com.quadcap.util.Debug;
059: import com.quadcap.util.ConfigString;
060:
061: /**
062: * General purpose sax handler class.
063: *
064: * @author Stan Bailes
065: */
066: public class Handler implements DocumentHandler, ErrorHandler {
067: protected Object context = null;
068: protected Hashtable env = new Hashtable();
069: protected StringBuffer data = new StringBuffer("");
070:
071: Parser parser;
072: Locator locator;
073:
074: /**
075: * Construct a new parser
076: */
077: public Handler() throws SAXException {
078: try {
079: ConfigString ps = ConfigString.find("xml.sax.parser",
080: "com.quadcap.text.sax.Parser");
081: ClassLoader cl = ClassLoader.getSystemClassLoader();
082: this .parser = (Parser) (Class.forName(ps.toString(), true,
083: cl).newInstance());
084: parser.setDocumentHandler(this );
085: parser.setErrorHandler(this );
086: } catch (Exception e) {
087: Debug.print(e);
088: throw new SAXException(e);
089: }
090: }
091:
092: /**
093: * Parse the specified file the context of the
094: * specified web application.
095: *
096: * @param dd the inputstream containing the deployment descriptor
097: * @param app the web application being constructed
098: */
099: public void parse(Reader r, Object context) throws SAXException {
100: this .context = context;
101: try {
102: parser.parse(new InputSource(new BufferedReader(r)));
103: } catch (SAXException e) {
104: throw e;
105: } catch (Exception e) {
106: Debug.print(e);
107: throw new SAXException(e);
108: }
109: }
110:
111: /**
112: * SAX parser callback to handle XML Parser errors.
113: * This implementation just prints them
114: * to System.err.
115: *
116: * @param exception the exception generated by the parser.
117: */
118: public void error(SAXParseException exception) {
119: System.err.println("error");
120: exception.printStackTrace(System.err);
121: }
122:
123: /**
124: * SAX parser callback to handle XML Parser fatal errors.
125: * This implementation just prints them
126: * to System.err.
127: *
128: * @param exception the exception generated by the parser.
129: */
130: public void fatalError(SAXParseException exception) {
131: System.err.println("fatal error");
132: exception.printStackTrace(System.err);
133: }
134:
135: /**
136: * SAX parser callback to handle XML Parser fatal errors.
137: * This implementation just prints them
138: * to System.err.
139: *
140: * @param exception the exception generated by the parser.
141: */
142: public void warning(SAXParseException exception) {
143: System.err.println("warning");
144: exception.printStackTrace(System.err);
145: }
146:
147: /**
148: * SAX parser callback to handle character data found in the
149: * parsed document.
150: *
151: * @param ch the buffer containing the parsed characters.
152: * @param star the buffer position of the first character
153: * @param length the number of characters
154: *
155: * @exception SAXException may be thrown if this data represents
156: * a database value and there's a SQL exception thrown while
157: * trying to update the underlying resultset object with this
158: * data.
159: */
160: public void characters(char[] ch, int start, int length)
161: throws SAXException {
162: data.append(ch, start, length);
163: }
164:
165: /**
166: * SAX parser callback function that is called when the end of the
167: * document is reached. This implementation does nothing.
168: */
169: public void endDocument() {
170: }
171:
172: /**
173: * SAX parser callback function called for the end of an element.
174: *
175: * @param name the name of this element
176: * @exception SAXException may be thrown
177: */
178: public void endElement(String name) throws SAXException {
179: }
180:
181: protected final String consumeAny(String name) throws SAXException {
182: String ret = (String) env.get(name);
183: if (ret != null) {
184: env.remove(name);
185: }
186: return ret;
187: }
188:
189: protected final String consume(String name) throws SAXException {
190: String ret = consumeAny(name);
191: if (ret == null) {
192: throw new SAXException("No value for <" + name + ">");
193: }
194: return ret;
195: }
196:
197: protected final String consumeData() {
198: String ret = data.toString().trim();
199: data.setLength(0);
200: return ret;
201: }
202:
203: /**
204: * SAX parser callback for ignorable whitespace. We just ignore it
205: *
206: * @param ch the buffer containing the parsed characters.
207: * @param star the buffer position of the first character
208: * @param length the number of characters
209: */
210: public void ignorableWhitespace(char[] ch, int start, int length) {
211: }
212:
213: /**
214: * SAX parser callback for processing instructions. This implementation
215: * does nothing.
216: *
217: * @param target the processing instruction target.
218: * @param data the processing instruction data.
219: */
220: public void processingInstruction(String target, String data) {
221: }
222:
223: /**
224: * SAX parser callback used to receive a document locator.
225: *
226: * @param locator the parser's locator object.
227: */
228: public void setDocumentLocator(Locator locator) {
229: this .locator = locator;
230: }
231:
232: /**
233: * SAX parser callback for document start.
234: * This implementation does nothing.
235: */
236: public void startDocument() {
237: }
238:
239: /**
240: * SAX parser callback for the start of an element.
241: *
242: * @param name the element name
243: * @param attrs the element's attributes
244: *
245: * @exception SAXException may be thrown
246: */
247: public void startElement(String name, AttributeList attrs)
248: throws SAXException {
249: data.setLength(0);
250: }
251:
252: }
|