001: package com.quadcap.http.servlets.jsp;
002:
003: /* Copyright 1999 - 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.CharArrayWriter;
042: import java.io.IOException;
043: import java.io.PrintWriter;
044:
045: import java.util.Enumeration;
046:
047: import org.xml.sax.AttributeList;
048:
049: import com.quadcap.util.Debug;
050:
051: /**
052: * The pseudo tag <b><jsp:root></b>.
053: *
054: * @author Stan Bailes
055: */
056: public class TagJspRoot extends TagJsp {
057: CharArrayWriter cw = null;
058: PrintWriter w = null;
059: PrintWriter ow = null;
060: AttributeList attributes = null;
061:
062: public TagJspRoot() {
063: }
064:
065: public TagJspRoot(TagContext context) {
066: super (context);
067: }
068:
069: public TagInstance makeInstance(TagContext context) {
070: return new TagJspRoot(context);
071: }
072:
073: public void doStartTag(String tagName, AttributeList attributes)
074: throws JspException {
075: super .doStartTag(tagName, attributes);
076: cw = new CharArrayWriter();
077: w = new PrintWriter(cw);
078: ow = context.pushPrintWriter(w);
079: this .attributes = attributes;
080: }
081:
082: public void writeProlog(PrintWriter w) {
083: w.print("package ");
084: w.print(context.getPackageName());
085: w.println(";");
086: w.println("//This code auto-generated");
087: w.println("");
088: w.println("import java.io.IOException;");
089: w.println("");
090: w.println("import javax.servlet.jsp.HttpJspPage;");
091: w.println("import javax.servlet.jsp.JspFactory;");
092: w.println("import javax.servlet.jsp.JspWriter;");
093: w.println("import javax.servlet.jsp.PageContext;");
094: w.println("");
095: w.println("import javax.servlet.ServletConfig;");
096: w.println("import javax.servlet.ServletContext;");
097: w.println("import javax.servlet.ServletException;");
098: w.println("");
099: 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 = context.getPageDirective("extends",
114: "com.quadcap.http.servlets.jsp.JspPage");
115: w.print(" extends ");
116: w.print(extendsStr);
117:
118: if (context.getPageDirective("isThreadSafe", "true").equals(
119: "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
130: .getPageDirectives("declarations");
131: while (declarations.hasMoreElements()) {
132: w.println(declarations.nextElement());
133: }
134: w.println();
135:
136: String info = context.getPageDirective("info");
137: if (info != null && info.length() > 0) {
138: w.println(" public String getServletInfo() {");
139: w.print(" return \"");
140: w.print(info);
141: w.println("\");");
142: w.println();
143: }
144:
145: w
146: .print(" public void _jspService(HttpServletRequest request,");
147: w
148: .print(" HttpServletResponse response) throws ServletException, ");
149: w.println(" IOException {");
150:
151: w
152: .println(" JspFactory factory = JspFactory.getDefaultFactory();");
153: w
154: .println(" PageContext pageContext = factory.getPageContext(");
155: w.print(" this, request, response, ");
156: String err = context.getPageDirective("errorPage");
157: if (err == null) {
158: err = "null";
159: } else {
160: err = "\"" + err + "\"";
161: }
162: w.print(err);
163: w.println(", false,");
164:
165: String b = context.getPageDirective("buffer", "8kb");
166: int mul = 1;
167: if (b.endsWith("kb")) {
168: b = b.substring(0, b.length() - 2);
169: mul = 1024;
170: }
171: int bufSize = Integer.parseInt(b) * mul;
172: w.print(" ");
173: w.print(String.valueOf(bufSize));
174: w.print(", ");
175:
176: String autoFlush = context
177: .getPageDirective("autoFlush", "true");
178: w.print(autoFlush);
179: w.println(");");
180:
181: String s = context.getPageDirective("session");
182: if (s == null || !s.equals("false")) {
183: w
184: .println(" HttpSession session = pageContext.getSession();");
185: }
186:
187: s = context.getPageDirective("isErrorPage");
188: if (s != null && s.equals("true")) {
189: w
190: .println(" Exception exception = pageContext.getException();");
191: }
192:
193: w.println(" JspWriter out = pageContext.getOut();");
194: w.println(" Object page = this;");
195: w
196: .println(" ServletContext application = pageContext.getServletContext();");
197: w
198: .println(" ServletConfig config = pageContext.getServletConfig();");
199:
200: s = context.getPageDirective("contentType");
201: if (s != null && s.length() > 0) {
202: w.print(" response.setContentType(\"");
203: w.print(s);
204: w.print("\");");
205: }
206: w.println(" try {");
207: }
208:
209: public void doEndTag() throws JspException {
210: w.flush();
211: writeProlog(ow);
212: try {
213: cw.writeTo(ow);
214: } catch (IOException e) {
215: Debug.print(e);
216: throw new JspException(e.toString());
217: }
218: doEpilog(ow);
219: context.popPrintWriter();
220: }
221:
222: void doEpilog(PrintWriter w) {
223: w.println(" } catch (Exception e) {");
224: w.println(" out.clear();");
225: w.println(" pageContext.handlePageException(e);");
226: // w.println(" } finally {");
227: // w.println(" out.flush();");
228: w.println(" }");
229: w.println(" }");
230: w.println("}");
231: }
232:
233: public void doCharacters(char[] ch, int off, int cnt)
234: throws JspException {
235: PrintWriter w = context.getPrintWriter();
236:
237: w.print("out.print(\"");
238: for (int i = 0; i < cnt; i++) {
239: char c = ch[off + i];
240: switch (c) {
241: case '\n':
242: w.print("\\n");
243: break;
244: case '\r':
245: w.print("\\r");
246: break;
247: case '\t':
248: w.print("\\t");
249: break;
250: case '\'':
251: w.print("\\'");
252: break;
253: case '\"':
254: w.print("\\\"");
255: break;
256: case '\\':
257: w.print("\\\\");
258: break;
259: default:
260: if (c > 0xfff) {
261: w.print("\\u");
262: w.print(Integer.toHexString(c));
263: } else if (c > 0xff) {
264: w.print("\\u0");
265: w.print(Integer.toHexString(c));
266: } else if (c <= 0x1f || c >= 0xf7f) {
267: w.print("\\u00");
268: w.print(Integer.toHexString(c));
269: } else {
270: w.print((char) c);
271: }
272: }
273: }
274: w.println("\");");
275: }
276: }
|