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: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jsp.java;
030:
031: import com.caucho.jsp.JspParseException;
032: import com.caucho.jsp.cfg.TldVariable;
033: import com.caucho.vfs.WriteStream;
034: import com.caucho.xml.QName;
035:
036: import java.io.IOException;
037: import java.util.ArrayList;
038:
039: /**
040: * jsp:invoke invokes a fragment
041: */
042: public class JspInvoke extends JspNode {
043: private static final QName FRAGMENT = new QName("fragment");
044: private static final QName VAR = new QName("var");
045: private static final QName SCOPE = new QName("scope");
046: private static final QName VAR_READER = new QName("varReader");
047:
048: private String _name;
049: private String _var;
050: private String _varReader;
051: private String _scope;
052:
053: /**
054: * Adds an attribute.
055: */
056: public void addAttribute(QName name, String value)
057: throws JspParseException {
058: if (FRAGMENT.equals(name))
059: _name = value;
060: else if (VAR.equals(name))
061: _var = value;
062: else if (VAR_READER.equals(name))
063: _varReader = value;
064: else if (SCOPE.equals(name))
065: _scope = value;
066: else
067: throw error(L.l(
068: "`{0}' is an unknown attribute for jsp:invoke.",
069: name.getName()));
070: }
071:
072: /**
073: * Called when the attributes end.
074: */
075: public void endAttributes() throws JspParseException {
076: if (!_gen.getParseState().isTag())
077: throw error(L
078: .l(
079: "`{0}' is only allowed in .tag files. Attribute directives are not allowed in normal JSP files.",
080: getTagName()));
081: }
082:
083: /**
084: * Called when the body ends.
085: */
086: public void endElement() throws JspParseException {
087: if (_name == null)
088: throw error(L
089: .l("'fragment' is a required attribute of <jsp:invoke>."));
090:
091: if (_scope != null && _var == null && _varReader == null)
092: throw error(L
093: .l("'scope' requires a 'var' or a 'varReader' attribute for <jsp:invoke>."));
094:
095: if (_var != null && _varReader != null)
096: throw error(L
097: .l("jsp:invoke may not have both 'var' and 'varReader'"));
098: }
099:
100: /**
101: * Generates the XML text representation for the tag validation.
102: *
103: * @param os write stream to the generated XML.
104: */
105: public void printXml(WriteStream os) throws IOException {
106: os.print("<jsp:invoke");
107:
108: if (_name != null)
109: os.print(" name=\"" + _name + "\"");
110:
111: if (_var != null)
112: os.print(" var=\"" + _var + "\"");
113:
114: if (_varReader != null)
115: os.print(" varReader=\"" + _varReader + "\"");
116:
117: if (_scope != null)
118: os.print(" scope=\"" + _scope + "\"");
119:
120: os.print("/>");
121: }
122:
123: /**
124: * Generates the children.
125: */
126: public void generate(JspJavaWriter out) throws Exception {
127: String name = "_jsp_frag_" + _gen.uniqueId();
128:
129: out
130: .println("javax.servlet.jsp.tagext.JspFragment "
131: + name
132: + " = (javax.servlet.jsp.tagext.JspFragment) pageContext.getAttribute(\""
133: + _name + "\");");
134:
135: out.println("if (" + name + " != null) {");
136: out.pushDepth();
137: JavaTagGenerator gen = (JavaTagGenerator) _gen;
138: ArrayList<TldVariable> vars = gen.getVariables();
139:
140: for (int i = 0; i < vars.size(); i++) {
141: TldVariable var = vars.get(i);
142:
143: if (var.getScope().equals("AT_END"))
144: continue;
145:
146: String srcName = var.getNameGiven();
147: String dstName = srcName;
148:
149: if (srcName == null) {
150: srcName = var.getAlias();
151: dstName = var.getNameFromAttribute();
152: dstName = "_jsp_var_from_attribute_" + i;
153: } else
154: dstName = "\"" + dstName + "\"";
155:
156: out.print("_jsp_parentContext.setAttribute(" + dstName
157: + ", ");
158: out.println("pageContext.getAttribute(\"" + srcName
159: + "\"));");
160: }
161:
162: /*
163: if (vars.size() > 0) {
164: out.println("try {");
165: out.pushDepth();
166: }
167: */
168:
169: String context = null;
170: if (_scope == null || _scope.equals("page"))
171: context = "pageContext";
172: else if (_scope.equals("request"))
173: context = "pageContext.getRequest()";
174: else if (_scope.equals("session"))
175: context = "pageContext.getSessionScope()";
176: else if (_scope.equals("application"))
177: context = "pageContext.getApplication()";
178: else
179: throw error(L
180: .l(
181: "Unknown scope `{0}' in <jsp:invoke>. Scope must be `page', `request', `session', or `application'.",
182: _scope));
183:
184: if (_var != null) {
185: out.print(context + ".setAttribute(\"" + _var + "\", ");
186: out.println("pageContext.invoke(" + name + "));");
187: } else if (_varReader != null) {
188: out.print(context + ".setAttribute(\"" + _varReader
189: + "\", ");
190: out.println("pageContext.invokeReader(" + name + "));");
191: } else {
192: out.println(name + ".invoke(null);");
193: }
194:
195: out.popDepth();
196: out.println("}");
197: }
198: }
|