001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.portal.pluto.om.common;
018:
019: import java.util.Collection;
020: import java.util.Iterator;
021: import java.util.Locale;
022: import java.util.MissingResourceException;
023: import java.util.ResourceBundle;
024: import java.util.Vector;
025:
026: import org.apache.pluto.om.common.Language;
027: import org.apache.pluto.om.common.LanguageSet;
028: import org.apache.pluto.util.StringUtils;
029:
030: /**
031: *
032: *
033: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
034: *
035: * @version CVS $Id: LanguageSetImpl.java 433543 2006-08-22 06:22:54Z crossley $
036: */
037: public class LanguageSetImpl extends AbstractSupportSet implements
038: LanguageSet, java.io.Serializable, Support {
039:
040: private String castorKeywords;
041:
042: private ClassLoader classLoader;
043:
044: /**
045: * contains Locale objects for locales supported by the portlet
046: */
047: private Vector locales;
048: private boolean resourceBundleInitialized;
049:
050: private String resources;
051: private String shortTitle;
052:
053: private String title;
054:
055: public LanguageSetImpl() {
056: locales = new Vector();
057: }
058:
059: // create Language object with data from this class (title, short-title, description, keywords)
060: private Language createLanguage(Locale locale, ResourceBundle bundle) {
061: LanguageImpl lang = new LanguageImpl(locale, bundle, title,
062: shortTitle, castorKeywords);
063:
064: return lang;
065: }
066:
067: /* never used
068: // creates a locale object from a string representation
069: private Locale createLocale(String locale)
070: {
071: // parse locale String
072: StringTokenizer tokenizer = new StringTokenizer(locale,"_");
073: String[] localeDef = new String[5]; // just in case we have more than one variant
074: for (int i = 0; i < localeDef.length; i++)
075: {
076: if (tokenizer.hasMoreTokens())
077: {
078: localeDef[i] = tokenizer.nextToken();
079: }
080: else
081: {
082: localeDef[i] = "";
083: }
084: }
085:
086: return new java.util.Locale(localeDef[0], localeDef[1], localeDef[2]+localeDef[3]+localeDef[4]);
087: }
088: */
089:
090: /* (non-Javadoc)
091: * @see org.apache.pluto.om.common.LanguageSet#get(java.util.Locale)
092: */
093: public Language get(Locale locale) {
094: if (resources != null && !resourceBundleInitialized) {
095: initResourceBundle();
096: this .resourceBundleInitialized = true;
097: }
098:
099: if (!locales.contains(locale)) {
100: locale = matchLocale(locale);
101: }
102:
103: Iterator iterator = this .iterator();
104: while (iterator.hasNext()) {
105: Language language = (Language) iterator.next();
106: if (language.getLocale().equals(locale) || size() == 1) {
107: return language;
108: }
109: }
110:
111: return null;
112: }
113:
114: /* (non-Javadoc)
115: * @see org.apache.pluto.om.common.LanguageSet#getLocales()
116: */
117: public Iterator getLocales() {
118: return locales.iterator();
119: }
120:
121: /* (non-Javadoc)
122: * @see org.apache.pluto.om.common.LanguageSet#getDefaultLocale()
123: */
124: public Locale getDefaultLocale() {
125: Locale defLoc = null;
126:
127: if (locales != null && locales.size() > 0) {
128: defLoc = (Locale) locales.firstElement();
129:
130: if (defLoc == null) {
131: defLoc = new Locale("en", "");
132: locales.add(defLoc);
133: }
134: } else {
135: defLoc = new Locale("en", "");
136: locales.add(defLoc);
137: }
138:
139: return defLoc;
140: }
141:
142: /* (non-Javadoc)
143: * @see org.apache.cocoon.portal.pluto.om.common.Support#postBuild(java.lang.Object)
144: */
145: public void postBuild(Object parameter) throws Exception {
146: // nothing to do
147: }
148:
149: /* (non-Javadoc)
150: * @see org.apache.cocoon.portal.pluto.om.common.Support#postLoad(java.lang.Object)
151: */
152: public void postLoad(Object parameter) throws Exception {
153: locales.addAll((Collection) parameter);
154: initInlinedInfos();
155: }
156:
157: /* (non-Javadoc)
158: * @see org.apache.cocoon.portal.pluto.om.common.Support#postStore(java.lang.Object)
159: */
160: public void postStore(Object parameter) throws Exception {
161: // nothing to do
162: }
163:
164: /* (non-Javadoc)
165: * @see org.apache.cocoon.portal.pluto.om.common.Support#preBuild(java.lang.Object)
166: */
167: public void preBuild(Object parameter) throws Exception {
168: // nothing to do
169: }
170:
171: /* (non-Javadoc)
172: * @see org.apache.cocoon.portal.pluto.om.common.Support#preStore(java.lang.Object)
173: */
174: public void preStore(Object parameter) throws Exception {
175: // nothing to do
176: }
177:
178: // internal methods.
179:
180: private void initInlinedInfos() throws Exception {
181: // if resource-bundle is given
182: // must be initialized later when classloader is known by initResourceBundle()
183:
184: if (locales.isEmpty()) {
185: getDefaultLocale(); //the defualt gets automaticaly added to the locals
186: }
187: if (castorKeywords == null) {
188: castorKeywords = "";
189: }
190: if (shortTitle == null) {
191: shortTitle = "";
192: }
193: if (title == null) {
194: title = "";
195: }
196: add(createLanguage(getDefaultLocale(), null));
197: }
198:
199: // create and add all resource bundle information as Language objects to this set
200: private void initResourceBundle() {
201: Iterator iter = locales.iterator();
202: while (iter.hasNext()) {
203: Locale locale = (Locale) iter.next();
204: ResourceBundle bundle = null;
205: bundle = loadResourceBundle(locale);
206: if (bundle != null) {
207: /*String title;
208: String shortTitle;
209: String keywords;
210:
211: try {
212: title=bundle.getString("javax.portlet.title");
213: } catch(MissingResourceException x) {
214: title = this.title;
215: }
216:
217: try {
218: shortTitle=bundle.getString("javax.portlet.short-title");
219: } catch(MissingResourceException x) {
220: shortTitle = this.shortTitle;
221: }
222:
223: try {
224: keywords=bundle.getString("javax.portlet.keywords");
225: } catch(MissingResourceException x) {
226: keywords = this.castorKeywords;
227: }*/
228:
229: Language language = createLanguage(locale, bundle);
230: remove(language);
231: add(language);
232: }
233: }
234: }
235:
236: // try to match the given locale to a supported locale
237: private Locale matchLocale(Locale locale) {
238:
239: String variant = locale.getVariant();
240: if (variant != null && variant.length() > 0) {
241: locale = new Locale(locale.getLanguage(), locale
242: .getCountry());
243: }
244:
245: if (!locales.contains(locale)) {
246: String country = locale.getCountry();
247: if (country != null && country.length() > 0) {
248: locale = new Locale(locale.getLanguage(), "");
249: }
250: }
251:
252: if (!locales.contains(locale)) {
253: locale = getDefaultLocale();
254: }
255:
256: return locale;
257: }
258:
259: // additional methods.
260:
261: public String getCastorKeywords() {
262: return this .castorKeywords;
263: }
264:
265: // additional methods
266:
267: public String getResources() {
268: return resources;
269: }
270:
271: public String getShortTitle() {
272: return this .shortTitle;
273: }
274:
275: // internal methods used by castor
276: public String getTitle() {
277: return this .title;
278: }
279:
280: // loads resource bundle files from WEB-INF/classes directory
281: protected ResourceBundle loadResourceBundle(Locale locale) {
282: ResourceBundle resourceBundle = null;
283: try {
284: if (classLoader != null) {
285: resourceBundle = ResourceBundle.getBundle(resources,
286: locale, classLoader);
287: } else {
288: resourceBundle = ResourceBundle.getBundle(resources,
289: locale, Thread.currentThread()
290: .getContextClassLoader());
291: }
292: } catch (MissingResourceException x) {
293: return null;
294: }
295: return resourceBundle;
296: }
297:
298: public void setCastorKeywords(String keywords) {
299: this .castorKeywords = keywords;
300: }
301:
302: // end castor methods
303:
304: public void setClassLoader(ClassLoader loader) {
305: this .classLoader = loader;
306: }
307:
308: public void setResources(String resources) {
309: this .resources = resources;
310: }
311:
312: public void setShortTitle(String shortTitle) {
313: this .shortTitle = shortTitle;
314: }
315:
316: public void setTitle(String title) {
317: this .title = title;
318: }
319:
320: /* (non-Javadoc)
321: * @see java.lang.Object#toString()
322: */
323: public String toString() {
324: return toString(0);
325: }
326:
327: public String toString(int indent) {
328: StringBuffer buffer = new StringBuffer(50);
329: StringUtils.newLine(buffer, indent);
330: buffer.append(getClass().toString());
331: buffer.append(": ");
332: Iterator iterator = this .iterator();
333: while (iterator.hasNext()) {
334: buffer.append(((LanguageImpl) iterator.next())
335: .toString(indent + 2));
336: }
337: return buffer.toString();
338: }
339: }
|