KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > xhtml > BundleUtil


1 /***************************************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others. All rights reserved. This program and the
3  * accompanying materials are made available under the terms of the Eclipse Public License v1.0
4  * which accompanies this distribution, and is available at
5  * http://www.eclipse.org/legal/epl-v10.html
6  *
7  * Contributors: IBM Corporation - initial API and implementation
8  **************************************************************************************************/

9
10 package org.eclipse.help.internal.xhtml;
11
12 import java.net.URL JavaDoc;
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 /**
24  * Bundle convenience methods.
25  */

26 public class BundleUtil {
27
28
29     private static String JavaDoc NL_TAG = "$nl$/"; //$NON-NLS-1$
30

31     /*
32      * Util method to return an URL to a plugin relative resource.
33      */

34     public static URL JavaDoc getResourceAsURL(String JavaDoc resource, String JavaDoc pluginId) {
35         Bundle bundle = Platform.getBundle(pluginId);
36         URL JavaDoc 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     /**
47      * Get the resource location, but do not force an $nl$ on it.
48      *
49      * @param resource
50      * @param element
51      * @return
52      */

53     public static String JavaDoc getResourceLocation(String JavaDoc resource, IConfigurationElement element) {
54         Bundle bundle = getBundleFromConfigurationElement(element);
55         return getResolvedResourceLocation(resource, bundle, false);
56     }
57
58
59
60     public static String JavaDoc getResolvedResourceLocation(String JavaDoc resource, Bundle bundle, boolean forceNLResolve) {
61         // quick exits.
62
if (resource == null)
63             return null;
64
65         if (bundle == null || !bundleHasValidState(bundle))
66             return resource;
67
68         URL JavaDoc localLocation = null;
69         try {
70             // we need to resolve this URL.
71
String JavaDoc copyResource = resource;
72             if (forceNLResolve && !copyResource.startsWith(NL_TAG)) {
73                 if (copyResource.startsWith("/") //$NON-NLS-1$
74
|| copyResource.startsWith("\\")) //$NON-NLS-1$
75
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                 // localLocation can be null if the passed resource could not
82
// be found relative to the plugin. log fact, return resource,
83
// as is.
84
String JavaDoc msg = "Could not find resource: " + //$NON-NLS-1$
85
resource + " in " + getBundleHeader( //$NON-NLS-1$
86
bundle, Constants.BUNDLE_NAME);
87                 HelpPlugin.logWarning(msg);
88                 return resource;
89             }
90             localLocation = FileLocator.toFileURL(localLocation);
91             return localLocation.toExternalForm();
92         } catch (Exception JavaDoc e) {
93             String JavaDoc msg = "Failed to load resource: " + //$NON-NLS-1$
94
resource + " from " + getBundleHeader(bundle, //$NON-NLS-1$
95
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); //$NON-NLS-1$
107
else {
108                 String JavaDoc msg = "Help tried accessing Bundle: " + getBundleHeader( //$NON-NLS-1$
109
bundle, Constants.BUNDLE_NAME)
110                         + " vendor: " + //$NON-NLS-1$
111
getBundleHeader(bundle, Constants.BUNDLE_VENDOR)
112                         + " bundle state: " + String.valueOf(bundle.getState()); //$NON-NLS-1$
113
HelpPlugin.logError(msg, null);
114             }
115             return false;
116         }
117
118         return true;
119     }
120
121     public static String JavaDoc getBundleHeader(Bundle bundle, String JavaDoc key) {
122         return (String JavaDoc) bundle.getHeaders().get(key);
123     }
124 }
125
Popular Tags