View Javadoc

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;
23  import java.util.Collection;
24  import java.util.Enumeration;
25  import java.util.HashMap;
26  import java.util.Iterator;
27  import java.util.ListResourceBundle;
28  import java.util.Locale;
29  import java.util.ResourceBundle;
30  import java.util.StringTokenizer;
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 {
37      // ResourceBundle creation part
38  
39      private static class DefaultsResourceBundle extends ListResourceBundle {
40          private Object[][] resources;
41  
42          public DefaultsResourceBundle(String defaultTitle, String defaultShortTitle, String defaultKeyWords) {
43              resources = new Object[][] {
44                  {"javax.portlet.title"      , defaultTitle},
45                  {"javax.portlet.short-title", defaultShortTitle},
46                  {"javax.portlet.keywords"   , defaultKeyWords}
47              };
48          }
49  
50          protected Object[][] getContents() {
51              return resources;
52          }
53      }
54  
55      private static class ResourceBundleImpl extends ResourceBundle {
56          private HashMap data;
57  
58          public ResourceBundleImpl(ResourceBundle bundle, ResourceBundle defaults) {
59              data = new HashMap();
60  
61              importData(defaults);
62              importData(bundle);
63          }
64  
65          private void importData(ResourceBundle bundle) {
66              if (bundle != null) {
67                  for (Enumeration enumerator= bundle.getKeys(); enumerator.hasMoreElements();) {
68                      String key   = (String)enumerator.nextElement();
69                      Object value = bundle.getObject(key);
70                      data.put(key, value);
71                  }
72              }
73          }
74  
75          protected Object handleGetObject(String key) {
76              return data.get(key);
77          }
78  
79          public Enumeration getKeys() {
80              return new Enumerator(data.keySet());
81          }
82      }
83  
84      private Locale         locale;
85      private String         title;
86      private String         shortTitle;
87      private ResourceBundle bundle;
88      private ArrayList      keywords;
89  
90      public LanguageImpl(Locale locale, ResourceBundle bundle, String defaultTitle, String defaultShortTitle, String 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 getLocale() {
102         return locale;
103     }
104 
105     public String getTitle() {
106         return title;
107     }
108 
109     public String getShortTitle() {
110         return shortTitle;
111     }
112 
113     public Iterator getKeywords() {
114         return keywords.iterator();
115     }
116 
117     public ResourceBundle getResourceBundle() {                            
118         return bundle;
119     }
120 
121     // internal methods.
122     private ArrayList toList(String value) {
123         ArrayList keywords = new ArrayList();
124 
125         for (StringTokenizer st = new StringTokenizer(value, ","); st.hasMoreTokens();) {
126             keywords.add(st.nextToken().trim());
127         }
128 
129         return keywords;
130     }
131     
132     public String toString()
133     {
134         return toString(0);
135     }
136 
137     public String toString(final int indent)
138     {
139         StringBuffer buffer = new StringBuffer(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 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 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 keywords) {
184         this.keywords.clear();
185         this.keywords.addAll(keywords);
186     }
187 
188     public void setLocale(Locale locale) {
189         this.locale = locale;
190     }
191 
192     public void setShortTitle(String shortTitle) {
193         this.shortTitle = shortTitle;
194     }
195 
196     public void setTitle(String title) {
197         this.title = title;
198     }
199 
200 }