1 11 package org.eclipse.core.internal.runtime; 12 13 import java.net.URL ; 14 import java.net.URLClassLoader ; 15 import java.util.*; 16 import org.eclipse.osgi.util.ManifestElement; 17 import org.osgi.framework.*; 18 19 22 public class ResourceTranslator { 23 private static final String KEY_PREFIX = "%"; private static final String KEY_DOUBLE_PREFIX = "%%"; 26 public static String getResourceString(Bundle bundle, String value) { 27 return getResourceString(bundle, value, null); 28 } 29 30 public static String getResourceString(Bundle bundle, String value, ResourceBundle resourceBundle) { 31 String s = value.trim(); 32 if (!s.startsWith(KEY_PREFIX, 0)) 33 return s; 34 if (s.startsWith(KEY_DOUBLE_PREFIX, 0)) 35 return s.substring(1); 36 37 int ix = s.indexOf(' '); 38 String key = ix == -1 ? s : s.substring(0, ix); 39 String dflt = ix == -1 ? s : s.substring(ix + 1); 40 41 if (resourceBundle == null && bundle != null) { 42 try { 43 resourceBundle = getResourceBundle(bundle); 44 } catch (MissingResourceException e) { 45 } 47 } 48 49 if (resourceBundle == null) 50 return dflt; 51 52 try { 53 return resourceBundle.getString(key.substring(1)); 54 } catch (MissingResourceException e) { 55 return dflt; 57 } 58 } 59 60 public static ResourceBundle getResourceBundle(Bundle bundle) throws MissingResourceException { 61 if (hasRuntime21(bundle)) 62 return ResourceBundle.getBundle("plugin", Locale.getDefault(), createTempClassloader(bundle)); return Activator.getDefault().getLocalization(bundle, null); 64 } 65 66 private static boolean hasRuntime21(Bundle b) { 67 try { 68 ManifestElement[] prereqs = ManifestElement.parseHeader(Constants.REQUIRE_BUNDLE, (String ) b.getHeaders("").get(Constants.REQUIRE_BUNDLE)); if (prereqs == null) 70 return false; 71 for (int i = 0; i < prereqs.length; i++) { 72 if ("2.1".equals(prereqs[i].getAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE)) && "org.eclipse.core.runtime".equals(prereqs[i].getValue())) { return true; 74 } 75 } 76 } catch (BundleException e) { 77 return false; 78 } 79 return false; 80 } 81 82 private static ClassLoader createTempClassloader(Bundle b) { 83 ArrayList classpath = new ArrayList(); 84 addClasspathEntries(b, classpath); 85 addBundleRoot(b, classpath); 86 addDevEntries(b, classpath); 87 addFragments(b, classpath); 88 URL [] urls = new URL [classpath.size()]; 89 return new URLClassLoader ((URL []) classpath.toArray(urls)); 90 } 91 92 private static void addFragments(Bundle host, ArrayList classpath) { 93 Activator activator = Activator.getDefault(); 94 if (activator == null) 95 return; 96 Bundle[] fragments = activator.getFragments(host); 97 if (fragments == null) 98 return; 99 100 for (int i = 0; i < fragments.length; i++) { 101 addClasspathEntries(fragments[i], classpath); 102 addDevEntries(fragments[i], classpath); 103 } 104 } 105 106 private static void addClasspathEntries(Bundle b, ArrayList classpath) { 107 ManifestElement[] classpathElements; 108 try { 109 classpathElements = ManifestElement.parseHeader(Constants.BUNDLE_CLASSPATH, (String ) b.getHeaders("").get(Constants.BUNDLE_CLASSPATH)); if (classpathElements == null) 111 return; 112 for (int i = 0; i < classpathElements.length; i++) { 113 URL classpathEntry = b.getEntry(classpathElements[i].getValue()); 114 if (classpathEntry != null) 115 classpath.add(classpathEntry); 116 } 117 } catch (BundleException e) { 118 } 120 } 121 122 private static void addBundleRoot(Bundle b, ArrayList classpath) { 123 classpath.add(b.getEntry("/")); } 125 126 private static void addDevEntries(Bundle b, ArrayList classpath) { 127 if (!DevClassPathHelper.inDevelopmentMode()) 128 return; 129 130 String [] binaryPaths = DevClassPathHelper.getDevClassPath(b.getSymbolicName()); 131 for (int i = 0; i < binaryPaths.length; i++) { 132 URL classpathEntry = b.getEntry(binaryPaths[i]); 133 if (classpathEntry != null) 134 classpath.add(classpathEntry); 135 } 136 } 137 } 138 | Popular Tags |