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.HashMap;
15 import java.util.Iterator;
16 import java.util.Map;
17 import java.util.Set;
18
19
20 /**
21 * Extends Map providing only a different toString() method which can be used in printing attributes inside an html tag.
22 * @author Fabrizio Giustina
23 * @version $Revision: 1081 $ ($Author: fgiust $)
24 */
25 public class HtmlAttributeMap extends HashMap
26 {
27
28 /**
29 * D1597A17A6.
30 */
31 private static final long serialVersionUID = 899149338534L;
32
33 /**
34 * Attribute value delimiter.
35 */
36 private static final char DELIMITER = '"';
37
38 /**
39 * character between name and value.
40 */
41 private static final char EQUALS = '=';
42
43 /**
44 * space before any attribute.
45 */
46 private static final char SPACE = ' ';
47
48 /**
49 * toString method: returns attributes in the format: attributename="attributevalue" attr2="attrValue2" ...
50 * @return String representation of the HtmlAttributeMap
51 */
52 public String toString()
53 {
54 // fast exit when no attribute are present
55 if (size() == 0)
56 {
57 return TagConstants.EMPTY_STRING;
58 }
59
60 // buffer extimated in number of attributes * 30
61 StringBuffer buffer = new StringBuffer(size() * 30);
62
63 // get the entrySet
64 Set entrySet = entrySet();
65
66 Iterator iterator = entrySet.iterator();
67
68 // iterates on attributes
69 while (iterator.hasNext())
70 {
71 Map.Entry entry = (Map.Entry) iterator.next();
72
73 // append a new atribute
74 buffer
75 .append(SPACE)
76 .append(entry.getKey())
77 .append(EQUALS)
78 .append(DELIMITER)
79 .append(entry.getValue())
80 .append(DELIMITER);
81 }
82
83 // return
84 return buffer.toString();
85 }
86 }