KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jsp > java > JspDoBody


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jsp.java;
31
32 import com.caucho.jsp.JspParseException;
33 import com.caucho.jsp.cfg.TldVariable;
34 import com.caucho.vfs.WriteStream;
35 import com.caucho.xml.QName;
36
37 import java.io.IOException JavaDoc;
38 import java.util.ArrayList JavaDoc;
39
40 /**
41  * Represents a custom tag.
42  */

43 public class JspDoBody extends JspNode {
44   private static final QName VAR = new QName("var");
45   private static final QName VAR_READER = new QName("varReader");
46   private static final QName SCOPE = new QName("scope");
47   
48   private String JavaDoc _var;
49   private String JavaDoc _varReader;
50   private String JavaDoc _scope;
51   
52   /**
53    * Adds an attribute.
54    */

55   public void addAttribute(QName name, String JavaDoc value)
56     throws JspParseException
57   {
58     if (VAR.equals(name))
59       _var = value;
60     else if (VAR_READER.equals(name))
61       _varReader = value;
62     else if (SCOPE.equals(name))
63       _scope = value;
64     else
65       throw error(L.l("'{0}' is an unknown attribute for jsp:doBody.",
66                       name.getName()));
67   }
68
69   /**
70    * Called when the attributes end.
71    */

72   public void endAttributes()
73     throws JspParseException
74   {
75     if (! _gen.getParseState().isTag())
76       throw error(L.l("'{0}' is only allowed in .tag files. Attribute directives are not allowed in normal JSP files.",
77                       getTagName()));
78
79     if (_var != null && _varReader != null)
80       throw error(L.l("'var' and 'varReader' cannot both be set in jsp:doBody."));
81
82     if (_scope != null && _var == null && _varReader == null)
83       throw error(L.l("jsp:doBody requires a 'var' or 'varReader' if 'scope' is set."));
84   }
85
86   /**
87    * Generates the XML text representation for the tag validation.
88    *
89    * @param os write stream to the generated XML.
90    */

91   public void printXml(WriteStream os)
92     throws IOException JavaDoc
93   {
94     os.print("<jsp:do-body");
95     os.print(" jsp:id=\"" + _gen.generateJspId() + "\"");
96     if (_var != null)
97       os.print(" var=\"" + _var + "\"");
98     os.print(">");
99     
100     os.print("</jsp:do-body>");
101   }
102
103   /**
104    * Generates the children.
105    */

106   public void generate(JspJavaWriter out)
107     throws Exception JavaDoc
108   {
109     String JavaDoc name = "_jsp_frag_" + _gen.uniqueId();
110
111     if (_gen.hasScripting())
112       out.println("javax.servlet.jsp.tagext.JspFragment " + name + " = getJspBody();");
113     else
114       out.println("javax.servlet.jsp.tagext.JspFragment " + name + " = _jspBody;");
115
116     out.println("if (" + name + " != null) {");
117     out.pushDepth();
118     
119     JavaTagGenerator gen = (JavaTagGenerator) _gen;
120     ArrayList JavaDoc<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 JavaDoc srcName = var.getNameGiven();
129       String JavaDoc dstName = srcName;
130       
131       if (srcName == null) {
132     srcName = var.getAlias();
133     dstName = var.getNameFromAttribute();
134     dstName = "_jsp_var_from_attribute_" + i;
135       }
136       else
137     dstName = "\"" + dstName + "\"";
138
139       out.print("_jsp_parentContext.setAttribute(" + dstName + ", ");
140       out.println("pageContext.getAttribute(\"" + srcName + "\"));");
141     }
142
143     /*
144     if (vars.size() > 0) {
145       out.println("try {");
146       out.pushDepth();
147     }
148     */

149
150     if (_var != null) {
151       out.print(getScope() + ".setAttribute(\"" + _var + "\", ");
152       out.println("pageContext.invoke(" + name + "));");
153     }
154     else if (_varReader != null) {
155       out.print(getScope() + ".setAttribute(\"" + _varReader + "\", ");
156       out.println("pageContext.invokeReader(" + name + "));");
157     }
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 JavaDoc getScope()
199     throws JspParseException
200   {
201     String JavaDoc context = null;
202     if (_scope == null || _scope.equals("page"))
203       return "pageContext";
204     else if (_scope.equals("request")) {
205       return "pageContext.getRequest()";
206     }
207     else if (_scope.equals("session")) {
208       return "pageContext.getSession()";
209     }
210     else if (_scope.equals("application")) {
211       return "pageContext.getApplication()";
212     }
213     else
214       throw error(L.l("Unknown scope '{0}' in <jsp:doBody>. Scope must be 'page', 'request', 'session', or 'application'.", _scope));
215   }
216 }
217   
218
Popular Tags