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: * JXCompiler.java
021: *
022: * Created on June 8, 2007, 11:36 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.swing.JOptionPane;
033:
034: import javax.script.ScriptEngine;
035: import javax.script.ScriptEngineManager;
036:
037: import org.openjx.conf.JXGLOBAL;
038:
039: import org.openjx.display.JXMessageBox;
040:
041: /**
042: * This class handles compilation of a JXObject into a Java class.
043: *
044: * @author Jared Spigner
045: */
046: public class JXCompiler {
047: /** This is a reference to the Virtual Machine. */
048: public VirtualMachine virtualMachine;
049:
050: /** This is a reference to the ScriptEngine. */
051: public ScriptEngine scriptEngine;
052:
053: /** This is a reference to the ScriptEngineManager. */
054: public ScriptEngineManager scriptEngineManager;
055:
056: /**
057: * This the constructor for the JXCompiler class. It creates a new
058: * instance of JXCompiler.
059: *
060: * @param vm is a reference to the virtual machine.
061: */
062: public JXCompiler(VirtualMachine vm) {
063: this .virtualMachine = vm;
064: }
065:
066: /**
067: * This method compiles the script recursively starting with the form
068: * object.
069: *
070: * @param jxObject is the object we want to start compilation with.
071: *
072: * @return true on success, else false on failure.
073: */
074: public boolean Compile(JXObject jxObject) {
075: if (!this .compileJXObject(jxObject)) {
076: return false;
077: }
078:
079: return this .compileScript(jxObject);
080: }
081:
082: /**
083: * This method compiles individual elements of the stack.
084: *
085: * @param jxObject is the object we want to compile.
086: *
087: * @return true on success, else false on failure.
088: */
089: public boolean compileJXObject(JXObject jxObject) {
090: if (JXGLOBAL.DEBUG_COMPILE)
091: System.out.println("COMPILE JXOBJECT: "
092: + jxObject.getName() + " object parent:"
093: + jxObject.getLast().getName());
094:
095: try {
096: Class c = null;
097:
098: try {
099: c = Class.forName("org.openjx.jx." + jxObject.getTAG()
100: + "CTL");
101: } catch (ClassNotFoundException e) {
102: try {
103: c = Class.forName(jxObject.getTAG() + "CTL");
104: } catch (ClassNotFoundException e2) {
105: new JXMessageBox(JOptionPane.ERROR_MESSAGE,
106: "JXObject Class Not Found Error",
107: "Can not find class " + e.getMessage()
108: + " or class " + e2.getMessage(),
109: this .virtualMachine.getViewer());
110: return false;
111: }
112: }
113:
114: Class parameters[] = { VirtualMachine.class };
115: Constructor cr = c.getConstructor(parameters);
116: Object obj = cr.newInstance(this .virtualMachine);
117:
118: c = obj.getClass();
119: Class p[] = { JXObject.class };
120: Method method = c.getMethod("Compile", p);
121: Object arguments[] = { jxObject };
122: return (Boolean) method.invoke(obj, arguments);
123: } catch (Exception e) {
124: new JXMessageBox(JOptionPane.ERROR_MESSAGE,
125: "JXObject Compilation Error", e.getMessage(),
126: this .virtualMachine.getViewer());
127: return false;
128: }
129: }
130:
131: /**
132: * This method compiles the script recursively.
133: *
134: * @param jxObject is the object we want to start compilation with.
135: *
136: * @return true on success, else false on failure.
137: */
138: public boolean compileScript(JXObject jxObject) {
139: for (int i = 0; i < jxObject.getLength(); i++) {
140: JXObject tmpObject = jxObject.stack.elementAt(i);
141:
142: this .compileScript(tmpObject);
143: this .compileJXObject(tmpObject);
144: }
145:
146: return true;
147: }
148:
149: /**
150: * This method initializes and sets up the scripting engine. We are using
151: * JavaScript, perhaps in the future we can add to the language base.
152: */
153: public void setupScriptEngine() {
154: this .scriptEngineManager = new ScriptEngineManager();
155: this .scriptEngine = this .scriptEngineManager
156: .getEngineByName("JavaScript");
157: }
158:
159: }
|