This commit is contained in:
Keith Donald
2008-03-06 05:18:59 +00:00
parent c0fb6c0b98
commit 4f95e0711a
3 changed files with 56 additions and 5 deletions

View File

@@ -31,8 +31,8 @@ import org.springframework.binding.expression.ExpressionVariable;
import org.springframework.binding.expression.ParserContext;
import org.springframework.binding.expression.ParserException;
import org.springframework.binding.expression.support.CompositeStringExpression;
import org.springframework.binding.expression.support.LiteralExpression;
import org.springframework.binding.expression.support.NullParserContext;
import org.springframework.binding.expression.support.StaticExpression;
import org.springframework.util.Assert;
/**
@@ -209,7 +209,7 @@ public class OgnlExpressionParser implements ExpressionParser {
if (prefixIndex >= startIdx) {
// a inner expression was found - this is a composite
if (prefixIndex > startIdx) {
expressions.add(new StaticExpression(expressionString.substring(startIdx, prefixIndex)));
expressions.add(new LiteralExpression(expressionString.substring(startIdx, prefixIndex)));
startIdx = prefixIndex;
}
int nextPrefixIndex = expressionString.indexOf(getExpressionPrefix(), prefixIndex
@@ -241,11 +241,11 @@ public class OgnlExpressionParser implements ExpressionParser {
expressions.add(doParseExpression(expressionString, context));
} else {
// treat entire string as a literal
expressions.add(new StaticExpression(expressionString));
expressions.add(new LiteralExpression(expressionString));
}
} else {
// no more ${expressions} found in string, add rest as static text
expressions.add(new StaticExpression(expressionString.substring(startIdx)));
expressions.add(new LiteralExpression(expressionString.substring(startIdx)));
}
startIdx = expressionString.length();
}

View File

@@ -17,6 +17,7 @@ package org.springframework.binding.expression.support;
import org.springframework.binding.expression.EvaluationException;
import org.springframework.binding.expression.Expression;
import org.springframework.binding.expression.SetValueAttempt;
import org.springframework.core.style.ToStringCreator;
/**
@@ -49,7 +50,8 @@ public class CompositeStringExpression implements Expression {
}
public void setValue(Object context, Object value) throws EvaluationException {
throw new UnsupportedOperationException("Cannot set a composite string expression value");
throw new EvaluationException(new SetValueAttempt(this, context, value), new UnsupportedOperationException(
"Cannot set a composite string expression value"));
}
public String toString() {

View File

@@ -0,0 +1,49 @@
package org.springframework.binding.expression.support;
import org.springframework.binding.expression.EvaluationException;
import org.springframework.binding.expression.Expression;
import org.springframework.binding.expression.SetValueAttempt;
import org.springframework.util.Assert;
public class LiteralExpression implements Expression {
/**
* The string literal.
*/
private String literal;
/**
* Create a literal expression for the given literal.
* @param literal the literal
*/
public LiteralExpression(String literal) {
Assert.notNull(literal, "The literal is required");
this.literal = literal;
}
public int hashCode() {
return literal.hashCode();
}
public boolean equals(Object o) {
if (!(o instanceof LiteralExpression)) {
return false;
}
LiteralExpression other = (LiteralExpression) o;
return literal.equals(other.literal);
}
public Object getValue(Object context) throws EvaluationException {
return literal;
}
public void setValue(Object context, Object value) throws EvaluationException {
throw new EvaluationException(new SetValueAttempt(this, context, value), new UnsupportedOperationException(
"Cannot set a literal expression value. Are you attempting to set a property expression? "
+ "If so, should the expression string be enclosed in eval delimiters?"));
}
public String toString() {
return "literal('" + literal + "')";
}
}