1 16 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 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 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 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 167 171 public boolean equals(Object o) { 172 return o == null ? false 173 : ((LanguageImpl)o).getLocale().equals(this.locale); 174 } 175 176 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 } 201 | Popular Tags |