Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
XHtmlTagTool |
|
| 0.0;0 |
1 | /* | |
2 | * FCKeditor - The text editor for Internet - http://www.fckeditor.net | |
3 | * Copyright (C) 2003-2008 Frederico Caldeira Knabben | |
4 | * | |
5 | * == BEGIN LICENSE == | |
6 | * | |
7 | * Licensed under the terms of any of the following licenses at your | |
8 | * choice: | |
9 | * | |
10 | * - GNU General Public License Version 2 or later (the "GPL") | |
11 | * http://www.gnu.org/licenses/gpl.html | |
12 | * | |
13 | * - GNU Lesser General Public License Version 2.1 or later (the "LGPL") | |
14 | * http://www.gnu.org/licenses/lgpl.html | |
15 | * | |
16 | * - Mozilla Public License Version 1.1 or later (the "MPL") | |
17 | * http://www.mozilla.org/MPL/MPL-1.1.html | |
18 | * | |
19 | * == END LICENSE == | |
20 | */ | |
21 | package net.fckeditor.tool; | |
22 | ||
23 | import java.util.HashMap; | |
24 | import java.util.Map; | |
25 | ||
26 | /** | |
27 | * Tool to construct a XHTML tag.<br> | |
28 | * <br> | |
29 | * Usage: | |
30 | * <pre> | |
31 | * XHtmlTagTool tag = XHtmlTagTool("a", "link"); | |
32 | * tag.addAttribute("href", "http://google.com"); | |
33 | * tag.toString();: <a href="http://google.com">link</a> | |
34 | * </pre> | |
35 | * | |
36 | * <em>Hint</em>: | |
37 | * <ul> | |
38 | * <li>Attributes are not ordered.</li> | |
39 | * <li>If your tag shouldn't have a value but the tag has to close with '</[tagname]>', set | |
40 | * the value to {@link XHtmlTagTool#SPACE}.</li> | |
41 | * </ul> | |
42 | * | |
43 | * @version $Id: XHtmlTagTool.java 2151 2008-07-02 22:03:15Z mosipov $ | |
44 | */ | |
45 | public class XHtmlTagTool { | |
46 | ||
47 | /** Name of the tag. */ | |
48 | private String name; | |
49 | ||
50 | /** Container for the attributes. */ | |
51 | 12 | private Map<String, String> attributes = new HashMap<String, String>(); |
52 | ||
53 | /** Value of the tag. */ | |
54 | 12 | private String value = null; |
55 | ||
56 | /** Indicator to uses non self-closing tag. */ | |
57 | public static final String SPACE = " "; | |
58 | ||
59 | 12 | public XHtmlTagTool(final String name, final String value) throws IllegalArgumentException { |
60 | 12 | if (Utils.isEmpty(name)) |
61 | 0 | throw new IllegalArgumentException("Parameter 'name' shouldn't be empty!"); |
62 | 12 | this.name = name; |
63 | 12 | this.value = value; |
64 | 12 | } |
65 | ||
66 | public XHtmlTagTool(final String name) { | |
67 | 3 | this(name, null); |
68 | 3 | } |
69 | ||
70 | /** | |
71 | * Setter for the value of the tag. | |
72 | * | |
73 | * @param value | |
74 | */ | |
75 | public void setValue(final String value) { | |
76 | 0 | this.value = value; |
77 | 0 | } |
78 | ||
79 | /** | |
80 | * Adds an attribute to the tag. | |
81 | * | |
82 | * @param name | |
83 | * @param value | |
84 | * @throws IllegalArgumentException if 'key' is empty. | |
85 | */ | |
86 | public void addAttribute(final String name, final String value) { | |
87 | 12 | if (Utils.isEmpty(name)) |
88 | 0 | throw new IllegalArgumentException("Parameter 'name' shouldn't be empty!"); |
89 | 12 | attributes.put(name, value); |
90 | 12 | } |
91 | ||
92 | /** | |
93 | * Constructs the tag. | |
94 | * | |
95 | * @see java.lang.Object#toString() | |
96 | */ | |
97 | @Override | |
98 | public String toString() { | |
99 | 4 | StringBuffer tag = new StringBuffer(); |
100 | ||
101 | // open tag | |
102 | 4 | tag.append("<").append(name); |
103 | ||
104 | // add attributes | |
105 | 4 | for (String key : attributes.keySet()) { |
106 | 0 | String val = attributes.get(key); |
107 | 0 | tag.append(' ').append(key).append('=').append('\"').append(val).append('\"'); |
108 | 0 | } |
109 | ||
110 | // close the tag | |
111 | 4 | if (Utils.isNotEmpty(value)) { |
112 | 2 | tag.append(">").append(value).append("</").append(name).append('>'); |
113 | } else | |
114 | 2 | tag.append(" />"); |
115 | ||
116 | 4 | return tag.toString(); |
117 | } | |
118 | ||
119 | @Override | |
120 | public boolean equals(Object obj) { | |
121 | 2 | if (obj == null) |
122 | 0 | return false; |
123 | try { | |
124 | 2 | XHtmlTagTool tag = (XHtmlTagTool) obj; |
125 | 2 | return value.equals(tag.value) && name.equals(tag.name) |
126 | && attributes.equals(tag.attributes); | |
127 | 0 | } catch (ClassCastException e) { |
128 | 0 | return false; |
129 | } | |
130 | } | |
131 | ||
132 | @Override | |
133 | public int hashCode() { | |
134 | ||
135 | 0 | return name.hashCode() + value.hashCode() + attributes.hashCode(); |
136 | } | |
137 | } |