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.PrintWriter;
042:
043: import java.util.Hashtable;
044:
045: import org.xml.sax.AttributeList;
046:
047: import com.quadcap.util.Debug;
048:
049: /**
050: *
051: * Implementation of the <b><jsp:useBean></b> tag.
052: *
053: * @author Stan Bailes
054: */
055: public class TagJspUseBean extends TagJsp {
056: static Hashtable scopes = new Hashtable();
057: static {
058: scopes.put("page", "PageContext.PAGE_SCOPE");
059: scopes.put("request", "PageContext.REQUEST_SCOPE");
060: scopes.put("session", "PageContext.SESSION_SCOPE");
061: scopes.put("application", "PageContext.APPLICATION_SCOPE");
062: }
063:
064: public TagJspUseBean() {
065: }
066:
067: public TagJspUseBean(TagContext context) {
068: super (context);
069: }
070:
071: public TagInstance makeInstance(TagContext context) {
072: return new TagJspUseBean(context);
073: }
074:
075: public void doStartTag(String tagName, AttributeList attributes)
076: throws JspException {
077: super .doStartTag(tagName, attributes);
078: PrintWriter w = context.getPrintWriter();
079: String id = attributes.getValue("id");
080: String clazz = attributes.getValue("class");
081: String scope = attributes.getValue("scope");
082: if (scope == null || scope.equals(""))
083: scope = "page";
084: String scopeVal = (String) scopes.get(scope);
085: if (scopeVal == null) {
086: throw new JspException("Invalid scope for <jsp:useBean: "
087: + scope);
088: }
089: // clazz id = null;
090: w.print(" ");
091: w.print(clazz);
092: w.print(" ");
093: w.print(id);
094: w.println(" = null;");
095:
096: // synchronized (this) {
097: w.println(" synchronized (this) {");
098:
099: // id = (clazz)pageContext.getAttribute("id", SCOPE);
100: w.print(" ");
101: w.print(id);
102: w.print(" = (");
103: w.print(clazz);
104: w.print(")pageContext.getAttribute(\"");
105: w.print(id);
106: w.print("\", ");
107: w.print(scopeVal);
108: w.println(");");
109:
110: // if (id == null) {
111: w.print(" if (");
112: w.print(id);
113: w.println(" == null) {");
114:
115: // id = (clazz)Class.forName("clazz").newInstance();
116: w.print(" ");
117: w.print(id);
118: w.print(" = (");
119: w.print(clazz);
120: w.print(")Class.forName(\"");
121: w.print(clazz);
122: w.println("\").newInstance();");
123:
124: // pageContext.setAttribute("id", id, SCOPE);
125: w.print(" pageContext.setAttribute(\"");
126: w.print(id);
127: w.print("\", ");
128: w.print(id);
129: w.print(", ");
130: w.print(scopeVal);
131: w.println(");");
132:
133: // }
134: w.println(" }");
135:
136: // }
137: w.println(" }");
138: }
139:
140: public void doCharacters(char[] ch, int off, int cnt)
141: throws JspException {
142: doTemplateCharacters(ch, off, cnt);
143: }
144: }
|