1 19 package org.netbeans.modules.ruby.rubyproject.execution; 20 21 import java.io.BufferedReader ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.InputStreamReader ; 25 import java.lang.InterruptedException ; 26 import java.util.List ; 27 import org.netbeans.modules.ruby.rubyproject.execution.OutputRecognizer.FileLocation; 28 import org.netbeans.modules.ruby.rubyproject.execution.OutputRecognizer.RecognizedOutput; 29 import org.openide.ErrorManager; 30 import org.openide.windows.OutputWriter; 31 32 33 49 class OutputForwarder implements Runnable { 50 private StopAction stopAction; 51 private InputStream str; 52 private OutputWriter writer; 53 private FileLocator fileLocator; 54 private List <OutputRecognizer> recognizers; 55 private String role; 56 57 public OutputForwarder(InputStream instream, OutputWriter out, FileLocator fileLocator, 58 List <OutputRecognizer> recognizers, StopAction stopAction, String role) { 59 str = instream; 60 writer = out; 61 this.fileLocator = fileLocator; 62 this.recognizers = recognizers; 63 this.stopAction = stopAction; 64 this.role = role; 65 } 66 67 public void processLine(String line) throws IOException { 68 if (fileLocator == null) { 69 writer.println(line); 70 } else { 71 FileLocation location = null; 72 73 for (OutputRecognizer recognizer : recognizers) { 74 RecognizedOutput loc = recognizer.processLine(line); 75 76 if (loc instanceof FileLocation) { 77 location = (FileLocation)loc; 78 79 } } 86 87 if (location != null) { 88 writer.println(line, new OutputProcessor(location.file, location.line, fileLocator)); 89 } else { 90 writer.println(line); 91 } 92 } 93 } 94 95 public void run() { 96 BufferedReader read = new BufferedReader (new InputStreamReader (str), 1200); 97 98 stopAction.addReader(read); 99 100 StringBuilder sb = new StringBuilder (); 101 102 try { 103 while (true) { 104 if (!read.ready()) { 105 try { 106 Thread.sleep(50); 107 } catch (InterruptedException ie) { 108 return; 109 } 110 111 if (stopAction.process == null) { 112 return; 114 } 115 116 if (!read.ready() && sb.length() > 0) { 117 String line = sb.toString(); 121 sb.setLength(0); 122 123 writer.print(line); 124 } 125 126 continue; 127 } 128 129 while (read.ready()) { 130 int c = -1; 131 c = read.read(); 132 133 if (c == -1) { 134 String line = sb.toString(); 135 sb.setLength(0); 136 137 processLine(line); 138 139 return; 140 } else if (c == '\n') { 141 String line = sb.toString(); 142 sb.setLength(0); 143 144 processLine(line); 145 146 try { 147 } catch (Exception e) { 149 return; 151 } 152 } else { 153 sb.append((char)c); 154 } 155 } 156 157 if (Thread.interrupted()) { 158 return; 159 } 160 } 161 } catch (IOException ioexc) { 162 } catch (Throwable t) { 164 System.err.println("Caught throwable " + t); 165 } finally { 166 try { 167 if (str.available() > 0) { 169 String line = read.readLine(); 170 171 if (line != null) { 172 processLine(line); 173 } 174 } 175 176 read.close(); 177 writer.close(); 178 } catch (IOException ioexc) { 179 ErrorManager.getDefault().notify(ioexc); 180 } 181 } 182 } 183 } 184 | Popular Tags |