CompositeStringExpression.java 2.7 KB
Newer Older
A
Andy Clement 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
package org.springframework.expression.common;

import org.springframework.expression.EvaluationContext;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.Expression;

/**
 * Represents a template expression broken into pieces. Each piece will be an Expression but pure text parts to the
 * template will be represented as LiteralExpression objects. An example of a template expression might be: <code><pre>
 * &quot;Hello ${getName()}&quot;
 * </pre></code> which will be represented as a CompositeStringExpression of two parts. The first part being a
 * LiteralExpression representing 'Hello ' and the second part being a real expression that will call getName() when
 * invoked.
A
Andy Clement 已提交
14
 * 
A
Andy Clement 已提交
15 16 17 18
 * @author Andy Clement
 */
public class CompositeStringExpression implements Expression {

A
Andy Clement 已提交
19
	private final String expressionString;
A
Andy Clement 已提交
20 21 22 23

	/**
	 * The array of expressions that make up the composite expression
	 */
A
Andy Clement 已提交
24
	private final Expression[] expressions;
A
Andy Clement 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37

	public CompositeStringExpression(String expressionString, Expression[] expressions) {
		this.expressionString = expressionString;
		this.expressions = expressions;
	}

	public String getExpressionString() {
		return expressionString;
	}

	public String getValue() throws EvaluationException {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < expressions.length; i++) {
A
Andy Clement 已提交
38 39
			// TODO is stringify ok for the non literal components? or should the converters be used? see another
			// case below
A
Andy Clement 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
			sb.append(expressions[i].getValue());
		}
		return sb.toString();
	}

	public String getValue(EvaluationContext context) throws EvaluationException {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < expressions.length; i++) {
			sb.append(expressions[i].getValue(context));
		}
		return sb.toString();
	}

	public Class getValueType(EvaluationContext context) throws EvaluationException {
		return String.class;
	}

A
Andy Clement 已提交
57 58 59 60
	public Class getValueType() throws EvaluationException {
		return String.class;
	}

A
Andy Clement 已提交
61 62 63 64
	public void setValue(EvaluationContext context, Object value) throws EvaluationException {
		throw new EvaluationException(expressionString, "Cannot call setValue() on a composite expression");
	}

65
	public <T> T getValue(EvaluationContext context, Class<T> expectedResultType) throws EvaluationException {
A
Andy Clement 已提交
66
		Object value = getValue(context);
67
		return (T)ExpressionUtils.convert(context, value, expectedResultType);
A
Andy Clement 已提交
68 69
	}

70
	public <T> T getValue(Class<T> expectedResultType) throws EvaluationException {
A
Andy Clement 已提交
71
		Object value = getValue();
72
		return (T)ExpressionUtils.convert(null, value, expectedResultType);
A
Andy Clement 已提交
73
	}
A
Andy Clement 已提交
74 75 76 77

	public boolean isWritable(EvaluationContext context) throws EvaluationException {
		return false;
	}
A
Andy Clement 已提交
78
}