01: /*
02: * $Id: BshUtil.java,v 1.1 2003/08/17 03:43:24 ajzeneski Exp $
03: *
04: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
05: *
06: * Permission is hereby granted, free of charge, to any person obtaining a
07: * copy of this software and associated documentation files (the "Software"),
08: * to deal in the Software without restriction, including without limitation
09: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10: * and/or sell copies of the Software, and to permit persons to whom the
11: * Software is furnished to do so, subject to the following conditions:
12: *
13: * The above copyright notice and this permission notice shall be included
14: * in all copies or substantial portions of the Software.
15: *
16: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23: *
24: */
25: package org.ofbiz.base.util;
26:
27: import java.util.*;
28: import bsh.*;
29:
30: /**
31: * BshUtil - BeanShell Utilities
32: *
33: *@author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
34: *@author Oswin Ondarza and Manuel Soto
35: *@created Oct 22, 2002
36: *@version $Revision: 1.1 $
37: */
38: public final class BshUtil {
39:
40: public static final String module = BshUtil.class.getName();
41:
42: /**
43: * Evaluate a BSH condition or expression
44: * @param expression The expression to evaluate
45: * @param context The context to use in evaluation (re-written)
46: * @return Object The result of the evaluation
47: * @throws EvalError
48: */
49: public static final Object eval(String expression, Map context)
50: throws EvalError {
51: Object o = null;
52: if (expression == null || expression.equals("")) {
53: Debug.logError("BSH Evaluation error. Empty expression",
54: module);
55: return null;
56: }
57:
58: if (Debug.verboseOn())
59: Debug.logVerbose("Evaluating -- " + expression, module);
60: if (Debug.verboseOn())
61: Debug.logVerbose("Using Context -- " + context, module);
62:
63: try {
64: Interpreter bsh = makeInterpreter(context);
65: // evaluate the expression
66: o = bsh.eval(expression);
67: if (Debug.verboseOn())
68: Debug.logVerbose("Evaluated to -- " + o, module);
69:
70: // read back the context info
71: NameSpace ns = bsh.getNameSpace();
72: String[] varNames = ns.getVariableNames();
73: for (int x = 0; x < varNames.length; x++) {
74: context.put(varNames[x], bsh.get(varNames[x]));
75: }
76: } catch (EvalError e) {
77: Debug.logError(e, "BSH Evaluation error.", module);
78: throw e;
79: }
80: return o;
81: }
82:
83: public static Interpreter makeInterpreter(Map context)
84: throws EvalError {
85: Interpreter bsh = new Interpreter();
86: // Set the context for the condition
87: if (context != null) {
88: Set keySet = context.keySet();
89: Iterator i = keySet.iterator();
90: while (i.hasNext()) {
91: Object key = i.next();
92: Object value = context.get(key);
93: bsh.set((String) key, value);
94: }
95: }
96:
97: return bsh;
98: }
99: }
|