1 17 package org.eclipse.emf.common; 18 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 import java.text.MessageFormat ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 import java.util.MissingResourceException ; 28 import java.util.PropertyResourceBundle ; 29 import java.util.ResourceBundle ; 30 31 import org.eclipse.core.runtime.IStatus; 32 import org.eclipse.core.runtime.Platform; 33 import org.eclipse.core.runtime.Plugin; 34 import org.eclipse.core.runtime.Status; 35 36 import org.eclipse.emf.common.util.Logger; 37 import org.eclipse.emf.common.util.ResourceLocator; 38 import org.eclipse.emf.common.util.URI; 39 import org.eclipse.emf.common.util.WrappedException; 40 41 42 58 public abstract class EMFPlugin implements ResourceLocator, Logger 59 { 60 public static final boolean IS_ECLIPSE_RUNNING; 61 static 62 { 63 boolean result = false; 64 try 65 { 66 result = Platform.isRunning(); 67 } 68 catch (Throwable exception) 69 { 70 } 71 IS_ECLIPSE_RUNNING = result; 72 } 73 74 protected ResourceLocator [] delegateResourceLocators; 75 protected URL baseURL; 76 protected ResourceBundle untranslatedResourceBundle; 77 protected ResourceBundle resourceBundle; 78 protected Map strings = new HashMap (); 79 protected Map untranslatedStrings = new HashMap (); 80 protected boolean shouldTranslate = true; 81 protected Map images = new HashMap (); 82 83 public EMFPlugin(ResourceLocator [] delegateResourceLocators) 84 { 85 this.delegateResourceLocators = delegateResourceLocators; 86 } 87 88 92 public abstract ResourceLocator getPluginResourceLocator(); 93 94 98 public Logger getPluginLogger() 99 { 100 return (Logger)getPluginResourceLocator(); 101 } 102 103 public String getSymbolicName() 104 { 105 if (getPluginResourceLocator() instanceof EclipsePlugin) 106 { 107 return ((EclipsePlugin)getPluginResourceLocator()).getSymbolicName(); 108 } 109 else 110 { 111 throw new UnsupportedOperationException ("Plugin ID not available " + this); 112 } 113 } 114 115 118 public URL getBaseURL() 119 { 120 if (baseURL == null) 121 { 122 if (getPluginResourceLocator() == null) 123 { 124 try 125 { 126 Class theClass = getClass(); 129 URL pluginPropertiesURL = theClass.getResource("plugin.properties"); 130 if (pluginPropertiesURL == null) 131 { 132 String className = theClass.getName(); 152 int index = className.lastIndexOf("."); 153 URL classURL = theClass.getResource((index == -1 ? className : className.substring(index + 1)) + ".class"); 154 URI uri = URI.createURI(classURL.toString()); 155 156 int count = 1; 159 for (int i = 0; (i = className.indexOf('.', i)) != -1; ++i) 160 { 161 ++count; 162 } 163 uri = uri.trimSegments(count); 164 165 if (URI.isArchiveScheme(uri.scheme())) 168 { 169 try 170 { 171 InputStream inputStream = new URL (uri.appendSegment("plugin.properties").toString()).openStream(); 174 inputStream.close(); 175 baseURL = new URL (uri.toString()); 176 } 177 catch (IOException exception) 178 { 179 uri = URI.createURI(uri.authority()).trimSegments(1); 184 } 185 } 186 187 if (baseURL == null) 190 { 191 String lastSegment = uri.lastSegment(); 194 if ("bin".equals(lastSegment) || "runtime".equals(lastSegment)) 195 { 196 uri = uri.trimSegments(1); 197 } 198 uri = uri.appendSegment("plugin.properties"); 199 try 200 { 201 InputStream inputStream = new URL (uri.toString()).openStream(); 204 inputStream.close(); 205 baseURL = new URL (uri.trimSegments(1).toString() + "/"); 206 } 207 catch (IOException exception) 208 { 209 } 210 } 211 212 if (baseURL == null) 215 { 216 String resourceName = 217 index == -1 ? 218 "plugin.properties" : 219 className.substring(0, index + 1).replace('.','/') + "plugin.properties"; 220 throw new MissingResourceException ("Missing properties: " + resourceName, theClass.getName(), "plugin.properties"); 221 } 222 } 223 else 224 { 225 baseURL = new URL (URI.createURI(pluginPropertiesURL.toString()).trimSegments(1).toString() + "/"); 226 } 227 } 228 catch (IOException exception) 229 { 230 throw new WrappedException(exception); 231 } 232 } 233 else 234 { 235 baseURL = getPluginResourceLocator().getBaseURL(); 236 } 237 } 238 239 return baseURL; 240 } 241 242 245 public Object getImage(String key) 246 { 247 Object result = (URL )images.get(key); 248 if (result == null) 249 { 250 if (getPluginResourceLocator() == null) 251 { 252 try 253 { 254 result = doGetImage(key); 255 } 256 catch (MalformedURLException exception) 257 { 258 throw new WrappedException(exception); 259 } 260 catch (IOException exception) 261 { 262 result = delegatedGetImage(key); 263 } 264 } 265 else 266 { 267 try 268 { 269 result = getPluginResourceLocator().getImage(key); 270 } 271 catch (MissingResourceException exception) 272 { 273 result = delegatedGetImage(key); 274 } 275 } 276 277 images.put(key, result); 278 } 279 280 return result; 281 } 282 283 290 protected Object doGetImage(String key) throws IOException 291 { 292 URL url = new URL (getBaseURL() + "icons/" + key + ".gif"); 293 InputStream inputStream = url.openStream(); 294 inputStream.close(); 295 return url; 296 } 297 298 305 protected Object delegatedGetImage(String key) throws MissingResourceException 306 { 307 for (int i = 0; i < delegateResourceLocators.length; ++i) 308 { 309 try 310 { 311 return delegateResourceLocators[i].getImage(key); 312 } 313 catch (MissingResourceException exception) 314 { 315 } 316 } 317 318 throw 319 new MissingResourceException 320 (CommonPlugin.INSTANCE.getString("_UI_ImageResourceNotFound_exception", new Object [] { key }), 321 getClass().getName(), 322 key); 323 } 324 325 330 public boolean shouldTranslate() 331 { 332 return shouldTranslate; 333 } 334 335 340 public void setShouldTranslate(boolean shouldTranslate) 341 { 342 this.shouldTranslate = shouldTranslate; 343 } 344 345 348 public String getString(String key) 349 { 350 return getString(key, shouldTranslate()); 351 } 352 353 356 public String getString(String key, boolean translate) 357 { 358 Map stringMap = translate ? strings : untranslatedStrings; 359 String result = (String )stringMap.get(key); 360 if (result == null) 361 { 362 try 363 { 364 if (getPluginResourceLocator() == null) 365 { 366 ResourceBundle bundle = translate ? resourceBundle : untranslatedResourceBundle; 367 if (bundle == null) 368 { 369 String packageName = getClass().getName(); 370 int index = packageName.lastIndexOf("."); 371 if (index != -1) 372 { 373 packageName = packageName.substring(0, index); 374 } 375 if (translate) 376 { 377 try 378 { 379 bundle = resourceBundle = ResourceBundle.getBundle(packageName + ".plugin"); 380 } 381 catch (MissingResourceException exception) 382 { 383 try 387 { 388 InputStream inputStream = new URL (getBaseURL().toString() + "plugin.properties").openStream(); 389 bundle = untranslatedResourceBundle = resourceBundle = new PropertyResourceBundle (inputStream); 390 inputStream.close(); 391 } 392 catch (IOException ioException) 393 { 394 } 395 if (resourceBundle == null) 396 { 397 throw exception; 398 } 399 } 400 } 401 else 402 { 403 String resourceName = getBaseURL().toString() + "plugin.properties"; 404 try 405 { 406 InputStream inputStream = new URL (resourceName).openStream(); 407 bundle = untranslatedResourceBundle = new PropertyResourceBundle (inputStream); 408 inputStream.close(); 409 } 410 catch (IOException ioException) 411 { 412 throw new MissingResourceException ("Missing properties: " + resourceName, getClass().getName(), "plugin.properties"); 413 } 414 } 415 } 416 result = bundle.getString(key); 417 } 418 else 419 { 420 result = getPluginResourceLocator().getString(key, translate); 421 } 422 } 423 catch (MissingResourceException exception) 424 { 425 result = delegatedGetString(key, translate); 426 } 427 428 stringMap.put(key, result); 429 } 430 431 return result; 432 } 433 434 441 protected String delegatedGetString(String key, boolean translate) 442 { 443 for (int i = 0; i < delegateResourceLocators.length; ++i) 444 { 445 try 446 { 447 return delegateResourceLocators[i].getString(key, translate); 448 } 449 catch (MissingResourceException exception) 450 { 451 } 452 } 453 454 throw 455 new MissingResourceException 456 (MessageFormat.format("The string resource ''{0}'' could not be located", new Object [] { key }), 457 getClass().getName(), 458 key); 459 } 460 461 464 public String getString(String key, Object [] substitutions) 465 { 466 return getString(key, substitutions, shouldTranslate()); 467 } 468 469 472 public String getString(String key, Object [] substitutions, boolean translate) 473 { 474 return MessageFormat.format(getString(key, translate), substitutions); 475 } 476 477 480 public void log(Object logEntry) 481 { 482 Logger logger = getPluginLogger(); 483 if (logger == null) 484 { 485 if (logEntry instanceof Throwable ) 486 { 487 ((Throwable )logEntry).printStackTrace(System.err); 488 } 489 else 490 { 491 System.err.println(logEntry); 492 } 493 } 494 else 495 { 496 logger.log(logEntry); 497 } 498 } 499 500 503 public static abstract class EclipsePlugin extends Plugin implements ResourceLocator, Logger 504 { 505 protected ResourceBundle resourceBundle; 506 protected ResourceBundle untranslatedResourceBundle; 507 508 511 public EclipsePlugin() 512 { 513 super(); 514 } 515 516 521 public EclipsePlugin(org.eclipse.core.runtime.IPluginDescriptor descriptor) 522 { 523 super(descriptor); 524 } 525 526 529 public String getSymbolicName() 530 { 531 return getBundle().getSymbolicName(); 532 } 533 534 537 public URL getBaseURL() 538 { 539 return getBundle().getEntry("/"); 540 } 541 542 545 public Object getImage(String key) 546 { 547 try 548 { 549 return doGetImage(key); 550 } 551 catch (MalformedURLException exception) 552 { 553 throw new WrappedException(exception); 554 } 555 catch (IOException exception) 556 { 557 throw 558 new MissingResourceException 559 (CommonPlugin.INSTANCE.getString("_UI_StringResourceNotFound_exception", new Object [] { key }), 560 getClass().getName(), 561 key); 562 } 563 } 564 565 572 protected Object doGetImage(String key) throws IOException 573 { 574 URL url = new URL (getBaseURL() + "icons/" + key + ".gif"); 575 InputStream inputStream = url.openStream(); 576 inputStream.close(); 577 return url; 578 } 579 580 583 public String getString(String key) 584 { 585 return getString(key, true); 586 } 587 588 591 public String getString(String key, boolean translate) 592 { 593 ResourceBundle bundle = translate ? resourceBundle : untranslatedResourceBundle; 594 if (bundle == null) 595 { 596 if (translate) 597 { 598 bundle = resourceBundle = Platform.getResourceBundle(getBundle()); 599 } 600 else 601 { 602 String resourceName = getBaseURL().toString() + "plugin.properties"; 603 try 604 { 605 InputStream inputStream = new URL (resourceName).openStream(); 606 bundle = untranslatedResourceBundle = new PropertyResourceBundle (inputStream); 607 inputStream.close(); 608 } 609 catch (IOException ioException) 610 { 611 throw new MissingResourceException ("Missing properties: " + resourceName, getClass().getName(), "plugin.properties"); 612 } 613 } 614 } 615 return bundle.getString(key); 616 } 617 618 621 public String getString(String key, Object [] substitutions) 622 { 623 return getString(key, substitutions, true); 624 } 625 626 629 public String getString(String key, Object [] substitutions, boolean translate) 630 { 631 return MessageFormat.format(getString(key, translate), substitutions); 632 } 633 634 637 public void log(Object logEntry) 638 { 639 IStatus status; 640 if (logEntry instanceof IStatus) 641 { 642 status = (IStatus)logEntry; 643 getLog().log(status); 644 } 645 else 646 { 647 if (logEntry == null) 648 { 649 logEntry = new RuntimeException (getString("_UI_NullLogEntry_exception")).fillInStackTrace(); 650 } 651 652 if (logEntry instanceof Throwable ) 653 { 654 Throwable throwable = (Throwable )logEntry; 655 656 659 String message = throwable.getLocalizedMessage(); 660 if (message == null) 661 { 662 message = ""; 663 } 664 665 getLog().log(new Status(IStatus.WARNING, getBundle().getSymbolicName(), 0, message, throwable)); 666 } 667 else 668 { 669 672 getLog().log (new Status (IStatus.WARNING, getBundle().getSymbolicName(), 0, logEntry.toString(), null)); 673 } 674 } 675 } 676 } 677 } 678 | Popular Tags |