KickJava   Java API By Example, From Geeks To Geeks.

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


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.xml.QName;
32
33 import javax.servlet.jsp.tagext.JspFragment JavaDoc;
34 import javax.servlet.jsp.tagext.TagAttributeInfo JavaDoc;
35 import javax.servlet.jsp.tagext.VariableInfo JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.util.ArrayList JavaDoc;
38
39 /**
40  * Represents a custom tag.
41  */

42 public class TagFileTag extends GenericTag {
43   private boolean _oldScriptingInvalid;
44   private JspBody _body;
45   private int _maxFragmentIndex;
46   private String JavaDoc _contextVarName;
47
48   /**
49    * Called when the attributes end.
50    */

51   public void endAttributes()
52   {
53     _oldScriptingInvalid = _parseState.isScriptingInvalid();
54     _parseState.setScriptingInvalid(true);
55   }
56   
57   /**
58    * Adds a child node.
59    */

60   public void endElement()
61     throws Exception JavaDoc
62   {
63     super.endElement();
64     
65     _parseState.setScriptingInvalid(_oldScriptingInvalid);
66     
67     if (_children == null || _children.size() == 0)
68       return;
69
70     for (int i = 0; i < _children.size(); i++) {
71       JspNode node = _children.get(i);
72
73       if (node instanceof JspBody) {
74     if (_body != null)
75       throw error(L.l("Only one <jsp:body> is allowed as a child of a tag."));
76         _body = (JspBody) node;
77         _children.remove(i);
78         return;
79       }
80     }
81
82     for (int i = 0; i < _children.size(); i++) {
83       JspNode node = _children.get(i);
84
85       if (! (node instanceof JspAttribute)) {
86     if (_body == null) {
87       _body = new JspBody();
88       _body.setParent(this);
89       _body.setGenerator(_gen);
90       _body.endAttributes();
91     }
92     
93         _body.addChild(node);
94       }
95     }
96     _body.endElement();
97     _children = null;
98   }
99
100   /**
101    * Generates the code for a custom tag.
102    *
103    * @param out the output writer for the generated java.
104    */

105   public void generateDeclaration(JspJavaWriter out)
106     throws IOException JavaDoc
107   {
108     super.generateDeclaration(out);
109
110     /*
111     out.println();
112     out.println("private static final com.caucho.jsp.java.JspTagFileSupport " + name + " = ");
113     out.println(" new " + className + "();");
114     */

115   }
116
117   /**
118    * Returns true if the tag file invocation contains a child tag.
119    */

120   public boolean hasTag()
121   {
122     return super.hasTag() || _body != null && _body.hasTag();
123   }
124   /**
125    * Returns null, since tag files aren't parent tags.
126    */

127   public String JavaDoc getCustomTagName()
128   {
129     return null;
130   }
131   
132   /**
133    * Generates code before the actual JSP.
134    */

135   public void generatePrologue(JspJavaWriter out)
136     throws Exception JavaDoc
137   {
138     super.generatePrologue(out);
139
140     if (_body != null) {
141       _body.setJspFragment(true);
142       _body.generateFragmentPrologue(out);
143     }
144   }
145   
146   /**
147    * Generates the code for a custom tag.
148    *
149    * @param out the output writer for the generated java.
150    */

151   public void generate(JspJavaWriter out)
152     throws Exception JavaDoc
153   {
154     String JavaDoc className = _tagInfo.getTagClassName();
155     Class JavaDoc cl = _tagClass;
156     
157     String JavaDoc name = className;
158
159     String JavaDoc childContext = fillTagFileAttributes(out, name);
160
161     out.print(name + ".doTag(pageContext, " + childContext + ", out, ");
162     if (_body != null)
163       generateFragment(out, _body, "pageContext");
164     else
165       out.print("null");
166     
167     out.println(");");
168
169     printVarDeclaration(out, VariableInfo.AT_END);
170   }
171   
172   public String JavaDoc fillTagFileAttributes(JspJavaWriter out, String JavaDoc tagName)
173     throws Exception JavaDoc
174   {
175     _contextVarName = "_jsp_context" + _gen.uniqueId();
176
177     String JavaDoc name = _contextVarName;
178
179     out.println("com.caucho.jsp.PageContextWrapper " + name);
180     out.println(" = com.caucho.jsp.PageContextWrapper.create(pageContext);");
181
182     TagAttributeInfo JavaDoc attrs[] = _tagInfo.getAttributes();
183
184     // clear any attributes mentioned in the taglib that aren't set
185
for (int i = 0; attrs != null && i < attrs.length; i++) {
186       int p = indexOf(_attributeNames, attrs[i].getName());
187       
188       if (p < 0 && attrs[i].isRequired()) {
189     throw error(L.l("required attribute `{0}' missing from <{1}>",
190                         attrs[i].getName(),
191                         getTagName()));
192       }
193     }
194
195     boolean isDynamic = _tagInfo.hasDynamicAttributes();
196     String JavaDoc mapAttribute = null;
197     String JavaDoc mapName = null;
198
199     if (isDynamic) {
200       TagInfoExt tagInfoImpl = (TagInfoExt) _tagInfo;
201       mapAttribute = tagInfoImpl.getDynamicAttributesName();
202     }
203     
204     // fill all mentioned attributes
205
for (int i = 0; i < _attributeNames.size(); i++) {
206       QName attrName = _attributeNames.get(i);
207       Object JavaDoc value = _attributeValues.get(i);
208       
209       TagAttributeInfo JavaDoc attribute = null;
210       int j = 0;
211       for (j = 0; attrs != null && j < attrs.length; j++) {
212     if (attrs[j].getName().equals(attrName.getName())) {
213           attribute = attrs[j];
214       break;
215         }
216       }
217
218       if (attribute == null && ! isDynamic)
219     throw error(L.l("unexpected attribute `{0}' in <{1}>",
220                         attrName.getName(), getTagName()));
221
222       boolean rtexprvalue = true;
223
224       Class JavaDoc cl = null;
225
226       if (attribute != null) {
227     cl = _gen.loadBeanClass(attribute.getTypeName());
228
229     rtexprvalue = attribute.canBeRequestTime();
230       }
231       
232       if (cl == null)
233     cl = String JavaDoc.class;
234
235       if (attribute == null) {
236     if (mapName == null) {
237       mapName = "_jsp_map_" + _gen.uniqueId();
238       out.println("java.util.HashMap " + mapName + " = new java.util.HashMap(8);");
239       out.println(name + ".setAttribute(\"" + mapAttribute + "\", " + mapName + ");");
240     }
241
242     out.print(mapName + ".put(\"" + attrName.getName() + "\", ");
243       }
244       else
245     out.print(name + ".setAttribute(\"" + attrName.getName() + "\", ");
246
247       if (value instanceof JspNode) {
248     JspFragmentNode frag = (JspFragmentNode) value;
249
250     if (attribute != null &&
251         attribute.getTypeName().equals(JspFragment JavaDoc.class.getName())) {
252       out.println(generateFragment(frag, "pageContext") + ");");
253     }
254     else
255       out.println(frag.generateValue() + ");");
256       }
257       else {
258     String JavaDoc convValue = generateParameterValue(cl,
259                           (String JavaDoc) value,
260                           rtexprvalue,
261                           attribute);
262       
263     // attribute.allowRtexpr());
264

265     out.println(toObject(cl, convValue) + ");");
266       }
267     }
268     
269     return name;
270   }
271
272   private int indexOf(ArrayList JavaDoc<QName> names, String JavaDoc name)
273   {
274     for (int i = 0; i < names.size(); i++) {
275       if (names.get(i).getName().equals(name))
276     return i;
277     }
278
279     return -1;
280   }
281
282   private String JavaDoc toObject(Class JavaDoc cl, String JavaDoc value)
283   {
284     if (boolean.class.equals(cl))
285       return "new Boolean(" + value + ")";
286     else if (byte.class.equals(cl))
287       return "new Byte(" + value + ")";
288     else if (short.class.equals(cl))
289       return "new Short(" + value + ")";
290     else if (int.class.equals(cl))
291       return "new Integer(" + value + ")";
292     else if (long.class.equals(cl))
293       return "new Long(" + value + ")";
294     else if (char.class.equals(cl))
295       return "new Character(" + value + ")";
296     else if (float.class.equals(cl))
297       return "new Float(" + value + ")";
298     else if (double.class.equals(cl))
299       return "new Double(" + value + ")";
300     else
301       return value;
302   }
303 }
304
Popular Tags