Quadcap Embeddable Server

com/quadcap/http/servlets/jsp/TagJspRoot.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.CharArrayWriter; 00042 import java.io.IOException; 00043 import java.io.PrintWriter; 00044 00045 import java.util.Enumeration; 00046 00047 import org.xml.sax.AttributeList; 00048 00049 import com.quadcap.util.Debug; 00050 00051 /** 00052 * The pseudo tag <b>&lt;jsp:root&gt;</b>. 00053 * 00054 * @author Stan Bailes 00055 */ 00056 public class TagJspRoot extends TagJsp { 00057 CharArrayWriter cw = null; 00058 PrintWriter w = null; 00059 PrintWriter ow = null; 00060 AttributeList attributes = null; 00061 00062 public TagJspRoot() {} 00063 00064 public TagJspRoot(TagContext context) { 00065 super(context); 00066 } 00067 00068 public TagInstance makeInstance(TagContext context) { 00069 return new TagJspRoot(context); 00070 } 00071 00072 public void doStartTag(String tagName, AttributeList attributes) 00073 throws JspException 00074 { 00075 super.doStartTag(tagName, attributes); 00076 cw = new CharArrayWriter(); 00077 w = new PrintWriter(cw); 00078 ow = context.pushPrintWriter(w); 00079 this.attributes = attributes; 00080 } 00081 00082 public void writeProlog(PrintWriter w) { 00083 w.print("package "); 00084 w.print(context.getPackageName()); 00085 w.println(";"); 00086 w.println("//This code auto-generated"); 00087 w.println(""); 00088 w.println("import java.io.IOException;"); 00089 w.println(""); 00090 w.println("import javax.servlet.jsp.HttpJspPage;"); 00091 w.println("import javax.servlet.jsp.JspFactory;"); 00092 w.println("import javax.servlet.jsp.JspWriter;"); 00093 w.println("import javax.servlet.jsp.PageContext;"); 00094 w.println(""); 00095 w.println("import javax.servlet.ServletConfig;"); 00096 w.println("import javax.servlet.ServletContext;"); 00097 w.println("import javax.servlet.ServletException;"); 00098 w.println(""); 00099 w.println("import javax.servlet.http.HttpServletRequest;"); 00100 w.println("import javax.servlet.http.HttpServletResponse;"); 00101 w.println("import javax.servlet.http.HttpSession;"); 00102 w.println(""); 00103 Enumeration imports = context.getPageDirectives("import"); 00104 while (imports.hasMoreElements()) { 00105 w.print("import "); 00106 w.print(imports.nextElement().toString()); 00107 w.println(";"); 00108 } 00109 w.println(""); 00110 w.print("public class "); 00111 w.print(context.getClassName()); 00112 00113 String extendsStr = 00114 context.getPageDirective("extends", 00115 "com.quadcap.http.servlets.jsp.JspPage"); 00116 w.print(" extends "); 00117 w.print(extendsStr); 00118 00119 if (context.getPageDirective("isThreadSafe", "true").equals("false")) { 00120 w.print(" implements javax.servlet.SingleThreadModel"); 00121 } 00122 w.println(" {"); 00123 00124 w.print(" public "); 00125 w.print(context.getClassName()); 00126 w.println("() {}"); 00127 w.println(); 00128 00129 Enumeration declarations = context.getPageDirectives("declarations"); 00130 while (declarations.hasMoreElements()) { 00131 w.println(declarations.nextElement()); 00132 } 00133 w.println(); 00134 00135 String info = context.getPageDirective("info"); 00136 if (info != null && info.length() > 0) { 00137 w.println(" public String getServletInfo() {"); 00138 w.print(" return \""); 00139 w.print(info); 00140 w.println("\");"); 00141 w.println(); 00142 } 00143 00144 00145 w.print(" public void _jspService(HttpServletRequest request,"); 00146 w.print(" HttpServletResponse response) throws ServletException, "); 00147 w.println(" IOException {"); 00148 00149 w.println(" JspFactory factory = JspFactory.getDefaultFactory();"); 00150 w.println(" PageContext pageContext = factory.getPageContext("); 00151 w.print(" this, request, response, "); 00152 String err = context.getPageDirective("errorPage"); 00153 if (err == null) { 00154 err = "null"; 00155 } else { 00156 err = "\"" + err + "\""; 00157 } 00158 w.print(err); 00159 w.println(", false,"); 00160 00161 String b = context.getPageDirective("buffer", "8kb"); 00162 int mul = 1; 00163 if (b.endsWith("kb")) { 00164 b = b.substring(0, b.length() - 2); 00165 mul = 1024; 00166 } 00167 int bufSize = Integer.parseInt(b) * mul; 00168 w.print(" "); 00169 w.print(String.valueOf(bufSize)); 00170 w.print(", "); 00171 00172 String autoFlush = context.getPageDirective("autoFlush", "true"); 00173 w.print(autoFlush); 00174 w.println(");"); 00175 00176 String s = context.getPageDirective("session"); 00177 if (s == null || !s.equals("false")) { 00178 w.println(" HttpSession session = pageContext.getSession();"); 00179 } 00180 00181 s = context.getPageDirective("isErrorPage"); 00182 if (s != null && s.equals("true")) { 00183 w.println(" Exception exception = pageContext.getException();"); 00184 } 00185 00186 w.println(" JspWriter out = pageContext.getOut();"); 00187 w.println(" Object page = this;"); 00188 w.println(" ServletContext application = pageContext.getServletContext();"); 00189 w.println(" ServletConfig config = pageContext.getServletConfig();"); 00190 00191 s = context.getPageDirective("contentType"); 00192 if (s != null && s.length() > 0) { 00193 w.print(" response.setContentType(\""); 00194 w.print(s); 00195 w.print("\");"); 00196 } 00197 w.println(" try {"); 00198 } 00199 00200 public void doEndTag() throws JspException { 00201 w.flush(); 00202 writeProlog(ow); 00203 try { 00204 cw.writeTo(ow); 00205 } catch (IOException e) { 00206 Debug.print(e); 00207 throw new JspException(e.toString()); 00208 } 00209 doEpilog(ow); 00210 context.popPrintWriter(); 00211 } 00212 00213 void doEpilog(PrintWriter w) { 00214 w.println(" } catch (Exception e) {"); 00215 w.println(" out.clear();"); 00216 w.println(" pageContext.handlePageException(e);"); 00217 // w.println(" } finally {"); 00218 // w.println(" out.flush();"); 00219 w.println(" }"); 00220 w.println(" }"); 00221 w.println("}"); 00222 } 00223 00224 public void doCharacters(char[] ch, int off, int cnt) throws JspException { 00225 PrintWriter w = context.getPrintWriter(); 00226 00227 w.print("out.print(\""); 00228 for (int i = 0; i < cnt; i++) { 00229 char c = ch[off + i]; 00230 switch (c) { 00231 case '\n': 00232 w.print("\\n"); 00233 break; 00234 case '\r': 00235 w.print("\\r"); 00236 break; 00237 case '\t': 00238 w.print("\\t"); 00239 break; 00240 case '\'': 00241 w.print("\\'"); 00242 break; 00243 case '\"': 00244 w.print("\\\""); 00245 break; 00246 case '\\': 00247 w.print("\\\\"); 00248 break; 00249 default: 00250 if (c > 0xfff) { 00251 w.print("\\u"); 00252 w.print(Integer.toHexString(c)); 00253 } else if (c > 0xff) { 00254 w.print("\\u0"); 00255 w.print(Integer.toHexString(c)); 00256 } else if (c <= 0x1f || c >= 0xf7f) { 00257 w.print("\\u00"); 00258 w.print(Integer.toHexString(c)); 00259 } else { 00260 w.print((char)c); 00261 } 00262 } 00263 } 00264 w.println("\");"); 00265 } 00266 } 00267