KickJava   Java API By Example, From Geeks To Geeks.

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


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.PrintWriter JavaDoc;
42
43 import java.util.Hashtable JavaDoc;
44
45 import org.xml.sax.AttributeList JavaDoc;
46
47 import com.quadcap.util.Debug;
48
49 /**
50  *
51  * Implementation of the <b>&lt;jsp:useBean&gt;</b> tag.
52  *
53  * @author Stan Bailes
54  */

55 public class TagJspUseBean extends TagJsp {
56     static Hashtable JavaDoc scopes = new Hashtable JavaDoc();
57     static {
58     scopes.put("page", "PageContext.PAGE_SCOPE");
59     scopes.put("request", "PageContext.REQUEST_SCOPE");
60     scopes.put("session", "PageContext.SESSION_SCOPE");
61     scopes.put("application", "PageContext.APPLICATION_SCOPE");
62     }
63
64     public TagJspUseBean() {}
65
66     public TagJspUseBean(TagContext context) {
67     super(context);
68     }
69
70     public TagInstance makeInstance(TagContext context) {
71     return new TagJspUseBean(context);
72     }
73
74     public void doStartTag(String JavaDoc tagName, AttributeList JavaDoc attributes)
75     throws JspException
76     {
77     super.doStartTag(tagName, attributes);
78     PrintWriter JavaDoc w = context.getPrintWriter();
79     String JavaDoc id = attributes.getValue("id");
80     String JavaDoc clazz = attributes.getValue("class");
81     String JavaDoc scope = attributes.getValue("scope");
82     if (scope == null || scope.equals("")) scope = "page";
83     String JavaDoc scopeVal = (String JavaDoc)scopes.get(scope);
84     if (scopeVal == null) {
85         throw new JspException("Invalid scope for <jsp:useBean: " +
86                    scope);
87     }
88     // clazz id = null;
89
w.print(" ");
90     w.print(clazz);
91     w.print(" ");
92     w.print(id);
93     w.println(" = null;");
94
95     // synchronized (this) {
96
w.println(" synchronized (this) {");
97
98     // id = (clazz)pageContext.getAttribute("id", SCOPE);
99
w.print (" ");
100     w.print(id);
101     w.print(" = (");
102     w.print(clazz);
103     w.print(")pageContext.getAttribute(\"");
104     w.print(id);
105     w.print("\", ");
106     w.print(scopeVal);
107     w.println(");");
108
109     // if (id == null) {
110
w.print (" if (");
111     w.print(id);
112     w.println(" == null) {");
113
114     // id = (clazz)Class.forName("clazz").newInstance();
115
w.print (" ");
116     w.print(id);
117     w.print(" = (");
118     w.print(clazz);
119     w.print(")Class.forName(\"");
120     w.print(clazz);
121     w.println("\").newInstance();");
122
123     // pageContext.setAttribute("id", id, SCOPE);
124
w.print (" pageContext.setAttribute(\"");
125     w.print(id);
126     w.print("\", ");
127     w.print(id);
128     w.print(", ");
129     w.print(scopeVal);
130     w.println(");");
131
132     // }
133
w.println(" }");
134
135     // }
136
w.println(" }");
137     }
138
139     public void doCharacters(char[] ch, int off, int cnt) throws JspException {
140     doTemplateCharacters(ch, off, cnt);
141     }
142 }
143
144
Popular Tags