|
7 | 7 | ******************************************************************************/ |
8 | 8 | package org.phoebus.framework.nls; |
9 | 9 |
|
10 | | -import java.io.File; |
11 | | -import java.io.FileInputStream; |
12 | | -import java.io.FilenameFilter; |
13 | 10 | import java.io.InputStream; |
14 | 11 | import java.lang.reflect.Field; |
15 | 12 | import java.lang.reflect.Modifier; |
16 | | -import java.net.URL; |
17 | | -import java.util.ArrayList; |
18 | | -import java.util.Enumeration; |
19 | 13 | import java.util.HashMap; |
20 | | -import java.util.List; |
21 | 14 | import java.util.Locale; |
22 | 15 | import java.util.Map; |
23 | | -import java.util.MissingResourceException; |
24 | 16 | import java.util.Properties; |
25 | 17 | import java.util.ResourceBundle; |
| 18 | +import java.util.MissingResourceException; |
26 | 19 | import java.util.logging.Level; |
27 | 20 | import java.util.logging.Logger; |
28 | 21 |
|
|
64 | 57 | @SuppressWarnings("nls") |
65 | 58 | public class NLS |
66 | 59 | { |
67 | | - public static final String MESSAGE = "messages"; |
68 | 60 | // Logger is unlikely to be called, so only create when needed |
69 | 61 | private static Logger getLogger() |
70 | 62 | { |
@@ -149,175 +141,4 @@ public static ResourceBundle getMessages(Class<?> clazz) |
149 | 141 |
|
150 | 142 | return bundle; |
151 | 143 | } |
152 | | - |
153 | | - |
154 | | - public static List<String> checkMessageFilesDifferences(Class<?> clazz) { |
155 | | - URL resource = clazz.getResource(MESSAGE + ".properties"); |
156 | | - return checkMessageFilesDifferences(resource.getFile()); |
157 | | - } |
158 | | - |
159 | | - /** |
160 | | - * Use for unit test only |
161 | | - * Check if all the existing messages_{LOCALE}.properties are synchronized on default messages.propertiesresource in the project |
162 | | - * |
163 | | - * @return the list of difference between the default resources , null or empty if it is synchronized |
164 | | - */ |
165 | | - public static List<String> checkAllMessageFilesDifferences(){ |
166 | | - List<String> differences = new ArrayList<>(); |
167 | | - URL resource = NLS.class.getResource("CheckMessagesFiles.txt"); |
168 | | - if (resource != null) { |
169 | | - String filePath = resource.getFile(); |
170 | | - System.out.println(filePath); |
171 | | - String[] split = filePath.split("/core/framework/"); |
172 | | - //First part is the parent folder |
173 | | - String parentFolder = split != null && split.length > 0 ? split[0] : null; |
174 | | - System.out.println("parentFolder=" + parentFolder); |
175 | | - File parentFile = new File(parentFolder); |
176 | | - List<File> fileList = listMessagesFiles(parentFile); |
177 | | - |
178 | | - for (File file : fileList) { |
179 | | - List<String> diff = NLS.checkMessageFilesDifferences(file.getAbsolutePath()); |
180 | | - if (diff != null && !diff.isEmpty()) { |
181 | | - differences.addAll(diff); |
182 | | - } |
183 | | - } |
184 | | - |
185 | | - if(differences.isEmpty()) { |
186 | | - System.out.println("All the "+ MESSAGE+ "_{LOCALE}.properties files are syncronized "); |
187 | | - } |
188 | | - else { |
189 | | - System.out.println("**There is " + differences.size() + " difference(s) found**"); |
190 | | - for (String dif : differences) { |
191 | | - System.out.println(dif); |
192 | | - } |
193 | | - } |
194 | | - } |
195 | | - return differences; |
196 | | - } |
197 | | - |
198 | | - /** |
199 | | - * Use for unit test only |
200 | | - * Check if the existing messages_{LOCALE}.properties are synchronized on default messages.propertiesresource |
201 | | - * |
202 | | - * @param clazz Class relative to which message resources are located |
203 | | - * @return the list of difference between the default ressources , null or empty if it is synchronized |
204 | | - */ |
205 | | - private static List<String> checkMessageFilesDifferences(String resourceFile) { |
206 | | - List<String> differences = new ArrayList<>(); |
207 | | - if (resourceFile != null) { |
208 | | - try { |
209 | | - File defaultFile = new File(resourceFile); |
210 | | - Properties defaultBundle = new Properties(); |
211 | | - defaultBundle.load(new FileInputStream(defaultFile)); |
212 | | - File parent = defaultFile.getParentFile(); |
213 | | - FilenameFilter fileNameFilter = new FilenameFilter() { |
214 | | - |
215 | | - @Override |
216 | | - public boolean accept(File dir, String name) { |
217 | | - return name.startsWith(MESSAGE) && name.endsWith(".properties"); |
218 | | - } |
219 | | - }; |
220 | | - |
221 | | - File[] listFiles = parent.listFiles(fileNameFilter); |
222 | | - if (listFiles != null && listFiles.length > 0) { |
223 | | - // System.out.println("Number of languages found =" + listFiles.length); |
224 | | - String fileName = null; |
225 | | - String countryCode = null; |
226 | | - String countryName = null; |
227 | | - Properties compareBundle = null; |
228 | | - Object key = null; |
229 | | - String value = null; |
230 | | - Locale locale = null; |
231 | | - Enumeration<Object> compareKeys = null; |
232 | | - Enumeration<Object> defaultKeys = null; |
233 | | - for (File tmpFile : listFiles) { |
234 | | - fileName = tmpFile.getName(); |
235 | | - // Do not compare to itself |
236 | | - if (!fileName.equalsIgnoreCase(defaultFile.getName())) { |
237 | | - // Extract the country code |
238 | | - countryCode = fileName.replaceFirst(MESSAGE + "_", ""); |
239 | | - countryCode = countryCode.replace(".properties", ""); |
240 | | - locale = getLocaleFromCountryCode(countryCode); |
241 | | - if (locale != null) { |
242 | | - countryName = locale.getDisplayCountry(); |
243 | | - compareBundle = new Properties(); |
244 | | - compareBundle.load(new FileInputStream(tmpFile)); |
245 | | - // Check if the key exist in the LOCAL file |
246 | | - // System.out.println("Compare " + tmpFile.getName() + " to " + defaultFile.getName()); |
247 | | - defaultKeys = defaultBundle.keys(); |
248 | | - while (defaultKeys.hasMoreElements()) { |
249 | | - key = defaultKeys.nextElement(); |
250 | | - if (!compareBundle.containsKey(key)) { |
251 | | - // Ignore env variables eg ${revision} |
252 | | - value = String.valueOf(defaultBundle.get(key)); |
253 | | - if (!(value.startsWith("${") && value.endsWith("}"))) { |
254 | | - differences.add("Missing " + key + " in " + countryName + " resource " |
255 | | - + tmpFile.getAbsolutePath()); |
256 | | - } |
257 | | - } |
258 | | - } |
259 | | - |
260 | | - // Check if there are some key to remove in LOCAL file |
261 | | - compareKeys = compareBundle.keys(); |
262 | | - while (compareKeys.hasMoreElements()) { |
263 | | - key = compareKeys.nextElement(); |
264 | | - if (!defaultBundle.containsKey(key)) { |
265 | | - differences.add("Remove " + key + " in " + countryName + " resource " |
266 | | - + tmpFile.getAbsolutePath()); |
267 | | - } |
268 | | - } |
269 | | - } |
270 | | - } |
271 | | - } |
272 | | - } |
273 | | - |
274 | | - } catch (Exception e) { |
275 | | - e.printStackTrace(); |
276 | | - } |
277 | | - } |
278 | | - return differences; |
279 | | - } |
280 | | - |
281 | | - private static List<File> listMessagesFiles(File folder) { |
282 | | - String filename = MESSAGE + ".properties"; |
283 | | - List<File> fileList = new ArrayList<>(); |
284 | | - //Ignore target folder from build |
285 | | - if(folder != null && folder.isDirectory() |
286 | | - && !folder.getAbsolutePath().contains("\\target\\") |
287 | | - && !folder.getAbsolutePath().contains("\\test\\")) { |
288 | | - File[] listFiles = folder.listFiles(); |
289 | | - for(File file : listFiles) { |
290 | | - if(file.isDirectory()) { |
291 | | - List<File> list = listMessagesFiles(file); |
292 | | - fileList.addAll(list); |
293 | | - } |
294 | | - else if (file.getName().equals(filename)){ |
295 | | - fileList.add(file); |
296 | | - } |
297 | | - } |
298 | | - } |
299 | | - return fileList; |
300 | | - } |
301 | | - |
302 | | - |
303 | | - /** |
304 | | - * To get Locale from a countryCode |
305 | | - * |
306 | | - * @param countryCode (fr , en ..." |
307 | | - * @return Locale |
308 | | - */ |
309 | | - private static Locale getLocaleFromCountryCode(String countryCode) { |
310 | | - Locale localFound = null; |
311 | | - if (countryCode != null && !countryCode.isEmpty()) { |
312 | | - Locale[] availableLocales = Locale.getAvailableLocales(); |
313 | | - for (Locale locale : availableLocales) { |
314 | | - if (locale.getCountry().toLowerCase().equals(countryCode.toLowerCase())) { |
315 | | - localFound = locale; |
316 | | - break; |
317 | | - } |
318 | | - } |
319 | | - } |
320 | | - return localFound; |
321 | | - } |
322 | | - |
323 | 144 | } |
0 commit comments