KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > pluto > om > common > LanguageSetImpl


1 /*
2  * Copyright 2004,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 package org.apache.cocoon.portal.pluto.om.common;
17
18 import java.util.Collection JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Locale JavaDoc;
21 import java.util.MissingResourceException JavaDoc;
22 import java.util.ResourceBundle JavaDoc;
23 import java.util.Vector JavaDoc;
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 /**
30  *
31  *
32  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
33  *
34  * @version CVS $Id: LanguageSetImpl.java 123407 2004-12-27 13:51:59Z cziegeler $
35  */

36 public class LanguageSetImpl extends AbstractSupportSet implements LanguageSet, java.io.Serializable JavaDoc, Support
37 {
38
39     private String JavaDoc castorKeywords;
40
41     private ClassLoader JavaDoc classLoader;
42
43     /**
44      * contains Locale objects for locales supported by the portlet
45      */

46     private Vector JavaDoc locales;
47     private boolean resourceBundleInitialized;
48
49     private String JavaDoc resources;
50     private String JavaDoc shortTitle;
51
52     private String JavaDoc title;
53
54     public LanguageSetImpl() {
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         LanguageImpl lang = new LanguageImpl(locale, bundle, title, shortTitle, castorKeywords);
61
62         return lang;
63     }
64
65     /* never used
66     // creates a locale object from a string representation
67     private Locale createLocale(String locale)
68     {
69         // parse locale String
70         StringTokenizer tokenizer = new StringTokenizer(locale,"_");
71         String[] localeDef = new String[5]; // just in case we have more than one variant
72         for (int i = 0; i < localeDef.length; i++)
73         {
74             if (tokenizer.hasMoreTokens())
75             {
76                 localeDef[i] = tokenizer.nextToken();
77             }
78             else
79             {
80                 localeDef[i] = "";
81             }
82         }
83
84         return new java.util.Locale(localeDef[0], localeDef[1], localeDef[2]+localeDef[3]+localeDef[4]);
85     }
86     */

87
88     /* (non-Javadoc)
89      * @see org.apache.pluto.om.common.LanguageSet#get(java.util.Locale)
90      */

91     public Language get(Locale JavaDoc 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 JavaDoc 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     /* (non-Javadoc)
113      * @see org.apache.pluto.om.common.LanguageSet#getLocales()
114      */

115     public Iterator JavaDoc getLocales() {
116         return locales.iterator();
117     }
118
119     /* (non-Javadoc)
120      * @see org.apache.pluto.om.common.LanguageSet#getDefaultLocale()
121      */

122     public Locale JavaDoc getDefaultLocale() {
123         Locale JavaDoc defLoc = null;
124
125         if (locales != null && locales.size() > 0) {
126             defLoc = (Locale JavaDoc)locales.firstElement();
127
128             if (defLoc == null) {
129                 defLoc = new Locale JavaDoc("en","");
130                 locales.add(defLoc);
131             }
132         } else {
133             defLoc = new Locale JavaDoc("en","");
134             locales.add(defLoc);
135         }
136
137         return defLoc;
138     }
139
140     /* (non-Javadoc)
141      * @see org.apache.cocoon.portal.pluto.om.common.Support#postBuild(java.lang.Object)
142      */

143     public void postBuild(Object JavaDoc parameter) throws Exception JavaDoc {
144         // nothing to do
145
}
146
147     /* (non-Javadoc)
148      * @see org.apache.cocoon.portal.pluto.om.common.Support#postLoad(java.lang.Object)
149      */

150     public void postLoad(Object JavaDoc parameter) throws Exception JavaDoc {
151         locales.addAll((Collection JavaDoc)parameter);
152         initInlinedInfos();
153     }
154
155     /* (non-Javadoc)
156      * @see org.apache.cocoon.portal.pluto.om.common.Support#postStore(java.lang.Object)
157      */

158     public void postStore(Object JavaDoc parameter) throws Exception JavaDoc {
159         // nothing to do
160
}
161
162     /* (non-Javadoc)
163      * @see org.apache.cocoon.portal.pluto.om.common.Support#preBuild(java.lang.Object)
164      */

165     public void preBuild(Object JavaDoc parameter) throws Exception JavaDoc {
166         // nothing to do
167
}
168
169     /* (non-Javadoc)
170      * @see org.apache.cocoon.portal.pluto.om.common.Support#preStore(java.lang.Object)
171      */

172     public void preStore(Object JavaDoc parameter) throws Exception JavaDoc {
173         // nothing to do
174
}
175
176
177     // internal methods.
178

179     private void initInlinedInfos() throws Exception JavaDoc {
180         // if resource-bundle is given
181
// must be initialized later when classloader is known by initResourceBundle()
182

183         if (locales.isEmpty()) {
184             getDefaultLocale(); //the defualt gets automaticaly added to the locals
185
}
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     // create and add all resource bundle information as Language objects to this set
199
private void initResourceBundle() {
200         Iterator JavaDoc iter = locales.iterator();
201         while (iter.hasNext()) {
202             Locale JavaDoc locale = (Locale JavaDoc)iter.next();
203             ResourceBundle JavaDoc bundle = null;
204             bundle = loadResourceBundle(locale);
205             if (bundle != null) {
206                 /*String title;
207                 String shortTitle;
208                 String keywords;
209                 
210                 try {
211                     title=bundle.getString("javax.portlet.title");
212                 } catch(MissingResourceException x) {
213                     title = this.title;
214                 }
215                 
216                 try {
217                     shortTitle=bundle.getString("javax.portlet.short-title");
218                 } catch(MissingResourceException x) {
219                     shortTitle = this.shortTitle;
220                 }
221
222                 try {
223                     keywords=bundle.getString("javax.portlet.keywords");
224                 } catch(MissingResourceException x) {
225                     keywords = this.castorKeywords;
226                 }*/

227
228                 Language language = createLanguage(locale, bundle);
229                 remove(language);
230                 add(language);
231             }
232         }
233     }
234
235     // try to match the given locale to a supported locale
236
private Locale JavaDoc matchLocale(Locale JavaDoc locale) {
237
238         String JavaDoc variant = locale.getVariant();
239         if (variant != null && variant.length() > 0) {
240             locale = new Locale JavaDoc(locale.getLanguage(), locale.getCountry());
241         }
242
243         if (! locales.contains(locale)) {
244             String JavaDoc country = locale.getCountry();
245             if (country != null && country.length() > 0) {
246                 locale = new Locale JavaDoc(locale.getLanguage(), "");
247             }
248         }
249
250         if (! locales.contains(locale)) {
251             locale = getDefaultLocale();
252         }
253
254         return locale;
255     }
256
257     // additional methods.
258

259     public String JavaDoc getCastorKeywords() {
260         return this.castorKeywords;
261     }
262
263     // additional methods
264

265     public String JavaDoc getResources() {
266         return resources;
267     }
268
269     public String JavaDoc getShortTitle() {
270         return this.shortTitle;
271     }
272
273     // internal methods used by castor
274
public String JavaDoc getTitle() {
275         return this.title;
276     }
277
278     // loads resource bundle files from WEB-INF/classes directory
279
protected ResourceBundle JavaDoc loadResourceBundle(Locale JavaDoc locale) {
280         ResourceBundle JavaDoc 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 JavaDoc x) {
288             return null;
289         }
290         return resourceBundle;
291     }
292
293     public void setCastorKeywords(String JavaDoc keywords) {
294         this.castorKeywords = keywords;
295     }
296     // end castor methods
297

298
299     public void setClassLoader(ClassLoader JavaDoc loader) {
300         this.classLoader = loader;
301     }
302
303     public void setResources(String JavaDoc resources) {
304         this.resources = resources;
305     }
306
307     public void setShortTitle(String JavaDoc shortTitle) {
308         this.shortTitle = shortTitle;
309     }
310
311     public void setTitle(String JavaDoc title) {
312         this.title = title;
313     }
314
315     /* (non-Javadoc)
316      * @see java.lang.Object#toString()
317      */

318     public String JavaDoc toString() {
319         return toString(0);
320     }
321
322     public String JavaDoc toString(int indent) {
323         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(50);
324         StringUtils.newLine(buffer,indent);
325         buffer.append(getClass().toString());
326         buffer.append(": ");
327         Iterator JavaDoc 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