org.apache.commons.jexl3.JexlExpression Java Examples

The following examples show how to use org.apache.commons.jexl3.JexlExpression. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JexlUtils.java    From syncope with Apache License 2.0 6 votes vote down vote up
public static String evaluate(final String expression, final JexlContext jexlContext) {
    String result = StringUtils.EMPTY;

    if (StringUtils.isNotBlank(expression) && jexlContext != null) {
        try {
            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;
}
 
Example #2
Source File: ExtractCCDAAttributes.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * 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
 */
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;
}
 
Example #3
Source File: Jexl3ConfigEnvironmentPreprocessor.java    From spring-cloud-formula with Apache License 2.0 5 votes vote down vote up
@Override
public String process(String key) {
    key = key.trim();
    if (key.startsWith("#{") && key.endsWith("}")) {
        int len = key.length();
        try {
            JexlExpression expr = jexlEngine.createExpression(key.substring(2, len - 1));
            return expr.evaluate(jc).toString();
        } catch (Exception e) {
            throw new PreprocessingException("error while evaluate expression: " + key, e);
        }
    }

    return key;
}
 
Example #4
Source File: Jexl3ConfigEnvironmentPreprocessorTest.java    From spring-cloud-formula with Apache License 2.0 5 votes vote down vote up
@Test
public void testCache() {
    JexlEngine engine = new JexlBuilder().cache(100).create();

    JexlExpression expr = engine.createExpression("a = 3 ? 1 : 3");
    JexlExpression expr2 = engine.createExpression("a = 3 ? 1 : 3");
    assertSame(expr, expr2);
}
 
Example #5
Source File: GlobalVariableValidationAction.java    From android-uiconductor with Apache License 2.0 5 votes vote down vote up
@Override
boolean validateRaw(ActionContext actionContext, AndroidDeviceDriver androidDeviceDriver) {
  // Create or retrieve an engine
  JexlEngine jexl = new JexlBuilder().create();

  // Create an expression
  JexlExpression e = jexl.createExpression(expression);

  JexlContext jc = new MapContext();

  // To use advanced expression, need a converter to do the trick, the expression will be like
  // "uicdTypeConverter.toInt($uicd_var1) + uicdTypeConverter.toInt($uicd_var2)";
  if (expression.contains(TYPE_CONVERTER_OBJ_KEYWORD)) {
    jc.set(TYPE_CONVERTER_OBJ_KEYWORD, new UicdTypeConverter());
  }
  // Create a context and add data
  // jc.set("$uicd_var1", new String("adbcd"));
  // Set the displayStr so that we can see the result in the test details.
  displayStr = expandUicdGlobalVariableToJexl(expression, jc, actionContext);

  // Now evaluate the expression, getting the result
  boolean ret = false;
  try {
    Object o = e.evaluate(jc);
    ret = Boolean.parseBoolean(o.toString());
  } catch (Exception ex) {
    System.out.println(ex.getMessage());
  }
  displayStr = String.format("%s|validation result:%s", displayStr, ret);
  return ret;
}
 
Example #6
Source File: ExpressionLanguageJEXLImpl.java    From oval with Eclipse Public License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Object evaluate(final String expression, final Map<String, ?> values) throws ExpressionEvaluationException {
   LOG.debug("Evaluating JEXL expression: {1}", expression);
   try {
      final JexlExpression expr = expressionCache.get(expression);
      return expr.evaluate(new MapContext((Map<String, Object>) values));
   } catch (final Exception ex) {
      throw new ExpressionEvaluationException("Evaluating JEXL expression failed: " + expression, ex);
   }
}
 
Example #7
Source File: JexlRowFactory.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
public MapBasedRow(Map<String, String> data, Map<String, List<String>> joinedData, ErrorHandler errorHandler,
                   Map<String, JexlExpression> expressions) {
  this.data = data;
  this.joinedData = joinedData;
  this.errorHandler = errorHandler;
  this.expressions = expressions;
}
 
Example #8
Source File: Debugger.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
/**
 * Position the debugger on the root of an expression.
 * @param jscript the expression
 * @return true if the expression was a {@link Script} instance, false otherwise
 */
public boolean debug(JexlExpression jscript) {
    if (jscript instanceof Script) {
        return debug(((Script) jscript).script);
    } else {
        return false;
    }
}
 
Example #9
Source File: ScriptVisitor.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
/**
 * Visits all AST constituents of a JEXL expression.
 * @param jscript the expression
 * @param data some data context
 * @return the visit result or null if jscript was not a Script implementation
 */
public Object visitExpression (JexlExpression jscript, Object data) {
    if (jscript instanceof Script) {
        return ((Script) jscript).getScript().jjtAccept(this, data);
    }
    return null;
}
 
Example #10
Source File: ArrayTest.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
/**
 * An example for array access.
 */
static void example(Output out) throws Exception {
    /*
     * First step is to retrieve an instance of a JexlEngine;
     * it might be already existing and shared or created anew.
     */
    JexlEngine jexl = new JexlBuilder().create();
    /*
     *  Second make a jexlContext and put stuff in it
     */
    JexlContext jc = new MapContext();

    List<Object> l = new ArrayList<Object>();
    l.add("Hello from location 0");
    Integer two = 2;
    l.add(two);
    jc.set("array", l);

    JexlExpression e = jexl.createExpression("array[1]");
    Object o = e.evaluate(jc);
    out.print("Object @ location 1 = ", o, two);

    e = jexl.createExpression("array[0].length()");
    o = e.evaluate(jc);

    out.print("The length of the string at location 0 is : ", o, 21);
}
 
Example #11
Source File: JexlProvider.java    From joyrpc with Apache License 2.0 4 votes vote down vote up
public Jexl3Expression(JexlExpression expression) {
    this.expression = expression;
}
 
Example #12
Source File: JEXLTemplateModel.java    From jweb-cms with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Expression add(String expression, Expression defaultValue, Integer row, Integer col, String source) {
    JexlExpression e = jexlEngine.createExpression(expression);
    expressions.add(e);
    return new ExpressionImpl(expressions.size() - 1, expression, defaultValue, row, col, source, this);
}
 
Example #13
Source File: MethodPropertyTest.java    From tools-journey with Apache License 2.0 4 votes vote down vote up
/**
 * An example for method access.
 */
public static void example(final Output out) throws Exception {
    /**
     * First step is to retrieve an instance of a JexlEngine;
     * it might be already existing and shared or created anew.
     */
    JexlEngine jexl = new JexlBuilder().create();
    /*
     *  Second make a jexlContext and put stuff in it
     */
    JexlContext jc = new MapContext();

    /**
     * The Java equivalents of foo and number for comparison and checking
     */
    Foo foo = new Foo();
    Integer number = new Integer(10);

    jc.set("foo", foo);
    jc.set("number", number);

    /*
     *  access a method w/o args
     */
    JexlExpression e = jexl.createExpression("foo.getFoo()");
    Object o = e.evaluate(jc);
    out.print("value returned by the method getFoo() is : ", o, foo.getFoo());

    /*
     *  access a method w/ args
     */
    e = jexl.createExpression("foo.convert(1)");
    o = e.evaluate(jc);
    out.print("value of " + e.getParsedText() + " is : ", o, foo.convert(1));

    e = jexl.createExpression("foo.convert(1+7)");
    o = e.evaluate(jc);
    out.print("value of " + e.getParsedText() + " is : ", o, foo.convert(1+7));

    e = jexl.createExpression("foo.convert(1+number)");
    o = e.evaluate(jc);
    out.print("value of " + e.getParsedText() + " is : ", o, foo.convert(1+number.intValue()));

    /*
     * access a property
     */
    e = jexl.createExpression("foo.bar");
    o = e.evaluate(jc);
    out.print("value returned for the property 'bar' is : ", o, foo.get("bar"));

}
 
Example #14
Source File: ExpressionLanguageJEXLImpl.java    From oval with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected JexlExpression load(final String expression) {
   return JEXL.createExpression(expression);
}
 
Example #15
Source File: MethodPropertyTest.java    From commons-jexl with Apache License 2.0 4 votes vote down vote up
/**
 * An example for method access.
 */
public static void example(final Output out) throws Exception {
    /*
     * First step is to retrieve an instance of a JexlEngine;
     * it might be already existing and shared or created anew.
     */
    JexlEngine jexl = new JexlBuilder().create();
    /*
     *  Second make a jexlContext and put stuff in it
     */
    JexlContext jc = new MapContext();

    /*
     * The Java equivalents of foo and number for comparison and checking
     */
    Foo foo = new Foo();
    Integer number = 10;

    jc.set("foo", foo);
    jc.set("number", number);

    /*
     *  access a method w/o args
     */
    JexlExpression e = jexl.createExpression("foo.getFoo()");
    Object o = e.evaluate(jc);
    out.print("value returned by the method getFoo() is : ", o, foo.getFoo());

    /*
     *  access a method w/ args
     */
    e = jexl.createExpression("foo.convert(1)");
    o = e.evaluate(jc);
    out.print("value of " + e.getParsedText() + " is : ", o, foo.convert(1));

    e = jexl.createExpression("foo.convert(1+7)");
    o = e.evaluate(jc);
    out.print("value of " + e.getParsedText() + " is : ", o, foo.convert(1+7));

    e = jexl.createExpression("foo.convert(1+number)");
    o = e.evaluate(jc);
    out.print("value of " + e.getParsedText() + " is : ", o, foo.convert(1+ number));

    /*
     * access a property
     */
    e = jexl.createExpression("foo.bar");
    o = e.evaluate(jc);
    out.print("value returned for the property 'bar' is : ", o, foo.get("bar"));

}