Save This Page
Home » displaytag-1.1.1-src » org » displaytag » util » [javadoc | source]
    1   /**
    2    * Licensed under the Artistic License; you may not use this file
    3    * except in compliance with the License.
    4    * You may obtain a copy of the License at
    5    *
    6    *      http://displaytag.sourceforge.net/license.html
    7    *
    8    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
    9    * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
   10    * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
   11    */
   12   package org.displaytag.util;
   13   
   14   import java.util.Iterator;
   15   import java.util.LinkedHashSet;
   16   import java.util.Set;
   17   
   18   import org.apache.commons.lang.StringUtils;
   19   import org.apache.commons.lang.UnhandledException;
   20   
   21   
   22   /**
   23    * Object used to contain html multiple attribute value (for the "class" attribute).
   24    * @author Fabrizio Giustina
   25    * @version $Revision: 1081 $ ($Author: fgiust $)
   26    */
   27   public class MultipleHtmlAttribute implements Cloneable
   28   {
   29   
   30       /**
   31        * Sets containing splitted attribute values.
   32        */
   33       private Set attributeSet;
   34   
   35       /**
   36        * Constructor for MultipleHtmlAttribute.
   37        * @param attributeValue String
   38        */
   39       public MultipleHtmlAttribute(String attributeValue)
   40       {
   41           this.attributeSet = new LinkedHashSet();
   42           addAllAttributesFromArray(StringUtils.split(attributeValue));
   43       }
   44   
   45       /**
   46        * Adds attributes from an array.
   47        * @param attributes Object[] Array containing attributes
   48        */
   49       private void addAllAttributesFromArray(String[] attributes)
   50       {
   51           if (attributes == null)
   52           {
   53               return;
   54           }
   55   
   56           // number of attributes to add
   57           int length = attributes.length;
   58   
   59           // add all the splitted attributes
   60           for (int j = 0; j < length; j++)
   61           {
   62   
   63               // don't add if empty
   64               if (!StringUtils.isEmpty(attributes[j]))
   65               {
   66                   this.attributeSet.add(attributes[j]);
   67               }
   68   
   69           }
   70       }
   71   
   72       /**
   73        * Returns the list of attributes separated by a space.
   74        * @return String
   75        */
   76       public String toString()
   77       {
   78           StringBuffer buffer = new StringBuffer();
   79   
   80           Iterator iterator = this.attributeSet.iterator();
   81   
   82           while (iterator.hasNext())
   83           {
   84               // apend next value
   85               buffer.append(iterator.next());
   86               if (iterator.hasNext())
   87               {
   88                   // append a space if there are more
   89                   buffer.append(' ');
   90               }
   91           }
   92   
   93           return buffer.toString();
   94       }
   95   
   96       /**
   97        * Adds a value to the attribute.
   98        * @param attributeValue value to add to the attribute
   99        */
  100       public void addAttributeValue(String attributeValue)
  101       {
  102           // don't add if empty
  103           if (!StringUtils.isEmpty(attributeValue))
  104           {
  105               this.attributeSet.add(attributeValue);
  106           }
  107   
  108       }
  109   
  110       /**
  111        * Return true if this MultipleHtmlValue doesn't store any value.
  112        * @return <code>true</code> if this MultipleHtmlValue doesn't store any value
  113        */
  114       public boolean isEmpty()
  115       {
  116           return attributeSet.isEmpty();
  117       }
  118   
  119       /**
  120        * @see java.lang.Object#clone()
  121        */
  122       protected Object clone()
  123       {
  124           MultipleHtmlAttribute clone;
  125   
  126           try
  127           {
  128               clone = (MultipleHtmlAttribute) super.clone();
  129           }
  130           catch (CloneNotSupportedException e)
  131           {
  132               // should never happen
  133               throw new UnhandledException(e);
  134           }
  135   
  136           // copy attributes
  137           clone.addAllAttributesFromArray((String[]) this.attributeSet.toArray(new String[this.attributeSet.size()]));
  138   
  139           return clone;
  140       }
  141   
  142   }

Save This Page
Home » displaytag-1.1.1-src » org » displaytag » util » [javadoc | source]