KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > facelets > tag > TagAttribute


1 /**
2  * Licensed under the Common Development and Distribution License,
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.sun.com/cddl/
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */

14
15 package com.sun.facelets.tag;
16
17 import javax.el.ELException;
18 import javax.el.ExpressionFactory;
19 import javax.el.MethodExpression;
20 import javax.el.ValueExpression;
21
22 import com.sun.facelets.FaceletContext;
23 import com.sun.facelets.el.ELText;
24 import com.sun.facelets.el.TagMethodExpression;
25 import com.sun.facelets.el.TagValueExpression;
26
27 /**
28  * Representation of a Tag's attribute in a Facelet File
29  *
30  * @author Jacob Hookom
31  * @version $Id: TagAttribute.java,v 1.7 2005/11/09 03:49:18 jhook Exp $
32  */

33 public final class TagAttribute {
34
35     private final boolean literal;
36
37     private final String JavaDoc localName;
38
39     private final Location location;
40
41     private final String JavaDoc namespace;
42
43     private final String JavaDoc qName;
44
45     private final String JavaDoc value;
46
47     private String JavaDoc string;
48
49     public TagAttribute(Location location, String JavaDoc ns, String JavaDoc localName,
50             String JavaDoc qName, String JavaDoc value) {
51         this.location = location;
52         this.namespace = ns;
53         this.localName = localName;
54         this.qName = qName;
55         this.value = value;
56         try {
57             this.literal = ELText.isLiteral(this.value);
58         } catch (ELException e) {
59             throw new TagAttributeException(this, e);
60         }
61     }
62
63     /**
64      * If literal, return
65      * {@link Boolean#getBoolean(java.lang.String) Boolean.getBoolean(java.lang.String)}
66      * passing our value, otherwise call
67      * {@link #getObject(FaceletContext, Class) getObject(FaceletContext, Class)}.
68      *
69      * @see Boolean#getBoolean(java.lang.String)
70      * @see #getObject(FaceletContext, Class)
71      * @param ctx
72      * FaceletContext to use
73      * @return boolean value
74      */

75     public boolean getBoolean(FaceletContext ctx) {
76         if (this.literal) {
77             return Boolean.valueOf(this.value).booleanValue();
78         } else {
79             return ((Boolean JavaDoc) this.getObject(ctx, Boolean JavaDoc.class))
80                     .booleanValue();
81         }
82     }
83
84     /**
85      * If literal, call
86      * {@link Integer#parseInt(java.lang.String) Integer.parseInt(String)},
87      * otherwise call
88      * {@link #getObject(FaceletContext, Class) getObject(FaceletContext, Class)}.
89      *
90      * @see Integer#parseInt(java.lang.String)
91      * @see #getObject(FaceletContext, Class)
92      * @param ctx
93      * FaceletContext to use
94      * @return int value
95      */

96     public int getInt(FaceletContext ctx) {
97         if (this.literal) {
98             return Integer.parseInt(this.value);
99         } else {
100             return ((Number JavaDoc) this.getObject(ctx, Number JavaDoc.class)).intValue();
101         }
102     }
103
104     /**
105      * Local name of this attribute
106      *
107      * @return local name of this attribute
108      */

109     public String JavaDoc getLocalName() {
110         return this.localName;
111     }
112
113     /**
114      * The location of this attribute in the FaceletContext
115      *
116      * @return the TagAttribute's location
117      */

118     public Location getLocation() {
119         return this.location;
120     }
121
122     /**
123      * Create a MethodExpression, using this attribute's value as the expression
124      * String.
125      *
126      * @see ExpressionFactory#createMethodExpression(javax.el.ELContext,
127      * java.lang.String, java.lang.Class, java.lang.Class[])
128      * @see MethodExpression
129      * @param ctx
130      * FaceletContext to use
131      * @param type
132      * expected return type
133      * @param paramTypes
134      * parameter type
135      * @return a MethodExpression instance
136      */

137     public MethodExpression getMethodExpression(FaceletContext ctx, Class JavaDoc type,
138             Class JavaDoc[] paramTypes) {
139         try {
140             ExpressionFactory f = ctx.getExpressionFactory();
141             return new TagMethodExpression(this, f.createMethodExpression(ctx,
142                     this.value, type, paramTypes));
143         } catch (Exception JavaDoc e) {
144             throw new TagAttributeException(this, e);
145         }
146     }
147
148     /**
149      * The resolved Namespace for this attribute
150      *
151      * @return resolved Namespace
152      */

153     public String JavaDoc getNamespace() {
154         return this.namespace;
155     }
156
157     /**
158      * Delegates to getObject with Object.class as a param
159      *
160      * @see #getObject(FaceletContext, Class)
161      * @param ctx
162      * FaceletContext to use
163      * @return Object representation of this attribute's value
164      */

165     public Object JavaDoc getObject(FaceletContext ctx) {
166         return this.getObject(ctx, Object JavaDoc.class);
167     }
168
169     /**
170      * The qualified name for this attribute
171      *
172      * @return the qualified name for this attribute
173      */

174     public String JavaDoc getQName() {
175         return this.qName;
176     }
177
178     /**
179      * Return the literal value of this attribute
180      *
181      * @return literal value
182      */

183     public String JavaDoc getValue() {
184         return this.value;
185     }
186
187     /**
188      * If literal, then return our value, otherwise delegate to getObject,
189      * passing String.class.
190      *
191      * @see #getObject(FaceletContext, Class)
192      * @param ctx
193      * FaceletContext to use
194      * @return String value of this attribute
195      */

196     public String JavaDoc getValue(FaceletContext ctx) {
197         if (this.literal) {
198             return this.value;
199         } else {
200             return (String JavaDoc) this.getObject(ctx, String JavaDoc.class);
201         }
202     }
203
204     /**
205      * If literal, simply coerce our String literal value using an
206      * ExpressionFactory, otherwise create a ValueExpression and evaluate it.
207      *
208      * @see ExpressionFactory#coerceToType(java.lang.Object, java.lang.Class)
209      * @see ExpressionFactory#createValueExpression(javax.el.ELContext,
210      * java.lang.String, java.lang.Class)
211      * @see ValueExpression
212      * @param ctx
213      * FaceletContext to use
214      * @param type
215      * expected return type
216      * @return Object value of this attribute
217      */

218     public Object JavaDoc getObject(FaceletContext ctx, Class JavaDoc type) {
219         if (this.literal) {
220             if (String JavaDoc.class.equals(type)) {
221                 return this.value;
222             } else {
223                 try {
224                     return ctx.getExpressionFactory().coerceToType(this.value,
225                             type);
226                 } catch (Exception JavaDoc e) {
227                     throw new TagAttributeException(this, e);
228                 }
229             }
230         } else {
231             ValueExpression ve = this.getValueExpression(ctx, type);
232             try {
233                 return ve.getValue(ctx);
234             } catch (Exception JavaDoc e) {
235                 throw new TagAttributeException(this, e);
236             }
237         }
238     }
239
240     /**
241      * Create a ValueExpression, using this attribute's literal value and the
242      * passed expected type.
243      *
244      * @see ExpressionFactory#createValueExpression(javax.el.ELContext,
245      * java.lang.String, java.lang.Class)
246      * @see ValueExpression
247      * @param ctx
248      * FaceletContext to use
249      * @param type
250      * expected return type
251      * @return ValueExpression instance
252      */

253     public ValueExpression getValueExpression(FaceletContext ctx, Class JavaDoc type) {
254         try {
255             ExpressionFactory f = ctx.getExpressionFactory();
256             return new TagValueExpression(this, f.createValueExpression(ctx,
257                     this.value, type));
258         } catch (Exception JavaDoc e) {
259             throw new TagAttributeException(this, e);
260         }
261     }
262
263     /**
264      * If this TagAttribute is literal (not #{..} or ${..})
265      *
266      * @return true if this attribute is literal
267      */

268     public boolean isLiteral() {
269         return this.literal;
270     }
271
272     /*
273      * (non-Javadoc)
274      *
275      * @see java.lang.Object#toString()
276      */

277     public String JavaDoc toString() {
278         if (this.string == null) {
279             this.string = this.location + " " + this.qName + "=\"" + this.value
280                     + "\"";
281         }
282         return this.string;
283     }
284
285 }
286
Popular Tags