KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > pluto > portalImpl > om > common > impl > LanguageSetImpl


1 /*
2  * Copyright 2003,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17
18  */

19
20 package org.apache.pluto.portalImpl.om.common.impl;
21
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Locale JavaDoc;
25 import java.util.MissingResourceException JavaDoc;
26 import java.util.ResourceBundle JavaDoc;
27 import java.util.Vector JavaDoc;
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 JavaDoc, Support
36 {
37
38     private String JavaDoc castorKeywords;
39
40     private ClassLoader JavaDoc classLoader;
41
42     /**
43      * contains Locale objects for locales supported by the portlet
44      */

45     private Vector JavaDoc locales;
46     private boolean resourceBundleInitialized;
47
48     private String JavaDoc resources;
49     private String JavaDoc shortTitle;
50
51     private String JavaDoc title;
52
53     public LanguageSetImpl()
54     {
55         locales = new Vector JavaDoc();
56     }
57
58     // create Language object with data from this class (title, short-title, description, keywords)
59
private Language createLanguage(Locale JavaDoc locale, ResourceBundle JavaDoc bundle)
60     {
61         LanguageImpl lang = new LanguageImpl(locale, bundle, title, shortTitle, castorKeywords);
62         return lang;
63     }
64
65     // AbstractSupportSet implementation.
66

67     public Language get(Locale JavaDoc 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 JavaDoc 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 JavaDoc getLocales()
94     {
95         return locales.iterator();
96     }
97
98     public Locale JavaDoc getDefaultLocale()
99     {
100         Locale JavaDoc defLoc = null;
101
102         if (locales != null && locales.size() > 0)
103         {
104             defLoc = (Locale JavaDoc)locales.firstElement();
105
106             if (defLoc == null)
107             {
108                 defLoc = new Locale JavaDoc("en","");
109                 locales.add(defLoc);
110             }
111         }
112         else
113         {
114             defLoc = new Locale JavaDoc("en","");
115             locales.add(defLoc);
116         }
117
118         return defLoc;
119     }
120
121     // Support implementation.
122

123     public void postBuild(Object JavaDoc parameter) throws Exception JavaDoc
124     {
125     }
126
127
128     public void postLoad(Object JavaDoc parameter) throws Exception JavaDoc
129     {
130         locales.addAll((Collection JavaDoc)parameter);
131         initInlinedInfos();
132         if ( resources != null )
133             initResourceBundle();
134     }
135
136     public void postStore(Object JavaDoc parameter) throws Exception JavaDoc
137     {
138     }
139
140     public void preBuild(Object JavaDoc parameter) throws Exception JavaDoc
141     {
142     }
143
144     public void preStore(Object JavaDoc parameter) throws Exception JavaDoc
145     {
146     }
147
148
149     // internal methods.
150

151     private void initInlinedInfos() throws Exception JavaDoc
152     {
153         // if resource-bundle is given
154
// must be initialized later when classloader is known by initResourceBundle()
155

156         if (locales.isEmpty())
157         {
158             getDefaultLocale(); //the defualt gets automaticaly added to the locals
159
}
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     // create and add all resource bundle information as Language objects to this set
176
private void initResourceBundle()
177     {
178         Iterator JavaDoc iter = locales.iterator();
179         while (iter.hasNext())
180         {
181             Locale JavaDoc locale = (Locale JavaDoc)iter.next();
182             ResourceBundle JavaDoc 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     // try to match the given locale to a supported locale
194
private Locale JavaDoc matchLocale(Locale JavaDoc locale)
195     {
196
197         String JavaDoc variant = locale.getVariant();
198         if (variant != null && variant.length() > 0)
199         {
200             locale = new Locale JavaDoc(locale.getLanguage(), locale.getCountry());
201         }
202
203         if (! locales.contains(locale))
204         {
205             String JavaDoc country = locale.getCountry();
206             if (country != null && country.length() > 0)
207             {
208                 locale = new Locale JavaDoc(locale.getLanguage(), "");
209             }
210         }
211
212         if (! locales.contains(locale))
213         {
214             locale = getDefaultLocale();
215         }
216
217         return locale;
218     }
219
220     // additional methods.
221

222     public String JavaDoc getCastorKeywords()
223     {
224         return this.castorKeywords;
225     }
226
227     // additional methods
228

229     public String JavaDoc getResources()
230     {
231         return resources;
232     }
233
234     public String JavaDoc getShortTitle()
235     {
236         return this.shortTitle;
237     }
238
239     // internal methods used by castor
240
public String JavaDoc getTitle()
241     {
242         return this.title;
243     }
244
245     // loads resource bundle files from WEB-INF/classes directory
246
protected ResourceBundle JavaDoc loadResourceBundle(Locale JavaDoc locale)
247     {
248         ResourceBundle JavaDoc 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 JavaDoc x)
261         {
262             return null;
263         }
264         return resourceBundle;
265     }
266
267     public void setCastorKeywords(String JavaDoc keywords)
268     {
269         this.castorKeywords = keywords;
270     }
271     // end castor methods
272

273
274     public void setClassLoader(ClassLoader JavaDoc loader)
275     {
276         this.classLoader = loader;
277     }
278
279     public void setResources(String JavaDoc resources)
280     {
281         this.resources = resources;
282     }
283
284     public void setShortTitle(String JavaDoc shortTitle)
285     {
286         this.shortTitle = shortTitle;
287     }
288
289     public void setTitle(String JavaDoc title)
290     {
291         this.title = title;
292     }
293
294     public String JavaDoc toString()
295     {
296         return toString(0);
297     }
298
299     public String JavaDoc toString(int indent)
300     {
301         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(50);
302         StringUtils.newLine(buffer,indent);
303         buffer.append(getClass().toString());
304         buffer.append(": ");
305         Iterator JavaDoc 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