KickJava   Java API By Example, From Geeks To Geeks.

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


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 JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Set JavaDoc;
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: 720 $ ($Author: fgiust $)
24  */

25 public class HtmlAttributeMap extends HashMap JavaDoc
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 JavaDoc 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 JavaDoc buffer = new StringBuffer JavaDoc(size() * 30);
62
63         // get the entrySet
64
Set JavaDoc entrySet = entrySet();
65
66         Iterator JavaDoc iterator = entrySet.iterator();
67
68         // iterates on attributes
69
while (iterator.hasNext())
70         {
71             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) 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 }
87
Popular Tags