KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > pluto > om > common > LanguageImpl


1 /*
2  * Copyright 2004,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 package org.apache.cocoon.portal.pluto.om.common;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.ListResourceBundle JavaDoc;
24 import java.util.Locale JavaDoc;
25 import java.util.ResourceBundle JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27
28 import org.apache.pluto.om.common.Language;
29 import org.apache.pluto.util.Enumerator;
30 import org.apache.pluto.util.StringUtils;
31
32 /**
33  *
34  *
35  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
36  *
37  * @version CVS $Id: LanguageImpl.java 123407 2004-12-27 13:51:59Z cziegeler $
38  */

39 public class LanguageImpl implements Language, java.io.Serializable JavaDoc {
40     // ResourceBundle creation part
41

42     /*private static class Resources extends ListResourceBundle {
43         private Object [][] resources = null;
44         private Language source = null;
45
46         public Resources(Language source)
47         {
48             this.source = source;
49             Vector v = new Vector();
50             Iterator it = source.getKeywords();
51             while(it != null && it.hasNext())
52                 v.add(it.next());
53
54             resources = new Object [][] {
55                     { "javax.portlet.title", source.getTitle()==null? "": source.getTitle()},
56                     { "javax.portlet.short-title", source.getShortTitle()==null? "": source.getShortTitle()},
57                     { "javax.portlet.keywords", v.size() > 0 ? v.toArray().toString() : ""}};
58         }
59
60         public Object[][] getContents()
61         {
62             return resources;
63         }
64     }*/

65
66     private static class DefaultsResourceBundle extends ListResourceBundle JavaDoc {
67         private Object JavaDoc[][] resources;
68
69         public DefaultsResourceBundle(String JavaDoc defaultTitle, String JavaDoc defaultShortTitle, String JavaDoc defaultKeyWords) {
70             resources = new Object JavaDoc[][] {
71                 {"javax.portlet.title" , defaultTitle},
72                 {"javax.portlet.short-title", defaultShortTitle},
73                 {"javax.portlet.keywords" , defaultKeyWords}
74             };
75         }
76
77         protected Object JavaDoc[][] getContents() {
78             return resources;
79         }
80     }
81
82     private static class ResourceBundleImpl extends ResourceBundle JavaDoc {
83         private HashMap JavaDoc data;
84
85         public ResourceBundleImpl(ResourceBundle JavaDoc bundle, ResourceBundle JavaDoc defaults) {
86             data = new HashMap JavaDoc();
87
88             importData(defaults);
89             importData(bundle);
90         }
91
92         private void importData(ResourceBundle JavaDoc bundle) {
93             if (bundle != null) {
94                 for (Enumeration JavaDoc enumeration = bundle.getKeys(); enumeration.hasMoreElements();) {
95                     String JavaDoc key = (String JavaDoc)enumeration.nextElement();
96                     Object JavaDoc value = bundle.getObject(key);
97
98                     data.put(key, value);
99                 }
100             }
101         }
102
103         protected Object JavaDoc handleGetObject(String JavaDoc key) {
104             return data.get(key);
105         }
106
107         public Enumeration JavaDoc getKeys() {
108             return new Enumerator(data.keySet());
109         }
110     }
111
112     private Locale JavaDoc locale;
113     private String JavaDoc title;
114     private String JavaDoc shortTitle;
115     private ResourceBundle JavaDoc bundle;
116     private ArrayList JavaDoc keywords;
117
118     public LanguageImpl(Locale JavaDoc locale, ResourceBundle JavaDoc bundle, String JavaDoc defaultTitle, String JavaDoc defaultShortTitle, String JavaDoc defaultKeyWords) {
119         this.bundle = new ResourceBundleImpl(bundle, new DefaultsResourceBundle(defaultTitle, defaultShortTitle, defaultKeyWords));
120
121         this.locale = locale;
122         title = this.bundle.getString("javax.portlet.title");
123         shortTitle = this.bundle.getString("javax.portlet.short-title");
124         keywords = toList(this.bundle.getString("javax.portlet.keywords"));
125     }
126
127     // Language implementation.
128

129     public Locale JavaDoc getLocale() {
130         return locale;
131     }
132
133     public String JavaDoc getTitle() {
134         return title;
135     }
136
137     public String JavaDoc getShortTitle() {
138         return shortTitle;
139     }
140
141     public Iterator JavaDoc getKeywords() {
142         return keywords.iterator();
143     }
144
145     public ResourceBundle JavaDoc getResourceBundle() {
146         return bundle;
147     }
148
149     // internal methods.
150
private ArrayList JavaDoc toList(String JavaDoc value) {
151         ArrayList JavaDoc keywords = new ArrayList JavaDoc();
152
153         for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(value, ","); st.hasMoreTokens();) {
154             keywords.add(st.nextToken().trim());
155         }
156
157         return keywords;
158     }
159     
160     public String JavaDoc toString() {
161         return toString(0);
162     }
163
164     public String JavaDoc toString(final int indent) {
165         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(50);
166         StringUtils.newLine(buffer,indent);
167         buffer.append(getClass().toString()); buffer.append(":");
168         StringUtils.newLine(buffer,indent);
169         buffer.append("{");
170         StringUtils.newLine(buffer,indent);
171         buffer.append("locale='"); buffer.append(locale); buffer.append("'");
172         StringUtils.newLine(buffer,indent);
173         buffer.append("title='"); buffer.append(title); buffer.append("'");
174         StringUtils.newLine(buffer,indent);
175         buffer.append("shortTitle='"); buffer.append(shortTitle); buffer.append("'");
176         Iterator JavaDoc iterator = keywords.iterator();
177         if (iterator.hasNext()) {
178             StringUtils.newLine(buffer,indent);
179             buffer.append("Keywords:");
180         }
181         while (iterator.hasNext()) {
182             buffer.append(iterator.next());
183             buffer.append(',');
184         }
185         StringUtils.newLine(buffer,indent);
186         buffer.append("}");
187         return buffer.toString();
188     }
189
190
191     // additional methods.
192

193     /* (non-Javadoc)
194      * @see java.lang.Object#equals(java.lang.Object)
195      * used for element equality according collection implementations
196      */

197     public boolean equals(Object JavaDoc o) {
198         return o == null ? false
199                          : ((LanguageImpl)o).getLocale().equals(this.locale);
200     }
201
202     /* (non-Javadoc)
203      * @see java.lang.Object#hashCode()
204      */

205     public int hashCode() {
206         return locale.hashCode();
207     }
208
209     public void setKeywords(Collection JavaDoc keywords) {
210         this.keywords.clear();
211         this.keywords.addAll(keywords);
212     }
213
214     public void setLocale(Locale JavaDoc locale) {
215         this.locale = locale;
216     }
217
218     public void setShortTitle(String JavaDoc shortTitle) {
219         this.shortTitle = shortTitle;
220     }
221
222     public void setTitle(String JavaDoc title) {
223         this.title = title;
224     }
225
226 }
227
Popular Tags