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.jetspeed.om.impl;
018:
019: import java.io.Serializable;
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Collections;
023: import java.util.Enumeration;
024: import java.util.HashSet;
025: import java.util.Iterator;
026: import java.util.Locale;
027: import java.util.MissingResourceException;
028: import java.util.ResourceBundle;
029: import java.util.Set;
030: import java.util.StringTokenizer;
031:
032: import org.apache.commons.lang.StringUtils;
033: import org.apache.jetspeed.om.common.MutableLanguage;
034: import org.apache.jetspeed.util.HashCodeBuilder;
035: import org.apache.jetspeed.util.JetspeedLocale;
036: import org.apache.pluto.om.common.Language;
037:
038: /**
039: *
040: * LanguageImpl <br>
041: * Okay, base Language really has nothing to really do at all with language per
042: * se. It actually represents the locallized <code>title</code> and
043: * <code>short-title</code> attributes of a portlet's definition. It also
044: * contains a resource bundle for the specifc locale. <br>
045: * TODO: org.apache.pluto.om.common.Language should be seperated into TODO a
046: * Language class that just contains the resource bundle and TODO a Title class
047: * that contains a localized title and short title.
048: *
049: * @author <a href="mailto:weaver@apache.org">Scott T. Weaver </a>
050: * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
051: * @version $Id: LanguageImpl.java 516448 2007-03-09 16:25:47Z ate $
052: *
053: */
054: public class LanguageImpl extends ResourceBundle implements
055: MutableLanguage, Serializable {
056: public static final String JAVAX_PORTLET_KEYWORDS = "javax.portlet.keywords";
057: public static final String JAVAX_PORTLET_SHORT_TITLE = "javax.portlet.short-title";
058: public static final String JAVAX_PORTLET_TITLE = "javax.portlet.title";
059:
060: private Set keys;
061: private String title;
062: private String shortTitle;
063: private Locale locale;
064: private String keywordStr;
065: private Collection keywords;
066:
067: /**
068: * This field can be used by persistence tools for storing PK info Otherwise
069: * it has no effect on the functioning of the portal.
070: */
071: protected long id;
072:
073: protected long portletId;
074:
075: public LanguageImpl() {
076: keys = Collections.synchronizedSet(new HashSet(3));
077: keys.add(JAVAX_PORTLET_TITLE);
078: keys.add(JAVAX_PORTLET_SHORT_TITLE);
079: keys.add(JAVAX_PORTLET_KEYWORDS);
080: this .locale = JetspeedLocale.getDefaultLocale();
081: }
082:
083: public Enumeration getKeys() {
084: return Collections.enumeration(keys);
085: }
086:
087: protected Object handleGetObject(String key) {
088: if (key.equals(JAVAX_PORTLET_TITLE)) {
089: return getTitle();
090: } else if (key.equals(JAVAX_PORTLET_SHORT_TITLE)) {
091: return getShortTitle();
092: } else if (key.equals(JAVAX_PORTLET_KEYWORDS)) {
093: return getKeywordStr();
094: }
095: return null;
096: }
097:
098: private String getStringValue(ResourceBundle bundle, String key,
099: String defaultValue) {
100: String value = defaultValue;
101: try {
102: value = (String) bundle.getObject(key);
103: } catch (MissingResourceException mre) {
104: } catch (ClassCastException cce) {
105: }
106: return value;
107: }
108:
109: public void setResourceBundle(ResourceBundle bundle) {
110: if (parent == null && bundle != null) {
111: Enumeration parentKeys = bundle.getKeys();
112: while (parentKeys.hasMoreElements()) {
113: keys.add(parentKeys.nextElement());
114: }
115: setParent(bundle);
116: }
117: }
118:
119: public void loadDefaults() {
120: ResourceBundle bundle = getParentResourceBundle();
121: if (bundle != null) {
122: setTitle(getStringValue(bundle, JAVAX_PORTLET_TITLE,
123: getTitle()));
124: setShortTitle(getStringValue(bundle,
125: JAVAX_PORTLET_SHORT_TITLE, getShortTitle()));
126: setKeywords(getStringValue(bundle, JAVAX_PORTLET_KEYWORDS,
127: getKeywordStr()));
128: }
129: }
130:
131: /**
132: * @see org.apache.pluto.om.common.Language#getLocale()
133: */
134: public Locale getLocale() {
135: return locale;
136: }
137:
138: /**
139: * @see org.apache.pluto.om.common.Language#getTitle()
140: */
141: public String getTitle() {
142: return title;
143: }
144:
145: /**
146: * @see org.apache.pluto.om.common.Language#getShortTitle()
147: */
148: public String getShortTitle() {
149: return shortTitle;
150: }
151:
152: /**
153: * @see org.apache.pluto.om.common.Language#getKeywords()
154: */
155: public Iterator getKeywords() {
156: if (keywords == null) {
157: return Collections.EMPTY_LIST.iterator();
158: }
159: return keywords.iterator();
160: }
161:
162: /**
163: * @see org.apache.pluto.om.common.Language#getResourceBundle()
164: */
165: public ResourceBundle getResourceBundle() {
166:
167: return this ;
168: }
169:
170: public ResourceBundle getParentResourceBundle() {
171: return parent;
172: }
173:
174: /**
175: * @see org.apache.pluto.om.common.LanguageCtrl#setLocale(java.util.Locale)
176: */
177: public void setLocale(Locale locale) {
178: this .locale = locale;
179: }
180:
181: /**
182: * @see org.apache.pluto.om.common.LanguageCtrl#setTitle(java.lang.String)
183: */
184: public void setTitle(String title) {
185: this .title = title;
186: }
187:
188: /**
189: * @see org.apache.pluto.om.common.LanguageCtrl#setShortTitle(java.lang.String)
190: */
191: public void setShortTitle(String shortTitle) {
192: this .shortTitle = shortTitle;
193: }
194:
195: /**
196: * @see org.apache.jetspeed.om.common.LanguageComposite#setKeywords(java.util.Collection)
197: */
198: public void setKeywords(Collection keywords) {
199: this .keywords = keywords;
200: }
201:
202: public void setKeywords(String keywordStr) {
203: if (keywords == null) {
204: keywords = new ArrayList();
205: } else {
206: keywords.clear();
207: }
208: if (keywordStr == null) {
209: keywordStr = "";
210: }
211: StringTokenizer tok = new StringTokenizer(keywordStr, ",");
212: while (tok.hasMoreTokens()) {
213: keywords.add(tok.nextToken());
214: }
215: this .keywordStr = keywordStr;
216: }
217:
218: public String getKeywordStr() {
219: if (keywordStr == null) {
220: keywordStr = StringUtils.join(getKeywords(), ",");
221: }
222: return keywordStr;
223: }
224:
225: /**
226: * @see java.lang.Object#equals(java.lang.Object)
227: */
228: public boolean equals(Object obj) {
229: if (obj != null && obj instanceof Language) {
230: return obj.hashCode() == this .hashCode();
231: }
232:
233: return false;
234: }
235:
236: /**
237: * @see java.lang.Object#hashCode()
238: */
239: public int hashCode() {
240: HashCodeBuilder hasher = new HashCodeBuilder(19, 79);
241: Locale locale = getLocale();
242: hasher.append(locale.getCountry()).append(locale.getLanguage())
243: .append(locale.getVariant());
244: return hasher.toHashCode();
245: }
246: }
|