@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)); }
/** * Given a ValueBinding <code>binding</code>, return a ValueExpression. * The returned ValueExpression will implement StateHolder and Serializable interfaces if * <code>ve</code> implements these interfaces. * @param binding The ValueBinding * @return a ValueExpression equivalent to the ValueBinding */ public static ValueExpression getValueExpression(ValueBinding binding) { // if we previously wrapped a ValueExpression, unwrap it and return it, otherwise create the // correct subclass of ValueBindingValueExpression if (binding instanceof ValueExpressionValueBinding) return ((ValueExpressionValueBinding)binding).getValueExpression(); else if (binding instanceof StateHolder) { if (binding instanceof Serializable) return new SerializableStateHolderValueBindingValueExpression(binding); else return new StateHolderValueBindingValueExpression(binding); } else if (binding instanceof Serializable) { return new SerializableValueBindingValueExpression(binding); } else { return new ValueBindingValueExpression(binding); } }
@Override public Expression parseExpression(String expression, @SuppressWarnings("rawtypes") // API does not use generics Class expectedType, FunctionMapper fMapper) throws ELException { try { ELContextImpl ctx = new ELContextImpl(ELContextImpl.getDefaultResolver()); if (fMapper != null) { ctx.setFunctionMapper(new FunctionMapperImpl(fMapper)); } ValueExpression ve = this.factory.createValueExpression(ctx, expression, expectedType); return new ExpressionImpl(ve); } catch (javax.el.ELException e) { throw new ELParseException(e.getMessage()); } }
@Override public Class<?> getType(EvaluationContext ctx) throws ELException { VariableMapper varMapper = ctx.getVariableMapper(); if (varMapper != null) { ValueExpression expr = varMapper.resolveVariable(this.image); if (expr != null) { return expr.getType(ctx.getELContext()); } } ctx.setPropertyResolved(false); Class<?> result = ctx.getELResolver().getType(ctx, null, this.image); if (!ctx.isPropertyResolved()) { throw new PropertyNotFoundException(MessageFactory.get( "error.resolver.unhandled.null", this.image)); } return result; }
@Override public Object put( PropertyKey key, Object value) { Object retValue = super.put(key, value); if (_createDeltas()) { if (key.getMutable().isAtLeastSometimesMutable() || !_equals(value, retValue)) _deltas.put(key, value); } else if (key.getMutable().isAtLeastSometimesMutable() && !(value instanceof ValueExpression)) { _getMutableTracker(true).addProperty(key); } if (key.isPartialStateHolder()) { _getPartialStateHolderTracker(true).addProperty(key); } return retValue; }
@Override public boolean isReadOnly(EvaluationContext ctx) throws ELException { VariableMapper varMapper = ctx.getVariableMapper(); if (varMapper != null) { ValueExpression expr = varMapper.resolveVariable(this.image); if (expr != null) { return expr.isReadOnly(ctx.getELContext()); } } ctx.setPropertyResolved(false); boolean result = ctx.getELResolver().isReadOnly(ctx, null, this.image); if (!ctx.isPropertyResolved()) { throw new PropertyNotFoundException(MessageFactory.get( "error.resolver.unhandled.null", this.image)); } return result; }
@Override public ValueReference getValueReference(EvaluationContext ctx) { VariableMapper varMapper = ctx.getVariableMapper(); if (varMapper == null) { return null; } ValueExpression expr = varMapper.resolveVariable(this.image); if (expr == null) { return null; } return expr.getValueReference(ctx); }
@Override public Object put( PropertyKey key, Object value) { Object retValue = super.put(key, value); if (_createDeltas()) { if ( key.getMutable().isAtLeastSometimesMutable() || !_equals(value, retValue)) _deltas.put(key, value); } else if (key.getMutable().isAtLeastSometimesMutable() && !(value instanceof ValueExpression)) { _getMutableTracker(true).addProperty(key); } if (key.isPartialStateHolder()) { _getPartialStateHolderTracker(true).addProperty(key); } return retValue; }
@Test public void testGetValueReference() { 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 ve = factory.createValueExpression( context, "${beanB.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()); }
@Override public Expression parseExpression(String expression, @SuppressWarnings("rawtypes") // API // does // not // use // generics Class expectedType, FunctionMapper fMapper) throws ELException { try { ELContextImpl ctx = new ELContextImpl(ELContextImpl.getDefaultResolver()); if (fMapper != null) { ctx.setFunctionMapper(new FunctionMapperImpl(fMapper)); } ValueExpression ve = this.factory.createValueExpression(ctx, expression, expectedType); return new ExpressionImpl(ve); } catch (javax.el.ELException e) { throw new ELParseException(e.getMessage()); } }
@Test public void testJavaKeyWordSuffix() { ExpressionFactory factory = ExpressionFactory.newInstance(); ELContext context = new ELContextImpl(); 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; } assertNotNull(e); }
@Override public void setValueExpression(String name, ValueExpression expression) { if (name == null) throw new NullPointerException(); if ((expression != null) && expression.isLiteralText()) { ELContext context = FacesContext.getCurrentInstance().getELContext(); getAttributes().put(name, expression.getValue(context)); } else { PropertyKey key = getPropertyKey(name); getFacesBean().setValueExpression(key, expression); } }
private void doTestBug56179(int parenthesesCount, String innerExpr) { ExpressionFactory factory = ExpressionFactory.newInstance(); ELContext context = new ELContextImpl(); 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); assertEquals("true", result); }
private void testExpression(String expression, String expected) { ExpressionFactory factory = ExpressionFactory.newInstance(); ELContext context = new ELContextImpl(); ValueExpression ve = factory.createValueExpression( context, expression, String.class); String result = (String) ve.getValue(context); assertEquals(expected, result); }
@Test public void testIsParametersProvided() { TesterBeanB beanB = new TesterBeanB(); beanB.setName("Tomcat"); ValueExpression var = factory.createValueExpression(beanB, TesterBeanB.class); context.getVariableMapper().setVariable("beanB", var); MethodExpression me1 = factory.createMethodExpression( context, "${beanB.getName}", String.class, new Class<?>[] {}); MethodExpression me2 = factory.createMethodExpression( context, "${beanB.sayHello('JUnit')}", String.class, new Class<?>[] { String.class }); assertFalse(me1.isParmetersProvided()); assertTrue(me2.isParmetersProvided()); }
@Override public boolean isReadOnly(EvaluationContext ctx) throws ELException { VariableMapper varMapper = ctx.getVariableMapper(); if (varMapper != null) { ValueExpression expr = varMapper.resolveVariable(this.image); if (expr != null) { return expr.isReadOnly(ctx.getELContext()); } } ctx.setPropertyResolved(false); boolean result = ctx.getELResolver().isReadOnly(ctx, null, this.image); if (!ctx.isPropertyResolved()) { throw new PropertyNotFoundException(MessageFactory.get("error.resolver.unhandled.null", this.image)); } return result; }
/** * Set a property of type java.util.Set<java.lang.String>. If the value * is an EL expression, it will be stored as a ValueExpression. * Otherwise, it will parsed as a whitespace-separated series * of strings. * Null values are ignored. */ protected void setStringSetProperty( FacesBean bean, PropertyKey key, ValueExpression expression) { if (expression == null) return; if (expression.isLiteralText()) { bean.setProperty(key, TagUtils.parseNameTokensAsSet( expression.getValue(FacesContext.getCurrentInstance().getELContext()))); } else { bean.setValueExpression(key, expression); } }
@Test public void testBug50105() { ExpressionFactory factory = ExpressionFactory.newInstance(); ELContext context = new ELContextImpl(); TesterEnum testEnum = TesterEnum.APPLE; ValueExpression var = factory.createValueExpression(testEnum, TesterEnum.class); context.getVariableMapper().setVariable("testEnum", var); // When coercing an Enum to a String, name() should always be used. ValueExpression ve1 = factory.createValueExpression( context, "${testEnum}", String.class); String result1 = (String) ve1.getValue(context); assertEquals("APPLE", result1); ValueExpression ve2 = factory.createValueExpression( context, "foo${testEnum}bar", String.class); String result2 = (String) ve2.getValue(context); assertEquals("fooAPPLEbar", result2); }
final public void setValueExpression(PropertyKey key, ValueExpression expression) { _checkNotListKey(key); if (!key.getSupportsBinding()) { throw new IllegalArgumentException(_LOG.getMessage( "CANNOT_FIND_PROPERTY", key.getName())); } if (expression == null) { PropertyMap map = _getExpressionsMap(false); if (map != null) map.remove(key); } else { _getExpressionsMap(true).put(key, expression); } }
/** * Test using list directly as variable. */ @Test public void testBug51544Direct() throws Exception { ExpressionFactory factory = ExpressionFactory.newInstance(); ELContext context = new ELContextImpl(); List<?> list = Collections.emptyList(); ValueExpression var = factory.createValueExpression(list, List.class); context.getVariableMapper().setVariable("list", var); ValueExpression ve = factory.createValueExpression( context, "${list.size()}", Integer.class); Integer result = (Integer) ve.getValue(context); assertEquals(Integer.valueOf(0), result); }
@Override public void setValue(EvaluationContext ctx, Object value) throws ELException { VariableMapper varMapper = ctx.getVariableMapper(); if (varMapper != null) { ValueExpression expr = varMapper.resolveVariable(this.image); if (expr != null) { expr.setValue(ctx.getELContext(), value); return; } } ctx.setPropertyResolved(false); ctx.getELResolver().setValue(ctx, null, this.image, value); if (!ctx.isPropertyResolved()) { throw new PropertyNotFoundException(MessageFactory.get( "error.resolver.unhandled.null", this.image)); } }
@SuppressWarnings("deprecation") public void apply(FaceletContext faceletContext, UIComponent parent) throws IOException, FacesException, FaceletException, ELException { if(ComponentHandler.isNew(parent)) { ActionSource actionSource = (ActionSource)parent; ReturnActionListener listener = new ReturnActionListener(); if (_value != null) { ValueExpression valueExp = _value.getValueExpression(faceletContext, Object.class); listener.setValueExpression(ReturnActionListener.VALUE_KEY,valueExp); } actionSource.addActionListener(listener); } }
/** * Gets a string representation of the label. If the label * is a ValueBinding, the expression is evaluated and the string * value returned. */ public String getLabelAsString(FacesContext context) { Object label = getLabel(); if (label instanceof ValueExpression) { label = ((ValueExpression) label).getValue(context.getELContext()); } else if (label instanceof ValueBinding) { label = ((ValueBinding) label).getValue(context); } if (label == null) return null; return label.toString(); }
/** * Test returning an empty list as a bean property. */ @Test public void testBug51544Bean() throws Exception { ExpressionFactory factory = ExpressionFactory.newInstance(); ELContext context = new ELContextImpl(); TesterBeanA beanA = new TesterBeanA(); beanA.setValList(Collections.emptyList()); ValueExpression var = factory.createValueExpression(beanA, TesterBeanA.class); context.getVariableMapper().setVariable("beanA", var); ValueExpression ve = factory.createValueExpression( context, "${beanA.valList.size()}", Integer.class); Integer result = (Integer) ve.getValue(context); assertEquals(Integer.valueOf(0), result); }
/** * Given a ValueExpression <code>ve</code>, return a ValueBinding. * The returned ValueBinding will implement StateHolder and Serializable interfaces if * <code>ve</code> implements these interfaces. * @param ve The ValueExpression * @return a ValueBinding equivalent to the ValueExpression */ public static ValueBinding getValueBinding(ValueExpression ve) { // if we previously wrapped a ValueBinding, unwrap it and return it, otherwise create the // correct subclass of ValueBinding if (ve instanceof ValueBindingValueExpression) return ((ValueBindingValueExpression)ve).getValueBinding(); else if (ve instanceof StateHolder) { if (ve instanceof Serializable) return new SerializableStateHolderValueExpressionValueBinding(ve); else return new StateHolderValueExpressionValueBinding(ve); } else if (ve instanceof Serializable) { return new SerializableValueExpressionValueBinding(ve); } else { return new ValueExpressionValueBinding(ve); } }
@Override public Class<?> getType(EvaluationContext ctx) throws ELException { VariableMapper varMapper = ctx.getVariableMapper(); if (varMapper != null) { ValueExpression expr = varMapper.resolveVariable(this.image); if (expr != null) { return expr.getType(ctx.getELContext()); } } ctx.setPropertyResolved(false); Class<?> result = ctx.getELResolver().getType(ctx, null, this.image); if (!ctx.isPropertyResolved()) { throw new PropertyNotFoundException(MessageFactory.get("error.resolver.unhandled.null", this.image)); } return result; }
private String _processPassThroughAttributes(String name, UIComponent component) { if (component == null) { return name; } _passThroughAttributes = component.getPassThroughAttributes(false); if (_passThroughAttributes != null) { Object value = _passThroughAttributes.get(Renderer.PASSTHROUGH_RENDERER_LOCALNAME_KEY); if (value instanceof ValueExpression) { value = ((ValueExpression)value).getValue(FacesContext.getCurrentInstance().getELContext()); } if (value != null) { String elementName = value.toString(); if (!name.equals(elementName)) { name = elementName; } } } return name; }
@Override public void setBinding(ValueExpression valueExpression) throws JspException { this._binding = valueExpression; super.setBinding(valueExpression); }
private static void _assertIsValidCustomMessageType(Object customMessagePattern) { if (!(customMessagePattern instanceof ValueExpression || customMessagePattern instanceof String)) throw new IllegalArgumentException(_LOG.getMessage( "CUSTOM_MESSAGE_SHOULD_BE_VALUEBINDING_OR_STRING_TYPE")); }
@Override public ValueExpression createValueExpression(Object instance, Class<?> expectedType) { if (expectedType == null) { throw new NullPointerException(MessageFactory .get("error.value.expectedType")); } return new ValueExpressionLiteral(instance, expectedType); }
public Object getProxy() { FacesContext context = FacesContext.getCurrentInstance(); String viewId = _getViewId(context); if (viewId != null) { // Certain views have their own preferences // storage. Figure out which preferences // we are proxying. String preferencesExpression = null; if (viewId.indexOf("/email/") >= 0) preferencesExpression = "#{email.preferences}"; else if (viewId.indexOf("SkinDemo") >= 0) preferencesExpression = "#{sessionScope}"; else if (viewId.indexOf("accessibilityProfileDemo") >= 0) preferencesExpression = "#{accProfileDemo}"; if (preferencesExpression != null) { ValueExpression ve = context.getApplication().getExpressionFactory().createValueExpression(context.getELContext(), preferencesExpression, Object.class); return ve.getValue(context.getELContext()); } } return Collections.EMPTY_MAP; }
@Override public ValueExpression resolveVariable(String variable) { if (vars == null) { return null; } return vars.get(variable); }
@Override public ValueExpression setVariable(String variable, ValueExpression expression) { if (vars == null) vars = new HashMap<String, ValueExpression>(); return vars.put(variable, expression); }
@Test public void testInvokeWithSuper() { MethodExpression me = factory.createMethodExpression(context, "${beanA.setBean(beanBB)}", null , new Class<?>[] { TesterBeanB.class }); me.invoke(context, null); ValueExpression ve = factory.createValueExpression(context, "${beanA.bean.name}", String.class); Object r = ve.getValue(context); assertEquals("BB", r); }
@Test public void testBug49655() throws Exception { // This is the call the failed MethodExpression me = factory.createMethodExpression(context, "#{beanA.setName('New value')}", null, null); // The rest is to check it worked correctly me.invoke(context, null); ValueExpression ve = factory.createValueExpression(context, "#{beanA.name}", java.lang.String.class); assertEquals("New value", ve.getValue(context)); }
public void setValue(EvaluationContext ctx, Object value) throws ELException { VariableMapper varMapper = ctx.getVariableMapper(); if (varMapper != null) { ValueExpression expr = varMapper.resolveVariable(this.image); if (expr != null) { expr.setValue(ctx.getELContext(), value); return; } } ctx.setPropertyResolved(false); ctx.getELResolver().setValue(ctx, null, this.image, value); }
@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()); }