001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.migration.model22.other;
023:
024: import org.apache.log4j.Logger;
025:
026: import java.util.ArrayList;
027: import java.util.Collection;
028: import java.util.Collections;
029: import java.util.Comparator;
030: import java.util.HashMap;
031: import java.util.Iterator;
032: import java.util.List;
033: import java.util.Locale;
034: import java.util.Map;
035:
036: /**
037: * Additional infos about a locale.
038: *
039: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
040: * @version $Revision: 8784 $
041: */
042: public final class LocaleInfo {
043:
044: // Static fields ****************************************************************************************************
045:
046: private static Logger log = Logger.getLogger(LocaleInfo.class);
047:
048: /** The locale info map. */
049: private static final Map infos = initializeInfos();
050:
051: /** The locale info objects. */
052: private static final Collection all = initializeAll();
053:
054: // Fields ***********************************************************************************************************
055:
056: /** The parent locale info or null if no parent. */
057: private LocaleInfo parent;
058:
059: /** The relaled locale. */
060: private Locale locale;
061:
062: /** The trailing name used to compute resource bundle name. */
063: private String trailingName;
064:
065: // Constructor ******************************************************************************************************
066:
067: private LocaleInfo(Locale locale) {
068: this .locale = locale;
069: this .trailingName = computeTrailingName(locale);
070: }
071:
072: // Public ***********************************************************************************************************
073:
074: public LocaleInfo getParent() {
075: return parent;
076: }
077:
078: public Locale getLocale() {
079: return locale;
080: }
081:
082: public String getTrailingName() {
083: return trailingName;
084: }
085:
086: // Object override **************************************************************************************************
087:
088: public String toString() {
089: return locale.toString();
090: }
091:
092: // Static ***********************************************************************************************************
093:
094: /**
095: * Compute the trainling name for a given locale.
096: *
097: * @param locale the locale
098: * @return the trailing name
099: * @throws IllegalArgumentException if locale is null
100: */
101: public static String computeTrailingName(Locale locale)
102: throws IllegalArgumentException {
103: if (locale == null) {
104: throw new IllegalArgumentException(
105: "locale parameter is null");
106: }
107: StringBuffer tmp = new StringBuffer();
108: if (locale.getLanguage() != null
109: && locale.getLanguage().length() > 0) {
110: tmp.append('_').append(locale.getLanguage());
111: if (locale.getCountry() != null
112: && locale.getCountry().length() > 0) {
113: tmp.append('_').append(locale.getCountry());
114: {
115: if (locale.getVariant() != null
116: && locale.getVariant().length() > 0) {
117: tmp.append('_').append(locale.getVariant());
118: }
119: }
120: }
121: }
122: return tmp.toString();
123: }
124:
125: private static final Map initializeInfos() {
126: Map locales = new HashMap();
127:
128: // Initialize all objects
129: Locale[] temp = Locale.getAvailableLocales();
130: for (int i = 0; i < temp.length; i++) {
131: Locale locale = temp[i];
132: LocaleInfo info = new LocaleInfo(locale);
133: locales.put(locale.toString(), info);
134: log.debug("LocaleInfo " + locale.toString()
135: + " initialized");
136: }
137:
138: // Resolve parent links
139: for (Iterator i = locales.values().iterator(); i.hasNext();) {
140: LocaleInfo info = (LocaleInfo) i.next();
141: LocaleInfo parent = null;
142: Locale locale = info.locale;
143:
144: // If this is a language/country/variant, try to get language/country
145: if (locale.getVariant() != null
146: && locale.getVariant().length() > 0) {
147: Locale key = new Locale(locale.getLanguage(), locale
148: .getCountry());
149: parent = (LocaleInfo) locales.get(key.toString());
150: }
151:
152: // If this is a language/country, try to get language
153: if (parent == null && locale.getCountry() != null
154: && locale.getCountry().length() > 0) {
155: Locale key = new Locale(locale.getLanguage());
156: parent = (LocaleInfo) locales.get(key.toString());
157: }
158:
159: // Set the parent
160: info.parent = parent;
161: }
162:
163: return Collections.unmodifiableMap(locales);
164: }
165:
166: private static Collection initializeAll() {
167: Comparator c = new Comparator() {
168: public int compare(Object o1, Object o2) {
169: String s1 = ((LocaleInfo) o1).getTrailingName();
170: String s2 = ((LocaleInfo) o2).getTrailingName();
171: return s1.compareTo(s2);
172: }
173: };
174: List all = new ArrayList(infos.values());
175: Collections.sort(all, c);
176: return Collections.unmodifiableList(all);
177: }
178:
179: public static Collection getAll() {
180: return all;
181: }
182:
183: public static LocaleInfo decodeLocaleInfo(String code) {
184: return (LocaleInfo) infos.get(code);
185: }
186:
187: public static LocaleInfo decodeLocaleInfo(Locale locale) {
188: return (LocaleInfo) infos.get(locale.toString());
189: }
190: }
|