Example usage for javax.el VariableMapper setVariable

List of usage examples for javax.el VariableMapper setVariable

Introduction

In this page you can find the example usage for javax.el VariableMapper setVariable.

Prototype

public abstract ValueExpression setVariable(String variable, ValueExpression expression);

Source Link

Usage

From source file:cherry.foundation.code.CodeTagSupport.java

@Override
protected int doStartTagInternal() throws Exception {
    CodeManager codeManager = getCodeManager();
    T object = getObject(codeManager, codeName, value, plainLabel);
    if (StringUtils.isNotEmpty(var)) {
        VariableMapper vm = pageContext.getELContext().getVariableMapper();
        if (vm != null) {
            vm.setVariable(var, null);
        }//  w  ww. j  a v a2  s.c  o  m
        pageContext.setAttribute(var, object, PageContext.PAGE_SCOPE);
    }
    if (doesEvalBody(codeManager, codeName, value, plainLabel)) {
        return EVAL_BODY_INCLUDE;
    } else {
        return SKIP_BODY;
    }
}

From source file:com.flexive.faces.components.content.FxValueHandler.java

private void assignAttribute(FaceletContext ctx, VariableMapper mapper, String name, Class<?> expectedClass) {
    final TagAttribute attribute = getAttribute(name);
    if (attribute != null) {
        mapper.setVariable(name, attribute.getValueExpression(ctx, expectedClass));
    }//from   w  ww.  ja v  a2  s.c  om
}

From source file:org.nuxeo.ecm.platform.actions.ELActionContext.java

@Override
public boolean checkCondition(String expression) throws ELException {
    if (StringUtils.isBlank(expression) || (expression != null && StringUtils.isBlank(expression.trim()))) {
        return false;
    }//from   w  w w. j ava  2 s  . c  o m
    String expr = expression.trim();
    // compatibility code, as JEXL could resolve that kind of expression:
    // detect if expression is in brackets #{}, otherwise add it
    if (!expr.startsWith("#{") && !expr.startsWith("${")
    // don't confuse error messages in case of simple mistakes in the
    // expression
            && !expr.endsWith("}")) {
        expr = "#{" + expr + "}";
    }
    VariableMapper vm = originalContext.getVariableMapper();
    // init default variables
    ValueExpression documentExpr = expressionFactory.createValueExpression(getCurrentDocument(),
            DocumentModel.class);
    ValueExpression userExpr = expressionFactory.createValueExpression(getCurrentPrincipal(),
            NuxeoPrincipal.class);
    // add variables originally exposed by the action framework,
    // do not add aliases currentDocument and currentUser here as they
    // should already be available in this JSF context
    vm.setVariable("document", documentExpr);
    vm.setVariable("principal", userExpr);
    vm.setVariable("currentDocument", documentExpr);
    vm.setVariable("currentUser", userExpr);
    // get custom context from ActionContext
    for (String key : localVariables.keySet()) {
        vm.setVariable(key, expressionFactory.createValueExpression(getLocalVariable(key), Object.class));
    }

    // evaluate expression
    ValueExpression ve = expressionFactory.createValueExpression(originalContext, expr, Boolean.class);
    return Boolean.TRUE.equals(ve.getValue(originalContext));
}

From source file:org.nuxeo.ecm.platform.actions.jsf.JSFActionContext.java

@Override
public boolean checkCondition(String expression) throws ELException {
    if (StringUtils.isBlank(expression) || (expression != null && StringUtils.isBlank(expression.trim()))) {
        return false;
    }//from w w  w  .j av  a 2s.c  o m
    String expr = expression.trim();
    // compatibility code, as JEXL could resolve that kind of expression:
    // detect if expression is in brackets #{}, otherwise add it
    if (!expr.startsWith("#{") && !expr.startsWith("${")
    // don't confuse error messages in case of simple mistakes in the
    // expression
            && !expr.endsWith("}")) {
        expr = "#{" + expr + "}";
    }
    ELContext finalContext = new JSFELContext(originalContext);
    VariableMapper vm = finalContext.getVariableMapper();
    // init default variables
    ValueExpression documentExpr = expressionFactory.createValueExpression(getCurrentDocument(),
            DocumentModel.class);
    ValueExpression userExpr = expressionFactory.createValueExpression(getCurrentPrincipal(),
            NuxeoPrincipal.class);
    vm.setVariable("actionContextDocument", documentExpr);
    // add variables originally exposed by the action framework,
    // do not add aliases currentDocument and currentUser here as they
    // should already be available in this JSF context
    vm.setVariable("document", documentExpr);
    vm.setVariable("principal", userExpr);
    // get custom context from ActionContext
    for (String key : localVariables.keySet()) {
        vm.setVariable(key, expressionFactory.createValueExpression(getLocalVariable(key), Object.class));
    }
    // expose Seam context for compatibility, although available its
    // components should be natively exposed in this JSF context
    putLocalVariable("SeamContext", new SeamContextHelper());

    // evaluate expression
    ValueExpression ve = expressionFactory.createValueExpression(finalContext, expr, Boolean.class);
    return Boolean.TRUE.equals(ve.getValue(finalContext));
}

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

/**
 * Compatibility methods/*from   w  w w  .  j  ava2 s .c  o  m*/
 */

@SuppressWarnings("unchecked")
public void applyCompat(FaceletContext ctx, UIComponent parent)
        throws IOException, FacesException, ELException {
    long start = FaceletDebugTracer.start();
    String logId = null;
    try {

        WebLayoutManager layoutService = Framework.getService(WebLayoutManager.class);

        // add additional properties put on tag
        Map<String, Serializable> additionalProps = new HashMap<String, Serializable>();
        List<String> reservedVars = Arrays.asList(reservedVarsArray);
        for (TagAttribute var : vars) {
            String localName = var.getLocalName();
            if (!reservedVars.contains(localName)) {
                // resolve value as there's no alias value expression exposed
                // for layout properties
                additionalProps.put(localName, (Serializable) var.getObject(ctx));
            }
        }

        // expose some layout variables before layout creation so that they
        // can be used in mode expressions
        VariableMapper orig = ctx.getVariableMapper();

        FaceletHandlerHelper helper = new FaceletHandlerHelper(config);
        try {
            VariableMapper vm = new VariableMapperWrapper(orig);
            ctx.setVariableMapper(vm);

            Layout layoutInstance = null;

            String valueName = value.getValue();
            if (ComponentTagUtils.isStrictValueReference(valueName)) {
                valueName = ComponentTagUtils.getBareValueName(valueName);
            }

            String templateValue = null;
            if (template != null) {
                templateValue = template.getValue(ctx);
            }

            boolean resolveOnlyValue = false;
            if (resolveOnly != null) {
                resolveOnlyValue = resolveOnly.getBoolean(ctx);
            }

            if (layout != null) {
                // resolve layout instance given as attribute
                layoutInstance = (Layout) layout.getObject(ctx, Layout.class);
                if (layoutInstance == null) {
                    String errMsg = "Layout instance not found";
                    applyErrorHandler(ctx, parent, helper, errMsg);
                } else {
                    Map<String, ValueExpression> vars = getVariablesForLayoutBuild(ctx,
                            layoutInstance.getMode());
                    for (Map.Entry<String, ValueExpression> var : vars.entrySet()) {
                        vm.setVariable(var.getKey(), var.getValue());
                    }
                    layoutInstance.setValueName(valueName);
                    applyCompatLayoutHandler(ctx, parent, helper, layoutService, layoutInstance, templateValue,
                            additionalProps, vars, resolveOnlyValue);
                }
            } else {
                // build layout instance from other attributes
                String modeValue = mode.getValue(ctx);

                List<String> selectedRowsValue = null;
                boolean selectAllByDefaultValue = false;

                Map<String, ValueExpression> vars = getVariablesForLayoutBuild(ctx, modeValue);
                for (Map.Entry<String, ValueExpression> var : vars.entrySet()) {
                    vm.setVariable(var.getKey(), var.getValue());
                }

                if (selectedRows != null || selectedColumns != null) {
                    if (selectedRows != null) {
                        selectedRowsValue = (List<String>) selectedRows.getObject(ctx, List.class);
                    } else if (selectedColumns != null) {
                        List<String> selectedColumnsList = (List<String>) selectedColumns.getObject(ctx,
                                List.class);
                        // Handle empty selected columns list as null to
                        // display all columns.
                        if (selectedColumnsList != null && selectedColumnsList.isEmpty()) {
                            selectedColumnsList = null;
                        }
                        selectedRowsValue = selectedColumnsList;
                    }
                }
                if (selectAllByDefault != null) {
                    selectAllByDefaultValue = selectAllByDefault.getBoolean(ctx);
                }

                if (name != null) {
                    String layoutCategory = null;
                    if (category != null) {
                        layoutCategory = category.getValue(ctx);
                    }

                    String nameValue = name.getValue(ctx);
                    List<String> layoutNames = resolveLayoutNames(nameValue);
                    logId = layoutNames.toString();
                    for (String layoutName : layoutNames) {
                        layoutInstance = layoutService.getLayout(ctx, layoutName, layoutCategory, modeValue,
                                valueName, selectedRowsValue, selectAllByDefaultValue);
                        if (layoutInstance == null) {
                            String errMsg = "Layout '" + layoutName + "' not found";
                            applyErrorHandler(ctx, parent, helper, errMsg);
                        } else {
                            applyCompatLayoutHandler(ctx, parent, helper, layoutService, layoutInstance,
                                    templateValue, additionalProps, vars, resolveOnlyValue);
                        }
                    }
                }

                if (definition != null) {
                    LayoutDefinition layoutDef = (LayoutDefinition) definition.getObject(ctx,
                            LayoutDefinition.class);

                    if (layoutDef == null) {
                        String errMsg = "Layout definition resolved to null";
                        applyErrorHandler(ctx, parent, helper, errMsg);
                    } else {
                        layoutInstance = layoutService.getLayout(ctx, layoutDef, modeValue, valueName,
                                selectedRowsValue, selectAllByDefaultValue);
                        applyCompatLayoutHandler(ctx, parent, helper, layoutService, layoutInstance,
                                templateValue, additionalProps, vars, resolveOnlyValue);
                        if (layoutInstance != null) {
                            logId = layoutInstance.getId();
                        } else {
                            logId = layoutDef.getName() + " (def)";
                        }
                    }
                }
            }

        } finally {
            // layout resolved => cleanup variable mapper
            ctx.setVariableMapper(orig);
        }

    } finally {
        FaceletDebugTracer.trace(start, config.getTag(), logId);
    }
}

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

protected void applyCompat(FaceletContext ctx, VariableMapper orig, Widget widgetInstance,
        boolean widgetInstanceBuilt) {
    if (!widgetInstanceBuilt) {
        return;//from  ww w  . ja  v  a2s  .  co m
    }
    // expose widget variable to the context as layout row has not done it already, and set unique id on
    // widget before exposing it to the context
    FaceletHandlerHelper helper = new FaceletHandlerHelper(config);
    WidgetTagHandler.generateWidgetId(ctx, helper, widgetInstance, false);

    VariableMapper vm = new VariableMapperWrapper(orig);
    ctx.setVariableMapper(vm);
    ExpressionFactory eFactory = ctx.getExpressionFactory();
    ValueExpression widgetVe = eFactory.createValueExpression(widgetInstance, Widget.class);
    vm.setVariable(RenderVariables.widgetVariables.widget.name(), widgetVe);
    // expose widget controls too
    for (Map.Entry<String, Serializable> ctrl : widgetInstance.getControls().entrySet()) {
        String key = ctrl.getKey();
        String name = RenderVariables.widgetVariables.widgetControl.name() + "_" + key;
        String value = "#{" + RenderVariables.widgetVariables.widget.name() + ".controls." + key + "}";
        vm.setVariable(name, eFactory.createValueExpression(ctx, value, Object.class));
    }
}

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

protected void applyCompat(FaceletContext ctx, UIComponent parent, Widget widget)
        throws IOException, ELException {
    // expose widget variable
    VariableMapper orig = ctx.getVariableMapper();
    VariableMapper vm = new VariableMapperWrapper(orig);
    ctx.setVariableMapper(vm);/*w  ww  . j a  v a  2 s .  c  om*/
    ValueExpression widgetVe = ctx.getExpressionFactory().createValueExpression(widget, Widget.class);
    vm.setVariable(RenderVariables.widgetVariables.widget.name(), widgetVe);
    vm.setVariable(RenderVariables.widgetVariables.widget.name() + "_" + widget.getLevel(), widgetVe);
    // TODO NXP-13280: expose widget controls too when they can be
    // retrieved from tag attributes
    try {
        // set unique id on widget before exposing it to the context
        FaceletHandlerHelper helper = new FaceletHandlerHelper(config);
        WidgetTagHandler.generateWidgetId(ctx, helper, widget, false);

        boolean resolveOnlyBool = false;
        if (resolveOnly != null) {
            resolveOnlyBool = resolveOnly.getBoolean(ctx);
        }
        if (resolveOnlyBool) {
            // NXP-12882: wrap handler in an nxu:set tag to avoid duplicate
            // id issue when widget definition changes, as component ids
            // can be cached and not generated-again on ajax re-render,
            // this is a quick fix that can be optimized, as the widget
            // variable is already exposed in the current variable mapper.
            // Update after NXP-15050: this does not seem to be necessary
            // anymore, could not reproduce the corresponding bug, to
            // remove after complementary tests.
            String setTagConfigId = widget.getTagConfigId();
            ComponentConfig aliasConfig = org.nuxeo.ecm.platform.ui.web.tag.handler.TagConfigFactory
                    .createAliasTagConfig(this.config, setTagConfigId,
                            RenderVariables.widgetVariables.widget.name(), "#{widget}", "true", "true",
                            nextHandler);
            FaceletHandler handler = new SetTagHandler(aliasConfig);
            handler.apply(ctx, parent);
        } else {
            WidgetTagHandler.applyWidgetHandler(ctx, parent, config, widget, value, true, nextHandler);
        }
    } finally {
        ctx.setVariableMapper(orig);
    }
}

From source file:org.nuxeo.ecm.platform.forms.layout.service.WebLayoutManagerImpl.java

/**
 * Computes a widget from a definition for a mode in a given context.
 * <p>//from w  ww. j  a  va  2  s  .c om
 * If the widget is configured not to be rendered in the given mode, returns null.
 * <p>
 * Sub widgets are also computed recursively.
 */
@SuppressWarnings("deprecation")
protected Widget getWidget(FaceletContext context, LayoutConversionContext lctx, String conversionCat,
        String layoutName, LayoutDefinition layoutDef, WidgetDefinition widgetDefinition, String widgetCategory,
        String layoutMode, String valueName, int level) {
    if (widgetDefinition == null) {
        return null;
    }
    WidgetDefinition wDef = widgetDefinition.clone();
    if (lctx != null && !StringUtils.isBlank(conversionCat)) {
        List<WidgetDefinitionConverter> lcs = getLayoutStore().getWidgetConverters(conversionCat);
        for (WidgetDefinitionConverter wc : lcs) {
            wDef = wc.getWidgetDefinition(wDef, lctx);
        }
    }
    VariableMapper orig = null;
    // avoid variable mapper changes if context is null for tests
    if (context != null) {
        // expose widget mode so that it can be used in a mode el
        // expression
        orig = context.getVariableMapper();
        VariableMapper vm = new VariableMapperWrapper(orig);
        context.setVariableMapper(vm);
        ExpressionFactory eFactory = context.getExpressionFactory();
        ValueExpression modeVe = eFactory.createValueExpression(layoutMode, String.class);
        vm.setVariable(RenderVariables.globalVariables.mode.name(), modeVe);
    }
    String wMode = getModeFromLayoutMode(context, wDef, layoutMode);
    if (context != null) {
        context.setVariableMapper(orig);
    }

    if (BuiltinWidgetModes.HIDDEN.equals(wMode)) {
        return null;
    }
    List<Widget> subWidgets = new ArrayList<Widget>();
    WidgetDefinition[] swDefs = wDef.getSubWidgetDefinitions();
    if (swDefs != null) {
        for (WidgetDefinition swDef : swDefs) {
            Widget subWidget = getWidget(context, lctx, conversionCat, layoutName, layoutDef, swDef,
                    widgetCategory, wMode, valueName, level + 1);
            if (subWidget != null) {
                subWidgets.add(subWidget);
            }
        }
    }

    WidgetReference[] swRefs = wDef.getSubWidgetReferences();
    if (swRefs != null) {
        for (WidgetReference swRef : swRefs) {
            String cat = swRef.getCategory();
            if (StringUtils.isBlank(cat)) {
                cat = widgetCategory;
            }
            WidgetDefinition swDef = lookupWidget(layoutDef, new WidgetReferenceImpl(cat, swRef.getName()));
            if (swDef == null) {
                log.error("Widget '" + swRef.getName() + "' not found in layout " + layoutName);
            } else {
                Widget subWidget = getWidget(context, lctx, conversionCat, layoutName, layoutDef, swDef, cat,
                        wMode, valueName, level + 1);
                if (subWidget != null) {
                    subWidgets.add(subWidget);
                }
            }
        }
    }

    boolean required = getBooleanValue(context, wDef.getRequired(layoutMode, wMode)).booleanValue();

    String wType = wDef.getType();
    String wTypeCat = wDef.getTypeCategory();
    // fill default property and control values from the widget definition
    Map<String, Serializable> props = new HashMap<String, Serializable>();
    Map<String, Serializable> controls = new HashMap<String, Serializable>();
    String actualWTypeCat = getStoreCategory(wTypeCat);
    WidgetTypeDefinition def = getLayoutStore().getWidgetTypeDefinition(actualWTypeCat, wType);

    WidgetTypeConfiguration conf = def != null ? def.getConfiguration() : null;
    if (conf != null) {
        Map<String, Serializable> defaultProps = conf.getDefaultPropertyValues(wMode);
        if (defaultProps != null && !defaultProps.isEmpty()) {
            props.putAll(defaultProps);
        }
        Map<String, Serializable> defaultControls = conf.getDefaultControlValues(wMode);
        if (defaultControls != null && !defaultControls.isEmpty()) {
            controls.putAll(defaultControls);
        }
    }

    props.putAll(wDef.getProperties(layoutMode, wMode));
    controls.putAll(wDef.getControls(layoutMode, wMode));

    WidgetImpl widget = new WidgetImpl(layoutName, wDef.getName(), wMode, wType, valueName,
            wDef.getFieldDefinitions(), wDef.getLabel(layoutMode), wDef.getHelpLabel(layoutMode),
            wDef.isTranslated(), wDef.isHandlingLabels(), props, required, subWidgets.toArray(new Widget[0]),
            level, wDef.getSelectOptions(), LayoutFunctions.computeWidgetDefinitionId(wDef),
            wDef.getRenderingInfos(layoutMode));
    widget.setControls(controls);
    widget.setTypeCategory(actualWTypeCat);
    if (Framework.isDevModeSet()) {
        widget.setDefinition(wDef);
    }
    return widget;
}

From source file:org.richfaces.taglib.ColumnsHandler.java

/**
 * Sets page request variables//from   ww w  . ja va  2  s  .  c  o m
 * 
 * @throws JspTagException
 */
private void exposeVariables(FaceletContext ctx) {
    IterationContext itContext = getIterationContext();
    VariableMapper vm = ctx.getVariableMapper();
    int k = itContext._index;

    if (itContext._itemId != null) {
        if (vm != null) {
            if (value != null) {
                ValueExpression srcVE = value.getValueExpression(ctx, Object.class);
                ValueExpression ve = getVarExpression(ctx, srcVE);
                vm.setVariable(itContext._itemId, ve);
            }
        }

    }

    // Set up index variable

    if (itContext._indexId != null) {
        if (vm != null) {
            ValueExpression ve = new IteratedIndexExpression(k);
            vm.setVariable(itContext._indexId, ve);
        }

    }

    int componentsCount = itContext._index - itContext._begin;
    if (componentsCount != 0) {
        ValueExpression ve = ctx.getExpressionFactory()
                .createValueExpression(UIViewRoot.UNIQUE_ID_PREFIX + componentsCount, String.class);
        vm.setVariable(ITERATION_INDEX_VARIABLE, ve);
    }
}

From source file:org.richfaces.taglib.ColumnsHandler.java

/**
 * Removes page attributes that we have exposed and, if applicable, restores
 * them to their prior values (and scopes).
 *///w w w.  j ava  2 s .c o  m
private void unExposeVariables(FaceletContext ctx) {
    IterationContext itContext = getIterationContext();
    VariableMapper vm = ctx.getVariableMapper();
    // "nested" variables are now simply removed
    if (itContext._itemId != null) {
        if (vm != null)
            vm.setVariable(itContext._itemId, null);
    }
    if (itContext._indexId != null) {
        if (vm != null)
            vm.setVariable(itContext._indexId, null);
    }

    vm.setVariable(ITERATION_INDEX_VARIABLE, null);
}