001: /**
002: * Licensed under the Common Development and Distribution License,
003: * you may not use this file except in compliance with the License.
004: * You may obtain a copy of the License at
005: *
006: * http://www.sun.com/cddl/
007: *
008: * Unless required by applicable law or agreed to in writing, software
009: * distributed under the License is distributed on an "AS IS" BASIS,
010: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
011: * implied. See the License for the specific language governing
012: * permissions and limitations under the License.
013: */package com.sun.facelets.tag;
014:
015: import javax.el.ELException;
016: import javax.el.ExpressionFactory;
017: import javax.el.MethodExpression;
018: import javax.el.ValueExpression;
019:
020: import com.sun.facelets.FaceletContext;
021: import com.sun.facelets.el.ELText;
022: import com.sun.facelets.el.TagMethodExpression;
023: import com.sun.facelets.el.TagValueExpression;
024:
025: /**
026: * Representation of a Tag's attribute in a Facelet File
027: *
028: * @author Jacob Hookom
029: * @version $Id: TagAttribute.java,v 1.8 2008/02/20 06:35:52 rlubke Exp $
030: */
031: public final class TagAttribute {
032:
033: private final boolean literal;
034:
035: private final String localName;
036:
037: private final Location location;
038:
039: private final String namespace;
040:
041: private final String qName;
042:
043: private final String value;
044:
045: private String string;
046:
047: public TagAttribute(Location location, String ns, String localName,
048: String qName, String value) {
049: this .location = location;
050: this .namespace = ns;
051: this .localName = localName;
052: this .qName = qName;
053: this .value = value;
054: try {
055: this .literal = ELText.isLiteral(this .value);
056: } catch (ELException e) {
057: throw new TagAttributeException(this , e);
058: }
059: }
060:
061: /**
062: * If literal, return
063: * {@link Boolean#getBoolean(java.lang.String) Boolean.getBoolean(java.lang.String)}
064: * passing our value, otherwise call
065: * {@link #getObject(FaceletContext, Class) getObject(FaceletContext, Class)}.
066: *
067: * @see Boolean#getBoolean(java.lang.String)
068: * @see #getObject(FaceletContext, Class)
069: * @param ctx
070: * FaceletContext to use
071: * @return boolean value
072: */
073: public boolean getBoolean(FaceletContext ctx) {
074: if (this .literal) {
075: return Boolean.valueOf(this .value).booleanValue();
076: } else {
077: return ((Boolean) this .getObject(ctx, Boolean.class))
078: .booleanValue();
079: }
080: }
081:
082: /**
083: * If literal, call
084: * {@link Integer#parseInt(java.lang.String) Integer.parseInt(String)},
085: * otherwise call
086: * {@link #getObject(FaceletContext, Class) getObject(FaceletContext, Class)}.
087: *
088: * @see Integer#parseInt(java.lang.String)
089: * @see #getObject(FaceletContext, Class)
090: * @param ctx
091: * FaceletContext to use
092: * @return int value
093: */
094: public int getInt(FaceletContext ctx) {
095: if (this .literal) {
096: return Integer.parseInt(this .value);
097: } else {
098: return ((Number) this .getObject(ctx, Integer.class))
099: .intValue();
100: }
101: }
102:
103: /**
104: * Local name of this attribute
105: *
106: * @return local name of this attribute
107: */
108: public String getLocalName() {
109: return this .localName;
110: }
111:
112: /**
113: * The location of this attribute in the FaceletContext
114: *
115: * @return the TagAttribute's location
116: */
117: public Location getLocation() {
118: return this .location;
119: }
120:
121: /**
122: * Create a MethodExpression, using this attribute's value as the expression
123: * String.
124: *
125: * @see ExpressionFactory#createMethodExpression(javax.el.ELContext,
126: * java.lang.String, java.lang.Class, java.lang.Class[])
127: * @see MethodExpression
128: * @param ctx
129: * FaceletContext to use
130: * @param type
131: * expected return type
132: * @param paramTypes
133: * parameter type
134: * @return a MethodExpression instance
135: */
136: public MethodExpression getMethodExpression(FaceletContext ctx,
137: Class type, Class[] paramTypes) {
138: try {
139: ExpressionFactory f = ctx.getExpressionFactory();
140: return new TagMethodExpression(this , f
141: .createMethodExpression(ctx, this .value, type,
142: paramTypes));
143: } catch (Exception 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 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 getObject(FaceletContext ctx) {
166: return this .getObject(ctx, Object.class);
167: }
168:
169: /**
170: * The qualified name for this attribute
171: *
172: * @return the qualified name for this attribute
173: */
174: public String 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 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 getValue(FaceletContext ctx) {
197: if (this .literal) {
198: return this .value;
199: } else {
200: return (String) this .getObject(ctx, String.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 getObject(FaceletContext ctx, Class type) {
219: if (this .literal) {
220: if (String.class.equals(type)) {
221: return this .value;
222: } else {
223: try {
224: return ctx.getExpressionFactory().coerceToType(
225: this .value, type);
226: } catch (Exception 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 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,
254: Class type) {
255: try {
256: ExpressionFactory f = ctx.getExpressionFactory();
257: return new TagValueExpression(this , f
258: .createValueExpression(ctx, this .value, type));
259: } catch (Exception e) {
260: throw new TagAttributeException(this , e);
261: }
262: }
263:
264: /**
265: * If this TagAttribute is literal (not #{..} or ${..})
266: *
267: * @return true if this attribute is literal
268: */
269: public boolean isLiteral() {
270: return this .literal;
271: }
272:
273: /*
274: * (non-Javadoc)
275: *
276: * @see java.lang.Object#toString()
277: */
278: public String toString() {
279: if (this .string == null) {
280: this .string = this .location + " " + this .qName + "=\""
281: + this .value + "\"";
282: }
283: return this.string;
284: }
285:
286: }
|