1 16 17 package org.apache.axis.components.compiler; 18 19 import org.apache.axis.components.logger.LogFactory; 20 import org.apache.axis.utils.Messages; 21 import org.apache.commons.logging.Log; 22 23 import java.io.BufferedInputStream ; 24 import java.io.BufferedReader ; 25 import java.io.ByteArrayInputStream ; 26 import java.io.ByteArrayOutputStream ; 27 import java.io.IOException ; 28 import java.io.OutputStream ; 29 import java.util.ArrayList ; 30 import java.util.List ; 31 import java.util.StringTokenizer ; 32 33 40 41 public class Jikes extends AbstractCompiler 42 { 43 protected static Log log = 44 LogFactory.getLog(Jikes.class.getName()); 45 46 static final int OUTPUT_BUFFER_SIZE = 1024; 47 static final int BUFFER_SIZE = 512; 48 49 private class StreamPumper extends Thread { 50 51 private BufferedInputStream stream; 52 private boolean endOfStream = false; 53 private boolean stopSignal = false; 54 private int SLEEP_TIME = 5; 55 private OutputStream out; 56 57 public StreamPumper(BufferedInputStream is, OutputStream out) { 58 this.stream = is; 59 this.out = out; 60 } 61 62 public void pumpStream() throws IOException { 63 byte[] buf = new byte[BUFFER_SIZE]; 64 if (!endOfStream) { 65 int bytesRead = stream.read(buf, 0, BUFFER_SIZE); 66 67 if (bytesRead > 0) { 68 out.write(buf, 0, bytesRead); 69 } else if (bytesRead == -1) { 70 endOfStream = true; 71 } 72 } 73 } 74 75 public void run() { 76 try { 77 while (!endOfStream) { 78 pumpStream(); 79 sleep(SLEEP_TIME); 80 } 81 } catch (Exception e) { 82 } 84 } 85 } 86 87 93 protected String [] toStringArray(List arguments) { 94 int i; 95 96 for (i = 0; i < arguments.size(); i++) { 97 String arg = (String ) arguments.get(i); 98 if (arg.equals("-sourcepath")) { 99 arguments.remove(i); 101 arguments.remove(i); 102 break; 103 } 104 } 105 106 String [] args = new String [arguments.size() + fileList.size()]; 107 for (i = 0; i < arguments.size(); i++) { 108 args[i] = (String ) arguments.get(i); 109 } 110 111 for (int j=0; j < fileList.size(); i++,j++) { 112 args[i] = (String )fileList.get(j); 113 } 114 115 return args; 116 } 117 118 121 public boolean compile() throws IOException { 122 123 List args = new ArrayList (); 124 args.add("jikes"); 126 args.add("+E"); 128 args.add("-nowarn"); 131 132 int exitValue; 133 ByteArrayOutputStream tmpErr = new ByteArrayOutputStream (OUTPUT_BUFFER_SIZE); 134 135 try { 136 Process p = Runtime.getRuntime().exec(toStringArray(fillArguments(args))); 137 138 BufferedInputStream compilerErr = new BufferedInputStream (p.getErrorStream()); 139 140 StreamPumper errPumper = new StreamPumper(compilerErr, tmpErr); 141 142 errPumper.start(); 143 144 p.waitFor(); 145 exitValue = p.exitValue(); 146 147 errPumper.join(); 149 compilerErr.close(); 150 151 p.destroy(); 152 153 tmpErr.close(); 154 this.errors = new ByteArrayInputStream (tmpErr.toByteArray()); 155 156 } catch (InterruptedException somethingHappened) { 157 log.debug("Jikes.compile():SomethingHappened", somethingHappened); 158 return false; 159 } 160 161 return ((exitValue == 0) && (tmpErr.size() == 0)); 166 } 167 168 176 protected List parseStream(BufferedReader input) throws IOException { 177 List errors = null; 178 String line = null; 179 StringBuffer buffer = null; 180 181 while (true) { 182 buffer = new StringBuffer (); 185 if (line == null) line = input.readLine(); 187 if (line == null) return errors; 188 log.debug(line); 189 buffer.append(line); 190 191 while (true) { 193 line = input.readLine(); 194 if (line == null) 196 break; 197 if (line.length() > 0 && line.charAt(0) != ' ') 199 break; 200 log.debug(line); 201 buffer.append('\n'); 202 buffer.append(line); 203 } 204 205 if (errors == null) errors = new ArrayList (); 207 208 errors.add(parseError(buffer.toString())); 210 } 211 } 212 213 219 private CompilerError parseError(String error) { 220 StringTokenizer tokens = new StringTokenizer (error, ":"); 221 String file = tokens.nextToken(); 222 if (file.length() == 1) file = new StringBuffer (file).append(":").append(tokens.nextToken()).toString(); 223 StringBuffer message = new StringBuffer (); 224 String type = ""; 225 int startline = 0; 226 int startcolumn = 0; 227 int endline = 0; 228 int endcolumn = 0; 229 230 try { 231 startline = Integer.parseInt(tokens.nextToken()); 232 startcolumn = Integer.parseInt(tokens.nextToken()); 233 endline = Integer.parseInt(tokens.nextToken()); 234 endcolumn = Integer.parseInt(tokens.nextToken()); 235 } catch (Exception e) { 236 message.append(Messages.getMessage("compilerFail00")); 238 type="error"; 239 log.error(Messages.getMessage("compilerFail00"), e); 240 } 241 242 if ("".equals(message)) { 243 type = tokens.nextToken().trim().toLowerCase(); 244 message.append(tokens.nextToken("\n").substring(1).trim()); 245 246 while (tokens.hasMoreTokens()) 247 message.append("\n").append(tokens.nextToken()); 248 } 249 250 return new CompilerError(file, type.equals("error"), startline, startcolumn, endline, endcolumn, message.toString()); 251 } 252 253 public String toString() { 254 return Messages.getMessage("ibmJikes"); 255 } 256 } 257 | Popular Tags |