KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > runtime > ResourceTranslator


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

11 package org.eclipse.core.internal.runtime;
12
13 import java.net.URL JavaDoc;
14 import java.net.URLClassLoader JavaDoc;
15 import java.util.*;
16 import org.eclipse.osgi.util.ManifestElement;
17 import org.osgi.framework.*;
18
19 /**
20  * This class can only be used if OSGi plugin is available.
21  */

22 public class ResourceTranslator {
23     private static final String JavaDoc KEY_PREFIX = "%"; //$NON-NLS-1$
24
private static final String JavaDoc KEY_DOUBLE_PREFIX = "%%"; //$NON-NLS-1$
25

26     public static String JavaDoc getResourceString(Bundle bundle, String JavaDoc value) {
27         return getResourceString(bundle, value, null);
28     }
29
30     public static String JavaDoc getResourceString(Bundle bundle, String JavaDoc value, ResourceBundle resourceBundle) {
31         String JavaDoc 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 JavaDoc key = ix == -1 ? s : s.substring(0, ix);
39         String JavaDoc 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                 // just return the default (dflt)
46
}
47         }
48
49         if (resourceBundle == null)
50             return dflt;
51
52         try {
53             return resourceBundle.getString(key.substring(1));
54         } catch (MissingResourceException e) {
55             //this will avoid requiring a bundle access on the next lookup
56
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)); //$NON-NLS-1$
63
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 JavaDoc) b.getHeaders("").get(Constants.REQUIRE_BUNDLE)); //$NON-NLS-1$
69
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())) { //$NON-NLS-1$//$NON-NLS-2$
73
return true;
74                 }
75             }
76         } catch (BundleException e) {
77             return false;
78         }
79         return false;
80     }
81
82     private static ClassLoader JavaDoc 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 JavaDoc[] urls = new URL JavaDoc[classpath.size()];
89         return new URLClassLoader JavaDoc((URL JavaDoc[]) 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 JavaDoc) b.getHeaders("").get(Constants.BUNDLE_CLASSPATH)); //$NON-NLS-1$
110
if (classpathElements == null)
111                 return;
112             for (int i = 0; i < classpathElements.length; i++) {
113                 URL JavaDoc classpathEntry = b.getEntry(classpathElements[i].getValue());
114                 if (classpathEntry != null)
115                     classpath.add(classpathEntry);
116             }
117         } catch (BundleException e) {
118             //ignore
119
}
120     }
121
122     private static void addBundleRoot(Bundle b, ArrayList classpath) {
123         classpath.add(b.getEntry("/")); //$NON-NLS-1$
124
}
125
126     private static void addDevEntries(Bundle b, ArrayList classpath) {
127         if (!DevClassPathHelper.inDevelopmentMode())
128             return;
129
130         String JavaDoc[] binaryPaths = DevClassPathHelper.getDevClassPath(b.getSymbolicName());
131         for (int i = 0; i < binaryPaths.length; i++) {
132             URL JavaDoc classpathEntry = b.getEntry(binaryPaths[i]);
133             if (classpathEntry != null)
134                 classpath.add(classpathEntry);
135         }
136     }
137 }
138
Popular Tags