Polishing EL integration.
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package org.springframework.binding.expression.el;
|
||||
|
||||
import javax.el.ELContext;
|
||||
import javax.el.ELResolver;
|
||||
import javax.el.FunctionMapper;
|
||||
import javax.el.VariableMapper;
|
||||
|
||||
/**
|
||||
* A default {@link ELContextFactory} for facilitating use of EL for expression evaluation.
|
||||
* @author Jeremy Grelle
|
||||
*
|
||||
*/
|
||||
public class DefaultELContextFactory implements ELContextFactory {
|
||||
|
||||
/**
|
||||
* Configures and returns a simple EL context to use to parse EL expressions.
|
||||
* @return The configured simple ELContext instance.
|
||||
*/
|
||||
public ELContext getParseTimeELContext() {
|
||||
return new SimpleELContext();
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures and returns a simple EL context to use to evaluate EL expressions on the given base target object.
|
||||
* @return The configured simple ELContext instance.
|
||||
*/
|
||||
public ELContext getEvalTimeELContext(Object target) {
|
||||
return new SimpleELContext(target);
|
||||
}
|
||||
|
||||
private static class SimpleELContext extends ELContext {
|
||||
private DefaultELResolver resolver = new DefaultELResolver();
|
||||
|
||||
public SimpleELContext() {
|
||||
|
||||
}
|
||||
|
||||
public SimpleELContext(Object target) {
|
||||
this.resolver.setTarget(target);
|
||||
}
|
||||
|
||||
public ELResolver getELResolver() {
|
||||
return resolver;
|
||||
}
|
||||
|
||||
public FunctionMapper getFunctionMapper() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public VariableMapper getVariableMapper() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.springframework.binding.expression.el;
|
||||
|
||||
import javax.el.ELContext;
|
||||
import javax.el.ELResolver;
|
||||
|
||||
/**
|
||||
* A factory for creating a EL context object that will be used to evaluate a target object of an EL expression.
|
||||
@@ -10,11 +11,18 @@ import javax.el.ELContext;
|
||||
public interface ELContextFactory {
|
||||
|
||||
/**
|
||||
* Configures and returns a {@link DelegatingELContext} to be used in evaluating EL expressions on the given base
|
||||
* target object.
|
||||
*
|
||||
* @return DelegatingELContext The configured DelegatingELContext instance.
|
||||
* Configures and returns an {@link ELContext} to be used in parsing EL expressions.
|
||||
* @return ELContext The configured ELContext instance for parsing expressions.
|
||||
*/
|
||||
public ELContext getELContext(Object target);
|
||||
public ELContext getParseTimeELContext();
|
||||
|
||||
/**
|
||||
* Configures and returns an {@link ELContext} to be used in evaluating EL expressions on the given base target
|
||||
* object. In certain environments the target will be null and the base object of the expression is expected to be
|
||||
* resolved via the ELContext's {@link ELResolver} chain.
|
||||
* @param target The base object for the expression evaluation.
|
||||
* @return ELContext The configured ELContext instance for evaluating expressions.
|
||||
*/
|
||||
public ELContext getEvalTimeELContext(Object target);
|
||||
|
||||
}
|
||||
@@ -59,7 +59,7 @@ public class ELExpression implements SettableExpression {
|
||||
* @return {@link ELContext} The thread-bound {@link ELContext} instance.
|
||||
*/
|
||||
protected ELContext getELContext(Object target) {
|
||||
ELContext ctx = factory.getELContext(target);
|
||||
ELContext ctx = factory.getEvalTimeELContext(target);
|
||||
return ctx;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,25 +2,18 @@ package org.springframework.binding.expression.el;
|
||||
|
||||
import javax.el.ELContext;
|
||||
import javax.el.ELException;
|
||||
import javax.el.ELResolver;
|
||||
import javax.el.ExpressionFactory;
|
||||
import javax.el.FunctionMapper;
|
||||
import javax.el.VariableMapper;
|
||||
|
||||
import org.jboss.el.ExpressionFactoryImpl;
|
||||
import org.springframework.binding.expression.Expression;
|
||||
import org.springframework.binding.expression.ExpressionParser;
|
||||
import org.springframework.binding.expression.ParserException;
|
||||
import org.springframework.binding.expression.SettableExpression;
|
||||
|
||||
/**
|
||||
* An expression parser that parses EL expressions. Beyond standard EL expression parsing, it makes use of the ability
|
||||
* of the JBoss-EL implementation to parse dynamic method invocations such as foo.bar() (the EL spec currently only
|
||||
* provides out-of-the-box support for functions).
|
||||
*
|
||||
* An expression parser that parses EL expressions.
|
||||
* @author Jeremy Grelle
|
||||
*/
|
||||
public class JBossELExpressionParser implements ExpressionParser {
|
||||
public class ELExpressionParser implements ExpressionParser {
|
||||
|
||||
/**
|
||||
* The expression prefix for deferred EL expressions.
|
||||
@@ -50,12 +43,13 @@ public class JBossELExpressionParser implements ExpressionParser {
|
||||
/**
|
||||
* The ExpressionFactory for constructing EL expressions
|
||||
*/
|
||||
private ExpressionFactory factory = new ExpressionFactoryImpl();
|
||||
private ExpressionFactory expressionFactory;
|
||||
|
||||
/**
|
||||
* Creates a new EL expression parser for standalone usage.
|
||||
*/
|
||||
public JBossELExpressionParser() {
|
||||
public ELExpressionParser(ExpressionFactory expressionFactory) {
|
||||
this.expressionFactory = expressionFactory;
|
||||
this.contextFactory = new DefaultELContextFactory();
|
||||
}
|
||||
|
||||
@@ -64,7 +58,8 @@ public class JBossELExpressionParser implements ExpressionParser {
|
||||
*
|
||||
* @param contextFactory the context factory
|
||||
*/
|
||||
public JBossELExpressionParser(ELContextFactory contextFactory) {
|
||||
public ELExpressionParser(ExpressionFactory expressionFactory, ELContextFactory contextFactory) {
|
||||
this.expressionFactory = expressionFactory;
|
||||
this.contextFactory = contextFactory;
|
||||
}
|
||||
|
||||
@@ -118,43 +113,12 @@ public class JBossELExpressionParser implements ExpressionParser {
|
||||
* @throws ParserException
|
||||
*/
|
||||
protected SettableExpression doParseSettableExpression(String expressionString) throws ParserException {
|
||||
ELContext ctx = contextFactory.getELContext(null);
|
||||
ELContext ctx = contextFactory.getParseTimeELContext();
|
||||
try {
|
||||
return new ELExpression(contextFactory, factory.createValueExpression(ctx, expressionString, Object.class));
|
||||
return new ELExpression(contextFactory, expressionFactory.createValueExpression(ctx, expressionString,
|
||||
Object.class));
|
||||
} catch (ELException ex) {
|
||||
throw new ParserException(expressionString, ex);
|
||||
}
|
||||
}
|
||||
|
||||
static class DefaultELContextFactory implements ELContextFactory {
|
||||
|
||||
/**
|
||||
* Configures and returns a simple EL context to use to evaluate EL expressions on the given base target object.
|
||||
* @return The configured simple ELContext instance.
|
||||
*/
|
||||
public ELContext getELContext(Object target) {
|
||||
return new SimpleELContext(target);
|
||||
}
|
||||
|
||||
private static class SimpleELContext extends ELContext {
|
||||
private DefaultELResolver resolver;
|
||||
|
||||
public SimpleELContext(Object target) {
|
||||
this.resolver = new DefaultELResolver();
|
||||
this.resolver.setTarget(target);
|
||||
}
|
||||
|
||||
public ELResolver getELResolver() {
|
||||
return resolver;
|
||||
}
|
||||
|
||||
public FunctionMapper getFunctionMapper() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public VariableMapper getVariableMapper() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user