1 16 package org.apache.cocoon.portal.pluto.om.common; 17 18 import java.util.ArrayList ; 19 import java.util.Collection ; 20 import java.util.Enumeration ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.ListResourceBundle ; 24 import java.util.Locale ; 25 import java.util.ResourceBundle ; 26 import java.util.StringTokenizer ; 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 39 public class LanguageImpl implements Language, java.io.Serializable { 40 42 65 66 private static class DefaultsResourceBundle extends ListResourceBundle { 67 private Object [][] resources; 68 69 public DefaultsResourceBundle(String defaultTitle, String defaultShortTitle, String defaultKeyWords) { 70 resources = new Object [][] { 71 {"javax.portlet.title" , defaultTitle}, 72 {"javax.portlet.short-title", defaultShortTitle}, 73 {"javax.portlet.keywords" , defaultKeyWords} 74 }; 75 } 76 77 protected Object [][] getContents() { 78 return resources; 79 } 80 } 81 82 private static class ResourceBundleImpl extends ResourceBundle { 83 private HashMap data; 84 85 public ResourceBundleImpl(ResourceBundle bundle, ResourceBundle defaults) { 86 data = new HashMap (); 87 88 importData(defaults); 89 importData(bundle); 90 } 91 92 private void importData(ResourceBundle bundle) { 93 if (bundle != null) { 94 for (Enumeration enumeration = bundle.getKeys(); enumeration.hasMoreElements();) { 95 String key = (String )enumeration.nextElement(); 96 Object value = bundle.getObject(key); 97 98 data.put(key, value); 99 } 100 } 101 } 102 103 protected Object handleGetObject(String key) { 104 return data.get(key); 105 } 106 107 public Enumeration getKeys() { 108 return new Enumerator(data.keySet()); 109 } 110 } 111 112 private Locale locale; 113 private String title; 114 private String shortTitle; 115 private ResourceBundle bundle; 116 private ArrayList keywords; 117 118 public LanguageImpl(Locale locale, ResourceBundle bundle, String defaultTitle, String defaultShortTitle, String 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 129 public Locale getLocale() { 130 return locale; 131 } 132 133 public String getTitle() { 134 return title; 135 } 136 137 public String getShortTitle() { 138 return shortTitle; 139 } 140 141 public Iterator getKeywords() { 142 return keywords.iterator(); 143 } 144 145 public ResourceBundle getResourceBundle() { 146 return bundle; 147 } 148 149 private ArrayList toList(String value) { 151 ArrayList keywords = new ArrayList (); 152 153 for (StringTokenizer st = new StringTokenizer (value, ","); st.hasMoreTokens();) { 154 keywords.add(st.nextToken().trim()); 155 } 156 157 return keywords; 158 } 159 160 public String toString() { 161 return toString(0); 162 } 163 164 public String toString(final int indent) { 165 StringBuffer buffer = new StringBuffer (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 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 193 197 public boolean equals(Object o) { 198 return o == null ? false 199 : ((LanguageImpl)o).getLocale().equals(this.locale); 200 } 201 202 205 public int hashCode() { 206 return locale.hashCode(); 207 } 208 209 public void setKeywords(Collection keywords) { 210 this.keywords.clear(); 211 this.keywords.addAll(keywords); 212 } 213 214 public void setLocale(Locale locale) { 215 this.locale = locale; 216 } 217 218 public void setShortTitle(String shortTitle) { 219 this.shortTitle = shortTitle; 220 } 221 222 public void setTitle(String title) { 223 this.title = title; 224 } 225 226 } 227 | Popular Tags |