1 16 package org.apache.cocoon.components.language.programming.java; 17 18 import java.io.BufferedReader ; 19 import java.io.ByteArrayInputStream ; 20 import java.io.ByteArrayOutputStream ; 21 import java.io.IOException ; 22 import java.io.PrintWriter ; 23 import java.util.ArrayList ; 24 import java.util.List ; 25 import java.util.NoSuchElementException ; 26 import java.util.StringTokenizer ; 27 28 import org.apache.cocoon.components.language.programming.CompilerError; 29 import org.apache.commons.lang.SystemUtils; 30 31 38 39 public class Javac extends AbstractJavaCompiler { 40 41 public Javac() { 42 } 44 45 51 public boolean compile() throws IOException { 52 53 ByteArrayOutputStream err = new ByteArrayOutputStream (); 54 55 boolean result; 56 if (!SystemUtils.IS_JAVA_1_3) { PrintWriter pw = new PrintWriter (err); 58 result = com.sun.tools.javac.Main.compile(toStringArray(fillArguments(new ArrayList ())), pw) == 0; 59 } else { 60 sun.tools.javac.Main compiler = new sun.tools.javac.Main(err, "javac"); 61 result = compiler.compile(toStringArray(fillArguments(new ArrayList ()))); 62 } 63 this.errors = new ByteArrayInputStream (err.toByteArray()); 64 return result; 65 } 66 67 75 protected List parseStream(BufferedReader input) throws IOException { 76 List errors = new ArrayList (); 77 String line = null; 78 StringBuffer buffer = null; 79 80 while (true) { 81 buffer = new StringBuffer (); 84 do { 86 if ((line = input.readLine()) == null) { 87 if (buffer.length() > 0) { 88 errors.add(new CompilerError("\n" + buffer.toString())); 90 } 91 return errors; 92 } 93 buffer.append(line); 94 buffer.append('\n'); 95 } while (!line.endsWith("^")); 96 97 errors.add(parseModernError(buffer.toString())); 99 } 100 } 101 102 108 private CompilerError parseModernError(String error) { 109 StringTokenizer tokens = new StringTokenizer (error, ":"); 110 try { 111 String file = tokens.nextToken(); 112 if (file.length() == 1) file = new StringBuffer (file).append(":").append(tokens.nextToken()).toString(); 113 int line = Integer.parseInt(tokens.nextToken()); 114 115 String message = tokens.nextToken("\n").substring(1); 116 String context = tokens.nextToken("\n"); 117 String pointer = tokens.nextToken("\n"); 118 int startcolumn = pointer.indexOf("^"); 119 int endcolumn = context.indexOf(" ", startcolumn); 120 if (endcolumn == -1) { 121 endcolumn = context.length(); 122 } 123 return new CompilerError(file, false, line, startcolumn, line, endcolumn, message); 124 } catch(NoSuchElementException nse) { 125 return new CompilerError("no more tokens - could not parse error message: " + error); 126 } catch(Exception nse) { 127 return new CompilerError("could not parse error message: " + error); 128 } 129 } 130 131 public String toString() { 132 return "Sun Javac Compiler"; 133 } 134 } 135 | Popular Tags |