KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > http > servlets > jsp > TagJspRoot


1 package com.quadcap.http.servlets.jsp;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.CharArrayWriter JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.PrintWriter JavaDoc;
44
45 import java.util.Enumeration JavaDoc;
46
47 import org.xml.sax.AttributeList JavaDoc;
48
49 import com.quadcap.util.Debug;
50
51 /**
52  * The pseudo tag <b>&lt;jsp:root&gt;</b>.
53  *
54  * @author Stan Bailes
55  */

56 public class TagJspRoot extends TagJsp {
57     CharArrayWriter JavaDoc cw = null;
58     PrintWriter JavaDoc w = null;
59     PrintWriter JavaDoc ow = null;
60     AttributeList JavaDoc 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 JavaDoc tagName, AttributeList JavaDoc attributes)
73     throws JspException
74     {
75     super.doStartTag(tagName, attributes);
76     cw = new CharArrayWriter JavaDoc();
77     w = new PrintWriter JavaDoc(cw);
78     ow = context.pushPrintWriter(w);
79     this.attributes = attributes;
80     }
81
82     public void writeProlog(PrintWriter JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc declarations = context.getPageDirectives("declarations");
130     while (declarations.hasMoreElements()) {
131         w.println(declarations.nextElement());
132     }
133     w.println();
134
135     String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc autoFlush = context.getPageDirective("autoFlush", "true");
173     w.print(autoFlush);
174     w.println(");");
175
176     String JavaDoc 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 JavaDoc e) {
206         Debug.print(e);
207         throw new JspException(e.toString());
208     }
209     doEpilog(ow);
210     context.popPrintWriter();
211     }
212
213     void doEpilog(PrintWriter JavaDoc w) {
214     w.println(" } catch (Exception e) {");
215     w.println(" out.clear();");
216     w.println(" pageContext.handlePageException(e);");
217 // w.println(" } finally {");
218
// w.println(" out.flush();");
219
w.println(" }");
220     w.println(" }");
221     w.println("}");
222     }
223
224     public void doCharacters(char[] ch, int off, int cnt) throws JspException {
225     PrintWriter JavaDoc 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