1 package com.quadcap.http.servlets.jsp; 2 3 40 41 import java.io.CharArrayWriter ; 42 import java.io.IOException ; 43 import java.io.PrintWriter ; 44 45 import java.util.Enumeration ; 46 47 import org.xml.sax.AttributeList ; 48 49 import com.quadcap.util.Debug; 50 51 56 public class TagJspRoot extends TagJsp { 57 CharArrayWriter cw = null; 58 PrintWriter w = null; 59 PrintWriter ow = null; 60 AttributeList attributes = null; 61 62 public TagJspRoot() {} 63 64 public TagJspRoot(TagContext context) { 65 super(context); 66 } 67 68 public TagInstance makeInstance(TagContext context) { 69 return new TagJspRoot(context); 70 } 71 72 public void doStartTag(String tagName, AttributeList attributes) 73 throws JspException 74 { 75 super.doStartTag(tagName, attributes); 76 cw = new CharArrayWriter (); 77 w = new PrintWriter (cw); 78 ow = context.pushPrintWriter(w); 79 this.attributes = attributes; 80 } 81 82 public void writeProlog(PrintWriter w) { 83 w.print("package "); 84 w.print(context.getPackageName()); 85 w.println(";"); 86 w.println("//This code auto-generated"); 87 w.println(""); 88 w.println("import java.io.IOException;"); 89 w.println(""); 90 w.println("import javax.servlet.jsp.HttpJspPage;"); 91 w.println("import javax.servlet.jsp.JspFactory;"); 92 w.println("import javax.servlet.jsp.JspWriter;"); 93 w.println("import javax.servlet.jsp.PageContext;"); 94 w.println(""); 95 w.println("import javax.servlet.ServletConfig;"); 96 w.println("import javax.servlet.ServletContext;"); 97 w.println("import javax.servlet.ServletException;"); 98 w.println(""); 99 w.println("import javax.servlet.http.HttpServletRequest;"); 100 w.println("import javax.servlet.http.HttpServletResponse;"); 101 w.println("import javax.servlet.http.HttpSession;"); 102 w.println(""); 103 Enumeration imports = context.getPageDirectives("import"); 104 while (imports.hasMoreElements()) { 105 w.print("import "); 106 w.print(imports.nextElement().toString()); 107 w.println(";"); 108 } 109 w.println(""); 110 w.print("public class "); 111 w.print(context.getClassName()); 112 113 String extendsStr = 114 context.getPageDirective("extends", 115 "com.quadcap.http.servlets.jsp.JspPage"); 116 w.print(" extends "); 117 w.print(extendsStr); 118 119 if (context.getPageDirective("isThreadSafe", "true").equals("false")) { 120 w.print(" implements javax.servlet.SingleThreadModel"); 121 } 122 w.println(" {"); 123 124 w.print(" public "); 125 w.print(context.getClassName()); 126 w.println("() {}"); 127 w.println(); 128 129 Enumeration declarations = context.getPageDirectives("declarations"); 130 while (declarations.hasMoreElements()) { 131 w.println(declarations.nextElement()); 132 } 133 w.println(); 134 135 String info = context.getPageDirective("info"); 136 if (info != null && info.length() > 0) { 137 w.println(" public String getServletInfo() {"); 138 w.print(" return \""); 139 w.print(info); 140 w.println("\");"); 141 w.println(); 142 } 143 144 145 w.print(" public void _jspService(HttpServletRequest request,"); 146 w.print(" HttpServletResponse response) throws ServletException, "); 147 w.println(" IOException {"); 148 149 w.println(" JspFactory factory = JspFactory.getDefaultFactory();"); 150 w.println(" PageContext pageContext = factory.getPageContext("); 151 w.print(" this, request, response, "); 152 String err = context.getPageDirective("errorPage"); 153 if (err == null) { 154 err = "null"; 155 } else { 156 err = "\"" + err + "\""; 157 } 158 w.print(err); 159 w.println(", false,"); 160 161 String b = context.getPageDirective("buffer", "8kb"); 162 int mul = 1; 163 if (b.endsWith("kb")) { 164 b = b.substring(0, b.length() - 2); 165 mul = 1024; 166 } 167 int bufSize = Integer.parseInt(b) * mul; 168 w.print(" "); 169 w.print(String.valueOf(bufSize)); 170 w.print(", "); 171 172 String autoFlush = context.getPageDirective("autoFlush", "true"); 173 w.print(autoFlush); 174 w.println(");"); 175 176 String s = context.getPageDirective("session"); 177 if (s == null || !s.equals("false")) { 178 w.println(" HttpSession session = pageContext.getSession();"); 179 } 180 181 s = context.getPageDirective("isErrorPage"); 182 if (s != null && s.equals("true")) { 183 w.println(" Exception exception = pageContext.getException();"); 184 } 185 186 w.println(" JspWriter out = pageContext.getOut();"); 187 w.println(" Object page = this;"); 188 w.println(" ServletContext application = pageContext.getServletContext();"); 189 w.println(" ServletConfig config = pageContext.getServletConfig();"); 190 191 s = context.getPageDirective("contentType"); 192 if (s != null && s.length() > 0) { 193 w.print(" response.setContentType(\""); 194 w.print(s); 195 w.print("\");"); 196 } 197 w.println(" try {"); 198 } 199 200 public void doEndTag() throws JspException { 201 w.flush(); 202 writeProlog(ow); 203 try { 204 cw.writeTo(ow); 205 } catch (IOException e) { 206 Debug.print(e); 207 throw new JspException(e.toString()); 208 } 209 doEpilog(ow); 210 context.popPrintWriter(); 211 } 212 213 void doEpilog(PrintWriter w) { 214 w.println(" } catch (Exception e) {"); 215 w.println(" out.clear();"); 216 w.println(" pageContext.handlePageException(e);"); 217 w.println(" }"); 220 w.println(" }"); 221 w.println("}"); 222 } 223 224 public void doCharacters(char[] ch, int off, int cnt) throws JspException { 225 PrintWriter w = context.getPrintWriter(); 226 227 w.print("out.print(\""); 228 for (int i = 0; i < cnt; i++) { 229 char c = ch[off + i]; 230 switch (c) { 231 case '\n': 232 w.print("\\n"); 233 break; 234 case '\r': 235 w.print("\\r"); 236 break; 237 case '\t': 238 w.print("\\t"); 239 break; 240 case '\'': 241 w.print("\\'"); 242 break; 243 case '\"': 244 w.print("\\\""); 245 break; 246 case '\\': 247 w.print("\\\\"); 248 break; 249 default: 250 if (c > 0xfff) { 251 w.print("\\u"); 252 w.print(Integer.toHexString(c)); 253 } else if (c > 0xff) { 254 w.print("\\u0"); 255 w.print(Integer.toHexString(c)); 256 } else if (c <= 0x1f || c >= 0xf7f) { 257 w.print("\\u00"); 258 w.print(Integer.toHexString(c)); 259 } else { 260 w.print((char)c); 261 } 262 } 263 } 264 w.println("\");"); 265 } 266 } 267 268 | Popular Tags |