Clover Coverage Report - itracker
Coverage timestamp: Tue May 1 2012 16:42:12 CEST
211   610   115   5.86
106   443   0.55   36
36     3.19  
1    
 
 
  ITrackerResources       Line # 44 211 115 18.1% 0.18130311
 
No Tests
 
1    /*
2    * This software was designed and created by Jason Carroll.
3    * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4    * The author can be reached at jcarroll@cowsultants.com
5    * ITracker website: http://www.cowsultants.com
6    * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7    *
8    * This program is free software; you can redistribute it and/or modify
9    * it only under the terms of the GNU General Public License as published by
10    * the Free Software Foundation; either version 2 of the License, or
11    * (at your option) any later version.
12    *
13    * This program is distributed in the hope that it will be useful,
14    * but WITHOUT ANY WARRANTY; without even the implied warranty of
15    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16    * GNU General Public License for more details.
17    */
18   
19    package org.itracker.core.resources;
20   
21    import java.util.HashMap;
22    import java.util.Iterator;
23    import java.util.LinkedHashMap;
24    import java.util.List;
25    import java.util.Locale;
26    import java.util.Map;
27    import java.util.MissingResourceException;
28    import java.util.ResourceBundle;
29    import java.util.Set;
30   
31    import org.apache.log4j.Logger;
32    import org.itracker.model.Language;
33    import org.itracker.persistence.dao.NoSuchEntityException;
34    import org.itracker.services.exceptions.ITrackerDirtyResourceException;
35    import org.itracker.web.util.ServletContextUtils;
36   
37    /**
38    *
39    * Please comment this class here. What is it for?
40    *
41    * @author ready
42    *
43    */
 
44    public class ITrackerResources {
45   
46    private static final Logger logger = Logger
47    .getLogger(ITrackerResources.class);
48   
49    public static final String RESOURCE_BUNDLE_NAME = "org.itracker.core.resources.ITracker";
50   
51    public static final String DEFINED_LOCALES_KEY = "itracker.locales";
52   
53    public static final String DEFAULT_LOCALE = "en_US";
54   
55    public static final String BASE_LOCALE = "BASE";
56   
57    public static final String NO_LOCALE = "ZZ_ZZ";
58   
59    public static final String KEY_BASE_CUSTOMFIELD_TYPE = "itracker.web.generic.";
60   
61    public static final String KEY_BASE_WORKFLOW_EVENT = "itracker.workflow.field.event.";
62   
63    public static final String KEY_BASE_PROJECT_STATUS = "itracker.project.status.";
64   
65    public static final String KEY_BASE_PERMISSION = "itracker.user.permission.";
66   
67    public static final String KEY_BASE_PRIORITY = "itracker.script.priority.";
68   
69    public static final String KEY_BASE_PRIORITY_LABEL = ".label";
70   
71    public static final String KEY_BASE_PRIORITY_SIZE = "size";
72   
73    public static final String KEY_BASE_RESOLUTION = "itracker.resolution.";
74   
75    public static final String KEY_BASE_ISSUE_RELATION = "itracker.issuerelation.";
76   
77    public static final String KEY_BASE_SEVERITY = "itracker.severity.";
78   
79    public static final String KEY_BASE_STATUS = "itracker.status.";
80   
81    public static final String KEY_BASE_USER_STATUS = "itracker.user.status.";
82   
83    public static final String KEY_BASE_CUSTOMFIELD = "itracker.customfield.";
84   
85    public static final String KEY_BASE_CUSTOMFIELD_OPTION = ".option.";
86   
87    public static final String KEY_BASE_CUSTOMFIELD_LABEL = ".label";
88   
89    public static final String KEY_BASE_LOCALE_NAME = "itracker.locale.name";
90   
91    private static String defaultLocale = DEFAULT_LOCALE;
92   
93    private static HashMap<String, Locale> locales = new HashMap<String, Locale>();
94   
95    private static HashMap<Locale, ResourceBundle> languages = new HashMap<Locale, ResourceBundle>();
96   
97    private static boolean initialized = false;
98   
99    private static Object bundleLock = new Object();
100   
101    // private static ConfigurationService configurationService;
102   
 
103  0 toggle public static Locale getLocale() {
104  0 return getLocale(getDefaultLocale());
105    }
106   
 
107  28 toggle public static Locale getLocale(String localeString) {
108   
109    // if (logger.isDebugEnabled()) {
110    // logger.debug("getLocale: " + localeString);
111    // }
112  28 if (localeString == null || localeString.trim().equals("")) {
113  0 return getLocale(getDefaultLocale());
114    }
115   
116  28 Locale locale = locales.get(localeString);
117  28 if (locale == null && localeString != null
118    && !localeString.trim().equals("")) {
119  2 try {
120  2 if (logger.isDebugEnabled()) {
121  2 logger.debug("Creating new locale for '" + localeString
122    + "'");
123    }
124  2 if (localeString.length() == 5) {
125  1 locale = new Locale(localeString.substring(0, 2),
126    localeString.substring(3));
127  1 } else if (localeString.length() == 2) {
128  0 locale = new Locale(localeString, "");
129  1 } else if (localeString.equals(BASE_LOCALE)) {
130  1 locale = new Locale("", "");
131    } else {
132   
133  0 logger
134    .error("Invalid locale '"
135    + localeString
136    + "' specified. It must be either LN or LN_CN.");
137  0 throw new Exception("Invalid locale string");
138    }
139    } catch (Exception ex) {
140  0 if (!localeString.equals(getDefaultLocale())) {
141  0 logger.error("Failed creating new locale for '"
142    + localeString
143    + "' attempting for default locale '"
144    + getDefaultLocale() + "'", ex);
145  0 return getLocale(getDefaultLocale());
146    } else {
147  0 logger.error("Failed creating new default locale for '"
148    + getDefaultLocale()
149    + "' attempting for DEFAULT_LOCALE '"
150    + DEFAULT_LOCALE + "'", ex);
151  0 return getLocale(DEFAULT_LOCALE);
152    }
153    }
154  2 locales.put(localeString, locale);
155    }
156  28 return locale;
157    }
158   
 
159  55 toggle public static String getDefaultLocale() {
160  55 return (defaultLocale == null ? DEFAULT_LOCALE : defaultLocale);
161    }
162   
 
163  27 toggle public static void setDefaultLocale(String value) {
164  27 defaultLocale = value;
165    }
166   
 
167  0 toggle public static String getLocaleDN(String locale, Locale displayLocale) {
168  0 String name;
169  0 if (null == displayLocale) {
170  0 displayLocale = getLocale();
171    }
172  0 try {
173  0 name = getBundle(displayLocale).getString(
174    KEY_BASE_LOCALE_NAME + "." + locale);
175    } catch (RuntimeException e) {
176  0 name = getLocaleNativeName(getLocale(locale));
177    }
178   
179  0 return name;
180    }
181   
 
182  0 toggle public static String getLocaleDN(Locale locale, Locale displayLocale) {
183   
184  0 if (null == displayLocale) {
185  0 return getLocaleNativeName(displayLocale);
186    }
187  0 return getLocaleDN(locale.toString(), displayLocale);
188   
189    }
 
190  0 toggle public static String getLocaleFullDN(Locale locale, Locale displayLocale) {
191   
192  0 if (null == locale) {
193  0 locale = new Locale("");
194    }
195  0 String fullName = getLocaleNativeName(locale);
196  0 if (null == displayLocale || locale.getLanguage().equals(displayLocale.getLanguage())) {
197  0 return fullName;
198    }
199  0 if (fullName.equals(locale.toString())) {
200  0 fullName = getLocaleDN(locale, displayLocale);
201    } else {
202  0 String localizedName = getLocaleDN(locale, displayLocale);
203  0 if (null != fullName && null != localizedName && !localizedName.trim().equals(fullName.trim())) {
204  0 return fullName.trim() + " (" + localizedName.trim() + ")";
205  0 } else if (null != localizedName) {
206  0 return localizedName.trim();
207  0 } else if (null != fullName) {
208  0 return fullName.trim();
209    }
210   
211    }
212   
213  0 return "Unknown: " + locale.toString();
214   
215    }
 
216  0 toggle public static String getLocaleNativeName(Locale locale) {
217  0 try {
218  0 return getString(KEY_BASE_LOCALE_NAME, locale);
219    // return getBundle(locale).getString(KEY_BASE_LOCALE_NAME);
220    } catch (MissingResourceException e) {
221  0 return locale.toString();
222    // return locale.getDisplayName(locale);
223    }
224    }
225   
 
226  0 toggle public static final Map<String, String> getLocaleNamesMap(Locale locale, Set<String> languageCodes,Map<String, List<String>> languagesMap ) {
227  0 Map<String, String> ret = new LinkedHashMap<String, String>();
228  0 for (String languageCode : languageCodes) {
229  0 List<String> languagelist = languagesMap.get(languageCode);
230   
231  0 String name = getLocaleFullDN(ITrackerResources.getLocale(languageCode), locale);
232   
233  0 ret.put(languageCode, name);
234  0 for (String languageitem : languagelist) {
235  0 name = getLocaleFullDN(ITrackerResources.getLocale(languageitem), locale);
236  0 ret.put(languageitem, name);
237    }
238   
239    }
240  0 if (ret.size() == 0) {
241  0 ret.put(getDefaultLocale(), getLocaleNativeName(getLocale(getDefaultLocale())));
242    }
243  0 return ret;
244   
245    }
 
246  27 toggle public static ResourceBundle getBundle() {
247  27 return getBundle(getDefaultLocale());
248    }
249   
 
250  27 toggle public static ResourceBundle getBundle(String locale) {
251  27 if (locale == null || locale.equals("")) {
252  0 locale = getDefaultLocale();
253    }
254   
255  27 return getBundle(getLocale(locale));
256    }
257   
 
258  34 toggle public static ResourceBundle getBundle(Locale locale) {
259  34 if (locale == null) {
260  0 locale = getLocale(getDefaultLocale());
261    }
262  34 ResourceBundle bundle = (ResourceBundle) languages.get(locale);
263  34 if (bundle == null) {
264  34 if (logger.isDebugEnabled()) {
265  34 logger.debug("getBundle: Loading new resource bundle for locale " + locale
266    + " from the database.");
267    }
268  34 List<Language> languageItems = ServletContextUtils
269    .getItrackerServices().getConfigurationService()
270    .getLanguage(locale);
271    // if (locale.getLanguage().equals("")) {
272    // locale = getLocale()
273    // }
274  0 bundle = ITrackerResourceBundle.loadBundle(locale, languageItems);
275  0 if (logger.isDebugEnabled()) {
276  0 logger.debug("getBundle: got loaded for locale " + locale
277    + " with items " + languageItems + " from the database.");
278    }
279  0 if (bundle != null) {
280  0 putBundle(locale, bundle);
281  0 } else if (!locale.toString().equals(getDefaultLocale())) {
282  0 bundle = getBundle(getLocale());
283    }
284    }
285   
286  0 return bundle;
287    }
288   
 
289  0 toggle public static ResourceBundle getEditBundle(Locale locale) {
290  0 if (locale == null) {
291  0 locale = getLocale(getDefaultLocale());
292    }
293  0 ResourceBundle bundle = (ResourceBundle) languages.get(locale);
294  0 logger.debug("Loading new resource bundle for locale " + locale
295    + " from the database.");
296  0 List<Language> languageItems = ServletContextUtils
297    .getItrackerServices().getConfigurationService().getLanguage(
298    locale);
299  0 bundle = ITrackerResourceBundle.loadBundle(locale, languageItems);
300  0 if (bundle != null) {
301  0 putBundle(locale, bundle);
302  0 } else if (!locale.toString().equals(getDefaultLocale())) {
303  0 bundle = getBundle(getLocale());
304    }
305   
306  0 return bundle;
307    }
308   
 
309  0 toggle public static void putBundle(Locale locale, ResourceBundle bundle) {
310  0 if (locale != null && bundle != null) {
311  0 synchronized (bundleLock) {
312  0 languages.put(locale, bundle);
313    // if (bundle instanceof ITrackerResourceBundle) {
314    // setData((ITrackerResourceBundle)bundle);
315    // }
316  0 String localeString = locale.toString();
317  0 if (localeString.length() == 5) {
318  0 localeString = localeString.substring(0, 2) + "_"
319    + localeString.substring(3).toUpperCase();
320    }
321  0 locales.put(localeString, locale);
322    }
323    }
324    }
325   
 
326  0 toggle static void setData(ITrackerResourceBundle bundle) {
327   
328  0 List<Language> languageItems = ServletContextUtils
329    .getItrackerServices().getConfigurationService()
330    .getLanguage(bundle.getLocale());
331  0 bundle.setContents(languageItems);
332    }
333    /**
334    * Clears a single cached resource bundle. The next time the bundle is
335    * accessed, it will be reloaded and placed into the cache.
336    */
 
337  0 toggle public static void clearBundle(Locale locale) {
338  0 if (locale != null) {
339  0 synchronized (bundleLock) {
340  0 ResourceBundle bundle = languages.get(locale);
341    // if (bundle instanceof ITrackerResourceBundle) {
342    // ((ITrackerResourceBundle)bundle).
343    // }
344  0 languages.remove(locale);
345    }
346    }
347    }
348   
349    /**
350    * Clears all cached resource bundles. The next time a bundle is accessed,
351    * it will be reloaded and placed into the cache.
352    */
 
353  0 toggle public static void clearBundles() {
354  0 synchronized (bundleLock) {
355   
356  0 languages.clear();
357   
358    }
359    }
360   
361    /**
362    * Clears a single key from all cached resource bundles. The key is then
363    * marked that it is dirty and should be reloaded on hte next access.
364    */
 
365  0 toggle public static void clearKeyFromBundles(String key, boolean markDirty) {
366  0 if (key != null) {
367  0 synchronized (bundleLock) {
368  0 for (Iterator<ResourceBundle> iter = languages.values()
369  0 .iterator(); iter.hasNext();) {
370  0 ((ITrackerResourceBundle) iter.next()).removeValue(key,
371    markDirty);
372    }
373    }
374    }
375    }
376   
 
377  0 toggle public static String getString(String key) {
378  0 return getString(key, getLocale(defaultLocale));
379    }
380   
 
381  0 toggle public static String getString(String key, String locale) {
382  0 if (key == null) {
383  0 return "";
384    }
385   
386  0 if (locale == null || locale.equals("")) {
387  0 locale = getDefaultLocale();
388    }
389   
390  0 return getString(key, getLocale(locale));
391    }
392   
 
393  7 toggle public static String getString(String key, Locale locale) {
394  7 if (key == null) {
395  0 return "";
396    }
397   
398  7 if (locale == null) {
399  0 locale = getLocale(getDefaultLocale());
400    }
401  7 String val;
402  7 try {
403  7 try {
404    // if (logger.isDebugEnabled()) {
405    // logger.debug("getString: " + key + " for locale " + locale);
406    // }
407  7 val = getBundle(locale).getString(key);
408  0 if (null != val) {
409   
410    // if (logger.isDebugEnabled()) {
411    // logger.debug("getString: found " + val + " for key" + key
412    // + ", locale " + locale);
413    // }
414  0 return val;
415    } else {
416  0 val = ITrackerResources.getString(key);
417   
418    // if (logger.isDebugEnabled()) {
419    // logger.debug("getString: found in base: " + val +
420    // " for key" + key);
421    // }
422    }
423    } catch (ITrackerDirtyResourceException idre) {
424   
425    // logger.debug("Loading new key to replace dirty key " + key +
426    // " for resource bundle for locale "
427    // + locale);
428  0 Language languageItem = ServletContextUtils
429    .getItrackerServices().getConfigurationService()
430    .getLanguageItemByKey(key, locale);
431  0 ResourceBundle bundle = getBundle(locale);
432  0 ((ITrackerResourceBundle) bundle)
433    .updateValue(languageItem);
434  0 val = bundle.getString(key);
435    }
436  0 return val;
437    } catch (NullPointerException ex) {
438  7 logger.error(
439    "Unable to get any resources. The requested locale was "
440    + locale, ex);
441  7 return "MISSING BUNDLE: " + locale;
442    } catch (MissingResourceException ex) {
443  0 logger.warn(
444    "MissingResourceException caught while retrieving translation key '"
445    + key + "' for locale " + locale, ex);
446  0 return "MISSING KEY: " + key;
447    } catch (NoSuchEntityException ex) {
448  0 logger.info("getString: not found " + key + " locale: " + locale,
449    ex);
450  0 try {
451  0 return getEditBundle(locale).getString(key);
452    } catch (Exception ex2) {
453  0 logger.warn(
454    "getString: caught while retrieving translation key '"
455    + key + "' for locale " + locale, ex2);
456  0 return "MISSING KEY: " + key;
457    }
458    }
459    }
460   
 
461  0 toggle public static String getString(String key, Object[] options) {
462  0 return getString(key, getLocale(getDefaultLocale()), options);
463    }
464   
 
465  0 toggle public static String getString(String key, String locale, Object[] options) {
466  0 return getString(key, getLocale(locale), options);
467    }
468   
 
469  0 toggle public static String getString(String key, Locale locale, Object[] options) {
470  0 String message = getString(key, locale);
471  0 return MessageFormat.format(message, options, locale);
472    }
473   
 
474  0 toggle public static String getString(String key, String locale, String option) {
475  0 String message = getString(key, locale);
476  0 return MessageFormat.format(message, new Object[] { option },
477    getLocale(locale));
478    }
479   
 
480  0 toggle public static String getString(String key, Locale locale, String option) {
481  0 String message = getString(key, locale);
482  0 return MessageFormat.format(message, new Object[] { option }, locale);
483    }
484   
 
485  0 toggle public static String getCheckForKey(String key)
486    throws MissingResourceException {
487  0 return getCheckForKey(key, getLocale());
488    }
489   
 
490  0 toggle public static String getCheckForKey(String key, Locale locale)
491    throws MissingResourceException {
492  0 try {
493  0 return getBundle(locale).getString(key);
494    } catch (ITrackerDirtyResourceException idre) {
495  0 return getString(key, locale);
496    } catch (NullPointerException ex) {
497  0 logger.error("Unable to get ResourceBundle for locale " + locale,
498    ex);
499  0 throw new MissingResourceException("MISSING LOCALE: " + locale,
500    "ITrackerResources", key);
501    }
502    }
503   
 
504  0 toggle public static boolean isLongString(String key) {
505  0 String value = getString(key);
506  0 if (value.length() > 80 || value.indexOf('\n') > 0) {
507  0 return true;
508    }
509  0 return false;
510    }
511   
 
512  0 toggle public static String escapeUnicodeString(String str, boolean escapeAll) {
513  0 if (str == null) {
514  0 return "";
515    }
516   
517  0 StringBuffer sb = new StringBuffer();
518  0 for (int i = 0; i < str.length(); i++) {
519  0 char ch = str.charAt(i);
520  0 if (!escapeAll && ((ch >= 0x0020) && (ch <= 0x007e))) {
521  0 sb.append(ch);
522    } else {
523  0 sb.append('\\').append('u');
524  0 sb.append(encodeHex((ch >> 12) & 0xF));
525  0 sb.append(encodeHex((ch >> 8) & 0xF));
526  0 sb.append(encodeHex((ch >> 4) & 0xF));
527  0 sb.append(encodeHex(ch & 0xF));
528    }
529    }
530  0 return sb.toString();
531    }
532   
 
533  12 toggle public static String unescapeUnicodeString(String str) {
534  12 StringBuffer sb = new StringBuffer();
535   
536  66 for (int i = 0; i < str.length();) {
537  54 char ch = str.charAt(i++);
538  54 if (ch == '\\') {
539  0 if (str.charAt(i++) == 'u') {
540  0 int value = 0;
541  0 for (int j = 0; j < 4; j++) {
542  0 value = (value << 4) + decodeHex(str.charAt(i++));
543    }
544  0 sb.append((char) value);
545    } else {
546  0 sb.append("\\" + str.charAt(i));
547    }
548    } else {
549  54 sb.append(ch);
550    }
551    }
552  12 return sb.toString();
553    }
554   
555    public static final String HEXCHARS = "0123456789ABCDEF";
556   
 
557  0 toggle public static char encodeHex(int value) {
558  0 return HEXCHARS.charAt(value & 0xf);
559    }
560   
 
561  0 toggle public static int decodeHex(char ch) {
562  0 int value = -1;
563   
564  0 if (ch >= '0' && ch <= '9') {
565  0 value = ch - '0';
566  0 } else if (ch >= 'a' && ch <= 'f') {
567  0 value = ch - 'a' + 10;
568  0 } else if (ch >= 'A' && ch <= 'F') {
569  0 value = ch - 'A' + 10;
570    }
571   
572  0 return value;
573    }
574   
575    // public static void setConfigurationService(ConfigurationService
576    // configurationService) {
577    // ITrackerResources.configurationService = configurationService;
578    // }
579   
 
580  0 toggle public static boolean isInitialized() {
581  0 return initialized;
582    }
583   
 
584  0 toggle public static void setInitialized(boolean initialized) {
585  0 ITrackerResources.initialized = initialized;
586    }
587   
 
588  0 toggle public static String getParentLocale(String locale) {
589  0 String localeCode = locale;
590  0 if (localeCode == null) {
591  0 localeCode = getDefaultLocale();
592  0 } else if (localeCode.equals(getDefaultLocale())) {
593  0 localeCode = BASE_LOCALE;
594    } else {
595  0 Locale l = getLocale(locale);
596   
597  0 if (!l.getVariant().equals("")) {
598  0 localeCode = l.getLanguage() + "_" + l.getCountry();
599    }
600  0 if (!l.getCountry().equals("")) {
601  0 localeCode = l.getLanguage();
602    }
603  0 if (!l.getLanguage().equals("")) {
604  0 localeCode = getDefaultLocale();
605    }
606    }
607  0 return localeCode;
608    }
609   
610    }