View Javadoc
1 /* 2 * $Header: /cvsroot/ebxmlrr/jaxr/src/com/sun/xml/registry/ebxml/infomodel/InternationalStringImpl.java,v 1.10 2003/08/22 03:01:38 farrukh_najmi Exp $ 3 * 4 * 5 */ 6 7 8 package com.sun.xml.registry.ebxml.infomodel; 9 10 import javax.xml.registry.*; 11 import javax.xml.registry.infomodel.*; 12 13 import java.util.*; 14 15 import com.sun.xml.registry.ebxml.LifeCycleManagerImpl; 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 19 //import org.oasis.ebxml.registry.bindings.rim.*; 20 21 22 /*** 23 * Class Declaration for Class1 24 * @author <a href="mailto:Farrukh.Najmi@Sun.COM">Farrukh S. Najmi</a> 25 */ 26 public class InternationalStringImpl implements InternationalString, Cloneable { 27 28 private static final Log log = 29 LogFactory.getLog(InternationalStringImpl.class); 30 31 private HashMap localizedStringMap = new HashMap(); 32 private LifeCycleManagerImpl lcm = null; 33 34 public InternationalStringImpl(LifeCycleManagerImpl lcm) 35 throws JAXRException 36 { 37 this.lcm = lcm; 38 } 39 40 public InternationalStringImpl(LifeCycleManagerImpl lcm, 41 org.oasis.ebxml.registry.bindings.rim.InternationalStringType ebObj) 42 throws JAXRException 43 { 44 org.oasis.ebxml.registry.bindings.rim.InternationalStringTypeItem[] items = 45 ebObj.getInternationalStringTypeItem(); 46 for (int i=0; i<items.length; i++) { 47 org.oasis.ebxml.registry.bindings.rim.LocalizedStringType lst = 48 items[i].getLocalizedString(); 49 50 LocalizedStringImpl ls = new LocalizedStringImpl(lcm, lst); 51 addLocalizedString(ls); 52 } 53 } 54 55 //The key to the map is a combination of locale and charset 56 //This allows map to have multiple entries for the same locale with different charsets 57 private String getKey(Locale l, String charSetName) { 58 return charSetName + "_" + l.toString(); 59 } 60 61 public String getValue() throws JAXRException { 62 return getValue(Locale.getDefault()); 63 } 64 65 public String getValue(Locale locale) throws JAXRException { 66 String value = null; 67 LocalizedString ls = (LocalizedString)localizedStringMap.get( 68 getKey(locale, LocalizedStringImpl.DEFAULT_CHARSET_NAME)); 69 if (ls != null) { 70 value = ls.getValue(); 71 } 72 73 return value; 74 } 75 76 public void setValue(String val) throws JAXRException { 77 setValue(Locale.getDefault(), val); 78 } 79 80 public void setValue(Locale locale, String val) throws JAXRException { 81 LocalizedString ls = (LocalizedString)localizedStringMap.get( 82 getKey(locale, LocalizedStringImpl.DEFAULT_CHARSET_NAME)); 83 if (ls != null) { 84 ls.setValue(val); 85 } 86 else { 87 ls = new LocalizedStringImpl(null); 88 ls.setLocale(locale); 89 ls.setValue(val); 90 localizedStringMap.put(getKey(locale, LocalizedStringImpl.DEFAULT_CHARSET_NAME), ls); 91 } 92 } 93 94 public void addLocalizedString(LocalizedString ls) throws JAXRException { 95 localizedStringMap.put(getKey(ls.getLocale(), ls.getCharsetName()), ls); 96 } 97 98 public void addLocalizedStrings(Collection localizedStrings) throws JAXRException { 99 Iterator iter = localizedStrings.iterator(); 100 while (iter.hasNext()) { 101 LocalizedString ls = (LocalizedString)iter.next(); 102 addLocalizedString(ls); 103 } 104 } 105 106 public void removeLocalizedString(LocalizedString ls) throws JAXRException { 107 String key = getKey(ls.getLocale(), ls.getCharsetName()); 108 LocalizedString old = (LocalizedString)localizedStringMap.get(key); 109 110 if (ls == old) { 111 localizedStringMap.remove(key); 112 } 113 } 114 115 public void removeLocalizedStrings(Collection localizedStrings) throws JAXRException { 116 Iterator iter = localizedStrings.iterator(); 117 while (iter.hasNext()) { 118 LocalizedString ls = (LocalizedString)iter.next(); 119 removeLocalizedString(ls); 120 } 121 } 122 123 public LocalizedString getLocalizedString(Locale locale, String charsetName) throws JAXRException { 124 String key = getKey(locale, charsetName); 125 return (LocalizedString)localizedStringMap.get(key); 126 } 127 128 public Collection getLocalizedStrings() throws JAXRException { 129 Collection localizedStrings = localizedStringMap.values(); 130 return localizedStrings; 131 } 132 133 /*** 134 * Gets the LocalizedString for the default locale (<code>Locale.getDefault() 135 * </code>) or the closest match, according to a precedence list (see {@link 136 * #getClosestKeys(java.util.Locale, java.lang.String) getClosestKeys( 137 * java.util.Locale, java.lang.String)}. 138 * 139 * @param locale the desired Locale 140 * @return LocalizedString for default Locale or for the 1st alternate Locale 141 * found. Null if nothing found. 142 */ 143 public LocalizedString getClosestLocalizedString() throws JAXRException { 144 return getClosestLocalizedString(Locale.getDefault(), LocalizedStringImpl.DEFAULT_CHARSET_NAME); 145 } 146 147 /*** 148 * Gets the LocalizedString for the given locale or the closest match, 149 * according to a precedence list (see {@link #getClosestKeys( 150 * java.util.Locale, java.lang.String) getClosestKeys(java.util.Locale, 151 * java.lang.String)}. 152 * 153 * @param locale the desired Locale 154 * @return LocalizedString for 'locale' or for the 1st alternate Locale found. 155 * Null if nothing found. 156 */ 157 public LocalizedString getClosestLocalizedString(Locale locale, String charsetName) throws JAXRException { 158 Iterator keys = getClosestKeys(locale, charsetName).iterator(); 159 while (keys.hasNext()) { 160 Object lString = localizedStringMap.get(keys.next()); 161 if (lString != null) { 162 // TO DO: make sure that when returning Java actual object, 163 // modifications are handled correctly. 164 return (LocalizedString)lString; 165 } 166 } 167 return null; 168 } 169 170 /*** 171 * Gets the localized value of an InternationalString using default locale 172 * (<code>Locale.getDefault()</code>) or the closest match, according to a 173 * precedence list (see {@link #getClosestKeys(java.util.Locale, 174 * java.lang.String) getClosestKeys(java.util.Locale, java.lang.String)}. 175 * 176 * @param locale the desired Locale 177 * @return String with LocalizedString's value for 'locale' or for the 1st 178 * alternate Locale found. Null if nothing found. 179 */ 180 public String getClosestValue() throws JAXRException { 181 return getClosestValue(Locale.getDefault()); 182 } 183 184 /*** 185 * Gets the localized value of an InternationalString for a given Locale or 186 * the closest match, according to a precedence list (see {@link 187 * #getClosestKeys(java.util.Locale, java.lang.String) getClosestKeys( 188 * java.util.Locale, java.lang.String)}. 189 * 190 * @param locale the desired Locale 191 * @return String with LocalizedString's value for 'locale' or for the 1st 192 * alternate Locale found. Null if nothing found. 193 */ 194 public String getClosestValue(Locale locale) throws JAXRException { 195 LocalizedString lString = getClosestLocalizedString(locale, null); 196 if (lString != null) { 197 return lString.getValue(); 198 } 199 return null; 200 } 201 202 203 /*** 204 * Gets a List of possible keys to be used when searching for the closest 205 * LocalizedString for a given locale/charset pair. The List starts with a 206 * key for the specific locale (ignored if null) followed by a lookup 207 * precedence list of alternative keys (inspired on 208 * <code>java.util.ResourceBundle</code>) as shown bellow. 209 * 210 * <ul> 211 * <li> charset + "_" + language1 + "_" + country1 + "_" + variant1 </li> 212 * <li> charset + "_" + language1 + "_" + country1 </li> 213 * <li> charset + "_" + language1 </li> 214 * <li> charset + "_" + language2 + "_" + country2 + "_" + variant2 </li> 215 * <li> charset + "_" + language2 + "_" + country2 </li> 216 * <li> charset + "_" + language2 </li> 217 * <li> charset + "_" + language3 + "_" + country3 + "_" + variant3 </li> 218 * <li> charset + "_" + language3 + "_" + country3 </li> 219 * <li> charset + "_" + language3 </li> 220 * </ul> 221 * 222 * Where: 223 * <ul> 224 * <li> Candidate keys where the final component is an empty string are omitted. 225 * For example, if country1 is an empty string, charset + "_" + language1 + 226 * "_" + country1 is omitted; </li> 227 * <li> language1, contry1, variant1 come from the 'locale' parameter;</li> 228 * <li> language2, contry2, variant2 come from JVM default Locale (Locale.getDefault());</li> 229 * <li> language3, contry3, variant3 come from ebxmlrr default Locale (default "en-US");</li> 230 * </ul> 231 * 232 * @param charset the charset name 233 * @param locale the locale 234 * @return List with the keys along the search path. 235 */ 236 public static List getClosestKeys(Locale locale, String charset) { 237 // TO DO: Cache generated ClosestKeysLists (??) 238 // TO DO: get it from ProviderProperties 239 Locale ebxmlrrDefaultLocale = new Locale("en", "US"); 240 charset = LocalizedStringImpl.DEFAULT_CHARSET_NAME; 241 List keysList = calculateKeys(locale, charset); 242 if (!locale.equals(Locale.getDefault())) { 243 keysList.addAll(calculateKeys(Locale.getDefault(), charset)); 244 } if (!Locale.getDefault().equals(ebxmlrrDefaultLocale) 245 && !locale.equals(ebxmlrrDefaultLocale)) { 246 // TO DO: Decide if default locale should be expanded, too. 247 keysList.addAll(calculateKeys(ebxmlrrDefaultLocale, charset)); 248 } 249 return keysList; 250 } 251 252 /*** 253 * Calculate the possible keys List for a locale/charset pair. Candidate keys 254 * are: 255 * <ul> 256 * <li> charset + "_" + language + "_" + country + "_" + variant 257 * <li> charset + "_" + language + "_" + country 258 * <li> charset + "_" + language 259 * </ul> 260 * 261 * Candidate keys where the final component is an empty string are omitted. 262 * For example, if country is an empty string, the second candidate key is omitted. 263 * 264 * @param charset the charset name 265 * @param locale the locale 266 * @return List with the keys along the search path. 267 */ 268 private static List calculateKeys(Locale locale, String charset) { 269 final ArrayList result = new ArrayList(3); 270 final String language = locale.getLanguage(); 271 final int languageLength = language.length(); 272 final String country = locale.getCountry(); 273 final int countryLength = country.length(); 274 final String variant = locale.getVariant(); 275 final int variantLength = variant.length(); 276 277 if (languageLength + countryLength + variantLength == 0) { 278 //The locale is "", "", "". 279 return result; 280 } 281 final StringBuffer temp = new StringBuffer(charset); 282 temp.append('_'); 283 temp.append(language); 284 if (languageLength > 0) { 285 result.add(0, temp.toString()); 286 } 287 288 if (countryLength + variantLength == 0) { 289 return result; 290 } 291 temp.append('_'); 292 temp.append(country); 293 if (countryLength > 0) { 294 result.add(0, temp.toString()); 295 } 296 297 if (variantLength == 0) { 298 return result; 299 } 300 temp.append('_'); 301 temp.append(variant); 302 result.add(0, temp.toString()); 303 304 return result; 305 } 306 307 protected void setBindingObject(org.oasis.ebxml.registry.bindings.rim.InternationalStringType ebIS) throws JAXRException { 308 309 Iterator iter = getLocalizedStrings().iterator(); 310 while (iter.hasNext()) { 311 LocalizedStringImpl ls = (LocalizedStringImpl)iter.next(); 312 org.oasis.ebxml.registry.bindings.rim.LocalizedString ebLS = ls.toBindingObject(); 313 org.oasis.ebxml.registry.bindings.rim.InternationalStringTypeItem item = 314 new org.oasis.ebxml.registry.bindings.rim.InternationalStringTypeItem(); 315 item.setLocalizedString(ebLS); 316 ebIS.addInternationalStringTypeItem(item); 317 } 318 } 319 320 public Object clone() { 321 InternationalStringImpl _clone = null; 322 323 try { 324 _clone = new InternationalStringImpl(lcm); 325 _clone.addLocalizedStrings(this.getLocalizedStrings()); 326 } 327 catch (JAXRException e) { 328 //Cannot happen. 329 e.printStackTrace(); 330 } 331 332 return _clone; 333 } 334 335 public String toString() { 336 String str = super.toString(); 337 try { 338 str = getClosestValue(); 339 } 340 catch (Exception e) { 341 log.warn(e); 342 } 343 return str; 344 } 345 346 }

This page was automatically generated by Maven