javax.el.ELContext Java Examples

The following examples show how to use javax.el.ELContext. 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: TestValueExpressionImpl.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testGetValueReference() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl(factory);

    TesterBeanB beanB = new TesterBeanB();
    beanB.setName("Tomcat");
    ValueExpression var =
        factory.createValueExpression(beanB, TesterBeanB.class);
    context.getVariableMapper().setVariable("beanB", var);

    ValueExpression ve = factory.createValueExpression(
            context, "${beanB.name}", String.class);

    // First check the basics work
    String result = (String) ve.getValue(context);
    Assert.assertEquals("Tomcat", result);

    // Now check the value reference
    ValueReference vr = ve.getValueReference(context);
    Assert.assertNotNull(vr);

    Assert.assertEquals(beanB, vr.getBase());
    Assert.assertEquals("name", vr.getProperty());
}
 
Example #2
Source File: TestELParser.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testJavaKeyWordSuffix() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl(factory);

    TesterBeanA beanA = new TesterBeanA();
    beanA.setInt("five");
    ValueExpression var =
        factory.createValueExpression(beanA, TesterBeanA.class);
    context.getVariableMapper().setVariable("beanA", var);

    // Should fail
    Exception e = null;
    try {
        factory.createValueExpression(context, "${beanA.int}",
                String.class);
    } catch (ELException ele) {
        e = ele;
    }
    Assert.assertNotNull(e);
}
 
Example #3
Source File: ELSupport.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Coerce an object to a string.
 * @param ctx the context in which this conversion is taking place
 * @param obj the object to convert
 * @return the String value of the object
 */
public static final String coerceToString(final ELContext ctx, final Object obj) {

    if (ctx != null) {
        boolean originalIsPropertyResolved = ctx.isPropertyResolved();
        try {
            Object result = ctx.getELResolver().convertToType(ctx, obj, String.class);
            if (ctx.isPropertyResolved()) {
                return (String) result;
            }
        } finally {
            ctx.setPropertyResolved(originalIsPropertyResolved);
        }
    }

    if (obj == null) {
        return "";
    } else if (obj instanceof String) {
        return (String) obj;
    } else if (obj instanceof Enum<?>) {
        return ((Enum<?>) obj).name();
    } else {
        return obj.toString();
    }
}
 
Example #4
Source File: ImplicitObjectELResolver.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Override
public void setValue(ELContext context, Object base, Object property,
        Object value) throws NullPointerException,
        PropertyNotFoundException, PropertyNotWritableException,
        ELException {
    if (context == null) {
        throw new NullPointerException();
    }

    if (base == null && property != null) {
        int idx = Arrays.binarySearch(SCOPE_NAMES, property.toString());
        if (idx >= 0) {
            context.setPropertyResolved(true);
            throw new PropertyNotWritableException();
        }
    }
}
 
Example #5
Source File: AstRangeBracket.java    From jinjava with Apache License 2.0 6 votes vote down vote up
private int intVal(
  AstNode node,
  int defVal,
  int baseLength,
  Bindings bindings,
  ELContext context
) {
  if (node == null) {
    return defVal;
  }
  Object val = node.eval(bindings, context);
  if (val == null) {
    return defVal;
  }
  if (!(val instanceof Number)) {
    throw new ELException("Range start/end is not a number");
  }
  int result = ((Number) val).intValue();
  return result >= 0 ? result : baseLength + result;
}
 
Example #6
Source File: ELResolverImpl.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
public void setValue(ELContext context, Object base, Object property,
        Object value) throws NullPointerException,
        PropertyNotFoundException, PropertyNotWritableException,
        ELException {
    if (context == null) {
        throw new NullPointerException();
    }

    if (base == null) {
        context.setPropertyResolved(true);
        throw new PropertyNotWritableException(
                "Legacy VariableResolver wrapped, not writable");
    }

    if (!context.isPropertyResolved()) {
        elResolver.setValue(context, base, property, value);
    }
}
 
Example #7
Source File: TestELParser.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
private void doTestBug56179(int parenthesesCount, String innerExpr) {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl(factory);

    ValueExpression var =
        factory.createValueExpression(Boolean.TRUE, Boolean.class);
    context.getVariableMapper().setVariable("test", var);

    StringBuilder expr = new StringBuilder();
    expr.append("${");
    for (int i = 0; i < parenthesesCount; i++) {
        expr.append("(");
    }
    expr.append(innerExpr);
    for (int i = 0; i < parenthesesCount; i++) {
        expr.append(")");
    }
    expr.append("}");
    ValueExpression ve = factory.createValueExpression(
            context, expr.toString(), String.class);

    String result = (String) ve.getValue(context);
    Assert.assertEquals("true", result);
}
 
Example #8
Source File: ELResolverImpl.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Override
public Object getValue(ELContext context, Object base, Object property)
        throws NullPointerException, PropertyNotFoundException, ELException {
    if (context == null) {
        throw new NullPointerException();
    }

    if (base == null) {
        context.setPropertyResolved(true);
        if (property != null) {
            try {
                return this.variableResolver.resolveVariable(property
                        .toString());
            } catch (javax.servlet.jsp.el.ELException e) {
                throw new ELException(e.getMessage(), e.getCause());
            }
        }
    }

    if (!context.isPropertyResolved()) {
        return elResolver.getValue(context, base, property);
    }
    return null;
}
 
Example #9
Source File: ElMetricName.java    From metrics-cdi with Apache License 2.0 6 votes vote down vote up
private ELContext createELContext(final ELResolver resolver, final FunctionMapper functionMapper, final VariableMapper variableMapper) {

        return new ELContext() {
            @Override
            public ELResolver getELResolver() {
                return resolver;
            }

            @Override
            public FunctionMapper getFunctionMapper() {
                return functionMapper;
            }

            @Override
            public VariableMapper getVariableMapper() {
                return variableMapper;
            }
        };
    }
 
Example #10
Source File: ELResolverImpl.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Override
public void setValue(ELContext context, Object base, Object property,
        Object value) throws NullPointerException,
        PropertyNotFoundException, PropertyNotWritableException,
        ELException {
    if (context == null) {
        throw new NullPointerException();
    }

    if (base == null) {
        context.setPropertyResolved(true);
        throw new PropertyNotWritableException(
                "Legacy VariableResolver wrapped, not writable");
    }

    if (!context.isPropertyResolved()) {
        elResolver.setValue(context, base, property, value);
    }
}
 
Example #11
Source File: ELResolverImpl.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
public Object getValue(ELContext context, Object base, Object property)
        throws NullPointerException, PropertyNotFoundException, ELException {
    if (context == null) {
        throw new NullPointerException();
    }

    if (base == null) {
        context.setPropertyResolved(true);
        if (property != null) {
            try {
                return this.variableResolver.resolveVariable(property
                        .toString());
            } catch (javax.servlet.jsp.el.ELException e) {
                throw new ELException(e.getMessage(), e.getCause());
            }
        }
    }

    if (!context.isPropertyResolved()) {
        return elResolver.getValue(context, base, property);
    }
    return null;
}
 
Example #12
Source File: TestValueExpressionImpl.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Test
public void testBug51177ObjectMap() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl();

    Object o1 = "String value";
    Object o2 = Integer.valueOf(32);

    Map<Object,Object> map = new HashMap<Object,Object>();
    map.put("key1", o1);
    map.put("key2", o2);

    ValueExpression var =
        factory.createValueExpression(map, Map.class);
    context.getVariableMapper().setVariable("map", var);

    ValueExpression ve1 = factory.createValueExpression(
            context, "${map.key1}", Object.class);
    ve1.setValue(context, o2);
    assertEquals(o2, ve1.getValue(context));

    ValueExpression ve2 = factory.createValueExpression(
            context, "${map.key2}", Object.class);
    ve2.setValue(context, o1);
    assertEquals(o1, ve2.getValue(context));
}
 
Example #13
Source File: TestELParser.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Test
public void bug56185() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl();

    TesterBeanC beanC = new TesterBeanC();
    ValueExpression var =
        factory.createValueExpression(beanC, TesterBeanC.class);
    context.getVariableMapper().setVariable("myBean", var);

    ValueExpression ve = factory.createValueExpression(context,
        "${(myBean.int1 > 1 and myBean.myBool) or "+
        "((myBean.myBool or myBean.myBool1) and myBean.int1 > 1)}",
        Boolean.class);
    assertEquals(Boolean.FALSE, ve.getValue(context));
    beanC.setInt1(2);
    beanC.setMyBool1(true);
    assertEquals(Boolean.TRUE, ve.getValue(context));
}
 
Example #14
Source File: SpringBeanELResolver.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isReadOnly(ELContext elContext, Object base, Object property) throws ELException {
	if (base == null) {
		String beanName = property.toString();
		BeanFactory bf = getBeanFactory(elContext);
		if (bf.containsBean(beanName)) {
			return true;
		}
	}
	return false;
}
 
Example #15
Source File: NodeELResolver.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
@Override
public Class<?> getType(ELContext context, Object base, Object property) {
    if (context == null) {
        throw new NullPointerException("context is null");
    }
    Class<?> result = null;
    if (isResolvable(base)) {
        result = Node.class;
        context.setPropertyResolved(true);
    }
    return result;
}
 
Example #16
Source File: ExpressionFactoryImpl.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public ValueExpression createValueExpression(ELContext context,
        String expression, Class<?> expectedType) {
    if (expectedType == null) {
        throw new NullPointerException(MessageFactory
                .get("error.value.expectedType"));
    }
    ExpressionBuilder builder = new ExpressionBuilder(expression, context);
    return builder.createValueExpression(expectedType);
}
 
Example #17
Source File: ElExpressionSample.java    From Android_Code_Arbiter with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void safeEL() {
    FacesContext context = FacesContext.getCurrentInstance();
    ExpressionFactory expressionFactory = context.getApplication().getExpressionFactory();
    ELContext elContext = context.getELContext();
    ValueExpression vex = expressionFactory.createValueExpression(elContext, "1+1", String.class);
    String result = (String) vex.getValue(elContext);
    System.out.println(result);
}
 
Example #18
Source File: JsonNodeELResolver.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
/**
 * If the base object is a map, returns the value associated with the given key, as specified by
 * the property argument. If the key was not found, null is returned. If the base is a Map, the
 * propertyResolved property of the ELContext object must be set to true by this resolver,
 * before returning. If this property is not true after this method is called, the caller should
 * ignore the return value. Just as in java.util.Map.get(Object), just because null is returned
 * doesn't mean there is no mapping for the key; it's also possible that the Map explicitly maps
 * the key to null.
 * 
 * @param context
 *            The context of this evaluation.
 * @param base
 *            The map to analyze. Only bases of type Map are handled by this resolver.
 * @param property
 *            The key to return the acceptable type for. Ignored by this resolver.
 * @return If the propertyResolved property of ELContext was set to true, then the value
 *         associated with the given key or null if the key was not found. Otherwise, undefined.
 * @throws ClassCastException
 *             if the key is of an inappropriate type for this map (optionally thrown by the
 *             underlying Map).
 * @throws NullPointerException
 *             if context is null, or if the key is null and this map does not permit null keys
 *             (the latter is optionally thrown by the underlying Map).
 * @throws ELException
 *             if an exception was thrown while performing the property or variable resolution.
 *             The thrown exception must be included as the cause property of this exception, if
 *             available.
 */
@Override
public Object getValue(ELContext context, Object base, Object property) {
  if (context == null) {
    throw new NullPointerException("context is null");
  }
  Object result = null;
  if (isResolvable(base)) {
    JsonNode resultNode = ((JsonNode) base).get(property.toString());
    if (resultNode != null && resultNode.isValueNode()) {
      if (resultNode.isBoolean()) {
        result = resultNode.asBoolean();
      } else if (resultNode.isLong()) {
        result = resultNode.asLong();
      } else if (resultNode.isBigDecimal() || resultNode.isDouble()) {
        result = resultNode.asDouble();
      } else if (resultNode.isTextual()) {
        result = resultNode.asText();
      } else {
        result = resultNode.toString();
      }
      
    } else {
      result = resultNode;
    }
    context.setPropertyResolved(true);
  }
  return result;
}
 
Example #19
Source File: ExpressionFactoryImpl.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
public MethodExpression createMethodExpression(ELContext context,
        String expression, Class<?> expectedReturnType,
        Class<?>[] expectedParamTypes) {
    ExpressionBuilder builder = new ExpressionBuilder(expression, context);
    return builder.createMethodExpression(expectedReturnType,
            expectedParamTypes);
}
 
Example #20
Source File: MethodExpressionLiteral.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public Object invoke(ELContext context, Object[] params) throws ELException {
    context.notifyBeforeEvaluation(getExpressionString());
    Object result;
    if (this.expectedType != null) {
        result = context.convertToType(this.expr, this.expectedType);
    } else {
        result = this.expr;
    }
    context.notifyAfterEvaluation(getExpressionString());
    return result;
}
 
Example #21
Source File: RootResolver.java    From validator-web with Apache License 2.0 5 votes vote down vote up
private Object evaluateFormatExpression(ELContext context, Object method, Object[] params) {
    if (!FORMAT.equals(method)) {
        throw new ELException("Wrong method name 'formatter#" + method + "' does not exist. Only formatter#format is supported.");
    }

    if (params.length == 0) {
        throw new ELException("Invalid number of arguments to Formatter#format");
    }

    if (!(params[0] instanceof String)) {
        throw new ELException("The first argument to Formatter#format must be String");
    }

    FormatterWrapper formatterWrapper = (FormatterWrapper) context.getVariableMapper()
            .resolveVariable(FORMATTER)
            .getValue(context);
    Object[] formattingParameters = new Object[params.length - 1];
    System.arraycopy(params, 1, formattingParameters, 0, params.length - 1);

    Object returnValue;
    try {
        returnValue = formatterWrapper.format((String) params[0], formattingParameters);
        context.setPropertyResolved(true);
    } catch (IllegalFormatException e) {
        throw new ELException("Error in Formatter#format call", e);
    }

    return returnValue;
}
 
Example #22
Source File: JspContextWrapper.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public ELContext getELContext() {
    if (elContext == null) {
        elContext = new ELContextWrapper(rootJspCtxt.getELContext(), jspTag, this);
        JspFactory factory = JspFactory.getDefaultFactory();
        JspApplicationContext jspAppCtxt = factory.getJspApplicationContext(servletContext);
        if (jspAppCtxt instanceof JspApplicationContextImpl) {
            ((JspApplicationContextImpl) jspAppCtxt).fireListeners(elContext);
        }
    }
    return elContext;
}
 
Example #23
Source File: TestValueExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetValueReferenceVariable() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl();

    TesterBeanB beanB = new TesterBeanB();
    beanB.setName("Tomcat");
    ValueExpression var =
        factory.createValueExpression(beanB, TesterBeanB.class);
    context.getVariableMapper().setVariable("beanB", var);

    ValueExpression var2 = factory.createValueExpression(
            context, "${beanB.name}", String.class);

    context.getVariableMapper().setVariable("foo", var2);

    ValueExpression ve = factory.createValueExpression(
            context, "${foo}", ValueExpression.class);


    // Now check the value reference
    ValueReference vr = ve.getValueReference(context);
    assertNotNull(vr);

    assertEquals(beanB, vr.getBase());
    assertEquals("name", vr.getProperty());
}
 
Example #24
Source File: TestRuleVerifyUtils.java    From proctor with Apache License 2.0 5 votes vote down vote up
private ELContext setUpElContextWithContext(final Map<String, Object> context, final String testRule) {
    final List<TestBucket> buckets = TestProctorUtils.fromCompactBucketFormat("inactive:-1,control:0,test:1");
    final ConsumableTestDefinition testDefVal1 = TestProctorUtils.constructDefinition(buckets,
            TestProctorUtils.fromCompactAllocationFormat(String.format("%s|-1:0.5,0:0.5,1:0.0", testRule), "-1:0.25,0:0.5,1:0.25"));
    final Map<String, ValueExpression> testConstants = ProctorUtils.convertToValueExpressionMap(expressionFactory, testDefVal1.getConstants());

    final ProvidedContext providedContext = ProvidedContext.forValueExpressionMap(ProctorUtils.convertToValueExpressionMap(expressionFactory, context), Collections.emptySet());
    final VariableMapper variableMapper = new MulticontextReadOnlyVariableMapper(testConstants, providedContext.getContext());

    final RuleEvaluator ruleEvaluator = new RuleEvaluator(expressionFactory, RuleEvaluator.FUNCTION_MAPPER, testDefVal1.getConstants());
    return ruleEvaluator.createELContext(variableMapper);
}
 
Example #25
Source File: DynamicBeanPropertyELResolver.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@Override
public Class<?> getType(ELContext context, Object base, Object property) {
  if (base == null || this.getCommonPropertyType(context, base) == null) {
    return null;
  }
  
  context.setPropertyResolved(true);
  return Object.class;
}
 
Example #26
Source File: TestValueExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testBug49345() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl();

    TesterBeanA beanA = new TesterBeanA();
    TesterBeanB beanB = new TesterBeanB();
    beanB.setName("Tomcat");
    beanA.setBean(beanB);

    ValueExpression var =
        factory.createValueExpression(beanA, TesterBeanA.class);
    context.getVariableMapper().setVariable("beanA", var);

    ValueExpression ve = factory.createValueExpression(
            context, "${beanA.bean.name}", String.class);

    // First check the basics work
    String result = (String) ve.getValue(context);
    assertEquals("Tomcat", result);

    // Now check the value reference
    ValueReference vr = ve.getValueReference(context);
    assertNotNull(vr);

    assertEquals(beanB, vr.getBase());
    assertEquals("name", vr.getProperty());
}
 
Example #27
Source File: ELResolverImpl.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public boolean isReadOnly(ELContext context, Object base, Object property) {
    Objects.requireNonNull(context);

    if (base == null) {
        context.setPropertyResolved(base, property);
        return true;
    }

    return elResolver.isReadOnly(context, base, property);
}
 
Example #28
Source File: ScopedAttributeELResolver.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public Class<Object> getType(ELContext context, Object base, Object property) {
    Objects.requireNonNull(context);

    if (base == null) {
        context.setPropertyResolved(base, property);
        return Object.class;
    }

    return null;
}
 
Example #29
Source File: ELResolverImpl.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isReadOnly(ELContext context, Object base, Object property)
        throws NullPointerException, PropertyNotFoundException, ELException {
    if (context == null) {
        throw new NullPointerException();
    }

    if (base == null) {
        context.setPropertyResolved(true);
        return true;
    }

    return elResolver.isReadOnly(context, base, property);
}
 
Example #30
Source File: FeelEngineImpl.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
protected ValueExpression transformSimpleUnaryTests(String simpleUnaryTests, String inputName, ELContext elContext) {

    String juelExpression = transformToJuelExpression(simpleUnaryTests, inputName);

    try {
      return expressionFactory.createValueExpression(elContext, juelExpression, Object.class);
    }
    catch (ELException e) {
      throw LOG.invalidExpression(simpleUnaryTests, e);
    }
  }