-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathXMLElement.java
More file actions
335 lines (281 loc) · 9.89 KB
/
XMLElement.java
File metadata and controls
335 lines (281 loc) · 9.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* For more information, please refer to <http://unlicense.org/>
*/
package com.jeffrodriguez.xmlwrapper;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
/**
* An {@link Element} wrapping utility class.
* @author <a href="mailto:[email protected]">Jeff Rodriguez</a>
*/
public class XMLElement {
/**
* The wrapped {@link Element}.
*/
private final Element element;
/**
* Wraps an {@link Element}.
* @param element the element to wrap.
*/
public XMLElement(Element element) {
this.element = element;
}
/**
* @return the wrapped element.
*/
public Element getElement() {
return element;
}
/**
* @return the parent of this element.
*/
public XMLElement getParent() {
return new XMLElement((Element) element.getParentNode());
}
/**
* Gets the tag name of the element.
* @return the tag name of the element.
*/
public String getName() {
return element.getTagName();
}
/**
* Creates a new child element and appends it to this one.
* @param name the name of the element.
* @return the new element.
*/
public XMLElement addChild(String name) {
Element child = element.getOwnerDocument().createElement(name);
element.appendChild(child);
return new XMLElement(child);
}
/**
* Gets a child by tag name.
* @param name the name of the tag.
* @return the child element, or null if the child doesn't exist.
* @throws IllegalStateException if more than one element with the name are found.
*/
public XMLElement getChild(String name) {
// The first matching child will be kept here
Node child = null;
// Iterate over all the child nodes
for (Node node : new NodeListIterator<Node>(element.getChildNodes()).toIterable()) {
// Look for Element instances that match the name given
if (node instanceof Element && name.equals(node.getNodeName())) {
// If the child is already set, we can't handle this
if (child != null) {
throw new IllegalStateException("More than one element with the name: " + name);
}
// Save the child node
child = node;
}
}
// Return null if no child was found
if (child == null) {
return null;
}
// Get the element
return new XMLElement((Element) child);
}
/**
* Gets the value of a child element's text content.
* @return the child element's text content or null if the child does not exist.
*/
public String getChildValue(String name) {
XMLElement child = getChild(name);
if (child != null) {
return child.getValue();
}
return null;
}
/**
* Sets the value of a child element's text content.
*
* If the child doesn't exist, a new element will be created.
* @return the child element.
*/
public XMLElement setChildValue(String name, String value) {
// Get the child
XMLElement child = getChild(name);
// Create the child if it doesn't exist yet
if (child == null) {
child = addChild(name);
}
// Set it's value and return the child
child.setValue(value);
return child;
}
/**
* Gets an {@link Iterable} for the children of this element by tag name.
* @param name the tag name of the children.
* @return an {@link Iterable} of the children.
*/
@SuppressWarnings("unchecked")
public Iterable<XMLElement> getChildren(String name) {
// Get the child element node list
final NodeList nodes = element.getChildNodes();
// Build the iterators
final NodeListIterator<Element> nodeListIterator = new NodeListIterator(nodes);
return new XMLElementByTagNameIterator(name, nodeListIterator).toIterable();
}
/**
* Gets an {@link Iterable} for the descendants of this element by tag name.
* @param name the tag name of the descendants.
* @return an {@link Iterable} of the descendants.
*/
@SuppressWarnings("unchecked")
public Iterable<XMLElement> getDescendants(String name) {
// Get the descendant element node list
final NodeList nodes = element.getElementsByTagName(name);
// Build the iterators
final NodeListIterator<Element> nodeListIterator = new NodeListIterator(nodes);
return new XMLElementIterator(nodeListIterator).toIterable();
}
/**
* Returns true if an element has children.
* @return true if the element has children.
*/
public boolean hasChildren() {
NodeListIterator iterator = new NodeListIterator(element.getChildNodes());
while (iterator.hasNext()) {
return iterator.next() instanceof Element;
}
return false;
}
/**
* Returns true if a child element of the specified name exists.
* @return true if the specified child element exists.
*/
public boolean hasChild(String name) {
NodeListIterator iterator = new NodeListIterator(element.getChildNodes());
while (iterator.hasNext()) {
Node node = iterator.next();
// If the element's name matches, return true.
if (node instanceof Element && ((Element) node).getTagName().equals(name)) {
return true;
}
}
return false;
}
/**
* Sets an attribute on the element.
* @param name the name of the attribute.
* @param value the value of the attribute.
* @return this element
*/
public XMLElement setAttribute(String name, String value) {
element.setAttribute(name, value);
return this;
}
/**
* Gets the value of an attribute.
* @param name the name of the attribute.
* @return the value of the attribute.
*/
public String getAttribute(String name) {
return element.getAttribute(name);
}
/**
* Sets the text content of the element.
* @param value the value to set.
*/
public void setValue(String value) {
// Delete any existing text/cdata nodes
NodeListIterator iterator = new NodeListIterator(element.getChildNodes());
while (iterator.hasNext()) {
// Get the child node
Node child = iterator.next();
// Delete the child node if it's text or cdata
switch (child.getNodeType()) {
case Node.TEXT_NODE:
case Node.CDATA_SECTION_NODE:
iterator.remove();
}
}
// Create a new text node
Text textNode = element.getOwnerDocument().createTextNode(value);
element.appendChild(textNode);
}
/**
* Gets the element's text content.
* @return the element's text content.
*/
public String getValue() {
StringBuilder value = new StringBuilder();
// Append the values of text and cdata nodes.
for (Node child : NodeListIterator.iterable(element.getChildNodes())) {
switch (child.getNodeType()) {
case Node.TEXT_NODE:
case Node.CDATA_SECTION_NODE:
value.append(child.getNodeValue());
}
}
return value.toString();
}
/**
* Gets the element's text content, parsed as a Long.
* @return the element's text content, parsed as a {@link Long} (possibly null).
*/
public Long getValueAsLong() {
String value = getValue();
if (value == null || value.isEmpty()) {
return null;
}
return Long.parseLong(value);
}
/**
* Gets the element's text content, parsed as an Integer.
* @return the element's text content, parsed as an {@link Integer} (possibly null).
*/
public Integer getValueAsInteger() {
String value = getValue();
if (value == null || value.isEmpty()) {
return null;
}
return Integer.parseInt(value);
}
@Override
public int hashCode() {
int hash = 7;
hash = 67 * hash + (this.element != null ? this.element.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final XMLElement other = (XMLElement) obj;
if (this.element != other.element && (this.element == null || !this.element.equals(other.element))) {
return false;
}
return true;
}
}