KickJava   Java API By Example, From Geeks To Geeks.

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


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.BufferedInputStream JavaDoc;
42 import java.io.BufferedReader JavaDoc;
43 import java.io.CharArrayWriter JavaDoc;
44 import java.io.File JavaDoc;
45 import java.io.FileReader JavaDoc;
46 import java.io.IOException JavaDoc;
47 import java.io.InputStream JavaDoc;
48 import java.io.InputStreamReader JavaDoc;
49 import java.io.PrintWriter JavaDoc;
50 import java.io.Reader JavaDoc;
51 import java.io.Writer JavaDoc;
52
53 import java.util.Enumeration JavaDoc;
54 import java.util.Hashtable JavaDoc;
55 import java.util.Vector JavaDoc;
56
57 import javax.servlet.ServletContext JavaDoc;
58
59 import org.xml.sax.AttributeList JavaDoc;
60 import org.xml.sax.DocumentHandler JavaDoc;
61 import org.xml.sax.DTDHandler JavaDoc;
62 import org.xml.sax.EntityResolver JavaDoc;
63 import org.xml.sax.ErrorHandler JavaDoc;
64 import org.xml.sax.HandlerBase JavaDoc;
65 import org.xml.sax.InputSource JavaDoc;
66 import org.xml.sax.Locator JavaDoc;
67 import org.xml.sax.Parser JavaDoc;
68 import org.xml.sax.SAXException JavaDoc;
69
70 import com.quadcap.util.collections.ArrayQueue;
71
72 import com.quadcap.util.Debug;
73
74 /**
75  * This class is a SAX parser handler for parsing JSP documents.
76  *
77  * @author Stan Bailes
78  */

79 public class JspHandler implements DocumentHandler JavaDoc, EntityResolver JavaDoc, TagContext {
80     ServletContext JavaDoc context;
81     JspParser parser;
82     JspObject jsp;
83     Hashtable JavaDoc tagHandlers = new Hashtable JavaDoc();
84     PrintWriter JavaDoc w;
85     CharArrayWriter JavaDoc cw = null;
86     boolean written = false;
87     ArrayQueue tagStack = new ArrayQueue(0, -1);
88     ArrayQueue wStack = new ArrayQueue(0, -1);
89     Hashtable JavaDoc pageDirectives = new Hashtable JavaDoc();
90
91     public JspHandler(ServletContext JavaDoc context, JspParser parser,
92                       PrintWriter JavaDoc w, JspObject jsp) {
93         this.context = context;
94     this.parser = parser;
95         this.jsp = jsp;
96     this.w = w;
97     addHandler("jsp:root", new TagJspRoot());
98     addHandler("jsp:useBean", new TagJspUseBean());
99     addHandler("jsp:expression", new TagJspExpression());
100     addHandler("jsp:expr", new TagJspExpression());
101     addHandler("jsp:declaration", new TagJspDeclaration());
102     addHandler("jsp:decl", new TagJspDeclaration());
103     addHandler("jsp:scriptlet", new TagJspScriptlet());
104     addHandler("jsp:directive.page", new TagJspPage());
105     addHandler("jsp:directive.include", new TagDirInclude());
106     addHandler("jsp:forward", new TagJspForward());
107     addHandler("jsp:include", new TagJspInclude());
108     addHandler("jsp:getProperty", new TagJspGetProperty());
109     addHandler("jsp:setProperty", new TagJspSetProperty());
110     }
111
112     public PrintWriter JavaDoc getPrintWriter() { return w; }
113     public String JavaDoc getPackageName() { return jsp.getPackageName(); }
114     public String JavaDoc getClassName() { return jsp.getClassName(); }
115
116     public void includeFile(String JavaDoc file) throws IOException JavaDoc, SAXException JavaDoc {
117     EntityResolver JavaDoc resolver = parser.getEntityResolver();
118     InputSource JavaDoc in = null;
119     if (resolver != null) {
120         in = resolver.resolveEntity(null, file);
121     } else {
122         FileReader JavaDoc f = new FileReader JavaDoc(file);
123         in = new InputSource JavaDoc(f);
124     }
125     if (in == null) {
126         throw new IOException JavaDoc("Can't resolve entity: " + file);
127     }
128     parser.pushInputSource(in);
129     }
130
131     public PrintWriter JavaDoc pushPrintWriter(PrintWriter JavaDoc p) {
132     wStack.addBack(w);
133     PrintWriter JavaDoc oldW = w;
134     w = p;
135     return oldW;
136     }
137
138     public PrintWriter JavaDoc popPrintWriter() {
139     w = (PrintWriter JavaDoc)wStack.popBack();
140     return w;
141     }
142
143     public void addPageDirective(String JavaDoc name, String JavaDoc val) {
144     Vector JavaDoc v = (Vector JavaDoc)pageDirectives.get(name);
145     if (v == null) {
146         v = new Vector JavaDoc();
147         pageDirectives.put(name, v);
148     }
149     v.addElement(val);
150     }
151
152     public Enumeration JavaDoc getPageDirectives(String JavaDoc name) {
153     Vector JavaDoc v = (Vector JavaDoc)pageDirectives.get(name);
154     if (v == null) v = new Vector JavaDoc();
155     return v.elements();
156     }
157
158     public String JavaDoc getPageDirective(String JavaDoc name) {
159     String JavaDoc s = null;
160     Vector JavaDoc v = (Vector JavaDoc)pageDirectives.get(name);
161     if (v != null && v.size() > 0) s = v.elementAt(0).toString();
162     return s;
163     }
164
165     public String JavaDoc getPageDirective(String JavaDoc name, String JavaDoc dflt) {
166     String JavaDoc s = dflt;
167     Vector JavaDoc v = (Vector JavaDoc)pageDirectives.get(name);
168     if (v != null && v.size() > 0) s = v.elementAt(0).toString();
169     return s;
170     }
171
172     public boolean isValidTag(String JavaDoc name) {
173     return tagHandlers.get(name) != null;
174     }
175
176     public void addHandler(String JavaDoc name, TagHandler h) {
177     tagHandlers.put(name, h);
178     }
179
180     public void startDocument() {
181     }
182
183     public void endDocument() {
184     }
185
186     public void ignorableWhitespace(char[] ch, int off, int cnt)
187     throws SAXException JavaDoc
188     {
189     characters(ch, off, cnt);
190     }
191
192     public void processingInstruction(String JavaDoc target, String JavaDoc data) {
193     }
194
195     public void setDocumentLocator(Locator JavaDoc locator) {
196     }
197
198     public void startElement(String JavaDoc tag, AttributeList JavaDoc attrs)
199     throws SAXException JavaDoc
200     {
201     //Debug.println("START: " + tag + " " + TagJsp.toString(attrs));
202
try {
203         TagHandler h = (TagHandler)tagHandlers.get(tag);
204         if (h != null) {
205         TagInstance t = h.makeInstance(this);
206         t.doStartTag(tag, attrs);
207         tagStack.addBack(t);
208         } else {
209         CharArrayWriter JavaDoc cw = new CharArrayWriter JavaDoc();
210         cw.write('<');
211         cw.write(tag);
212         for (int i = 0; i < attrs.getLength(); i++) {
213             cw.write(' ');
214             cw.write(attrs.getName(i));
215             cw.write('=');
216             cw.write('"');
217             cw.write(attrs.getValue(i));
218             cw.write('"');
219         }
220         cw.write('>');
221         char[] ch = cw.toCharArray();
222         characters(ch, 0, ch.length);
223         }
224     } catch (Exception JavaDoc e) {
225         Debug.print(e);
226         throw new SAXException JavaDoc(e.toString());
227     }
228     }
229    
230     public void characters(char[] ch, int off, int len) throws SAXException JavaDoc {
231     try {
232         TagInstance t = (TagInstance)tagStack.tail();
233         if (t != null) {
234         t.doCharacters(ch, off, len);
235         }
236     } catch (Exception JavaDoc e) {
237         Debug.print(e);
238         throw new SAXException JavaDoc(e.toString());
239     }
240     }
241
242     public void endElement(String JavaDoc tag) throws SAXException JavaDoc {
243     //Debug.println("END: " + tag);
244
try {
245         TagInstance t = (TagInstance)tagStack.tail();
246         //Debug.println("t = " + t);
247
if (t != null && t.getTagName().equals(tag)) {
248         tagStack.popBack();
249         t.doEndTag();
250         } else {
251         CharArrayWriter JavaDoc cw = new CharArrayWriter JavaDoc();
252         cw.write("</");
253         cw.write(tag);
254         cw.write(">");
255         char[] ch = cw.toCharArray();
256         characters(ch, 0, ch.length);
257         }
258     } catch (Exception JavaDoc e) {
259         Debug.print(e);
260         throw new SAXException JavaDoc(e.toString());
261     }
262     }
263
264     /**
265      * Entity resolver interface.
266      */

267     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
268     throws IOException JavaDoc
269     {
270         String JavaDoc name = null;
271     if (!systemId.startsWith("/")) {
272         name = jsp.getPackageName() + "/" + systemId;
273     }
274         InputStream JavaDoc in = context.getResourceAsStream(name);
275         InputSource JavaDoc is = null;
276         if (is != null) {
277             BufferedInputStream JavaDoc bin = new BufferedInputStream JavaDoc(in);
278             InputStreamReader JavaDoc r = new InputStreamReader JavaDoc(bin);
279             is = new InputSource JavaDoc(r);
280         }
281     return is;
282     }
283 }
284
285
286
Popular Tags