1 10 package org.mmbase.util; 11 12 import java.util.*; 13 import org.mmbase.util.logging.*; 14 import org.mmbase.util.xml.DocumentReader; 15 import org.w3c.dom.*; 16 17 38 public class LocalizedString implements java.io.Serializable , Cloneable { 39 40 private static final Logger log = Logging.getLoggerInstance(LocalizedString.class); 41 42 private static final long serialVersionUID = 1L; 43 44 private static Locale defaultLocale = null; 46 56 public static Locale setDefault(Locale locale) { 57 if (defaultLocale != null) return defaultLocale; 58 defaultLocale = locale; 59 return null; 60 } 61 64 public static Locale getDefault() { 65 return defaultLocale != null ? defaultLocale : Locale.getDefault(); 66 } 67 68 73 public static Collection toStrings(Collection col, Locale locale) { 74 if (col == null || col.size() == 0) return col; 75 Collection res = new ArrayList(); 76 Iterator i = col.iterator(); 77 while (i.hasNext()) { 78 LocalizedString s = (LocalizedString) i.next(); 79 res.add(s.get(locale)); 80 } 81 return res; 82 } 83 84 private String key; 85 86 private Map values = null; 87 private String bundle = null; 88 89 protected LocalizedString() { 91 } 92 93 96 public LocalizedString(String k) { 97 key = k; 98 } 99 100 103 public String getKey() { 104 return key; 105 } 106 107 110 public void setKey(String key) { 111 this.key = key; 112 } 113 114 117 public String get(Locale locale) { 118 if (locale == null) { 119 locale = defaultLocale == null ? Locale.getDefault() : defaultLocale; 120 } 121 if (values != null) { 122 String result = (String ) values.get(locale); 123 124 if (result != null) return result; 125 126 String variant = locale.getVariant(); 127 String country = locale.getCountry(); 128 String language = locale.getLanguage(); 129 130 if (! "".equals(variant)) { 131 result = (String ) values.get(new Locale(language, country)); 132 if (result != null) return result; 133 } 134 135 if (! "".equals(country)) { 136 result = (String ) values.get(new Locale(language)); 137 if (result != null) return result; 138 } 139 140 if (locale.equals(defaultLocale)) { 147 result = (String ) values.get(null); 148 if (result != null) { 149 values.put(locale, result); 150 return result; 151 } 152 } 153 } 154 155 if (bundle != null) { 156 try { 157 return ResourceBundle.getBundle(bundle, locale).getString(key); 158 } catch (MissingResourceException mre) { 159 if (log.isDebugEnabled()) { 161 log.debug("Cannot get resource from bundle: " + bundle + ", key: " + key); 162 } 163 } 164 } 165 166 return key; 167 } 168 169 173 public void set(final String value, Locale locale) { 174 if (key == null) key = value; 175 176 if (values == null) { 177 values = new HashMap(); 178 } 179 180 if (locale == null) { 181 locale = defaultLocale; 182 } 183 184 values.put(locale, value); 185 186 if (locale != null) { 187 String variant = locale.getVariant(); 188 String country = locale.getCountry(); 189 String language = locale.getLanguage(); 190 if (! "".equals(variant)) { 191 Locale loc = new Locale(language, country); 192 if (values.get(loc) == null) { 193 values.put(loc, value); 194 } 195 } 196 if (! "".equals(country)) { 197 Locale loc = new Locale(language); 198 if (values.get(loc) == null) { 199 values.put(loc, value); 200 } 201 } 202 } 203 } 204 205 209 public Map asMap() { 210 if (values == null) return Collections.EMPTY_MAP; 211 return Collections.unmodifiableMap(values); 212 } 213 214 219 220 public void setBundle(String b) { 221 bundle = b; 222 } 223 224 229 public String toString() { 230 return get(null); 231 } 232 233 234 240 public static Locale getLocale(Element element) { 241 return getLocale(element.getAttribute("xml:lang")); 242 } 243 244 245 248 public static Locale getLocale(String xmlLang) { 249 Locale loc = null; 250 if (xmlLang != null && (! xmlLang.equals(""))) { 251 252 String [] split = xmlLang.split("-"); 253 if (split.length == 1) { 254 loc = new Locale(split[0]); 255 } else if (split.length == 2) { 256 loc = new Locale(split[0], split[1]); 257 } else { 258 loc = new Locale(split[0], split[1], split[2]); 259 } 260 } 261 return loc; 262 } 263 264 271 public static String getXmlLang(Locale locale) { 272 if (locale == null) return null; 273 StringBuffer lang = new StringBuffer (locale.getLanguage()); 274 String country = locale.getCountry(); 275 if (country.length() > 0) { 276 lang.append("-").append(country); 277 String variant = locale.getVariant(); 278 if (variant != null && variant.length() > 0) { 279 lang.append("-").append(variant); 280 } 281 } 282 return lang.toString(); 283 } 284 285 291 public static void setXmlLang(Element element, Locale locale) { 292 String xmlLang = getXmlLang(locale); 293 if (xmlLang != null) { 294 element.setAttribute("xml:lang", xmlLang); 295 } 296 } 297 298 302 303 public void fillFromXml(final String tagName, final Element element) { 304 NodeList childNodes = element.getChildNodes(); 305 for (int k = 0; k < childNodes.getLength(); k++) { 306 if (childNodes.item(k) instanceof Element) { 307 Element childElement = (Element) childNodes.item(k); 308 if (tagName.equals(childElement.getLocalName())) { 309 Locale locale = getLocale(childElement); 310 String description = DocumentReader.getNodeTextValue(childElement); 311 set(description, locale); 312 } 313 } 314 } 315 } 316 317 329 public void toXml(final String tagName, final String ns, final Element element, final String path) { 330 if (values != null) { 332 org.w3c.dom.NodeList nl = element.getElementsByTagName(tagName); 334 Iterator i = values.entrySet().iterator(); 335 while (i.hasNext()) { 336 Map.Entry entry = (Map.Entry) i.next(); 337 Locale loc = (Locale) entry.getKey(); 338 String value = (String ) entry.getValue(); 339 String xmlLang = getXmlLang(loc); 340 Element child = null; 342 for (int j = 0; j < nl.getLength(); j++) { 343 Element cand = (Element) nl.item(j); 344 if (cand.getAttribute("xml:lang").equals(xmlLang)) { 345 child = cand; 346 break; 347 } 348 } 349 if (child == null) { 350 if (ns != null) { 351 child = element.getOwnerDocument().createElementNS(ns, tagName); 352 } else { 353 child = element.getOwnerDocument().createElement(tagName); 354 } 355 DocumentReader.appendChild(element, child, path); 356 setXmlLang(child, loc); 357 } 358 DocumentReader.setNodeTextValue(child, value); 359 } 360 } 361 } 362 363 public Object clone() { 364 try { 365 LocalizedString clone = (LocalizedString)super.clone(); 366 if (values != null) { 367 clone.values = (Map)((HashMap)values).clone(); 368 } 369 return clone; 370 } catch (CloneNotSupportedException cnse) { 371 log.error("Cannot clone this LocalizedString"); 373 throw new RuntimeException ("Cannot clone this LocalizedString", cnse); 374 } 375 } 376 377 public boolean equals(Object o) { 378 if (o instanceof LocalizedString) { 379 LocalizedString os = (LocalizedString) o; 380 return 381 key.equals(os.key) && 382 (values == null ? os.values == null : values.equals(os.values)) && 383 (bundle == null ? os.bundle == null : bundle.equals(os.bundle)) 384 ; 385 } else { 386 return false; 387 } 388 } 389 390 public int hashCode() { 391 int result = 0; 392 result = HashCodeUtil.hashCode(result, key); 393 result = HashCodeUtil.hashCode(result, values); 394 result = HashCodeUtil.hashCode(result, bundle); 395 return result; 396 } 397 398 } 399 | Popular Tags |