Save This Page
Home » struts-1.3.9-src » org.apache.struts.taglib » html » [javadoc | source]
    1   /*
    2    * $Id: OptionsTag.java 190807 2005-06-15 21:01:56Z jholmes $ 
    3    *
    4    * Copyright 1999-2004 The Apache Software Foundation.
    5    * 
    6    * Licensed under the Apache License, Version 2.0 (the "License");
    7    * you may not use this file except in compliance with the License.
    8    * You may obtain a copy of the License at
    9    * 
   10    *      http://www.apache.org/licenses/LICENSE-2.0
   11    * 
   12    * Unless required by applicable law or agreed to in writing, software
   13    * distributed under the License is distributed on an "AS IS" BASIS,
   14    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   15    * See the License for the specific language governing permissions and
   16    * limitations under the License.
   17    */
   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   /**
   37    * Tag for creating multiple <select> options from a collection.  The
   38    * associated values displayed to the user may optionally be specified by a
   39    * second collection, or will be the same as the values themselves.  Each
   40    * collection may be an array of objects, a Collection, an Enumeration,
   41    * an Iterator, or a Map.
   42    * <b>NOTE</b> - This tag requires a Java2 (JDK 1.2 or later) platform.
   43    *
   44    */
   45   
   46   public class OptionsTag extends TagSupport {
   47   
   48       /**
   49        * The message resources for this package.
   50        */
   51       protected static MessageResources messages =
   52           MessageResources.getMessageResources(Constants.Package + ".LocalStrings");
   53   
   54       /**
   55        * The name of the collection containing beans that have properties to
   56        * provide both the values and the labels (identified by the
   57        * <code>property</code> and <code>labelProperty</code> attributes).
   58        */
   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       /**
   70        * Should the label values be filtered for HTML sensitive characters?
   71        */
   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       /**
   83        * The name of the bean containing the labels collection.
   84        */
   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       /**
   96        * The bean property containing the labels collection.
   97        */
   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       /**
  109        * The name of the bean containing the values collection.
  110        */
  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       /**
  122        * The name of the property to use to build the values collection.
  123        */
  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       /**
  135        * The style associated with this tag.
  136        */
  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       /**
  148        * The named style class associated with this tag.
  149        */
  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       /**
  161        * Process the start of this tag.
  162        *
  163        * @exception JspException if a JSP exception has occurred
  164        */
  165   
  166       public int doStartTag() throws JspException {
  167           return SKIP_BODY;
  168       }
  169   
  170       /**
  171        * Process the end of this tag.
  172        *
  173        * @exception JspException if a JSP exception has occurred
  174        */
  175       public int doEndTag() throws JspException {
  176   
  177           // Acquire the select tag we are associated with
  178           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 a collection was specified, use that mode to render options
  185           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           // Otherwise, use the separate iterators mode to render options
  240           else {
  241   
  242               // Construct iterators for the values and labels collections
  243               Iterator valuesIterator = getIterator(name, property);
  244               Iterator labelsIterator = null;
  245               if ((labelName != null) || (labelProperty != null)) {
  246                   labelsIterator = getIterator(labelName, labelProperty);
  247               }
  248   
  249               // Render the options tags for each element of the values coll.
  250               while (valuesIterator.hasNext()) {
  251                   Object valueObject = valuesIterator.next();
  252                   if (valueObject == null) {
  253                       valueObject = "";
  254                   }
  255                   String value = valueObject.toString();
  256                   String label = value;
  257                   if ((labelsIterator != null) && labelsIterator.hasNext()) {
  258                       Object labelObject = labelsIterator.next();
  259                       if (labelObject == null) {
  260                           labelObject = "";
  261                       }
  262                       label = labelObject.toString();
  263                   }
  264                   addOption(sb, value, label, selectTag.isMatched(value));
  265               }
  266           }
  267   
  268           TagUtils.getInstance().write(pageContext, sb.toString());
  269   
  270           return EVAL_PAGE;
  271   
  272       }
  273   
  274       /**
  275        * Release any acquired resources.
  276        */
  277       public void release() {
  278   
  279           super.release();
  280           collection = null;
  281           filter = true;
  282           labelName = null;
  283           labelProperty = null;
  284           name = null;
  285           property = null;
  286           style = null;
  287           styleClass = null;
  288       }
  289   
  290       // ------------------------------------------------------ Protected Methods
  291   
  292       /**
  293        * Add an option element to the specified StringBuffer based on the
  294        * specified parameters.
  295        *<p>
  296        * Note that this tag specifically does not support the
  297        * <code>styleId</code> tag attribute, which causes the HTML
  298        * <code>id</code> attribute to be emitted.  This is because the HTML
  299        * specification states that all "id" attributes in a document have to be
  300        * unique.  This tag will likely generate more than one <code>option</code>
  301        * element element, but it cannot use the same <code>id</code> value.  It's
  302        * conceivable some sort of mechanism to supply an array of <code>id</code>
  303        * values could be devised, but that doesn't seem to be worth the trouble.
  304        *
  305        * @param sb StringBuffer accumulating our results
  306        * @param value Value to be returned to the server for this option
  307        * @param label Value to be shown to the user for this option
  308        * @param matched Should this value be marked as selected?
  309        */
  310       protected void addOption(StringBuffer sb, String value, String label, boolean matched) {
  311   
  312           sb.append("<option value=\"");
  313           if (filter) {
  314               sb.append(TagUtils.getInstance().filter(value));
  315           } else {
  316               sb.append(value);
  317           }
  318           sb.append("\"");
  319           if (matched) {
  320               sb.append(" selected=\"selected\"");
  321           }
  322           if (style != null) {
  323               sb.append(" style=\"");
  324               sb.append(style);
  325               sb.append("\"");
  326           }
  327           if (styleClass != null) {
  328               sb.append(" class=\"");
  329               sb.append(styleClass);
  330               sb.append("\"");
  331           }
  332           
  333           sb.append(">");
  334           
  335           if (filter) {
  336               sb.append(TagUtils.getInstance().filter(label));
  337           } else {
  338               sb.append(label);
  339           }
  340           
  341           sb.append("</option>\r\n");
  342   
  343       }
  344   
  345       /**
  346        * Return an iterator for the option labels or values, based on our
  347        * configured properties.
  348        *
  349        * @param name Name of the bean attribute (if any)
  350        * @param property Name of the bean property (if any)
  351        *
  352        * @exception JspException if an error occurs
  353        */
  354       protected Iterator getIterator(String name, String property) throws JspException {
  355   
  356           // Identify the bean containing our collection
  357           String beanName = name;
  358           if (beanName == null) {
  359               beanName = Constants.BEAN_KEY;
  360           }
  361   
  362           Object bean = TagUtils.getInstance().lookup(pageContext, beanName, null);
  363           if (bean == null) {
  364               throw new JspException(messages.getMessage("getter.bean", beanName));
  365           }
  366   
  367           // Identify the collection itself
  368           Object collection = bean;
  369           if (property != null) {
  370               try {
  371                   collection = PropertyUtils.getProperty(bean, property);
  372                   if (collection == null) {
  373                       throw new JspException(messages.getMessage("getter.property", property));
  374                   }
  375               } catch (IllegalAccessException e) {
  376                   throw new JspException(messages.getMessage("getter.access", property, name));
  377               } catch (InvocationTargetException e) {
  378                   Throwable t = e.getTargetException();
  379                   throw new JspException(
  380                       messages.getMessage("getter.result", property, t.toString()));
  381               } catch (NoSuchMethodException e) {
  382                   throw new JspException(messages.getMessage("getter.method", property, name));
  383               }
  384           }
  385   
  386           // Construct and return an appropriate iterator
  387           if (collection.getClass().isArray()) {
  388               collection = Arrays.asList((Object[]) collection);
  389           }
  390   
  391           if (collection instanceof Collection) {
  392               return (((Collection) collection).iterator());
  393   
  394           } else if (collection instanceof Iterator) {
  395               return ((Iterator) collection);
  396   
  397           } else if (collection instanceof Map) {
  398               return (((Map) collection).entrySet().iterator());
  399   
  400           } else if (collection instanceof Enumeration) {
  401               return new IteratorAdapter((Enumeration) collection);
  402   
  403           } else {
  404               throw new JspException(
  405                   messages.getMessage("optionsTag.iterator", collection.toString()));
  406           }
  407       }
  408   
  409   }

Save This Page
Home » struts-1.3.9-src » org.apache.struts.taglib » html » [javadoc | source]