1 29 30 package com.caucho.java; 31 32 import com.caucho.util.CharBuffer; 33 34 import java.io.CharArrayWriter ; 35 import java.io.IOException ; 36 import java.io.OutputStream ; 37 import java.io.PrintWriter ; 38 import java.io.Serializable ; 39 import java.util.ArrayList ; 40 import java.util.Iterator ; 41 42 48 public class LineMap implements Serializable { 49 private String _dstFilename; 50 private String _srcFilename; 51 52 private String _srcType = "JSP"; 53 54 private String _lastSrcFilename; 55 56 private ArrayList <Line> _lines = new ArrayList <Line>(); 57 58 61 public LineMap() 62 { 63 } 64 65 public LineMap(String dstFilename, String srcFilename) 66 { 67 int tail = dstFilename.lastIndexOf('/'); 68 if (tail < 0) 69 _dstFilename = dstFilename; 70 else 71 dstFilename = dstFilename.substring(tail + 1); 72 73 _srcFilename = srcFilename; 74 _lastSrcFilename = _srcFilename; 75 } 76 77 public LineMap(String dstFilename) 78 { 79 int tail = dstFilename.lastIndexOf('/'); 80 if (tail < 0) 81 _dstFilename = dstFilename; 82 else 83 _dstFilename = dstFilename.substring(tail + 1); 84 } 85 86 public void setSourceType(String type) 87 { 88 _srcType = type; 89 } 90 91 public String getSourceType() 92 { 93 return _srcType; 94 } 95 96 public String getDestFilename() 97 { 98 return _dstFilename; 99 } 100 101 public String getLastSourceFilename() 102 { 103 return _lastSrcFilename; 104 } 105 106 117 public boolean add(String srcFilename, int srcLine, int dstLine) 118 { 119 _lastSrcFilename = srcFilename; 120 121 if (_lines.size() > 0) { 122 Line line = _lines.get(_lines.size() - 1); 123 124 if (line.add(srcFilename, srcLine, dstLine)) { 125 if (_lines.size() > 1) { 126 Line prevLine = _lines.get(_lines.size() - 2); 127 128 if (prevLine.merge(line)) { 129 _lines.remove(_lines.size() - 1); 130 } 131 } 132 133 return true; 134 } 135 136 if (line.getLastDestinationLine() + 1 < dstLine) { 137 _lines.add(new Line(line.getLastSourceLine(), 138 line.getSourceFilename(), 139 1, 140 line.getLastDestinationLine() + 1, 141 dstLine - line.getLastDestinationLine())); 142 } 143 } 144 145 _lines.add(new Line(srcFilename, srcLine, dstLine)); 146 147 return true; 148 } 149 150 153 public void addLine(int startLine, String sourceFile, int repeatCount, 154 int outputLine, int outputIncrement) 155 { 156 _lines.add(new Line(startLine, sourceFile, repeatCount, 157 outputLine, outputIncrement)); 158 } 159 160 public void add(int srcLine, int dstLine) 161 { 162 add(_lastSrcFilename, srcLine, dstLine); 163 } 164 165 public Iterator <Line> iterator() 166 { 167 return _lines.iterator(); 168 } 169 170 public int size() 171 { 172 return _lines.size(); 173 } 174 175 public Line get(int i) 176 { 177 return _lines.get(i); 178 } 179 180 public Line getLine(int line) 181 { 182 for (int i = 0; i < _lines.size(); i++) { 183 Line map = _lines.get(i); 184 185 if (map._dstLine <= line && line <= map.getLastDestinationLine()) { 186 return map; 187 } 188 } 189 190 return null; 191 } 192 193 197 public String convertError(String filename, int line, 198 int column, String message) 199 { 200 String srcFilename = null; 201 int destLine = 0; 202 int srcLine = 0; 203 204 for (int i = 0; i < _lines.size(); i++) { 205 Line map = _lines.get(i); 206 207 if (filename != null && ! filename.endsWith(_dstFilename)) { 208 } 209 else if (map._dstLine <= line && line <= map.getLastDestinationLine()) { 210 srcFilename = map._srcFilename; 211 srcLine = map.getSourceLine(line); 212 } 213 } 214 215 if (srcFilename != null) 216 return srcFilename + ":" + srcLine + ": " + message; 217 else 218 return filename + ":" + line + ": " + message; 219 } 220 221 public String convertLine(String filename, int line) 222 { 223 Line bestLine = getLine(line); 224 225 if (bestLine != null) 226 return bestLine.getSourceFilename() + ":" + bestLine.getSourceLine(line); 227 else 228 return filename + ":" + line; 229 } 230 231 234 public void printStackTrace(Throwable e, OutputStream os) 235 { 236 CharArrayWriter writer = new CharArrayWriter (); 237 PrintWriter pw = new PrintWriter (writer); 238 239 e.printStackTrace(pw); 240 241 pw.close(); 242 char []array = writer.toCharArray(); 243 244 CharBuffer cb = filter(array); 245 246 if (os != null) { 247 byte []b = cb.toString().getBytes(); 248 249 try { 250 os.write(b, 0, b.length); 251 } catch (IOException e1) { 252 } 253 } else 254 System.out.println(cb); 255 } 256 257 260 public void printStackTrace(Throwable e, PrintWriter os) 261 { 262 CharArrayWriter writer = new CharArrayWriter (); 263 PrintWriter pw = new PrintWriter (writer); 264 265 e.printStackTrace(pw); 266 267 pw.close(); 268 char []array = writer.toCharArray(); 269 270 CharBuffer cb = filter(array); 271 272 if (os != null) 273 os.print(cb.toString()); 274 else 275 System.out.println(cb); 276 } 277 278 282 private CharBuffer filter(char []array) 283 { 284 CharBuffer buf = new CharBuffer(); 285 CharBuffer fun = new CharBuffer(); 286 CharBuffer file = new CharBuffer(); 287 288 int i = 0; 289 while (i < array.length) { 290 fun.clear(); 291 file.clear(); 292 int start = i; 293 int end; 294 for (end = i; end < array.length && array[end] != '\n'; end++) { 295 } 296 297 for (; i < end && Character.isWhitespace(array[i]); i++) { 298 fun.append(array[i]); 299 } 300 301 for (; i < end && ! Character.isWhitespace(array[i]); i++) { 303 fun.append(array[i]); 304 } 305 306 if (! fun.endsWith("at")) { 307 for (i = start; i < end; i++) { 308 buf.append(array[i]); 309 } 310 i = end + 1; 311 312 buf.append('\n'); 313 314 continue; 315 } 316 317 for (; i < end && Character.isWhitespace(array[i]); i++) { 318 } 319 320 fun.clear(); 321 for (; i < end && ! Character.isWhitespace(array[i]) && 322 array[i] != '('; i++) { 323 fun.append(array[i]); 324 } 325 326 if (i < end && array[i] == '(') 327 i++; 328 329 for (; i < end && ! Character.isWhitespace(array[i]) && 330 array[i] != ':' && array[i] != ')'; i++) { 331 file.append(array[i]); 332 } 333 334 int line = -1; 335 if (i < end && array[i] == ':') { 336 line = 0; 337 for (i++; i < end && array[i] >= '0' && array[i] <= '9'; i++) { 338 line = 10 * line + array[i] - '0'; 339 } 340 } 341 342 for (; i < end && ! Character.isWhitespace(array[i]) && 343 array[i] != ':' && array[i] != ')'; i++) { 344 file.append(array[i]); 345 } 346 347 buf.append("\tat "); 348 buf.append(fun); 349 buf.append("("); 350 String dstFile = file.toString(); 351 352 if (dstFile.equals(_dstFilename)) { 353 convertError(buf, line); 354 } 355 else { 356 buf.append(file); 357 if (line > 0) { 358 buf.append(":"); 359 buf.append(line); 360 } 361 } 362 buf.append(array, i, end - i); 363 buf.append('\n'); 364 i = end + 1; 365 } 366 367 return buf; 368 } 369 370 376 private void convertError(CharBuffer buf, int line) 377 { 378 String srcFilename = null; 379 int destLine = 0; 380 int srcLine = 0; 381 int srcTailLine = Integer.MAX_VALUE; 382 383 for (int i = 0; i < _lines.size(); i++) { 384 Line map = (Line) _lines.get(i); 385 386 if (map._dstLine <= line && line <= map.getLastDestinationLine()) { 387 srcFilename = map._srcFilename; 388 destLine = map._dstLine; 389 srcLine = map.getSourceLine(line); 390 break; 391 } 392 } 393 394 if (srcFilename != null) { 395 } 396 else if (_lines.size() > 0) 397 srcFilename = ((Line) _lines.get(0))._srcFilename; 398 else 399 srcFilename = ""; 400 401 buf.append(srcFilename); 402 if (line >= 0) { 403 buf.append(":"); 404 buf.append(srcLine + (line - destLine)); 405 } 406 } 407 408 public static class Line implements Serializable { 409 String _srcFilename; 410 int _srcLine; 411 412 int _dstLine; 413 int _dstIncrement = 1; 414 415 int _repeat = 1; 416 417 420 public Line() 421 { 422 } 423 424 Line(String srcFilename, int srcLine, int dstLine) 425 { 426 _srcFilename = srcFilename; 427 _srcLine = srcLine; 428 _dstLine = dstLine; 429 } 430 431 Line(int srcLine, String srcFilename, int repeat, 432 int dstLine, int dstIncrement) 433 { 434 _srcFilename = srcFilename; 435 _srcLine = srcLine; 436 _dstLine = dstLine; 437 _repeat = repeat; 438 _dstIncrement = dstIncrement; 439 } 440 441 444 boolean add(String srcFilename, int srcLine, int dstLine) 445 { 446 if (_srcFilename != null && 447 (! _srcFilename.equals(srcFilename) || srcFilename == null)) 448 return false; 449 450 if (dstLine <= _dstLine) { 452 return true; 453 } 454 455 if (srcLine == _srcLine) { 456 _dstIncrement = dstLine - _dstLine + 1; 457 458 return true; 459 } 460 else if (dstLine - _dstLine == (srcLine - _srcLine) * _dstIncrement) { 461 _repeat = srcLine - _srcLine + 1; 462 463 return true; 464 } 465 else if (srcLine == _srcLine + 1 && _repeat == 1) { 466 _dstIncrement = dstLine - _dstLine; 467 468 return false; 469 } 470 479 480 return false; 481 } 482 483 486 boolean merge(Line next) 487 { 488 if (_srcFilename != null && ! _srcFilename.equals(next._srcFilename)) 489 return false; 490 491 else if (_dstIncrement != next._dstIncrement) 492 return false; 493 else if (getLastDestinationLine() + 1 != next._dstLine) 494 return false; 495 else if (getLastSourceLine() + 1 != next._srcLine) 496 return false; 497 else { 498 _repeat += next._repeat; 499 500 return true; 501 } 502 } 503 504 public String getSourceFilename() 505 { 506 return _srcFilename; 507 } 508 509 public int getSourceLine() 510 { 511 return _srcLine; 512 } 513 514 517 public int getSourceLine(int dstLine) 518 { 519 return _srcLine + (dstLine - _dstLine) / _dstIncrement; 520 } 521 522 public int getRepeatCount() 523 { 524 return _repeat; 525 } 526 527 public int getDestinationLine() 528 { 529 return _dstLine; 530 } 531 532 public int getLastSourceLine() 533 { 534 return _srcLine + _repeat - 1; 535 } 536 537 public int getLastDestinationLine() 538 { 539 return _dstLine + _dstIncrement * _repeat - 1; 540 } 541 542 public int getDestinationIncrement() 543 { 544 return _dstIncrement; 545 } 546 547 public String toString() 548 { 549 return "Line[src:" + _srcFilename + ":" + _srcLine + ",dst:" + _dstLine + "]"; 550 } 551 } 552 } 553 | Popular Tags |