1 16 package org.apache.myfaces.taglib.core; 17 18 import java.util.ArrayList ; 19 import java.util.Collection ; 20 import java.util.Enumeration ; 21 import java.util.HashSet ; 22 import java.util.List ; 23 import java.util.Locale ; 24 import java.util.Map ; 25 import java.util.MissingResourceException ; 26 import java.util.ResourceBundle ; 27 import java.util.Set ; 28 29 import javax.faces.component.UIViewRoot; 30 import javax.faces.context.FacesContext; 31 import javax.faces.webapp.UIComponentTag; 32 import javax.servlet.jsp.JspException ; 33 import javax.servlet.jsp.tagext.Tag ; 34 import javax.servlet.jsp.tagext.TagSupport ; 35 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 39 68 public class LoadBundleTag 69 extends TagSupport 70 { 71 private static final Log log = LogFactory.getLog(LoadBundleTag.class); 72 73 private String _basename; 74 private String _var; 75 76 public void setBasename(String basename) 77 { 78 _basename = basename; 79 } 80 81 public void setVar(String var) 82 { 83 _var = var; 84 } 85 86 public int doStartTag() throws JspException 87 { 88 FacesContext facesContext = FacesContext.getCurrentInstance(); 89 if (facesContext == null) 90 { 91 throw new JspException ("No faces context?!"); 92 } 93 94 UIViewRoot viewRoot = facesContext.getViewRoot(); 95 if (viewRoot == null) 96 { 97 throw new JspException ("No view root! LoadBundle must be nested inside <f:view> action."); 98 } 99 100 Locale locale = viewRoot.getLocale(); 101 if (locale == null) 102 { 103 locale = facesContext.getApplication().getDefaultLocale(); 104 } 105 106 String basename = null; 107 108 if (_basename!=null) { 109 if (UIComponentTag.isValueReference(_basename)) { 110 basename = (String )facesContext.getApplication().createValueBinding(_basename).getValue(facesContext); 111 } else { 112 basename = _basename; 113 } 114 } 115 116 final ResourceBundle bundle; 117 try 118 { 119 bundle = ResourceBundle.getBundle(basename, locale); 120 } 121 catch (MissingResourceException e) 122 { 123 log.error("Resource bundle '" + basename + "' could not be found."); 124 return Tag.SKIP_BODY; 125 } 126 127 facesContext.getExternalContext().getRequestMap().put(_var, 128 new BundleMap(bundle)); 129 return Tag.SKIP_BODY; 130 } 131 132 133 private static class BundleMap implements Map 134 { 135 private ResourceBundle _bundle; 136 private List _values; 137 138 public BundleMap(ResourceBundle bundle) 139 { 140 _bundle = bundle; 141 } 142 143 145 public Object get(Object key) 146 { 147 try { 148 return _bundle.getObject(key.toString()); 149 } catch (Exception e) { 150 return "MISSING: " + key + " :MISSING"; 151 } 152 } 153 154 public boolean isEmpty() 155 { 156 return !_bundle.getKeys().hasMoreElements(); 157 } 158 159 public boolean containsKey(Object key) 160 { 161 return _bundle.getObject(key.toString()) != null; 162 } 163 164 165 167 public Collection values() 168 { 169 if (_values == null) 170 { 171 _values = new ArrayList (); 172 for (Enumeration enumer = _bundle.getKeys(); enumer.hasMoreElements(); ) 173 { 174 String v = _bundle.getString((String )enumer.nextElement()); 175 _values.add(v); 176 } 177 } 178 return _values; 179 } 180 181 public int size() 182 { 183 return values().size(); 184 } 185 186 public boolean containsValue(Object value) 187 { 188 return values().contains(value); 189 } 190 191 public Set entrySet() 192 { 193 Set set = new HashSet (); 194 for (Enumeration enumer = _bundle.getKeys(); enumer.hasMoreElements(); ) 195 { 196 final String k = (String )enumer.nextElement(); 197 set.add(new Map.Entry () { 198 public Object getKey() 199 { 200 return k; 201 } 202 203 public Object getValue() 204 { 205 return _bundle.getObject(k); 206 } 207 208 public Object setValue(Object value) 209 { 210 throw new UnsupportedOperationException (this.getClass().getName() + " UnsupportedOperationException"); 211 } 212 }); 213 } 214 return set; 215 } 216 217 public Set keySet() 218 { 219 Set set = new HashSet (); 220 for (Enumeration enumer = _bundle.getKeys(); enumer.hasMoreElements(); ) 221 { 222 set.add(enumer.nextElement()); 223 } 224 return set; 225 } 226 227 228 230 public Object remove(Object key) 231 { 232 throw new UnsupportedOperationException (this.getClass().getName() + " UnsupportedOperationException"); 233 } 234 235 public void putAll(Map t) 236 { 237 throw new UnsupportedOperationException (this.getClass().getName() + " UnsupportedOperationException"); 238 } 239 240 public Object put(Object key, Object value) 241 { 242 throw new UnsupportedOperationException (this.getClass().getName() + " UnsupportedOperationException"); 243 } 244 245 public void clear() 246 { 247 throw new UnsupportedOperationException (this.getClass().getName() + " UnsupportedOperationException"); 248 } 249 250 } 251 252 } 253 | Popular Tags |