1 16 package org.apache.cocoon.woody.datatype.convertor; 17 18 import org.apache.commons.collections.FastHashMap; 19 20 import java.util.Locale ; 21 22 30 public class LocaleMap { 31 private FastHashMap map = new FastHashMap(); 32 33 38 public Object get(Locale locale) { 39 if (map.size() == 0) 40 return null; 41 42 String full = getFullKey(locale); 43 44 if (!map.containsKey(full)) { 45 String altKey = locale.getLanguage() + '-' + locale.getCountry(); 48 Object object = map.get(altKey); 49 if (object != null) { 50 map.put(full, object); 51 return object; 52 } 53 54 altKey = locale.getLanguage(); 55 object = map.get(altKey); 56 if (object != null) { 57 map.put(full, object); 58 return object; 59 } 60 61 object = map.get(""); 62 if (object != null) { 63 map.put(full, object); 64 return object; 65 } 66 67 map.put(full, null); 68 } 69 70 return map.get(full); 71 } 72 73 private final String getFullKey(Locale locale) { 74 return locale.getLanguage() + '-' + locale.getCountry() + '-' + locale.getVariant(); 75 } 76 77 private final String getKey(Locale locale) { 78 boolean hasLanguage = !locale.getLanguage().equals(""); 79 boolean hasCountry = !locale.getCountry().equals(""); 80 boolean hasVariant = !locale.getVariant().equals(""); 81 82 if (hasLanguage && hasCountry && hasVariant) 83 return locale.getLanguage() + '-' + locale.getCountry() + '-' + locale.getVariant(); 84 else if (hasLanguage && hasCountry) 85 return locale.getLanguage() + '-' + locale.getCountry(); 86 else if (hasLanguage) 87 return locale.getLanguage(); 88 else 89 return ""; 90 } 91 92 public void put(Locale locale, Object object) { 93 map.put(getKey(locale), object); 94 } 95 } 96 | Popular Tags |