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 org.apache.pluto.om.common.*;
23  import org.apache.pluto.util.Enumerator;
24  import org.apache.pluto.util.StringUtils;
25  import java.util.*;
26  
27  public class LanguageImpl implements Language, java.io.Serializable {
28      // ResourceBundle creation part
29  
30      private static class DefaultsResourceBundle extends ListResourceBundle {
31          private Object[][] resources;
32  
33          public DefaultsResourceBundle(String defaultTitle, String defaultShortTitle, String defaultKeyWords) {
34              resources = new Object[][] {
35                  {"javax.portlet.title"      , defaultTitle},
36                  {"javax.portlet.short-title", defaultShortTitle},
37                  {"javax.portlet.keywords"   , defaultKeyWords}
38              };
39          }
40  
41          protected Object[][] getContents() {
42              return resources;
43          }
44      }
45  
46      private static class ResourceBundleImpl extends ResourceBundle {
47          private HashMap data;
48  
49          public ResourceBundleImpl(ResourceBundle bundle, ResourceBundle defaults) {
50              data = new HashMap();
51  
52              importData(defaults);
53              importData(bundle);
54          }
55  
56          private void importData(ResourceBundle bundle) {
57              if (bundle != null) {
58                  for (Enumeration enum = bundle.getKeys(); enum.hasMoreElements();) {
59                      String key   = (String)enum.nextElement();
60                      Object value = bundle.getObject(key);
61                      data.put(key, value);
62                  }
63              }
64          }
65  
66          protected Object handleGetObject(String key) {
67              return data.get(key);
68          }
69  
70          public Enumeration getKeys() {
71              return new Enumerator(data.keySet());
72          }
73      }
74  
75      private Locale         locale;
76      private String         title;
77      private String         shortTitle;
78      private ResourceBundle bundle;
79      private ArrayList      keywords;
80  
81      public LanguageImpl(Locale locale, ResourceBundle bundle, String defaultTitle, String defaultShortTitle, String defaultKeyWords) {
82          this.bundle = new ResourceBundleImpl(bundle, new DefaultsResourceBundle(defaultTitle, defaultShortTitle, defaultKeyWords));
83  
84          this.locale = locale;
85          title       = this.bundle.getString("javax.portlet.title");
86          shortTitle  = this.bundle.getString("javax.portlet.short-title");
87          keywords    = toList(this.bundle.getString("javax.portlet.keywords"));
88      }
89  
90      // Language implementation.
91  
92      public Locale getLocale() {
93          return locale;
94      }
95  
96      public String getTitle() {
97          return title;
98      }
99  
100     public String getShortTitle() {
101         return shortTitle;
102     }
103 
104     public Iterator getKeywords() {
105         return keywords.iterator();
106     }
107 
108     public ResourceBundle getResourceBundle() {                            
109         return bundle;
110     }
111 
112     // internal methods.
113     private ArrayList toList(String value) {
114         ArrayList keywords = new ArrayList();
115 
116         for (StringTokenizer st = new StringTokenizer(value, ","); st.hasMoreTokens();) {
117             keywords.add(st.nextToken().trim());
118         }
119 
120         return keywords;
121     }
122     
123     public String toString()
124     {
125         return toString(0);
126     }
127 
128     public String toString(final int indent)
129     {
130         StringBuffer buffer = new StringBuffer(50);
131         StringUtils.newLine(buffer,indent);
132         buffer.append(getClass().toString()); buffer.append(":");
133         StringUtils.newLine(buffer,indent);
134         buffer.append("{");
135         StringUtils.newLine(buffer,indent);
136         buffer.append("locale='"); buffer.append(locale); buffer.append("'");
137         StringUtils.newLine(buffer,indent);
138         buffer.append("title='"); buffer.append(title); buffer.append("'");
139         StringUtils.newLine(buffer,indent);
140         buffer.append("shortTitle='"); buffer.append(shortTitle); buffer.append("'");
141         Iterator iterator = keywords.iterator();
142         if (iterator.hasNext()) {
143             StringUtils.newLine(buffer,indent);
144             buffer.append("Keywords:");
145         }
146         while (iterator.hasNext()) {
147             buffer.append(iterator.next());
148             buffer.append(',');
149         }
150         StringUtils.newLine(buffer,indent);
151         buffer.append("}");
152         return buffer.toString();
153     }
154 
155 
156     // additional methods.
157     
158     /* (non-Javadoc)
159      * @see java.lang.Object#equals(java.lang.Object)
160      * used for element equality according collection implementations
161      */
162     public boolean equals(Object o) {
163         return o == null ? false
164                          : ((LanguageImpl)o).getLocale().equals(this.locale);
165     }
166 
167     /* (non-Javadoc)
168      * @see java.lang.Object#hashCode()
169      */
170     public int hashCode() {
171         return locale.hashCode();
172     }
173 
174     public void setKeywords(Collection keywords) {
175         this.keywords.clear();
176         this.keywords.addAll(keywords);
177     }
178 
179     public void setLocale(Locale locale) {
180         this.locale = locale;
181     }
182 
183     public void setShortTitle(String shortTitle) {
184         this.shortTitle = shortTitle;
185     }
186 
187     public void setTitle(String title) {
188         this.title = title;
189     }
190 
191 }