KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > common > util > LocaleInfo


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.common.util;
10
11 import java.util.Collection JavaDoc;
12 import java.util.Collections JavaDoc;
13 import java.util.Comparator JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Locale JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Set JavaDoc;
19 import java.util.TreeSet JavaDoc;
20
21 import org.apache.log4j.Logger;
22
23 /**
24  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
25  * @version $Revision: 1.2 $
26  */

27 public final class LocaleInfo
28 {
29
30    // Static fields ****************************************************************************************************
31

32    private static Logger log = Logger.getLogger(LocaleInfo.class);
33
34    /** The locale info map. */
35    private static final Map JavaDoc infos = initializeInfos();
36
37    /** The locale info objects as a sorted set. */
38    private static final Set JavaDoc all = initializeSet();
39
40    // Fields ***********************************************************************************************************
41

42    /** The parent locale info or null if no parent. */
43    private LocaleInfo parent;
44
45    /** The relaled locale. */
46    private Locale JavaDoc locale;
47
48    /** The trailing name used to compute resource bundle name. */
49    private String JavaDoc trailingName;
50
51    // Constructor ******************************************************************************************************
52

53    private LocaleInfo(Locale JavaDoc locale)
54    {
55       this.locale = locale;
56       this.trailingName = computeTrailingName(locale);
57    }
58
59    // Public ***********************************************************************************************************
60

61    public LocaleInfo getParent()
62    {
63       return parent;
64    }
65
66    public Locale JavaDoc getLocale()
67    {
68       return locale;
69    }
70
71    public String JavaDoc getTrailingName()
72    {
73       return trailingName;
74    }
75
76    // Object override **************************************************************************************************
77

78    public String JavaDoc toString()
79    {
80       return locale.toString();
81    }
82
83    // Static ***********************************************************************************************************
84

85    /**
86     * Compute the trainling name for a given locale.
87     *
88     * @param locale the locale
89     * @return the trailing name
90     * @throws IllegalArgumentException if locale is null
91     */

92    public static final String JavaDoc computeTrailingName(Locale JavaDoc locale) throws IllegalArgumentException JavaDoc
93    {
94       if (locale == null)
95       {
96          throw new IllegalArgumentException JavaDoc("locale parameter is null");
97       }
98       StringBuffer JavaDoc tmp = new StringBuffer JavaDoc();
99       if (locale.getLanguage() != null && locale.getLanguage().length() > 0)
100       {
101          tmp.append('_').append(locale.getLanguage());
102          if (locale.getCountry() != null && locale.getCountry().length() > 0)
103          {
104             tmp.append('_').append(locale.getCountry());
105             {
106                if (locale.getVariant() != null && locale.getVariant().length() > 0)
107                {
108                   tmp.append('_').append(locale.getVariant());
109                }
110             }
111          }
112       }
113       return tmp.toString();
114    }
115
116    private static final Map JavaDoc initializeInfos()
117    {
118       Map JavaDoc locales = new HashMap JavaDoc();
119
120       // Initialize all objects
121
Locale JavaDoc[] temp = Locale.getAvailableLocales();
122       for (int i = 0;i < temp.length;i++)
123       {
124          Locale JavaDoc locale = temp[i];
125          LocaleInfo info = new LocaleInfo(locale);
126          locales.put(locale.toString(), info);
127          log.debug("LocaleInfo " + locale.toString() + " initialized");
128       }
129
130       // Resolve parent links
131
for (Iterator JavaDoc i = locales.values().iterator();i.hasNext();)
132       {
133          LocaleInfo info = (LocaleInfo)i.next();
134          LocaleInfo parent = null;
135          Locale JavaDoc locale = info.locale;
136
137          // If this is a language/country/variant, try to get language/country
138
if (locale.getVariant() != null && locale.getVariant().length() > 0)
139          {
140             Locale JavaDoc key = new Locale JavaDoc(locale.getLanguage(), locale.getCountry());
141             parent = (LocaleInfo)locales.get(key.toString());
142          }
143
144          // If this is a language/country, try to get language
145
if (parent == null && locale.getCountry() != null && locale.getCountry().length() > 0)
146          {
147             Locale JavaDoc key = new Locale JavaDoc(locale.getLanguage());
148             parent = (LocaleInfo)locales.get(key.toString());
149          }
150
151          // Set the parent
152
info.parent = parent;
153       }
154
155       return Collections.unmodifiableMap(locales);
156    }
157
158    private static final Set JavaDoc initializeSet()
159    {
160       TreeSet JavaDoc set = new TreeSet JavaDoc(new Comparator JavaDoc()
161       {
162          public int compare(Object JavaDoc o1, Object JavaDoc o2)
163          {
164             String JavaDoc s1 = ((LocaleInfo)o1).getTrailingName();
165             String JavaDoc s2 = ((LocaleInfo)o2).getTrailingName();
166             return s1.compareTo(s2);
167          }
168       });
169       set.addAll(infos.values());
170       return Collections.unmodifiableSet(set);
171    }
172
173    public static Collection JavaDoc getAll()
174    {
175       return all;
176    }
177
178    public static LocaleInfo decodeLocaleInfo(String JavaDoc code)
179    {
180       return (LocaleInfo)infos.get(code);
181    }
182
183    public static LocaleInfo decodeLocaleInfo(Locale JavaDoc locale)
184    {
185       return (LocaleInfo)infos.get(locale.toString());
186    }
187 }
188
Popular Tags