Coverage Report - org.apache.commons.scxml.env.jexl.JexlEvaluator

Classes in this File Line Coverage Branch Coverage Complexity
JexlEvaluator
81% 
100% 
4.833

 1  
 /*
 2  
  *
 3  
  *   Copyright 2006 The Apache Software Foundation.
 4  
  *
 5  
  *  Licensed under the Apache License, Version 2.0 (the "License");
 6  
  *  you may not use this file except in compliance with the License.
 7  
  *  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  *  Unless required by applicable law or agreed to in writing, software
 12  
  *  distributed under the License is distributed on an "AS IS" BASIS,
 13  
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  *  See the License for the specific language governing permissions and
 15  
  *  limitations under the License.
 16  
  *
 17  
  */
 18  
 package org.apache.commons.scxml.env.jexl;
 19  
 
 20  
 import java.util.ArrayList;
 21  
 import java.util.HashMap;
 22  
 import java.util.List;
 23  
 import java.util.Map;
 24  
 import java.util.regex.Pattern;
 25  
 
 26  
 import org.apache.commons.jexl.Expression;
 27  
 import org.apache.commons.jexl.ExpressionFactory;
 28  
 import org.apache.commons.scxml.Context;
 29  
 import org.apache.commons.scxml.Evaluator;
 30  
 import org.apache.commons.scxml.SCXMLExpressionException;
 31  
 import org.w3c.dom.Node;
 32  
 
 33  
 /**
 34  
  * Evaluator implementation enabling use of JEXL expressions in
 35  
  * SCXML documents.
 36  
  *
 37  
  */
 38  
 public class JexlEvaluator implements Evaluator {
 39  
 
 40  
     /** Error message if evaluation context is not a JexlContext. */
 41  
     private static final String ERR_CTX_TYPE = "Error evaluating JEXL "
 42  
         + "expression, Context must be a org.apache.commons.jexl.JexlContext";
 43  
 
 44  
     /** Pattern for recognizing the SCXML In() special predicate. */
 45  4
     private static Pattern inFct = Pattern.compile("In\\(");
 46  
     /** Pattern for recognizing the Commons SCXML Data() builtin function. */
 47  4
     private static Pattern dataFct = Pattern.compile("Data\\(");
 48  
 
 49  
     /** Constructor. */
 50  
     public JexlEvaluator() {
 51  28
         super();
 52  28
     }
 53  
 
 54  
     /**
 55  
      * Evaluate an expression.
 56  
      *
 57  
      * @param ctx variable context
 58  
      * @param expr expression
 59  
      * @return a result of the evaluation
 60  
      * @throws SCXMLExpressionException For a malformed expression
 61  
      * @see Evaluator#eval(Context, String)
 62  
      */
 63  
     public Object eval(final Context ctx, final String expr)
 64  
     throws SCXMLExpressionException {
 65  35
         if (expr == null) {
 66  0
             return null;
 67  
         }
 68  35
         JexlContext jexlCtx = null;
 69  35
         if (ctx instanceof JexlContext) {
 70  35
             jexlCtx = (JexlContext) ctx;
 71  
         } else {
 72  0
             throw new SCXMLExpressionException(ERR_CTX_TYPE);
 73  
         }
 74  35
         Expression exp = null;
 75  
         try {
 76  35
             String evalExpr = inFct.matcher(expr).
 77  
                 replaceAll("_builtin.isMember(_ALL_STATES, ");
 78  35
             evalExpr = dataFct.matcher(evalExpr).
 79  
                 replaceAll("_builtin.data(");
 80  35
             exp = ExpressionFactory.createExpression(evalExpr);
 81  35
             return exp.evaluate(getEffectiveContext(jexlCtx));
 82  0
         } catch (Exception e) {
 83  0
             throw new SCXMLExpressionException(e);
 84  
         }
 85  
     }
 86  
 
 87  
     /**
 88  
      * @see Evaluator#evalCond(Context, String)
 89  
      */
 90  
     public Boolean evalCond(final Context ctx, final String expr)
 91  
     throws SCXMLExpressionException {
 92  40
         if (expr == null) {
 93  0
             return null;
 94  
         }
 95  40
         JexlContext jexlCtx = null;
 96  40
         if (ctx instanceof JexlContext) {
 97  40
             jexlCtx = (JexlContext) ctx;
 98  
         } else {
 99  0
             throw new SCXMLExpressionException(ERR_CTX_TYPE);
 100  
         }
 101  40
         Expression exp = null;
 102  
         try {
 103  40
             String evalExpr = inFct.matcher(expr).
 104  
                 replaceAll("_builtin.isMember(_ALL_STATES, ");
 105  40
             evalExpr = dataFct.matcher(evalExpr).
 106  
                 replaceAll("_builtin.data(");
 107  40
             exp = ExpressionFactory.createExpression(evalExpr);
 108  40
             return (Boolean) exp.evaluate(getEffectiveContext(jexlCtx));
 109  0
         } catch (Exception e) {
 110  0
             throw new SCXMLExpressionException(e);
 111  
         }
 112  
     }
 113  
 
 114  
     /**
 115  
      * @see Evaluator#evalLocation(Context, String)
 116  
      */
 117  
     public Node evalLocation(final Context ctx, final String expr)
 118  
     throws SCXMLExpressionException {
 119  9
         if (expr == null) {
 120  0
             return null;
 121  
         }
 122  9
         JexlContext jexlCtx = null;
 123  9
         if (ctx instanceof JexlContext) {
 124  9
             jexlCtx = (JexlContext) ctx;
 125  
         } else {
 126  0
             throw new SCXMLExpressionException(ERR_CTX_TYPE);
 127  
         }
 128  9
         Expression exp = null;
 129  
         try {
 130  9
             String evalExpr = inFct.matcher(expr).
 131  
                 replaceAll("_builtin.isMember(_ALL_STATES, ");
 132  9
             evalExpr = dataFct.matcher(evalExpr).
 133  
                 replaceFirst("_builtin.dataNode(");
 134  9
             evalExpr = dataFct.matcher(evalExpr).
 135  
                 replaceAll("_builtin.data(");
 136  9
             exp = ExpressionFactory.createExpression(evalExpr);
 137  9
             return (Node) exp.evaluate(getEffectiveContext(jexlCtx));
 138  2
         } catch (Exception e) {
 139  2
             throw new SCXMLExpressionException(e);
 140  
         }
 141  
     }
 142  
 
 143  
     /**
 144  
      * Create a new child context.
 145  
      *
 146  
      * @param parent parent context
 147  
      * @return new child context
 148  
      * @see Evaluator#newContext(Context)
 149  
      */
 150  
     public Context newContext(final Context parent) {
 151  106
         return new JexlContext(parent);
 152  
     }
 153  
 
 154  
     /**
 155  
      * Create a new context which is the summation of contexts from the
 156  
      * current state to document root, child has priority over parent
 157  
      * in scoping rules.
 158  
      *
 159  
      * @param nodeCtx The JexlContext for this state.
 160  
      * @return The effective JexlContext for the path leading up to
 161  
      *         document root.
 162  
      */
 163  
     private JexlContext getEffectiveContext(final JexlContext nodeCtx) {
 164  84
         List contexts = new ArrayList();
 165  
         // trace path to root
 166  84
         JexlContext currentCtx = nodeCtx;
 167  317
         while (currentCtx != null) {
 168  233
             contexts.add(currentCtx);
 169  233
             currentCtx = (JexlContext) currentCtx.getParent();
 170  
         }
 171  84
         Map vars = new HashMap();
 172  
         // summation of the contexts, parent first, child wins
 173  317
         for (int i = contexts.size() - 1; i > -1; i--) {
 174  233
             vars.putAll(((JexlContext) contexts.get(i)).getVars());
 175  
         }
 176  84
         return new JexlContext(vars);
 177  
     }
 178  
 
 179  
 }
 180