KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > pluto > portalImpl > om > common > impl > LanguageImpl


1 /*
2  * Copyright 2003,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 /*
17
18  */

19
20 package org.apache.pluto.portalImpl.om.common.impl;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.ListResourceBundle JavaDoc;
28 import java.util.Locale JavaDoc;
29 import java.util.ResourceBundle JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31
32 import org.apache.pluto.om.common.Language;
33 import org.apache.pluto.util.Enumerator;
34 import org.apache.pluto.util.StringUtils;
35
36 public class LanguageImpl implements Language, java.io.Serializable JavaDoc {
37     // ResourceBundle creation part
38

39     private static class DefaultsResourceBundle extends ListResourceBundle JavaDoc {
40         private Object JavaDoc[][] resources;
41
42         public DefaultsResourceBundle(String JavaDoc defaultTitle, String JavaDoc defaultShortTitle, String JavaDoc defaultKeyWords) {
43             resources = new Object JavaDoc[][] {
44                 {"javax.portlet.title" , defaultTitle},
45                 {"javax.portlet.short-title", defaultShortTitle},
46                 {"javax.portlet.keywords" , defaultKeyWords}
47             };
48         }
49
50         protected Object JavaDoc[][] getContents() {
51             return resources;
52         }
53     }
54
55     private static class ResourceBundleImpl extends ResourceBundle JavaDoc {
56         private HashMap JavaDoc data;
57
58         public ResourceBundleImpl(ResourceBundle JavaDoc bundle, ResourceBundle JavaDoc defaults) {
59             data = new HashMap JavaDoc();
60
61             importData(defaults);
62             importData(bundle);
63         }
64
65         private void importData(ResourceBundle JavaDoc bundle) {
66             if (bundle != null) {
67                 for (Enumeration JavaDoc enumerator= bundle.getKeys(); enumerator.hasMoreElements();) {
68                     String JavaDoc key = (String JavaDoc)enumerator.nextElement();
69                     Object JavaDoc value = bundle.getObject(key);
70                     data.put(key, value);
71                 }
72             }
73         }
74
75         protected Object JavaDoc handleGetObject(String JavaDoc key) {
76             return data.get(key);
77         }
78
79         public Enumeration JavaDoc getKeys() {
80             return new Enumerator(data.keySet());
81         }
82     }
83
84     private Locale JavaDoc locale;
85     private String JavaDoc title;
86     private String JavaDoc shortTitle;
87     private ResourceBundle JavaDoc bundle;
88     private ArrayList JavaDoc keywords;
89
90     public LanguageImpl(Locale JavaDoc locale, ResourceBundle JavaDoc bundle, String JavaDoc defaultTitle, String JavaDoc defaultShortTitle, String JavaDoc defaultKeyWords) {
91         this.bundle = new ResourceBundleImpl(bundle, new DefaultsResourceBundle(defaultTitle, defaultShortTitle, defaultKeyWords));
92
93         this.locale = locale;
94         title = this.bundle.getString("javax.portlet.title");
95         shortTitle = this.bundle.getString("javax.portlet.short-title");
96         keywords = toList(this.bundle.getString("javax.portlet.keywords"));
97     }
98
99     // Language implementation.
100

101     public Locale JavaDoc getLocale() {
102         return locale;
103     }
104
105     public String JavaDoc getTitle() {
106         return title;
107     }
108
109     public String JavaDoc getShortTitle() {
110         return shortTitle;
111     }
112
113     public Iterator JavaDoc getKeywords() {
114         return keywords.iterator();
115     }
116
117     public ResourceBundle JavaDoc getResourceBundle() {
118         return bundle;
119     }
120
121     // internal methods.
122
private ArrayList JavaDoc toList(String JavaDoc value) {
123         ArrayList JavaDoc keywords = new ArrayList JavaDoc();
124
125         for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(value, ","); st.hasMoreTokens();) {
126             keywords.add(st.nextToken().trim());
127         }
128
129         return keywords;
130     }
131     
132     public String JavaDoc toString()
133     {
134         return toString(0);
135     }
136
137     public String JavaDoc toString(final int indent)
138     {
139         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(50);
140         StringUtils.newLine(buffer,indent);
141         buffer.append(getClass().toString()); buffer.append(":");
142         StringUtils.newLine(buffer,indent);
143         buffer.append("{");
144         StringUtils.newLine(buffer,indent);
145         buffer.append("locale='"); buffer.append(locale); buffer.append("'");
146         StringUtils.newLine(buffer,indent);
147         buffer.append("title='"); buffer.append(title); buffer.append("'");
148         StringUtils.newLine(buffer,indent);
149         buffer.append("shortTitle='"); buffer.append(shortTitle); buffer.append("'");
150         Iterator JavaDoc iterator = keywords.iterator();
151         if (iterator.hasNext()) {
152             StringUtils.newLine(buffer,indent);
153             buffer.append("Keywords:");
154         }
155         while (iterator.hasNext()) {
156             buffer.append(iterator.next());
157             buffer.append(',');
158         }
159         StringUtils.newLine(buffer,indent);
160         buffer.append("}");
161         return buffer.toString();
162     }
163
164
165     // additional methods.
166

167     /* (non-Javadoc)
168      * @see java.lang.Object#equals(java.lang.Object)
169      * used for element equality according collection implementations
170      */

171     public boolean equals(Object JavaDoc o) {
172         return o == null ? false
173                          : ((LanguageImpl)o).getLocale().equals(this.locale);
174     }
175
176     /* (non-Javadoc)
177      * @see java.lang.Object#hashCode()
178      */

179     public int hashCode() {
180         return locale.hashCode();
181     }
182
183     public void setKeywords(Collection JavaDoc keywords) {
184         this.keywords.clear();
185         this.keywords.addAll(keywords);
186     }
187
188     public void setLocale(Locale JavaDoc locale) {
189         this.locale = locale;
190     }
191
192     public void setShortTitle(String JavaDoc shortTitle) {
193         this.shortTitle = shortTitle;
194     }
195
196     public void setTitle(String JavaDoc title) {
197         this.title = title;
198     }
199
200 }
201
Popular Tags