1 /**
2 * Licensed under the Artistic License; you may not use this file
3 * except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://displaytag.sourceforge.net/license.html
7 *
8 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
9 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11 */
12 package org.displaytag.tags.el;
13
14 import javax.servlet.jsp.JspException;
15 import javax.servlet.jsp.PageContext;
16 import javax.servlet.jsp.tagext.Tag;
17
18 import org.apache.commons.lang.BooleanUtils;
19 import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
20
21
22 /**
23 * Utility class to help with the evaluation of JSTL Expression Language. It mainly encapsulates the calls to
24 * ExpressionEvaluationManager to ease the use of this class.
25 * @author Tim McCune
26 * @author Fabrizio Giustina
27 * @version $Revision: 1081 $ ($Author: fgiust $)
28 */
29 public class ExpressionEvaluator
30 {
31
32 /**
33 * page context.
34 */
35 private PageContext pageContext;
36
37 /**
38 * tag.
39 */
40 private Tag tag;
41
42 /**
43 * Creates a new ExpressionEvaluator for the given tag and pagecontext.
44 * @param sourceTag tag where the expression has to be evaluated
45 * @param context the page context
46 */
47 public ExpressionEvaluator(Tag sourceTag, PageContext context)
48 {
49 this.tag = sourceTag;
50 this.pageContext = context;
51 }
52
53 /**
54 * Evaluate expression in attrValue.
55 * @param attrName attribute name
56 * @param attrValue attribute value
57 * @return evaluate expression of attrValue, null if attrValue is null.
58 * @param returnClass class the returned object is instance of
59 * @throws JspException exception thrown by ExpressionEvaluatorManager
60 */
61 public Object eval(String attrName, String attrValue, Class returnClass) throws JspException
62 {
63 return attrValue != null ? ExpressionEvaluatorManager.evaluate(
64 attrName,
65 attrValue,
66 returnClass,
67 this.tag,
68 this.pageContext) : null;
69 }
70
71 /**
72 * Evaluate expression in attrValueas as a String.
73 * @param attrName attribute name
74 * @param attrValue attribute value
75 * @return evaluate expression of attrValue, null if attrValue is null.
76 * @throws JspException exception thrown by ExpressionEvaluatorManager
77 */
78 public String evalString(String attrName, String attrValue) throws JspException
79 {
80 return (String) eval(attrName, attrValue, String.class);
81 }
82
83 /**
84 * Evaluate expression in attrValueas as a boolean.
85 * @param attrName attribute name
86 * @param attrValue attribute value
87 * @return evaluate expression of attrValue, false if attrValue is null.
88 * @throws JspException exception thrown by ExpressionEvaluatorManager
89 */
90 public boolean evalBoolean(String attrName, String attrValue) throws JspException
91 {
92 return BooleanUtils.toBoolean((Boolean) eval(attrName, attrValue, Boolean.class));
93 }
94
95 /**
96 * Evaluate expression in attrValueas as a long.
97 * @param attrName attribute name
98 * @param attrValue attribute value
99 * @return evaluate expression of attrValue, -1 if attrValue is null.
100 * @throws JspException exception thrown by ExpressionEvaluatorManager
101 */
102 public long evalLong(String attrName, String attrValue) throws JspException
103 {
104 Long rtn = (Long) eval(attrName, attrValue, Long.class);
105 if (rtn != null)
106 {
107 return rtn.longValue();
108 }
109
110 return -1L;
111 }
112
113 /**
114 * Evaluate expression in attrValueas as a int.
115 * @param attrName attribute name
116 * @param attrValue attribute value
117 * @return evaluate expression of attrValue, -1 if attrValue is null.
118 * @throws JspException exception thrown by ExpressionEvaluatorManager
119 */
120 public int evalInt(String attrName, String attrValue) throws JspException
121 {
122 Integer rtn = (Integer) eval(attrName, attrValue, Integer.class);
123 if (rtn != null)
124 {
125 return rtn.intValue();
126 }
127
128 return -1;
129 }
130 }