001: /*
002: * Copyright 2004,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.bsf;
018:
019: import java.awt.Frame;
020: import java.awt.event.WindowAdapter;
021: import java.awt.event.WindowEvent;
022: import java.io.FileReader;
023: import java.io.FileWriter;
024: import java.io.IOException;
025: import java.io.InputStreamReader;
026: import java.io.PrintWriter;
027: import java.io.Reader;
028: import java.util.Hashtable;
029:
030: import org.apache.bsf.util.CodeBuffer;
031: import org.apache.bsf.util.IOUtils;
032:
033: /**
034: * This is the main driver for BSF to be run on the command line
035: * to eval/exec/compile scripts directly.
036: *
037: * @author Sanjiva Weerawarana
038: * @author Matthew J. Duftler
039: * @author Sam Ruby
040: */
041: public class Main {
042: private static String ARG_IN = "-in";
043: private static String ARG_LANG = "-lang";
044: private static String ARG_MODE = "-mode";
045: private static String ARG_OUT = "-out";
046: private static String ARG_VAL_EVAL = "eval";
047: private static String ARG_VAL_EXEC = "exec";
048: private static String ARG_VAL_COMPILE = "compile";
049: private static String DEFAULT_IN_FILE_NAME = "<STDIN>";
050: private static String DEFAULT_MODE = ARG_VAL_EVAL;
051: private static String DEFAULT_CLASS_NAME = "Test";
052:
053: /**
054: * Static driver to be able to run BSF scripts from the command line.
055: *
056: * @param args command line arguments
057: *
058: * @exception IOException if any I/O error while loading script
059: */
060: public static void main(String[] args) throws IOException {
061: try {
062: if ((args.length == 0) || (args.length % 2 != 0)) {
063: printHelp();
064: System.exit(1);
065: }
066:
067: Hashtable argsTable = new Hashtable();
068:
069: argsTable.put(ARG_OUT, DEFAULT_CLASS_NAME);
070: argsTable.put(ARG_MODE, DEFAULT_MODE);
071:
072: for (int i = 0; i < args.length; i += 2) {
073: argsTable.put(args[i], args[i + 1]);
074: }
075:
076: String inFileName = (String) argsTable.get(ARG_IN);
077: String language = (String) argsTable.get(ARG_LANG);
078:
079: if (language == null) {
080: if (inFileName != null) {
081: language = BSFManager
082: .getLangFromFilename(inFileName);
083: } else {
084: throw new BSFException(
085: BSFException.REASON_OTHER_ERROR,
086: "unable to determine language");
087: }
088: }
089:
090: Reader in;
091:
092: if (inFileName != null) {
093: in = new FileReader(inFileName);
094: } else {
095: in = new InputStreamReader(System.in);
096: inFileName = DEFAULT_IN_FILE_NAME;
097: }
098:
099: BSFManager mgr = new BSFManager();
100: String mode = (String) argsTable.get(ARG_MODE);
101:
102: if (mode.equals(ARG_VAL_COMPILE)) {
103: String outClassName = (String) argsTable.get(ARG_OUT);
104: FileWriter out = new FileWriter(outClassName + ".java");
105: PrintWriter pw = new PrintWriter(out);
106:
107: CodeBuffer cb = new CodeBuffer();
108: cb.setClassName(outClassName);
109: mgr.compileScript(language, inFileName, 0, 0, IOUtils
110: .getStringFromReader(in), cb);
111: cb.print(pw, true);
112: out.close();
113: } else {
114: if (mode.equals(ARG_VAL_EXEC)) {
115: mgr.exec(language, inFileName, 0, 0, IOUtils
116: .getStringFromReader(in));
117: } else { /* eval */
118: Object obj = mgr.eval(language, inFileName, 0, 0,
119: IOUtils.getStringFromReader(in));
120:
121: // Try to display the result.
122:
123: if (obj instanceof java.awt.Component) {
124: Frame f;
125: if (obj instanceof Frame) {
126: f = (Frame) obj;
127: } else {
128: f = new Frame("BSF Result: " + inFileName);
129: f.add((java.awt.Component) obj);
130: }
131: // Add a window listener to quit on closing.
132: f.addWindowListener(new WindowAdapter() {
133: public void windowClosing(WindowEvent e) {
134: System.exit(0);
135: }
136: });
137: f.pack();
138: // f.show(); // javac 1.5 warns to use f.show(), Apache build scripts abort as a result :(
139: f.setVisible(true); // available since Java 1.1
140: } else {
141: System.err.println("Result: " + obj);
142:
143: }
144:
145: System.err.println("Result: " + obj);
146: }
147: }
148: } catch (BSFException e) {
149: e.printStackTrace();
150: }
151: }
152:
153: private static void printHelp() {
154: System.err.println("Usage:");
155: System.err.println();
156: System.err
157: .println(" java " + Main.class.getName() + " [args]");
158: System.err.println();
159: System.err.println(" args:");
160: System.err.println();
161: System.err
162: .println(" [-in fileName] default: "
163: + DEFAULT_IN_FILE_NAME);
164: System.err
165: .println(" [-lang languageName] default: "
166: + "<If -in is specified and -lang");
167: System.err
168: .println(" "
169: + " is not, attempt to determine");
170: System.err
171: .println(" "
172: + " language from file extension;");
173: System.err
174: .println(" "
175: + " otherwise, -lang is required.>");
176: System.err
177: .println(" [-mode (eval|exec|compile)] default: "
178: + DEFAULT_MODE);
179: System.err.println();
180: System.err.println(" Additional args used only if -mode is "
181: + "set to \"compile\":");
182: System.err.println();
183: System.err
184: .println(" [-out className] default: "
185: + DEFAULT_CLASS_NAME);
186: }
187: }
|