List of usage examples for javax.el ExpressionFactory createValueExpression
public abstract ValueExpression createValueExpression(Object instance, Class<?> expectedType);
From source file:org.nuxeo.ecm.platform.forms.layout.facelets.LayoutTagHandler.java
protected void applyCompatLayoutHandler(FaceletContext ctx, UIComponent parent, FaceletHandlerHelper helper, WebLayoutManager layoutService, Layout layoutInstance, String templateValue, Map<String, Serializable> additionalProps, Map<String, ValueExpression> vars, boolean resolveOnly) throws IOException, FacesException, ELException { // set unique id on layout, unless layout is only resolved if (!resolveOnly) { layoutInstance.setId(FaceletHandlerHelper.generateLayoutId(ctx, layoutInstance.getName())); }//from w w w . j ava 2 s . c om // add additional properties put on tag Map<String, Serializable> layoutProps = layoutInstance.getProperties(); if (additionalProps != null && !additionalProps.isEmpty()) { for (Map.Entry<String, Serializable> entry : additionalProps.entrySet()) { // XXX: do not override with empty property values if already // set on the layout properties String key = entry.getKey(); Serializable value = entry.getValue(); if (layoutProps.containsKey(key) && (value == null || ((value instanceof String) && StringUtils.isBlank((String) value)))) { // do not override property on layout if (log.isDebugEnabled()) { log.debug(String.format( "Do not override property '%s' with " + "empty value on layout named '%s'", key, layoutInstance.getName())); } } else { layoutInstance.setProperty(key, value); } } } if (StringUtils.isBlank(templateValue)) { templateValue = layoutInstance.getTemplate(); } if (!resolveOnly) { boolean scaffold = Boolean.parseBoolean(String.valueOf(layoutInstance.getProperty("scaffold"))); if (scaffold) { // generate ids on widgets Map<String, Widget> widgetMap = layoutInstance.getWidgetMap(); if (widgetMap != null) { for (Widget widget : widgetMap.values()) { if (widget != null && (widget.getId() == null)) { WidgetTagHandler.generateWidgetId(ctx, helper, widget, false); } } } } } // expose layout instance to variable mapper to ensure good // resolution of properties ExpressionFactory eFactory = ctx.getExpressionFactory(); ValueExpression layoutVe = eFactory.createValueExpression(layoutInstance, Layout.class); ctx.getVariableMapper().setVariable(RenderVariables.layoutVariables.layout.name(), layoutVe); // expose all variables through an alias tag handler vars.putAll(getVariablesForLayoutRendering(ctx, layoutService, layoutInstance)); List<String> blockedPatterns = new ArrayList<String>(); blockedPatterns.add(RenderVariables.layoutVariables.layout.name()); blockedPatterns.add(RenderVariables.layoutVariables.layoutProperty.name() + "_*"); final String layoutTagConfigId = layoutInstance.getTagConfigId(); if (resolveOnly) { FaceletHandler handler = helper.getAliasFaceletHandler(layoutTagConfigId, vars, blockedPatterns, nextHandler); // apply handler.apply(ctx, parent); } else { if (!StringUtils.isBlank(templateValue)) { TagAttribute srcAttr = helper.createAttribute("template", templateValue); TagConfig config = TagConfigFactory.createTagConfig(this.config, layoutTagConfigId, FaceletHandlerHelper.getTagAttributes(srcAttr), nextHandler); FaceletHandler includeHandler = new DecorateHandler(config); FaceletHandler handler; if (FaceletHandlerHelper.isDevModeEnabled(ctx)) { // decorate handler with dev handler FaceletHandler devHandler = getDevFaceletHandler(ctx, helper, config, layoutInstance); FaceletHandler nextHandler; if (devHandler == null) { nextHandler = includeHandler; } else { nextHandler = new DevTagHandler(config, layoutInstance.getName(), includeHandler, devHandler); } handler = helper.getAliasFaceletHandler(layoutTagConfigId, vars, blockedPatterns, nextHandler); } else { handler = helper.getAliasFaceletHandler(layoutTagConfigId, vars, blockedPatterns, includeHandler); } // apply handler.apply(ctx, parent); } else { String errMsg = "Missing template property for layout '" + layoutInstance.getName() + "'"; applyErrorHandler(ctx, parent, helper, errMsg); } } }
From source file:org.nuxeo.ecm.platform.forms.layout.facelets.LayoutTagHandler.java
protected Map<String, ValueExpression> getVariablesForLayoutBuild(FaceletContext ctx, String modeValue) { Map<String, ValueExpression> vars = new HashMap<String, ValueExpression>(); ValueExpression valueExpr = value.getValueExpression(ctx, Object.class); vars.put(RenderVariables.globalVariables.value.name(), valueExpr); // vars.put(RenderVariables.globalVariables.document.name(), // valueExpr); vars.put(RenderVariables.globalVariables.layoutValue.name(), valueExpr); ExpressionFactory eFactory = ctx.getExpressionFactory(); ValueExpression modeVe = eFactory.createValueExpression(modeValue, String.class); vars.put(RenderVariables.globalVariables.layoutMode.name(), modeVe); // mode as alias to layoutMode vars.put(RenderVariables.globalVariables.mode.name(), modeVe); return vars;// www . ja v a2 s .co m }
From source file:org.nuxeo.ecm.platform.forms.layout.facelets.LayoutTagHandler.java
/** * Computes variables for rendering, making available the layout instance and its properties to the context. */// w w w. j a v a 2 s . c o m protected Map<String, ValueExpression> getVariablesForLayoutRendering(FaceletContext ctx, WebLayoutManager layoutService, Layout layoutInstance) { Map<String, ValueExpression> vars = new HashMap<String, ValueExpression>(); ExpressionFactory eFactory = ctx.getExpressionFactory(); // expose layout value ValueExpression layoutVe = eFactory.createValueExpression(layoutInstance, Layout.class); vars.put(RenderVariables.layoutVariables.layout.name(), layoutVe); // expose layout properties too for (Map.Entry<String, Serializable> prop : layoutInstance.getProperties().entrySet()) { String key = prop.getKey(); String name = RenderVariables.layoutVariables.layoutProperty.name() + "_" + key; String value; Serializable valueInstance = prop.getValue(); if (!layoutService.referencePropertyAsExpression(key, valueInstance, null, null, null, null)) { // 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.layoutVariables.layout.name() + ".properties." + key + "}"; } vars.put(name, eFactory.createValueExpression(ctx, value, Object.class)); } return vars; }
From source file:org.nuxeo.ecm.platform.forms.layout.facelets.SubWidgetTagHandler.java
protected void applyCompat(FaceletContext ctx, UIComponent parent, Widget[] subWidgets, FaceletHandlerHelper helper, boolean recomputeIdsBool) throws IOException, FacesException, ELException { int subWidgetCounter = 0; for (Widget subWidget : subWidgets) { // set unique id on widget before exposing it to the context, but assumes iteration could be done several // times => do not generate id again if already set, unless specified by attribute "recomputeIds" if (subWidget != null && (subWidget.getId() == null || recomputeIdsBool)) { WidgetTagHandler.generateWidgetId(ctx, helper, subWidget, false); }/* w ww .j a v a 2 s . c o m*/ // expose widget variables Map<String, ValueExpression> variables = new HashMap<String, ValueExpression>(); ExpressionFactory eFactory = ctx.getExpressionFactory(); ValueExpression subWidgetVe = eFactory.createValueExpression(subWidget, Widget.class); Integer level = null; String tagConfigId = null; if (subWidget != null) { level = Integer.valueOf(subWidget.getLevel()); tagConfigId = subWidget.getTagConfigId(); } variables.put(RenderVariables.widgetVariables.widget.name(), subWidgetVe); // variables.put(String.format("%s_%s", // RenderVariables.widgetVariables.widget.name(), level), // subWidgetVe); ValueExpression subWidgetIndexVe = eFactory.createValueExpression(Integer.valueOf(subWidgetCounter), Integer.class); variables.put(RenderVariables.widgetVariables.widgetIndex.name(), subWidgetIndexVe); variables.put(RenderVariables.widgetVariables.widgetIndex.name() + "_" + level, subWidgetIndexVe); // XXX: expose widget controls too, need to figure out // why controls cannot be references to widget.controls like // properties are in TemplateWidgetTypeHandler if (subWidget != null) { for (Map.Entry<String, Serializable> ctrl : subWidget.getControls().entrySet()) { String key = ctrl.getKey(); String name = RenderVariables.widgetVariables.widgetControl.name() + "_" + key; Serializable value = ctrl.getValue(); variables.put(name, eFactory.createValueExpression(value, Object.class)); } } List<String> blockedPatterns = new ArrayList<String>(); blockedPatterns.add(RenderVariables.widgetVariables.widget.name()); blockedPatterns.add(RenderVariables.widgetVariables.widgetIndex.name() + "*"); blockedPatterns.add(RenderVariables.widgetVariables.widgetControl.name() + "_*"); FaceletHandler handlerWithVars = helper.getAliasFaceletHandler(tagConfigId, variables, blockedPatterns, nextHandler); // apply handlerWithVars.apply(ctx, parent); subWidgetCounter++; } }
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 w w w .ja v a 2 s . com } // 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.WidgetTagHandler.java
public static void applyWidgetHandler(FaceletContext ctx, UIComponent parent, TagConfig config, Widget widget, TagAttribute value, boolean fillVariables, FaceletHandler nextHandler) throws IOException { if (widget == null) { return;//from w w w . ja v a 2 s .c o m } FaceletHandlerHelper helper = new FaceletHandlerHelper(config); TagConfig wtConfig = TagConfigFactory.createTagConfig(config, widget.getTagConfigId(), null, nextHandler); WebLayoutManager layoutService = Framework.getService(WebLayoutManager.class); WidgetTypeHandler handler = layoutService.getWidgetTypeHandler(wtConfig, widget); if (handler == null) { String widgetTypeName = widget.getType(); String widgetTypeCategory = widget.getTypeCategory(); String message = String.format("No widget handler found for type '%s' in category '%s'", widgetTypeName, widgetTypeCategory); log.error(message); FaceletHandler h = helper.getErrorComponentHandler(null, message); h.apply(ctx, parent); return; } FaceletHandler fh = handler; if (FaceletHandlerHelper.isDevModeEnabled(ctx)) { // decorate handler with dev handler FaceletHandler devHandler = handler.getDevFaceletHandler(config, widget); if (devHandler != null) { // expose the widget variable to sub dev handler String widgetTagConfigId = widget.getTagConfigId(); Map<String, ValueExpression> variables = new HashMap<String, ValueExpression>(); ExpressionFactory eFactory = ctx.getExpressionFactory(); ValueExpression widgetVe = eFactory.createValueExpression(widget, Widget.class); variables.put(RenderVariables.widgetVariables.widget.name(), widgetVe); List<String> blockedPatterns = new ArrayList<String>(); blockedPatterns.add(RenderVariables.widgetVariables.widget.name() + "*"); FaceletHandler devAliasHandler = helper.getAliasFaceletHandler(widgetTagConfigId, variables, blockedPatterns, devHandler); String refId = widget.getName(); fh = new DevTagHandler(config, refId, handler, devAliasHandler); } } if (FaceletHandlerHelper.isAliasOptimEnabled()) { if (fillVariables) { // expose widget variables VariableMapper cvm = ctx.getVariableMapper(); if (!(cvm instanceof BlockingVariableMapper)) { throw new IllegalArgumentException( "Current context variable mapper should be an instance of MetaVariableMapper"); } BlockingVariableMapper vm = (BlockingVariableMapper) cvm; ValueExpression valueExpr; if (value == null) { valueExpr = new ValueExpressionLiteral(null, Object.class); } else { valueExpr = value.getValueExpression(ctx, Object.class); } vm.setVariable(RenderVariables.globalVariables.value.name(), valueExpr); vm.setVariable(RenderVariables.globalVariables.value.name() + "_" + widget.getLevel(), valueExpr); } fh.apply(ctx, parent); } else { if (fillVariables) { // expose widget variables Map<String, ValueExpression> variables = new HashMap<String, ValueExpression>(); ValueExpression valueExpr; if (value == null) { valueExpr = new ValueExpressionLiteral(null, Object.class); } else { valueExpr = value.getValueExpression(ctx, Object.class); } variables.put(RenderVariables.globalVariables.value.name(), valueExpr); variables.put(RenderVariables.globalVariables.value.name() + "_" + widget.getLevel(), valueExpr); FaceletHandler handlerWithVars = helper.getAliasFaceletHandler(widget.getTagConfigId(), variables, null, fh); // apply handlerWithVars.apply(ctx, parent); } else { // just apply fh.apply(ctx, parent); } } }
From source file:org.nuxeo.ecm.platform.forms.layout.facelets.WidgetTagHandler.java
public static void exposeWidgetVariables(FaceletContext ctx, BlockingVariableMapper vm, Widget widget, Integer widgetIndex, boolean exposeLevel) { ExpressionFactory eFactory = ctx.getExpressionFactory(); ValueExpression widgetVe = eFactory.createValueExpression(widget, Widget.class); vm.setVariable(RenderVariables.widgetVariables.widget.name(), widgetVe); vm.addBlockedPattern(RenderVariables.widgetVariables.widget.name()); ValueExpression widgetIndexVe = null; if (widgetIndex != null) { widgetIndexVe = eFactory.createValueExpression(widgetIndex, Integer.class); vm.setVariable(RenderVariables.widgetVariables.widgetIndex.name(), widgetIndexVe); }/*from w ww.ja v a 2 s .c om*/ if (exposeLevel && !FaceletHandlerHelper.isAliasOptimEnabled()) { Integer level = null; if (widget != null) { level = widget.getLevel(); } vm.setVariable(RenderVariables.widgetVariables.widget.name() + "_" + level, widgetVe); if (widgetIndexVe != null) { vm.setVariable(RenderVariables.widgetVariables.widgetIndex.name() + "_" + level, widgetIndexVe); } vm.addBlockedPattern(RenderVariables.widgetVariables.widget.name() + "_*"); vm.addBlockedPattern(RenderVariables.widgetVariables.widgetIndex.name() + "*"); } // expose widget controls too if (widget != null) { for (Map.Entry<String, Serializable> ctrl : widget.getControls().entrySet()) { String key = ctrl.getKey(); String name = RenderVariables.widgetVariables.widgetControl.name() + "_" + key; ValueExpression ve = eFactory.createValueExpression(ctrl.getValue(), Object.class); vm.setVariable(name, ve); } } vm.addBlockedPattern(RenderVariables.widgetVariables.widgetControl.name() + "_*"); }
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>// www .j av a2 s . c o m * 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.nuxeo.ecm.platform.ui.web.component.holder.ValueHolderTagHandler.java
@Override public void applyNextHandler(FaceletContext ctx, UIComponent c) throws IOException, FacesException, ELException { long start = FaceletDebugTracer.start(); String varName = null;/*from w w w . java 2s. c o m*/ try { boolean varSet = false; if (var != null) { varName = var.getValue(ctx); } VariableMapper orig = ctx.getVariableMapper(); AliasVariableMapper alias = new AliasVariableMapper(); // XXX: reuse the component id as the alias variable mapper id so that // the value holder JSF component can reuse it at render time to expose // the value it keeps String aliasId = (String) c.getAttributes().get(ComponentSupport.MARK_CREATED); alias.setId(aliasId); if (!StringUtils.isBlank(varName)) { varSet = true; List<String> blockedPatterns = new ArrayList<String>(); blockedPatterns.add(varName); alias.setBlockedPatterns(blockedPatterns); } try { if (varSet) { Object valueToExpose = retrieveValueToExpose(ctx, c); ExpressionFactory eFactory = ctx.getExpressionFactory(); ValueExpression valueVe = eFactory.createValueExpression(valueToExpose, Object.class); alias.setVariable(varName, valueVe); VariableMapper vm = alias.getVariableMapperForBuild(orig); ctx.setVariableMapper(vm); AliasVariableMapper.exposeAliasesToRequest(ctx.getFacesContext(), alias); } super.applyNextHandler(ctx, c); } finally { if (varSet) { AliasVariableMapper.removeAliasesExposedToRequest(ctx.getFacesContext(), aliasId); ctx.setVariableMapper(orig); } } } finally { FaceletDebugTracer.trace(start, getTag(), varName); } }
From source file:org.tinygroup.jspengine.runtime.PageContextImpl.java
public static void setMethodVariable(PageContext pageContext, String variable, MethodExpression expression) { ExpressionFactory expFactory = getExpressionFactory(pageContext); ValueExpression exp = expFactory.createValueExpression(expression, Object.class); setValueVariable(pageContext, variable, exp); }