Quadcap Embeddable Server

com/quadcap/http/servlets/jsp/JspHandler.java

Go to the documentation of this file.
00001 package com.quadcap.http.servlets.jsp; 00002 00003 /* Copyright 1999 - 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.BufferedInputStream; 00042 import java.io.BufferedReader; 00043 import java.io.CharArrayWriter; 00044 import java.io.File; 00045 import java.io.FileReader; 00046 import java.io.IOException; 00047 import java.io.InputStream; 00048 import java.io.InputStreamReader; 00049 import java.io.PrintWriter; 00050 import java.io.Reader; 00051 import java.io.Writer; 00052 00053 import java.util.Enumeration; 00054 import java.util.Hashtable; 00055 import java.util.Vector; 00056 00057 import javax.servlet.ServletContext; 00058 00059 import org.xml.sax.AttributeList; 00060 import org.xml.sax.DocumentHandler; 00061 import org.xml.sax.DTDHandler; 00062 import org.xml.sax.EntityResolver; 00063 import org.xml.sax.ErrorHandler; 00064 import org.xml.sax.HandlerBase; 00065 import org.xml.sax.InputSource; 00066 import org.xml.sax.Locator; 00067 import org.xml.sax.Parser; 00068 import org.xml.sax.SAXException; 00069 00070 import com.quadcap.util.collections.ArrayQueue; 00071 00072 import com.quadcap.util.Debug; 00073 00074 /** 00075 * This class is a SAX parser handler for parsing JSP documents. 00076 * 00077 * @author Stan Bailes 00078 */ 00079 public class JspHandler implements DocumentHandler, EntityResolver, TagContext { 00080 ServletContext context; 00081 JspParser parser; 00082 JspObject jsp; 00083 Hashtable tagHandlers = new Hashtable(); 00084 PrintWriter w; 00085 CharArrayWriter cw = null; 00086 boolean written = false; 00087 ArrayQueue tagStack = new ArrayQueue(0, -1); 00088 ArrayQueue wStack = new ArrayQueue(0, -1); 00089 Hashtable pageDirectives = new Hashtable(); 00090 00091 public JspHandler(ServletContext context, JspParser parser, 00092 PrintWriter w, JspObject jsp) { 00093 this.context = context; 00094 this.parser = parser; 00095 this.jsp = jsp; 00096 this.w = w; 00097 addHandler("jsp:root", new TagJspRoot()); 00098 addHandler("jsp:useBean", new TagJspUseBean()); 00099 addHandler("jsp:expression", new TagJspExpression()); 00100 addHandler("jsp:expr", new TagJspExpression()); 00101 addHandler("jsp:declaration", new TagJspDeclaration()); 00102 addHandler("jsp:decl", new TagJspDeclaration()); 00103 addHandler("jsp:scriptlet", new TagJspScriptlet()); 00104 addHandler("jsp:directive.page", new TagJspPage()); 00105 addHandler("jsp:directive.include", new TagDirInclude()); 00106 addHandler("jsp:forward", new TagJspForward()); 00107 addHandler("jsp:include", new TagJspInclude()); 00108 addHandler("jsp:getProperty", new TagJspGetProperty()); 00109 addHandler("jsp:setProperty", new TagJspSetProperty()); 00110 } 00111 00112 public PrintWriter getPrintWriter() { return w; } 00113 public String getPackageName() { return jsp.getPackageName(); } 00114 public String getClassName() { return jsp.getClassName(); } 00115 00117 EntityResolver resolver = parser.getEntityResolver(); 00119 if (resolver != null) { 00120 in = resolver.resolveEntity(null, file); 00121 } else { 00122 FileReader f = new FileReader(file); 00124 } 00125 if (in == null) { 00126 throw new IOException("Can't resolve entity: " + file); 00127 } 00128 parser.pushInputSource(in); 00129 } 00130 00131 public PrintWriter pushPrintWriter(PrintWriter p) { 00132 wStack.addBack(w); 00133 PrintWriter oldW = w; 00134 w = p; 00135 return oldW; 00136 } 00137 00138 public PrintWriter popPrintWriter() { 00139 w = (PrintWriter)wStack.popBack(); 00140 return w; 00141 } 00142 00143 public void addPageDirective(String name, String val) { 00144 Vector v = (Vector)pageDirectives.get(name); 00145 if (v == null) { 00146 v = new Vector(); 00147 pageDirectives.put(name, v); 00148 } 00149 v.addElement(val); 00150 } 00151 00152 public Enumeration getPageDirectives(String name) { 00153 Vector v = (Vector)pageDirectives.get(name); 00154 if (v == null) v = new Vector(); 00155 return v.elements(); 00156 } 00157 00158 public String getPageDirective(String name) { 00159 String s = null; 00160 Vector v = (Vector)pageDirectives.get(name); 00161 if (v != null && v.size() > 0) s = v.elementAt(0).toString(); 00162 return s; 00163 } 00164 00165 public String getPageDirective(String name, String dflt) { 00166 String s = dflt; 00167 Vector v = (Vector)pageDirectives.get(name); 00168 if (v != null && v.size() > 0) s = v.elementAt(0).toString(); 00169 return s; 00170 } 00171 00172 public boolean isValidTag(String name) { 00173 return tagHandlers.get(name) != null; 00174 } 00175 00176 public void addHandler(String name, TagHandler h) { 00177 tagHandlers.put(name, h); 00178 } 00179 00180 public void startDocument() { 00181 } 00182 00183 public void endDocument() { 00184 } 00185 00186 public void ignorableWhitespace(char[] ch, int off, int cnt) 00188 { 00189 characters(ch, off, cnt); 00190 } 00191 00192 public void processingInstruction(String target, String data) { 00193 } 00194 00196 } 00197 00200 { 00201 //Debug.println("START: " + tag + " " + TagJsp.toString(attrs)); 00202 try { 00203 TagHandler h = (TagHandler)tagHandlers.get(tag); 00204 if (h != null) { 00205 TagInstance t = h.makeInstance(this); 00206 t.doStartTag(tag, attrs); 00207 tagStack.addBack(t); 00208 } else { 00209 CharArrayWriter cw = new CharArrayWriter(); 00210 cw.write('<'); 00211 cw.write(tag); 00212 for (int i = 0; i < attrs.getLength(); i++) { 00213 cw.write(' '); 00214 cw.write(attrs.getName(i)); 00215 cw.write('='); 00216 cw.write('"'); 00217 cw.write(attrs.getValue(i)); 00218 cw.write('"'); 00219 } 00220 cw.write('>'); 00221 char[] ch = cw.toCharArray(); 00222 characters(ch, 0, ch.length); 00223 } 00224 } catch (Exception e) { 00225 Debug.print(e); 00227 } 00228 } 00229 00231 try { 00232 TagInstance t = (TagInstance)tagStack.tail(); 00233 if (t != null) { 00234 t.doCharacters(ch, off, len); 00235 } 00236 } catch (Exception e) { 00237 Debug.print(e); 00239 } 00240 } 00241 00243 //Debug.println("END: " + tag); 00244 try { 00245 TagInstance t = (TagInstance)tagStack.tail(); 00246 //Debug.println("t = " + t); 00247 if (t != null && t.getTagName().equals(tag)) { 00248 tagStack.popBack(); 00249 t.doEndTag(); 00250 } else { 00251 CharArrayWriter cw = new CharArrayWriter(); 00252 cw.write("</"); 00253 cw.write(tag); 00254 cw.write(">"); 00255 char[] ch = cw.toCharArray(); 00256 characters(ch, 0, ch.length); 00257 } 00258 } catch (Exception e) { 00259 Debug.print(e); 00261 } 00262 } 00263 00264 /** 00265 * Entity resolver interface. 00266 */ 00268 throws IOException 00269 { 00270 String name = null; 00271 if (!systemId.startsWith("/")) { 00272 name = jsp.getPackageName() + "/" + systemId; 00273 } 00274 InputStream in = context.getResourceAsStream(name); 00276 if (is != null) { 00277 BufferedInputStream bin = new BufferedInputStream(in); 00278 InputStreamReader r = new InputStreamReader(bin); 00280 } 00281 return is; 00282 } 00283 } 00284 00285