1 9 package org.jboss.portal.common.util; 10 11 import java.util.Collection ; 12 import java.util.Collections ; 13 import java.util.Comparator ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.Locale ; 17 import java.util.Map ; 18 import java.util.Set ; 19 import java.util.TreeSet ; 20 21 import org.apache.log4j.Logger; 22 23 27 public final class LocaleInfo 28 { 29 30 32 private static Logger log = Logger.getLogger(LocaleInfo.class); 33 34 35 private static final Map infos = initializeInfos(); 36 37 38 private static final Set all = initializeSet(); 39 40 42 43 private LocaleInfo parent; 44 45 46 private Locale locale; 47 48 49 private String trailingName; 50 51 53 private LocaleInfo(Locale locale) 54 { 55 this.locale = locale; 56 this.trailingName = computeTrailingName(locale); 57 } 58 59 61 public LocaleInfo getParent() 62 { 63 return parent; 64 } 65 66 public Locale getLocale() 67 { 68 return locale; 69 } 70 71 public String getTrailingName() 72 { 73 return trailingName; 74 } 75 76 78 public String toString() 79 { 80 return locale.toString(); 81 } 82 83 85 92 public static final String computeTrailingName(Locale locale) throws IllegalArgumentException 93 { 94 if (locale == null) 95 { 96 throw new IllegalArgumentException ("locale parameter is null"); 97 } 98 StringBuffer tmp = new StringBuffer (); 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 initializeInfos() 117 { 118 Map locales = new HashMap (); 119 120 Locale [] temp = Locale.getAvailableLocales(); 122 for (int i = 0;i < temp.length;i++) 123 { 124 Locale 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 for (Iterator i = locales.values().iterator();i.hasNext();) 132 { 133 LocaleInfo info = (LocaleInfo)i.next(); 134 LocaleInfo parent = null; 135 Locale locale = info.locale; 136 137 if (locale.getVariant() != null && locale.getVariant().length() > 0) 139 { 140 Locale key = new Locale (locale.getLanguage(), locale.getCountry()); 141 parent = (LocaleInfo)locales.get(key.toString()); 142 } 143 144 if (parent == null && locale.getCountry() != null && locale.getCountry().length() > 0) 146 { 147 Locale key = new Locale (locale.getLanguage()); 148 parent = (LocaleInfo)locales.get(key.toString()); 149 } 150 151 info.parent = parent; 153 } 154 155 return Collections.unmodifiableMap(locales); 156 } 157 158 private static final Set initializeSet() 159 { 160 TreeSet set = new TreeSet (new Comparator () 161 { 162 public int compare(Object o1, Object o2) 163 { 164 String s1 = ((LocaleInfo)o1).getTrailingName(); 165 String 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 getAll() 174 { 175 return all; 176 } 177 178 public static LocaleInfo decodeLocaleInfo(String code) 179 { 180 return (LocaleInfo)infos.get(code); 181 } 182 183 public static LocaleInfo decodeLocaleInfo(Locale locale) 184 { 185 return (LocaleInfo)infos.get(locale.toString()); 186 } 187 } 188 | Popular Tags |