001: /*
002: * Copyright (C) 2007 Jared Alexander Spigner
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * jspigner@openjx.org
019: *
020: * JXInterpreter.java
021: *
022: * Created on June 9, 2007, 11:19 PM
023: *
024: */
025:
026: package org.openjx.core;
027:
028: import java.lang.reflect.Constructor;
029: import java.lang.reflect.Field;
030: import java.lang.reflect.Method;
031:
032: import javax.script.ScriptEngine;
033: import javax.script.ScriptEngineManager;
034:
035: import javax.swing.JOptionPane;
036:
037: import org.openjx.conf.JXGLOBAL;
038: import org.openjx.display.JXMessageBox;
039:
040: /**
041: * This class interprets the object stack into Java and executes it.
042: *
043: * @author Jared Spigner
044: */
045: public class JXInterpreter {
046:
047: /** This is a reference to the Virtual Machine. */
048: private VirtualMachine virtualMachine;
049:
050: /** This is a reference to the ScriptEngine. */
051: private ScriptEngine scriptEngine;
052:
053: /** This is a reference to the ScriptEngineManager. */
054: private ScriptEngineManager scriptEngineManager;
055:
056: /**
057: * This the constructor for the JXInterpreter class. It creates a new
058: * instance of JXInterpreter.
059: *
060: * @param vm is a reference to the virtual machine.
061: */
062: public JXInterpreter(VirtualMachine vm) {
063: this .virtualMachine = vm;
064: }
065:
066: /**
067: * This method executes a specific script.
068: *
069: * @param script is the script we want to execute.
070: *
071: * @return true on success, else false on failure.
072: */
073: public boolean executeScript(String script) {
074: try {
075: if (script != null && !script.equals(""))
076: this .scriptEngine.eval(script);
077: } catch (Exception e) {
078: new JXMessageBox(JOptionPane.ERROR_MESSAGE,
079: "Interpreter Error", e.getMessage(),
080: this .virtualMachine.getViewer());
081: return false;
082: }
083:
084: return true;
085: }
086:
087: /**
088: * This method interprets individual elements of the stack.
089: *
090: * @param jxObject is the object we want to interpret.
091: *
092: * @return true on success, else false on failure.
093: */
094: public boolean interpretJXObject(JXObject jxObject) {
095: if (JXGLOBAL.DEBUG_INTERPRET)
096: System.out.println("INTERPRET JXOBJECT: "
097: + jxObject.getName());
098:
099: try {
100: Class c = null;
101:
102: try {
103: c = Class.forName("org.openjx.jx." + jxObject.getTAG()
104: + "CTL");
105: } catch (ClassNotFoundException e) {
106: try {
107: c = Class.forName(jxObject.getTAG() + "CTL");
108: } catch (ClassNotFoundException e2) {
109: new JXMessageBox(JOptionPane.ERROR_MESSAGE,
110: "JXObject Interpretation Error",
111: "Can not find class " + e.getMessage()
112: + " or class " + e2.getMessage(),
113: this .virtualMachine.getViewer());
114: return false;
115: }
116: }
117:
118: Class parameters[] = { VirtualMachine.class };
119: Constructor cr = c.getConstructor(parameters);
120: Object obj = cr.newInstance(this .virtualMachine);
121:
122: c = obj.getClass();
123: Class p[] = { JXObject.class };
124: Method method = c.getMethod("Interpret", p);
125: Object arguments[] = { jxObject };
126: return (Boolean) method.invoke(obj, arguments);
127: } catch (Exception e) {
128: new JXMessageBox(JOptionPane.ERROR_MESSAGE,
129: "Interpreter Error", "Error interpreting class "
130: + jxObject.getTAG() + "CTL, "
131: + e.getMessage(), this .virtualMachine
132: .getViewer());
133: return false;
134: }
135: }
136:
137: /**
138: * This method interprets the jxscript by walking the stack.
139: *
140: * @param jxObject is the object we want to start interpretation with.
141: *
142: * @return true on success, else false on failure.
143: */
144: public boolean interpretScript(JXObject jxObject) {
145: for (int i = 0; i < jxObject.getLength(); i++) {
146: JXObject tmpObject = jxObject.stack.elementAt(i);
147:
148: this .interpretScript(tmpObject);
149: this .interpretJXObject(tmpObject);
150: }
151:
152: return true;
153: }
154:
155: /**
156: * This method interprets the jxscript by walking the stack.
157: *
158: * @param jxObject is the top level JXObject.
159: *
160: * @return true on success, else false on failure.
161: */
162: public boolean oldinterpretScript(JXObject jxObject) {
163: for (int i = 0; i < jxObject.getLength(); i++) {
164: JXObject tmpObject = jxObject.stack.elementAt(i);
165:
166: this .interpretScript(tmpObject);
167: this .interpretJXObject(tmpObject);
168: }
169:
170: this .interpretJXObject(this .virtualMachine.stackObject);
171:
172: return this .interpretScript(this .virtualMachine.stackObject);
173:
174: }
175:
176: /**
177: * This method sets the script engine and script engine manager. It also
178: * makes the virtual machine available to the scripting language.
179: *
180: * @param se is the script engine we want to set.
181: * @param sem is the script engine manager we want to set.
182: */
183: public void setScriptEngine(ScriptEngine se, ScriptEngineManager sem) {
184:
185: this .scriptEngine = se;
186: this .scriptEngineManager = sem;
187:
188: this .scriptEngine.put("VM", this.virtualMachine);
189: }
190:
191: }
|