org.apache.commons.jexl2.JexlException Java Examples

The following examples show how to use org.apache.commons.jexl2.JexlException. 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: ClusterManager.java    From uyuni with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Evaluate a JEXL expression.
 * @param expr expression
 * @param ctx context for the expression
 * @return evaluation result
 */
public Object evalExpression(String expr, Map<String, Object> ctx) {
    JexlEngine jexl = new JexlEngine();

    // Create an expression
    Expression jexlExpr = jexl.createExpression(expr);

    // Create a context and add data
    JexlContext jc = new MapContext(ctx);
    try {
        return jexlExpr.evaluate(jc);
    }
    catch (JexlException e) {
        LOG.error("Error evaluating expression: " + expr, e);
        throw new RuntimeException(e);
    }
}
 
Example #2
Source File: ExpressionContextImpl.java    From uflo with Apache License 2.0 6 votes vote down vote up
public synchronized Object eval(long processInstanceId, String expression) {
	expression=expression.trim();
	if(expression.startsWith("${") && expression.endsWith("}")){
		expression=expression.substring(2,expression.length()-1);
	}else{
		return expression;
	}
	CacheService cacheService=EnvironmentUtils.getEnvironment().getCache();
	ProcessMapContext context=cacheService.getContext(processInstanceId);
	if(context==null){
		buildProcessInstanceContext(processService.getProcessInstanceById(processInstanceId));
		context=cacheService.getContext(processInstanceId);
	}
	if(context==null){
		log.warn("ProcessInstance "+processInstanceId+" variable context is not exist!");
		return null;
	}
	Object obj=null;
	try{
		obj=jexl.createExpression(expression).evaluate(context);
	}catch(JexlException ex){
		log.info("Named "+expression+" variable was not found in ProcessInstance "+processInstanceId);
	}
	return obj;			
}
 
Example #3
Source File: JexlParserAdapter.java    From mrgeo with Apache License 2.0 6 votes vote down vote up
@Override
public ParserNode parse(String expression) throws ParserException
{
  if (engine == null)
  {
    throw new ParserException("Map algebra parser engine was not initialized");
  }
  try
  {
    engine.createScript(expression);
    jexlRootNode = engine.getScript();
    //jexlRootNode = (ASTJexlScript)engine.createScript(expression);
    ParserNode last = null;
    for (int i = 0; i < jexlRootNode.jjtGetNumChildren(); i++)
    {
      last = convertToMrGeoNode(jexlRootNode.jjtGetChild(i));
    }
    return last;
  }
  catch (JexlException e)
  {
    throw new ParserException(e);
  }
}
 
Example #4
Source File: DatawaveInterpreter.java    From datawave with Apache License 2.0 5 votes vote down vote up
/**
 * Triggered when variable can not be resolved.
 * 
 * @param xjexl
 *            the JexlException ("undefined variable " + variable)
 * @return throws JexlException if strict, null otherwise
 */
@Override
protected Object unknownVariable(JexlException xjexl) {
    if (strict) {
        throw xjexl;
    }
    // do not warn
    return null;
}
 
Example #5
Source File: ContentFunctionsTest.java    From datawave with Apache License 2.0 5 votes vote down vote up
/**
 * Ensure that we have a failure
 */
@Test(expected = JexlException.class)
public void testQuotedEvaluation_1_fail() {
    String query = buildFunction(ContentFunctions.CONTENT_WITHIN_FUNCTION_NAME, "1", Constants.TERM_OFFSET_MAP_JEXL_VARIABLE_NAME, "'dog's'", "'cat'");
    Expression expr = engine.createExpression(query);
    
    fail("Query should have failed to parse");
}
 
Example #6
Source File: ExpressionUtils.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Indicates whether the given expression is valid and evaluates to true or
 * false.
 *
 * @param expression the expression.
 * @param vars the variables, can be null.
 * @return true or false.
 */
public static boolean isBoolean( String expression, Map<String, Object> vars )
{
    try
    {
        Object result = evaluate( expression, vars );

        return ( result instanceof Boolean );
    }
    catch ( JexlException | NumberFormatException ex )
    {
        return false;
    }
}
 
Example #7
Source File: ExpressionUtils.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Indicates whether the given expression is valid, i.e. can be successfully
 * evaluated.
 *
 * @param expression the expression.
 * @param vars the variables, can be null.
 * @return true or false.
 */
public static boolean isValid( String expression, Map<String, Object> vars )
{
    try
    {
        Object result = evaluate( expression, vars, true );

        return result != null;
    }
    catch ( JexlException | NumberFormatException ex )
    {
        // TODO division by zero masking
        return ex.getMessage().contains( "divide error" );
    }
}
 
Example #8
Source File: ExpressionUtils.java    From dhis2-android-sdk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Indicates whether the given expression is valid and evaluates to true or
 * false.
 *
 * @param expression the expression.
 * @param vars       the variables, can be null.
 * @return true or false.
 */
static boolean isBoolean(String expression, Map<String, Object> vars) {
    try {
        Object result = evaluate(expression, vars);

        return result instanceof Boolean;
    } catch (JexlException ex) {
        return false;
    }
}
 
Example #9
Source File: ExpressionUtils.java    From dhis2-android-sdk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Indicates whether the given expression is valid, i.e. can be successfully
 * evaluated.
 *
 * @param expression the expression.
 * @param vars       the variables, can be null.
 * @return true or false.
 */
static boolean isValid(String expression, Map<String, Object> vars) {
    try {
        Object result = evaluate(expression, vars, true);

        return result != null;
    } catch (JexlException ex) {
        //TODO Masking bug in Jexl, fix
        return ex.getMessage().contains("divide error");
    }
}
 
Example #10
Source File: ReportLayoutUtil.java    From nextreports-designer with Apache License 2.0 5 votes vote down vote up
public static String getExpressionType(String expression)  {
    Object value = null;
    try {
        value = testExpression(expression);
    } catch (JexlException ex) {
        ex.printStackTrace();
        Show.error(ex);
    }

    String type = "java.lang.Double";
    if ((value instanceof String) || (value instanceof Boolean) || (value instanceof Date) )  {
        type = value.getClass().getCanonicalName();
    }
    return type;
}
 
Example #11
Source File: ReportLayoutUtil.java    From nextreports-designer with Apache License 2.0 5 votes vote down vote up
public static boolean isValidExpression(String expression) {
    Object value = null;
    try {
        value = testExpression(expression);
    } catch (JexlException ex) {
        ex.printStackTrace();
        return false;
    }
    return true;
}
 
Example #12
Source File: ReportLayoutUtil.java    From nextreports-designer with Apache License 2.0 5 votes vote down vote up
public static boolean isValidBooleanExpression(String expression) {
    Object value = null;
    try {
        value = testExpression(expression);
        if  ( !(value instanceof Boolean) ) {
            return false;
        }
    } catch (JexlException ex) {
        ex.printStackTrace();
        return false;
    }
    return true;
}
 
Example #13
Source File: EdgeQueryLogic.java    From datawave with Apache License 2.0 4 votes vote down vote up
@Override
public void setupQuery(GenericQueryConfiguration configuration) throws Exception {
    config = (EdgeQueryConfiguration) configuration;
    prefilterValues = null;
    EdgeQueryConfiguration.dateType dateFilterType = ((EdgeQueryConfiguration) configuration).getDateRangeType();
    
    log.debug("Performing edge table query: " + config.getQueryString());
    
    boolean includeStats = ((EdgeQueryConfiguration) configuration).includeStats();
    
    String queryString = config.getQueryString();
    String normalizedQuery = null;
    String statsNormalizedQuery = null;
    
    queryString = fixQueryString(queryString);
    QueryData qData = configureRanges(queryString);
    setRanges(qData.getRanges());
    
    VisitationContext context = null;
    try {
        context = normalizeJexlQuery(queryString, false);
        normalizedQuery = context.getNormalizedQuery().toString();
        statsNormalizedQuery = context.getNormalizedStatsQuery().toString();
        log.debug("Jexl after normalizing SOURCE and SINK: " + normalizedQuery);
    } catch (JexlException ex) {
        log.error("Error parsing user query.", ex);
    }
    
    if ((null == normalizedQuery || normalizedQuery.equals("")) && qData.getRanges().size() < 1) {
        throw new IllegalStateException("Query string is empty after initial processing, no ranges or filters can be generated to execute.");
    }
    
    addIterators(qData, getDateBasedIterators(config.getBeginDate(), config.getEndDate(), currentIteratorPriority, dateFilterSkipLimit, dateFilterType));
    
    if (!normalizedQuery.equals("")) {
        log.debug("Query being sent to the filter iterator: " + normalizedQuery);
        IteratorSetting edgeIteratorSetting = new IteratorSetting(currentIteratorPriority, EdgeFilterIterator.class.getSimpleName() + "_"
                        + currentIteratorPriority, EdgeFilterIterator.class);
        edgeIteratorSetting.addOption(EdgeFilterIterator.JEXL_OPTION, normalizedQuery);
        edgeIteratorSetting.addOption(EdgeFilterIterator.PROTOBUF_OPTION, "TRUE");
        
        if (!statsNormalizedQuery.equals("")) {
            edgeIteratorSetting.addOption(EdgeFilterIterator.JEXL_STATS_OPTION, statsNormalizedQuery);
        }
        if (prefilterValues != null) {
            String value = serializePrefilter();
            edgeIteratorSetting.addOption(EdgeFilterIterator.PREFILTER_WHITELIST, value);
        }
        
        if (includeStats) {
            edgeIteratorSetting.addOption(EdgeFilterIterator.INCLUDE_STATS_OPTION, "TRUE");
        } else {
            edgeIteratorSetting.addOption(EdgeFilterIterator.INCLUDE_STATS_OPTION, "FALSE");
        }
        
        addIterator(qData, edgeIteratorSetting);
    }
    
    log.debug("Configuring connection: tableName: " + config.getTableName() + ", auths: " + config.getAuthorizations());
    
    BatchScanner scanner = createBatchScanner(config);
    
    log.debug("Using the following ranges: " + qData.getRanges());
    
    if (context != null && context.isHasAllCompleteColumnFamilies()) {
        for (Text columnFamily : context.getColumnFamilies()) {
            scanner.fetchColumnFamily(columnFamily);
        }
        
    }
    
    scanner.setRanges(qData.getRanges());
    
    addCustomFilters(qData, currentIteratorPriority);
    
    for (IteratorSetting setting : qData.getSettings()) {
        scanner.addScanIterator(setting);
    }
    
    this.scanner = scanner;
    iterator = scanner.iterator();
}
 
Example #14
Source File: ContactSelectorView.java    From elexis-3-core with Eclipse Public License 1.0 4 votes vote down vote up
public void keyReleased(KeyEvent ke){
	String txt = text.getText();
	
	// We have a formula, if the string starts with "="
	if (txt.startsWith("=")) {
		String formula;
		if (txt.contains(";")) {
			formula = txt.substring(1, txt.indexOf(";"));
			
			Map<String, Object> functions = new HashMap<>();
			functions.put("math", Math.class);
			JexlEngine jexl = new JexlEngine();
			jexl.setLenient(false);
			jexl.setFunctions(functions);
			
			try {
				Expression expr = jexl.createExpression(formula);
				Object result = expr.evaluate(new MapContext());
				text.setText("");
				text.setMessage(formula + "=" + result + "");
				result = null;
			} catch (JexlException e) {
				text.setText("");
				text.setMessage("Invalid expression: " + formula);
			}
		}
		return;
	}
	
	if (txt.length() > 1) {
		filterPositionTitle.setSearchText(txt);
		viewer.getControl().setRedraw(false);
		viewer.refresh();
		viewer.getControl().setRedraw(true);
	} else {
		filterPositionTitle.setSearchText(null);
		viewer.getControl().setRedraw(false);
		viewer.refresh();
		viewer.getControl().setRedraw(true);
	}
}