Search in sources :

Example 6 with ParseException

use of org.springframework.expression.ParseException in project spring-framework by spring-projects.

the class SetValueTests method setValueExpectError.

/**
	 * Call setValue() but expect it to fail.
	 */
protected void setValueExpectError(String expression, Object value) {
    try {
        Expression e = parser.parseExpression(expression);
        if (e == null) {
            fail("Parser returned null for expression");
        }
        if (DEBUG) {
            SpelUtilities.printAbstractSyntaxTree(System.out, e);
        }
        StandardEvaluationContext lContext = TestScenarioCreator.getTestEvaluationContext();
        e.setValue(lContext, value);
        fail("expected an error");
    } catch (ParseException pe) {
        pe.printStackTrace();
        fail("Unexpected Exception: " + pe.getMessage());
    } catch (EvaluationException ee) {
    // success!
    }
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) ParseException(org.springframework.expression.ParseException) EvaluationException(org.springframework.expression.EvaluationException)

Example 7 with ParseException

use of org.springframework.expression.ParseException in project spring-framework by spring-projects.

the class TemplateExpressionParsingTests method testNestedExpressions.

@Test
public void testNestedExpressions() throws Exception {
    SpelExpressionParser parser = new SpelExpressionParser();
    // treat the nested ${..} as a part of the expression
    Expression ex = parser.parseExpression("hello ${listOfNumbersUpToTen.$[#this<5]} world", DEFAULT_TEMPLATE_PARSER_CONTEXT);
    String s = ex.getValue(TestScenarioCreator.getTestEvaluationContext(), String.class);
    assertEquals("hello 4 world", s);
    // not a useful expression but tests nested expression syntax that clashes with template prefix/suffix
    ex = parser.parseExpression("hello ${listOfNumbersUpToTen.$[#root.listOfNumbersUpToTen.$[#this%2==1]==3]} world", DEFAULT_TEMPLATE_PARSER_CONTEXT);
    assertEquals(CompositeStringExpression.class, ex.getClass());
    CompositeStringExpression cse = (CompositeStringExpression) ex;
    Expression[] exprs = cse.getExpressions();
    assertEquals(3, exprs.length);
    assertEquals("listOfNumbersUpToTen.$[#root.listOfNumbersUpToTen.$[#this%2==1]==3]", exprs[1].getExpressionString());
    s = ex.getValue(TestScenarioCreator.getTestEvaluationContext(), String.class);
    assertEquals("hello  world", s);
    ex = parser.parseExpression("hello ${listOfNumbersUpToTen.$[#this<5]} ${listOfNumbersUpToTen.$[#this>5]} world", DEFAULT_TEMPLATE_PARSER_CONTEXT);
    s = ex.getValue(TestScenarioCreator.getTestEvaluationContext(), String.class);
    assertEquals("hello 4 10 world", s);
    try {
        ex = parser.parseExpression("hello ${listOfNumbersUpToTen.$[#this<5]} ${listOfNumbersUpToTen.$[#this>5] world", DEFAULT_TEMPLATE_PARSER_CONTEXT);
        fail("Should have failed");
    } catch (ParseException pe) {
        assertEquals("No ending suffix '}' for expression starting at character 41: ${listOfNumbersUpToTen.$[#this>5] world", pe.getSimpleMessage());
    }
    try {
        ex = parser.parseExpression("hello ${listOfNumbersUpToTen.$[#root.listOfNumbersUpToTen.$[#this%2==1==3]} world", DEFAULT_TEMPLATE_PARSER_CONTEXT);
        fail("Should have failed");
    } catch (ParseException pe) {
        assertEquals("Found closing '}' at position 74 but most recent opening is '[' at position 30", pe.getSimpleMessage());
    }
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) CompositeStringExpression(org.springframework.expression.common.CompositeStringExpression) Expression(org.springframework.expression.Expression) CompositeStringExpression(org.springframework.expression.common.CompositeStringExpression) ParseException(org.springframework.expression.ParseException) Test(org.junit.Test)

Example 8 with ParseException

use of org.springframework.expression.ParseException in project spring-security by spring-projects.

the class ExpressionBasedAnnotationAttributeFactory method createPreInvocationAttribute.

public PreInvocationAttribute createPreInvocationAttribute(String preFilterAttribute, String filterObject, String preAuthorizeAttribute) {
    try {
        // TODO: Optimization of permitAll
        ExpressionParser parser = getParser();
        Expression preAuthorizeExpression = preAuthorizeAttribute == null ? parser.parseExpression("permitAll") : parser.parseExpression(preAuthorizeAttribute);
        Expression preFilterExpression = preFilterAttribute == null ? null : parser.parseExpression(preFilterAttribute);
        return new PreInvocationExpressionAttribute(preFilterExpression, filterObject, preAuthorizeExpression);
    } catch (ParseException e) {
        throw new IllegalArgumentException("Failed to parse expression '" + e.getExpressionString() + "'", e);
    }
}
Also used : Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) ParseException(org.springframework.expression.ParseException)

Example 9 with ParseException

use of org.springframework.expression.ParseException in project spring-framework by spring-projects.

the class SetValueTests method setValue.

/**
	 * For use when coercion is happening during a setValue().  The expectedValue should be
	 * the coerced form of the value.
	 */
protected void setValue(String expression, Object value, Object expectedValue) {
    try {
        Expression e = parser.parseExpression(expression);
        if (e == null) {
            fail("Parser returned null for expression");
        }
        if (DEBUG) {
            SpelUtilities.printAbstractSyntaxTree(System.out, e);
        }
        StandardEvaluationContext lContext = TestScenarioCreator.getTestEvaluationContext();
        assertTrue("Expression is not writeable but should be", e.isWritable(lContext));
        e.setValue(lContext, value);
        Object a = expectedValue;
        Object b = e.getValue(lContext);
        if (!a.equals(b)) {
            fail("Not the same: [" + a + "] type=" + a.getClass() + "  [" + b + "] type=" + b.getClass());
        //				assertEquals("Retrieved value was not equal to set value", expectedValue, e.getValue(lContext));
        }
    } catch (EvaluationException ee) {
        ee.printStackTrace();
        fail("Unexpected Exception: " + ee.getMessage());
    } catch (ParseException pe) {
        pe.printStackTrace();
        fail("Unexpected Exception: " + pe.getMessage());
    }
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) EvaluationException(org.springframework.expression.EvaluationException) ParseException(org.springframework.expression.ParseException)

Example 10 with ParseException

use of org.springframework.expression.ParseException in project uPortal by Jasig.

the class StylesheetAttributeSource method doSpelEvaluationForAttributeValue.

protected String doSpelEvaluationForAttributeValue(final WebRequest request, final String attributeValue) {
    String result;
    try {
        final String valueForEvaluation = this.getValueForSpelEvaluation(attributeValue);
        final Expression expression = this.portalSpELService.parseExpression(valueForEvaluation);
        result = this.portalSpELService.getValue(expression, request, String.class);
    } catch (ParseException e) {
        this.getLogger().info("SpEL parse exception parsing: {}; Exception: {}", attributeValue, e);
        result = attributeValue;
    } catch (EvaluationException e) {
        this.getLogger().info("SpEL evaluation exception evaluating: {}; Exception: {}", attributeValue, e);
        result = attributeValue;
    }
    return result;
}
Also used : Expression(org.springframework.expression.Expression) ParseException(org.springframework.expression.ParseException) EvaluationException(org.springframework.expression.EvaluationException)

Aggregations

ParseException (org.springframework.expression.ParseException)14 Expression (org.springframework.expression.Expression)11 EvaluationException (org.springframework.expression.EvaluationException)5 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)5 Test (org.junit.Test)4 ExpressionParser (org.springframework.expression.ExpressionParser)3 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)3 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HierarchicalConfiguration (org.apache.commons.configuration.HierarchicalConfiguration)1 Callback (org.craftercms.commons.lang.Callback)1 ConfigurationException (org.craftercms.engine.exception.ConfigurationException)1 SiteContext (org.craftercms.engine.service.context.SiteContext)1 CompositeStringExpression (org.springframework.expression.common.CompositeStringExpression)1 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)1 ConfigAttribute (org.springframework.security.access.ConfigAttribute)1 FilterInvocation (org.springframework.security.web.FilterInvocation)1