Example usage for org.springframework.expression.spel.support StandardEvaluationContext StandardEvaluationContext

List of usage examples for org.springframework.expression.spel.support StandardEvaluationContext StandardEvaluationContext

Introduction

In this page you can find the example usage for org.springframework.expression.spel.support StandardEvaluationContext StandardEvaluationContext.

Prototype

public StandardEvaluationContext(Object rootObject) 

Source Link

Document

Create a StandardEvaluationContext with the given root object.

Usage

From source file:com.enonic.cms.core.portal.datasource.el.ExpressionFunctionsExecutor.java

public String evaluate(final String expression) {
    final ExpressionRootObject rootObject = new ExpressionRootObject();

    rootObject.setSession(new SessionAccessor(verticalSession));
    rootObject.setCookie(new CookieAccessor(httpRequest));
    rootObject.setProperties(new CmsAndSitePropertiesAccessor(rootProperties, siteProperties));
    rootObject.setPortal(new PortalAccessor(expressionContext));
    rootObject.setUser(new UserAccessor(expressionContext.getUser()));
    rootObject.setParam(new ParamAccessor(requestParameters));
    rootObject.setParams(new ParamsAccessor(requestParameters));

    final String evaluatedString;

    try {//  w  w  w.ja va 2s . co m
        final StandardEvaluationContext context = new StandardEvaluationContext(rootObject);

        context.setPropertyAccessors(PROPERTY_ACCESSORS);

        ExpressionFunctionsFactory.get().setContext(expressionContext);

        Object result;

        try {
            final Expression exp = EXPR_FACTORY.parseExpression(expression, TEMPLATE_PARSER_CONTEXT);

            result = exp.getValue(context);
        } catch (SpelEvaluationException e) {
            result = null;
        } catch (Exception e) {
            LOG.error(e.getMessage());

            result = "ERROR: " + e.getMessage();
        }

        // must be converted here, because param.x[0] will not work
        if (result instanceof String[]) {
            evaluatedString = StringUtils.join((String[]) result, ',');
        } else {
            evaluatedString = result != null ? result.toString() : null;
        }
    } finally {
        ExpressionFunctionsFactory.get().removeContext();
    }

    return evaluatedString;
}

From source file:org.apache.camel.language.spel.SpelExpression.java

private EvaluationContext createEvaluationContext(Exchange exchange) {
    StandardEvaluationContext evaluationContext = new StandardEvaluationContext(new RootObject(exchange));
    if (exchange.getContext() instanceof SpringCamelContext) {
        // Support references (like @foo) in expressions to beans defined in the Registry/ApplicationContext
        ApplicationContext applicationContext = ((SpringCamelContext) exchange.getContext())
                .getApplicationContext();
        evaluationContext.setBeanResolver(new BeanFactoryResolver(applicationContext));
    }/*from  w  ww  .  j  a v a2s. com*/
    return evaluationContext;
}

From source file:uk.co.christhomson.sibyl.cache.connectors.HashMapConnector.java

public Map<?, ?> query(String cacheName, String query) throws CacheException {
    Map<Object, Object> results = new HashMap<Object, Object>();
    Map<?, ?> data = getCache(cacheName);

    ExpressionParser parser = new SpelExpressionParser();

    for (Object key : data.keySet()) {
        Object value = data.get(key);
        EvaluationContext keyContext = new StandardEvaluationContext(key);

        Expression exp = parser.parseExpression(query);
        boolean result = exp.getValue(keyContext, Boolean.class);

        if (result) {
            results.put(key, value);/*from  ww  w  .  ja  va  2  s.c  o  m*/
        }
    }

    return results;
}

From source file:gov.nih.nci.calims2.taglib.form.SingleSelectViewTag.java

/**
 * Gets the value of the selected option.
 * //  w  ww  .  ja v  a  2s . c  o  m
 * @return the value of the selected option.
 */
@SuppressWarnings("unchecked")
private String getSelectedOptionForView() {
    if (selected == null) {
        return "";
    }
    if (collectionType == SelectCollectionType.ENUMERATIONS) {
        I18nEnumeration enumValue = (I18nEnumeration) selected;
        return enumValue.getLocalizedValue(TagHelper.getRequestContext(pageContext).getLocale());
    }
    if (optionCreatorCallback != null) {
        OptionCreatorCallback<Object> callback = (OptionCreatorCallback<Object>) optionCreatorCallback;
        return callback.getLabel(selected);
    }
    Map<String, Expression> propertyMap = SingleSelectHelper.getPropertyMap(properties);
    Expression labelProperty = propertyMap.get("label");
    EvaluationContext context = new StandardEvaluationContext(selected);
    return SingleSelectHelper.evaluateExpression(labelProperty, context);
}

From source file:com.creactiviti.piper.core.task.SpelTaskEvaluator.java

private StandardEvaluationContext createEvaluationContext(Context aContext) {
    StandardEvaluationContext context = new StandardEvaluationContext(aContext);
    context.addPropertyAccessor(new MapPropertyAccessor());
    context.addMethodResolver(methodResolver());
    return context;
}

From source file:net.nan21.dnet.core.presenter.converter.AbstractDsConverter.java

/**
 * Populate model fields from entity according to mappings.
 *///  ww w. j a  va2s .com

protected void entityToModel_(E e, M m, EntityManager em, Map<String, Method> setters,
        Map<String, String> refpaths) throws Exception {

    StandardEvaluationContext context = new StandardEvaluationContext(e);

    for (Map.Entry<String, Method> entry : setters.entrySet()) {
        String fieldName = entry.getKey();
        Method method = entry.getValue();

        if (method.getName().startsWith("set") && !method.getName().equals("set__clientRecordId__")) {
            try {
                method.invoke(m, parser.parseExpression(refpaths.get(fieldName)).getValue(context));
            } catch (Exception exc) {

            }
        }
    }
}

From source file:com.google.code.activetemplates.impl.TemplateCompilerImpl.java

@Override
public void compile(final Template t, final TemplateModel model, XmlResult out)
        throws TemplateCompileException {

    final XmlSource s = t.createSource();
    XMLEventReader r = null;/*w ww. j  av a2  s  . co m*/
    XMLEventWriter w = null;
    try {
        r = inFactory.createXMLEventReader(s.getSource());
        w = outFactory.createXMLEventWriter(out.getResult());
        final XMLStreamException[] xe = new XMLStreamException[1];

        final XMLEventReader fr = r;
        final XMLEventWriter fw = w;

        StandardEvaluationContext eContext = new StandardEvaluationContext(model);
        eContext.addPropertyAccessor(new TemplateModelPropertyAccessor());

        CompileContext ctx = new CompileContext(fr, fw, eFactory, eComponentFactory, expressionParser,
                eContext);
        doCompile(t.getName(), ctx);

        if (xe[0] != null) {
            throw xe[0];
        }

    } catch (XMLStreamException e) {
        throw new TemplateCompileException(e);
    } finally {
        s.close();
        if (r != null)
            try {
                r.close();
            } catch (XMLStreamException e) {
            }
        if (w != null)
            try {
                w.close();
            } catch (XMLStreamException e) {
            }
    }
}

From source file:de.itsvs.cwtrpc.security.DefaultRpcAuthenticationFailureHandler.java

protected EvaluationContext createEvaluationContext(HttpServletRequest request,
        AuthenticationException exception) {
    final StandardEvaluationContext evaluationContext;

    evaluationContext = new StandardEvaluationContext(exception);
    evaluationContext.setVariable("request", request);

    return evaluationContext;
}

From source file:gov.nih.nci.calims2.taglib.form.SingleSelectTag.java

@SuppressWarnings("unchecked")
private String getSelectedId() {
    if (selected == null) {
        return "";
    }//from  w  w w  .j  av a  2  s. c o  m
    if (collectionType == SelectCollectionType.ENUMERATIONS) {
        return ((I18nEnumeration) selected).getName();
    }
    if (optionCreatorCallback != null) {
        OptionCreatorCallback<Object> callback = (OptionCreatorCallback<Object>) optionCreatorCallback;
        return StringUtils.stripToEmpty(callback.getId(selected));
    }
    Expression idProperty = propertyMap.get("id");
    EvaluationContext context = new StandardEvaluationContext(selected);
    return StringUtils.stripToEmpty(SingleSelectHelper.evaluateExpression(idProperty, context));
}

From source file:net.nan21.dnet.core.presenter.converter.AbstractDsConverter.java

/**
 * Update entity attribute fields./*from   www  .  ja va 2s. com*/
 * 
 * @param m
 * @param e
 * @param isInsert
 * @param entityService
 * @throws Exception
 */
protected void modelToEntityAttributes(M m, E e, boolean isInsert, EntityManager em) throws Exception {
    StandardEvaluationContext context = new StandardEvaluationContext(m);
    Map<String, String> attrmap = this.descriptor.getM2eConv();
    Method[] methods = this.getEntityClass().getMethods();

    List<String> noInserts = this.descriptor.getNoInserts();
    List<String> noUpdates = this.descriptor.getNoUpdates();

    for (Method method : methods) {
        if (method.getName().startsWith("set")) {
            String fn = StringUtils.uncapitalize(method.getName().substring(3));
            boolean doit = true;
            if (attrmap.containsKey(fn)) {
                String dsf = attrmap.get(fn);
                if (isInsert && noInserts.contains(fn)) {
                    doit = false;
                }
                if (!isInsert && noUpdates.contains(fn)) {
                    doit = false;
                }

                try {
                    if (doit) {
                        method.invoke(e, parser.parseExpression(dsf).getValue(context));
                    }
                } catch (Exception exc) {

                }
            }
        }
    }
}