%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.jetspeed.om.impl.LanguageImpl |
|
|
1 | /* |
|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
|
3 | * contributor license agreements. See the NOTICE file distributed with |
|
4 | * this work for additional information regarding copyright ownership. |
|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
6 | * (the "License"); you may not use this file except in compliance with |
|
7 | * the License. You may obtain a copy of the License at |
|
8 | * |
|
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
10 | * |
|
11 | * Unless required by applicable law or agreed to in writing, software |
|
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 | * See the License for the specific language governing permissions and |
|
15 | * limitations under the License. |
|
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 | 0 | { |
77 | 0 | keys = Collections.synchronizedSet(new HashSet(3)); |
78 | 0 | keys.add(JAVAX_PORTLET_TITLE); |
79 | 0 | keys.add(JAVAX_PORTLET_SHORT_TITLE); |
80 | 0 | keys.add(JAVAX_PORTLET_KEYWORDS); |
81 | 0 | this.locale = JetspeedLocale.getDefaultLocale(); |
82 | 0 | } |
83 | ||
84 | public Enumeration getKeys() |
|
85 | { |
|
86 | 0 | return Collections.enumeration(keys); |
87 | } |
|
88 | ||
89 | protected Object handleGetObject(String key) |
|
90 | { |
|
91 | 0 | if (key.equals(JAVAX_PORTLET_TITLE)) |
92 | { |
|
93 | 0 | return getTitle(); |
94 | } |
|
95 | 0 | else if (key.equals(JAVAX_PORTLET_SHORT_TITLE)) |
96 | { |
|
97 | 0 | return getShortTitle(); |
98 | } |
|
99 | 0 | else if (key.equals(JAVAX_PORTLET_KEYWORDS)) |
100 | { |
|
101 | 0 | return getKeywordStr(); |
102 | } |
|
103 | 0 | return null; |
104 | } |
|
105 | ||
106 | private String getStringValue(ResourceBundle bundle, String key, String defaultValue) |
|
107 | { |
|
108 | 0 | String value = defaultValue; |
109 | try |
|
110 | { |
|
111 | 0 | value = (String)bundle.getObject(key); |
112 | } |
|
113 | 0 | catch (MissingResourceException mre) |
114 | { |
|
115 | } |
|
116 | 0 | catch (ClassCastException cce) |
117 | { |
|
118 | 0 | } |
119 | 0 | return value; |
120 | } |
|
121 | ||
122 | public void setResourceBundle(ResourceBundle bundle) |
|
123 | { |
|
124 | 0 | if ( parent == null && bundle != class="keyword">null ) |
125 | { |
|
126 | 0 | Enumeration parentKeys = bundle.getKeys(); |
127 | 0 | while ( parentKeys.hasMoreElements() ) |
128 | { |
|
129 | 0 | keys.add(parentKeys.nextElement()); |
130 | } |
|
131 | 0 | setParent(bundle); |
132 | } |
|
133 | 0 | } |
134 | ||
135 | public void loadDefaults() |
|
136 | { |
|
137 | 0 | ResourceBundle bundle = getParentResourceBundle(); |
138 | 0 | if ( bundle != null ) |
139 | { |
|
140 | 0 | setTitle(getStringValue(bundle, JAVAX_PORTLET_TITLE, getTitle())); |
141 | 0 | setShortTitle(getStringValue(bundle, JAVAX_PORTLET_SHORT_TITLE, getShortTitle())); |
142 | 0 | setKeywords(getStringValue(bundle, JAVAX_PORTLET_KEYWORDS, getKeywordStr())); |
143 | } |
|
144 | 0 | } |
145 | ||
146 | /** |
|
147 | * @see org.apache.pluto.om.common.Language#getLocale() |
|
148 | */ |
|
149 | public Locale getLocale() |
|
150 | { |
|
151 | 0 | return locale; |
152 | } |
|
153 | ||
154 | /** |
|
155 | * @see org.apache.pluto.om.common.Language#getTitle() |
|
156 | */ |
|
157 | public String getTitle() |
|
158 | { |
|
159 | 0 | return title; |
160 | } |
|
161 | ||
162 | /** |
|
163 | * @see org.apache.pluto.om.common.Language#getShortTitle() |
|
164 | */ |
|
165 | public String getShortTitle() |
|
166 | { |
|
167 | 0 | return shortTitle; |
168 | } |
|
169 | ||
170 | /** |
|
171 | * @see org.apache.pluto.om.common.Language#getKeywords() |
|
172 | */ |
|
173 | public Iterator getKeywords() |
|
174 | { |
|
175 | 0 | if ( keywords == null ) |
176 | { |
|
177 | 0 | return Collections.EMPTY_LIST.iterator(); |
178 | } |
|
179 | 0 | return keywords.iterator(); |
180 | } |
|
181 | ||
182 | /** |
|
183 | * @see org.apache.pluto.om.common.Language#getResourceBundle() |
|
184 | */ |
|
185 | public ResourceBundle getResourceBundle() |
|
186 | { |
|
187 | ||
188 | 0 | return this; |
189 | } |
|
190 | ||
191 | public ResourceBundle getParentResourceBundle() |
|
192 | { |
|
193 | 0 | 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 | 0 | this.locale = locale; |
202 | 0 | } |
203 | ||
204 | /** |
|
205 | * @see org.apache.pluto.om.common.LanguageCtrl#setTitle(java.lang.String) |
|
206 | */ |
|
207 | public void setTitle( String title ) |
|
208 | { |
|
209 | 0 | this.title = title; |
210 | 0 | } |
211 | ||
212 | /** |
|
213 | * @see org.apache.pluto.om.common.LanguageCtrl#setShortTitle(java.lang.String) |
|
214 | */ |
|
215 | public void setShortTitle( String shortTitle ) |
|
216 | { |
|
217 | 0 | this.shortTitle = shortTitle; |
218 | 0 | } |
219 | ||
220 | /** |
|
221 | * @see org.apache.jetspeed.om.common.LanguageComposite#setKeywords(java.util.Collection) |
|
222 | */ |
|
223 | public void setKeywords( Collection keywords ) |
|
224 | { |
|
225 | 0 | this.keywords = keywords; |
226 | 0 | } |
227 | ||
228 | public void setKeywords(String keywordStr) |
|
229 | { |
|
230 | 0 | if (keywords == null) |
231 | { |
|
232 | 0 | keywords = new ArrayList(); |
233 | } |
|
234 | else |
|
235 | { |
|
236 | 0 | keywords.clear(); |
237 | } |
|
238 | 0 | if ( keywordStr == null ) |
239 | { |
|
240 | 0 | keywordStr = ""; |
241 | } |
|
242 | 0 | StringTokenizer tok = new StringTokenizer(keywordStr, ","); |
243 | 0 | while (tok.hasMoreTokens()) |
244 | { |
|
245 | 0 | keywords.add(tok.nextToken()); |
246 | } |
|
247 | 0 | this.keywordStr = keywordStr; |
248 | 0 | } |
249 | ||
250 | public String getKeywordStr() |
|
251 | { |
|
252 | 0 | if ( keywordStr == null ) |
253 | { |
|
254 | 0 | keywordStr = StringUtils.join(getKeywords(),","); |
255 | } |
|
256 | 0 | return keywordStr; |
257 | } |
|
258 | ||
259 | /** |
|
260 | * @see java.lang.Object#equals(java.lang.Object) |
|
261 | */ |
|
262 | public boolean equals( Object obj ) |
|
263 | { |
|
264 | 0 | if (obj != null && obj instanceof Language) |
265 | { |
|
266 | 0 | return obj.hashCode() == this.hashCode(); |
267 | } |
|
268 | ||
269 | 0 | return false; |
270 | } |
|
271 | ||
272 | /** |
|
273 | * @see java.lang.Object#hashCode() |
|
274 | */ |
|
275 | public int hashCode() |
|
276 | { |
|
277 | 0 | HashCodeBuilder hasher = new HashCodeBuilder(19, 79); |
278 | 0 | Locale locale = getLocale(); |
279 | 0 | hasher.append(locale.getCountry()).append(locale.getLanguage()).append(locale.getVariant()); |
280 | 0 | return hasher.toHashCode(); |
281 | } |
|
282 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |