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: * OpenJX.java
021: *
022: * Created on June 7, 2007, 11:44 PM
023: *
024: */
025:
026: package org.openjx;
027:
028: import javax.swing.JOptionPane;
029:
030: import org.openjx.display.JXMessageBox;
031: import org.openjx.display.JXViewer;
032:
033: import org.openjx.core.VirtualMachine;
034:
035: /**
036: * This is the main class for the OpenJX virtual machine. It handles the
037: * initial setup and execution of the virtual machine.
038: *
039: * @author Jared Spigner
040: */
041: public class OpenJX extends JXViewer {
042:
043: /** This is the identifier of the class. */
044: private static final long serialVersionUID = 1999L;
045:
046: /**
047: * This is the base name of the application, no prefix or suffix included.
048: */
049: private String applicationName;
050:
051: /** This is the mode of operation (desktop or applet) application. */
052: public String applicationMode = "desktop";
053:
054: /**
055: * This is a reference to the Virtual Machine.
056: */
057: private VirtualMachine virtualMachine;
058:
059: /**
060: * This is the main method to the OpenJX application. It is only used
061: * when OpenJX is to be run as an application. It instantiates the class
062: * with the command line argument array.
063: *
064: * @param argv is an array of command line arguments to the application.
065: */
066: public static void main(String[] argv) {
067: OpenJX openJX = new OpenJX(argv);
068: }
069:
070: /**
071: * This is the main method to the OpenJX applet. It is only used when
072: * OpenJX is to be run as an applet. It instantiates the class with the
073: * applet parameters.
074: */
075: public void init() {
076: String[] argv = new String[2];
077:
078: argv[0] = "-a" + this .getParameter("applicationName");
079: argv[1] = "-m" + "applet";
080:
081: this .executeJX(argv);
082: }
083:
084: /**
085: * This is the constructor for the OpenJX application.
086: */
087: public OpenJX() {
088: }
089:
090: /**
091: * This is the constructor for the OpenJX application. It creates a new
092: * instance of OpenJX based on the argument array passed to it.
093: *
094: * @param argv is an array of command line arguments to the application.
095: */
096: public OpenJX(String[] argv) {
097: this .executeJX(argv);
098: }
099:
100: /**
101: * This is called by the constructor for the OpenJX application.
102: * It creates a new instance of OpenJX based on the argument array passed
103: * to it.
104: *
105: * @param argv is an array of command line arguments to the application.
106: */
107: public void executeJX(String[] argv) {
108: this .applicationName = "";
109: this .applicationMode = "";
110:
111: this .parseArguments(argv);
112:
113: this .virtualMachine = new VirtualMachine();
114: this .virtualMachine.setViewer(this );
115: this .virtualMachine.setApplicationFile(this .applicationName
116: + ".jx");
117:
118: if (!this .virtualMachine.parsePlugins()) {
119: System.exit(0);
120: }
121:
122: if (!this .virtualMachine.loadPlugins()) {
123: System.exit(0);
124: }
125:
126: if (!this .virtualMachine.parseProgram()) {
127: System.exit(0);
128: }
129:
130: if (!this .virtualMachine.compileProgram()) {
131: System.exit(0);
132: }
133:
134: if (!this .displayMode(this .applicationMode)) {
135: System.exit(0);
136: }
137:
138: if (!this .virtualMachine.interpretProgram()) {
139: System.exit(0);
140: }
141:
142: if (!this .virtualMachine.displayProgram(this , 1)) {
143: System.exit(0);
144: }
145: }
146:
147: /**
148: * This method parses the command line arguments and test for missing
149: * required arguments. If any are missing it calls the printUsage method
150: * and then exits the application.
151: *
152: * @param argv is an array of command line arguments to the application.
153: */
154: public void parseArguments(String[] argv) {
155: for (int x = 0; x < argv.length; x++) {
156: char args[] = argv[x].toCharArray();
157:
158: if (args[0] == '-' && args[1] != 0) {
159: switch (args[1]) {
160: case 'a':
161: this .applicationName = argv[x].substring(2, argv[x]
162: .length());
163: break;
164: case 'p':
165: break;
166: case 'j':
167: break;
168: case 'm':
169: this .applicationMode = argv[x].substring(2, argv[x]
170: .length());
171: break;
172: default:
173: this .printUsage();
174: System.exit(0);
175: }
176: }
177: }
178:
179: if (this .applicationName.equals("")) {
180: this .printUsage();
181: System.exit(0);
182: }
183: }
184:
185: public void printUsage() {
186: System.out
187: .println("usage ./java -jar OpenJX.jar -a[applicationName] -m[mode(desktop|applet default:desktop)]");
188: }
189:
190: }
|