1 package org.antlr.works.engine; 2 3 import org.antlr.works.IDE; 4 import org.antlr.works.prefs.AWPrefs; 5 import org.antlr.works.utils.Console; 6 import org.antlr.works.utils.StreamWatcher; 7 import org.antlr.works.utils.StreamWatcherDelegate; 8 import org.antlr.works.utils.Utils; 9 import org.antlr.xjlib.foundation.XJSystem; 10 import org.antlr.xjlib.foundation.XJUtils; 11 12 import java.io.File ; 13 import java.lang.reflect.Method ; 14 import java.util.HashMap ; 15 import java.util.Map ; 16 46 47 public class EngineRuntime { 48 49 public static Map <Thread ,Process > processPerThread = new HashMap <Thread , Process >(); 50 51 public static void setProcess(Process p) { 52 processPerThread.put(Thread.currentThread(), p); 53 } 54 55 public static void removeProcess() { 56 processPerThread.remove(Thread.currentThread()); 57 } 58 59 public static Process getProcess(Thread t) { 60 return processPerThread.get(t); 61 } 62 63 public static Process getProcess() { 64 return processPerThread.get(Thread.currentThread()); 65 } 66 67 public static String getClassPath(String outputPath) { 68 String appPath = IDE.getApplicationPath(); 69 70 String isDebuggingPlugin = System.getProperty("org.antlr.works.debug.plugin"); 71 if(isDebuggingPlugin != null && Boolean.parseBoolean(isDebuggingPlugin)) { 72 String sandbox = XJUtils.getPathByDeletingLastComponent(appPath)+File.separatorChar+"lib"; 75 File dir = new File (sandbox); 76 if(dir.isDirectory()) { 77 for(File f : dir.listFiles()) { 78 String candidate = f.getAbsolutePath(); 79 if(candidate.endsWith(".jar")) { 80 appPath += File.pathSeparatorChar+candidate; 81 } 82 } 83 } 84 } 85 86 String classPath = outputPath; 89 if(appPath != null) 90 classPath += File.pathSeparatorChar+Utils.unquotePath(appPath); 91 92 if(AWPrefs.getUseSystemClassPath()) 93 classPath += File.pathSeparatorChar+Utils.unquotePath(System.getProperty("java.class.path")); 94 95 if(AWPrefs.getUseCustomClassPath()) 96 classPath += File.pathSeparatorChar+Utils.unquotePath(AWPrefs.getCustomClassPath()); 97 98 classPath += File.pathSeparatorChar+"."; 99 100 if(XJSystem.isWindows()) 104 return Utils.quotePath(classPath); 105 else 106 return classPath; 107 } 108 109 public static String runANTLR(Console console, String file, String libPath, String outputPath, StreamWatcherDelegate delegate) { 110 String error = null; 111 StreamWatcher esw = null; 112 int result = 0; 113 try { 114 String [] args = new String [9]; 115 args[0] = "java"; 116 args[1] = "-cp"; 117 args[2] = getClassPath(outputPath); 118 args[3] = "org.antlr.Tool"; 119 args[4] = "-o"; 120 args[5] = Utils.quotePath(outputPath); 121 args[6] = "-lib"; 122 args[7] = Utils.quotePath(libPath); 123 args[8] = file; 124 125 IDE.debugVerbose(console, EngineRuntime.class, "Run ANTLR: "+Utils.toString(args)); 126 127 Process p = Runtime.getRuntime().exec(args); 128 setProcess(p); 129 esw = new StreamWatcher(p.getErrorStream(), "ANTLR[error]", delegate); 130 esw.start(); 131 new StreamWatcher(p.getInputStream(), "ANTLR[stdout]", delegate).start(); 132 result = p.waitFor(); 133 } catch(Exception e) { 134 error = "Failed to run ANTLR with exception:\n"+e.toString(); 135 } finally { 136 removeProcess(); 137 } 138 139 if(result != 0) { 140 error = "Failed to run ANTLR with result:\n"+result; 141 } 142 143 146 147 if(error == null && esw != null) { 148 for (String line : esw.getLines()) { 149 if (line.startsWith("ANTLR Parser Generator")) 150 continue; 151 152 if (line.startsWith("no such locale file")) 153 continue; 154 155 error = line; 156 break; 157 } 158 } 159 return error; 160 } 161 162 public static String runJava(Console console, String currentPath, String [] params, StreamWatcherDelegate delegate) { 163 String error = null; 164 int result = 0; 165 try { 166 String [] args = new String [3+params.length]; 167 args[0] = "java"; 168 args[1] = "-cp"; 169 args[2] = getClassPath(currentPath); 170 System.arraycopy(params, 0, args, 3, params.length); 171 172 IDE.debugVerbose(console, EngineRuntime.class, "Run Java: "+Utils.toString(args)); 173 174 Process p = Runtime.getRuntime().exec(args, null, new File (currentPath)); 175 setProcess(p); 176 new StreamWatcher(p.getErrorStream(), "Java[error]", delegate).start(); 177 new StreamWatcher(p.getInputStream(), "Java[stdout]", delegate).start(); 178 result = p.waitFor(); 179 } catch(Exception e) { 180 error = "Failed to run Java with exception:\n"+e.toString(); 181 } finally { 182 removeProcess(); 183 } 184 185 if(result != 0) { 186 error = "Failed to run Java with result:\n"+result; 187 } 188 189 return error; 190 } 191 192 public static String compileFiles(Console console, String [] files, String outputFileDir, StreamWatcherDelegate delegate) { 193 String error = null; 194 195 int result = 0; 196 try { 197 String compiler = AWPrefs.getCompiler(); 198 String classPath = getClassPath(outputFileDir); 199 200 if(compiler.equalsIgnoreCase(AWPrefs.COMPILER_JAVAC)) { 201 String [] args = new String [5+files.length]; 202 if(AWPrefs.getJavaCCustomPath()) 203 args[0] = XJUtils.concatPath(AWPrefs.getJavaCPath(), "javac"); 204 else 205 args[0] = "javac"; 206 args[1] = "-classpath"; 207 args[2] = classPath; 208 args[3] = "-d"; 209 args[4] = Utils.quotePath(outputFileDir); 210 System.arraycopy(files, 0, args, 5, files.length); 211 212 IDE.debugVerbose(console, EngineRuntime.class, "Compile: "+Utils.toString(args)); 213 214 Process p = Runtime.getRuntime().exec(args); 215 setProcess(p); 216 new StreamWatcher(p.getErrorStream(), "Compiler[error]", delegate).start(); 217 new StreamWatcher(p.getInputStream(), "Compiler[stdout]", delegate).start(); 218 result = p.waitFor(); 219 } else if(compiler.equalsIgnoreCase(AWPrefs.COMPILER_JIKES)) { 220 String jikesPath = XJUtils.concatPath(AWPrefs.getJikesPath(), "jikes"); 221 222 String [] args = new String [5+files.length]; 223 args[0] = jikesPath; 224 args[1] = "-classpath"; 225 args[2] = classPath; 226 args[3] = "-d"; 227 args[4] = Utils.quotePath(outputFileDir); 228 System.arraycopy(files, 0, args, 5, files.length); 229 230 IDE.debugVerbose(console, EngineRuntime.class, "Compile: "+Utils.toString(args)); 231 232 Process p = Runtime.getRuntime().exec(args); 233 setProcess(p); 234 new StreamWatcher(p.getErrorStream(), "Compiler[error]", delegate).start(); 235 new StreamWatcher(p.getInputStream(), "Compiler[stdout]", delegate).start(); 236 result = p.waitFor(); 237 } else if(compiler.equalsIgnoreCase(AWPrefs.COMPILER_INTEGRATED)) { 238 String [] args = new String [2+files.length]; 239 args[0] = "-d"; 240 args[1] = outputFileDir; 241 System.arraycopy(files, 0, args, 2, files.length); 242 243 Class <?> javac = Class.forName("com.sun.tools.javac.Main"); 244 Class [] p = new Class [] { String [].class }; 245 Method m = javac.getMethod("compile", p); 246 Object [] a = new Object [] { args }; 247 Object r = m.invoke(javac.newInstance(), a); 248 result = (Integer ) r; 249 } 251 252 } catch(Error e) { 253 error = "Compiler error:\n"+e.toString(); 254 e.printStackTrace(); 255 } catch(Exception e) { 256 error = "Compiler exception:\n"+e.toString(); 257 e.printStackTrace(); 258 } finally { 259 removeProcess(); 260 } 261 262 if(result != 0) { 263 error = "Compiler failed with result code "+result; 264 } 265 266 return error; 267 } 268 269 } 270 | Popular Tags |