1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package io.wcm.handler.commons.dom;
21
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import org.apache.commons.lang3.StringUtils;
26 import org.osgi.annotation.versioning.ConsumerType;
27
28
29
30
31
32 @ConsumerType
33 @SuppressWarnings("java:S110")
34 public class HtmlElement extends AbstractHtmlElementFactory {
35
36 private static final long serialVersionUID = 1L;
37
38 private static final String ATTRIBUTE_ID = "id";
39 private static final String ATTRIBUTE_CLASS = "class";
40 private static final String ATTRIBUTE_STYLE = "style";
41 private static final String ATTRIBUTE_TITLE = "title";
42 private static final String ATTRIBUTE_DATA_PREFIX = "data-";
43
44
45
46
47
48 public HtmlElement(String name) {
49 super(name);
50 }
51
52
53
54
55
56
57
58
59 public final <HtmlElementType extends HtmlElement> HtmlElementType add(HtmlElementType element) {
60 this.addContent(element);
61 return element;
62 }
63
64
65
66
67
68
69 protected final boolean getEmptyAttributeValueAsBoolean(String attributeName) {
70 return attributeName.equalsIgnoreCase(getAttributeValue(attributeName));
71 }
72
73
74
75
76
77
78
79 protected HtmlElement setEmptyAttributeValueAsBoolean(String attributeName, boolean value) {
80 if (value) {
81 setAttribute(attributeName, attributeName.toLowerCase());
82 }
83 else {
84 removeAttribute(attributeName);
85 }
86 return this;
87 }
88
89
90
91
92
93 public final String getId() {
94 return getAttributeValue(ATTRIBUTE_ID);
95 }
96
97
98
99
100
101
102 public HtmlElement setId(String value) {
103 setAttribute(ATTRIBUTE_ID, value);
104 return this;
105 }
106
107
108
109
110
111 public final String getCssClass() {
112 return getAttributeValue(ATTRIBUTE_CLASS);
113 }
114
115
116
117
118
119
120
121 public HtmlElement setCssClass(String value) {
122 setAttribute(ATTRIBUTE_CLASS, value);
123 return this;
124 }
125
126
127
128
129
130
131 public HtmlElement addCssClass(String value) {
132 if (StringUtils.isNotEmpty(value)) {
133 return setCssClass(StringUtils.isNotEmpty(getCssClass()) ? getCssClass() + " " + value : value);
134 }
135 else {
136 return this;
137 }
138 }
139
140
141
142
143
144 public final String getStyleString() {
145 return getAttributeValue(ATTRIBUTE_STYLE);
146 }
147
148
149
150
151
152 public final Map<String, String> getStyles() {
153 Map<String, String> styleMap = new HashMap<>();
154
155
156 String styleString = getStyleString();
157 if (styleString != null) {
158 String[] styles = StringUtils.split(styleString, ";");
159 for (String styleSubString : styles) {
160 String[] styleParts = StringUtils.split(styleSubString, ":");
161 if (styleParts.length > 1) {
162 styleMap.put(styleParts[0].trim(), styleParts[1].trim());
163 }
164 }
165 }
166
167 return styleMap;
168 }
169
170
171
172
173
174
175 public final String getStyle(String styleAttribute) {
176 return getStyles().get(styleAttribute);
177 }
178
179
180
181
182
183
184 public HtmlElement setStyleString(String value) {
185 setAttribute(ATTRIBUTE_STYLE, value);
186 return this;
187 }
188
189
190
191
192
193
194
195 public HtmlElement setStyle(String styleAttribute, String styleValue) {
196
197
198 Map<String, String> styleMap = getStyles();
199 styleMap.put(styleAttribute, styleValue);
200
201
202 StringBuilder styleString = new StringBuilder();
203 for (Map.Entry<String, String> style : styleMap.entrySet()) {
204 styleString.append(style.getKey())
205 .append(':')
206 .append(style.getValue())
207 .append(';');
208 }
209 setStyleString(styleString.toString());
210 return this;
211 }
212
213
214
215
216
217 public final String getTitle() {
218 return getAttributeValue(ATTRIBUTE_TITLE);
219 }
220
221
222
223
224
225
226 public HtmlElement setTitle(String value) {
227 setAttribute(ATTRIBUTE_TITLE, value);
228 return this;
229 }
230
231
232
233
234
235
236 public final String getData(String attributeName) {
237 return getAttributeValue(ATTRIBUTE_DATA_PREFIX + attributeName);
238 }
239
240
241
242
243
244
245
246 public HtmlElement setData(String attributeName, String value) {
247 setAttribute(ATTRIBUTE_DATA_PREFIX + attributeName, value);
248 return this;
249 }
250
251 }