Skip to content

Commit 39ef196

Browse files
committed
Enhancements to ElementW and HtmlTag
1 parent 08ee1b7 commit 39ef196

File tree

3 files changed

+201
-32
lines changed

3 files changed

+201
-32
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>io.github.emays</groupId>
77
<artifactId>parent</artifactId>
8-
<version>0.0.5-SNAPSHOT</version>
8+
<version>0.0.6-SNAPSHOT</version>
99
<relativePath />
1010
</parent>
1111

@@ -33,7 +33,7 @@
3333
</developers>
3434

3535
<scm>
36-
<connection>https://www.github.com/emays/util</connection>
36+
<connection>scm:git:https://www.github.com/emays/util</connection>
3737
<developerConnection>scm:git:https://www.github.com/emays/util</developerConnection>
3838
<url>https://www.github.com/emays/util</url>
3939
<tag>HEAD</tag>

src/main/java/com/mays/util/html/ElementW.java

Lines changed: 191 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,144 @@
66
import org.dom4j.Element;
77
import org.dom4j.Node;
88

9+
/**
10+
* {@code ElementW} adds methods to {@code ElementW} via delegation. Does not
11+
* implement all methods, so use getElement() to access the wrapped Element.
12+
*/
913
public class ElementW {
1014

1115
private Element element;
1216

17+
/**
18+
* Creates a new {@code ElementW} wrapping the given element
19+
*
20+
* @param element is the {@code Element} to wrap
21+
* @return the new {@code ElementW}
22+
*/
23+
public static ElementW of(Element element) {
24+
return new ElementW(element);
25+
}
26+
27+
/**
28+
* Constructs a new {@code ElementW} wrapping the given element
29+
*
30+
* @param element is the {@code Element} to wrap
31+
*/
1332
public ElementW(Element element) {
1433
super();
1534
this.element = element;
1635
}
1736

37+
/**
38+
* Access the wrapped {@code Element}
39+
*
40+
* @return the wrapped {@code Element}
41+
*/
1842
public Element getElement() {
1943
return element;
2044
}
2145

46+
/**
47+
* Adds the given {@code ElementW} to this element. If the given element already
48+
* has a parent defined then an {@code IllegalAddException} will be thrown.
49+
*
50+
* @param el is the element to be added
51+
*/
52+
public void add(ElementW el) {
53+
element.add(el.getElement());
54+
}
55+
56+
/**
57+
* Adds a new {@code ElementW} node with the given name to this element and
58+
* returns the new node.
59+
*
60+
* @param name is the name for the {@code ElementW} node.
61+
* @return the newly added {@code ElementW} node.
62+
*/
2263
public ElementW addElement(String name) {
2364
return new ElementW(element.addElement(name));
2465
}
2566

67+
/**
68+
* Adds a new {@code ElementW} node with a name of the given HtmlTag tag to this
69+
* element and returns the new node. Adds an attribute to the new node for each
70+
* name and value in attributes.
71+
*
72+
* <p>
73+
* <blockquote> addElement(HtmlTag.DIV, "id", "div1", "style", "font: 10pt
74+
* sans-serif;") </blockquote>
75+
*
76+
* @param tag is the HtmlTag name for the {@code ElementW} node
77+
* @param attributes is a sequence of attribute name and value
78+
* @return the newly added {@code ElementW} node
79+
* @throws IllegalArgumentException if the length of attributes is not a
80+
* multiple of 2
81+
*/
82+
public ElementW addElement(HtmlTag tag, String... attributes) {
83+
return addElement(tag.toString(), attributes);
84+
}
85+
86+
/**
87+
* Adds a new {@code ElementW} node with the given name to this element and
88+
* returns the new node. Adds an attribute to the new node for each name and
89+
* value in attributes.
90+
*
91+
* <p>
92+
* <blockquote> addElement("div", "id", "div1", "style", "font: 10pt
93+
* sans-serif;") </blockquote>
94+
*
95+
* @param name is the name for the {@code ElementW} node
96+
* @param attributes is a sequence of attribute name and value
97+
* @return the newly added {@code ElementW} node
98+
* @throws IllegalArgumentException if the length of attributes is not a
99+
* multiple of 2
100+
*/
101+
public ElementW addElement(String name, String... attributes) {
102+
if (attributes.length % 2 != 0)
103+
throw new IllegalArgumentException("Attributes length " + attributes.length);
104+
Element tag_elem = element.addElement(name);
105+
for (int i = 0; i < attributes.length; i = i + 2) {
106+
tag_elem.addAttribute(attributes[i], attributes[i + 1]);
107+
}
108+
return new ElementW(tag_elem);
109+
}
110+
111+
/**
112+
* This returns the attribute value for the attribute with the given name and
113+
* any namespace or null if there is no such attribute or the empty string if
114+
* the attribute value is empty.
115+
*
116+
* @param name is the name of the attribute value to be returned
117+
* @return the value of the attribute, null if the attribute does not exist or
118+
* the empty string
119+
*/
120+
public String attributeValue(String name) {
121+
return element.attributeValue(name);
122+
}
123+
124+
/**
125+
* Adds the attribute value of the given local name. If an attribute already
126+
* exists for the given name it will be replaced. Attributes with null values
127+
* are silently ignored. If the value of the attribute is null then this method
128+
* call will remove any attributes with the given name.
129+
*
130+
* @param name is the name of the attribute whose value is to be added or
131+
* updated
132+
* @param value is the attribute's value
133+
* @return this {@code ElementW}
134+
*/
26135
public ElementW addAttribute(String name, String value) {
27136
element.addAttribute(name, value);
28137
return this;
29138
}
30139

140+
/**
141+
* Appends the value to the class attribute of this {@code ElementW} creating a
142+
* new attribute if one doesn't already exist
143+
*
144+
* @param value is the value of the class attribute to append or set
145+
* @return this {@code ElementW}
146+
*/
31147
public ElementW addClass(String value) {
32148
String attr = element.attributeValue("class");
33149
if (attr != null) {
@@ -39,77 +155,126 @@ public ElementW addClass(String value) {
39155
return this.addAttribute("class", attr);
40156
}
41157

158+
/**
159+
* Adds a new {@code CDATA} node with the given text to this element.
160+
*
161+
* @param cdata is the text for the {@code CDATA} node
162+
* @return this {@code ElementW}
163+
*/
42164
public ElementW addCDATA(String cdata) {
43165
element.addCDATA(cdata);
44166
return this;
45167
}
46168

169+
/**
170+
* Adds a new {@code Text} node with the given text to this element.
171+
*
172+
* @param text is the text for the {@code Text} node
173+
* @return this {@code ElementW}
174+
*/
47175
public ElementW addText(String text) {
48176
element.addText(text);
49177
return this;
50178
}
51179

180+
/**
181+
* Removes the child nodes of this element and adds a new {@code Text} node with
182+
* the given text to this element.
183+
*/
52184
public void setText(String text) {
53185
removeChildren();
54186
element.setText(text);
55187
}
56188

189+
/**
190+
* Adds a new {@code Comment} node with the given text to this element.
191+
*
192+
* @param comment is the text for the {@code Comment} node
193+
* @return this {@code ElementW} instance
194+
*/
195+
public ElementW addComment(String comment) {
196+
element.addComment(comment);
197+
return this;
198+
}
199+
200+
/**
201+
* Removes the child nodes of this element.
202+
*/
57203
public void removeChildren() {
58204
List<Node> nodes = element.selectNodes("child::node()");
59205
nodes.forEach(node -> element.remove(node));
60206
}
61207

62-
public ElementW addElement(HtmlTag tag, String... attributes) {
63-
return addElement(tag.toString(), attributes);
208+
/**
209+
* Returns the elements contained in this element.
210+
*
211+
* @return a list of all the elements in this element.
212+
*/
213+
public List<ElementW> elements() {
214+
return element.elements().stream().map(el -> new ElementW(el)).toList();
64215
}
65216

66-
public ElementW element(int i) {
217+
/**
218+
* Returns the element at the specified position in the elements contained in
219+
* this element.
220+
*
221+
* @param index index of the element to return
222+
* @return the element at the specified position in the elements
223+
* @throws IndexOutOfBoundsException if the index is out of range
224+
*/
225+
public ElementW element(int index) {
67226
List<Element> els = element.elements();
68-
return new ElementW(els.get(i));
227+
return new ElementW(els.get(index));
228+
}
229+
230+
/**
231+
* Returns the first element for the given local name and any namespace.
232+
*
233+
* @param name the name to search for
234+
* @return the first element with the given local name
235+
*/
236+
public ElementW element(String name) {
237+
return new ElementW(element.element(name));
69238
}
70239

71-
public ElementW elementById(String id) {
240+
/**
241+
* Returns the first element in this element with an id attribute equal to
242+
* idValue (ignoring case). Searches recursively.
243+
*
244+
* @param idValue the id value to search for
245+
* @return the first element with the given local name
246+
*/
247+
public ElementW elementById(String idValue) {
72248
for (Element el : element.elements()) {
73249
for (Attribute attr : el.attributes()) {
74-
if (attr.getName().equalsIgnoreCase("id") && attr.getValue().equalsIgnoreCase(id))
250+
if (attr.getName().equalsIgnoreCase("id") && attr.getValue().equalsIgnoreCase(idValue))
75251
return new ElementW(el);
76252
}
77-
ElementW el_el = new ElementW(el).elementById(id);
253+
ElementW el_el = new ElementW(el).elementById(idValue);
78254
if (el_el != null)
79255
return el_el;
80256
}
81257
return null;
82258
}
83259

84-
public void removeId() {
85-
if (element.attribute("id") != null)
86-
element.remove(element.attribute("id"));
87-
}
88-
89-
public ElementW addElement(String tag, String... attributes) {
90-
if (attributes.length % 2 != 0)
91-
throw new RuntimeException("Attributes length " + attributes.length);
92-
Element tag_elem = element.addElement(tag);
93-
for (int i = 0; i < attributes.length; i = i + 2) {
94-
tag_elem.addAttribute(attributes[i], attributes[i + 1]);
95-
}
96-
return new ElementW(tag_elem);
97-
}
98-
260+
@Deprecated
99261
public ElementW addDiv(String div_class) {
100262
return addElement(HtmlTag.DIV, "class", div_class);
101263
}
102264

265+
@Deprecated
103266
public ElementW addDiv(String div_class, String text) {
104267
return addDiv(div_class).addText(text);
105268
}
106269

270+
/**
271+
* Creates a deep copy of this element. The new element is detached from its
272+
* parent, and getParent() on the clone will return null.
273+
*
274+
* @return a new deep copy ElementW
275+
*/
107276
public ElementW createCopy() {
108277
return new ElementW(element.createCopy());
109278
}
110279

111-
public void add(ElementW el) {
112-
element.add(el.getElement());
113-
}
114-
115280
}

src/main/java/com/mays/util/html/HtmlTag.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@
55

66
public enum HtmlTag {
77

8-
HEAD, TITLE, LINK, SCRIPT,
8+
HEAD, TITLE, LINK, SCRIPT, STYLE,
99
//
10-
BODY, A, B, BR, DIV, P,
10+
BODY, A, P, B, BR, DIV, SPAN,
11+
//
12+
H1, H2, H3, H4, H5, H6,
1113
//
1214
TABLE, TBODY, TH, TD, TR, CAPTION,
1315
//
14-
DEL, INS, HR, SPAN, UL, LI,
16+
DEL, INS, HR, UL, LI,
17+
//
18+
PRE,
1519
//
16-
BUTTON, PRE,
20+
FORM, INPUT, TEXTAREA, BUTTON, SELECT, OPTION, OPTGROUP, FIELDSET, LABEL, OUTPUT,
1721
//
1822
IMG;
1923

0 commit comments

Comments
 (0)