001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.jsp.java;
031:
032: import com.caucho.jsp.JspParseException;
033: import com.caucho.jsp.cfg.TldVariable;
034: import com.caucho.vfs.WriteStream;
035: import com.caucho.xml.QName;
036:
037: import java.io.IOException;
038: import java.util.ArrayList;
039:
040: /**
041: * Represents a custom tag.
042: */
043: public class JspDoBody extends JspNode {
044: private static final QName VAR = new QName("var");
045: private static final QName VAR_READER = new QName("varReader");
046: private static final QName SCOPE = new QName("scope");
047:
048: private String _var;
049: private String _varReader;
050: private String _scope;
051:
052: /**
053: * Adds an attribute.
054: */
055: public void addAttribute(QName name, String value)
056: throws JspParseException {
057: if (VAR.equals(name))
058: _var = value;
059: else if (VAR_READER.equals(name))
060: _varReader = value;
061: else if (SCOPE.equals(name))
062: _scope = value;
063: else
064: throw error(L.l(
065: "'{0}' is an unknown attribute for jsp:doBody.",
066: name.getName()));
067: }
068:
069: /**
070: * Called when the attributes end.
071: */
072: public void endAttributes() throws JspParseException {
073: if (!_gen.getParseState().isTag())
074: throw error(L
075: .l(
076: "'{0}' is only allowed in .tag files. Attribute directives are not allowed in normal JSP files.",
077: getTagName()));
078:
079: if (_var != null && _varReader != null)
080: throw error(L
081: .l("'var' and 'varReader' cannot both be set in jsp:doBody."));
082:
083: if (_scope != null && _var == null && _varReader == null)
084: throw error(L
085: .l("jsp:doBody requires a 'var' or 'varReader' if 'scope' is set."));
086: }
087:
088: /**
089: * Generates the XML text representation for the tag validation.
090: *
091: * @param os write stream to the generated XML.
092: */
093: public void printXml(WriteStream os) throws IOException {
094: os.print("<jsp:do-body");
095: os.print(" jsp:id=\"" + _gen.generateJspId() + "\"");
096: if (_var != null)
097: os.print(" var=\"" + _var + "\"");
098: os.print(">");
099:
100: os.print("</jsp:do-body>");
101: }
102:
103: /**
104: * Generates the children.
105: */
106: public void generate(JspJavaWriter out) throws Exception {
107: String name = "_jsp_frag_" + _gen.uniqueId();
108:
109: if (_gen.hasScripting())
110: out.println("javax.servlet.jsp.tagext.JspFragment " + name
111: + " = getJspBody();");
112: else
113: out.println("javax.servlet.jsp.tagext.JspFragment " + name
114: + " = _jspBody;");
115:
116: out.println("if (" + name + " != null) {");
117: out.pushDepth();
118:
119: JavaTagGenerator gen = (JavaTagGenerator) _gen;
120: ArrayList<TldVariable> vars = gen.getVariables();
121:
122: for (int i = 0; i < vars.size(); i++) {
123: TldVariable var = vars.get(i);
124:
125: if (var.getScope().equals("AT_END"))
126: continue;
127:
128: String srcName = var.getNameGiven();
129: String dstName = srcName;
130:
131: if (srcName == null) {
132: srcName = var.getAlias();
133: dstName = var.getNameFromAttribute();
134: dstName = "_jsp_var_from_attribute_" + i;
135: } else
136: dstName = "\"" + dstName + "\"";
137:
138: out.print("_jsp_parentContext.setAttribute(" + dstName
139: + ", ");
140: out.println("pageContext.getAttribute(\"" + srcName
141: + "\"));");
142: }
143:
144: /*
145: if (vars.size() > 0) {
146: out.println("try {");
147: out.pushDepth();
148: }
149: */
150:
151: if (_var != null) {
152: out.print(getScope() + ".setAttribute(\"" + _var + "\", ");
153: out.println("pageContext.invoke(" + name + "));");
154: } else if (_varReader != null) {
155: out.print(getScope() + ".setAttribute(\"" + _varReader
156: + "\", ");
157: out.println("pageContext.invokeReader(" + name + "));");
158: } else {
159: out.println(name + ".invoke(null);");
160: }
161:
162: /*
163: if (vars.size() > 0) {
164: out.popDepth();
165: out.println("} finally {");
166: out.pushDepth();
167: }
168:
169: for (int i = 0; i < vars.size(); i++) {
170: TldVariable var = vars.get(i);
171:
172: if (var.getScope().equals("AT_END"))
173: continue;
174:
175: String srcName = var.getNameGiven();
176: String dstName = srcName;
177:
178: if (srcName == null) {
179: srcName = var.getAlias();
180: dstName = "_jsp_var_from_attribute_" + i;
181: }
182: else
183: dstName = "\"" + dstName + "\"";
184:
185: out.println("pageContext.setAttribute(\"" + srcName + "\", _jsp_parentContext.getAttribute(" + dstName + "));");
186: }
187:
188: if (vars.size() > 0) {
189: out.popDepth();
190: out.println("}");
191: }
192: */
193:
194: out.popDepth();
195: out.println("}");
196: }
197:
198: private String getScope() throws JspParseException {
199: String context = null;
200: if (_scope == null || _scope.equals("page"))
201: return "pageContext";
202: else if (_scope.equals("request")) {
203: return "pageContext.getRequest()";
204: } else if (_scope.equals("session")) {
205: return "pageContext.getSession()";
206: } else if (_scope.equals("application")) {
207: return "pageContext.getApplication()";
208: } else
209: throw error(L
210: .l(
211: "Unknown scope '{0}' in <jsp:doBody>. Scope must be 'page', 'request', 'session', or 'application'.",
212: _scope));
213: }
214: }
|