/* * ElHelper.java * * Created on March 7, 2009, 9:38 PM * * Copyright 2009, Britt Miner. */ package org.bminer.jbpm.util; import java.util.regex.*; import org.jbpm.graph.exe.ExecutionContext; import org.jbpm.jpdl.el.impl.JbpmExpressionEvaluator; /** * * @author britt */ public class ElHelper { // Prepare to identify any EL expressions, #{...}, regex: "#\{.*?\}" String elPatternStr = "#\\{.*?\\}"; Pattern elPattern = Pattern.compile(elPatternStr); /* Evaluate the input as a possible EL expression. */ public Object evaluateEL(String inputStr, ExecutionContext ec) { if (inputStr == null) { return null; } Matcher matcher = elPattern.matcher(inputStr); if (matcher.matches()) { //input is one big EL expression return JbpmExpressionEvaluator.evaluate(inputStr, ec); } else { return inputStr; } } /* Treats input as a possible series of EL expressions and concatenates what * is found. */ public String concatenateEL(String inputStr, ExecutionContext ec) { if (inputStr == null) { return null; } Matcher matcher = elPattern.matcher(inputStr); StringBuffer buf = new StringBuffer(); while (matcher.find()) { // Get the match result String elExpr = matcher.group(); // Evaluate EL expression Object o = JbpmExpressionEvaluator.evaluate(elExpr, ec); String elValue = ""; if (o != null) { elValue = String.valueOf(JbpmExpressionEvaluator.evaluate(elExpr, ec)); } // Insert the calculated value in place of the EL expression matcher.appendReplacement(buf, elValue); } matcher.appendTail(buf); // Deliver result if (buf.length() > 0) { return buf.toString(); } else { return null; } } /* Returns true if the value is a String which contains the pattern delineating * an EL expression. */ public boolean hasEL(Object value) { if (value instanceof String) { Matcher matcher = elPattern.matcher((String) value); return matcher.find(); } return false; } /* Returns true if the value is a String which in its entirety composes * one EL expression. */ public boolean isEL(Object value) { if (value instanceof String) { Matcher matcher = elPattern.matcher((String) value); return matcher.matches(); } return false; } /** * Evaluates potential EL expressions specifically used as conditions--that is * expressions that are to be evaluated to a boolean. Catering to this use: * * Empty or null inputs will return true, as if no limiting condition was * specified. * * Expressions that cannot be parsed or fail to evaluate will throw an Exception. * @param inputStr String of the EL expression to be evaluated. Only a single * EL expression is valid. * @param ec The current jBPM ExecutionContext. * @return True if the supplied EL condition evaluates to True or if no EL * condition was supplied. False if the supplied EL condition evaluates to * False. */ public boolean evaluateElAsCondition(String inputStr, ExecutionContext ec) { if (inputStr != null) inputStr = inputStr.trim(); // If no condition is supplied, then that condition is certainly met if (inputStr == null || inputStr.isEmpty() || "#{}".equals(inputStr.replaceAll(" ", ""))) { return true; } /* // If a string is specified but isn't identified as an EL, then we have an issue Matcher matcher = elPattern.matcher(inputStr); if (!matcher.matches()) { //input is one big EL expression throw new Exception("Condition input '" + inputStr + "' doesn't match the EL pattern '#{}'."); } // Do a jig around JbpmExpressionEvaluator's back. We'd like to know specifically if our syntax is faulty ExpressionEvaluator evaluator = new ExpressionEvaluatorImpl(); try { evaluator.parseExpression(inputStr, Boolean.class, null); } catch (ELException elExc) { throw new Exception("Can't parse condition input '" + inputStr + "' as an EL: " + elExc.getMessage()); }*/ Object obj = this.evaluateEL(inputStr, ec); return ((Boolean) obj).booleanValue(); } }