KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > uitags > tagutil > ElEvaluator


1 /**
2  * Nov 15, 2004
3  *
4  * Copyright 2004 uitags
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package net.sf.uitags.tagutil;
19
20 import java.util.Collection JavaDoc;
21
22 import javax.servlet.jsp.JspException JavaDoc;
23 import javax.servlet.jsp.PageContext JavaDoc;
24 import javax.servlet.jsp.tagext.Tag JavaDoc;
25
26 import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
27
28 /**
29  * Shields the rest of the taglib from and provides a simpler interface to
30  * Apache's expression evaluator.
31  *
32  * @author jonni
33  * @version $Id$
34  */

35 public final class ElEvaluator {
36   /**
37    * The tag whose attribute values are to be evaluated.
38    */

39   private Tag JavaDoc tag;
40   /**
41    * The tag's page context.
42    */

43   private PageContext JavaDoc pageContext;
44
45   /**
46    * Creates an evaluator, suppling information about the tag it will be
47    * woking for.
48    *
49    * @param tag the tag whose attribute values are to be evaluated
50    * @param pageContext the tag's page context
51    */

52   public ElEvaluator(Tag JavaDoc tag, PageContext JavaDoc pageContext) {
53     this.tag = tag;
54     this.pageContext = pageContext;
55   }
56
57   /**
58    * Evaluates the expression <code>expr</code> to produce an object of
59    * the <code>expected</code> type.
60    *
61    * @param attrName the name of the attribute being evaluated
62    * @param expr the expression to evaluate
63    * @param expected the expected type of the evaluation result
64    * @return evaluation result or <code>null</code> if <code>expr</code> is
65    * <code>null</code>
66    * @throws RuntimeException if failed to perform evaluation
67    */

68   public Object JavaDoc toObject(String JavaDoc attrName, String JavaDoc expr, Class JavaDoc expected) {
69     try {
70       return (expr == null) ? null :
71           ExpressionEvaluatorManager.evaluate(
72           attrName, expr, expected, this.tag, this.pageContext);
73     }
74     catch (JspException JavaDoc e) {
75       throw new RuntimeException JavaDoc(e);
76     }
77   }
78
79   /**
80    * Evaluates the expression <code>expr</code> to produce a
81    * <code>String</code>.
82    *
83    * @param attrName the name of the attribute being evaluated
84    * @param expr the expression to evaluate
85    * @return evaluation result or <code>null</code> if <code>expr</code> is
86    * <code>null</code>
87    * @throws RuntimeException if failed to perform evaluation
88    */

89   public String JavaDoc toString(String JavaDoc attrName, String JavaDoc expr) {
90     try {
91       return (expr == null) ? null :
92           (String JavaDoc) ExpressionEvaluatorManager.evaluate(
93           attrName, expr, String JavaDoc.class, this.tag, this.pageContext);
94     }
95     catch (JspException JavaDoc e) {
96       throw new RuntimeException JavaDoc(e);
97     }
98   }
99
100   /**
101    * Evaluates the expression <code>expr</code> to produce an
102    * <code>Integer</code>.
103    *
104    * @param attrName the name of the attribute being evaluated
105    * @param expr the expression to evaluate
106    * @return evaluation result or <code>null</code> if <code>expr</code> is
107    * <code>null</code>
108    * @throws RuntimeException if failed to perform evaluation
109    */

110   public Integer JavaDoc toIntegerObject(String JavaDoc attrName, String JavaDoc expr) {
111     try {
112       return (expr == null) ? null :
113           (Integer JavaDoc) ExpressionEvaluatorManager.evaluate(
114           attrName, expr, Integer JavaDoc.class, this.tag, this.pageContext);
115     }
116     catch (JspException JavaDoc e) {
117       throw new RuntimeException JavaDoc(e);
118     }
119   }
120
121   /**
122    * Evaluates the expression <code>expr</code> to produce an
123    * <code>int</code>.
124    *
125    * @param attrName the name of the attribute being evaluated
126    * @param expr the expression to evaluate
127    * @return evaluation result
128    */

129   public int toIntegerValue(String JavaDoc attrName, String JavaDoc expr) {
130     return toIntegerObject(attrName, expr).intValue();
131   }
132
133   /**
134    * Evaluates the expression <code>expr</code> to produce a
135    * <code>Boolean</code>.
136    *
137    * @param attrName the name of the attribute being evaluated
138    * @param expr the expression to evaluate
139    * @return evaluation result or <code>null</code> if <code>expr</code> is
140    * <code>null</code>
141    * @throws RuntimeException if failed to perform evaluation
142    */

143   public Boolean JavaDoc toBooleanObject(String JavaDoc attrName, String JavaDoc expr) {
144     try {
145       return (expr == null) ? null :
146           (Boolean JavaDoc) ExpressionEvaluatorManager.evaluate(
147           attrName, expr, Boolean JavaDoc.class, this.tag, this.pageContext);
148     }
149     catch (JspException JavaDoc e) {
150       throw new RuntimeException JavaDoc(e);
151     }
152   }
153
154   /**
155    * Evaluates the expression <code>expr</code> to produce an
156    * <code>boolean</code>.
157    *
158    * @param attrName the name of the attribute being evaluated
159    * @param expr the expression to evaluate
160    * @return evaluation result
161    */

162   public boolean toBooleanValue(String JavaDoc attrName, String JavaDoc expr) {
163     return toBooleanObject(attrName, expr).booleanValue();
164   }
165
166   /**
167    * Evaluates the expression <code>expr</code> to produce a
168    * <code>Collection</code>.
169    *
170    * @param attrName the name of the attribute being evaluated
171    * @param expr the expression to evaluate
172    * @return evaluation result or <code>null</code> if <code>expr</code> is
173    * <code>null</code>
174    * @throws RuntimeException if failed to perform evaluation
175    */

176   public Collection JavaDoc toCollection(String JavaDoc attrName, String JavaDoc expr) {
177     try {
178       return (expr == null) ? null :
179           (Collection JavaDoc) ExpressionEvaluatorManager.evaluate(
180           attrName, expr, Collection JavaDoc.class, this.tag, this.pageContext);
181     }
182     catch (JspException JavaDoc e) {
183       throw new RuntimeException JavaDoc(e);
184     }
185   }
186 }
Popular Tags