Debian Codegraph @

bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/spi/p2/publisher/LocalizationHelper.java

raw
1 /*******************************************************************************
2 * Copyright (c) 2008, 2021 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.equinox.spi.p2.publisher;
15
16 import java.io.*;
17 import java.net.URL;
18 import java.net.URLConnection;
19 import java.util.*;
20 import java.util.zip.ZipEntry;
21 import java.util.zip.ZipFile;
22 import org.eclipse.equinox.internal.p2.core.helpers.CollectionUtils;
23
24 /**
25 * Helper functions supporting the processing of localized property files.
26 *
27 */
28 public final class LocalizationHelper {
29
30 private static final String PROPERTIES_FILE_EXTENSION = ".properties"; //$NON-NLS-1$
31 private static final Locale DEFAULT_LOCALE = new Locale("df", "LT"); //$NON-NLS-1$//$NON-NLS-2$
32
33 // Extract the locale string from the properties file with the given filename
34 // where the locale string follows the given prefix. For example, return "zh_HK"
35 // from filename == "plugin_zh_HK.properties" and prefix == "plugin".
36 static public String getLocaleString(String filename, String prefix) {
37 String localeString = null;
38 if (filename.startsWith(prefix) && filename.endsWith(PROPERTIES_FILE_EXTENSION)) {
39 if (filename.length() > prefix.length() + PROPERTIES_FILE_EXTENSION.length()) {
40 localeString = filename.substring(prefix.length() + 1,
41 filename.length() - PROPERTIES_FILE_EXTENSION.length());
42 } else {
43 localeString = ""; //$NON-NLS-1$
44 }
45 }
46 return localeString;
47 }
48
49 // Get the locale corresponding to the given locale string
50 static public Locale getLocale(String localeString) {
51 Locale locale = DEFAULT_LOCALE;
52 if (localeString.length() == 5 && localeString.indexOf('_') == 2) {
53 locale = new Locale(localeString.substring(0, 2), localeString.substring(3, 5));
54 } else if (localeString.length() == 2) {
55 locale = new Locale(localeString.substring(0, 2));
56 }
57 return locale;
58 }
59
60 // For the given root directory and path to localization files within that
61 // directory
62 // get a map from locale to property set for the localization property files.
63 public static Map<Locale, Map<String, String>> getDirPropertyLocalizations(File root, String localizationPath,
64 Locale defaultLocale, String[] propertyKeys) {
65 File fullPath = new File(root, localizationPath);
66 File localizationDir = fullPath.getParentFile();
67 final String localizationFile = fullPath.getName();
68 String[] localizationFiles = LocalizationHelper.getLocalizationFiles(localizationDir, localizationFile);
69
70 HashMap<Locale, Map<String, String>> localizations = null;
71
72 if (localizationFiles != null && localizationFiles.length > 0) {
73 localizations = new HashMap<>(localizationFiles.length);
74 for (String nextFile : localizationFiles) {
75 Locale nextLocale = getLocale(LocalizationHelper.getLocaleString(nextFile, localizationFile));
76
77 try {
78 Map<String, String> properties = loadProperties(localizationDir, nextFile);
79 Map<String, String> localizedStrings = getLocalizedProperties(propertyKeys, properties);
80 if (localizedStrings.size() > 0) {
81 localizations.put(nextLocale, localizedStrings);
82 if (DEFAULT_LOCALE.equals(nextLocale) && defaultLocale != null) {
83 localizations.put(nextLocale, localizedStrings);
84 }
85 }
86 } catch (IOException ioe) {
87 ioe.printStackTrace();
88 }
89 }
90 }
91
92 return localizations;
93 }
94
95 public static Map<Locale, Map<String, String>> getJarPropertyLocalizations(File root, String localizationPath,
96 Locale defaultLocale, String[] propertyKeys) {
97 Map<Locale, Map<String, String>> localizations = new HashMap<>(4);
98 try (ZipFile jarFile = new ZipFile(root, ZipFile.OPEN_READ)) {
99 for (Enumeration<? extends ZipEntry> entries = jarFile.entries(); entries.hasMoreElements();) {
100 ZipEntry nextEntry = entries.nextElement();
101 String nextName = nextEntry.getName();
102 String localeString = LocalizationHelper.getLocaleString(nextName, localizationPath);
103
104 if (!nextEntry.isDirectory() && localeString != null) {
105 Locale nextLocale = LocalizationHelper.getLocale(localeString);
106 try (InputStream stream = jarFile.getInputStream(nextEntry)) {
107 Map<String, String> properties = CollectionUtils.loadProperties(stream);
108 Map<String, String> localizedStrings = LocalizationHelper.getLocalizedProperties(propertyKeys,
109 properties);
110 if (localizedStrings.size() > 0) {
111 localizations.put(nextLocale, localizedStrings);
112 if (DEFAULT_LOCALE.equals(nextLocale) && defaultLocale != null) {
113 localizations.put(nextLocale, localizedStrings);
114 }
115 }
116 }
117 }
118 }
119 } catch (IOException ioe) {
120 ioe.printStackTrace();
121 }
122
123 return localizations;
124 }
125
126 // Load a property set from given root and file with the given name
127 private static Map<String, String> loadProperties(File root, String propertyFilename) throws IOException {
128 InputStream propertyStream = null;
129 try {
130 try {
131 if (root.isDirectory())
132 propertyStream = new FileInputStream(new File(root, propertyFilename));
133 else {
134 URLConnection connection = new URL("jar:" + root.toURL().toExternalForm() + "!/" + propertyFilename) //$NON-NLS-1$ //$NON-NLS-2$
135 .openConnection();
136 connection.setUseCaches(false);
137 propertyStream = connection.getInputStream();
138 }
139 } catch (FileNotFoundException e) {
140 // if there is no messages file then just return;
141 return Collections.emptyMap();
142 }
143 return CollectionUtils.loadProperties(propertyStream);
144 } finally {
145 if (propertyStream != null)
146 propertyStream.close();
147 }
148 }
149
150 // Given a list of keys and the corresponding localized property set,
151 // return a new property set with those keys and the localized values.
152 static public Map<String, String> getLocalizedProperties(String[] propertyKeys, Map<String, String> properties) {
153 Map<String, String> localizedProperties = new HashMap<>();
154 for (String key : propertyKeys) {
155 if (key != null) {
156 String localizedValue = properties.get(key);
157 if (localizedValue != null)
158 localizedProperties.put(key, localizedValue);
159 }
160 }
161 return localizedProperties;
162 }
163
164 public static String[] getLocalizationFiles(File localizationDir, final String filenamePrefix) {
165 return localizationDir
166 .list((directory, filename) -> (getLocaleString(filename, filenamePrefix) != null ? true : false));
167 }
168
169 private LocalizationHelper() {
170 //
171 }
172
173 }