Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 222309 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/equinox/internal/provisional/p2/ui/query/IUPropertyUtils.java (-6 / +42 lines)
Lines 10-15 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.equinox.internal.provisional.p2.ui.query;
11
package org.eclipse.equinox.internal.provisional.p2.ui.query;
12
12
13
import java.net.URL;
13
import java.util.*;
14
import java.util.*;
14
import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper;
15
import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper;
15
import org.eclipse.equinox.internal.p2.ui.ProvUIActivator;
16
import org.eclipse.equinox.internal.p2.ui.ProvUIActivator;
Lines 24-35 Link Here
24
	static final Locale DEFAULT_LOCALE = new Locale("df", "LT"); //$NON-NLS-1$//$NON-NLS-2$
25
	static final Locale DEFAULT_LOCALE = new Locale("df", "LT"); //$NON-NLS-1$//$NON-NLS-2$
25
	static final String NAMESPACE_IU_LOCALIZATION = "org.eclipse.equinox.p2.localization"; //$NON-NLS-1$
26
	static final String NAMESPACE_IU_LOCALIZATION = "org.eclipse.equinox.p2.localization"; //$NON-NLS-1$
26
27
28
	public static License getLicense(IInstallableUnit iu, Locale locale) {
29
		License license = iu.getLicense();
30
		String body = (license != null ? license.getBody() : null);
31
		if (body == null || body.length() <= 1 || body.charAt(0) != '%')
32
			return license;
33
		final String actualKey = body.substring(1); // Strip off the %
34
		body = getLocalizedIUProperty(iu, actualKey, locale);
35
		URL url = license.getURL();
36
		return new License((url != null ? url.toExternalForm() : null), body);
37
	}
38
39
	public static Copyright getCopyright(IInstallableUnit iu, Locale locale) {
40
		Copyright copyright = iu.getCopyright();
41
		String body = (copyright != null ? copyright.getBody() : null);
42
		if (body == null || body.length() <= 1 || body.charAt(0) != '%')
43
			return copyright;
44
		final String actualKey = body.substring(1); // Strip off the %
45
		body = getLocalizedIUProperty(iu, actualKey, locale);
46
		URL url = copyright.getURL();
47
		return new Copyright((url != null ? url.toExternalForm() : null), body);
48
	}
49
27
	public static String getIUProperty(IInstallableUnit iu, String propertyKey, Locale locale) {
50
	public static String getIUProperty(IInstallableUnit iu, String propertyKey, Locale locale) {
28
		String value = iu.getProperty(propertyKey);
51
		String value = iu.getProperty(propertyKey);
29
		if (value == null || value.length() <= 1 || value.charAt(0) != '%')
52
		if (value == null || value.length() <= 1 || value.charAt(0) != '%')
30
			return value;
53
			return value;
31
		// else have a localizable property
54
		// else have a localizable property
32
		final String actualKey = value.substring(1); // Strip off the %
55
		final String actualKey = value.substring(1); // Strip off the %
56
		return getLocalizedIUProperty(iu, actualKey, locale);
57
	}
58
59
	private static String getLocalizedIUProperty(IInstallableUnit iu, String actualKey, Locale locale) {
60
		// Short circuit query if iu provides value for matching locale	
61
		String localizedKey = makeLocalizedKey(actualKey, locale.toString());
62
		String localizedValue = iu.getProperty(localizedKey);
63
		if (localizedValue != null)
64
			return localizedValue;
65
33
		final List locales = buildLocaleVariants(locale);
66
		final List locales = buildLocaleVariants(locale);
34
		final IInstallableUnit theUnit = iu;
67
		final IInstallableUnit theUnit = iu;
35
68
Lines 89-100 Link Here
89
			}
122
			}
90
		}
123
		}
91
124
92
		String defaultKey = makeLocalizedKey(actualKey, DEFAULT_LOCALE.toString());
125
		for (Iterator iter = locales.iterator(); iter.hasNext();) {
93
		String defaultValue = iu.getProperty(defaultKey);
126
			String nextLocale = (String) iter.next();
94
		if (defaultValue != null)
127
			String localeKey = makeLocalizedKey(actualKey, nextLocale);
95
			return defaultValue;
128
			String nextValue = iu.getProperty(localeKey);
129
			if (nextValue != null)
130
				return nextValue;
131
		}
96
132
97
		return value;
133
		return actualKey;
98
	}
134
	}
99
135
100
	/**
136
	/**
Lines 111-117 Link Here
111
			nl = nl.substring(0, lastSeparator);
147
			nl = nl.substring(0, lastSeparator);
112
		}
148
		}
113
		// Add the default locale (most general)
149
		// Add the default locale (most general)
114
		result.add(DEFAULT_LOCALE);
150
		result.add(DEFAULT_LOCALE.toString());
115
		return result;
151
		return result;
116
	}
152
	}
117
153
(-)src/org/eclipse/equinox/internal/provisional/p2/ui/dialogs/IULicensePropertyPage.java (-2 / +4 lines)
Lines 10-19 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.equinox.internal.provisional.p2.ui.dialogs;
11
package org.eclipse.equinox.internal.provisional.p2.ui.dialogs;
12
12
13
import java.util.Locale;
13
import org.eclipse.equinox.internal.p2.ui.ProvUIMessages;
14
import org.eclipse.equinox.internal.p2.ui.ProvUIMessages;
14
import org.eclipse.equinox.internal.p2.ui.dialogs.IUPropertyPage;
15
import org.eclipse.equinox.internal.p2.ui.dialogs.IUPropertyPage;
15
import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit;
16
import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit;
16
import org.eclipse.equinox.internal.provisional.p2.metadata.License;
17
import org.eclipse.equinox.internal.provisional.p2.metadata.License;
18
import org.eclipse.equinox.internal.provisional.p2.ui.query.IUPropertyUtils;
17
import org.eclipse.osgi.util.NLS;
19
import org.eclipse.osgi.util.NLS;
18
import org.eclipse.swt.SWT;
20
import org.eclipse.swt.SWT;
19
import org.eclipse.swt.events.SelectionAdapter;
21
import org.eclipse.swt.events.SelectionAdapter;
Lines 30-37 Link Here
30
public class IULicensePropertyPage extends IUPropertyPage {
32
public class IULicensePropertyPage extends IUPropertyPage {
31
33
32
	protected Control createIUPage(Composite parent, IInstallableUnit iu) {
34
	protected Control createIUPage(Composite parent, IInstallableUnit iu) {
33
35
		Locale currentLocale = Locale.getDefault();
34
		final License license = iu.getLicense();
36
		final License license = IUPropertyUtils.getLicense(iu, currentLocale);
35
		if (license != null && license.getBody().length() > 0) {
37
		if (license != null && license.getBody().length() > 0) {
36
			Composite composite = new Composite(parent, SWT.NONE);
38
			Composite composite = new Composite(parent, SWT.NONE);
37
			GridLayout layout = new GridLayout();
39
			GridLayout layout = new GridLayout();
(-)src/org/eclipse/equinox/internal/provisional/p2/ui/dialogs/IUCopyrightPropertyPage.java (-1 / +4 lines)
Lines 10-19 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.equinox.internal.provisional.p2.ui.dialogs;
11
package org.eclipse.equinox.internal.provisional.p2.ui.dialogs;
12
12
13
import java.util.Locale;
13
import org.eclipse.equinox.internal.p2.ui.ProvUIMessages;
14
import org.eclipse.equinox.internal.p2.ui.ProvUIMessages;
14
import org.eclipse.equinox.internal.p2.ui.dialogs.IUPropertyPage;
15
import org.eclipse.equinox.internal.p2.ui.dialogs.IUPropertyPage;
15
import org.eclipse.equinox.internal.provisional.p2.metadata.Copyright;
16
import org.eclipse.equinox.internal.provisional.p2.metadata.Copyright;
16
import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit;
17
import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit;
18
import org.eclipse.equinox.internal.provisional.p2.ui.query.IUPropertyUtils;
17
import org.eclipse.osgi.util.NLS;
19
import org.eclipse.osgi.util.NLS;
18
import org.eclipse.swt.SWT;
20
import org.eclipse.swt.SWT;
19
import org.eclipse.swt.events.SelectionAdapter;
21
import org.eclipse.swt.events.SelectionAdapter;
Lines 30-36 Link Here
30
public class IUCopyrightPropertyPage extends IUPropertyPage {
32
public class IUCopyrightPropertyPage extends IUPropertyPage {
31
33
32
	protected Control createIUPage(Composite parent, IInstallableUnit iu) {
34
	protected Control createIUPage(Composite parent, IInstallableUnit iu) {
33
		final Copyright copyright = iu.getCopyright();
35
		Locale currentLocale = Locale.getDefault();
36
		final Copyright copyright = IUPropertyUtils.getCopyright(iu, currentLocale);
34
		if (copyright != null && copyright.getBody().length() > 0) {
37
		if (copyright != null && copyright.getBody().length() > 0) {
35
			Composite composite = new Composite(parent, SWT.NONE);
38
			Composite composite = new Composite(parent, SWT.NONE);
36
			GridLayout layout = new GridLayout();
39
			GridLayout layout = new GridLayout();
(-)src/org/eclipse/equinox/internal/provisional/p2/metadata/generator/MetadataGeneratorHelper.java (-165 / +29 lines)
Lines 12-19 Link Here
12
package org.eclipse.equinox.internal.provisional.p2.metadata.generator;
12
package org.eclipse.equinox.internal.provisional.p2.metadata.generator;
13
13
14
import java.io.*;
14
import java.io.*;
15
import java.net.URL;
16
import java.net.URLConnection;
17
import java.util.*;
15
import java.util.*;
18
import java.util.zip.ZipEntry;
16
import java.util.zip.ZipEntry;
19
import java.util.zip.ZipFile;
17
import java.util.zip.ZipFile;
Lines 22-27 Link Here
22
import org.eclipse.equinox.internal.p2.metadata.ArtifactKey;
20
import org.eclipse.equinox.internal.p2.metadata.ArtifactKey;
23
import org.eclipse.equinox.internal.p2.metadata.InstallableUnit;
21
import org.eclipse.equinox.internal.p2.metadata.InstallableUnit;
24
import org.eclipse.equinox.internal.p2.metadata.generator.Activator;
22
import org.eclipse.equinox.internal.p2.metadata.generator.Activator;
23
import org.eclipse.equinox.internal.p2.metadata.generator.LocalizationHelper;
25
import org.eclipse.equinox.internal.p2.metadata.generator.features.SiteCategory;
24
import org.eclipse.equinox.internal.p2.metadata.generator.features.SiteCategory;
26
import org.eclipse.equinox.internal.provisional.frameworkadmin.BundleInfo;
25
import org.eclipse.equinox.internal.provisional.frameworkadmin.BundleInfo;
27
import org.eclipse.equinox.internal.provisional.p2.artifact.repository.ArtifactDescriptor;
26
import org.eclipse.equinox.internal.provisional.p2.artifact.repository.ArtifactDescriptor;
Lines 112-118 Link Here
112
111
113
	static final String DEFAULT_BUNDLE_LOCALIZATION = "plugin"; //$NON-NLS-1$	
112
	static final String DEFAULT_BUNDLE_LOCALIZATION = "plugin"; //$NON-NLS-1$	
114
	static final String PROPERTIES_FILE_EXTENSION = ".properties"; //$NON-NLS-1$
113
	static final String PROPERTIES_FILE_EXTENSION = ".properties"; //$NON-NLS-1$
115
	static final String MANIFEST_LOCALIZATIONS = "eclipse.p2.manifest.localizations"; //$NON-NLS-1$
116
114
117
	static final String BUNDLE_ADVICE_FILE = "META-INF/p2.inf"; //$NON-NLS-1$
115
	static final String BUNDLE_ADVICE_FILE = "META-INF/p2.inf"; //$NON-NLS-1$
118
	static final String ADVICE_INSTRUCTIONS_PREFIX = "instructions."; //$NON-NLS-1$
116
	static final String ADVICE_INSTRUCTIONS_PREFIX = "instructions."; //$NON-NLS-1$
Lines 643-649 Link Here
643
		// TODO: shouldn't the filter for the group be constructed from os, ws, arch, nl
641
		// TODO: shouldn't the filter for the group be constructed from os, ws, arch, nl
644
		// 		 of the feature?
642
		// 		 of the feature?
645
		// iu.setFilter(filter);
643
		// iu.setFilter(filter);
646
		iu.setCapabilities(new ProvidedCapability[] {createSelfCapability(id, version)});
644
645
		// Create set of provided capabilities
646
		ArrayList providedCapabilities = new ArrayList();
647
		providedCapabilities.add(createSelfCapability(id, version));
648
649
		Map manifestLocalizations = feature.getLocalizations();
650
		if (manifestLocalizations != null) {
651
			for (Iterator iter = manifestLocalizations.keySet().iterator(); iter.hasNext();) {
652
				Locale locale = (Locale) iter.next();
653
				Properties translatedStrings = (Properties) manifestLocalizations.get(locale);
654
				Enumeration propertyKeys = translatedStrings.propertyNames();
655
				while (propertyKeys.hasMoreElements()) {
656
					String nextKey = (String) propertyKeys.nextElement();
657
					iu.setProperty(locale.toString() + '.' + nextKey, translatedStrings.getProperty(nextKey));
658
				}
659
				providedCapabilities.add(makeTranslationCapability(id, locale));
660
			}
661
		}
662
663
		iu.setCapabilities((ProvidedCapability[]) providedCapabilities.toArray(new ProvidedCapability[providedCapabilities.size()]));
647
664
648
		if (extraProperties != null) {
665
		if (extraProperties != null) {
649
			Enumeration e = extraProperties.propertyNames();
666
			Enumeration e = extraProperties.propertyNames();
Lines 663-672 Link Here
663
		Version version = new Version(feature.getVersion());
680
		Version version = new Version(feature.getVersion());
664
		iu.setVersion(version);
681
		iu.setVersion(version);
665
		iu.setProperty(IInstallableUnit.PROP_NAME, feature.getLabel());
682
		iu.setProperty(IInstallableUnit.PROP_NAME, feature.getLabel());
666
		if (feature.getDescription() != null)
667
			iu.setProperty(IInstallableUnit.PROP_DESCRIPTION, feature.getDescription());
668
		if (feature.getDescriptionURL() != null)
669
			iu.setProperty(IInstallableUnit.PROP_DESCRIPTION_URL, feature.getDescriptionURL());
670
		if (feature.getLicense() != null)
683
		if (feature.getLicense() != null)
671
			iu.setLicense(new License(feature.getLicenseURL(), feature.getLicense()));
684
			iu.setLicense(new License(feature.getLicenseURL(), feature.getLicense()));
672
		if (feature.getCopyright() != null)
685
		if (feature.getCopyright() != null)
Lines 1098-1106 Link Here
1098
1111
1099
		if ("jar".equalsIgnoreCase(new Path(bundleLocation.getName()).getFileExtension()) && //$NON-NLS-1$
1112
		if ("jar".equalsIgnoreCase(new Path(bundleLocation.getName()).getFileExtension()) && //$NON-NLS-1$
1100
				bundleLocation.isFile()) {
1113
				bundleLocation.isFile()) {
1101
			localizations = getJarManifestLocalization(bundleLocation, bundleLocalization, defaultLocale, bundleManifestValues);
1114
			localizations = LocalizationHelper.getJarPropertyLocalizations(bundleLocation, bundleLocalization, defaultLocale, bundleManifestValues);
1115
			//localizations = getJarManifestLocalization(bundleLocation, bundleLocalization, defaultLocale, bundleManifestValues);
1102
		} else {
1116
		} else {
1103
			localizations = getDirManifestLocalization(bundleLocation, bundleLocalization, defaultLocale, bundleManifestValues);
1117
			localizations = LocalizationHelper.getDirPropertyLocalizations(bundleLocation, bundleLocalization, defaultLocale, bundleManifestValues);
1118
			// localizations = getDirManifestLocalization(bundleLocation, bundleLocalization, defaultLocale, bundleManifestValues);
1104
		}
1119
		}
1105
1120
1106
		return localizations;
1121
		return localizations;
Lines 1129-1293 Link Here
1129
1144
1130
		if ("jar".equalsIgnoreCase(new Path(bundleLocation.getName()).getFileExtension()) && //$NON-NLS-1$
1145
		if ("jar".equalsIgnoreCase(new Path(bundleLocation.getName()).getFileExtension()) && //$NON-NLS-1$
1131
				bundleLocation.isFile()) {
1146
				bundleLocation.isFile()) {
1132
			localizations = getJarManifestLocalization(bundleLocation, hostBundleLocalization, defaultLocale, hostBundleManifestValues);
1147
			localizations = LocalizationHelper.getJarPropertyLocalizations(bundleLocation, hostBundleLocalization, defaultLocale, hostBundleManifestValues);
1148
			//localizations = getJarManifestLocalization(bundleLocation, hostBundleLocalization, defaultLocale, hostBundleManifestValues);
1133
		} else {
1149
		} else {
1134
			localizations = getDirManifestLocalization(bundleLocation, hostBundleLocalization, defaultLocale, hostBundleManifestValues);
1150
			localizations = LocalizationHelper.getDirPropertyLocalizations(bundleLocation, hostBundleLocalization, defaultLocale, hostBundleManifestValues);
1135
		}
1151
			// localizations = getDirManifestLocalization(bundleLocation, hostBundleLocalization, defaultLocale, hostBundleManifestValues);
1136
1137
		return localizations;
1138
	}
1139
1140
	private static Map getJarManifestLocalization(File bundleLocation, String bundleLocalization, Locale defaultLocale, String[] bundleManifestValues) {
1141
		ZipFile jarFile = null;
1142
		Map localizations = new HashMap(4);
1143
		try {
1144
			jarFile = new ZipFile(bundleLocation, ZipFile.OPEN_READ);
1145
			for (Enumeration entries = jarFile.entries(); entries.hasMoreElements();) {
1146
				ZipEntry nextEntry = (ZipEntry) entries.nextElement();
1147
				String nextName = nextEntry.getName();
1148
				String localeString = getLocaleString(nextName, bundleLocalization);
1149
1150
				if (!nextEntry.isDirectory() && localeString != null) {
1151
					Locale nextLocale = getLocale(localeString);
1152
					InputStream stream = null;
1153
					try {
1154
						stream = jarFile.getInputStream(nextEntry);
1155
						Properties properties = new Properties();
1156
						properties.load(stream);
1157
						Properties localizedStrings = getLocalizedProperties(bundleManifestValues, properties);
1158
						if (localizedStrings.size() > 0) {
1159
							localizations.put(nextLocale, localizedStrings);
1160
							if (DEFAULT_LOCALE.equals(nextLocale) && defaultLocale != null) {
1161
								localizations.put(nextLocale, localizedStrings);
1162
							}
1163
						}
1164
					} finally {
1165
						if (stream != null)
1166
							stream.close();
1167
					}
1168
				}
1169
			}
1170
		} catch (IOException ioe) {
1171
			ioe.printStackTrace();
1172
		} finally {
1173
			if (jarFile != null) {
1174
				try {
1175
					jarFile.close();
1176
				} catch (IOException ioe) {
1177
					// do nothing
1178
				}
1179
			}
1180
		}
1152
		}
1181
1153
1182
		return localizations;
1154
		return localizations;
1183
	}
1155
	}
1184
1156
1185
	private static Map getDirManifestLocalization(File bundleLocation, String bundleLocalization, Locale defaultLocale, String[] hostBundleManifestValues) {
1186
		File localizationPath = new File(bundleLocation, bundleLocalization);
1187
		File localizationDir = localizationPath.getParentFile();
1188
		final String localizationFile = localizationPath.getName();
1189
		MetadataGeneratorHelper foo = new MetadataGeneratorHelper();
1190
		String[] localizationFiles = localizationDir.list(foo.new LocalizationFileFilter() {
1191
			public boolean accept(File directory, String filename) {
1192
				return (getLocaleString(filename, localizationFile) != null ? true : false);
1193
			}
1194
		});
1195
1196
		HashMap localizations = null;
1197
1198
		if (localizationFiles != null) {
1199
			localizations = new HashMap(localizationFiles.length);
1200
			for (int i = 0; i < localizationFiles.length; i++) {
1201
				String nextFile = localizationFiles[i];
1202
				Locale nextLocale = getLocale(getLocaleString(nextFile, localizationFile));
1203
1204
				try {
1205
					Properties properties = loadProperties(bundleLocation, nextFile);
1206
					Properties localizedStrings = getLocalizedProperties(hostBundleManifestValues, properties);
1207
					if (localizedStrings.size() > 0) {
1208
						localizations.put(nextLocale, localizedStrings);
1209
						if (DEFAULT_LOCALE.equals(nextLocale) && defaultLocale != null) {
1210
							localizations.put(nextLocale, localizedStrings);
1211
						}
1212
					}
1213
				} catch (IOException ioe) {
1214
					ioe.printStackTrace();
1215
				}
1216
			}
1217
		}
1218
1219
		return localizations;
1220
	}
1221
1222
	private abstract class LocalizationFileFilter implements FilenameFilter {
1223
1224
		public LocalizationFileFilter() {
1225
			// Nothing to do
1226
		}
1227
1228
		/* (non-Javadoc)
1229
		 * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
1230
		 */
1231
		public abstract boolean accept(File directory, String filename);
1232
	}
1233
1234
	static public String getLocaleString(String filename, String filenamePrefix) {
1235
		String localeString = null;
1236
		if (filename.startsWith(filenamePrefix) && filename.endsWith(PROPERTIES_FILE_EXTENSION)) {
1237
			if (filename.length() > filenamePrefix.length() + PROPERTIES_FILE_EXTENSION.length()) {
1238
				localeString = filename.substring(filenamePrefix.length() + 1, filename.length() - PROPERTIES_FILE_EXTENSION.length());
1239
			} else {
1240
				localeString = ""; //$NON-NLS-1$
1241
			}
1242
		}
1243
		return localeString;
1244
	}
1245
1246
	private static Properties loadProperties(File bundleLocation, String localizationFile) throws IOException {
1247
		Properties result = new Properties();
1248
		InputStream propertyStream = null;
1249
		try {
1250
			try {
1251
				if (bundleLocation.isDirectory())
1252
					propertyStream = new FileInputStream(new File(bundleLocation, localizationFile));
1253
				else {
1254
					URLConnection connection = new URL("jar:" + bundleLocation.toURL().toExternalForm() + "!/" + localizationFile).openConnection(); //$NON-NLS-1$ //$NON-NLS-2$
1255
					connection.setUseCaches(false);
1256
					propertyStream = connection.getInputStream();
1257
				}
1258
			} catch (FileNotFoundException e) {
1259
				// if there is no messages file then just return;
1260
				return result;
1261
			}
1262
			result.load(propertyStream);
1263
		} finally {
1264
			if (propertyStream != null)
1265
				propertyStream.close();
1266
		}
1267
		return result;
1268
	}
1269
1270
	static private Locale getLocale(String localeString) {
1271
		Locale locale = DEFAULT_LOCALE;
1272
		if (localeString.length() == 5 && localeString.indexOf('_') == 2) {
1273
			locale = new Locale(localeString.substring(0, 2), localeString.substring(3, 5));
1274
		} else if (localeString.length() == 2) {
1275
			locale = new Locale(localeString.substring(0, 2));
1276
		}
1277
		return locale;
1278
	}
1279
1280
	static private Properties getLocalizedProperties(String[] bundleManifestKeys, Properties properties) {
1281
		Properties localizedProperties = new Properties();
1282
		for (int i = 0; i < BUNDLE_LOCALIZED_PROPERTIES.length; i++) {
1283
			String key = bundleManifestKeys[i];
1284
			if (key != null) {
1285
				String localizedValue = properties.getProperty(key);
1286
				if (localizedValue != null)
1287
					localizedProperties.put(key, localizedValue);
1288
			}
1289
		}
1290
		return localizedProperties;
1291
	}
1292
1293
}
1157
}
(-)src/org/eclipse/equinox/internal/provisional/p2/metadata/generator/Feature.java (-3 / +15 lines)
Lines 11-16 Link Here
11
package org.eclipse.equinox.internal.provisional.p2.metadata.generator;
11
package org.eclipse.equinox.internal.provisional.p2.metadata.generator;
12
12
13
import java.util.ArrayList;
13
import java.util.ArrayList;
14
import java.util.Map;
14
15
15
/**
16
/**
16
 * 
17
 * 
Lines 43-48 Link Here
43
44
44
	private String location;
45
	private String location;
45
46
47
	private Map localizations;
48
46
	public Feature(String id, String version) {
49
	public Feature(String id, String version) {
47
		if (id == null)
50
		if (id == null)
48
			throw new IllegalArgumentException();
51
			throw new IllegalArgumentException();
Lines 50-63 Link Here
50
		this.version = version;
53
		this.version = version;
51
	}
54
	}
52
55
53
	public void addDiscoverySite(String label, String url) {
56
	public void addDiscoverySite(String siteLabel, String url) {
54
		if (label == null && url == null)
57
		if (siteLabel == null && url == null)
55
			return;
58
			return;
56
59
57
		if (this.discoverySites == null)
60
		if (this.discoverySites == null)
58
			this.discoverySites = new ArrayList();
61
			this.discoverySites = new ArrayList();
59
62
60
		URLEntry entry = new URLEntry(url, label);
63
		URLEntry entry = new URLEntry(url, siteLabel);
61
		this.discoverySites.add(entry);
64
		this.discoverySites.add(entry);
62
	}
65
	}
63
66
Lines 143-148 Link Here
143
		return null;
146
		return null;
144
	}
147
	}
145
148
149
	public Map getLocalizations() {
150
		return this.localizations;
151
	}
152
146
	public String getLocation() {
153
	public String getLocation() {
147
		return this.location;
154
		return this.location;
148
	}
155
	}
Lines 242-247 Link Here
242
		this.license.setURL(licenseURL);
249
		this.license.setURL(licenseURL);
243
	}
250
	}
244
251
252
	public void setLocalizations(Map localizations) {
253
		this.localizations = localizations;
254
	}
255
245
	public void setLocation(String location) {
256
	public void setLocation(String location) {
246
		this.location = location;
257
		this.location = location;
247
	}
258
	}
Lines 263-268 Link Here
263
	}
274
	}
264
275
265
	public void setURL(String value) {
276
	public void setURL(String value) {
277
		//
266
	}
278
	}
267
279
268
	public void setVersion(String version) {
280
	public void setVersion(String version) {
(-)src/org/eclipse/equinox/internal/p2/metadata/generator/features/FeatureParser.java (-19 / +31 lines)
Lines 12-18 Link Here
12
12
13
import java.io.*;
13
import java.io.*;
14
import java.net.URL;
14
import java.net.URL;
15
import java.util.Properties;
15
import java.util.*;
16
import java.util.jar.JarEntry;
16
import java.util.jar.JarEntry;
17
import java.util.jar.JarFile;
17
import java.util.jar.JarFile;
18
import javax.xml.parsers.*;
18
import javax.xml.parsers.*;
Lines 20-25 Link Here
20
import org.eclipse.core.runtime.Status;
20
import org.eclipse.core.runtime.Status;
21
import org.eclipse.equinox.internal.p2.core.helpers.LogHelper;
21
import org.eclipse.equinox.internal.p2.core.helpers.LogHelper;
22
import org.eclipse.equinox.internal.p2.metadata.generator.Activator;
22
import org.eclipse.equinox.internal.p2.metadata.generator.Activator;
23
import org.eclipse.equinox.internal.p2.metadata.generator.LocalizationHelper;
23
import org.eclipse.equinox.internal.provisional.p2.metadata.generator.Feature;
24
import org.eclipse.equinox.internal.provisional.p2.metadata.generator.Feature;
24
import org.eclipse.equinox.internal.provisional.p2.metadata.generator.FeatureEntry;
25
import org.eclipse.equinox.internal.provisional.p2.metadata.generator.FeatureEntry;
25
import org.eclipse.osgi.util.NLS;
26
import org.eclipse.osgi.util.NLS;
Lines 41-46 Link Here
41
	private StringBuffer characters = null;
42
	private StringBuffer characters = null;
42
43
43
	private Properties messages = null;
44
	private Properties messages = null;
45
	private List messageKeys = null;
44
46
45
	public FeatureParser() {
47
	public FeatureParser() {
46
		this(true);
48
		this(true);
Lines 87-103 Link Here
87
		return result;
89
		return result;
88
	}
90
	}
89
91
90
	private Properties loadProperties(File directory) {
92
	private void loadProperties(File directory, Properties properties) {
91
		//skip directories that don't contain a feature.properties file
93
		//skip directories that don't contain a feature.properties file
92
		File file = new File(directory, "feature.properties"); //$NON-NLS-1$
94
		File file = new File(directory, "feature.properties"); //$NON-NLS-1$
93
		if (!file.exists())
95
		if (!file.exists())
94
			return null;
96
			return;
95
		try {
97
		try {
96
			InputStream input = new BufferedInputStream(new FileInputStream(file));
98
			InputStream input = new BufferedInputStream(new FileInputStream(file));
97
			try {
99
			try {
98
				Properties result = new Properties();
100
				properties.load(input);
99
				result.load(input);
100
				return result;
101
			} finally {
101
			} finally {
102
				if (input != null)
102
				if (input != null)
103
					input.close();
103
					input.close();
Lines 105-123 Link Here
105
		} catch (IOException e) {
105
		} catch (IOException e) {
106
			e.printStackTrace();
106
			e.printStackTrace();
107
		}
107
		}
108
		return null;
109
	}
108
	}
110
109
111
	private Properties loadProperties(JarFile jar) {
110
	private void loadProperties(JarFile jar, Properties properties) {
112
		JarEntry entry = jar.getJarEntry("feature.properties"); //$NON-NLS-1$
111
		JarEntry entry = jar.getJarEntry("feature.properties"); //$NON-NLS-1$
113
		if (entry == null)
112
		if (entry == null)
114
			return null;
113
			return;
115
		try {
114
		try {
116
			InputStream input = new BufferedInputStream(jar.getInputStream(entry));
115
			InputStream input = new BufferedInputStream(jar.getInputStream(entry));
117
			try {
116
			try {
118
				Properties result = new Properties();
117
				properties.load(input);
119
				result.load(input);
120
				return result;
121
			} finally {
118
			} finally {
122
				if (input != null)
119
				if (input != null)
123
					input.close();
120
					input.close();
Lines 125-131 Link Here
125
		} catch (IOException e) {
122
		} catch (IOException e) {
126
			e.printStackTrace();
123
			e.printStackTrace();
127
		}
124
		}
128
		return null;
129
	}
125
	}
130
126
131
	private String localize(String value) {
127
	private String localize(String value) {
Lines 133-139 Link Here
133
			return value;
129
			return value;
134
		if (!value.startsWith("%")) //$NON-NLS-1$
130
		if (!value.startsWith("%")) //$NON-NLS-1$
135
			return value;
131
			return value;
136
		return messages.getProperty(value.substring(1), value);
132
		String key = value.substring(1);
133
		messageKeys.add(key);
134
		return value;
135
		//return messages.getProperty(value.substring(1), value);
137
	}
136
	}
138
137
139
	/**
138
	/**
Lines 146-160 Link Here
146
	public Feature parse(File location) {
145
	public Feature parse(File location) {
147
		if (!location.exists())
146
		if (!location.exists())
148
			return null;
147
			return null;
148
149
		Feature feature = null;
150
		Properties properties = new Properties();
151
149
		if (location.isDirectory()) {
152
		if (location.isDirectory()) {
150
			//skip directories that don't contain a feature.xml file
153
			//skip directories that don't contain a feature.xml file
151
			File file = new File(location, "feature.xml"); //$NON-NLS-1$
154
			File file = new File(location, "feature.xml"); //$NON-NLS-1$
152
			if (!file.exists())
155
			if (!file.exists())
153
				return null;
156
				return null;
154
			Properties properties = loadProperties(location);
157
			loadProperties(location, properties);
155
			try {
158
			try {
156
				InputStream input = new BufferedInputStream(new FileInputStream(file));
159
				InputStream input = new BufferedInputStream(new FileInputStream(file));
157
				return parse(input, properties);
160
				feature = parse(input, properties);
161
				if (feature != null) {
162
					String[] keyStrings = (String[]) messageKeys.toArray(new String[messageKeys.size()]);
163
					feature.setLocalizations(LocalizationHelper.getDirPropertyLocalizations(location, "feature", null, keyStrings)); //$NON-NLS-1$
164
				}
158
			} catch (FileNotFoundException e) {
165
			} catch (FileNotFoundException e) {
159
				e.printStackTrace();
166
				e.printStackTrace();
160
			}
167
			}
Lines 162-173 Link Here
162
			JarFile jar = null;
169
			JarFile jar = null;
163
			try {
170
			try {
164
				jar = new JarFile(location);
171
				jar = new JarFile(location);
165
				Properties properties = loadProperties(jar);
172
				loadProperties(jar, properties);
166
				JarEntry entry = jar.getJarEntry("feature.xml"); //$NON-NLS-1$
173
				JarEntry entry = jar.getJarEntry("feature.xml"); //$NON-NLS-1$
167
				if (entry == null)
174
				if (entry == null)
168
					return null;
175
					return null;
169
				InputStream input = new BufferedInputStream(jar.getInputStream(entry));
176
				InputStream input = new BufferedInputStream(jar.getInputStream(entry));
170
				return parse(input, properties);
177
				feature = parse(input, properties);
178
				if (feature != null) {
179
					String[] keyStrings = (String[]) messageKeys.toArray(new String[messageKeys.size()]);
180
					feature.setLocalizations(LocalizationHelper.getDirPropertyLocalizations(location, "feature", null, keyStrings)); //$NON-NLS-1$
181
				}
171
			} catch (IOException e) {
182
			} catch (IOException e) {
172
				e.printStackTrace();
183
				e.printStackTrace();
173
			} catch (SecurityException e) {
184
			} catch (SecurityException e) {
Lines 181-187 Link Here
181
				}
192
				}
182
			}
193
			}
183
		}
194
		}
184
		return null;
195
		return feature;
185
	}
196
	}
186
197
187
	/**
198
	/**
Lines 190-195 Link Here
190
	 */
201
	 */
191
	public Feature parse(InputStream in, Properties messages) {
202
	public Feature parse(InputStream in, Properties messages) {
192
		this.messages = messages;
203
		this.messages = messages;
204
		this.messageKeys = new ArrayList(messages.size());
193
		result = null;
205
		result = null;
194
		try {
206
		try {
195
			parser.parse(new InputSource(in), this);
207
			parser.parse(new InputSource(in), this);
(-)src/org/eclipse/equinox/internal/p2/metadata/generator/LocalizationHelper.java (+200 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 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.equinox.internal.p2.metadata.generator;
12
13
import java.io.*;
14
import java.net.URL;
15
import java.net.URLConnection;
16
import java.util.*;
17
import java.util.zip.ZipEntry;
18
import java.util.zip.ZipFile;
19
20
/**
21
 * 	Helper functions supporting the processing of localized
22
 * 	property files.
23
 *
24
 */
25
public final class LocalizationHelper {
26
27
	private static final String PROPERTIES_FILE_EXTENSION = ".properties"; //$NON-NLS-1$
28
	private static final Locale DEFAULT_LOCALE = new Locale("df", "LT"); //$NON-NLS-1$//$NON-NLS-2$
29
	private static LocalizationHelper instance = new LocalizationHelper();
30
31
	// Extract the locale string from the properties file with the given filename
32
	// where the locale string follows the given prefix. For example, return "zh_HK"
33
	// from filename == "plugin_zh_HK.properties" and prefix == "plugin". 
34
	static public String getLocaleString(String filename, String prefix) {
35
		String localeString = null;
36
		if (filename.startsWith(prefix) && filename.endsWith(PROPERTIES_FILE_EXTENSION)) {
37
			if (filename.length() > prefix.length() + PROPERTIES_FILE_EXTENSION.length()) {
38
				localeString = filename.substring(prefix.length() + 1, filename.length() - PROPERTIES_FILE_EXTENSION.length());
39
			} else {
40
				localeString = ""; //$NON-NLS-1$
41
			}
42
		}
43
		return localeString;
44
	}
45
46
	// Get the locale corresponding to the given locale string
47
	static public Locale getLocale(String localeString) {
48
		Locale locale = DEFAULT_LOCALE;
49
		if (localeString.length() == 5 && localeString.indexOf('_') == 2) {
50
			locale = new Locale(localeString.substring(0, 2), localeString.substring(3, 5));
51
		} else if (localeString.length() == 2) {
52
			locale = new Locale(localeString.substring(0, 2));
53
		}
54
		return locale;
55
	}
56
57
	// For the given root directory and path to localization files within that directory
58
	// get a map from locale to property set for the localization property files.
59
	public static Map getDirPropertyLocalizations(File root, String localizationPath, Locale defaultLocale, String[] propertyKeys) {
60
		File fullPath = new File(root, localizationPath);
61
		File localizationDir = fullPath.getParentFile();
62
		final String localizationFile = fullPath.getName();
63
		String[] localizationFiles = LocalizationHelper.getLocalizationFiles(localizationDir, localizationFile);
64
65
		HashMap localizations = null;
66
67
		if (localizationFiles != null) {
68
			localizations = new HashMap(localizationFiles.length);
69
			for (int i = 0; i < localizationFiles.length; i++) {
70
				String nextFile = localizationFiles[i];
71
				Locale nextLocale = getLocale(LocalizationHelper.getLocaleString(nextFile, localizationFile));
72
73
				try {
74
					Properties properties = loadProperties(root, nextFile);
75
					Properties localizedStrings = getLocalizedProperties(propertyKeys, properties);
76
					if (localizedStrings.size() > 0) {
77
						localizations.put(nextLocale, localizedStrings);
78
						if (DEFAULT_LOCALE.equals(nextLocale) && defaultLocale != null) {
79
							localizations.put(nextLocale, localizedStrings);
80
						}
81
					}
82
				} catch (IOException ioe) {
83
					ioe.printStackTrace();
84
				}
85
			}
86
		}
87
88
		return localizations;
89
	}
90
91
	public static Map getJarPropertyLocalizations(File root, String localizationPath, Locale defaultLocale, String[] propertyKeys) {
92
		ZipFile jarFile = null;
93
		Map localizations = new HashMap(4);
94
		try {
95
			jarFile = new ZipFile(root, ZipFile.OPEN_READ);
96
			for (Enumeration entries = jarFile.entries(); entries.hasMoreElements();) {
97
				ZipEntry nextEntry = (ZipEntry) entries.nextElement();
98
				String nextName = nextEntry.getName();
99
				String localeString = LocalizationHelper.getLocaleString(nextName, localizationPath);
100
101
				if (!nextEntry.isDirectory() && localeString != null) {
102
					Locale nextLocale = LocalizationHelper.getLocale(localeString);
103
					InputStream stream = null;
104
					try {
105
						stream = jarFile.getInputStream(nextEntry);
106
						Properties properties = new Properties();
107
						properties.load(stream);
108
						Properties localizedStrings = LocalizationHelper.getLocalizedProperties(propertyKeys, properties);
109
						if (localizedStrings.size() > 0) {
110
							localizations.put(nextLocale, localizedStrings);
111
							if (DEFAULT_LOCALE.equals(nextLocale) && defaultLocale != null) {
112
								localizations.put(nextLocale, localizedStrings);
113
							}
114
						}
115
					} finally {
116
						if (stream != null)
117
							stream.close();
118
					}
119
				}
120
			}
121
		} catch (IOException ioe) {
122
			ioe.printStackTrace();
123
		} finally {
124
			if (jarFile != null) {
125
				try {
126
					jarFile.close();
127
				} catch (IOException ioe) {
128
					// do nothing
129
				}
130
			}
131
		}
132
133
		return localizations;
134
	}
135
136
	// Load a property set from given root and file with the given name
137
	private static Properties loadProperties(File root, String propertyFilename) throws IOException {
138
		Properties result = new Properties();
139
		InputStream propertyStream = null;
140
		try {
141
			try {
142
				if (root.isDirectory())
143
					propertyStream = new FileInputStream(new File(root, propertyFilename));
144
				else {
145
					URLConnection connection = new URL("jar:" + root.toURL().toExternalForm() + "!/" + propertyFilename).openConnection(); //$NON-NLS-1$ //$NON-NLS-2$
146
					connection.setUseCaches(false);
147
					propertyStream = connection.getInputStream();
148
				}
149
			} catch (FileNotFoundException e) {
150
				// if there is no messages file then just return;
151
				return result;
152
			}
153
			result.load(propertyStream);
154
		} finally {
155
			if (propertyStream != null)
156
				propertyStream.close();
157
		}
158
		return result;
159
	}
160
161
	// Given a list of keys and the corresponding localized property set,
162
	// return a new property set with those keys and the localized values. 
163
	static public Properties getLocalizedProperties(String[] propertyKeys, Properties properties) {
164
		Properties localizedProperties = new Properties();
165
		for (int i = 0; i < propertyKeys.length; i++) {
166
			String key = propertyKeys[i];
167
			if (key != null) {
168
				String localizedValue = properties.getProperty(key);
169
				if (localizedValue != null)
170
					localizedProperties.put(key, localizedValue);
171
			}
172
		}
173
		return localizedProperties;
174
	}
175
176
	public static String[] getLocalizationFiles(File localizationDir, final String filenamePrefix) {
177
		return localizationDir.list(instance.new FileFilter() {
178
			public boolean accept(File directory, String filename) {
179
				return (getLocaleString(filename, filenamePrefix) != null ? true : false);
180
			}
181
		});
182
	}
183
184
	private abstract class FileFilter implements FilenameFilter {
185
186
		public FileFilter() {
187
			// Nothing to do
188
		}
189
190
		/* (non-Javadoc)
191
		 * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
192
		 */
193
		public abstract boolean accept(File directory, String filename);
194
	}
195
196
	private LocalizationHelper() {
197
		//
198
	}
199
200
}

Return to bug 222309