View Javadoc

1   /**
2    *
3    * Copyright 2003-2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  //
19  // This source code implements specifications defined by the Java
20  // Community Process. In order to remain compliant with the specification
21  // DO NOT add / change / or delete method signatures!
22  //
23  
24  package javax.servlet.jsp.el;
25  
26  
27  /**
28   * <p>The abstract base class for an expression-language evaluator.
29   * Classes that implement an expression language expose their functionality
30   * via this abstract class.</p>
31   *
32   * <p>An instance of the ExpressionEvaluator can be obtained via the 
33   * JspContext / PageContext</p>
34   *
35   * <p>The parseExpression() and evaluate() methods must be thread-safe.  
36   * That is, multiple threads may call these methods on the same 
37   * ExpressionEvaluator object simultaneously.  Implementations should 
38   * synchronize access if they depend on transient state.  Implementations 
39   * should not, however, assume that only one object of each 
40   * ExpressionEvaluator type will be instantiated; global caching should 
41   * therefore be static.</p>
42   *
43   * <p>Only a single EL expression, starting with '${' and ending with
44   * '}', can be parsed or evaluated at a time.  EL expressions 
45   * cannot be mixed with static text.  For example, attempting to 
46   * parse or evaluate "<code>abc${1+1}def${1+1}ghi</code>" or even
47   * "<code>${1+1}${1+1}</code>" will cause an <code>ELException</code> to
48   * be thrown.</p>
49   *
50   * <p>The following are examples of syntactically legal EL expressions:
51   *
52   * <ul>
53   *   <li><code>${person.lastName}</code></li>
54   *   <li><code>${8 * 8}</code></li>
55   *   <li><code>${my:reverse('hello')}</code></li>
56   * </ul>
57   * </p>
58   *
59   * @since 2.0
60   */
61  public abstract class ExpressionEvaluator {
62  
63      /**
64       * Prepare an expression for later evaluation.  This method should perform
65       * syntactic validation of the expression; if in doing so it detects 
66       * errors, it should raise an ELParseException.
67       *
68       * @param expression The expression to be evaluated.
69       * @param expectedType The expected type of the result of the evaluation
70       * @param fMapper A FunctionMapper to resolve functions found in 
71       *     the expression.  It can be null, in which case no functions 
72       *     are supported for this invocation.  The ExpressionEvaluator 
73       *     must not hold on to the FunctionMapper reference after 
74       *     returning from <code>parseExpression()</code>.  The 
75       *     <code>Expression</code> object returned must invoke the same 
76       *     functions regardless of whether the mappings in the 
77       *     provided <code>FunctionMapper</code> instance change between 
78       *     calling <code>ExpressionEvaluator.parseExpression()</code>
79       *     and <code>Expression.evaluate()</code>.
80       * @return The Expression object encapsulating the arguments.
81       *
82       * @exception ELException Thrown if parsing errors were found.
83       */ 
84      public abstract Expression parseExpression( String expression, 
85  				       Class expectedType, 
86  				       FunctionMapper fMapper ) 
87        throws ELException; 
88  
89  
90      /** 
91       * Evaluates an expression.  This method may perform some syntactic 
92       * validation and, if so, it should raise an ELParseException error if 
93       * it encounters syntactic errors.  EL evaluation errors should cause 
94       * an ELException to be raised.
95       *
96       * @param expression The expression to be evaluated.
97       * @param expectedType The expected type of the result of the evaluation
98       * @param vResolver A VariableResolver instance that can be used at 
99       *     runtime to resolve the name of implicit objects into Objects.
100      * @param fMapper A FunctionMapper to resolve functions found in 
101      *     the expression.  It can be null, in which case no functions 
102      *     are supported for this invocation.  
103      * @return The result of the expression evaluation.
104      *
105      * @exception ELException Thrown if the expression evaluation failed.
106      */ 
107     public abstract Object evaluate( String expression, 
108 			    Class expectedType, 
109 			    VariableResolver vResolver,
110 			    FunctionMapper fMapper ) 
111       throws ELException; 
112 }
113