Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 99221 Details for
Bug 222309
Review of the metadata translation support
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read
this important communication.
[patch]
Patch for localization of group IUs generated from features
222309Patch3.txt (text/plain), 34.07 KB, created by
Dave Stevenson
on 2008-05-08 01:51:13 EDT
(
hide
)
Description:
Patch for localization of group IUs generated from features
Filename:
MIME Type:
Creator:
Dave Stevenson
Created:
2008-05-08 01:51:13 EDT
Size:
34.07 KB
patch
obsolete
>### Eclipse Workspace Patch 1.0 >#P org.eclipse.equinox.p2.ui >Index: src/org/eclipse/equinox/internal/provisional/p2/ui/query/IUPropertyUtils.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.equinox/p2/bundles/org.eclipse.equinox.p2.ui/src/org/eclipse/equinox/internal/provisional/p2/ui/query/IUPropertyUtils.java,v >retrieving revision 1.3 >diff -u -r1.3 IUPropertyUtils.java >--- src/org/eclipse/equinox/internal/provisional/p2/ui/query/IUPropertyUtils.java 1 Apr 2008 00:48:07 -0000 1.3 >+++ src/org/eclipse/equinox/internal/provisional/p2/ui/query/IUPropertyUtils.java 8 May 2008 05:46:42 -0000 >@@ -10,6 +10,7 @@ > *******************************************************************************/ > package org.eclipse.equinox.internal.provisional.p2.ui.query; > >+import java.net.URL; > import java.util.*; > import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper; > import org.eclipse.equinox.internal.p2.ui.ProvUIActivator; >@@ -24,12 +25,44 @@ > static final Locale DEFAULT_LOCALE = new Locale("df", "LT"); //$NON-NLS-1$//$NON-NLS-2$ > static final String NAMESPACE_IU_LOCALIZATION = "org.eclipse.equinox.p2.localization"; //$NON-NLS-1$ > >+ public static License getLicense(IInstallableUnit iu, Locale locale) { >+ License license = iu.getLicense(); >+ String body = (license != null ? license.getBody() : null); >+ if (body == null || body.length() <= 1 || body.charAt(0) != '%') >+ return license; >+ final String actualKey = body.substring(1); // Strip off the % >+ body = getLocalizedIUProperty(iu, actualKey, locale); >+ URL url = license.getURL(); >+ return new License((url != null ? url.toExternalForm() : null), body); >+ } >+ >+ public static Copyright getCopyright(IInstallableUnit iu, Locale locale) { >+ Copyright copyright = iu.getCopyright(); >+ String body = (copyright != null ? copyright.getBody() : null); >+ if (body == null || body.length() <= 1 || body.charAt(0) != '%') >+ return copyright; >+ final String actualKey = body.substring(1); // Strip off the % >+ body = getLocalizedIUProperty(iu, actualKey, locale); >+ URL url = copyright.getURL(); >+ return new Copyright((url != null ? url.toExternalForm() : null), body); >+ } >+ > public static String getIUProperty(IInstallableUnit iu, String propertyKey, Locale locale) { > String value = iu.getProperty(propertyKey); > if (value == null || value.length() <= 1 || value.charAt(0) != '%') > return value; > // else have a localizable property > final String actualKey = value.substring(1); // Strip off the % >+ return getLocalizedIUProperty(iu, actualKey, locale); >+ } >+ >+ private static String getLocalizedIUProperty(IInstallableUnit iu, String actualKey, Locale locale) { >+ // Short circuit query if iu provides value for matching locale >+ String localizedKey = makeLocalizedKey(actualKey, locale.toString()); >+ String localizedValue = iu.getProperty(localizedKey); >+ if (localizedValue != null) >+ return localizedValue; >+ > final List locales = buildLocaleVariants(locale); > final IInstallableUnit theUnit = iu; > >@@ -89,12 +122,15 @@ > } > } > >- String defaultKey = makeLocalizedKey(actualKey, DEFAULT_LOCALE.toString()); >- String defaultValue = iu.getProperty(defaultKey); >- if (defaultValue != null) >- return defaultValue; >+ for (Iterator iter = locales.iterator(); iter.hasNext();) { >+ String nextLocale = (String) iter.next(); >+ String localeKey = makeLocalizedKey(actualKey, nextLocale); >+ String nextValue = iu.getProperty(localeKey); >+ if (nextValue != null) >+ return nextValue; >+ } > >- return value; >+ return actualKey; > } > > /** >@@ -111,7 +147,7 @@ > nl = nl.substring(0, lastSeparator); > } > // Add the default locale (most general) >- result.add(DEFAULT_LOCALE); >+ result.add(DEFAULT_LOCALE.toString()); > return result; > } > >Index: src/org/eclipse/equinox/internal/provisional/p2/ui/dialogs/IULicensePropertyPage.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.equinox/p2/bundles/org.eclipse.equinox.p2.ui/src/org/eclipse/equinox/internal/provisional/p2/ui/dialogs/IULicensePropertyPage.java,v >retrieving revision 1.5 >diff -u -r1.5 IULicensePropertyPage.java >--- src/org/eclipse/equinox/internal/provisional/p2/ui/dialogs/IULicensePropertyPage.java 29 Feb 2008 00:47:38 -0000 1.5 >+++ src/org/eclipse/equinox/internal/provisional/p2/ui/dialogs/IULicensePropertyPage.java 8 May 2008 05:46:42 -0000 >@@ -10,10 +10,12 @@ > *******************************************************************************/ > package org.eclipse.equinox.internal.provisional.p2.ui.dialogs; > >+import java.util.Locale; > import org.eclipse.equinox.internal.p2.ui.ProvUIMessages; > import org.eclipse.equinox.internal.p2.ui.dialogs.IUPropertyPage; > import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit; > import org.eclipse.equinox.internal.provisional.p2.metadata.License; >+import org.eclipse.equinox.internal.provisional.p2.ui.query.IUPropertyUtils; > import org.eclipse.osgi.util.NLS; > import org.eclipse.swt.SWT; > import org.eclipse.swt.events.SelectionAdapter; >@@ -30,8 +32,8 @@ > public class IULicensePropertyPage extends IUPropertyPage { > > protected Control createIUPage(Composite parent, IInstallableUnit iu) { >- >- final License license = iu.getLicense(); >+ Locale currentLocale = Locale.getDefault(); >+ final License license = IUPropertyUtils.getLicense(iu, currentLocale); > if (license != null && license.getBody().length() > 0) { > Composite composite = new Composite(parent, SWT.NONE); > GridLayout layout = new GridLayout(); >Index: src/org/eclipse/equinox/internal/provisional/p2/ui/dialogs/IUCopyrightPropertyPage.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.equinox/p2/bundles/org.eclipse.equinox.p2.ui/src/org/eclipse/equinox/internal/provisional/p2/ui/dialogs/IUCopyrightPropertyPage.java,v >retrieving revision 1.4 >diff -u -r1.4 IUCopyrightPropertyPage.java >--- src/org/eclipse/equinox/internal/provisional/p2/ui/dialogs/IUCopyrightPropertyPage.java 14 Feb 2008 08:30:37 -0000 1.4 >+++ src/org/eclipse/equinox/internal/provisional/p2/ui/dialogs/IUCopyrightPropertyPage.java 8 May 2008 05:46:42 -0000 >@@ -10,10 +10,12 @@ > *******************************************************************************/ > package org.eclipse.equinox.internal.provisional.p2.ui.dialogs; > >+import java.util.Locale; > import org.eclipse.equinox.internal.p2.ui.ProvUIMessages; > import org.eclipse.equinox.internal.p2.ui.dialogs.IUPropertyPage; > import org.eclipse.equinox.internal.provisional.p2.metadata.Copyright; > import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit; >+import org.eclipse.equinox.internal.provisional.p2.ui.query.IUPropertyUtils; > import org.eclipse.osgi.util.NLS; > import org.eclipse.swt.SWT; > import org.eclipse.swt.events.SelectionAdapter; >@@ -30,7 +32,8 @@ > public class IUCopyrightPropertyPage extends IUPropertyPage { > > protected Control createIUPage(Composite parent, IInstallableUnit iu) { >- final Copyright copyright = iu.getCopyright(); >+ Locale currentLocale = Locale.getDefault(); >+ final Copyright copyright = IUPropertyUtils.getCopyright(iu, currentLocale); > if (copyright != null && copyright.getBody().length() > 0) { > Composite composite = new Composite(parent, SWT.NONE); > GridLayout layout = new GridLayout(); >#P org.eclipse.equinox.p2.metadata.generator >Index: src/org/eclipse/equinox/internal/provisional/p2/metadata/generator/MetadataGeneratorHelper.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.equinox/p2/bundles/org.eclipse.equinox.p2.metadata.generator/src/org/eclipse/equinox/internal/provisional/p2/metadata/generator/MetadataGeneratorHelper.java,v >retrieving revision 1.47 >diff -u -r1.47 MetadataGeneratorHelper.java >--- src/org/eclipse/equinox/internal/provisional/p2/metadata/generator/MetadataGeneratorHelper.java 2 May 2008 20:05:06 -0000 1.47 >+++ src/org/eclipse/equinox/internal/provisional/p2/metadata/generator/MetadataGeneratorHelper.java 8 May 2008 05:46:44 -0000 >@@ -12,8 +12,6 @@ > package org.eclipse.equinox.internal.provisional.p2.metadata.generator; > > import java.io.*; >-import java.net.URL; >-import java.net.URLConnection; > import java.util.*; > import java.util.zip.ZipEntry; > import java.util.zip.ZipFile; >@@ -22,6 +20,7 @@ > import org.eclipse.equinox.internal.p2.metadata.ArtifactKey; > import org.eclipse.equinox.internal.p2.metadata.InstallableUnit; > import org.eclipse.equinox.internal.p2.metadata.generator.Activator; >+import org.eclipse.equinox.internal.p2.metadata.generator.LocalizationHelper; > import org.eclipse.equinox.internal.p2.metadata.generator.features.SiteCategory; > import org.eclipse.equinox.internal.provisional.frameworkadmin.BundleInfo; > import org.eclipse.equinox.internal.provisional.p2.artifact.repository.ArtifactDescriptor; >@@ -112,7 +111,6 @@ > > static final String DEFAULT_BUNDLE_LOCALIZATION = "plugin"; //$NON-NLS-1$ > static final String PROPERTIES_FILE_EXTENSION = ".properties"; //$NON-NLS-1$ >- static final String MANIFEST_LOCALIZATIONS = "eclipse.p2.manifest.localizations"; //$NON-NLS-1$ > > static final String BUNDLE_ADVICE_FILE = "META-INF/p2.inf"; //$NON-NLS-1$ > static final String ADVICE_INSTRUCTIONS_PREFIX = "instructions."; //$NON-NLS-1$ >@@ -643,7 +641,26 @@ > // TODO: shouldn't the filter for the group be constructed from os, ws, arch, nl > // of the feature? > // iu.setFilter(filter); >- iu.setCapabilities(new ProvidedCapability[] {createSelfCapability(id, version)}); >+ >+ // Create set of provided capabilities >+ ArrayList providedCapabilities = new ArrayList(); >+ providedCapabilities.add(createSelfCapability(id, version)); >+ >+ Map manifestLocalizations = feature.getLocalizations(); >+ if (manifestLocalizations != null) { >+ for (Iterator iter = manifestLocalizations.keySet().iterator(); iter.hasNext();) { >+ Locale locale = (Locale) iter.next(); >+ Properties translatedStrings = (Properties) manifestLocalizations.get(locale); >+ Enumeration propertyKeys = translatedStrings.propertyNames(); >+ while (propertyKeys.hasMoreElements()) { >+ String nextKey = (String) propertyKeys.nextElement(); >+ iu.setProperty(locale.toString() + '.' + nextKey, translatedStrings.getProperty(nextKey)); >+ } >+ providedCapabilities.add(makeTranslationCapability(id, locale)); >+ } >+ } >+ >+ iu.setCapabilities((ProvidedCapability[]) providedCapabilities.toArray(new ProvidedCapability[providedCapabilities.size()])); > > if (extraProperties != null) { > Enumeration e = extraProperties.propertyNames(); >@@ -663,10 +680,6 @@ > Version version = new Version(feature.getVersion()); > iu.setVersion(version); > iu.setProperty(IInstallableUnit.PROP_NAME, feature.getLabel()); >- if (feature.getDescription() != null) >- iu.setProperty(IInstallableUnit.PROP_DESCRIPTION, feature.getDescription()); >- if (feature.getDescriptionURL() != null) >- iu.setProperty(IInstallableUnit.PROP_DESCRIPTION_URL, feature.getDescriptionURL()); > if (feature.getLicense() != null) > iu.setLicense(new License(feature.getLicenseURL(), feature.getLicense())); > if (feature.getCopyright() != null) >@@ -1098,9 +1111,11 @@ > > if ("jar".equalsIgnoreCase(new Path(bundleLocation.getName()).getFileExtension()) && //$NON-NLS-1$ > bundleLocation.isFile()) { >- localizations = getJarManifestLocalization(bundleLocation, bundleLocalization, defaultLocale, bundleManifestValues); >+ localizations = LocalizationHelper.getJarPropertyLocalizations(bundleLocation, bundleLocalization, defaultLocale, bundleManifestValues); >+ //localizations = getJarManifestLocalization(bundleLocation, bundleLocalization, defaultLocale, bundleManifestValues); > } else { >- localizations = getDirManifestLocalization(bundleLocation, bundleLocalization, defaultLocale, bundleManifestValues); >+ localizations = LocalizationHelper.getDirPropertyLocalizations(bundleLocation, bundleLocalization, defaultLocale, bundleManifestValues); >+ // localizations = getDirManifestLocalization(bundleLocation, bundleLocalization, defaultLocale, bundleManifestValues); > } > > return localizations; >@@ -1129,165 +1144,14 @@ > > if ("jar".equalsIgnoreCase(new Path(bundleLocation.getName()).getFileExtension()) && //$NON-NLS-1$ > bundleLocation.isFile()) { >- localizations = getJarManifestLocalization(bundleLocation, hostBundleLocalization, defaultLocale, hostBundleManifestValues); >+ localizations = LocalizationHelper.getJarPropertyLocalizations(bundleLocation, hostBundleLocalization, defaultLocale, hostBundleManifestValues); >+ //localizations = getJarManifestLocalization(bundleLocation, hostBundleLocalization, defaultLocale, hostBundleManifestValues); > } else { >- localizations = getDirManifestLocalization(bundleLocation, hostBundleLocalization, defaultLocale, hostBundleManifestValues); >- } >- >- return localizations; >- } >- >- private static Map getJarManifestLocalization(File bundleLocation, String bundleLocalization, Locale defaultLocale, String[] bundleManifestValues) { >- ZipFile jarFile = null; >- Map localizations = new HashMap(4); >- try { >- jarFile = new ZipFile(bundleLocation, ZipFile.OPEN_READ); >- for (Enumeration entries = jarFile.entries(); entries.hasMoreElements();) { >- ZipEntry nextEntry = (ZipEntry) entries.nextElement(); >- String nextName = nextEntry.getName(); >- String localeString = getLocaleString(nextName, bundleLocalization); >- >- if (!nextEntry.isDirectory() && localeString != null) { >- Locale nextLocale = getLocale(localeString); >- InputStream stream = null; >- try { >- stream = jarFile.getInputStream(nextEntry); >- Properties properties = new Properties(); >- properties.load(stream); >- Properties localizedStrings = getLocalizedProperties(bundleManifestValues, properties); >- if (localizedStrings.size() > 0) { >- localizations.put(nextLocale, localizedStrings); >- if (DEFAULT_LOCALE.equals(nextLocale) && defaultLocale != null) { >- localizations.put(nextLocale, localizedStrings); >- } >- } >- } finally { >- if (stream != null) >- stream.close(); >- } >- } >- } >- } catch (IOException ioe) { >- ioe.printStackTrace(); >- } finally { >- if (jarFile != null) { >- try { >- jarFile.close(); >- } catch (IOException ioe) { >- // do nothing >- } >- } >+ localizations = LocalizationHelper.getDirPropertyLocalizations(bundleLocation, hostBundleLocalization, defaultLocale, hostBundleManifestValues); >+ // localizations = getDirManifestLocalization(bundleLocation, hostBundleLocalization, defaultLocale, hostBundleManifestValues); > } > > return localizations; > } > >- private static Map getDirManifestLocalization(File bundleLocation, String bundleLocalization, Locale defaultLocale, String[] hostBundleManifestValues) { >- File localizationPath = new File(bundleLocation, bundleLocalization); >- File localizationDir = localizationPath.getParentFile(); >- final String localizationFile = localizationPath.getName(); >- MetadataGeneratorHelper foo = new MetadataGeneratorHelper(); >- String[] localizationFiles = localizationDir.list(foo.new LocalizationFileFilter() { >- public boolean accept(File directory, String filename) { >- return (getLocaleString(filename, localizationFile) != null ? true : false); >- } >- }); >- >- HashMap localizations = null; >- >- if (localizationFiles != null) { >- localizations = new HashMap(localizationFiles.length); >- for (int i = 0; i < localizationFiles.length; i++) { >- String nextFile = localizationFiles[i]; >- Locale nextLocale = getLocale(getLocaleString(nextFile, localizationFile)); >- >- try { >- Properties properties = loadProperties(bundleLocation, nextFile); >- Properties localizedStrings = getLocalizedProperties(hostBundleManifestValues, properties); >- if (localizedStrings.size() > 0) { >- localizations.put(nextLocale, localizedStrings); >- if (DEFAULT_LOCALE.equals(nextLocale) && defaultLocale != null) { >- localizations.put(nextLocale, localizedStrings); >- } >- } >- } catch (IOException ioe) { >- ioe.printStackTrace(); >- } >- } >- } >- >- return localizations; >- } >- >- private abstract class LocalizationFileFilter implements FilenameFilter { >- >- public LocalizationFileFilter() { >- // Nothing to do >- } >- >- /* (non-Javadoc) >- * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String) >- */ >- public abstract boolean accept(File directory, String filename); >- } >- >- static public String getLocaleString(String filename, String filenamePrefix) { >- String localeString = null; >- if (filename.startsWith(filenamePrefix) && filename.endsWith(PROPERTIES_FILE_EXTENSION)) { >- if (filename.length() > filenamePrefix.length() + PROPERTIES_FILE_EXTENSION.length()) { >- localeString = filename.substring(filenamePrefix.length() + 1, filename.length() - PROPERTIES_FILE_EXTENSION.length()); >- } else { >- localeString = ""; //$NON-NLS-1$ >- } >- } >- return localeString; >- } >- >- private static Properties loadProperties(File bundleLocation, String localizationFile) throws IOException { >- Properties result = new Properties(); >- InputStream propertyStream = null; >- try { >- try { >- if (bundleLocation.isDirectory()) >- propertyStream = new FileInputStream(new File(bundleLocation, localizationFile)); >- else { >- URLConnection connection = new URL("jar:" + bundleLocation.toURL().toExternalForm() + "!/" + localizationFile).openConnection(); //$NON-NLS-1$ //$NON-NLS-2$ >- connection.setUseCaches(false); >- propertyStream = connection.getInputStream(); >- } >- } catch (FileNotFoundException e) { >- // if there is no messages file then just return; >- return result; >- } >- result.load(propertyStream); >- } finally { >- if (propertyStream != null) >- propertyStream.close(); >- } >- return result; >- } >- >- static private Locale getLocale(String localeString) { >- Locale locale = DEFAULT_LOCALE; >- if (localeString.length() == 5 && localeString.indexOf('_') == 2) { >- locale = new Locale(localeString.substring(0, 2), localeString.substring(3, 5)); >- } else if (localeString.length() == 2) { >- locale = new Locale(localeString.substring(0, 2)); >- } >- return locale; >- } >- >- static private Properties getLocalizedProperties(String[] bundleManifestKeys, Properties properties) { >- Properties localizedProperties = new Properties(); >- for (int i = 0; i < BUNDLE_LOCALIZED_PROPERTIES.length; i++) { >- String key = bundleManifestKeys[i]; >- if (key != null) { >- String localizedValue = properties.getProperty(key); >- if (localizedValue != null) >- localizedProperties.put(key, localizedValue); >- } >- } >- return localizedProperties; >- } >- > } >Index: src/org/eclipse/equinox/internal/provisional/p2/metadata/generator/Feature.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.equinox/p2/bundles/org.eclipse.equinox.p2.metadata.generator/src/org/eclipse/equinox/internal/provisional/p2/metadata/generator/Feature.java,v >retrieving revision 1.2 >diff -u -r1.2 Feature.java >--- src/org/eclipse/equinox/internal/provisional/p2/metadata/generator/Feature.java 11 Feb 2008 05:46:43 -0000 1.2 >+++ src/org/eclipse/equinox/internal/provisional/p2/metadata/generator/Feature.java 8 May 2008 05:46:44 -0000 >@@ -11,6 +11,7 @@ > package org.eclipse.equinox.internal.provisional.p2.metadata.generator; > > import java.util.ArrayList; >+import java.util.Map; > > /** > * >@@ -43,6 +44,8 @@ > > private String location; > >+ private Map localizations; >+ > public Feature(String id, String version) { > if (id == null) > throw new IllegalArgumentException(); >@@ -50,14 +53,14 @@ > this.version = version; > } > >- public void addDiscoverySite(String label, String url) { >- if (label == null && url == null) >+ public void addDiscoverySite(String siteLabel, String url) { >+ if (siteLabel == null && url == null) > return; > > if (this.discoverySites == null) > this.discoverySites = new ArrayList(); > >- URLEntry entry = new URLEntry(url, label); >+ URLEntry entry = new URLEntry(url, siteLabel); > this.discoverySites.add(entry); > } > >@@ -143,6 +146,10 @@ > return null; > } > >+ public Map getLocalizations() { >+ return this.localizations; >+ } >+ > public String getLocation() { > return this.location; > } >@@ -242,6 +249,10 @@ > this.license.setURL(licenseURL); > } > >+ public void setLocalizations(Map localizations) { >+ this.localizations = localizations; >+ } >+ > public void setLocation(String location) { > this.location = location; > } >@@ -263,6 +274,7 @@ > } > > public void setURL(String value) { >+ // > } > > public void setVersion(String version) { >Index: src/org/eclipse/equinox/internal/p2/metadata/generator/features/FeatureParser.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.equinox/p2/bundles/org.eclipse.equinox.p2.metadata.generator/src/org/eclipse/equinox/internal/p2/metadata/generator/features/FeatureParser.java,v >retrieving revision 1.19 >diff -u -r1.19 FeatureParser.java >--- src/org/eclipse/equinox/internal/p2/metadata/generator/features/FeatureParser.java 30 Apr 2008 03:00:41 -0000 1.19 >+++ src/org/eclipse/equinox/internal/p2/metadata/generator/features/FeatureParser.java 8 May 2008 05:46:44 -0000 >@@ -12,7 +12,7 @@ > > import java.io.*; > import java.net.URL; >-import java.util.Properties; >+import java.util.*; > import java.util.jar.JarEntry; > import java.util.jar.JarFile; > import javax.xml.parsers.*; >@@ -20,6 +20,7 @@ > import org.eclipse.core.runtime.Status; > import org.eclipse.equinox.internal.p2.core.helpers.LogHelper; > import org.eclipse.equinox.internal.p2.metadata.generator.Activator; >+import org.eclipse.equinox.internal.p2.metadata.generator.LocalizationHelper; > import org.eclipse.equinox.internal.provisional.p2.metadata.generator.Feature; > import org.eclipse.equinox.internal.provisional.p2.metadata.generator.FeatureEntry; > import org.eclipse.osgi.util.NLS; >@@ -41,6 +42,7 @@ > private StringBuffer characters = null; > > private Properties messages = null; >+ private List messageKeys = null; > > public FeatureParser() { > this(true); >@@ -87,17 +89,15 @@ > return result; > } > >- private Properties loadProperties(File directory) { >+ private void loadProperties(File directory, Properties properties) { > //skip directories that don't contain a feature.properties file > File file = new File(directory, "feature.properties"); //$NON-NLS-1$ > if (!file.exists()) >- return null; >+ return; > try { > InputStream input = new BufferedInputStream(new FileInputStream(file)); > try { >- Properties result = new Properties(); >- result.load(input); >- return result; >+ properties.load(input); > } finally { > if (input != null) > input.close(); >@@ -105,19 +105,16 @@ > } catch (IOException e) { > e.printStackTrace(); > } >- return null; > } > >- private Properties loadProperties(JarFile jar) { >+ private void loadProperties(JarFile jar, Properties properties) { > JarEntry entry = jar.getJarEntry("feature.properties"); //$NON-NLS-1$ > if (entry == null) >- return null; >+ return; > try { > InputStream input = new BufferedInputStream(jar.getInputStream(entry)); > try { >- Properties result = new Properties(); >- result.load(input); >- return result; >+ properties.load(input); > } finally { > if (input != null) > input.close(); >@@ -125,7 +122,6 @@ > } catch (IOException e) { > e.printStackTrace(); > } >- return null; > } > > private String localize(String value) { >@@ -133,7 +129,10 @@ > return value; > if (!value.startsWith("%")) //$NON-NLS-1$ > return value; >- return messages.getProperty(value.substring(1), value); >+ String key = value.substring(1); >+ messageKeys.add(key); >+ return value; >+ //return messages.getProperty(value.substring(1), value); > } > > /** >@@ -146,15 +145,23 @@ > public Feature parse(File location) { > if (!location.exists()) > return null; >+ >+ Feature feature = null; >+ Properties properties = new Properties(); >+ > if (location.isDirectory()) { > //skip directories that don't contain a feature.xml file > File file = new File(location, "feature.xml"); //$NON-NLS-1$ > if (!file.exists()) > return null; >- Properties properties = loadProperties(location); >+ loadProperties(location, properties); > try { > InputStream input = new BufferedInputStream(new FileInputStream(file)); >- return parse(input, properties); >+ feature = parse(input, properties); >+ if (feature != null) { >+ String[] keyStrings = (String[]) messageKeys.toArray(new String[messageKeys.size()]); >+ feature.setLocalizations(LocalizationHelper.getDirPropertyLocalizations(location, "feature", null, keyStrings)); //$NON-NLS-1$ >+ } > } catch (FileNotFoundException e) { > e.printStackTrace(); > } >@@ -162,12 +169,16 @@ > JarFile jar = null; > try { > jar = new JarFile(location); >- Properties properties = loadProperties(jar); >+ loadProperties(jar, properties); > JarEntry entry = jar.getJarEntry("feature.xml"); //$NON-NLS-1$ > if (entry == null) > return null; > InputStream input = new BufferedInputStream(jar.getInputStream(entry)); >- return parse(input, properties); >+ feature = parse(input, properties); >+ if (feature != null) { >+ String[] keyStrings = (String[]) messageKeys.toArray(new String[messageKeys.size()]); >+ feature.setLocalizations(LocalizationHelper.getDirPropertyLocalizations(location, "feature", null, keyStrings)); //$NON-NLS-1$ >+ } > } catch (IOException e) { > e.printStackTrace(); > } catch (SecurityException e) { >@@ -181,7 +192,7 @@ > } > } > } >- return null; >+ return feature; > } > > /** >@@ -190,6 +201,7 @@ > */ > public Feature parse(InputStream in, Properties messages) { > this.messages = messages; >+ this.messageKeys = new ArrayList(messages.size()); > result = null; > try { > parser.parse(new InputSource(in), this); >Index: src/org/eclipse/equinox/internal/p2/metadata/generator/LocalizationHelper.java >=================================================================== >RCS file: src/org/eclipse/equinox/internal/p2/metadata/generator/LocalizationHelper.java >diff -N src/org/eclipse/equinox/internal/p2/metadata/generator/LocalizationHelper.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/equinox/internal/p2/metadata/generator/LocalizationHelper.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,200 @@ >+/******************************************************************************* >+ * Copyright (c) 2008 IBM Corporation and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * IBM Corporation - initial API and implementation >+ *******************************************************************************/ >+package org.eclipse.equinox.internal.p2.metadata.generator; >+ >+import java.io.*; >+import java.net.URL; >+import java.net.URLConnection; >+import java.util.*; >+import java.util.zip.ZipEntry; >+import java.util.zip.ZipFile; >+ >+/** >+ * Helper functions supporting the processing of localized >+ * property files. >+ * >+ */ >+public final class LocalizationHelper { >+ >+ private static final String PROPERTIES_FILE_EXTENSION = ".properties"; //$NON-NLS-1$ >+ private static final Locale DEFAULT_LOCALE = new Locale("df", "LT"); //$NON-NLS-1$//$NON-NLS-2$ >+ private static LocalizationHelper instance = new LocalizationHelper(); >+ >+ // Extract the locale string from the properties file with the given filename >+ // where the locale string follows the given prefix. For example, return "zh_HK" >+ // from filename == "plugin_zh_HK.properties" and prefix == "plugin". >+ static public String getLocaleString(String filename, String prefix) { >+ String localeString = null; >+ if (filename.startsWith(prefix) && filename.endsWith(PROPERTIES_FILE_EXTENSION)) { >+ if (filename.length() > prefix.length() + PROPERTIES_FILE_EXTENSION.length()) { >+ localeString = filename.substring(prefix.length() + 1, filename.length() - PROPERTIES_FILE_EXTENSION.length()); >+ } else { >+ localeString = ""; //$NON-NLS-1$ >+ } >+ } >+ return localeString; >+ } >+ >+ // Get the locale corresponding to the given locale string >+ static public Locale getLocale(String localeString) { >+ Locale locale = DEFAULT_LOCALE; >+ if (localeString.length() == 5 && localeString.indexOf('_') == 2) { >+ locale = new Locale(localeString.substring(0, 2), localeString.substring(3, 5)); >+ } else if (localeString.length() == 2) { >+ locale = new Locale(localeString.substring(0, 2)); >+ } >+ return locale; >+ } >+ >+ // For the given root directory and path to localization files within that directory >+ // get a map from locale to property set for the localization property files. >+ public static Map getDirPropertyLocalizations(File root, String localizationPath, Locale defaultLocale, String[] propertyKeys) { >+ File fullPath = new File(root, localizationPath); >+ File localizationDir = fullPath.getParentFile(); >+ final String localizationFile = fullPath.getName(); >+ String[] localizationFiles = LocalizationHelper.getLocalizationFiles(localizationDir, localizationFile); >+ >+ HashMap localizations = null; >+ >+ if (localizationFiles != null) { >+ localizations = new HashMap(localizationFiles.length); >+ for (int i = 0; i < localizationFiles.length; i++) { >+ String nextFile = localizationFiles[i]; >+ Locale nextLocale = getLocale(LocalizationHelper.getLocaleString(nextFile, localizationFile)); >+ >+ try { >+ Properties properties = loadProperties(root, nextFile); >+ Properties localizedStrings = getLocalizedProperties(propertyKeys, properties); >+ if (localizedStrings.size() > 0) { >+ localizations.put(nextLocale, localizedStrings); >+ if (DEFAULT_LOCALE.equals(nextLocale) && defaultLocale != null) { >+ localizations.put(nextLocale, localizedStrings); >+ } >+ } >+ } catch (IOException ioe) { >+ ioe.printStackTrace(); >+ } >+ } >+ } >+ >+ return localizations; >+ } >+ >+ public static Map getJarPropertyLocalizations(File root, String localizationPath, Locale defaultLocale, String[] propertyKeys) { >+ ZipFile jarFile = null; >+ Map localizations = new HashMap(4); >+ try { >+ jarFile = new ZipFile(root, ZipFile.OPEN_READ); >+ for (Enumeration entries = jarFile.entries(); entries.hasMoreElements();) { >+ ZipEntry nextEntry = (ZipEntry) entries.nextElement(); >+ String nextName = nextEntry.getName(); >+ String localeString = LocalizationHelper.getLocaleString(nextName, localizationPath); >+ >+ if (!nextEntry.isDirectory() && localeString != null) { >+ Locale nextLocale = LocalizationHelper.getLocale(localeString); >+ InputStream stream = null; >+ try { >+ stream = jarFile.getInputStream(nextEntry); >+ Properties properties = new Properties(); >+ properties.load(stream); >+ Properties localizedStrings = LocalizationHelper.getLocalizedProperties(propertyKeys, properties); >+ if (localizedStrings.size() > 0) { >+ localizations.put(nextLocale, localizedStrings); >+ if (DEFAULT_LOCALE.equals(nextLocale) && defaultLocale != null) { >+ localizations.put(nextLocale, localizedStrings); >+ } >+ } >+ } finally { >+ if (stream != null) >+ stream.close(); >+ } >+ } >+ } >+ } catch (IOException ioe) { >+ ioe.printStackTrace(); >+ } finally { >+ if (jarFile != null) { >+ try { >+ jarFile.close(); >+ } catch (IOException ioe) { >+ // do nothing >+ } >+ } >+ } >+ >+ return localizations; >+ } >+ >+ // Load a property set from given root and file with the given name >+ private static Properties loadProperties(File root, String propertyFilename) throws IOException { >+ Properties result = new Properties(); >+ InputStream propertyStream = null; >+ try { >+ try { >+ if (root.isDirectory()) >+ propertyStream = new FileInputStream(new File(root, propertyFilename)); >+ else { >+ URLConnection connection = new URL("jar:" + root.toURL().toExternalForm() + "!/" + propertyFilename).openConnection(); //$NON-NLS-1$ //$NON-NLS-2$ >+ connection.setUseCaches(false); >+ propertyStream = connection.getInputStream(); >+ } >+ } catch (FileNotFoundException e) { >+ // if there is no messages file then just return; >+ return result; >+ } >+ result.load(propertyStream); >+ } finally { >+ if (propertyStream != null) >+ propertyStream.close(); >+ } >+ return result; >+ } >+ >+ // Given a list of keys and the corresponding localized property set, >+ // return a new property set with those keys and the localized values. >+ static public Properties getLocalizedProperties(String[] propertyKeys, Properties properties) { >+ Properties localizedProperties = new Properties(); >+ for (int i = 0; i < propertyKeys.length; i++) { >+ String key = propertyKeys[i]; >+ if (key != null) { >+ String localizedValue = properties.getProperty(key); >+ if (localizedValue != null) >+ localizedProperties.put(key, localizedValue); >+ } >+ } >+ return localizedProperties; >+ } >+ >+ public static String[] getLocalizationFiles(File localizationDir, final String filenamePrefix) { >+ return localizationDir.list(instance.new FileFilter() { >+ public boolean accept(File directory, String filename) { >+ return (getLocaleString(filename, filenamePrefix) != null ? true : false); >+ } >+ }); >+ } >+ >+ private abstract class FileFilter implements FilenameFilter { >+ >+ public FileFilter() { >+ // Nothing to do >+ } >+ >+ /* (non-Javadoc) >+ * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String) >+ */ >+ public abstract boolean accept(File directory, String filename); >+ } >+ >+ private LocalizationHelper() { >+ // >+ } >+ >+}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 222309
:
93829
|
94301
|
99221
|
99406
|
100028
|
100281