KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > bsf > Main


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2002 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if
20  * any, must include the following acknowlegement:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowlegement may appear in the software itself,
24  * if and wherever such third-party acknowlegements normally appear.
25  *
26  * 4. The names "Apache BSF", "Apache", and "Apache Software Foundation"
27  * must not be used to endorse or promote products derived from
28  * this software without prior written permission. For written
29  * permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache"
32  * nor may "Apache" appear in their names without prior written
33  * permission of the Apache Group.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many individuals
50  * on behalf of the Apache Software Foundation and was originally created by
51  * Sanjiva Weerawarana and others at International Business Machines
52  * Corporation. For more information on the Apache Software Foundation,
53  * please see <http://www.apache.org/>.
54  */

55
56 package org.apache.bsf;
57
58 import java.awt.Frame JavaDoc;
59 import java.awt.event.*;
60
61 import java.io.*;
62 import java.util.Hashtable JavaDoc;
63
64 import org.apache.bsf.util.*;
65
66 /**
67  * This is the main driver for BSF to be run on the command line
68  * to eval/exec/compile scripts directly.
69  *
70  * @author Sanjiva Weerawarana
71  * @author Matthew J. Duftler
72  * @author Sam Ruby
73  */

74 public class Main {
75     private static String JavaDoc ARG_IN = "-in";
76     private static String JavaDoc ARG_LANG = "-lang";
77     private static String JavaDoc ARG_MODE = "-mode";
78     private static String JavaDoc ARG_OUT = "-out";
79     private static String JavaDoc ARG_VAL_EVAL = "eval";
80     private static String JavaDoc ARG_VAL_EXEC = "exec";
81     private static String JavaDoc ARG_VAL_COMPILE = "compile";
82     private static String JavaDoc DEFAULT_IN_FILE_NAME = "<STDIN>";
83     private static String JavaDoc DEFAULT_MODE = ARG_VAL_EVAL;
84     private static String JavaDoc DEFAULT_CLASS_NAME = "Test";
85
86     /**
87      * Static driver to be able to run BSF scripts from the command line.
88      *
89      * @param args command line arguments
90      *
91      * @exception IOException if any I/O error while loading script
92      */

93     public static void main(String JavaDoc[] args) throws IOException {
94         try {
95             if ((args.length == 0) || (args.length % 2 != 0)) {
96                 printHelp();
97                 System.exit(1);
98             }
99
100             Hashtable JavaDoc argsTable = new Hashtable JavaDoc();
101
102             argsTable.put(ARG_OUT, DEFAULT_CLASS_NAME);
103             argsTable.put(ARG_MODE, DEFAULT_MODE);
104
105             for (int i = 0; i < args.length; i += 2) {
106                 argsTable.put(args[i], args[i + 1]);
107             }
108
109             String JavaDoc inFileName = (String JavaDoc) argsTable.get(ARG_IN);
110             String JavaDoc language = (String JavaDoc) argsTable.get(ARG_LANG);
111
112             if (language == null) {
113                 if (inFileName != null) {
114                     language = BSFManager.getLangFromFilename(inFileName);
115                 } else {
116                     throw new BSFException(
117                         BSFException.REASON_OTHER_ERROR,
118                         "unable to determine language");
119                 }
120             }
121
122             Reader in;
123
124             if (inFileName != null) {
125                 in = new FileReader(inFileName);
126             } else {
127                 in = new InputStreamReader(System.in);
128                 inFileName = "<STDIN>";
129             }
130
131             BSFManager mgr = new BSFManager();
132             String JavaDoc mode = (String JavaDoc) argsTable.get(ARG_MODE);
133
134             if (mode.equals(ARG_VAL_COMPILE)) {
135                 String JavaDoc outClassName = (String JavaDoc) argsTable.get(ARG_OUT);
136                 FileWriter out = new FileWriter(outClassName + ".java");
137                 PrintWriter pw = new PrintWriter(out);
138
139                 CodeBuffer cb = new CodeBuffer();
140                 cb.setClassName(outClassName);
141                 mgr.compileScript(
142                     language,
143                     inFileName,
144                     0,
145                     0,
146                     IOUtils.getStringFromReader(in),
147                     cb);
148                 cb.print(pw, true);
149                 out.close();
150             } else
151                 if (mode.equals(ARG_VAL_EXEC)) {
152                     mgr.exec(language, inFileName, 0, 0, IOUtils.getStringFromReader(in));
153                 } else /* eval */ {
154                     Object JavaDoc obj =
155                                             mgr.eval(language, inFileName, 0, 0, IOUtils.getStringFromReader(in));
156                                         
157                     // Try to display the result.
158

159                     if (obj instanceof java.awt.Component JavaDoc) {
160                                             Frame JavaDoc f;
161                                             
162                                             if (obj instanceof Frame JavaDoc) {
163                         f = (Frame JavaDoc) obj;
164                                             } else {
165                         f = new Frame JavaDoc ("BSF Result: " + inFileName);
166                         f.add ((java.awt.Component JavaDoc) obj);
167                                             }
168                                             
169                                             // Add a window listener to quit on closing.
170
f.addWindowListener(
171                                                                 new WindowAdapter () {
172                                                                     public void windowClosing (WindowEvent e) {
173                                                                         System.exit (0);
174                                                                     }
175                                                                 });
176                                             f.pack ();
177                                             f.show ();
178                     } else {
179                                             System.err.println("Result: " +
180                                                                obj);
181                     }
182                                         
183                     System.err.println("Result: " + obj);
184                 }
185         } catch (BSFException e) {
186                     e.printStackTrace();
187         }
188     }
189     private static void printHelp() {
190         System.err.println("Usage:");
191         System.err.println();
192         System.err.println(" java " + Main.class.getName() + " [args]");
193         System.err.println();
194         System.err.println(" args:");
195         System.err.println();
196         System.err.println(
197             " [-in fileName] default: " + DEFAULT_IN_FILE_NAME);
198         System.err.println(
199             " [-lang languageName] default: "
200                 + "<If -in is specified and -lang");
201         System.err.println(
202             " "
203                 + " is not, attempt to determine");
204         System.err.println(
205             " "
206                 + " language from file extension;");
207         System.err.println(
208             " "
209                 + " otherwise, -lang is required.>");
210         System.err.println(
211             " [-mode (eval|exec|compile)] default: " + DEFAULT_MODE);
212         System.err.println();
213         System.err.println(
214             " Additional args used only if -mode is " + "set to \"compile\":");
215         System.err.println();
216         System.err.println(
217             " [-out className] default: " + DEFAULT_CLASS_NAME);
218     }
219 }
220
Popular Tags