001: /*
002: * $Id: EvalHelper.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.strutsel.taglib.utils;
022:
023: import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
024:
025: import javax.servlet.jsp.JspException;
026: import javax.servlet.jsp.PageContext;
027: import javax.servlet.jsp.tagext.Tag;
028:
029: /**
030: * This class is used in the <code>evaluateExpressions</code> method of each
031: * Tag class. It is used to process the original attribute value through the
032: * JSTL EL engine to produce an evaluated value. It provides functions to
033: * evaluate the expression assuming it is an Object, String, Integer, or
034: * Boolean result.
035: */
036: public final class EvalHelper {
037: private EvalHelper() {
038: }
039:
040: /**
041: * Evaluates the attribute value in the JSTL EL engine, returning the raw
042: * Object value of the evaluated expression. If the original expression
043: * is null, or the resulting value is null, it will return null.
044: */
045: public static Object eval(String attrName, String attrValue,
046: Tag tagObject, PageContext pageContext) throws JspException {
047: Object result = null;
048:
049: if (attrValue != null) {
050: result = ExpressionEvaluatorManager.evaluate(attrName,
051: attrValue, Object.class, tagObject, pageContext);
052: }
053:
054: return (result);
055: }
056:
057: /**
058: * Evaluates the attribute value in the JSTL EL engine, assuming the
059: * resulting value is a String object. If the original expression is
060: * null, or the resulting value is null, it will return null.
061: */
062: public static String evalString(String attrName, String attrValue,
063: Tag tagObject, PageContext pageContext) throws JspException {
064: Object result = null;
065:
066: if (attrValue != null) {
067: result = ExpressionEvaluatorManager.evaluate(attrName,
068: attrValue, String.class, tagObject, pageContext);
069: }
070:
071: return ((String) result);
072: }
073:
074: /**
075: * Evaluates the attribute value in the JSTL EL engine, assuming the
076: * resulting value is an Integer object. If the original expression is
077: * null, or the resulting value is null, it will return null.
078: */
079: public static Integer evalInteger(String attrName,
080: String attrValue, Tag tagObject, PageContext pageContext)
081: throws JspException {
082: Object result = null;
083:
084: if (attrValue != null) {
085: result = ExpressionEvaluatorManager.evaluate(attrName,
086: attrValue, Integer.class, tagObject, pageContext);
087: }
088:
089: return ((Integer) result);
090: }
091:
092: /**
093: * Evaluates the attribute value in the JSTL EL engine, assuming the
094: * resulting value is an Boolean object. If the original expression is
095: * null, or the resulting value is null, it will return null.
096: */
097: public static Boolean evalBoolean(String attrName,
098: String attrValue, Tag tagObject, PageContext pageContext)
099: throws JspException {
100: Object result = null;
101:
102: if (attrValue != null) {
103: result = ExpressionEvaluatorManager.evaluate(attrName,
104: attrValue, Boolean.class, tagObject, pageContext);
105: }
106:
107: return ((Boolean) result);
108: }
109: }
|