1 16 19 20 package org.apache.pluto.portalImpl.om.common.impl; 21 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 import java.util.Locale ; 25 import java.util.MissingResourceException ; 26 import java.util.ResourceBundle ; 27 import java.util.Vector ; 28 29 import org.apache.pluto.om.common.Language; 30 import org.apache.pluto.om.common.LanguageSet; 31 import org.apache.pluto.portalImpl.om.common.AbstractSupportSet; 32 import org.apache.pluto.portalImpl.om.common.Support; 33 import org.apache.pluto.util.StringUtils; 34 35 public class LanguageSetImpl extends AbstractSupportSet implements LanguageSet, java.io.Serializable , Support 36 { 37 38 private String castorKeywords; 39 40 private ClassLoader classLoader; 41 42 45 private Vector locales; 46 private boolean resourceBundleInitialized; 47 48 private String resources; 49 private String shortTitle; 50 51 private String title; 52 53 public LanguageSetImpl() 54 { 55 locales = new Vector (); 56 } 57 58 private Language createLanguage(Locale locale, ResourceBundle bundle) 60 { 61 LanguageImpl lang = new LanguageImpl(locale, bundle, title, shortTitle, castorKeywords); 62 return lang; 63 } 64 65 67 public Language get(Locale locale) 68 { 69 if (resources!=null && resourceBundleInitialized==false) 70 { 71 initResourceBundle(); 72 this.resourceBundleInitialized = true; 73 } 74 75 if (!locales.contains(locale)) 76 { 77 locale = matchLocale(locale); 78 } 79 80 Iterator iterator = this.iterator(); 81 while (iterator.hasNext()) 82 { 83 Language language = (Language)iterator.next(); 84 if (language.getLocale().equals(locale) || size()==1) 85 { 86 return language; 87 } 88 } 89 90 return null; 91 } 92 93 public Iterator getLocales() 94 { 95 return locales.iterator(); 96 } 97 98 public Locale getDefaultLocale() 99 { 100 Locale defLoc = null; 101 102 if (locales != null && locales.size() > 0) 103 { 104 defLoc = (Locale )locales.firstElement(); 105 106 if (defLoc == null) 107 { 108 defLoc = new Locale ("en",""); 109 locales.add(defLoc); 110 } 111 } 112 else 113 { 114 defLoc = new Locale ("en",""); 115 locales.add(defLoc); 116 } 117 118 return defLoc; 119 } 120 121 123 public void postBuild(Object parameter) throws Exception 124 { 125 } 126 127 128 public void postLoad(Object parameter) throws Exception 129 { 130 locales.addAll((Collection )parameter); 131 initInlinedInfos(); 132 if ( resources != null ) 133 initResourceBundle(); 134 } 135 136 public void postStore(Object parameter) throws Exception 137 { 138 } 139 140 public void preBuild(Object parameter) throws Exception 141 { 142 } 143 144 public void preStore(Object parameter) throws Exception 145 { 146 } 147 148 149 151 private void initInlinedInfos() throws Exception 152 { 153 156 if (locales.isEmpty()) 157 { 158 getDefaultLocale(); } 160 if (castorKeywords == null) 161 { 162 castorKeywords=""; 163 } 164 if (shortTitle == null) 165 { 166 shortTitle=""; 167 } 168 if (title == null) 169 { 170 title=""; 171 } 172 boolean added = add(createLanguage(getDefaultLocale(), null)); 173 } 174 175 private void initResourceBundle() 177 { 178 Iterator iter = locales.iterator(); 179 while (iter.hasNext()) 180 { 181 Locale locale = (Locale )iter.next(); 182 ResourceBundle bundle = null; 183 bundle = loadResourceBundle(locale); 184 if (bundle != null) 185 { 186 Language language = createLanguage(locale, bundle); 187 remove(language); 188 add(language); 189 } 190 } 191 } 192 193 private Locale matchLocale(Locale locale) 195 { 196 197 String variant = locale.getVariant(); 198 if (variant != null && variant.length() > 0) 199 { 200 locale = new Locale (locale.getLanguage(), locale.getCountry()); 201 } 202 203 if (! locales.contains(locale)) 204 { 205 String country = locale.getCountry(); 206 if (country != null && country.length() > 0) 207 { 208 locale = new Locale (locale.getLanguage(), ""); 209 } 210 } 211 212 if (! locales.contains(locale)) 213 { 214 locale = getDefaultLocale(); 215 } 216 217 return locale; 218 } 219 220 222 public String getCastorKeywords() 223 { 224 return this.castorKeywords; 225 } 226 227 229 public String getResources() 230 { 231 return resources; 232 } 233 234 public String getShortTitle() 235 { 236 return this.shortTitle; 237 } 238 239 public String getTitle() 241 { 242 return this.title; 243 } 244 245 protected ResourceBundle loadResourceBundle(Locale locale) 247 { 248 ResourceBundle resourceBundle = null; 249 try 250 { 251 if (classLoader != null) 252 { 253 resourceBundle=ResourceBundle.getBundle(resources, locale, classLoader); 254 } 255 else 256 { 257 resourceBundle=ResourceBundle.getBundle(resources, locale, Thread.currentThread().getContextClassLoader()); 258 } 259 } 260 catch (MissingResourceException x) 261 { 262 return null; 263 } 264 return resourceBundle; 265 } 266 267 public void setCastorKeywords(String keywords) 268 { 269 this.castorKeywords = keywords; 270 } 271 273 274 public void setClassLoader(ClassLoader loader) 275 { 276 this.classLoader = loader; 277 } 278 279 public void setResources(String resources) 280 { 281 this.resources = resources; 282 } 283 284 public void setShortTitle(String shortTitle) 285 { 286 this.shortTitle = shortTitle; 287 } 288 289 public void setTitle(String title) 290 { 291 this.title = title; 292 } 293 294 public String toString() 295 { 296 return toString(0); 297 } 298 299 public String toString(int indent) 300 { 301 StringBuffer buffer = new StringBuffer (50); 302 StringUtils.newLine(buffer,indent); 303 buffer.append(getClass().toString()); 304 buffer.append(": "); 305 Iterator iterator = this.iterator(); 306 while (iterator.hasNext()) 307 { 308 buffer.append(((LanguageImpl)iterator.next()).toString(indent+2)); 309 } 310 return buffer.toString(); 311 } 312 } 313 | Popular Tags |