1 55 56 package org.apache.bsf; 57 58 import java.awt.Frame ; 59 import java.awt.event.*; 60 61 import java.io.*; 62 import java.util.Hashtable ; 63 64 import org.apache.bsf.util.*; 65 66 74 public class Main { 75 private static String ARG_IN = "-in"; 76 private static String ARG_LANG = "-lang"; 77 private static String ARG_MODE = "-mode"; 78 private static String ARG_OUT = "-out"; 79 private static String ARG_VAL_EVAL = "eval"; 80 private static String ARG_VAL_EXEC = "exec"; 81 private static String ARG_VAL_COMPILE = "compile"; 82 private static String DEFAULT_IN_FILE_NAME = "<STDIN>"; 83 private static String DEFAULT_MODE = ARG_VAL_EVAL; 84 private static String DEFAULT_CLASS_NAME = "Test"; 85 86 93 public static void main(String [] args) throws IOException { 94 try { 95 if ((args.length == 0) || (args.length % 2 != 0)) { 96 printHelp(); 97 System.exit(1); 98 } 99 100 Hashtable argsTable = new Hashtable (); 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 inFileName = (String ) argsTable.get(ARG_IN); 110 String language = (String ) 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 mode = (String ) argsTable.get(ARG_MODE); 133 134 if (mode.equals(ARG_VAL_COMPILE)) { 135 String outClassName = (String ) 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 { 154 Object obj = 155 mgr.eval(language, inFileName, 0, 0, IOUtils.getStringFromReader(in)); 156 157 159 if (obj instanceof java.awt.Component ) { 160 Frame f; 161 162 if (obj instanceof Frame ) { 163 f = (Frame ) obj; 164 } else { 165 f = new Frame ("BSF Result: " + inFileName); 166 f.add ((java.awt.Component ) obj); 167 } 168 169 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 |