revised expression parser API design
This commit is contained in:
@@ -1,115 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.expression;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanExpressionContext;
|
||||
import org.springframework.beans.factory.config.BeanExpressionResolver;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Abstract implementation of the {@link BeanExpressionResolver} interface.
|
||||
* Handles the common mixing of expression parts with literal parts.
|
||||
*
|
||||
* <p>Subclasses need to implement the {@link #evaluateExpression} template
|
||||
* method for actual expression evaluation.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.0
|
||||
* @see #setExpressionPrefix
|
||||
* @see #setExpressionSuffix
|
||||
*/
|
||||
public abstract class AbstractBeanExpressionResolver implements BeanExpressionResolver {
|
||||
|
||||
/** Default expression prefix: "#{" */
|
||||
public static final String DEFAULT_EXPRESSION_PREFIX = "#{";
|
||||
|
||||
/** Default expression suffix: "}" */
|
||||
public static final String DEFAULT_EXPRESSION_SUFFIX = "}";
|
||||
|
||||
|
||||
private String expressionPrefix = DEFAULT_EXPRESSION_PREFIX;
|
||||
|
||||
private String expressionSuffix = DEFAULT_EXPRESSION_SUFFIX;
|
||||
|
||||
|
||||
/**
|
||||
* Set the prefix that an expression string starts with.
|
||||
* The default is "#{".
|
||||
* @see #DEFAULT_EXPRESSION_PREFIX
|
||||
*/
|
||||
public void setExpressionPrefix(String expressionPrefix) {
|
||||
Assert.hasText(expressionPrefix, "Expression prefix must not be empty");
|
||||
this.expressionPrefix = expressionPrefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the suffix that an expression string ends with.
|
||||
* The default is "}".
|
||||
* @see #DEFAULT_EXPRESSION_SUFFIX
|
||||
*/
|
||||
public void setExpressionSuffix(String expressionSuffix) {
|
||||
Assert.hasText(expressionSuffix, "Expression suffix must not be empty");
|
||||
this.expressionSuffix = expressionSuffix;
|
||||
}
|
||||
|
||||
|
||||
public Object evaluate(String value, BeanExpressionContext evalContext) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
Object result = "";
|
||||
int prefixIndex = value.indexOf(this.expressionPrefix);
|
||||
int endIndex = 0;
|
||||
while (prefixIndex != -1) {
|
||||
int exprStart = prefixIndex + this.expressionPrefix.length();
|
||||
int suffixIndex = value.indexOf(this.expressionSuffix, exprStart);
|
||||
if (suffixIndex != -1) {
|
||||
if (prefixIndex > 0) {
|
||||
result = result + value.substring(endIndex, prefixIndex);
|
||||
}
|
||||
endIndex = suffixIndex + this.expressionSuffix.length();
|
||||
String expr = value.substring(exprStart, suffixIndex);
|
||||
Object exprResult = evaluateExpression(expr, evalContext);
|
||||
if (result != null && !"".equals(result)) {
|
||||
result = result.toString() + exprResult.toString();
|
||||
}
|
||||
else {
|
||||
result = exprResult;
|
||||
}
|
||||
prefixIndex = value.indexOf(this.expressionPrefix, suffixIndex);
|
||||
}
|
||||
else {
|
||||
prefixIndex = -1;
|
||||
}
|
||||
}
|
||||
if (endIndex < value.length()) {
|
||||
return result + value.substring(endIndex);
|
||||
}
|
||||
else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluate the given expression.
|
||||
* @param exprString the expression String to evaluate
|
||||
* @param evalContext the context to evaluate the expression within
|
||||
* @return the evaluation result
|
||||
*/
|
||||
protected abstract Object evaluateExpression(String exprString, BeanExpressionContext evalContext);
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -30,19 +30,19 @@ import org.springframework.expression.PropertyAccessor;
|
||||
*/
|
||||
public class BeanExpressionContextAccessor implements PropertyAccessor {
|
||||
|
||||
public boolean canRead(EvaluationContext context, Object target, Object name) throws AccessException {
|
||||
return (((BeanExpressionContext) target).containsObject(name.toString()));
|
||||
public boolean canRead(EvaluationContext context, Object target, String name) throws AccessException {
|
||||
return (((BeanExpressionContext) target).containsObject(name));
|
||||
}
|
||||
|
||||
public Object read(EvaluationContext context, Object target, Object name) throws AccessException {
|
||||
return ((BeanExpressionContext) target).getObject(name.toString());
|
||||
public Object read(EvaluationContext context, Object target, String name) throws AccessException {
|
||||
return ((BeanExpressionContext) target).getObject(name);
|
||||
}
|
||||
|
||||
public boolean canWrite(EvaluationContext context, Object target, Object name) throws AccessException {
|
||||
public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void write(EvaluationContext context, Object target, Object name, Object newValue) throws AccessException {
|
||||
public void write(EvaluationContext context, Object target, String name, Object newValue) throws AccessException {
|
||||
throw new AccessException("Beans in a BeanFactory are read-only");
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -30,19 +30,19 @@ import org.springframework.expression.PropertyAccessor;
|
||||
*/
|
||||
public class BeanFactoryAccessor implements PropertyAccessor {
|
||||
|
||||
public boolean canRead(EvaluationContext context, Object target, Object name) throws AccessException {
|
||||
return (((BeanFactory) target).containsBean(name.toString()));
|
||||
public boolean canRead(EvaluationContext context, Object target, String name) throws AccessException {
|
||||
return (((BeanFactory) target).containsBean(name));
|
||||
}
|
||||
|
||||
public Object read(EvaluationContext context, Object target, Object name) throws AccessException {
|
||||
return ((BeanFactory) target).getBean(name.toString());
|
||||
public Object read(EvaluationContext context, Object target, String name) throws AccessException {
|
||||
return ((BeanFactory) target).getBean(name);
|
||||
}
|
||||
|
||||
public boolean canWrite(EvaluationContext context, Object target, Object name) throws AccessException {
|
||||
public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void write(EvaluationContext context, Object target, Object name, Object newValue) throws AccessException {
|
||||
public void write(EvaluationContext context, Object target, String name, Object newValue) throws AccessException {
|
||||
throw new AccessException("Beans in a BeanFactory are read-only");
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -31,19 +31,20 @@ import org.springframework.expression.PropertyAccessor;
|
||||
*/
|
||||
public class MapAccessor implements PropertyAccessor {
|
||||
|
||||
public boolean canRead(EvaluationContext context, Object target, Object name) throws AccessException {
|
||||
public boolean canRead(EvaluationContext context, Object target, String name) throws AccessException {
|
||||
return (((Map) target).containsKey(name));
|
||||
}
|
||||
|
||||
public Object read(EvaluationContext context, Object target, Object name) throws AccessException {
|
||||
public Object read(EvaluationContext context, Object target, String name) throws AccessException {
|
||||
return ((Map) target).get(name);
|
||||
}
|
||||
|
||||
public boolean canWrite(EvaluationContext context, Object target, Object name) throws AccessException {
|
||||
public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void write(EvaluationContext context, Object target, Object name, Object newValue) throws AccessException {
|
||||
@SuppressWarnings("unchecked")
|
||||
public void write(EvaluationContext context, Object target, String name, Object newValue) throws AccessException {
|
||||
((Map) target).put(name, newValue);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,12 +16,18 @@
|
||||
|
||||
package org.springframework.context.expression;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanExpressionException;
|
||||
import org.springframework.beans.factory.config.BeanExpressionContext;
|
||||
import org.springframework.beans.factory.config.BeanExpressionResolver;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.ExpressionParser;
|
||||
import org.springframework.expression.spel.SpelExpressionParser;
|
||||
import org.springframework.expression.spel.standard.StandardEvaluationContext;
|
||||
import org.springframework.expression.ParserContext;
|
||||
import org.springframework.expression.spel.antlr.SpelAntlrExpressionParser;
|
||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
@@ -32,13 +38,61 @@ import org.springframework.util.Assert;
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.0
|
||||
* @see org.springframework.expression.ExpressionParser
|
||||
* @see org.springframework.expression.spel.SpelExpressionParser
|
||||
* @see org.springframework.expression.spel.standard.StandardEvaluationContext
|
||||
* @see org.springframework.expression.spel.antlr.SpelAntlrExpressionParser
|
||||
* @see org.springframework.expression.spel.support.StandardEvaluationContext
|
||||
*/
|
||||
public class StandardBeanExpressionResolver extends AbstractBeanExpressionResolver {
|
||||
public class StandardBeanExpressionResolver implements BeanExpressionResolver {
|
||||
|
||||
private ExpressionParser expressionParser = new SpelExpressionParser();
|
||||
/** Default expression prefix: "#{" */
|
||||
public static final String DEFAULT_EXPRESSION_PREFIX = "#{";
|
||||
|
||||
/** Default expression suffix: "}" */
|
||||
public static final String DEFAULT_EXPRESSION_SUFFIX = "}";
|
||||
|
||||
|
||||
private String expressionPrefix = DEFAULT_EXPRESSION_PREFIX;
|
||||
|
||||
private String expressionSuffix = DEFAULT_EXPRESSION_SUFFIX;
|
||||
|
||||
private ExpressionParser expressionParser = new SpelAntlrExpressionParser();
|
||||
|
||||
private final Map<String, Expression> expressionCache = new ConcurrentHashMap<String, Expression>();
|
||||
|
||||
private final Map<BeanExpressionContext, StandardEvaluationContext> evaluationCache =
|
||||
new ConcurrentHashMap<BeanExpressionContext, StandardEvaluationContext>();
|
||||
|
||||
private final ParserContext beanExpressionParserContext = new ParserContext() {
|
||||
public boolean isTemplate() {
|
||||
return true;
|
||||
}
|
||||
public String getExpressionPrefix() {
|
||||
return expressionPrefix;
|
||||
}
|
||||
public String getExpressionSuffix() {
|
||||
return expressionSuffix;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Set the prefix that an expression string starts with.
|
||||
* The default is "#{".
|
||||
* @see #DEFAULT_EXPRESSION_PREFIX
|
||||
*/
|
||||
public void setExpressionPrefix(String expressionPrefix) {
|
||||
Assert.hasText(expressionPrefix, "Expression prefix must not be empty");
|
||||
this.expressionPrefix = expressionPrefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the suffix that an expression string ends with.
|
||||
* The default is "}".
|
||||
* @see #DEFAULT_EXPRESSION_SUFFIX
|
||||
*/
|
||||
public void setExpressionSuffix(String expressionSuffix) {
|
||||
Assert.hasText(expressionSuffix, "Expression suffix must not be empty");
|
||||
this.expressionSuffix = expressionSuffix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the EL parser to use for expression parsing.
|
||||
@@ -51,14 +105,24 @@ public class StandardBeanExpressionResolver extends AbstractBeanExpressionResolv
|
||||
}
|
||||
|
||||
|
||||
protected Object evaluateExpression(String exprString, BeanExpressionContext evalContext) {
|
||||
public Object evaluate(String value, BeanExpressionContext evalContext) throws BeansException {
|
||||
try {
|
||||
Expression expr = this.expressionParser.parseExpression(exprString);
|
||||
StandardEvaluationContext ec = new StandardEvaluationContext(evalContext);
|
||||
ec.addPropertyAccessor(new BeanExpressionContextAccessor());
|
||||
ec.addPropertyAccessor(new BeanFactoryAccessor());
|
||||
ec.addPropertyAccessor(new MapAccessor());
|
||||
return expr.getValue(ec);
|
||||
Expression expr = this.expressionCache.get(value);
|
||||
if (expr == null) {
|
||||
expr = this.expressionParser.parseExpression(value, this.beanExpressionParserContext);
|
||||
this.expressionCache.put(value, expr);
|
||||
}
|
||||
StandardEvaluationContext sec = this.evaluationCache.get(evalContext);
|
||||
if (sec == null) {
|
||||
sec = new StandardEvaluationContext();
|
||||
sec.setRootObject(evalContext);
|
||||
sec.addPropertyAccessor(new BeanExpressionContextAccessor());
|
||||
sec.addPropertyAccessor(new BeanFactoryAccessor());
|
||||
sec.addPropertyAccessor(new MapAccessor());
|
||||
customizeEvaluationContext(sec);
|
||||
this.evaluationCache.put(evalContext, sec);
|
||||
}
|
||||
return expr.getValue(sec);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new BeanExpressionException("Expression parsing failed", ex);
|
||||
|
||||
Reference in New Issue
Block a user