View Javadoc

1   /*
2    * $Id: EvalHelper.java 376782 2006-02-10 18:11:36Z husted $
3    *
4    * Copyright 1999-2004 The Apache Software Foundation.
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 org.apache.strutsel.taglib.utils;
19  
20  import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
21  
22  import javax.servlet.jsp.JspException;
23  import javax.servlet.jsp.PageContext;
24  import javax.servlet.jsp.tagext.Tag;
25  
26  /***
27   * This class is used in the <code>evaluateExpressions</code> method of each
28   * Tag class.  It is used to process the original attribute value through the
29   * JSTL EL engine to produce an evaluated value.  It provides functions to
30   * evaluate the expression assuming it is an Object, String, Integer, or
31   * Boolean result.
32   */
33  public final class EvalHelper {
34      private EvalHelper() {
35      }
36  
37      /***
38       * Evaluates the attribute value in the JSTL EL engine, returning the raw
39       * Object value of the evaluated expression.  If the original expression
40       * is null, or the resulting value is null, it will return null.
41       */
42      public static Object eval(String attrName, String attrValue, Tag tagObject,
43          PageContext pageContext)
44          throws JspException {
45          Object result = null;
46  
47          if (attrValue != null) {
48              result =
49                  ExpressionEvaluatorManager.evaluate(attrName, attrValue,
50                      Object.class, tagObject, pageContext);
51          }
52  
53          return (result);
54      }
55  
56      /***
57       * Evaluates the attribute value in the JSTL EL engine, assuming the
58       * resulting value is a String object.  If the original expression is
59       * null, or the resulting value is null, it will return null.
60       */
61      public static String evalString(String attrName, String attrValue,
62          Tag tagObject, PageContext pageContext)
63          throws JspException {
64          Object result = null;
65  
66          if (attrValue != null) {
67              result =
68                  ExpressionEvaluatorManager.evaluate(attrName, attrValue,
69                      String.class, tagObject, pageContext);
70          }
71  
72          return ((String) result);
73      }
74  
75      /***
76       * Evaluates the attribute value in the JSTL EL engine, assuming the
77       * resulting value is an Integer object.  If the original expression is
78       * null, or the resulting value is null, it will return null.
79       */
80      public static Integer evalInteger(String attrName, String attrValue,
81          Tag tagObject, PageContext pageContext)
82          throws JspException {
83          Object result = null;
84  
85          if (attrValue != null) {
86              result =
87                  ExpressionEvaluatorManager.evaluate(attrName, attrValue,
88                      Integer.class, tagObject, pageContext);
89          }
90  
91          return ((Integer) result);
92      }
93  
94      /***
95       * Evaluates the attribute value in the JSTL EL engine, assuming the
96       * resulting value is an Boolean object.  If the original expression is
97       * null, or the resulting value is null, it will return null.
98       */
99      public static Boolean evalBoolean(String attrName, String attrValue,
100         Tag tagObject, PageContext pageContext)
101         throws JspException {
102         Object result = null;
103 
104         if (attrValue != null) {
105             result =
106                 ExpressionEvaluatorManager.evaluate(attrName, attrValue,
107                     Boolean.class, tagObject, pageContext);
108         }
109 
110         return ((Boolean) result);
111     }
112 }