KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > lang > jstl > Evaluator


1 /*
2  * Copyright 1999-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
17 package org.apache.taglibs.standard.lang.jstl;
18
19 import java.text.MessageFormat JavaDoc;
20 import java.util.Map 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.ExpressionEvaluator;
27
28 /**
29  *
30  * <p>This is the expression evaluator "adapter" that customizes it
31  * for use with the JSP Standard Tag Library. It uses a
32  * VariableResolver implementation that looks up variables from the
33  * PageContext and also implements its implicit objects. It also
34  * wraps ELExceptions in JspExceptions that describe the attribute
35  * name and value causing the error.
36  *
37  * @author Nathan Abramson - Art Technology Group
38  * @author Shawn Bayern
39  * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: pierred $
40  **/

41
42 public class Evaluator
43   implements ExpressionEvaluator
44 {
45   //-------------------------------------
46
// Properties
47
//-------------------------------------
48

49   //-------------------------------------
50
// Member variables
51
//-------------------------------------
52

53   /** The singleton instance of the evaluator **/
54   static ELEvaluator sEvaluator =
55     new ELEvaluator
56     (new JSTLVariableResolver ());
57
58   //-------------------------------------
59
// ExpressionEvaluator methods
60
//-------------------------------------
61
/**
62    *
63    * Translation time validation of an attribute value. This method
64    * will return a null String if the attribute value is valid;
65    * otherwise an error message.
66    **/

67   public String JavaDoc validate (String JavaDoc pAttributeName,
68               String JavaDoc pAttributeValue)
69   {
70     try {
71       sEvaluator.parseExpressionString (pAttributeValue);
72       return null;
73     }
74     catch (ELException exc) {
75       return
76     MessageFormat.format
77     (Constants.ATTRIBUTE_PARSE_EXCEPTION,
78      new Object JavaDoc [] {
79        "" + pAttributeName,
80        "" + pAttributeValue,
81        exc.getMessage ()
82      });
83     }
84   }
85
86   //-------------------------------------
87
/**
88    *
89    * Evaluates the expression at request time
90    **/

91   public Object JavaDoc evaluate (String JavaDoc pAttributeName,
92               String JavaDoc pAttributeValue,
93               Class JavaDoc pExpectedType,
94               Tag JavaDoc pTag,
95               PageContext JavaDoc pPageContext,
96               Map JavaDoc functions,
97               String JavaDoc defaultPrefix)
98     throws JspException JavaDoc
99   {
100     try {
101       return sEvaluator.evaluate
102     (pAttributeValue,
103      pPageContext,
104      pExpectedType,
105      functions,
106      defaultPrefix);
107     }
108     catch (ELException exc) {
109       throw new JspException JavaDoc
110     (MessageFormat.format
111      (Constants.ATTRIBUTE_EVALUATION_EXCEPTION,
112       new Object JavaDoc [] {
113         "" + pAttributeName,
114         "" + pAttributeValue,
115         exc.getMessage(),
116         exc.getRootCause()
117       }), exc.getRootCause());
118     }
119   }
120
121   /** Conduit to old-style call for convenience. */
122   public Object JavaDoc evaluate (String JavaDoc pAttributeName,
123               String JavaDoc pAttributeValue,
124               Class JavaDoc pExpectedType,
125               Tag JavaDoc pTag,
126               PageContext JavaDoc pPageContext)
127     throws JspException JavaDoc
128   {
129     return evaluate(pAttributeName,
130            pAttributeValue,
131            pExpectedType,
132            pTag,
133            pPageContext,
134            null,
135            null);
136   }
137
138
139   //-------------------------------------
140
// Testing methods
141
//-------------------------------------
142
/**
143    *
144    * Parses the given attribute value, then converts it back to a
145    * String in its canonical form.
146    **/

147   public static String JavaDoc parseAndRender (String JavaDoc pAttributeValue)
148     throws JspException JavaDoc
149   {
150     try {
151       return sEvaluator.parseAndRender (pAttributeValue);
152     }
153     catch (ELException exc) {
154       throw new JspException JavaDoc
155     (MessageFormat.format
156      (Constants.ATTRIBUTE_PARSE_EXCEPTION,
157       new Object JavaDoc [] {
158         "test",
159         "" + pAttributeValue,
160         exc.getMessage ()
161       }));
162     }
163   }
164
165   //-------------------------------------
166

167 }
168
Popular Tags