01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: ScriptedEngineRhino.java 3784 2007-06-11 16:44:35Z gbevin $
07: */
08: package com.uwyn.rife.engine;
09:
10: import com.uwyn.rife.engine.exceptions.ChildTriggerNotImplementedException;
11: import com.uwyn.rife.engine.exceptions.EngineException;
12: import com.uwyn.rife.engine.exceptions.ScriptErrorException;
13: import com.uwyn.rife.tools.Convert;
14: import com.uwyn.rife.tools.exceptions.LightweightError;
15: import org.mozilla.javascript.Context;
16: import org.mozilla.javascript.Function;
17: import org.mozilla.javascript.Scriptable;
18: import org.mozilla.javascript.ScriptableObject;
19: import org.mozilla.javascript.WrappedException;
20:
21: class ScriptedEngineRhino extends ScriptedEngine {
22: private Context mContext = null;
23: private Scriptable mScope = null;
24:
25: ScriptedEngineRhino(String code) throws EngineException {
26: super ("javascript", code);
27:
28: mContext = Context.enter();
29: mScope = mContext.initStandardObjects();
30: }
31:
32: void processElement() throws EngineException {
33: Object wrappedOut = Context.javaToJS(getElement(), mScope);
34: ScriptableObject.putProperty(mScope, "element", wrappedOut);
35: try {
36: mContext.evaluateString(mScope, mCode, getElement()
37: .getElementInfo().getImplementation(), 1, null);
38: } catch (WrappedException e) {
39: if (e.getWrappedException() instanceof LightweightError) {
40: throw (LightweightError) e.getWrappedException();
41: } else if (e.getWrappedException() instanceof EngineException) {
42: throw (EngineException) e.getWrappedException();
43: }
44:
45: throw new ScriptErrorException(getElement()
46: .getElementInfo().getDeclarationName(), e);
47: } finally {
48: Context.exit();
49: }
50: }
51:
52: boolean childTriggered(String name, String[] values) {
53: Object function_object = mScope.get("childTriggered", mScope);
54: if (!(function_object instanceof Function)) {
55: throw new ChildTriggerNotImplementedException(getElement()
56: .getDeclarationName(), name);
57: } else {
58: Function function = (Function) function_object;
59: try {
60: Object result_object = function.call(mContext, mScope,
61: mScope, new Object[] { name, values });
62: String result = Context.toString(result_object);
63: return Convert.toBoolean(result, false);
64: } catch (WrappedException e) {
65: if (e.getWrappedException() instanceof LightweightError) {
66: throw (LightweightError) e.getWrappedException();
67: } else if (e.getWrappedException() instanceof EngineException) {
68: throw (EngineException) e.getWrappedException();
69: }
70:
71: throw new ScriptErrorException(getElement()
72: .getElementInfo().getDeclarationName(), e);
73: }
74: }
75: }
76: }
|