1 14 15 package com.sun.facelets.tag.jsf.core; 16 17 import java.io.IOException ; 18 import java.util.Collection ; 19 import java.util.Enumeration ; 20 import java.util.HashSet ; 21 import java.util.Locale ; 22 import java.util.Map ; 23 import java.util.MissingResourceException ; 24 import java.util.ResourceBundle ; 25 import java.util.Set ; 26 27 import javax.el.ELException; 28 import javax.faces.FacesException; 29 import javax.faces.component.UIComponent; 30 import javax.faces.component.UIViewRoot; 31 import javax.faces.context.FacesContext; 32 33 import com.sun.facelets.FaceletContext; 34 import com.sun.facelets.FaceletException; 35 import com.sun.facelets.tag.TagAttribute; 36 import com.sun.facelets.tag.TagAttributeException; 37 import com.sun.facelets.tag.TagConfig; 38 import com.sun.facelets.tag.TagHandler; 39 import com.sun.facelets.tag.jsf.ComponentSupport; 40 41 51 public final class LoadBundleHandler extends TagHandler { 52 53 private final static class ResourceBundleMap implements Map { 54 private final static class ResourceEntry implements Map.Entry { 55 56 protected final String key; 57 58 protected final String value; 59 60 public ResourceEntry(String key, String value) { 61 this.key = key; 62 this.value = value; 63 } 64 65 public Object getKey() { 66 return this.key; 67 } 68 69 public Object getValue() { 70 return this.value; 71 } 72 73 public Object setValue(Object value) { 74 throw new UnsupportedOperationException (); 75 } 76 77 public int hashCode() { 78 return this.key.hashCode(); 79 } 80 81 public boolean equals(Object obj) { 82 return (obj instanceof ResourceEntry && this.hashCode() == obj 83 .hashCode()); 84 } 85 } 86 87 protected final ResourceBundle bundle; 88 89 public ResourceBundleMap(ResourceBundle bundle) { 90 this.bundle = bundle; 91 } 92 93 public void clear() { 94 throw new UnsupportedOperationException (); 95 } 96 97 public boolean containsKey(Object key) { 98 try { 99 bundle.getString(key.toString()); 100 return true; 101 } catch (MissingResourceException e) { 102 return false; 103 } 104 } 105 106 public boolean containsValue(Object value) { 107 throw new UnsupportedOperationException (); 108 } 109 110 public Set entrySet() { 111 Enumeration e = this.bundle.getKeys(); 112 Set s = new HashSet (); 113 String k; 114 while (e.hasMoreElements()) { 115 k = (String ) e.nextElement(); 116 s.add(new ResourceEntry(k, this.bundle.getString(k))); 117 } 118 return s; 119 } 120 121 public Object get(Object key) { 122 return this.bundle.getObject((String ) key); 123 } 124 125 public boolean isEmpty() { 126 return false; 127 } 128 129 public Set keySet() { 130 Enumeration e = this.bundle.getKeys(); 131 Set s = new HashSet (); 132 while (e.hasMoreElements()) { 133 s.add(e.nextElement()); 134 } 135 return s; 136 } 137 138 public Object put(Object key, Object value) { 139 throw new UnsupportedOperationException (); 140 } 141 142 public void putAll(Map t) { 143 throw new UnsupportedOperationException (); 144 } 145 146 public Object remove(Object key) { 147 throw new UnsupportedOperationException (); 148 } 149 150 public int size() { 151 return this.keySet().size(); 152 } 153 154 public Collection values() { 155 Enumeration e = this.bundle.getKeys(); 156 Set s = new HashSet (); 157 while (e.hasMoreElements()) { 158 s.add(this.bundle.getObject((String ) e.nextElement())); 159 } 160 return s; 161 } 162 } 163 164 private final TagAttribute basename; 165 166 private final TagAttribute var; 167 168 171 public LoadBundleHandler(TagConfig config) { 172 super(config); 173 this.basename = this.getRequiredAttribute("basename"); 174 this.var = this.getRequiredAttribute("var"); 175 } 176 177 183 public void apply(FaceletContext ctx, UIComponent parent) 184 throws IOException , FacesException, FaceletException, ELException { 185 UIViewRoot root = ComponentSupport.getViewRoot(ctx, parent); 186 ResourceBundle bundle = null; 187 try { 188 String name = this.basename.getValue(ctx); 189 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 190 if (root != null && root.getLocale() != null) { 191 bundle = ResourceBundle.getBundle(name, root.getLocale(), cl); 192 } else { 193 bundle = ResourceBundle 194 .getBundle(name, Locale.getDefault(), cl); 195 } 196 } catch (Exception e) { 197 throw new TagAttributeException(this.tag, this.basename, e); 198 } 199 ResourceBundleMap map = new ResourceBundleMap(bundle); 200 FacesContext faces = ctx.getFacesContext(); 201 faces.getExternalContext().getRequestMap().put(this.var.getValue(ctx), 202 map); 203 } 204 } 205 | Popular Tags |