1 33 34 package edu.rice.cs.drjava.model.compiler; 35 36 import java.io.File ; 37 import java.io.IOException ; 38 import javax.swing.text.*; 39 import java.util.Arrays ; 40 import java.util.List ; 41 import java.util.LinkedList ; 42 import java.util.HashMap ; 43 44 import edu.rice.cs.util.UnexpectedException; 45 import edu.rice.cs.drjava.model.DummyGlobalModel; 46 import edu.rice.cs.drjava.model.FileGroupingState; 47 import edu.rice.cs.drjava.model.GlobalModel; 48 import edu.rice.cs.util.OperationCanceledException; 49 import edu.rice.cs.drjava.model.OpenDefinitionsDocument; 50 import edu.rice.cs.drjava.model.FileMovedException; 51 52 57 public class CompilerErrorModel { 58 private static final String newLine = System.getProperty("line.separator"); 59 68 private final CompilerError[] _errors; 69 70 73 private final Position[] _positions; 74 75 76 private final int _numErrors; 77 78 79 private int _numCompilerErrors; 80 81 82 private int _numWarnings; 83 84 85 91 private int _onlyWarnings = -1; 92 93 96 private final HashMap <File , StartAndEndIndex> _filesToIndexes = new HashMap <File , StartAndEndIndex>(); 97 98 99 private final GlobalModel _model; 100 101 102 public CompilerErrorModel() { 103 _model = new DummyGlobalModel() { 104 public OpenDefinitionsDocument getDocumentForFile(File file) { 105 throw new IllegalStateException ("No documents to get!"); 106 } 107 public boolean isAlreadyOpen(File file) { return false; } 108 public List <OpenDefinitionsDocument> getOpenDefinitionsDocuments() { 109 return new LinkedList <OpenDefinitionsDocument>(); 110 } 111 public boolean hasModifiedDocuments() { return false; } 112 public boolean hasUntitledDocuments() { return false; } 113 }; 114 _errors = new CompilerError[0]; 115 _numErrors = 0; 116 _numWarnings = 0; 117 _numCompilerErrors = 0; 118 _positions = new Position[0]; 119 } 120 121 126 public CompilerErrorModel(CompilerError[] errors, GlobalModel model) { 127 _model = model; 128 129 _errors = errors; 131 132 _numErrors = errors.length; 134 _positions = new Position[_numErrors]; 135 136 _numWarnings =0; 137 _numCompilerErrors = 0; 138 for (int i =0; i<errors.length; i++){ 139 if (errors[i].isWarning()) _numWarnings++; 140 else _numCompilerErrors++; 141 } 142 143 Arrays.sort(_errors); 145 146 _calculatePositions(); 148 } 149 150 157 public CompilerError getError(int idx) { return _errors[idx]; } 158 159 160 public Position getPosition(CompilerError error) { 161 int spot = Arrays.binarySearch(_errors, error); 162 return _positions[spot]; 163 } 164 165 166 public int getNumErrors() { return _numErrors; } 167 168 169 public int getNumCompErrors() { return _numCompilerErrors; } 170 171 172 public int getNumWarnings() { return _numWarnings; } 173 174 175 public String toString() { 176 final StringBuilder buf = new StringBuilder (); 177 buf.append(this.getClass().toString() + ":\n "); 178 for (int i=0; i < _numErrors; i++) { 179 buf.append(_errors[i].toString()); 180 buf.append("\n "); 181 } 182 return buf.toString(); 183 } 184 185 190 public CompilerError getErrorAtOffset(OpenDefinitionsDocument odd, int offset) { 191 File file; 192 try { 193 file = odd.getFile(); 194 if (file == null) return null; 195 } 196 catch (FileMovedException e) { file = e.getFile(); } 197 198 try { file = file.getCanonicalFile(); } 200 catch (IOException ioe) { 201 } 203 204 StartAndEndIndex saei = _filesToIndexes.get(file); 205 if (saei == null) return null; 206 int start = saei.getStartPos(); 207 int end = saei.getEndPos(); 208 if (start == end) return null; 209 210 int errorAfter; for (errorAfter = start; errorAfter < end; errorAfter++) { 215 if (_positions[errorAfter] == null) { 216 return null; 218 } 219 if (_positions[errorAfter].getOffset() >=offset) break; 220 } 221 222 int errorBefore = errorAfter - 1; 224 225 int shouldSelect = -1; 227 228 if (errorBefore >= start) { int errPos = _positions[errorBefore].getOffset(); 230 try { 231 String betweenDotAndErr = odd.getText(errPos, offset - errPos); 232 if (betweenDotAndErr.indexOf('\n') == -1) shouldSelect = errorBefore; 233 } 234 catch (BadLocationException e) { } 235 } 236 237 if ((shouldSelect == -1) && (errorAfter < end)) { int errPos = _positions[errorAfter].getOffset(); 242 try { 243 String betweenDotAndErr = odd.getText(offset, errPos - offset); 244 if (betweenDotAndErr.indexOf('\n') == -1) shouldSelect = errorAfter; 245 } 246 catch (BadLocationException e) { } 247 } 248 249 if (shouldSelect == -1) return null; 250 return _errors[shouldSelect]; 251 } 252 253 254 public boolean hasErrorsWithPositions(OpenDefinitionsDocument odd) { 255 File file = null; 256 try { 257 file = odd.getFile(); 258 if (file == null) return false; 259 } 260 catch (FileMovedException fme) { file = fme.getFile(); } 261 262 try { file = file.getCanonicalFile(); } 264 catch (IOException ioe) { } 265 266 StartAndEndIndex saei = _filesToIndexes.get(file); 267 if (saei == null) return false; 268 if (saei.getStartPos() == saei.getEndPos()) return false; 269 return true; 270 } 271 272 276 public boolean hasOnlyWarnings() { 277 if (_onlyWarnings == 0) return false; 279 if (_onlyWarnings == 1) return true; 280 else { 281 boolean clean = true; 283 for (int i = 0; clean && (i < _numErrors); i++) { 284 clean = _errors[i].isWarning(); 285 } 286 _onlyWarnings = clean? 1: 0; 288 return clean; 289 } 290 } 291 292 293 private void _calculatePositions() { 294 try { 295 int curError = 0; 296 297 while ((curError < _numErrors)) { 299 curError = nextErrorWithLine(curError); 301 if (curError >= _numErrors) {break;} 302 303 File file = _errors[curError].file(); 305 OpenDefinitionsDocument document; 306 try { document = _model.getDocumentForFile(file); } 307 catch (Exception e) { 308 if ((e instanceof IOException ) || (e instanceof OperationCanceledException)) { 310 do { curError++;} 312 while ((curError < _numErrors) && (_errors[curError].file().equals(file))); 313 314 continue; 316 } 317 else throw new UnexpectedException(e); 318 } 319 if (curError >= _numErrors) break; 320 321 final int fileStartIndex = curError; 323 final int defsLength = document.getLength(); 324 final String defsText = document.getText(0, defsLength); 325 int curLine = 0; 326 int offset = 0; 328 while ((curError < _numErrors) && file.equals(_errors[curError].file()) && (offset <= defsLength)) { 334 while ((curError < _numErrors) && file.equals(_errors[curError].file()) && (_errors[curError].lineNumber() == curLine)) { 337 _positions[curError] = document.createPosition(offset + _errors[curError].startColumn()); 338 curError++; 339 } 340 341 if (curError < _numErrors) { 346 int curErrorLine = _errors[curError].lineNumber(); 347 int nextNewline = 0; 348 while ((curLine != curErrorLine) 349 && (nextNewline != -1) 350 && (file.equals(_errors[curError].file()))) { 351 nextNewline = defsText.indexOf(newLine, offset); 352 if (nextNewline == -1) nextNewline = defsText.indexOf("\n", offset); 353 if (nextNewline != -1) { 354 curLine++; 355 offset = nextNewline + 1; 356 } 357 } 358 } 359 } 360 361 int fileEndIndex = curError; 364 if (fileEndIndex != fileStartIndex) { 365 try { file = file.getCanonicalFile(); } 367 catch (IOException ioe) { } 368 _filesToIndexes.put(file, new StartAndEndIndex(fileStartIndex, fileEndIndex)); 369 } 370 } 371 } 372 catch (BadLocationException ble) { throw new UnexpectedException(ble); } 373 } 374 375 379 private int nextErrorWithLine(int idx) { 380 while (idx < _numErrors && (_errors[idx].hasNoLocation() || _errors[idx].file() == null)) idx++; 381 return idx; 382 } 383 384 387 private static class StartAndEndIndex { 388 private int startPos; 389 private int endPos; 390 391 public StartAndEndIndex(int startPos, int endPos) { 392 this.startPos = startPos; 393 this.endPos = endPos; 394 } 395 public int getStartPos() { return startPos; } 396 public int getEndPos() { return endPos; } 397 } 398 } 399 | Popular Tags |