Making DefaultELResolver more flexible.

This commit is contained in:
Jeremy Grelle
2007-09-28 22:31:32 +00:00
parent c1b68c5c15
commit b9513cd42b
4 changed files with 23 additions and 48 deletions

View File

@@ -6,10 +6,11 @@ import javax.el.CompositeELResolver;
import javax.el.ELContext;
import javax.el.ListELResolver;
import javax.el.MapELResolver;
import javax.el.PropertyNotFoundException;
import javax.el.PropertyNotWritableException;
import javax.el.ResourceBundleELResolver;
import org.springframework.binding.collection.MapAdaptable;
import org.springframework.util.Assert;
/**
* A generic ELResolver to be used as a default when no other ELResolvers have been configured by the client
@@ -35,21 +36,27 @@ public class DefaultELResolver extends CompositeELResolver {
}
public Object getValue(ELContext context, Object base, Object property) {
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);
try {
return super.getValue(context, target, property);
} catch (PropertyNotFoundException ex) {
context.setPropertyResolved(false);
}
}
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) {
super.setValue(context, target, property, val);
} else {
super.setValue(context, adaptIfNecessary(base), property, val);
try {
super.setValue(context, target, property, val);
if (context.isPropertyResolved())
return;
} catch (PropertyNotWritableException ex) {
context.setPropertyResolved(false);
}
}
super.setValue(context, adaptIfNecessary(base), property, val);
}
public Object getTarget() {

View File

@@ -84,35 +84,16 @@ public class ELExpressionParser implements ExpressionParser {
}
public final Expression parseExpression(String expressionString) throws ParserException {
if (!isDelimitedExpression(expressionString)) {
expressionString = expressionPrefix + expressionString + expressionSuffix;
}
return doParseExpression(expressionString);
return parseSettableExpression(expressionString);
}
/**
* Parses the expression string into an EL value expression.
* @param expressionString
* @throws ParserException
*/
public final SettableExpression parseSettableExpression(String expressionString) throws ParserException,
UnsupportedOperationException {
if (!isDelimitedExpression(expressionString)) {
expressionString = expressionPrefix + expressionString + expressionSuffix;
}
return doParseSettableExpression(expressionString);
}
/**
* Parses the expression string into an EL value expression.
* @param expressionString
* @throws ParserException
*/
protected Expression doParseExpression(String expressionString) throws ParserException {
return doParseSettableExpression(expressionString);
}
/**
* Parses the expression string into an EL value expression.
* @param expressionString
* @throws ParserException
*/
protected SettableExpression doParseSettableExpression(String expressionString) throws ParserException {
ELContext ctx = contextFactory.getParseContext();
try {
return new ELExpression(contextFactory, expressionFactory.createValueExpression(ctx, expressionString,

View File

@@ -79,11 +79,6 @@ public class ELExpressionParserTests extends TestCase {
Expression result1 = parser.parseExpression(expStr1);
assertNotNull(result1);
assertEquals(expStr1, result1.toString());
String expStr2 = "foo.bar()";
Expression result2 = parser.parseExpression(expStr2);
assertNotNull(result2);
assertEquals(expStr1, result2.toString());
}
public void testMethodWithParams() {
@@ -92,11 +87,6 @@ public class ELExpressionParserTests extends TestCase {
Expression result1 = parser.parseExpression(expStr1);
assertNotNull(result1);
assertEquals(expStr1, result1.toString());
String expStr2 = "foo.bar(moe.curly, groucho.harpo)";
Expression result2 = parser.parseExpression(expStr2);
assertNotNull(result2);
assertEquals(expStr1, result2.toString());
}
}

View File

@@ -75,7 +75,6 @@ public class SimpleExpressionTests extends TestCase {
public void testGetValue() {
assertEquals(Boolean.TRUE, expressionParser.parseExpression(expressionPrefix + "{flag}").evaluate(bean, null));
assertEquals(Boolean.TRUE, expressionParser.parseExpression("flag").evaluate(bean, null));
assertSame(bean.getList(), expressionParser.parseExpression(expressionPrefix + "{list}").evaluate(bean, null));
assertEquals("foo", expressionParser.parseExpression(expressionPrefix + "{list[0]}").evaluate(bean, null));
}
@@ -83,8 +82,6 @@ public class SimpleExpressionTests extends TestCase {
public void testSetValue() {
expressionParser.parseSettableExpression(expressionPrefix + "{flag}").evaluateToSet(bean, Boolean.FALSE, null);
assertFalse(bean.isFlag());
expressionParser.parseSettableExpression("flag").evaluateToSet(bean, Boolean.TRUE, null);
assertTrue(bean.isFlag());
List newList = new ArrayList();
newList.add("boo");
expressionParser.parseSettableExpression(expressionPrefix + "{list}").evaluateToSet(bean, newList, null);
@@ -95,7 +92,7 @@ public class SimpleExpressionTests extends TestCase {
public void testSyntaxError() {
try {
expressionParser.parseExpression("foo(").evaluate(bean, null);
expressionParser.parseExpression(expressionPrefix + "{foo(}").evaluate(bean, null);
fail("should have failed");
} catch (ParserException e) {
} catch (EvaluationException e) {