KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > ExpressionHandling


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

18 package org.apache.beehive.netui.tags;
19
20 import org.apache.beehive.netui.script.ExpressionEvaluationException;
21 import org.apache.beehive.netui.script.ExpressionEvaluator;
22 import org.apache.beehive.netui.script.ExpressionEvaluatorFactory;
23 import org.apache.beehive.netui.script.ExpressionUpdateException;
24 import org.apache.beehive.netui.script.common.ImplicitObjectUtil;
25 import org.apache.beehive.netui.util.Bundle;
26 import org.apache.beehive.netui.util.logging.Logger;
27
28 import javax.servlet.jsp.JspException JavaDoc;
29 import javax.servlet.jsp.PageContext JavaDoc;
30 import javax.servlet.jsp.el.VariableResolver JavaDoc;
31
32 public class ExpressionHandling
33 {
34     private static final Logger logger = Logger.getInstance(ExpressionHandling.class);
35
36     private ExpressionEvaluator ee = null; // cache the expression evaluator
37
private INetuiTag _tag;
38
39     public ExpressionHandling(INetuiTag tag)
40     {
41         _tag = tag;
42     }
43
44     /**
45      * An internal method that is used for evaluating the <code>dataSource</code>
46      * attribute. The <code>dataSource</code> attribute is handled specially
47      * becuase it must be of a particular format in order to be usable by
48      * NetUI tags. This requirement exists in order to facilitate
49      * round-tripping the <code>dataSource</code> attribute as the
50      * <code>name</code> attribute of HTML tags. Upon a POST, the <code>name</code>
51      * attribute is used as an l-value for an update expression in order
52      * to push any POST-ed data back into the bean from whence it came.
53      */

54     /**
55      * Ensure that the passed in data source is a valid expression.
56      * @param dataSource
57      * @param attrName
58      * @param errorId
59      * @return String
60      * @throws javax.servlet.jsp.JspException
61      */

62     public String JavaDoc ensureValidExpression(String JavaDoc dataSource, String JavaDoc attrName, String JavaDoc errorId)
63             throws JspException JavaDoc
64     {
65         try {
66             boolean isExpr = isExpression(dataSource);
67
68             // @perf: if the isExpr call fails, this is an error condition, and the containsExpression
69
// call cost is irrelevant
70
if (!isExpr && containsExpression(dataSource)) {
71                 String JavaDoc s = Bundle.getString(errorId, new Object JavaDoc[]{dataSource});
72                 _tag.registerTagError(s, null);
73                 return null;
74             }
75
76             if (!isExpr) {
77                 String JavaDoc s = Bundle.getString(errorId, new Object JavaDoc[]{dataSource});
78                 _tag.registerTagError(s, null);
79                 return null;
80             }
81         }
82         catch (Exception JavaDoc e) {
83             // pass throw JspExceptions
84
if (e instanceof JspException JavaDoc)
85                 throw (JspException JavaDoc) e;
86
87             String JavaDoc s = Bundle.getString(errorId, new Object JavaDoc[]{dataSource});
88             _tag.registerTagError(s, e);
89             return null;
90         }
91         return dataSource;
92     }
93
94     /**
95      * @param expression
96      * @param attrName
97      * @return Object
98      * @throws JspException
99      */

100     public Object JavaDoc evaluateExpression(String JavaDoc expression, String JavaDoc attrName, PageContext JavaDoc ctxt)
101             throws JspException JavaDoc
102     {
103         return evaluateExpressionInternal(expression, attrName, ctxt);
104     }
105
106     /**
107      * This method will update the object identified by the <code>expr</code> parameter with
108      * the value. If the
109      * @param expr
110      * @param value
111      * @throws org.apache.beehive.netui.script.ExpressionUpdateException
112      *
113      */

114     public void updateExpression(String JavaDoc expr, Object JavaDoc value, PageContext JavaDoc pageContext)
115             throws ExpressionUpdateException, JspException JavaDoc
116     {
117         if (isExpression(expr)) {
118
119             VariableResolver JavaDoc vr = ImplicitObjectUtil.getUpdateVariableResolver(pageContext.getRequest(), pageContext.getResponse(), false);
120             ExpressionEvaluatorFactory.getInstance().update(expr, value, vr, false);
121         }
122         else {
123             String JavaDoc s = Bundle.getString("Tags_BindingUpdateExpressionError", new Object JavaDoc[]{expr});
124             _tag.registerTagError(s, null);
125         }
126     }
127
128     /**
129      * Return a boolean indicating if the string contains an expression or not.
130      * @param expression a <code>String</code> that may or may not contain an expresion.
131      * @return <code>true</code> if the string contains an expression.
132      */

133     private boolean containsExpression(String JavaDoc expression)
134     {
135         // this shouldn't happen because we have checked in isExpression that the expression isn't null
136
assert (expression != null) : "The parameter expression must not be null.";
137         return getExpressionEvaluator().containsExpression(expression);
138     }
139
140     /**
141      */

142     private boolean isExpression(String JavaDoc expression)
143     {
144         if (expression == null)
145             return false;
146         return getExpressionEvaluator().isExpression(expression);
147     }
148
149     /**
150      * This is the real implementation of evaluateExpression.
151      * @param expression
152      * @param attrName
153      * @return
154      * @throws JspException
155      */

156     private Object JavaDoc evaluateExpressionInternal(String JavaDoc expression, String JavaDoc attrName, PageContext JavaDoc pageContext)
157             throws JspException JavaDoc
158     {
159         if (logger.isDebugEnabled()) logger.debug("evaluate expression=\"" + expression + "\"");
160
161         Object JavaDoc result = null;
162
163         try {
164             result = getExpressionEvaluator().evaluateStrict(expression, pageContext.getVariableResolver());
165         }
166         catch (ExpressionEvaluationException ee) {
167             // if there is an expression evaluation error set the error and
168
// return null;
169

170             if (logger.isWarnEnabled())
171                 logger.warn(Bundle.getString("Tags_ExpressionEvaluationFailure", expression));
172
173             // create the expression info an add it to the error tracking
174
EvalErrorInfo info = new EvalErrorInfo();
175             info.evalExcp = ee;
176             info.expression = expression;
177             info.attr = attrName;
178             info.tagType = _tag.getTagName();
179
180             // report the error
181
_tag.registerTagError(info);
182             return null;
183         }
184         catch (Exception JavaDoc e) {
185             String JavaDoc s = Bundle.getString("Tags_ExpressionEvaluationException", new Object JavaDoc[]{expression, e.toString()});
186             _tag.registerTagError(s, e);
187             return null;
188         }
189
190         if (logger.isDebugEnabled()) logger.debug("resulting object: " + result);
191         return result;
192     }
193
194     /**
195      * Return a cached instance of an <code>ExpressionEvaluator</code>. This will be cached by the
196      * tag and release during <code>localRelease</code>.
197      * @return the <code>ExpressionEvalutor</code> for tis tag.
198      */

199     private final ExpressionEvaluator getExpressionEvaluator()
200     {
201         if (ee == null)
202             ee = ExpressionEvaluatorFactory.getInstance();
203
204         assert(ee != null);
205         return ee;
206     }
207 }
208
Popular Tags