1 9 10 package org.eclipse.help.internal.xhtml; 11 12 import java.net.URL ; 13 14 import org.eclipse.core.runtime.FileLocator; 15 import org.eclipse.core.runtime.IConfigurationElement; 16 import org.eclipse.core.runtime.IPath; 17 import org.eclipse.core.runtime.Path; 18 import org.eclipse.core.runtime.Platform; 19 import org.eclipse.help.internal.HelpPlugin; 20 import org.osgi.framework.Bundle; 21 import org.osgi.framework.Constants; 22 23 26 public class BundleUtil { 27 28 29 private static String NL_TAG = "$nl$/"; 31 34 public static URL getResourceAsURL(String resource, String pluginId) { 35 Bundle bundle = Platform.getBundle(pluginId); 36 URL localLocation = FileLocator.find(bundle, new Path(resource), null); 37 return localLocation; 38 } 39 40 41 public static Bundle getBundleFromConfigurationElement(IConfigurationElement cfg) { 42 return Platform.getBundle(cfg.getContributor().getName()); 43 } 44 45 46 53 public static String getResourceLocation(String resource, IConfigurationElement element) { 54 Bundle bundle = getBundleFromConfigurationElement(element); 55 return getResolvedResourceLocation(resource, bundle, false); 56 } 57 58 59 60 public static String getResolvedResourceLocation(String resource, Bundle bundle, boolean forceNLResolve) { 61 if (resource == null) 63 return null; 64 65 if (bundle == null || !bundleHasValidState(bundle)) 66 return resource; 67 68 URL localLocation = null; 69 try { 70 String copyResource = resource; 72 if (forceNLResolve && !copyResource.startsWith(NL_TAG)) { 73 if (copyResource.startsWith("/") || copyResource.startsWith("\\")) copyResource = resource.substring(1); 76 copyResource = NL_TAG + copyResource; 77 } 78 IPath resourcePath = new Path(copyResource); 79 localLocation = FileLocator.find(bundle, resourcePath, null); 80 if (localLocation == null) { 81 String msg = "Could not find resource: " + resource + " in " + getBundleHeader( bundle, Constants.BUNDLE_NAME); 87 HelpPlugin.logWarning(msg); 88 return resource; 89 } 90 localLocation = FileLocator.toFileURL(localLocation); 91 return localLocation.toExternalForm(); 92 } catch (Exception e) { 93 String msg = "Failed to load resource: " + resource + " from " + getBundleHeader(bundle, Constants.BUNDLE_NAME); 96 HelpPlugin.logError(msg, e); 97 return resource; 98 } 99 } 100 101 public static boolean bundleHasValidState(Bundle bundle) { 102 if (bundle == null || bundle.getState() == Bundle.UNINSTALLED 103 || bundle.getState() == Bundle.INSTALLED) { 104 105 if (bundle == null) 106 HelpPlugin.logError("Help tried accessing a NULL bundle.", null); else { 108 String msg = "Help tried accessing Bundle: " + getBundleHeader( bundle, Constants.BUNDLE_NAME) 110 + " vendor: " + getBundleHeader(bundle, Constants.BUNDLE_VENDOR) 112 + " bundle state: " + String.valueOf(bundle.getState()); HelpPlugin.logError(msg, null); 114 } 115 return false; 116 } 117 118 return true; 119 } 120 121 public static String getBundleHeader(Bundle bundle, String key) { 122 return (String ) bundle.getHeaders().get(key); 123 } 124 } 125 | Popular Tags |