KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > displaytag > util > MultipleHtmlAttribute


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 JavaDoc;
15 import java.util.LinkedHashSet JavaDoc;
16 import java.util.Set JavaDoc;
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: 956 $ ($Author: fgiust $)
26  */

27 public class MultipleHtmlAttribute implements Cloneable JavaDoc
28 {
29
30     /**
31      * Sets containing splitted attribute values.
32      */

33     private Set JavaDoc attributeSet;
34
35     /**
36      * Constructor for MultipleHtmlAttribute.
37      * @param attributeValue String
38      */

39     public MultipleHtmlAttribute(String JavaDoc attributeValue)
40     {
41
42         // split initial attribute
43
String JavaDoc[] attributes = StringUtils.split(attributeValue);
44
45         addAllAttributesFromArray(attributes);
46     }
47
48     /**
49      * Adds attributes from an array.
50      * @param attributes Object[] Array containing attributes
51      */

52     private void addAllAttributesFromArray(String JavaDoc[] attributes)
53     {
54
55         // number of attributes to add
56
int length = attributes.length;
57
58         // create new HashSet with correct size
59
this.attributeSet = new LinkedHashSet JavaDoc(length);
60
61         // add all the splitted attributes
62
for (int j = 0; j < length; j++)
63         {
64
65             // don't add if empty
66
if (!StringUtils.isEmpty(attributes[j]))
67             {
68                 this.attributeSet.add(attributes[j]);
69             }
70
71         }
72     }
73
74     /**
75      * Returns the list of attributes separated by a space.
76      * @return String
77      */

78     public String JavaDoc toString()
79     {
80         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
81
82         Iterator JavaDoc iterator = this.attributeSet.iterator();
83
84         while (iterator.hasNext())
85         {
86             // apend next value
87
buffer.append(iterator.next());
88             if (iterator.hasNext())
89             {
90                 // append a space if there are more
91
buffer.append(' ');
92             }
93         }
94
95         return buffer.toString();
96     }
97
98     /**
99      * Adds a value to the attribute.
100      * @param attributeValue value to add to the attribute
101      */

102     public void addAttributeValue(String JavaDoc attributeValue)
103     {
104         // don't add if empty
105
if (!StringUtils.isEmpty(attributeValue))
106         {
107             this.attributeSet.add(attributeValue);
108         }
109
110     }
111
112     /**
113      * @see java.lang.Object#clone()
114      */

115     protected Object JavaDoc clone()
116     {
117         MultipleHtmlAttribute clone;
118
119         try
120         {
121             clone = (MultipleHtmlAttribute) super.clone();
122         }
123         catch (CloneNotSupportedException JavaDoc e)
124         {
125             // should never happen
126
throw new UnhandledException(e);
127         }
128
129         // copy attributes
130
clone.addAllAttributesFromArray((String JavaDoc[]) this.attributeSet.toArray(new String JavaDoc[this.attributeSet.size()]));
131
132         return clone;
133     }
134
135 }
Popular Tags