Example usage for org.apache.commons.jexl3 JexlExpression evaluate

List of usage examples for org.apache.commons.jexl3 JexlExpression evaluate

Introduction

In this page you can find the example usage for org.apache.commons.jexl3 JexlExpression evaluate.

Prototype

Object evaluate(JexlContext context);

Source Link

Document

Evaluates the expression with the variables contained in the supplied JexlContext .

Usage

From source file:com.technophobia.substeps.model.Arguments.java

public static Object evaluateExpression(String expressionWithDelimiters) {

    ParameterSubstitution parameterSubstituionConfig = NewSubstepsExecutionConfig
            .getParameterSubstituionConfig();

    // TODO - check that the expression doesn't contain any of the bad words
    // or and eq ne lt gt le ge div mod not null true false new var return
    // any of those words need to be qutoed or ['  ']
    // http://commons.apache.org/proper/commons-jexl/reference/syntax.html

    // try evaluating this expression against the executionContext

    // TODO check flag to see whether we can evaluate things from the ec

    if (expressionWithDelimiters != null && parameterSubstituionConfig.substituteParameters()
            && expressionWithDelimiters.startsWith(parameterSubstituionConfig.startDelimiter())) {
        String expression = StringUtils.stripStart(
                StringUtils.stripEnd(expressionWithDelimiters, parameterSubstituionConfig.endDelimiter()),
                parameterSubstituionConfig.startDelimiter());

        JexlContext context = new MapContext(ExecutionContext.flatten());

        JexlExpression e = jexl.createExpression(expression);

        return e.evaluate(context);
    } else {// www. ja v  a  2 s. c om
        return expressionWithDelimiters;
    }
}

From source file:com.dianping.cat.report.page.business.graph.CustomDataCalculator.java

private double calculate(String pattern) {
    JexlExpression e = jexl.createExpression(pattern);
    Number result = (Number) e.evaluate(null);
    return result.doubleValue();
}

From source file:org.apache.jmeter.functions.Jexl3Function.java

/** {@inheritDoc} */
@Override/*ww  w.jav a  2  s .  c om*/
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    String str = ""; //$NON-NLS-1$

    CompoundVariable var = (CompoundVariable) values[0];
    String exp = var.execute();

    String varName = ""; //$NON-NLS-1$
    if (values.length > 1) {
        varName = ((CompoundVariable) values[1]).execute().trim();
    }

    JMeterContext jmctx = JMeterContextService.getContext();
    JMeterVariables vars = jmctx.getVariables();

    try {
        JexlContext jc = new MapContext();
        jc.set("log", log); //$NON-NLS-1$
        jc.set("ctx", jmctx); //$NON-NLS-1$
        jc.set("vars", vars); //$NON-NLS-1$
        jc.set("props", JMeterUtils.getJMeterProperties()); //$NON-NLS-1$
        // Previously mis-spelt as theadName
        jc.set("threadName", Thread.currentThread().getName()); //$NON-NLS-1$
        jc.set("sampler", currentSampler); //$NON-NLS-1$ (may be null)
        jc.set("sampleResult", previousResult); //$NON-NLS-1$ (may be null)
        jc.set("OUT", System.out);//$NON-NLS-1$

        // Now evaluate the script, getting the result
        JexlExpression e = getJexlEngine().createExpression(exp);
        Object o = e.evaluate(jc);
        if (o != null) {
            str = o.toString();
        }
        if (vars != null && varName.length() > 0) {// vars will be null on TestPlan
            vars.put(varName, str);
        }
    } catch (Exception e) {
        log.error("An error occurred while evaluating the expression \"" + exp + "\"\n", e);
    }
    return str;
}

From source file:org.apache.nifi.processors.ccda.ExtractCCDAAttributes.java

/**
 * Process elements children based on the parser mapping.
 * Any String values are added to attributes
 * For List, the processList method is called to iterate and process
 * For an Object this method is called recursively
 * While adding to the attributes the key is prefixed by parent
 * @param parent       parent key for this element, used as a prefix for attribute key
 * @param element      element to be processed
 * @param attributes   map of attributes to populate
 * @return             map of processed data, value can contain String or Map of Strings
 *//*from   w  w  w. j  av  a 2  s  .co m*/
protected Map<String, Object> processElement(String parent, Object element, Map<String, String> attributes) {
    final StopWatch stopWatch = new StopWatch(true);

    Map<String, Object> map = new LinkedHashMap<String, Object>();
    String name = element.getClass().getName();
    Map<String, String> jexlMap = processMap.get(name); // get JEXL mappings for this element

    if (jexlMap == null) {
        getLogger().warn("Missing mapping for element " + name);
        return null;
    }

    for (Entry<String, String> entry : jexlMap.entrySet()) { // evaluate JEXL for each child element
        jexlCtx.set("element", element);
        JexlExpression jexlExpr = jexl.createExpression(entry.getValue());
        Object value = jexlExpr.evaluate(jexlCtx);
        String key = entry.getKey();
        String prefix = parent != null ? parent + "." + key : key;
        addElement(map, prefix, key, value, attributes);
    }
    stopWatch.stop();
    getLogger().debug("Processed {} in {}",
            new Object[] { name, stopWatch.getDuration(TimeUnit.MILLISECONDS) });

    return map;
}

From source file:org.apache.syncope.core.provisioning.java.jexl.JexlUtils.java

public static String evaluate(final String expression, final JexlContext jexlContext) {
    String result = StringUtils.EMPTY;

    if (StringUtils.isNotBlank(expression) && jexlContext != null) {
        try {//from   w ww.j  a  v a  2  s .  c  o  m
            JexlExpression jexlExpression = getEngine().createExpression(expression);
            Object evaluated = jexlExpression.evaluate(jexlContext);
            if (evaluated != null) {
                result = evaluated.toString();
            }
        } catch (Exception e) {
            LOG.error("Error while evaluating JEXL expression: " + expression, e);
        }
    } else {
        LOG.debug("Expression not provided or invalid context");
    }

    return result;
}