KickJava   Java API By Example, From Geeks To Geeks.

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


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.config.types.Signature;
32 import com.caucho.jsp.JspParseException;
33 import com.caucho.jsp.cfg.TldAttribute;
34 import com.caucho.util.L10N;
35 import com.caucho.vfs.WriteStream;
36 import com.caucho.xml.QName;
37
38 import javax.servlet.jsp.tagext.JspFragment JavaDoc;
39 import java.io.IOException JavaDoc;
40
41 public class JspDirectiveAttribute extends JspNode {
42   static L10N L = new L10N(JspDirectiveAttribute.class);
43
44   private static final QName NAME = new QName("name");
45   private static final QName REQUIRED = new QName("required");
46   private static final QName FRAGMENT = new QName("fragment");
47   private static final QName RTEXPRVALUE = new QName("rtexprvalue");
48   private static final QName TYPE = new QName("type");
49   private static final QName DESCRIPTION = new QName("description");
50   private static final QName DEFERRED_VALUE
51     = new QName("deferredValue");
52   private static final QName DEFERRED_VALUE_TYPE
53     = new QName("deferredValueType");
54   private static final QName DEFERRED_METHOD
55     = new QName("deferredMethod");
56   private static final QName DEFERRED_METHOD_SIGNATURE
57     = new QName("deferredMethodSignature");
58
59   private String JavaDoc _name;
60   private String JavaDoc _type;
61   private boolean _isRequired;
62   private boolean _isFragment;
63   private Boolean JavaDoc _isRtexprvalue;
64   private String JavaDoc _description;
65
66   private boolean _deferredValue;
67   private String JavaDoc _deferredValueType;
68   private boolean _deferredMethod;
69   private String JavaDoc _deferredMethodSignature;
70   
71   /**
72    * Adds an attribute.
73    *
74    * @param name the attribute name
75    * @param value the attribute value
76    */

77   public void addAttribute(QName name, String JavaDoc value)
78     throws JspParseException
79   {
80     if (! _gen.getParseState().isTag())
81       throw error(L.l("'{0}' is only allowed in .tag files. Attribute directives are not allowed in normal JSP files.",
82                       getTagName()));
83     
84     JavaTagGenerator gen = (JavaTagGenerator) _gen;
85     
86     if (NAME.equals(name)) {
87       if (gen.findVariable(value) != null) {
88     throw error(L.l("@attribute name '{0}' is already used by a variable.",
89             value));
90       }
91       else if (gen.findAttribute(value) != null) {
92     throw error(L.l("@attribute name '{0}' is already used by another attribute.",
93             value));
94       }
95       
96       _name = value;
97     }
98     else if (TYPE.equals(name))
99       _type = value;
100     else if (REQUIRED.equals(name))
101       _isRequired = attributeToBoolean(name.getName(), value);
102     else if (FRAGMENT.equals(name))
103       _isFragment = attributeToBoolean(name.getName(), value);
104     else if (RTEXPRVALUE.equals(name))
105       _isRtexprvalue = attributeToBoolean(name.getName(), value);
106     else if (DESCRIPTION.equals(name))
107       _description = value;
108     else if (DEFERRED_VALUE.equals(name)) {
109       _deferredValue = attributeToBoolean(name.getName(), value);
110       if (_deferredValue)
111     _type = "javax.el.ValueExpression";
112     }
113     else if (DEFERRED_VALUE_TYPE.equals(name)) {
114       _type = "javax.el.ValueExpression";
115       _deferredValueType = value;
116     }
117     else if (DEFERRED_METHOD.equals(name)) {
118       _deferredMethod = attributeToBoolean(name.getName(), value);
119       if (_deferredMethod)
120     _type = "javax.el.MethodExpression";
121     }
122     else if (DEFERRED_METHOD_SIGNATURE.equals(name)) {
123       try {
124     new Signature(value);
125       } catch (Exception JavaDoc e) {
126     throw error(e.getMessage());
127       }
128
129       _type = "javax.el.MethodExpression";
130       _deferredMethodSignature = value;
131     }
132     else {
133       throw error(L.l("'{0}' is an unknown JSP attribute directive attributes. The valid attributes are: deferredMethod, deferredMethodSignature, deferredValue, deferredValueType, description, fragment, name, rtexprvalue, type.",
134                       name.getName()));
135     }
136   }
137
138   /**
139    * When the element complets.
140    */

141   public void endElement()
142     throws JspParseException
143   {
144     if (! _gen.getParseState().isTag())
145       throw error(L.l("'{0}' is only allowed in .tag files. Attribute directives are not allowed in normal JSP files.",
146                       getTagName()));
147     
148     if (_name == null)
149       throw error(L.l("<{0}> needs a 'name' attribute.",
150                       getTagName()));
151
152     JavaTagGenerator tagGen = (JavaTagGenerator) _gen;
153
154     TldAttribute attr = new TldAttribute();
155     attr.setName(_name);
156
157     if (_type != null) {
158       Class JavaDoc type = loadClass(_type);
159       
160       if (type == null)
161         throw error(L.l("type '{0}' is an unknown class for tag attribute {1}.",
162                         _type, _name));
163       
164       if (type.isPrimitive())
165         throw error(L.l("attribute type '{0}' cannot be a Java primitive for {1}.",
166                         _type, _name));
167       
168       attr.setType(type);
169     }
170
171     attr.setRequired(_isRequired);
172     
173     if (_isFragment && _isRtexprvalue != null)
174       throw error(L.l("@attribute rtexprvalue cannot be set when fragment is true."));
175     
176     if (_isFragment && _type != null)
177       throw error(L.l("@attribute type cannot be set when fragment is true."));
178
179     if (_isRtexprvalue == null || Boolean.TRUE.equals(_isRtexprvalue))
180       attr.setRtexprvalue(Boolean.TRUE);
181     
182     if (_isFragment)
183       attr.setType(JspFragment JavaDoc.class);
184     
185     if (_deferredValue && _deferredValueType != null)
186       throw error(L.l("@attribute deferredValue and deferredValueType may not both be specified"));
187     
188     if (_deferredMethod && _deferredMethodSignature != null)
189       throw error(L.l("@attribute deferredMethod and deferredMethodSignature may not both be specified"));
190     
191     if ((_deferredValue || _deferredValueType != null)
192     && (_deferredMethod || _deferredMethodSignature != null))
193       throw error(L.l("@attribute deferredValue and deferredMethod may not both be specified"));
194
195     if (_deferredValue || _deferredValueType != null) {
196       attr.setDeferredValue(new TldAttribute.DeferredValue());
197
198       if (_deferredValueType != null)
199     attr.getDeferredValue().setType(_deferredValueType);
200     }
201
202     if (_deferredMethod || _deferredMethodSignature != null) {
203       attr.setDeferredMethod(new TldAttribute.DeferredMethod());
204
205       if (_deferredMethodSignature != null)
206     attr.getDeferredMethod().setMethodSignature(new Signature(_deferredMethodSignature));
207     }
208
209     tagGen.addAttribute(attr);
210   }
211   
212   /**
213    * Return true if the node only has static text.
214    */

215   public boolean isStatic()
216   {
217     return true;
218   }
219
220   /**
221    * Generates the XML text representation for the tag validation.
222    *
223    * @param os write stream to the generated XML.
224    */

225   public void printXml(WriteStream os)
226     throws IOException JavaDoc
227   {
228     os.print("<jsp:directive.attribute");
229     os.print(" jsp:id=\"" + _gen.generateJspId() + "\"");
230     os.print(" name=\"" + _name + "\"");
231
232     if (_type != null)
233       os.print(" type=\"" + _type + "\"");
234
235     os.println("/>");
236   }
237
238   /**
239    * Generates the code for the tag
240    *
241    * @param out the output writer for the generated java.
242    */

243   public void generate(JspJavaWriter out)
244     throws Exception JavaDoc
245   {
246   }
247 }
248
Popular Tags