polish
This commit is contained in:
@@ -1,21 +0,0 @@
|
||||
package org.springframework.binding.expression.el;
|
||||
|
||||
import javax.el.ELContext;
|
||||
|
||||
public class DefaultELContextFactory implements 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.
|
||||
*/
|
||||
public ELContext getELContext(Object target) {
|
||||
DelegatingELContext context = new DelegatingELContext();
|
||||
DefaultELResolver baseResolver = new DefaultELResolver();
|
||||
baseResolver.setTarget(target);
|
||||
context.setELResolver(baseResolver);
|
||||
return context;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,7 +21,6 @@ import org.springframework.util.Assert;
|
||||
* Note - Requires Java 5 or higher due to the use of generics in the API's basic resolvers.
|
||||
*
|
||||
* @author Jeremy Grelle
|
||||
*
|
||||
*/
|
||||
public class DefaultELResolver extends CompositeELResolver {
|
||||
|
||||
@@ -39,16 +38,18 @@ public class DefaultELResolver extends CompositeELResolver {
|
||||
Assert.notNull(target, "The DefaultELResolver must have a target base property set.");
|
||||
if (base == null) {
|
||||
return super.getValue(context, target, property);
|
||||
} else {
|
||||
return super.getValue(context, adaptIfNecessary(base), property);
|
||||
}
|
||||
return super.getValue(context, adaptIfNecessary(base), property);
|
||||
}
|
||||
|
||||
public void setValue(ELContext context, Object base, Object property, Object val) {
|
||||
Assert.notNull(target, "The DefaultELResolver must have a target base property set.");
|
||||
if (base == null)
|
||||
if (base == null) {
|
||||
super.setValue(context, target, property, val);
|
||||
else
|
||||
} else {
|
||||
super.setValue(context, adaptIfNecessary(base), property, val);
|
||||
}
|
||||
}
|
||||
|
||||
public Object getTarget() {
|
||||
@@ -60,9 +61,11 @@ public class DefaultELResolver extends CompositeELResolver {
|
||||
}
|
||||
|
||||
private Object adaptIfNecessary(Object base) {
|
||||
if (base instanceof MapAdaptable)
|
||||
if (base instanceof MapAdaptable) {
|
||||
return ((MapAdaptable) base).asMap();
|
||||
return base;
|
||||
} else {
|
||||
return base;
|
||||
}
|
||||
}
|
||||
|
||||
private void configureResolvers() {
|
||||
|
||||
@@ -1,96 +0,0 @@
|
||||
package org.springframework.binding.expression.el;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import javax.el.CompositeELResolver;
|
||||
import javax.el.ELContext;
|
||||
import javax.el.ELResolver;
|
||||
import javax.el.FunctionMapper;
|
||||
import javax.el.VariableMapper;
|
||||
|
||||
/**
|
||||
* An {@link ELContext} implementation that is meant to aggregate the {@link ELResolver}s of pre-existing
|
||||
* {@link ELContext}s. Can also be used standalone if no other {@link ELContext} exists in the current environment.
|
||||
*
|
||||
* Note - Using this context standalone requires Java 5 or higher.
|
||||
*
|
||||
* @author Jeremy Grelle
|
||||
*
|
||||
*/
|
||||
public class DelegatingELContext extends ELContext {
|
||||
|
||||
private ELResolver resolver;
|
||||
|
||||
private FunctionMapper functionMapper;
|
||||
|
||||
private VariableMapper variableMapper;
|
||||
|
||||
private List delegates = new ArrayList();
|
||||
|
||||
public ELResolver getELResolver() {
|
||||
return resolver;
|
||||
}
|
||||
|
||||
public void setELResolver(ELResolver resolver) {
|
||||
this.resolver = resolver;
|
||||
}
|
||||
|
||||
public FunctionMapper getFunctionMapper() {
|
||||
return functionMapper;
|
||||
}
|
||||
|
||||
public void setFunctionMapper(FunctionMapper functionMapper) {
|
||||
this.functionMapper = functionMapper;
|
||||
}
|
||||
|
||||
public VariableMapper getVariableMapper() {
|
||||
return variableMapper;
|
||||
}
|
||||
|
||||
public void setVariableMapper(VariableMapper variableMapper) {
|
||||
this.variableMapper = variableMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a delegate {@link ELContext}.
|
||||
*
|
||||
* If this context is currently configured with the {@link DefaultELResolver}, that resolver instance will be
|
||||
* replaced with a new {@link CompositeELResolver}.
|
||||
*
|
||||
* The delegate's base {@link ELResolver} will be added to this context's base {@link CompositeELResolver}.
|
||||
*
|
||||
* @param context
|
||||
* The {@link ELContext} whose base {@link ELResolver} needs to be included in this context's base
|
||||
* {@link CompositeELResolver}.
|
||||
*/
|
||||
public void addDelegate(ELContext context) {
|
||||
delegates.add(context);
|
||||
if (getELResolver() == null || getELResolver() instanceof DefaultELResolver) {
|
||||
CompositeELResolver composite = new CompositeELResolver();
|
||||
composite.add(context.getELResolver());
|
||||
setELResolver(composite);
|
||||
} else if (getELResolver() instanceof CompositeELResolver) {
|
||||
((CompositeELResolver) getELResolver()).add(context.getELResolver());
|
||||
}
|
||||
}
|
||||
|
||||
public Object getContext(Class key) {
|
||||
Object context = super.getContext(key);
|
||||
if (context != null) {
|
||||
return context;
|
||||
}
|
||||
|
||||
Iterator i = delegates.iterator();
|
||||
while (i.hasNext()) {
|
||||
ELContext delegate = (ELContext) i.next();
|
||||
context = delegate.getContext(key);
|
||||
if (context != null) {
|
||||
return context;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,6 +2,11 @@ package org.springframework.binding.expression.el;
|
||||
|
||||
import javax.el.ELContext;
|
||||
|
||||
/**
|
||||
* A factory for creating a EL context object that will be used to evaluate a target object of an EL expression.
|
||||
*
|
||||
* @author Jeremy Grelle
|
||||
*/
|
||||
public interface ELContextFactory {
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,7 +2,10 @@ 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;
|
||||
@@ -42,34 +45,42 @@ public class JBossELExpressionParser implements ExpressionParser {
|
||||
/**
|
||||
* The {@link ELContextFactory} for retrieving a configured ELContext.
|
||||
*/
|
||||
private ELContextFactory contextFactory = new DefaultELContextFactory();
|
||||
private ELContextFactory contextFactory;
|
||||
|
||||
/**
|
||||
* The ExpressionFactory for constructing EL expressions
|
||||
*/
|
||||
private ExpressionFactory factory = new ExpressionFactoryImpl();
|
||||
|
||||
public String getExpressionPrefix() {
|
||||
return expressionPrefix;
|
||||
/**
|
||||
* Creates a new EL expression parser for standalone usage.
|
||||
*/
|
||||
public JBossELExpressionParser() {
|
||||
this.contextFactory = new DefaultELContextFactory();
|
||||
}
|
||||
|
||||
public String getExpressionSuffix() {
|
||||
return expressionSuffix;
|
||||
/**
|
||||
* Creates a new EL expression parser with a custom context factory for a specific environment.
|
||||
*
|
||||
* @param contextFactory the context factory
|
||||
*/
|
||||
public JBossELExpressionParser(ELContextFactory contextFactory) {
|
||||
this.contextFactory = contextFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether or not given criteria are expressed as an expression.
|
||||
*/
|
||||
public boolean isDelimitedExpression(String expressionString) {
|
||||
int prefixIndex = expressionString.indexOf(getExpressionPrefix());
|
||||
int prefixIndex = expressionString.indexOf(expressionPrefix);
|
||||
if (prefixIndex == -1) {
|
||||
return false;
|
||||
}
|
||||
int suffixIndex = expressionString.indexOf(getExpressionSuffix(), prefixIndex);
|
||||
int suffixIndex = expressionString.indexOf(expressionSuffix, prefixIndex);
|
||||
if (suffixIndex == -1) {
|
||||
return false;
|
||||
} else {
|
||||
if (suffixIndex == prefixIndex + getExpressionPrefix().length()) {
|
||||
if (suffixIndex == prefixIndex + expressionPrefix.length()) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
@@ -77,24 +88,23 @@ public class JBossELExpressionParser implements ExpressionParser {
|
||||
}
|
||||
}
|
||||
|
||||
public Expression parseExpression(String expressionString) throws ParserException {
|
||||
public final Expression parseExpression(String expressionString) throws ParserException {
|
||||
if (!isDelimitedExpression(expressionString)) {
|
||||
expressionString = getExpressionPrefix() + expressionString + getExpressionSuffix();
|
||||
expressionString = expressionPrefix + expressionString + expressionSuffix;
|
||||
}
|
||||
return doParseExpression(expressionString);
|
||||
}
|
||||
|
||||
public SettableExpression parseSettableExpression(String expressionString) throws ParserException,
|
||||
public final SettableExpression parseSettableExpression(String expressionString) throws ParserException,
|
||||
UnsupportedOperationException {
|
||||
if (!isDelimitedExpression(expressionString)) {
|
||||
expressionString = getExpressionPrefix() + expressionString + getExpressionSuffix();
|
||||
expressionString = expressionPrefix + expressionString + expressionSuffix;
|
||||
}
|
||||
return doParseSettableExpression(expressionString);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the expression string into an EL value expression.
|
||||
*
|
||||
* @param expressionString
|
||||
* @throws ParserException
|
||||
*/
|
||||
@@ -104,26 +114,47 @@ public class JBossELExpressionParser implements ExpressionParser {
|
||||
|
||||
/**
|
||||
* Parses the expression string into an EL value expression.
|
||||
*
|
||||
* @param expressionString
|
||||
* @throws ParserException
|
||||
*/
|
||||
protected SettableExpression doParseSettableExpression(String expressionString) throws ParserException {
|
||||
ELContext ctx = getELContextFactory().getELContext(null);
|
||||
ELContext ctx = contextFactory.getELContext(null);
|
||||
try {
|
||||
return new ELExpression(getELContextFactory(), factory.createValueExpression(ctx, expressionString,
|
||||
Object.class));
|
||||
return new ELExpression(contextFactory, factory.createValueExpression(ctx, expressionString, Object.class));
|
||||
} catch (ELException ex) {
|
||||
throw new ParserException(expressionString, ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the proper {@link ELContextFactory} for the current environment.
|
||||
*
|
||||
* @return ELContextFactory The ELContextFactory for the current environment.
|
||||
*/
|
||||
protected ELContextFactory getELContextFactory() {
|
||||
return contextFactory;
|
||||
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