Example usage for javax.el ExpressionFactory createValueExpression

List of usage examples for javax.el ExpressionFactory createValueExpression

Introduction

In this page you can find the example usage for javax.el ExpressionFactory createValueExpression.

Prototype

public abstract ValueExpression createValueExpression(ELContext context, String expression,
        Class<?> expectedType);

Source Link

Document

Create a new value expression.

Usage

From source file:org.nuxeo.ecm.platform.forms.layout.facelets.FaceletHandlerHelper.java

/**
 * @since 6.0/*from   www.j a v a2  s  .c o m*/
 */
public static boolean isDevModeEnabled(FaceletContext ctx) {
    // avoid stack overflow when using layout tags within the dev
    // handler
    if (Framework.isDevModeSet()) {
        NuxeoLayoutManagerBean bean = lookupBean(ctx.getFacesContext());
        if (bean.isDevModeSet()) {
            ExpressionFactory eFactory = ctx.getExpressionFactory();
            ValueExpression disableDevAttr = eFactory.createValueExpression(ctx,
                    "#{" + DEV_MODE_DISABLED_VARIABLE + "}", Boolean.class);
            if (!Boolean.TRUE.equals(disableDevAttr.getValue(ctx))) {
                return true;
            }
        }
    }
    return false;
}

From source file:org.nuxeo.ecm.platform.forms.layout.facelets.plugins.TemplateWidgetTypeHandler.java

/**
 * Computes variables for rendering, making available the field values in templates using the format "field_0",
 * "field_1", etc. and also the widget properties using the format "widgetProperty_thePropertyName".
 *//*  w ww  . j a va 2 s. c  o m*/
protected Map<String, ValueExpression> getVariablesForRendering(FaceletContext ctx, FaceletHandlerHelper helper,
        Widget widget, String widgetTagConfigId, String template) {
    Map<String, ValueExpression> variables = new HashMap<String, ValueExpression>();
    ExpressionFactory eFactory = ctx.getExpressionFactory();

    FieldDefinition[] fieldDefs = widget.getFieldDefinitions();
    // expose field variables
    FieldDefinition firstField = null;
    if (fieldDefs != null && fieldDefs.length > 0) {
        for (int i = 0; i < fieldDefs.length; i++) {
            if (i == 0) {
                addFieldVariable(variables, ctx, widget, fieldDefs[i], null);
                firstField = fieldDefs[i];
            }
            addFieldVariable(variables, ctx, widget, fieldDefs[i], Integer.valueOf(i));
        }
    } else if (getBindValueIfNoFieldValue(widget)) {
        // expose value as first parameter
        addFieldVariable(variables, ctx, widget, null, null);
        addFieldVariable(variables, ctx, widget, null, Integer.valueOf(0));
    }

    // add binding "fieldOrValue" available since 5.6, in case template
    // widget is always supposed to bind value when no field is defined
    String computedValue = ValueExpressionHelper.createExpressionString(widget.getValueName(), firstField);
    variables.put(RenderVariables.widgetVariables.fieldOrValue.name(),
            eFactory.createValueExpression(ctx, computedValue, Object.class));

    // expose widget properties too
    WebLayoutManager layoutService = Framework.getService(WebLayoutManager.class);
    Map<String, ValueExpression> mappedExpressions = new HashMap<String, ValueExpression>();
    for (Map.Entry<String, Serializable> prop : widget.getProperties().entrySet()) {
        String key = prop.getKey();
        String name = RenderVariables.widgetVariables.widgetProperty.name() + "_" + key;
        String value;
        Serializable valueInstance = prop.getValue();
        if (!layoutService.referencePropertyAsExpression(key, valueInstance, widget.getType(),
                widget.getTypeCategory(), widget.getMode(), template)) {
            // FIXME: this will not be updated correctly using ajax
            value = (String) valueInstance;
        } else {
            // create a reference so that it's a real expression and it's
            // not kept (cached) in a component value on ajax refresh
            value = "#{" + RenderVariables.widgetVariables.widget.name() + ".properties." + key + "}";
        }
        ValueExpression ve = eFactory.createValueExpression(ctx, value, Object.class);
        variables.put(name, ve);
        mappedExpressions.put(key, ve);
    }
    variables.put(RenderVariables.widgetVariables.widgetProperties.name(),
            new MapValueExpression(mappedExpressions));
    // expose widget controls too
    for (Map.Entry<String, Serializable> ctrl : widget.getControls().entrySet()) {
        String key = ctrl.getKey();
        String name = RenderVariables.widgetVariables.widgetControl.name() + "_" + key;
        String value = "#{" + RenderVariables.widgetVariables.widget.name() + ".controls." + key + "}";
        variables.put(name, eFactory.createValueExpression(ctx, value, Object.class));
    }
    return variables;
}

From source file:org.nuxeo.ecm.platform.forms.layout.facelets.plugins.TemplateWidgetTypeHandler.java

protected void addFieldVariable(Map<String, ValueExpression> variables, FaceletContext ctx, Widget widget,
        FieldDefinition fieldDef, Integer index) {
    String computedName;/*from   w  ww  .  ja  va2  s  .c om*/
    if (index == null) {
        computedName = RenderVariables.widgetVariables.field.name();
    } else {
        computedName = RenderVariables.widgetVariables.field.name() + "_" + index;
    }
    String computedValue = ValueExpressionHelper.createExpressionString(widget.getValueName(), fieldDef);

    ExpressionFactory eFactory = ctx.getExpressionFactory();
    variables.put(computedName, eFactory.createValueExpression(ctx, computedValue, Object.class));
}

From source file:org.nuxeo.ecm.platform.ui.web.binding.alias.AliasTagHandler.java

protected boolean isAnchored(FaceletContext ctx) {
    if (cache != null && cache.getBoolean(ctx)) {
        return false;
    }//from  w  w  w  .ja  va 2s.c  om
    ExpressionFactory eFactory = ctx.getExpressionFactory();
    ValueExpression ve = eFactory.createValueExpression(ctx, "#{" + ANCHOR_ENABLED_VARIABLE + "}",
            Boolean.class);
    if (Boolean.TRUE.equals(ve.getValue(ctx))) {
        return true;
    }
    if (anchor != null) {
        return anchor.getBoolean(ctx);
    }
    return false;
}

From source file:org.nuxeo.ecm.platform.ui.web.binding.MetaValueExpression.java

@Override
public Object getValue(ELContext context) {
    ELContext nxcontext = getLocalContext(context);
    Object res = null;//from   ww w .  j av  a  2 s.co  m
    if (originalValueExpression != null) {
        res = originalValueExpression.getValue(nxcontext);
        if (res instanceof String) {
            String expression = (String) res;
            if (ComponentTagUtils.isValueReference(expression)) {
                FacesContext faces = FacesContext.getCurrentInstance();
                Application app = faces.getApplication();
                ExpressionFactory factory = app.getExpressionFactory();
                ValueExpression newExpr = factory.createValueExpression(nxcontext, expression, expectedType);
                try {
                    res = newExpr.getValue(nxcontext);
                } catch (ELException err) {
                    log.error("Error processing expression " + expression + ": " + err);
                    res = null;
                }
            } else {
                res = expression;
            }
        }
    }
    return res;
}

From source file:org.nuxeo.ecm.platform.ui.web.rest.services.URLPolicyServiceImpl.java

@Override
public void applyRequestParameters(FacesContext facesContext) {
    // try to set document view
    ExpressionFactory ef = facesContext.getApplication().getExpressionFactory();
    ELContext context = facesContext.getELContext();

    HttpServletRequest httpRequest = (HttpServletRequest) facesContext.getExternalContext().getRequest();

    URLPatternDescriptor pattern = getURLPatternDescriptor(httpRequest);
    if (pattern == null) {
        return;//from ww  w .  j  a  v a  2  s  . c om
    }

    DocumentView docView = getDocumentViewFromRequest(pattern.getName(), httpRequest);
    // pattern applies => document view will not be null
    if (docView != null) {
        String documentViewBinding = pattern.getDocumentViewBinding();
        if (documentViewBinding != null && !"".equals(documentViewBinding)) {
            // try to set it from custom mapping
            ValueExpression ve = ef.createValueExpression(context, pattern.getDocumentViewBinding(),
                    Object.class);
            ve.setValue(context, docView);
        }
    }

    Map<String, String> docViewParameters = null;
    if (docView != null) {
        docViewParameters = docView.getParameters();
    }
    ValueBindingDescriptor[] bindings = pattern.getValueBindings();
    if (bindings != null && httpRequest.getAttribute(URLPolicyService.DISABLE_ACTION_BINDING_KEY) == null) {
        for (ValueBindingDescriptor binding : bindings) {
            if (!binding.getCallSetter()) {
                continue;
            }
            String paramName = binding.getName();
            // try doc view parameters
            Object value = null;
            if (docViewParameters != null && docViewParameters.containsKey(paramName)) {
                value = docView.getParameter(paramName);
            } else {
                // try request attributes
                value = httpRequest.getAttribute(paramName);
            }
            String expr = binding.getExpression();
            if (ComponentTagUtils.isValueReference(expr)) {
                ValueExpression ve = ef.createValueExpression(context, expr, Object.class);
                try {
                    ve.setValue(context, value);
                } catch (ELException e) {
                    log.error("Could not apply request parameter '" + value + "' to expression '" + expr + "'",
                            e);
                }
            }
        }
    }
}

From source file:org.nuxeo.ecm.platform.ui.web.rest.services.URLPolicyServiceImpl.java

public void appendParametersToRequest(FacesContext facesContext, String pattern) {
    // try to get doc view from custom mapping
    DocumentView docView = null;/*from   w w  w.  j av  a  2s. c om*/
    ExpressionFactory ef = facesContext.getApplication().getExpressionFactory();
    ELContext context = facesContext.getELContext();
    HttpServletRequest httpRequest = (HttpServletRequest) facesContext.getExternalContext().getRequest();

    // get existing document view from given pattern, else create it
    URLPatternDescriptor patternDesc = null;
    if (pattern != null && !"".equals(pattern)) {
        patternDesc = getURLPatternDescriptor(pattern);
    } else {
        // iterate over pattern descriptors, and take the first one that
        // applies, or use the default one
        List<URLPatternDescriptor> descs = getURLPatternDescriptors();
        boolean applies = false;
        for (URLPatternDescriptor desc : descs) {
            String documentViewAppliesExpr = desc.getDocumentViewBindingApplies();
            if (!StringUtils.isBlank(documentViewAppliesExpr)) {
                // TODO: maybe put view id to the request to help writing
                // the EL expression
                ValueExpression ve = ef.createValueExpression(context, documentViewAppliesExpr, Object.class);
                try {
                    Object res = ve.getValue(context);
                    if (Boolean.TRUE.equals(res)) {
                        applies = true;
                    }
                } catch (ELException e) {
                    if (log.isDebugEnabled()) {
                        log.debug(String.format("Error executing expression '%s' for " + "url pattern '%s': %s",
                                documentViewAppliesExpr, desc.getName(), e.getMessage()));
                    }
                }
            }
            if (applies) {
                patternDesc = desc;
                break;
            }
        }
        if (patternDesc == null) {
            // default on the default pattern desc
            patternDesc = getDefaultPatternDescriptor();
        }
    }
    if (patternDesc != null) {
        // resolved doc view values thanks to bindings
        Object docViewValue = null;
        String documentViewBinding = patternDesc.getDocumentViewBinding();
        if (!StringUtils.isBlank(documentViewBinding)) {
            ValueExpression ve = ef.createValueExpression(context, documentViewBinding, Object.class);
            docViewValue = ve.getValue(context);
        }
        if (docViewValue == null) {
            documentViewBinding = patternDesc.getNewDocumentViewBinding();
            if (!StringUtils.isBlank(documentViewBinding)) {
                ValueExpression ve = ef.createValueExpression(context, documentViewBinding, Object.class);
                docViewValue = ve.getValue(context);
            }
        }
        if (docViewValue instanceof DocumentView) {
            docView = (DocumentView) docViewValue;
            // set pattern name in case it was just created
            docView.setPatternName(patternDesc.getName());
            ValueBindingDescriptor[] bindings = patternDesc.getValueBindings();
            if (bindings != null) {
                for (ValueBindingDescriptor binding : bindings) {
                    if (!binding.getCallGetter()) {
                        continue;
                    }
                    String paramName = binding.getName();
                    String expr = binding.getExpression();
                    try {
                        Object value;
                        if (ComponentTagUtils.isValueReference(expr)) {
                            ValueExpression ve = ef.createValueExpression(context, expr, Object.class);
                            value = ve.getValue(context);
                        } else {
                            value = expr;
                        }
                        if (docView != null) {
                            // do not set attributes on the request as
                            // document view will be put in the request
                            // anyway
                            docView.addParameter(paramName, (String) value);
                        } else {
                            httpRequest.setAttribute(paramName, value);
                        }
                    } catch (ELException e) {
                        log.error(
                                String.format("Could not get parameter %s from expression %s", paramName, expr),
                                e);
                    }
                }
            }
        }
    }

    // save document view to the request
    setDocumentViewInRequest(httpRequest, docView);
}

From source file:org.nuxeo.ecm.platform.ui.web.util.ComponentTagUtils.java

/**
 * Resolves given value expression as string and sets given value on it.
 *
 * @since 6.0/*from   w  w w.j ava 2  s .co m*/
 */
public static void applyValueExpression(FacesContext context, String elExpression, Object value) {
    if (!isStrictValueReference(elExpression)) {
        log.warn("Cannot set value '" + value + "' for expression '" + elExpression + "'");
    } else {
        if (context == null) {
            log.error("FacesContext is null => cannot resolve el expression '" + elExpression + "'");
            return;
        }
        Application app = context.getApplication();
        ExpressionFactory eFactory = app.getExpressionFactory();
        ELContext elContext = context.getELContext();
        try {
            ValueExpression vExpression = eFactory.createValueExpression(elContext, elExpression, Object.class);
            vExpression.setValue(elContext, value);
        } catch (ELException e) {
            log.error("Error setting value '" + value + "' for expression '" + elExpression + "'", e);
        }
    }
}

From source file:org.nuxeo.ecm.platform.ui.web.util.ComponentTagUtils.java

/**
 * Resolves an expression from a given facelet context, using its {@link ExpressionFactory} that can hold a wider
 * context than the faces context behind it.
 * <p>//from   w  w  w .  j  a  v a 2s.  c o m
 * Resolves the expression a second time when first resolution gives a String value using the EL Expression syntax.
 * <p>
 * Does not throw any error when resolution fails (only logs an error message).
 */
public static Object resolveElExpression(FaceletContext faceletContext, String elExpression) {
    if (!isValueReference(elExpression)) {
        // literal
        return elExpression;
    } else {
        if (faceletContext == null) {
            log.error("FaceletContext is null => cannot resolve el expression '" + elExpression + "'");
            return null;
        }
        // expression => evaluate
        ExpressionFactory eFactory = faceletContext.getExpressionFactory();
        ELContext elContext = faceletContext.getFacesContext().getELContext();
        ValueExpression expr = eFactory.createValueExpression(faceletContext, elExpression, Object.class);
        try {
            return expr.getValue(elContext);
        } catch (ELException e) {
            log.error("Facelet context: Error processing expression '" + elExpression + "'", e);
            return null;
        }
    }
}

From source file:org.richfaces.component.UIScrollableDataTable.java

private List<SortField2> getSortFields() {
    FacesContext context = FacesContext.getCurrentInstance();
    ELContext eLContext = context.getELContext();
    ExpressionFactory expressionFactory = context.getApplication().getExpressionFactory();
    String var = getVar();
    SortOrder sortOrder = getSortOrder();
    List<SortField2> sortFields2 = null;
    if (sortOrder != null) {
        SortField[] sortFields = sortOrder.getFields();
        sortFields2 = new LinkedList<SortField2>();
        for (SortField sortField : sortFields) {
            ValueExpression valueExpression = null;
            String name = sortField.getName();
            if (ELUtils.isValueReference(name)) {
                valueExpression = expressionFactory.createValueExpression(eLContext, name, Object.class);
            } else if (!name.startsWith(UIViewRoot.UNIQUE_ID_PREFIX)) {
                valueExpression = expressionFactory.createValueExpression(eLContext,
                        "#{" + var + "." + name + "}", Object.class);
            }//from   w  ww  .j ava  2 s. c  o  m
            Ordering ordering = Ordering.UNSORTED;
            Boolean ascending = sortField.getAscending();
            if (ascending != null) {
                if (ascending) {
                    ordering = Ordering.ASCENDING;
                } else {
                    ordering = Ordering.DESCENDING;
                }
            }
            if (valueExpression != null && !Ordering.UNSORTED.equals(ordering)) {
                sortFields2.add(new SortField2(valueExpression, ordering));
            }
        }
    }
    return sortFields2;
}