Quadcap Embeddable Database

com/quadcap/text/sax/Handler.java

Go to the documentation of this file.
00001 package com.quadcap.text.sax; 00002 00003 /* Copyright 2000 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.BufferedReader; 00042 import java.io.IOException; 00043 import java.io.Reader; 00044 00045 import java.util.Hashtable; 00046 00047 import org.xml.sax.AttributeList; 00048 import org.xml.sax.DocumentHandler; 00049 import org.xml.sax.ErrorHandler; 00050 import org.xml.sax.InputSource; 00051 import org.xml.sax.Parser; 00052 import org.xml.sax.Locator; 00053 import org.xml.sax.SAXException; 00054 import org.xml.sax.SAXParseException; 00055 00056 import org.xml.sax.helpers.ParserFactory; 00057 00058 import com.quadcap.util.Debug; 00059 import com.quadcap.util.ConfigString; 00060 00061 /** 00062 * General purpose sax handler class. 00063 * 00064 * @author Stan Bailes 00065 */ 00066 public class Handler implements DocumentHandler, ErrorHandler { 00067 protected Object context = null; 00068 protected Hashtable env = new Hashtable(); 00069 protected StringBuffer data = new StringBuffer(""); 00070 00071 Parser parser; 00072 Locator locator; 00073 00074 /** 00075 * Construct a new parser 00076 */ 00078 try { 00079 ConfigString ps = ConfigString.find("xml.sax.parser", 00080 "com.quadcap.text.sax.Parser"); 00082 this.parser = 00083 (Parser)(Class.forName(ps.toString(), true, cl).newInstance()); 00084 parser.setDocumentHandler(this); 00085 parser.setErrorHandler(this); 00086 } catch (Exception e) { 00087 Debug.print(e); 00088 throw new SAXException(e); 00089 } 00090 } 00091 00092 /** 00093 * Parse the specified file the context of the 00094 * specified web application. 00095 * 00096 * @param dd the inputstream containing the deployment descriptor 00097 * @param app the web application being constructed 00098 */ 00099 public void parse(Reader r, Object context) 00101 { 00102 this.context = context; 00103 try { 00106 throw e; 00107 } catch (Exception e) { 00108 Debug.print(e); 00110 } 00111 } 00112 00113 /** 00114 * SAX parser callback to handle XML Parser errors. 00115 * This implementation just prints them 00116 * to System.err. 00117 * 00118 * @param exception the exception generated by the parser. 00119 */ 00121 System.err.println("error"); 00122 exception.printStackTrace(System.err); 00123 } 00124 00125 /** 00126 * SAX parser callback to handle XML Parser fatal errors. 00127 * This implementation just prints them 00128 * to System.err. 00129 * 00130 * @param exception the exception generated by the parser. 00131 */ 00133 System.err.println("fatal error"); 00134 exception.printStackTrace(System.err); 00135 } 00136 00137 /** 00138 * SAX parser callback to handle XML Parser fatal errors. 00139 * This implementation just prints them 00140 * to System.err. 00141 * 00142 * @param exception the exception generated by the parser. 00143 */ 00145 System.err.println("warning"); 00146 exception.printStackTrace(System.err); 00147 } 00148 00149 /** 00150 * SAX parser callback to handle character data found in the 00151 * parsed document. 00152 * 00153 * @param ch the buffer containing the parsed characters. 00154 * @param star the buffer position of the first character 00155 * @param length the number of characters 00156 * 00157 * @exception SAXException may be thrown if this data represents 00158 * a database value and there's a SQL exception thrown while 00159 * trying to update the underlying resultset object with this 00160 * data. 00161 */ 00162 public void characters(char[] ch, int start, int length) 00164 { 00165 data.append(ch, start, length); 00166 } 00167 00168 /** 00169 * SAX parser callback function that is called when the end of the 00170 * document is reached. This implementation does nothing. 00171 */ 00172 public void endDocument() { 00173 } 00174 00175 /** 00176 * SAX parser callback function called for the end of an element. 00177 * 00178 * @param name the name of this element 00179 * @exception SAXException may be thrown 00180 */ 00182 } 00183 00184 00186 String ret = (String)env.get(name); 00187 if (ret != null) { 00188 env.remove(name); 00189 } 00190 return ret; 00191 } 00192 00194 String ret = consumeAny(name); 00195 if (ret == null) { 00197 } 00198 return ret; 00199 } 00200 00201 protected final String consumeData() { 00202 String ret = data.toString().trim(); 00203 data.setLength(0); 00204 return ret; 00205 } 00206 00207 /** 00208 * SAX parser callback for ignorable whitespace. We just ignore it 00209 * 00210 * @param ch the buffer containing the parsed characters. 00211 * @param star the buffer position of the first character 00212 * @param length the number of characters 00213 */ 00214 public void ignorableWhitespace(char[] ch, int start, int length) { 00215 } 00216 00217 /** 00218 * SAX parser callback for processing instructions. This implementation 00219 * does nothing. 00220 * 00221 * @param target the processing instruction target. 00222 * @param data the processing instruction data. 00223 */ 00224 public void processingInstruction(String target, String data) { 00225 } 00226 00227 /** 00228 * SAX parser callback used to receive a document locator. 00229 * 00230 * @param locator the parser's locator object. 00231 */ 00232 public void setDocumentLocator(Locator locator) { 00233 this.locator = locator; 00234 } 00235 00236 /** 00237 * SAX parser callback for document start. 00238 * This implementation does nothing. 00239 */ 00240 public void startDocument() { 00241 } 00242 00243 /** 00244 * SAX parser callback for the start of an element. 00245 * 00246 * @param name the element name 00247 * @param attrs the element's attributes 00248 * 00249 * @exception SAXException may be thrown 00250 */ 00251 public void startElement(String name, AttributeList attrs) 00253 { 00254 data.setLength(0); 00255 } 00256 00257 00258 00259 }