001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.intro.impl.model.util;
011:
012: import java.io.IOException;
013: import java.net.URL;
014:
015: import org.eclipse.core.runtime.FileLocator;
016: import org.eclipse.core.runtime.IConfigurationElement;
017: import org.eclipse.core.runtime.IPath;
018: import org.eclipse.core.runtime.IProduct;
019: import org.eclipse.core.runtime.Path;
020: import org.eclipse.core.runtime.Platform;
021: import org.eclipse.ui.internal.intro.impl.util.Log;
022: import org.eclipse.ui.internal.intro.impl.util.StringUtil;
023: import org.osgi.framework.Bundle;
024: import org.osgi.framework.Constants;
025:
026: /**
027: * Bundle convenience methods.
028: */
029: public class BundleUtil {
030:
031: private static String NL_TAG = "$nl$/"; //$NON-NLS-1$
032: private final static String PRODUCT_PLUGIN = "PRODUCT_PLUGIN"; //$NON-NLS-1$
033: private final static String PLUGINS_ROOT = "PLUGINS_ROOT/"; //$NON-NLS-1$
034:
035: /**
036: * Utility method to validate the state of a bundle. Log invalid bundles to
037: * log file.
038: */
039: public static boolean bundleHasValidState(Bundle bundle) {
040: if (bundle == null || bundle.getState() == Bundle.UNINSTALLED
041: || bundle.getState() == Bundle.INSTALLED) {
042:
043: if (bundle == null)
044: Log.error("Intro tried accessing a NULL bundle.", null); //$NON-NLS-1$
045: else {
046: String msg = StringUtil
047: .concat(
048: "Intro tried accessing Bundle: ", getBundleHeader( //$NON-NLS-1$
049: bundle, Constants.BUNDLE_NAME),
050: " vendor: ", //$NON-NLS-1$
051: getBundleHeader(bundle,
052: Constants.BUNDLE_VENDOR),
053: " bundle state: ", String.valueOf(bundle.getState())).toString(); //$NON-NLS-1$
054: Log.error(msg, null);
055: }
056: return false;
057: }
058:
059: return true;
060: }
061:
062: /**
063: * Retrieves the given key from the bundle header.
064: *
065: * @param bundle
066: * @param key
067: * @return
068: */
069: public static String getBundleHeader(Bundle bundle, String key) {
070: return (String) bundle.getHeaders().get(key);
071: }
072:
073: public static Bundle getBundleFromConfigurationElement(
074: IConfigurationElement cfg) {
075: return Platform.getBundle(cfg.getContributor().getName());
076: }
077:
078: /**
079: * Get the resourcelocation, but do not force an $nl$ on it.
080: *
081: * @param resource
082: * @param element
083: * @return
084: */
085: public static String getResourceLocation(String resource,
086: IConfigurationElement element) {
087: Bundle bundle = getBundleFromConfigurationElement(element);
088: return getResolvedResourceLocation(resource, bundle, false);
089: }
090:
091: /**
092: * Returns the fully qualified location of the passed resource string from
093: * the passed plugin id. If the file could not be loaded from the plugin,
094: * the resource is returned as is.
095: *
096: * @param resource
097: * @return
098: */
099: public static String getResolvedResourceLocation(String resource,
100: String pluginId) {
101: Bundle bundle = Platform.getBundle(pluginId);
102: return getResolvedResourceLocation(resource, bundle, true);
103: }
104:
105: /**
106: * Shorthand util method.
107: *
108: * @param resource
109: * @return
110: */
111: public static String getResolvedResourceLocation(String resource,
112: Bundle bundle) {
113: return getResolvedResourceLocation(resource, bundle, true);
114: }
115:
116: public static String getResolvedResourceLocation(String base,
117: String resource, Bundle bundle) {
118: // quick exits.
119: if (resource == null)
120: return null;
121:
122: String fullResource = new Path(base).append(resource)
123: .toString();
124: String resolvedResource = getResolvedResourceLocation(
125: fullResource, bundle, true);
126:
127: if (resolvedResource.equals(fullResource))
128: // return resource as is when the resource does not exist.
129: return resource;
130: return resolvedResource;
131: }
132:
133: public static String getResolvedResourceLocation(String resource,
134: Bundle bundle, boolean forceNLResolve) {
135: // quick exits.
136: if (resource == null)
137: return null;
138:
139: if (bundle == null || !bundleHasValidState(bundle))
140: return resource;
141:
142: URL localLocation = null;
143: try {
144: // resolve PLUGINS_ROOT
145: int index = resource.indexOf(PLUGINS_ROOT);
146: if (index != -1) {
147: resource = resource.substring(index
148: + PLUGINS_ROOT.length());
149: index = resource.indexOf('/');
150: if (index != -1) {
151: String bundleName = resource.substring(0, index);
152: if (PRODUCT_PLUGIN.equals(bundleName)) {
153: IProduct product = Platform.getProduct();
154: if (product != null) {
155: Bundle productBundle = product
156: .getDefiningBundle();
157: if (productBundle != null) {
158: bundleName = productBundle
159: .getSymbolicName();
160: }
161: }
162: }
163: resource = resource.substring(index + 1);
164: Bundle actualBundle = Platform
165: .getBundle(bundleName);
166: if (actualBundle != null) {
167: return getResolvedResourceLocation(resource,
168: actualBundle, forceNLResolve);
169: }
170: }
171: }
172:
173: // we need to resolve this URL.
174: String copyResource = resource;
175: if (forceNLResolve && !copyResource.startsWith(NL_TAG)) {
176: if (copyResource.startsWith("/") //$NON-NLS-1$
177: || copyResource.startsWith("\\")) //$NON-NLS-1$
178: copyResource = resource.substring(1);
179: copyResource = NL_TAG + copyResource;
180: }
181: IPath resourcePath = new Path(copyResource);
182: localLocation = FileLocator
183: .find(bundle, resourcePath, null);
184: if (localLocation == null) {
185: // localLocation can be null if the passed resource could not
186: // be found relative to the plugin. log fact, return resource,
187: // as is.
188: String msg = StringUtil.concat(
189: "Could not find resource: ", //$NON-NLS-1$
190: resource, " in ", getBundleHeader( //$NON-NLS-1$
191: bundle, Constants.BUNDLE_NAME))
192: .toString();
193: Log.warning(msg);
194: return resource;
195: }
196: /*
197: localLocation = FileLocator.toFileURL(localLocation);
198: return localLocation.toExternalForm();
199: */
200: return toExternalForm(localLocation);
201: } catch (Exception e) {
202: String msg = StringUtil.concat("Failed to load resource: ", //$NON-NLS-1$
203: resource, " from ", getBundleHeader(bundle, //$NON-NLS-1$
204: Constants.BUNDLE_NAME)).toString();
205: Log.error(msg, e);
206: return resource;
207: }
208: }
209:
210: /** *** used by Intro parser ***** */
211: /*
212: * Util method to return an URL to a plugin relative resource.
213: */
214: public static URL getResourceAsURL(String resource, String pluginId) {
215: Bundle bundle = Platform.getBundle(pluginId);
216: URL localLocation = FileLocator.find(bundle,
217: new Path(resource), null);
218: return localLocation;
219: }
220:
221: /** ********************* Used by HTML generator ****************** */
222: /**
223: * Get the absolute path of the given bundle, in the form
224: * file:/path_to_plugin
225: *
226: * @param bundle
227: * @return
228: */
229: public static String getResolvedBundleLocation(Bundle bundle) {
230: try {
231: URL bundleLocation = bundle.getEntry(""); //$NON-NLS-1$
232: if (bundleLocation == null)
233: return null;
234: /*
235: bundleLocation = FileLocator.toFileURL(bundleLocation);
236: return bundleLocation.toExternalForm();
237: */
238: return toExternalForm(bundleLocation);
239: } catch (IllegalStateException e) {
240: Log.error("Failed to access bundle: " //$NON-NLS-1$
241: + bundle.getSymbolicName(), e);
242: return null;
243: }/* catch (IOException e) {
244: Log.error("Failed to resolve URL path for bundle: " //$NON-NLS-1$
245: + bundle.getSymbolicName(), e);
246: return null;
247: } */
248: }
249:
250: /**
251: * Get the absolute path of the bundle with id <code>bundleId</code>. If
252: * no such bundle is found, return null.
253: *
254: * @param bundleId
255: * @return
256: */
257: public static String getResolvedBundleLocation(String bundleId) {
258: Bundle bundle = Platform.getBundle(bundleId);
259: if (bundle == null)
260: return null;
261: return getResolvedBundleLocation(bundle);
262: }
263:
264: /*
265: * Bug 126085 - need to fix up file: protocol
266: * to a form that IE7 understands (file:///).
267: */
268:
269: private static String toExternalForm(URL localURL) {
270: try {
271: localURL = FileLocator.toFileURL(localURL);
272: String result = localURL.toExternalForm();
273: if (result.startsWith("file:/")) { //$NON-NLS-1$
274: if (result.startsWith("file:///") == false) { //$NON-NLS-1$
275: result = "file:///" + result.substring(6); //$NON-NLS-1$
276: }
277: }
278: return result;
279: } catch (IOException e) {
280: String msg = "Failed to resolve URL: " //$NON-NLS-1$
281: + localURL.toString();
282: Log.error(msg, e);
283: return localURL.toString();
284: }
285: }
286:
287: }
|