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