1 16 package org.apache.cocoon.portal.pluto.om.common; 17 18 import java.util.Collection ; 19 import java.util.Iterator ; 20 import java.util.Locale ; 21 import java.util.MissingResourceException ; 22 import java.util.ResourceBundle ; 23 import java.util.Vector ; 24 25 import org.apache.pluto.om.common.Language; 26 import org.apache.pluto.om.common.LanguageSet; 27 import org.apache.pluto.util.StringUtils; 28 29 36 public class LanguageSetImpl extends AbstractSupportSet implements LanguageSet, java.io.Serializable , Support 37 { 38 39 private String castorKeywords; 40 41 private ClassLoader classLoader; 42 43 46 private Vector locales; 47 private boolean resourceBundleInitialized; 48 49 private String resources; 50 private String shortTitle; 51 52 private String title; 53 54 public LanguageSetImpl() { 55 locales = new Vector (); 56 } 57 58 private Language createLanguage(Locale locale, ResourceBundle bundle) { 60 LanguageImpl lang = new LanguageImpl(locale, bundle, title, shortTitle, castorKeywords); 61 62 return lang; 63 } 64 65 87 88 91 public Language get(Locale locale) { 92 if (resources != null && !resourceBundleInitialized) { 93 initResourceBundle(); 94 this.resourceBundleInitialized = true; 95 } 96 97 if (!locales.contains(locale)) { 98 locale = matchLocale(locale); 99 } 100 101 Iterator iterator = this.iterator(); 102 while (iterator.hasNext()) { 103 Language language = (Language)iterator.next(); 104 if (language.getLocale().equals(locale) || size()==1) { 105 return language; 106 } 107 } 108 109 return null; 110 } 111 112 115 public Iterator getLocales() { 116 return locales.iterator(); 117 } 118 119 122 public Locale getDefaultLocale() { 123 Locale defLoc = null; 124 125 if (locales != null && locales.size() > 0) { 126 defLoc = (Locale )locales.firstElement(); 127 128 if (defLoc == null) { 129 defLoc = new Locale ("en",""); 130 locales.add(defLoc); 131 } 132 } else { 133 defLoc = new Locale ("en",""); 134 locales.add(defLoc); 135 } 136 137 return defLoc; 138 } 139 140 143 public void postBuild(Object parameter) throws Exception { 144 } 146 147 150 public void postLoad(Object parameter) throws Exception { 151 locales.addAll((Collection )parameter); 152 initInlinedInfos(); 153 } 154 155 158 public void postStore(Object parameter) throws Exception { 159 } 161 162 165 public void preBuild(Object parameter) throws Exception { 166 } 168 169 172 public void preStore(Object parameter) throws Exception { 173 } 175 176 177 179 private void initInlinedInfos() throws Exception { 180 183 if (locales.isEmpty()) { 184 getDefaultLocale(); } 186 if (castorKeywords == null) { 187 castorKeywords=""; 188 } 189 if (shortTitle == null) { 190 shortTitle=""; 191 } 192 if (title == null) { 193 title=""; 194 } 195 add(createLanguage(getDefaultLocale(), null)); 196 } 197 198 private void initResourceBundle() { 200 Iterator iter = locales.iterator(); 201 while (iter.hasNext()) { 202 Locale locale = (Locale )iter.next(); 203 ResourceBundle bundle = null; 204 bundle = loadResourceBundle(locale); 205 if (bundle != null) { 206 227 228 Language language = createLanguage(locale, bundle); 229 remove(language); 230 add(language); 231 } 232 } 233 } 234 235 private Locale matchLocale(Locale locale) { 237 238 String variant = locale.getVariant(); 239 if (variant != null && variant.length() > 0) { 240 locale = new Locale (locale.getLanguage(), locale.getCountry()); 241 } 242 243 if (! locales.contains(locale)) { 244 String country = locale.getCountry(); 245 if (country != null && country.length() > 0) { 246 locale = new Locale (locale.getLanguage(), ""); 247 } 248 } 249 250 if (! locales.contains(locale)) { 251 locale = getDefaultLocale(); 252 } 253 254 return locale; 255 } 256 257 259 public String getCastorKeywords() { 260 return this.castorKeywords; 261 } 262 263 265 public String getResources() { 266 return resources; 267 } 268 269 public String getShortTitle() { 270 return this.shortTitle; 271 } 272 273 public String getTitle() { 275 return this.title; 276 } 277 278 protected ResourceBundle loadResourceBundle(Locale locale) { 280 ResourceBundle resourceBundle = null; 281 try { 282 if (classLoader != null) { 283 resourceBundle=ResourceBundle.getBundle(resources, locale, classLoader); 284 } else { 285 resourceBundle=ResourceBundle.getBundle(resources, locale, Thread.currentThread().getContextClassLoader()); 286 } 287 } catch (MissingResourceException x) { 288 return null; 289 } 290 return resourceBundle; 291 } 292 293 public void setCastorKeywords(String keywords) { 294 this.castorKeywords = keywords; 295 } 296 298 299 public void setClassLoader(ClassLoader loader) { 300 this.classLoader = loader; 301 } 302 303 public void setResources(String resources) { 304 this.resources = resources; 305 } 306 307 public void setShortTitle(String shortTitle) { 308 this.shortTitle = shortTitle; 309 } 310 311 public void setTitle(String title) { 312 this.title = title; 313 } 314 315 318 public String toString() { 319 return toString(0); 320 } 321 322 public String toString(int indent) { 323 StringBuffer buffer = new StringBuffer (50); 324 StringUtils.newLine(buffer,indent); 325 buffer.append(getClass().toString()); 326 buffer.append(": "); 327 Iterator iterator = this.iterator(); 328 while (iterator.hasNext()) 329 { 330 buffer.append(((LanguageImpl)iterator.next()).toString(indent+2)); 331 } 332 return buffer.toString(); 333 } 334 } 335 | Popular Tags |