Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4bd6d111bf3fe7113381d17ee8e5aa8dbf82c6c7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*******************************************************************************
 * Copyright (c) 2003, 2015 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Sergey Prigogin (Google) - use parameterized types (bug 442021)
 *******************************************************************************/
package org.eclipse.core.internal.runtime;

import java.net.URL;
import java.net.URLClassLoader;
import java.util.*;
import org.eclipse.osgi.util.ManifestElement;
import org.osgi.framework.*;

/**
 * This class can only be used if OSGi plugin is available.
 */
public class ResourceTranslator {
	private static final String KEY_PREFIX = "%"; //$NON-NLS-1$
	private static final String KEY_DOUBLE_PREFIX = "%%"; //$NON-NLS-1$	

	public static String getResourceString(Bundle bundle, String value) {
		return getResourceString(bundle, value, null);
	}

	public static String getResourceString(Bundle bundle, String value, ResourceBundle resourceBundle) {
		String s = value.trim();
		if (!s.startsWith(KEY_PREFIX, 0))
			return s;
		if (s.startsWith(KEY_DOUBLE_PREFIX, 0))
			return s.substring(1);

		int ix = s.indexOf(' ');
		String key = ix == -1 ? s : s.substring(0, ix);
		String dflt = ix == -1 ? s : s.substring(ix + 1);

		if (resourceBundle == null && bundle != null) {
			try {
				resourceBundle = getResourceBundle(bundle);
			} catch (MissingResourceException e) {
				// just return the default (dflt)
			}
		}

		if (resourceBundle == null)
			return dflt;

		try {
			return resourceBundle.getString(key.substring(1));
		} catch (MissingResourceException e) {
			//this will avoid requiring a bundle access on the next lookup
			return dflt;
		}
	}

	public static ResourceBundle getResourceBundle(Bundle bundle) throws MissingResourceException {
		return getResourceBundle(bundle, null);
	}

	private static ResourceBundle getResourceBundle(Bundle bundle, String language) throws MissingResourceException {
		if (hasRuntime21(bundle)) {
			Locale locale = (language == null) ? Locale.getDefault() : new Locale(language);
			return ResourceBundle.getBundle("plugin", locale, createTempClassloader(bundle)); //$NON-NLS-1$
		}
		return Activator.getDefault().getLocalization(bundle, language);
	}

	public static String[] getResourceString(Bundle bundle, String[] nonTranslated, String locale) {
		if (bundle == null)
			return nonTranslated;

		ResourceBundle resourceBundle = null;
		try {
			resourceBundle = getResourceBundle(bundle, locale);
		} catch (MissingResourceException e) {
			// ignore - bug 371103
		}
		String[] translated = new String[nonTranslated.length];
		for (int i = 0; i < nonTranslated.length; i++) {
			translated[i] = getResourceString(bundle, nonTranslated[i], resourceBundle);
		}
		return translated;
	}

	private static boolean hasRuntime21(Bundle b) {
		try {
			ManifestElement[] prereqs = ManifestElement.parseHeader(Constants.REQUIRE_BUNDLE, b.getHeaders("").get(Constants.REQUIRE_BUNDLE)); //$NON-NLS-1$
			if (prereqs == null)
				return false;
			for (int i = 0; i < prereqs.length; i++) {
				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$
					return true;
				}
			}
		} catch (BundleException e) {
			return false;
		}
		return false;
	}

	private static ClassLoader createTempClassloader(Bundle b) {
		ArrayList<URL> classpath = new ArrayList<>();
		addClasspathEntries(b, classpath);
		addBundleRoot(b, classpath);
		addDevEntries(b, classpath);
		addFragments(b, classpath);
		URL[] urls = new URL[classpath.size()];
		return new URLClassLoader(classpath.toArray(urls));
	}

	private static void addFragments(Bundle host, ArrayList<URL> classpath) {
		Activator activator = Activator.getDefault();
		if (activator == null)
			return;
		Bundle[] fragments = activator.getFragments(host);
		if (fragments == null)
			return;

		for (int i = 0; i < fragments.length; i++) {
			addClasspathEntries(fragments[i], classpath);
			addDevEntries(fragments[i], classpath);
		}
	}

	private static void addClasspathEntries(Bundle b, ArrayList<URL> classpath) {
		ManifestElement[] classpathElements;
		try {
			classpathElements = ManifestElement.parseHeader(Constants.BUNDLE_CLASSPATH, b.getHeaders("").get(Constants.BUNDLE_CLASSPATH)); //$NON-NLS-1$
			if (classpathElements == null)
				return;
			for (int i = 0; i < classpathElements.length; i++) {
				URL classpathEntry = b.getEntry(classpathElements[i].getValue());
				if (classpathEntry != null)
					classpath.add(classpathEntry);
			}
		} catch (BundleException e) {
			//ignore
		}
	}

	private static void addBundleRoot(Bundle b, ArrayList<URL> classpath) {
		classpath.add(b.getEntry("/")); //$NON-NLS-1$
	}

	private static void addDevEntries(Bundle b, ArrayList<URL> classpath) {
		if (!DevClassPathHelper.inDevelopmentMode())
			return;

		String[] binaryPaths = DevClassPathHelper.getDevClassPath(b.getSymbolicName());
		for (int i = 0; i < binaryPaths.length; i++) {
			URL classpathEntry = b.getEntry(binaryPaths[i]);
			if (classpathEntry != null)
				classpath.add(classpathEntry);
		}
	}
}

Back to the top