1 16 package org.apache.cocoon.faces.taglib; 17 18 import org.apache.cocoon.taglib.TagSupport; 19 import org.apache.cocoon.util.ClassUtils; 20 21 import org.apache.cocoon.faces.FacesUtils; 22 import org.xml.sax.Attributes ; 23 import org.xml.sax.SAXException ; 24 25 import javax.faces.FacesException; 26 import javax.faces.context.FacesContext; 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.Enumeration ; 30 import java.util.HashMap ; 31 import java.util.HashSet ; 32 import java.util.Map ; 33 import java.util.ResourceBundle ; 34 import java.util.Set ; 35 36 39 public class LoadBundleTag extends TagSupport { 40 41 private String basename; 42 private String var; 43 44 public void setBasename(String basename) { 45 this.basename = basename; 46 } 47 48 public void setVar(String var) { 49 this.var = var; 50 } 51 52 public int doStartTag(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 53 final FacesContext context = FacesUtils.getFacesContext(this, objectModel); 54 55 final String basename = (String ) FacesUtils.evaluate(context, this.basename); 56 57 final ResourceBundle bundle = ResourceBundle.getBundle(basename, 58 context.getViewRoot().getLocale(), 59 ClassUtils.getClassLoader()); 60 61 if (bundle == null) { 62 throw new FacesException("Tag <" + getClass().getName() + "> " + 63 "could not find ResourceBundle for <" + basename + ">"); 64 } 65 66 context.getExternalContext().getRequestMap().put(this.var, new BundleMap(bundle)); 67 68 return SKIP_BODY; 69 } 70 71 public void recycle() { 72 super.recycle(); 73 this.basename = null; 74 this.var = null; 75 } 76 } 77 78 class BundleMap implements Map { 79 private ResourceBundle bundle; 80 81 public BundleMap (ResourceBundle bundle) { 82 this.bundle = bundle; 83 } 84 85 public void clear() { 86 throw new UnsupportedOperationException ("BundleMap does not support clear()"); 87 } 88 89 public boolean containsKey(Object key) { 90 if (key == null) { 91 return false; 92 } 93 94 try { 95 this.bundle.getObject(key.toString()); 96 return true; 97 } catch (Exception e) { 98 return false; 99 } 100 } 101 102 public boolean containsValue(Object value) { 103 if (value == null) { 104 return false; 105 } 106 107 for (Enumeration i = this.bundle.getKeys(); i.hasMoreElements();) { 108 Object obj = bundle.getObject((String ) i.nextElement()); 109 if (value == obj || value.equals(obj)) { 110 return true; 111 } 112 } 113 114 return false; 115 } 116 117 public Set entrySet() { 118 final HashMap entries = new HashMap (); 119 for (Enumeration i = this.bundle.getKeys(); i.hasMoreElements();) { 120 String key = (String ) i.nextElement(); 121 entries.put(key, this.bundle.getObject(key)); 122 } 123 124 return entries.entrySet(); 125 } 126 127 public boolean equals(Object obj) { 128 if (obj == null || !(obj instanceof Map )) { 129 return false; 130 } 131 132 return entrySet().equals(((Map ) obj).entrySet()); 133 } 134 135 public Object get(Object key) { 136 if (key == null) { 137 return null; 138 } 139 140 return bundle.getObject(key.toString()); 141 } 142 143 public int hashCode() { 144 return this.bundle.hashCode(); 145 } 146 147 public boolean isEmpty() { 148 return !this.bundle.getKeys().hasMoreElements(); 149 } 150 151 public Set keySet() { 152 final Set keys = new HashSet (); 153 for (Enumeration i = this.bundle.getKeys(); i.hasMoreElements();) { 154 keys.add(i.nextElement()); 155 } 156 return keys; 157 } 158 159 public Object put(Object k, Object v) { 160 throw new UnsupportedOperationException ("BundleMap does not support put()"); 161 } 162 163 public void putAll(Map t) { 164 throw new UnsupportedOperationException ("BundleMap does not support putAll()"); 165 } 166 167 public Object remove(Object k) { 168 throw new UnsupportedOperationException ("BundleMap does not support remove()"); 169 } 170 171 public int size() { 172 int result = 0; 173 for (Enumeration i = this.bundle.getKeys(); i.hasMoreElements();) { 174 i.nextElement(); 175 result ++; 176 } 177 178 return result; 179 } 180 181 public Collection values() { 182 ArrayList values = new ArrayList (); 183 for (Enumeration i = this.bundle.getKeys(); i.hasMoreElements();) { 184 values.add(this.bundle.getObject((String ) i.nextElement())); 185 } 186 return values; 187 } 188 } 189 | Popular Tags |