001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * glassfish/bootstrap/legal/CDDLv1.0.txt or
009: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
010: * See the License for the specific language governing
011: * permissions and limitations under the License.
012: *
013: * When distributing Covered Code, include this CDDL
014: * HEADER in each file and include the License file at
015: * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
016: * add the following below this CDDL HEADER, with the
017: * fields enclosed by brackets "[]" replaced with your
018: * own identifying information: Portions Copyright [yyyy]
019: * [name of copyright owner]
020: *
021: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
022: */
023:
024: package javax.el;
025:
026: /**
027: * Parses a <code>String</code> into a {@link ValueExpression} or
028: * {@link MethodExpression} instance for later evaluation.
029: *
030: * <p>Classes that implement the EL expression language expose their
031: * functionality via this abstract class.
032: * The {@link #newInstance} method can be used to obtain an
033: * instance of the implementation.
034: * Technologies such as
035: * JavaServer Pages and JavaServer Faces provide access to an
036: * implementation via factory methods.</p>
037: *
038: * <p>The {@link #createValueExpression} method is used to parse expressions
039: * that evaluate to values (both l-values and r-values are supported).
040: * The {@link #createMethodExpression} method is used to parse expressions
041: * that evaluate to a reference to a method on an object.</p>
042: *
043: * <p>Unlike previous incarnations of this API, there is no way to parse
044: * and evaluate an expression in one single step. The expression needs to first
045: * be parsed, and then evaluated.</p>
046: *
047: * <p>Resolution of model objects is performed at evaluation time, via the
048: * {@link ELResolver} associated with the {@link ELContext} passed to
049: * the <code>ValueExpression</code> or <code>MethodExpression</code>.</p>
050: *
051: * <p>The ELContext object also provides access to the {@link FunctionMapper}
052: * and {@link VariableMapper} to be used when parsing the expression.
053: * EL function and variable mapping is performed at parse-time, and
054: * the results are
055: * bound to the expression. Therefore, the {@link ELContext},
056: * {@link FunctionMapper},
057: * and {@link VariableMapper}
058: * are not stored for future use and do not have to be
059: * <code>Serializable</code>.</p>
060: *
061: * <p>The <code>createValueExpression</code> and
062: * <code>createMethodExpression</code> methods must be thread-safe. That is,
063: * multiple threads may call these methods on the same
064: * <code>ExpressionFactory</code> object simultaneously. Implementations
065: * should synchronize access if they depend on transient state.
066: * Implementations should not, however, assume that only one object of
067: * each <code>ExpressionFactory</code> type will be instantiated; global
068: * caching should therefore be static.</p>
069: *
070: * <p>The <code>ExpressionFactory</code> must be able to handle the following
071: * types of input for the <code>expression</code> parameter:
072: * <ul>
073: * <li>Single expressions using the <code>${}</code> delimiter
074: * (e.g. <code>"${employee.lastName}"</code>).</li>
075: * <li>Single expressions using the <code>#{}</code> delimiter
076: * (e.g. <code>"#{employee.lastName}"</code>).</li>
077: * <li>Literal text containing no <code>${}</code> or <code>#{}</code>
078: * delimiters (e.g. <code>"John Doe"</code>).</li>
079: * <li>Multiple expressions using the same delimiter (e.g.
080: * <code>"${employee.firstName}${employee.lastName}"</code> or
081: * <code>"#{employee.firstName}#{employee.lastName}"</code>).</li>
082: * <li>Mixed literal text and expressions using the same delimiter (e.g.
083: * <code>"Name: ${employee.firstName} ${employee.lastName}"</code>).</li>
084: * </ul></p>
085: *
086: * <p>The following types of input are illegal and must cause an
087: * {@link ELException} to be thrown:
088: * <ul>
089: * <li>Multiple expressions using different delimiters (e.g.
090: * <code>"${employee.firstName}#{employee.lastName}"</code>).</li>
091: * <li>Mixed literal text and expressions using different delimiters(e.g.
092: * <code>"Name: ${employee.firstName} #{employee.lastName}"</code>).</li>
093: * </ul></p>
094: *
095: * @since JSP 2.1
096: */
097:
098: import java.util.Properties;
099:
100: public abstract class ExpressionFactory {
101:
102: /**
103: * Creates a new instance of a <code>ExpressionFactory</code>.
104: * This method uses the following ordered lookup procedure to determine
105: * the <code>ExpressionFactory</code> implementation class to load:
106: * <ul>
107: * <li>Use the Services API (as detailed in the JAR specification).
108: * If a resource with the name of
109: * <code>META-INF/services/javax.el.ExpressionFactory</code> exists,
110: * then its first line, if present, is used as the UTF-8 encoded name of
111: * the implementation class. </li>
112: * <li>Use the properties file "lib/el.properties" in the JRE directory.
113: * If this file exists and it is readable by the
114: * <code> java.util.Properties.load(InputStream)</code> method,
115: * and it contains an entry whose key is "javax.el.ExpressionFactory",
116: * then the value of that entry is used as the name of the
117: * implementation class.</li>
118: * <li>Use the <code>javax.el.ExpressionFactory</code> system property.
119: * If a system property with this name is defined, then its value is
120: * used as the name of the implementation class.</li>
121: * <li>Use a platform default implementation.</li>
122: * </ul>
123: */
124: public static ExpressionFactory newInstance() {
125: return ExpressionFactory.newInstance(null);
126: }
127:
128: /**
129: * <p>Create a new instance of a <code>ExpressionFactory</code>, with
130: * optional properties.
131: * This method uses the same lookup procedure as the one used in
132: * <code>newInstance()</code>.
133: * </p>
134: * <p>
135: * If the argument <code>properties</code> is not null, and if the
136: * implementation contains a constructor with a single parameter of
137: * type <code>java.util.Properties</code>, then the constructor is used
138: * to create the instance.
139: * </p>
140: * <p>
141: * Properties are optional and can be ignored by an implementation.
142: * </p>
143: * <p>The name of a property should start with "javax.el."</p>
144: * <p>
145: * The following are some suggested names for properties.
146: * <ul>
147: * <li>javax.el.cacheSize</li>
148: * </ul></p>
149: *
150: * @param properties Properties passed to the implementation.
151: * If null, then no properties.
152: */
153: public static ExpressionFactory newInstance(Properties properties) {
154: return (ExpressionFactory) FactoryFinder.find(
155: "javax.el.ExpressionFactory",
156: "com.sun.el.ExpressionFactoryImpl", properties);
157: }
158:
159: /**
160: * Parses an expression into a {@link ValueExpression} for later
161: * evaluation. Use this method for expressions that refer to values.
162: *
163: * <p>This method should perform syntactic validation of the expression.
164: * If in doing so it detects errors, it should raise an
165: * <code>ELException</code>.</p>
166: *
167: * @param context The EL context used to parse the expression.
168: * The <code>FunctionMapper</code> and <code>VariableMapper</code>
169: * stored in the ELContext
170: * are used to resolve functions and variables found in
171: * the expression. They can be <code>null</code>, in which case
172: * functions or variables are not supported for this expression.
173: * The object
174: * returned must invoke the same functions and access the same
175: * variable mappings
176: * regardless of whether
177: * the mappings in the provided <code>FunctionMapper</code>
178: * and <code>VariableMapper</code> instances
179: * change between calling
180: * <code>ExpressionFactory.createValueExpression()</code> and any
181: * method on <code>ValueExpression</code>.
182: * <p>
183: * Note that within the EL, the ${} and #{} syntaxes are treated identically.
184: * This includes the use of VariableMapper and FunctionMapper at expression creation
185: * time. Each is invoked if not null, independent
186: * of whether the #{} or ${} syntax is used for the expression.</p>
187: * @param expression The expression to parse
188: * @param expectedType The type the result of the expression
189: * will be coerced to after evaluation.
190: * @return The parsed expression
191: * @throws NullPointerException Thrown if expectedType is null.
192: * @throws ELException Thrown if there are syntactical errors in the
193: * provided expression.
194: */
195: public abstract ValueExpression createValueExpression(
196: ELContext context, String expression, Class<?> expectedType);
197:
198: /**
199: * Creates a ValueExpression that wraps an object instance. This
200: * method can be used to pass any object as a ValueExpression. The
201: * wrapper ValueExpression is read only, and returns the wrapped
202: * object via its <code>getValue()</code> method, optionally coerced.
203: *
204: * @param instance The object instance to be wrapped.
205: * @param expectedType The type the result of the expression
206: * will be coerced to after evaluation. There will be no
207: * coercion if it is Object.class,
208: * @throws NullPointerException Thrown if expectedType is null.
209: */
210: public abstract ValueExpression createValueExpression(
211: Object instance, Class<?> expectedType);
212:
213: /**
214: * Parses an expression into a {@link MethodExpression} for later
215: * evaluation. Use this method for expressions that refer to methods.
216: *
217: * <p>
218: * If the expression is a String literal, a <code>MethodExpression
219: * </code> is created, which when invoked, returns the String literal,
220: * coerced to expectedReturnType. An ELException is thrown if
221: * expectedReturnType is void or if the coercion of the String literal
222: * to the expectedReturnType yields an error (see Section "1.16 Type
223: * Conversion").
224: * </p>
225: * <p>This method should perform syntactic validation of the expression.
226: * If in doing so it detects errors, it should raise an
227: * <code>ELException</code>.</p>
228: *
229: * @param context The EL context used to parse the expression.
230: * The <code>FunctionMapper</code> and <code>VariableMapper</code>
231: * stored in the ELContext
232: * are used to resolve functions and variables found in
233: * the expression. They can be <code>null</code>, in which
234: * case functions or variables are not supported for this expression.
235: * The object
236: * returned must invoke the same functions and access the same variable
237: * mappings
238: * regardless of whether
239: * the mappings in the provided <code>FunctionMapper</code>
240: * and <code>VariableMapper</code> instances
241: * change between calling
242: * <code>ExpressionFactory.createMethodExpression()</code> and any
243: * method on <code>MethodExpression</code>.
244: * <p>
245: * Note that within the EL, the ${} and #{} syntaxes are treated identically.
246: * This includes the use of VariableMapper and FunctionMapper at expression creation
247: * time. Each is invoked if not null, independent
248: * of whether the #{} or ${} syntax is used for the expression.</p>
249: *
250: * @param expression The expression to parse
251: * @param expectedReturnType The expected return type for the method
252: * to be found. After evaluating the expression, the
253: * <code>MethodExpression</code> must check that the return type of
254: * the actual method matches this type. Passing in a value of
255: * <code>null</code> indicates the caller does not care what the
256: * return type is, and the check is disabled.
257: * @param expectedParamTypes The expected parameter types for the method to
258: * be found. Must be an array with no elements if there are
259: * no parameters expected. It is illegal to pass <code>null</code>.
260: * @return The parsed expression
261: * @throws ELException Thrown if there are syntactical errors in the
262: * provided expression.
263: * @throws NullPointerException if paramTypes is <code>null</code>.
264: */
265: public abstract MethodExpression createMethodExpression(
266: ELContext context, String expression,
267: Class<?> expectedReturnType, Class<?>[] expectedParamTypes);
268:
269: /**
270: * Coerces an object to a specific type according to the
271: * EL type conversion rules.
272: *
273: * <p>An <code>ELException</code> is thrown if an error results from
274: * applying the conversion rules.
275: * </p>
276: *
277: * @param obj The object to coerce.
278: * @param targetType The target type for the coercion.
279: * @throws ELException thrown if an error results from applying the
280: * conversion rules.
281: */
282: public abstract Object coerceToType(Object obj, Class<?> targetType);
283:
284: }
|