KickJava   Java API By Example, From Geeks To Geeks.

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


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

42 public class CustomSimpleTag extends GenericTag
43 {
44   JspBody _body;
45   private boolean _oldScriptingInvalid;
46
47   /**
48    * Simple tags can't be reused.
49    */

50   public boolean isReuse()
51   {
52     return false;
53   }
54   
55   /**
56    * Called when the attributes end.
57    */

58   public void endAttributes()
59     throws JspParseException
60   {
61     super.endAttributes();
62     
63     _oldScriptingInvalid = _parseState.isScriptingInvalid();
64     // jsp/18dj
65
// _parseState.setScriptingInvalid(true);
66
}
67   
68   /**
69    * Adds a child node.
70    */

71   public void endElement()
72     throws Exception JavaDoc
73   {
74     super.endElement();
75     
76     _parseState.setScriptingInvalid(_oldScriptingInvalid);
77     
78     if (isEmpty())
79       return;
80
81     for (int i = 0; i < _children.size(); i++) {
82       JspNode node = (JspNode) _children.get(i);
83
84       if (node instanceof JspBody) {
85     if (_body != null)
86       throw error(L.l("Only one <jsp:body> is allowed as a child of a tag."));
87               
88         _body = (JspBody) node;
89         _children.remove(i);
90         return;
91       }
92     }
93
94     _body = new JspBody();
95     _body.setParent(this);
96     _body.setStartLocation(_sourcePath, _filename, _startLine);
97     _body.setGenerator(_gen);
98     _body.endAttributes();
99     _body.setEndLocation(_filename, _startLine);
100     
101     for (int i = 0; i < _children.size(); i++) {
102       JspNode node = _children.get(i);
103
104       if (! (node instanceof JspAttribute))
105     _body.addChild(node);
106       
107       _body.setEndLocation(node.getFilename(), node.getEndLine());
108     }
109     _body.endElement();
110     _children = null;
111   }
112   
113   /**
114    * Set true if the node contains a child tag.
115    */

116   public boolean hasCustomTag()
117   {
118     if (_body != null && _body.hasCustomTag())
119       return true;
120     else
121       return super.hasCustomTag();
122   }
123   
124   /**
125    * Generates code before the actual JSP.
126    */

127   public void generatePrologue(JspJavaWriter out)
128     throws Exception JavaDoc
129   {
130     super.generatePrologue(out);
131
132     if (_body != null) {
133       _body.setJspFragment(true);
134       _body.generateFragmentPrologue(out);
135     }
136     
137     if (hasCustomTag()) {
138       // jsp/18ei, jsp/18e8
139
if (_tag.generateAdapterDeclaration()) {
140     out.println("javax.servlet.jsp.tagext.Tag " + _tag.getId() + "_adapter = null;");
141       }
142     }
143   }
144   
145   /**
146    * Generates the code for a custom tag.
147    *
148    * @param out the output writer for the generated java.
149    */

150   public void generate(JspJavaWriter out)
151     throws Exception JavaDoc
152   {
153     String JavaDoc name = _tag.getId();
154     String JavaDoc className = _tagInfo.getTagClassName();
155     Class JavaDoc cl = _tagClass;
156
157     if (! isReuse()) {
158       generateTagInit(out);
159     }
160     else if (! isDeclared()) {
161       out.println("if (" + name + " == null) {");
162       out.pushDepth();
163       generateTagInit(out);
164       out.popDepth();
165       out.println("}");
166       out.println();
167     }
168
169     fillAttributes(out, name);
170
171     if (_body != null) {
172       out.print(name + ".setJspBody(");
173       generateFragment(out, _body, "pageContext");
174       out.println(");");
175     }
176
177     out.println(name + ".doTag();");
178
179     printVarDeclaration(out, VariableInfo.AT_END);
180   }
181
182   /**
183    * Generates the initialization code for the tag.
184    *
185    * @param out the output stream
186    */

187   private void generateTagInit(JspJavaWriter out)
188     throws Exception JavaDoc
189   {
190     TagInstance parent = _tag.getParent();
191
192     String JavaDoc var = _tag.getId();
193     String JavaDoc className = _tag.getTagClass().getName();
194       
195     out.print(var + " = new ");
196     out.printClass(_tag.getTagClass());
197     out.println("();");
198
199     if (JspIdConsumer JavaDoc.class.isAssignableFrom(_tag.getTagClass())) {
200       String JavaDoc shortName = className;
201       int p = shortName.lastIndexOf('.');
202       if (p >= 0)
203     shortName = shortName.substring(p + 1);
204
205       out.print(var + ".setJspId(\"" + shortName + "-" + _gen.generateJspId() + "\");");
206     }
207
208     out.println(var + ".setJspContext(pageContext);");
209     JspNode parentNode = getParent().getParentTagNode();
210     if (parentNode != null) {
211       out.println(var + ".setParent(" + parentNode.getCustomTagName() + ");");
212     }
213
214     if (_tag.getAnalyzedTag() != null
215     && _tag.getAnalyzedTag().getHasInjection()) {
216       out.println("_jsp_inject_" + _tag.getId() + ".configure(" + var + ");");
217     }
218
219     if (hasCustomTag()) {
220       out.println(var + "_adapter = new javax.servlet.jsp.tagext.TagAdapter(" + var + ");");
221     }
222
223     ArrayList JavaDoc<QName> names = _tag.getAttributeNames();
224     for (int i = 0; i < names.size(); i++) {
225       QName name = names.get(i);
226
227       String JavaDoc value = _tag.getAttribute(name);
228       if (value == null)
229         continue;
230
231       generateSetAttribute(out, var, name, value, false, false,
232                _tag.getAttributeInfo(name.getLocalName()));
233     }
234   }
235 }
236
Popular Tags