1 19 20 package org.netbeans.modules.java; 21 22 import javax.swing.text.Document ; 23 import javax.swing.text.BadLocationException ; 24 import javax.swing.text.StyledDocument ; 25 import org.openide.filesystems.FileObject; 26 import org.openide.filesystems.FileUtil; 27 import org.openide.loaders.DataObject; 28 import org.openide.loaders.ExtensionList; 29 import org.openide.text.IndentEngine; 30 import org.netbeans.modules.java.JavaEditor.GuardedReader; 31 import java.util.ResourceBundle ; 32 import java.util.Enumeration ; 33 import java.util.Hashtable ; 34 import java.util.List ; 35 import java.util.Iterator ; 36 import java.util.LinkedList ; 37 import org.openide.cookies.SaveCookie; 38 import org.openide.ErrorManager; 39 40 import org.openide.text.NbDocument; 41 import org.openide.text.PositionRef; 42 import org.netbeans.modules.java.settings.JavaSettings; 43 import java.io.*; 44 import org.openide.util.SharedClassObject; 45 import java.lang.ref.WeakReference ; 46 import java.lang.ref.ReferenceQueue ; 47 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 48 49 import org.openide.src.SourceException; 50 import org.openide.util.NbBundle; 51 52 56 public final class Util extends Object { 57 59 62 private static ErrorManager errorManager; 63 64 static final String ATTR_FILE_ENCODING = "Content-Encoding"; 66 70 static String getString(String key) { 71 return NbBundle.getMessage(Util.class, key); 72 } 73 74 static ErrorManager getErrorManager() { 75 if (errorManager != null) 76 return errorManager; 77 ErrorManager main = ErrorManager.getDefault(); 78 if (main == null) { 79 System.err.println("WARNING: can't lookup error manager"); return null; 81 } 82 return errorManager = main; 83 } 84 85 static void annotateThrowable(Throwable t, Throwable nested) { 86 getErrorManager().annotate(t, nested); 87 } 88 89 static void annotateThrowable(Throwable t, String localizedMessage, 90 boolean user) { 91 if (user) { 92 getErrorManager().annotate(t, ErrorManager.USER, null, 93 localizedMessage, null, null); 94 } else { 95 getErrorManager().annotate(t, ErrorManager.EXCEPTION, null, 96 localizedMessage, null, null); 97 } 98 } 99 100 102 static FileObject findBrother(FileObject f, String extension) { 103 return FileUtil.findBrother(f, extension); 104 } 105 106 private static FileObject findSibling(FileObject base, String name, Enumeration extlist) { 107 FileObject ret; 108 while (extlist.hasMoreElements()) { 109 String ext = (String )extlist.nextElement(); 110 if (ext == null) 111 continue; 112 ret = base.getFileObject(name, ext); 113 if (ret != null) 114 return ret; 115 } 116 return null; 117 } 118 119 123 private static void notifyException(Throwable t, String msg) { 124 getErrorManager().notify(t); 125 } 126 127 128 130 135 static Writer findIndentWriter(Document doc, int offset, Writer writer) { 136 IndentEngine engine = IndentEngine.find(doc); return engine.createWriter(doc, offset, writer); 138 } 139 140 142 147 static void runAtomic(StyledDocument doc, ExceptionRunnable run) throws SourceException { 148 RunnableSupport support = new RunnableSupport(run); 149 NbDocument.runAtomic(doc, support); 150 support.throwException(); 151 } 152 153 157 interface ExceptionRunnable { 158 public void run() throws Exception ; 159 } 160 161 165 private static class RunnableSupport implements Runnable { 166 167 private Exception e; 168 169 170 private ExceptionRunnable runnable; 171 172 173 public RunnableSupport(ExceptionRunnable runnable) { 174 this.runnable = runnable; 175 e = null; 176 } 177 178 179 public boolean hasException() { 180 return e != null; 181 } 182 183 184 public Exception getException() { 185 return e; 186 } 187 188 192 public void throwException() throws SourceException { 193 if (e != null) { 194 if (e instanceof SourceException) { 195 throw (SourceException)e; 197 } 198 199 SourceException wrapper = new SourceException(e.getMessage()); 200 if (e instanceof BadLocationException ) { 202 ErrorManager.getDefault().annotate( 204 wrapper, ErrorManager.USER, 205 e.getMessage(), 206 e.getLocalizedMessage(), 207 e, null 208 ); 209 } else { 210 ErrorManager.getDefault().annotate( 211 wrapper, e 212 ); 213 } 214 throw wrapper; 215 } 216 } 217 218 221 public void run() { 222 try { 223 runnable.run(); 224 } 225 catch (Exception e) { 226 this.e = e; 227 } 228 } 229 } 230 231 240 public static String getFileEncoding0(FileObject someFile) { 241 return (String )someFile.getAttribute(ATTR_FILE_ENCODING); 242 } 243 244 253 public static String getFileEncoding(FileObject someFile) { 254 String enc = getFileEncoding0(someFile); 255 if (enc == null) { 256 enc = JavaSettings.getDefault().getDefaultEncoding(); 257 } 258 if ("".equals(enc)) 259 return null; 260 else 261 return enc; 262 } 263 264 274 public static void setFileEncoding(FileObject someFile, String enc) throws IOException { 275 someFile.setAttribute(ATTR_FILE_ENCODING, enc); 276 } 277 278 static void throwException(String desc, String bundleKey) throws SourceException { 279 throw (SourceException)getErrorManager().annotate( 280 new SourceException(desc), 281 getString(bundleKey) 282 ); 283 } 284 285 static SourceException wrapException(Exception ex) { 286 SourceException x = new SourceException("Exception wrapper"); annotateThrowable(x, ex); 288 return x; 289 } 290 291 public static char[] readContents(Reader r) throws IOException { 292 int read = 0; 293 int total = 0; 294 int offset; 295 char[] buffer; 296 List buflist = new LinkedList (); 297 298 do { 299 buffer = new char[2048]; 300 offset = 0; 301 while (offset < buffer.length) { 302 read = r.read(buffer, offset, buffer.length - offset); 303 if (read == -1) break; 304 offset += read; 305 } 306 if (offset > 0) buflist.add(buffer); 307 total += offset; 308 } while (read >= 0); 309 r.close(); 310 311 buffer = new char[total]; 312 Iterator it = buflist.iterator(); 313 int offset2 = 0; 314 while (it.hasNext()) { 315 char[] buf = (char[])it.next(); 316 int size = (it.hasNext()) ? buf.length : total - offset2; 317 System.arraycopy(buf, 0, buffer, offset2, size); 318 offset2 += size; 319 } 320 return buffer; 321 } 322 323 private static String getDocumentText(FileObject fo, boolean save) throws IOException { 324 DataObject obj = DataObject.find(fo); 325 JavaEditor editor = null; 326 327 if (obj instanceof JavaDataObject) 328 editor = ((JavaDataObject) obj).getJavaEditor(); 329 330 final Document doc; 331 if ((editor != null) && (doc = editor.getDocument()) != null) { 332 final String [] str = new String [1]; 334 Runnable run = new Runnable () { 336 public void run() { 337 try { 338 str[0] = doc.getText(0, doc.getLength()); 339 } 340 catch (BadLocationException e) { 341 } 343 } 344 }; 345 if (save) { 346 SaveCookie cookie = (SaveCookie) obj.getCookie(SaveCookie.class); 347 if (cookie != null) { 348 cookie.save(); 349 } 350 } 351 JavaMetamodel.getDefaultRepository().beginTrans(false); 352 try { 353 doc.render(run); 354 } finally { 355 JavaMetamodel.getDefaultRepository().endTrans(); 356 } 357 return str[0]; 358 } else { 359 return null; 360 } 361 } 362 363 370 public static char[] getContent(FileObject fo, boolean save, boolean filter, String encoding) throws IOException { 371 String text = getDocumentText(fo, save); 372 if (text == null) { 373 InputStream is = new BufferedInputStream(fo.getInputStream()); 375 Reader reader; 376 if (filter) { 377 reader = new GuardedReader(is, true, encoding); 378 } else { 379 if (encoding == null) { 380 reader = new InputStreamReader(is); 381 } else { 382 reader = new InputStreamReader(is, encoding); 383 } 384 } 385 return readContents(reader); 386 } else { 387 return text.toCharArray(); 388 } 389 } 390 391 public static InputStream createInputStream(FileObject fo, boolean save, boolean store) throws IOException { 392 return createInputStream(fo, save,store, null); 393 } 394 395 405 public static InputStream createInputStream(FileObject fo, boolean save, boolean store, String encoding) throws IOException { 406 String text = getDocumentText(fo, save); 407 408 if (text == null) { 409 InputStream is = new BufferedInputStream(fo.getInputStream()); 411 if (store) { 412 ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); 414 OutputStreamWriter outWriter = new OutputStreamWriter(byteStream); 415 GuardedReader reader = new GuardedReader(is, true); 416 try { 417 int c; 418 while ((c = reader.read()) != -1) { 419 outWriter.write(c); 420 } 421 } 422 finally { 423 outWriter.close(); 424 is.close(); 425 } 426 is = new ByteArrayInputStream(byteStream.toByteArray()); 427 } 428 return new ParserInputStream(is); 429 } else { 430 return new ParserInputStream(text, encoding); 431 } 432 } 433 434 438 static class ParserInputStream extends InputStream { 439 440 441 private InputStream stream; 442 443 444 private String text; 445 446 447 private StringBuffer buffer; 448 449 452 private boolean mode; 453 454 455 private int counter; 456 457 459 private int[] lines = new int[200]; 460 461 463 int lineCounter = 2; 464 465 467 int currentLineLength = 0; 468 469 471 ParserInputStream(String text) { 472 this(text, null); 473 } 474 475 ParserInputStream(String text, String encoding) { 476 this.text = text; 477 counter = 0; 478 mode = false; 479 ByteArrayOutputStream outstm = new ByteArrayOutputStream(text.length()); 480 Writer wr = null; 481 482 if (encoding != null) { 483 try { 484 wr = new OutputStreamWriter(outstm, encoding); 485 } catch (UnsupportedEncodingException ex) { 486 } 487 } 488 if (wr == null) { 489 wr = new OutputStreamWriter(outstm); 490 } 491 492 try { 493 wr.write(text); 494 wr.close(); 495 } catch (IOException ex) { 496 } 497 this.stream = new ByteArrayInputStream(outstm.toByteArray()); 498 } 499 500 501 ParserInputStream(InputStream stream) { 502 this.stream = stream; 503 buffer = new StringBuffer (); 504 mode = true; 505 } 506 507 511 public String getString(int begin, int end) { 512 return mode ? buffer.substring(begin, end) : text.substring(begin, end); 513 } 514 515 519 public String getString(int begin) { 520 if (mode) { 521 return buffer.substring(begin); 522 } 523 else { 524 int end = Math.min(counter - 1, text.length()); 525 return text.substring(begin, end); 526 } 527 } 528 529 530 public int read() throws IOException { 531 int x = stream.read(); 532 if (mode && (x != -1)) { 533 buffer.append((char)x); 534 counter++; 535 } 536 537 if (x == (int)'\n') { 539 if (lineCounter == lines.length - 1) { 540 int[] newLines = new int[lineCounter + lineCounter]; 541 System.arraycopy(lines, 0, newLines, 0, lines.length); 542 lines = newLines; 543 } 544 lines[lineCounter] = lines[lineCounter - 1] + currentLineLength + 1; 545 lineCounter++; 546 currentLineLength = 0; 547 } 548 else { 549 currentLineLength++; 550 } 551 552 return x; 553 } 554 555 556 public void close() throws IOException { 557 stream.close(); 558 } 559 560 563 int getOffset(int line, int column) { 564 return lines[line] + column - 1; 565 } 566 567 } 568 } 569 | Popular Tags |