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 45 46 public class OptionsTag extends TagSupport { 47 48 51 protected static MessageResources messages = 52 MessageResources.getMessageResources(Constants.Package + ".LocalStrings"); 53 54 59 protected String collection = null; 60 61 public String getCollection() { 62 return (this.collection); 63 } 64 65 public void setCollection(String collection) { 66 this.collection = collection; 67 } 68 69 72 protected boolean filter = true; 73 74 public boolean getFilter() { 75 return filter; 76 } 77 78 public void setFilter(boolean filter) { 79 this.filter = filter; 80 } 81 82 85 protected String labelName = null; 86 87 public String getLabelName() { 88 return labelName; 89 } 90 91 public void setLabelName(String labelName) { 92 this.labelName = labelName; 93 } 94 95 98 protected String labelProperty = null; 99 100 public String getLabelProperty() { 101 return labelProperty; 102 } 103 104 public void setLabelProperty(String labelProperty) { 105 this.labelProperty = labelProperty; 106 } 107 108 111 protected String name = null; 112 113 public String getName() { 114 return name; 115 } 116 117 public void setName(String name) { 118 this.name = name; 119 } 120 121 124 protected String property = null; 125 126 public String getProperty() { 127 return property; 128 } 129 130 public void setProperty(String property) { 131 this.property = property; 132 } 133 134 137 private String style = null; 138 139 public String getStyle() { 140 return style; 141 } 142 143 public void setStyle(String style) { 144 this.style = style; 145 } 146 147 150 private String styleClass = null; 151 152 public String getStyleClass() { 153 return styleClass; 154 } 155 156 public void setStyleClass(String styleClass) { 157 this.styleClass = styleClass; 158 } 159 160 165 166 public int doStartTag() throws JspException { 167 return SKIP_BODY; 168 } 169 170 175 public int doEndTag() throws JspException { 176 177 SelectTag selectTag = (SelectTag) pageContext.getAttribute(Constants.SELECT_KEY); 179 if (selectTag == null) { 180 throw new JspException (messages.getMessage("optionsTag.select")); 181 } 182 StringBuffer sb = new StringBuffer (); 183 184 if (collection != null) { 186 Iterator collIterator = getIterator(collection, null); 187 while (collIterator.hasNext()) { 188 189 Object bean = collIterator.next(); 190 Object value = null; 191 Object label = null; 192 193 try { 194 value = PropertyUtils.getProperty(bean, property); 195 if (value == null) { 196 value = ""; 197 } 198 } catch (IllegalAccessException e) { 199 throw new JspException ( 200 messages.getMessage("getter.access", property, collection)); 201 } catch (InvocationTargetException e) { 202 Throwable t = e.getTargetException(); 203 throw new JspException ( 204 messages.getMessage("getter.result", property, t.toString())); 205 } catch (NoSuchMethodException e) { 206 throw new JspException ( 207 messages.getMessage("getter.method", property, collection)); 208 } 209 210 try { 211 if (labelProperty != null) { 212 label = PropertyUtils.getProperty(bean, labelProperty); 213 } else { 214 label = value; 215 } 216 217 if (label == null) { 218 label = ""; 219 } 220 } catch (IllegalAccessException e) { 221 throw new JspException ( 222 messages.getMessage("getter.access", labelProperty, collection)); 223 } catch (InvocationTargetException e) { 224 Throwable t = e.getTargetException(); 225 throw new JspException ( 226 messages.getMessage("getter.result", labelProperty, t.toString())); 227 } catch (NoSuchMethodException e) { 228 throw new JspException ( 229 messages.getMessage("getter.method", labelProperty, collection)); 230 } 231 232 String stringValue = value.toString(); 233 addOption(sb, stringValue, label.toString(), selectTag.isMatched(stringValue)); 234 235 } 236 237 } 238 239 else { 241 242 Iterator valuesIterator = getIterator(name, property); 244 Iterator labelsIterator = null; 245 if ((labelName == null) && (labelProperty == null)) { 246 labelsIterator = getIterator(name, property); } else { 248 labelsIterator = getIterator(labelName, labelProperty); 249 } 250 251 while (valuesIterator.hasNext()) { 253 Object valueObject = valuesIterator.next(); 254 if (valueObject == null) { 255 valueObject = ""; 256 } 257 String value = valueObject.toString(); 258 String label = value; 259 if (labelsIterator.hasNext()) { 260 Object labelObject = labelsIterator.next(); 261 if (labelObject == null) { 262 labelObject = ""; 263 } 264 label = labelObject.toString(); 265 } 266 addOption(sb, value, label, selectTag.isMatched(value)); 267 } 268 } 269 270 TagUtils.getInstance().write(pageContext, sb.toString()); 271 272 return EVAL_PAGE; 273 274 } 275 276 279 public void release() { 280 281 super.release(); 282 collection = null; 283 filter = true; 284 labelName = null; 285 labelProperty = null; 286 name = null; 287 property = null; 288 style = null; 289 styleClass = null; 290 } 291 292 294 312 protected void addOption(StringBuffer sb, String value, String label, boolean matched) { 313 314 sb.append("<option value=\""); 315 if (filter) { 316 sb.append(TagUtils.getInstance().filter(value)); 317 } else { 318 sb.append(value); 319 } 320 sb.append("\""); 321 if (matched) { 322 sb.append(" selected=\"selected\""); 323 } 324 if (style != null) { 325 sb.append(" style=\""); 326 sb.append(style); 327 sb.append("\""); 328 } 329 if (styleClass != null) { 330 sb.append(" class=\""); 331 sb.append(styleClass); 332 sb.append("\""); 333 } 334 335 sb.append(">"); 336 337 if (filter) { 338 sb.append(TagUtils.getInstance().filter(label)); 339 } else { 340 sb.append(label); 341 } 342 343 sb.append("</option>\r\n"); 344 345 } 346 347 356 protected Iterator getIterator(String name, String property) throws JspException { 357 358 String beanName = name; 360 if (beanName == null) { 361 beanName = Constants.BEAN_KEY; 362 } 363 364 Object bean = TagUtils.getInstance().lookup(pageContext, beanName, null); 365 if (bean == null) { 366 throw new JspException (messages.getMessage("getter.bean", beanName)); 367 } 368 369 Object collection = bean; 371 if (property != null) { 372 try { 373 collection = PropertyUtils.getProperty(bean, property); 374 if (collection == null) { 375 throw new JspException (messages.getMessage("getter.property", property)); 376 } 377 } catch (IllegalAccessException e) { 378 throw new JspException (messages.getMessage("getter.access", property, name)); 379 } catch (InvocationTargetException e) { 380 Throwable t = e.getTargetException(); 381 throw new JspException ( 382 messages.getMessage("getter.result", property, t.toString())); 383 } catch (NoSuchMethodException e) { 384 throw new JspException (messages.getMessage("getter.method", property, name)); 385 } 386 } 387 388 if (collection.getClass().isArray()) { 390 collection = Arrays.asList((Object []) collection); 391 } 392 393 if (collection instanceof Collection ) { 394 return (((Collection ) collection).iterator()); 395 396 } else if (collection instanceof Iterator ) { 397 return ((Iterator ) collection); 398 399 } else if (collection instanceof Map ) { 400 return (((Map ) collection).entrySet().iterator()); 401 402 } else if (collection instanceof Enumeration ) { 403 return new IteratorAdapter((Enumeration ) collection); 404 405 } else { 406 throw new JspException ( 407 messages.getMessage("optionsTag.iterator", collection.toString())); 408 } 409 } 410 411 } 412 | Popular Tags |