Java Code Examples for javax.el.ELProcessor#eval()

The following examples show how to use javax.el.ELProcessor#eval() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExpressionLanguageTest.java    From telekom-workflow-engine with MIT License 6 votes vote down vote up
@Test
public void testEnvironmentResolver(){
    EnvironmentImpl environment = new EnvironmentImpl();
    environment.setAttribute( "testAtr1", true );
    environment.setAttribute( "testAtr2", "33" );
    environment.setAttribute( "testAtr3", null );

    ELProcessor processor = ElUtil.initNewELProcessor( environment, null );

    boolean result = (boolean)processor.eval( "empty testAtr3 && testAtr1" );

    // this again fails: "ELResolver cannot handle a null base Object with identifier 'unknownAtr'"
    //boolean result = (boolean)processor.eval( "empty testAtr3 && testAtr1 && empty unknownAtr" );

    Assert.assertTrue( result );
}
 
Example 2
Source File: ValidateAttributeActivity.java    From telekom-workflow-engine with MIT License 5 votes vote down vote up
@Override
public void execute( GraphEngine engine, Token token ){
    GraphInstance instance = token.getInstance();
    Environment environment = instance.getEnvironment();

    if( !environment.containsAttribute( attribute ) ){
        if( isRequired ){
            // Throw exception
            throw new WorkflowException( "Missing required attribute '" + attribute + "'" );
        }
        else{
            // Use default value
            if( defaultValue instanceof String && ElUtil.hasBrackets( (String)defaultValue ) ){
                ELProcessor processor = ElUtil.initNewELProcessor( environment, instance.getExternalId() );
                Object expressionResult = processor.eval( ElUtil.removeBrackets( (String)defaultValue ) );
                environment.setAttribute( attribute, expressionResult );
            }
            else{
                environment.setAttribute( attribute, defaultValue );
            }
        }
    }

    // Validate type of value
    Object value = environment.getAttribute( attribute );
    if( value != null && !type.isAssignableFrom( value.getClass() ) ){
        throw new WorkflowException( "The value of attribute '" + attribute
                + "' is of type " + value.getClass().getCanonicalName()
                + " whis is not assignable to the expected type " + type.getCanonicalName() );
    }

    engine.complete( token, null );
}
 
Example 3
Source File: SetAttributeActivity.java    From telekom-workflow-engine with MIT License 5 votes vote down vote up
@Override
public void execute( GraphEngine engine, Token token ){
    GraphInstance instance = token.getInstance();
    Environment environment = instance.getEnvironment();
    if( value instanceof String && ElUtil.hasBrackets( (String)value ) ){
        ELProcessor processor = ElUtil.initNewELProcessor( environment, instance.getExternalId() );
        Object expressionResult = processor.eval( ElUtil.removeBrackets( (String)value ) );
        environment.setAttribute( attribute, expressionResult );
    }
    else{
        environment.setAttribute( attribute, value );
    }
    engine.complete( token, null );
}
 
Example 4
Source File: ExpressionLanguageTest.java    From telekom-workflow-engine with MIT License 5 votes vote down vote up
@Test
public void testConditions(){
    ELProcessor processor = ElUtil.initNewELProcessor( new EnvironmentImpl(), null );

    processor.defineBean( "client", new Client( 1, "Heli Kopter" ) );
    boolean result = (boolean)processor.eval( "not empty client.name and client.id > 0" );

    Assert.assertTrue( result );
}
 
Example 5
Source File: ExpressionLanguageMapping.java    From telekom-workflow-engine with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public T evaluate( GraphInstance instance ){
    if( StringUtils.isNotBlank( expression ) ){
        ELProcessor processor = ElUtil.initNewELProcessor( instance.getEnvironment(), instance.getExternalId() );
        Object expressionResult = processor.eval( ElUtil.removeBrackets( expression ) );
        return (T)expressionResult;
    }
    return null;
}
 
Example 6
Source File: ExpressionLanguageTest.java    From telekom-workflow-engine with MIT License 5 votes vote down vote up
@Test
public void testEnvironmentResolverInstanceIdNull(){
    EnvironmentImpl environment = new EnvironmentImpl();

    ELProcessor processor = ElUtil.initNewELProcessor( environment, null );

    Long instanceId = (Long)processor.eval( "WORKFLOW_INSTANCE_ID" );
    Assert.assertEquals( null, instanceId );
}
 
Example 7
Source File: ExpressionLanguageTest.java    From telekom-workflow-engine with MIT License 5 votes vote down vote up
@Test
public void testEnvironmentResolverInstanceId(){
    EnvironmentImpl environment = new EnvironmentImpl();

    ELProcessor processor = ElUtil.initNewELProcessor( environment, 55L );

    long instanceId = (long)processor.eval( "WORKFLOW_INSTANCE_ID" );
    Assert.assertEquals( 55, instanceId );
}
 
Example 8
Source File: ExpressionLanguageTest.java    From telekom-workflow-engine with MIT License 5 votes vote down vote up
@Test
public void testEnvironmentResolverNull(){
    EnvironmentImpl environment = new EnvironmentImpl();
    environment.setAttribute( "testDate1", null );

    ELProcessor processor = ElUtil.initNewELProcessor( environment, null );

    // in EL, null.fieldName does not throw a nullpointer, and null in conditions always returns false
    boolean result = (boolean)processor.eval( "testDate1.time < System.currentTimeMillis()" );
    Assert.assertFalse( result );
    result = (boolean)processor.eval( "testDate1.whatever > System.currentTimeMillis()" );
    Assert.assertFalse( result );
}
 
Example 9
Source File: TestAstNot.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void test03() {
    ELProcessor processor = new ELProcessor();
    Object result = processor.eval("!false");
    Assert.assertEquals(Boolean.TRUE, result);
}
 
Example 10
Source File: TestELArithmetic.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void testUnaryMinus01() {
    ELProcessor processor = new ELProcessor();
    Object result = processor.eval("-null");
    Assert.assertEquals(Long.valueOf(0), result);
}
 
Example 11
Source File: TestELArithmetic.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void testDivide02() {
    ELProcessor processor = new ELProcessor();
    Object result = processor.eval("null / null");
    Assert.assertEquals(Long.valueOf(0), result);
}
 
Example 12
Source File: TestELArithmetic.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void testAdd02() {
    ELProcessor processor = new ELProcessor();
    Object result = processor.eval("null + null");
    Assert.assertEquals(Long.valueOf(0), result);
}
 
Example 13
Source File: TestAstChoice.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void test01() {
    ELProcessor processor = new ELProcessor();
    Object result = processor.eval("null?1:2");
    Assert.assertEquals(Long.valueOf(2), result);
}
 
Example 14
Source File: TestAstAnd.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void test04() {
    ELProcessor processor = new ELProcessor();
    Object result = processor.eval("null && null");
    Assert.assertEquals(Boolean.FALSE, result);
}
 
Example 15
Source File: TestAstAnd.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void test03() {
    ELProcessor processor = new ELProcessor();
    Object result = processor.eval("null && true");
    Assert.assertEquals(Boolean.FALSE, result);
}
 
Example 16
Source File: TestAstAnd.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void test01() {
    ELProcessor processor = new ELProcessor();
    Object result = processor.eval("true && true");
    Assert.assertEquals(Boolean.TRUE, result);
}
 
Example 17
Source File: TestAstOr.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void test04() {
    ELProcessor processor = new ELProcessor();
    Object result = processor.eval("null || null");
    Assert.assertEquals(Boolean.FALSE, result);
}
 
Example 18
Source File: TestAstOr.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void test03() {
    ELProcessor processor = new ELProcessor();
    Object result = processor.eval("null || true");
    Assert.assertEquals(Boolean.TRUE, result);
}
 
Example 19
Source File: TestAstOr.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void test02() {
    ELProcessor processor = new ELProcessor();
    Object result = processor.eval("true || null");
    Assert.assertEquals(Boolean.TRUE, result);
}
 
Example 20
Source File: Expressions.java    From trimou with Apache License 2.0 3 votes vote down vote up
/**
 * Note that we have to use a separate {@link ELProcessor} for each evaluation
 * as the {@link OptionsELResolver} may not be reused.
 *
 * @param expression
 * @param options
 * @param configuration
 * @return the result of the expression evaluation
 */
static Object eval(String expression, Options options, Configuration configuration) {
    ELProcessorFactory elpFactory = (ELProcessorFactory) configuration
            .getPropertyValue(ELProcessorFactory.EL_PROCESSOR_FACTORY_KEY);
    ELProcessor elp = elpFactory.createELProcessor(configuration);
    elp.getELManager().addELResolver(new OptionsELResolver(options));
    return elp.eval(expression);
}