KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

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

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

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

75   public void endAttributes()
76     throws JspParseException
77   {
78     if (! _gen.getParseState().isTag())
79       throw error(L.l("`{0}' is only allowed in .tag files. Attribute directives are not allowed in normal JSP files.",
80                       getTagName()));
81   }
82
83   /**
84    * Called when the body ends.
85    */

86   public void endElement()
87     throws JspParseException
88   {
89     if (_name == null)
90       throw error(L.l("'fragment' is a required attribute of <jsp:invoke>."));
91
92     if (_scope != null && _var == null && _varReader == null)
93       throw error(L.l("'scope' requires a 'var' or a 'varReader' attribute for <jsp:invoke>."));
94
95     if (_var != null && _varReader != null)
96       throw error(L.l("jsp:invoke may not have both 'var' and 'varReader'"));
97   }
98
99   /**
100    * Generates the XML text representation for the tag validation.
101    *
102    * @param os write stream to the generated XML.
103    */

104   public void printXml(WriteStream os)
105     throws IOException JavaDoc
106   {
107     os.print("<jsp:invoke");
108
109     if (_name != null)
110       os.print(" name=\"" + _name + "\"");
111     
112     if (_var != null)
113       os.print(" var=\"" + _var + "\"");
114     
115     if (_varReader != null)
116       os.print(" varReader=\"" + _varReader + "\"");
117     
118     if (_scope != null)
119       os.print(" scope=\"" + _scope + "\"");
120
121     os.print("/>");
122   }
123
124   /**
125    * Generates the children.
126    */

127   public void generate(JspJavaWriter out)
128     throws Exception JavaDoc
129   {
130     String JavaDoc name = "_jsp_frag_" + _gen.uniqueId();
131     
132     out.println("javax.servlet.jsp.tagext.JspFragment " + name + " = (javax.servlet.jsp.tagext.JspFragment) pageContext.getAttribute(\"" + _name + "\");");
133
134     out.println("if (" + name + " != null) {");
135     out.pushDepth();
136     JavaTagGenerator gen = (JavaTagGenerator) _gen;
137     ArrayList JavaDoc<TldVariable> vars = gen.getVariables();
138
139     for (int i = 0; i < vars.size(); i++) {
140       TldVariable var = vars.get(i);
141
142       if (var.getScope().equals("AT_END"))
143     continue;
144
145       String JavaDoc srcName = var.getNameGiven();
146       String JavaDoc dstName = srcName;
147       
148       if (srcName == null) {
149     srcName = var.getAlias();
150     dstName = var.getNameFromAttribute();
151     dstName = "_jsp_var_from_attribute_" + i;
152       }
153       else
154     dstName = "\"" + dstName + "\"";
155
156       out.print("_jsp_parentContext.setAttribute(" + dstName + ", ");
157       out.println("pageContext.getAttribute(\"" + srcName + "\"));");
158     }
159
160     /*
161     if (vars.size() > 0) {
162       out.println("try {");
163       out.pushDepth();
164     }
165     */

166     
167     String JavaDoc context = null;
168     if (_scope == null || _scope.equals("page"))
169       context = "pageContext";
170     else if (_scope.equals("request"))
171       context = "pageContext.getRequest()";
172     else if (_scope.equals("session"))
173       context = "pageContext.getSessionScope()";
174     else if (_scope.equals("application"))
175       context = "pageContext.getApplication()";
176     else
177       throw error(L.l("Unknown scope `{0}' in <jsp:invoke>. Scope must be `page', `request', `session', or `application'.", _scope));
178
179
180     if (_var != null) {
181       out.print(context + ".setAttribute(\"" + _var + "\", ");
182       out.println("pageContext.invoke(" + name + "));");
183     }
184     else if (_varReader != null) {
185       out.print(context + ".setAttribute(\"" + _varReader + "\", ");
186       out.println("pageContext.invokeReader(" + name + "));");
187     }
188     else {
189       out.println(name + ".invoke(null);");
190     }
191     
192     out.popDepth();
193     out.println("}");
194   }
195 }
196
Popular Tags