1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.om.impl;
18
19 import java.io.Serializable;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.Enumeration;
24 import java.util.HashSet;
25 import java.util.Iterator;
26 import java.util.Locale;
27 import java.util.MissingResourceException;
28 import java.util.ResourceBundle;
29 import java.util.Set;
30 import java.util.StringTokenizer;
31
32 import org.apache.commons.lang.StringUtils;
33 import org.apache.jetspeed.om.common.MutableLanguage;
34 import org.apache.jetspeed.util.HashCodeBuilder;
35 import org.apache.jetspeed.util.JetspeedLocale;
36 import org.apache.pluto.om.common.Language;
37
38 /***
39 *
40 * LanguageImpl <br>
41 * Okay, base Language really has nothing to really do at all with language per
42 * se. It actually represents the locallized <code>title</code> and
43 * <code>short-title</code> attributes of a portlet's definition. It also
44 * contains a resource bundle for the specifc locale. <br>
45 * TODO: org.apache.pluto.om.common.Language should be seperated into TODO a
46 * Language class that just contains the resource bundle and TODO a Title class
47 * that contains a localized title and short title.
48 *
49 * @author <a href="mailto:weaver@apache.org">Scott T. Weaver </a>
50 * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
51 * @version $Id: LanguageImpl.java 516448 2007-03-09 16:25:47Z ate $
52 *
53 */
54 public class LanguageImpl extends ResourceBundle implements MutableLanguage, Serializable
55 {
56 public static final String JAVAX_PORTLET_KEYWORDS = "javax.portlet.keywords";
57 public static final String JAVAX_PORTLET_SHORT_TITLE = "javax.portlet.short-title";
58 public static final String JAVAX_PORTLET_TITLE = "javax.portlet.title";
59
60 private Set keys;
61 private String title;
62 private String shortTitle;
63 private Locale locale;
64 private String keywordStr;
65 private Collection keywords;
66
67 /***
68 * This field can be used by persistence tools for storing PK info Otherwise
69 * it has no effect on the functioning of the portal.
70 */
71 protected long id;
72
73 protected long portletId;
74
75 public LanguageImpl()
76 {
77 keys = Collections.synchronizedSet(new HashSet(3));
78 keys.add(JAVAX_PORTLET_TITLE);
79 keys.add(JAVAX_PORTLET_SHORT_TITLE);
80 keys.add(JAVAX_PORTLET_KEYWORDS);
81 this.locale = JetspeedLocale.getDefaultLocale();
82 }
83
84 public Enumeration getKeys()
85 {
86 return Collections.enumeration(keys);
87 }
88
89 protected Object handleGetObject(String key)
90 {
91 if (key.equals(JAVAX_PORTLET_TITLE))
92 {
93 return getTitle();
94 }
95 else if (key.equals(JAVAX_PORTLET_SHORT_TITLE))
96 {
97 return getShortTitle();
98 }
99 else if (key.equals(JAVAX_PORTLET_KEYWORDS))
100 {
101 return getKeywordStr();
102 }
103 return null;
104 }
105
106 private String getStringValue(ResourceBundle bundle, String key, String defaultValue)
107 {
108 String value = defaultValue;
109 try
110 {
111 value = (String)bundle.getObject(key);
112 }
113 catch (MissingResourceException mre)
114 {
115 }
116 catch (ClassCastException cce)
117 {
118 }
119 return value;
120 }
121
122 public void setResourceBundle(ResourceBundle bundle)
123 {
124 if ( parent == null && bundle != null )
125 {
126 Enumeration parentKeys = bundle.getKeys();
127 while ( parentKeys.hasMoreElements() )
128 {
129 keys.add(parentKeys.nextElement());
130 }
131 setParent(bundle);
132 }
133 }
134
135 public void loadDefaults()
136 {
137 ResourceBundle bundle = getParentResourceBundle();
138 if ( bundle != null )
139 {
140 setTitle(getStringValue(bundle, JAVAX_PORTLET_TITLE, getTitle()));
141 setShortTitle(getStringValue(bundle, JAVAX_PORTLET_SHORT_TITLE, getShortTitle()));
142 setKeywords(getStringValue(bundle, JAVAX_PORTLET_KEYWORDS, getKeywordStr()));
143 }
144 }
145
146 /***
147 * @see org.apache.pluto.om.common.Language#getLocale()
148 */
149 public Locale getLocale()
150 {
151 return locale;
152 }
153
154 /***
155 * @see org.apache.pluto.om.common.Language#getTitle()
156 */
157 public String getTitle()
158 {
159 return title;
160 }
161
162 /***
163 * @see org.apache.pluto.om.common.Language#getShortTitle()
164 */
165 public String getShortTitle()
166 {
167 return shortTitle;
168 }
169
170 /***
171 * @see org.apache.pluto.om.common.Language#getKeywords()
172 */
173 public Iterator getKeywords()
174 {
175 if ( keywords == null )
176 {
177 return Collections.EMPTY_LIST.iterator();
178 }
179 return keywords.iterator();
180 }
181
182 /***
183 * @see org.apache.pluto.om.common.Language#getResourceBundle()
184 */
185 public ResourceBundle getResourceBundle()
186 {
187
188 return this;
189 }
190
191 public ResourceBundle getParentResourceBundle()
192 {
193 return parent;
194 }
195
196 /***
197 * @see org.apache.pluto.om.common.LanguageCtrl#setLocale(java.util.Locale)
198 */
199 public void setLocale( Locale locale )
200 {
201 this.locale = locale;
202 }
203
204 /***
205 * @see org.apache.pluto.om.common.LanguageCtrl#setTitle(java.lang.String)
206 */
207 public void setTitle( String title )
208 {
209 this.title = title;
210 }
211
212 /***
213 * @see org.apache.pluto.om.common.LanguageCtrl#setShortTitle(java.lang.String)
214 */
215 public void setShortTitle( String shortTitle )
216 {
217 this.shortTitle = shortTitle;
218 }
219
220 /***
221 * @see org.apache.jetspeed.om.common.LanguageComposite#setKeywords(java.util.Collection)
222 */
223 public void setKeywords( Collection keywords )
224 {
225 this.keywords = keywords;
226 }
227
228 public void setKeywords(String keywordStr)
229 {
230 if (keywords == null)
231 {
232 keywords = new ArrayList();
233 }
234 else
235 {
236 keywords.clear();
237 }
238 if ( keywordStr == null )
239 {
240 keywordStr = "";
241 }
242 StringTokenizer tok = new StringTokenizer(keywordStr, ",");
243 while (tok.hasMoreTokens())
244 {
245 keywords.add(tok.nextToken());
246 }
247 this.keywordStr = keywordStr;
248 }
249
250 public String getKeywordStr()
251 {
252 if ( keywordStr == null )
253 {
254 keywordStr = StringUtils.join(getKeywords(),",");
255 }
256 return keywordStr;
257 }
258
259 /***
260 * @see java.lang.Object#equals(java.lang.Object)
261 */
262 public boolean equals( Object obj )
263 {
264 if (obj != null && obj instanceof Language)
265 {
266 return obj.hashCode() == this.hashCode();
267 }
268
269 return false;
270 }
271
272 /***
273 * @see java.lang.Object#hashCode()
274 */
275 public int hashCode()
276 {
277 HashCodeBuilder hasher = new HashCodeBuilder(19, 79);
278 Locale locale = getLocale();
279 hasher.append(locale.getCountry()).append(locale.getLanguage()).append(locale.getVariant());
280 return hasher.toHashCode();
281 }
282 }