1 18 19 package org.apache.struts.taglib.html; 20 21 import java.lang.reflect.InvocationTargetException ; 22 import java.util.Arrays ; 23 import java.util.Collection ; 24 import java.util.Enumeration ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 28 import javax.servlet.jsp.JspException ; 29 import javax.servlet.jsp.tagext.TagSupport ; 30 31 import org.apache.commons.beanutils.PropertyUtils; 32 import org.apache.struts.util.IteratorAdapter; 33 import org.apache.struts.taglib.TagUtils; 34 import org.apache.struts.util.MessageResources; 35 36 51 public class OptionsCollectionTag extends TagSupport { 52 53 55 58 protected static MessageResources messages = 59 MessageResources.getMessageResources(Constants.Package + ".LocalStrings"); 60 61 63 66 protected boolean filter = true; 67 68 public boolean getFilter() { 69 return filter; 70 } 71 72 public void setFilter(boolean filter) { 73 this.filter = filter; 74 } 75 76 79 protected String label = "label"; 80 81 public String getLabel() { 82 return label; 83 } 84 85 public void setLabel(String label) { 86 this.label = label; 87 } 88 89 92 protected String name = Constants.BEAN_KEY; 93 94 public String getName() { 95 return name; 96 } 97 98 public void setName(String name) { 99 this.name = name; 100 } 101 102 105 protected String property = null; 106 107 public String getProperty() { 108 return property; 109 } 110 111 public void setProperty(String property) { 112 this.property = property; 113 } 114 115 118 private String style = null; 119 120 public String getStyle() { 121 return style; 122 } 123 124 public void setStyle(String style) { 125 this.style = style; 126 } 127 128 131 private String styleClass = null; 132 133 public String getStyleClass() { 134 return styleClass; 135 } 136 137 public void setStyleClass(String styleClass) { 138 this.styleClass = styleClass; 139 } 140 141 144 protected String value = "value"; 145 146 public String getValue() { 147 return value; 148 } 149 150 public void setValue(String value) { 151 this.value = value; 152 } 153 154 156 161 public int doStartTag() throws JspException { 162 163 SelectTag selectTag = (SelectTag) pageContext.getAttribute(Constants.SELECT_KEY); 165 166 if (selectTag == null) { 167 JspException e = new JspException (messages.getMessage("optionsCollectionTag.select")); 168 TagUtils.getInstance().saveException(pageContext, e); 169 throw e; 170 } 171 172 Object collection = TagUtils.getInstance().lookup(pageContext, name, property, null); 174 175 if (collection == null) { 176 JspException e = 177 new JspException (messages.getMessage("optionsCollectionTag.collection")); 178 TagUtils.getInstance().saveException(pageContext, e); 179 throw e; 180 } 181 182 Iterator iter = getIterator(collection); 184 185 StringBuffer sb = new StringBuffer (); 186 187 while (iter.hasNext()) { 189 190 Object bean = iter.next(); 191 Object beanLabel = null; 192 Object beanValue = null; 193 194 try { 196 beanLabel = PropertyUtils.getProperty(bean, label); 197 if (beanLabel == null) { 198 beanLabel = ""; 199 } 200 } catch (IllegalAccessException e) { 201 JspException jspe = 202 new JspException (messages.getMessage("getter.access", label, bean)); 203 TagUtils.getInstance().saveException(pageContext, jspe); 204 throw jspe; 205 } catch (InvocationTargetException e) { 206 Throwable t = e.getTargetException(); 207 JspException jspe = 208 new JspException (messages.getMessage("getter.result", label, t.toString())); 209 TagUtils.getInstance().saveException(pageContext, jspe); 210 throw jspe; 211 } catch (NoSuchMethodException e) { 212 JspException jspe = 213 new JspException (messages.getMessage("getter.method", label, bean)); 214 TagUtils.getInstance().saveException(pageContext, jspe); 215 throw jspe; 216 } 217 218 try { 220 beanValue = PropertyUtils.getProperty(bean, value); 221 if (beanValue == null) { 222 beanValue = ""; 223 } 224 } catch (IllegalAccessException e) { 225 JspException jspe = 226 new JspException (messages.getMessage("getter.access", value, bean)); 227 TagUtils.getInstance().saveException(pageContext, jspe); 228 throw jspe; 229 } catch (InvocationTargetException e) { 230 Throwable t = e.getTargetException(); 231 JspException jspe = 232 new JspException (messages.getMessage("getter.result", value, t.toString())); 233 TagUtils.getInstance().saveException(pageContext, jspe); 234 throw jspe; 235 } catch (NoSuchMethodException e) { 236 JspException jspe = 237 new JspException (messages.getMessage("getter.method", value, bean)); 238 TagUtils.getInstance().saveException(pageContext, jspe); 239 throw jspe; 240 } 241 242 String stringLabel = beanLabel.toString(); 243 String stringValue = beanValue.toString(); 244 245 addOption(sb, stringLabel, stringValue, selectTag.isMatched(stringValue)); 247 } 248 249 TagUtils.getInstance().write(pageContext, sb.toString()); 250 251 return SKIP_BODY; 252 } 253 254 257 public void release() { 258 super.release(); 259 filter = true; 260 label = "label"; 261 name = Constants.BEAN_KEY; 262 property = null; 263 style = null; 264 styleClass = null; 265 value = "value"; 266 } 267 268 270 288 protected void addOption(StringBuffer sb, String label, String value, boolean matched) { 289 290 sb.append("<option value=\""); 291 if (filter) { 292 sb.append(TagUtils.getInstance().filter(value)); 293 } else { 294 sb.append(value); 295 } 296 sb.append("\""); 297 if (matched) { 298 sb.append(" selected=\"selected\""); 299 } 300 if (style != null) { 301 sb.append(" style=\""); 302 sb.append(style); 303 sb.append("\""); 304 } 305 if (styleClass != null) { 306 sb.append(" class=\""); 307 sb.append(styleClass); 308 sb.append("\""); 309 } 310 311 sb.append(">"); 312 313 if (filter) { 314 sb.append(TagUtils.getInstance().filter(label)); 315 } else { 316 sb.append(label); 317 } 318 319 sb.append("</option>\r\n"); 320 321 } 322 323 330 protected Iterator getIterator(Object collection) throws JspException { 331 332 if (collection.getClass().isArray()) { 333 collection = Arrays.asList((Object []) collection); 334 } 335 336 if (collection instanceof Collection ) { 337 return (((Collection ) collection).iterator()); 338 339 } else if (collection instanceof Iterator ) { 340 return ((Iterator ) collection); 341 342 } else if (collection instanceof Map ) { 343 return (((Map ) collection).entrySet().iterator()); 344 345 } else if (collection instanceof Enumeration ) { 346 return new IteratorAdapter((Enumeration ) collection); 347 348 } else { 349 throw new JspException ( 350 messages.getMessage("optionsCollectionTag.iterator", collection.toString())); 351 } 352 } 353 354 } 355 | Popular Tags |