1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.ws.commons.schema.utils;
21
22 import org.w3c.dom.*;
23
24 import java.lang.reflect.Method;
25
26
27
28
29
30
31
32
33
34
35
36
37 public class DOMUtil {
38
39 private static final String DEFAULT_ENCODING = "UTF-8";
40
41
42
43
44
45
46
47
48 protected DOMUtil() {
49 }
50
51
52
53
54
55
56
57
58 public static Element getFirstChildElement(Node parent) {
59
60
61 Node child = parent.getFirstChild();
62 while (child != null) {
63 if (child.getNodeType() == Node.ELEMENT_NODE) {
64 return (Element) child;
65 }
66 child = child.getNextSibling();
67 }
68
69
70 return null;
71
72 }
73
74
75
76
77 public static Element getLastChildElement(Node parent) {
78
79
80 Node child = parent.getLastChild();
81 while (child != null) {
82 if (child.getNodeType() == Node.ELEMENT_NODE) {
83 return (Element) child;
84 }
85 child = child.getPreviousSibling();
86 }
87
88
89 return null;
90
91 }
92
93
94
95
96
97 public static Element getNextSiblingElement(Node node) {
98
99
100 Node sibling = node.getNextSibling();
101 while (sibling != null) {
102 if (sibling.getNodeType() == Node.ELEMENT_NODE) {
103 return (Element) sibling;
104 }
105 sibling = sibling.getNextSibling();
106 }
107
108
109 return null;
110
111 }
112
113
114
115
116 public static Element getFirstChildElement(Node parent, String elemName) {
117
118
119 Node child = parent.getFirstChild();
120 while (child != null) {
121 if (child.getNodeType() == Node.ELEMENT_NODE) {
122 if (child.getNodeName().equals(elemName)) {
123 return (Element) child;
124 }
125 }
126 child = child.getNextSibling();
127 }
128
129
130 return null;
131
132 }
133
134
135
136
137 public static Element getLastChildElement(Node parent, String elemName) {
138
139
140 Node child = parent.getLastChild();
141 while (child != null) {
142 if (child.getNodeType() == Node.ELEMENT_NODE) {
143 if (child.getNodeName().equals(elemName)) {
144 return (Element) child;
145 }
146 }
147 child = child.getPreviousSibling();
148 }
149
150
151 return null;
152
153 }
154
155
156
157
158 public static Element getNextSiblingElement(Node node, String elemName) {
159
160
161 Node sibling = node.getNextSibling();
162 while (sibling != null) {
163 if (sibling.getNodeType() == Node.ELEMENT_NODE) {
164 if (sibling.getNodeName().equals(elemName)) {
165 return (Element) sibling;
166 }
167 }
168 sibling = sibling.getNextSibling();
169 }
170
171
172 return null;
173
174 }
175
176
177
178
179 public static Element getFirstChildElementNS(Node parent,
180 String uri, String localpart) {
181
182
183 Node child = parent.getFirstChild();
184 while (child != null) {
185 if (child.getNodeType() == Node.ELEMENT_NODE) {
186 String childURI = child.getNamespaceURI();
187 if (childURI != null && childURI.equals(uri) &&
188 child.getLocalName().equals(localpart)) {
189 return (Element) child;
190 }
191 }
192 child = child.getNextSibling();
193 }
194
195
196 return null;
197
198 }
199
200
201
202
203 public static Element getLastChildElementNS(Node parent,
204 String uri, String localpart) {
205
206
207 Node child = parent.getLastChild();
208 while (child != null) {
209 if (child.getNodeType() == Node.ELEMENT_NODE) {
210 String childURI = child.getNamespaceURI();
211 if (childURI != null && childURI.equals(uri) &&
212 child.getLocalName().equals(localpart)) {
213 return (Element) child;
214 }
215 }
216 child = child.getPreviousSibling();
217 }
218
219
220 return null;
221
222 }
223
224
225
226
227 public static Element getNextSiblingElementNS(Node node,
228 String uri, String localpart) {
229
230
231 Node sibling = node.getNextSibling();
232 while (sibling != null) {
233 if (sibling.getNodeType() == Node.ELEMENT_NODE) {
234 String siblingURI = sibling.getNamespaceURI();
235 if (siblingURI != null && siblingURI.equals(uri) &&
236 sibling.getLocalName().equals(localpart)) {
237 return (Element) sibling;
238 }
239 }
240 sibling = sibling.getNextSibling();
241 }
242
243
244 return null;
245
246 }
247
248
249
250
251 public static Element getFirstChildElement(Node parent, String elemNames[]) {
252
253
254 Node child = parent.getFirstChild();
255 while (child != null) {
256 if (child.getNodeType() == Node.ELEMENT_NODE) {
257 for (int i = 0; i < elemNames.length; i++) {
258 if (child.getNodeName().equals(elemNames[i])) {
259 return (Element) child;
260 }
261 }
262 }
263 child = child.getNextSibling();
264 }
265
266
267 return null;
268
269 }
270
271
272
273
274 public static Element getLastChildElement(Node parent, String elemNames[]) {
275
276
277 Node child = parent.getLastChild();
278 while (child != null) {
279 if (child.getNodeType() == Node.ELEMENT_NODE) {
280 for (int i = 0; i < elemNames.length; i++) {
281 if (child.getNodeName().equals(elemNames[i])) {
282 return (Element) child;
283 }
284 }
285 }
286 child = child.getPreviousSibling();
287 }
288
289
290 return null;
291
292 }
293
294
295
296
297 public static Element getNextSiblingElement(Node node, String elemNames[]) {
298
299
300 Node sibling = node.getNextSibling();
301 while (sibling != null) {
302 if (sibling.getNodeType() == Node.ELEMENT_NODE) {
303 for (int i = 0; i < elemNames.length; i++) {
304 if (sibling.getNodeName().equals(elemNames[i])) {
305 return (Element) sibling;
306 }
307 }
308 }
309 sibling = sibling.getNextSibling();
310 }
311
312
313 return null;
314
315 }
316
317
318
319
320 public static Element getFirstChildElementNS(Node parent,
321 String[][] elemNames) {
322
323
324 Node child = parent.getFirstChild();
325 while (child != null) {
326 if (child.getNodeType() == Node.ELEMENT_NODE) {
327 for (int i = 0; i < elemNames.length; i++) {
328 String uri = child.getNamespaceURI();
329 if (uri != null && uri.equals(elemNames[i][0]) &&
330 child.getLocalName().equals(elemNames[i][1])) {
331 return (Element) child;
332 }
333 }
334 }
335 child = child.getNextSibling();
336 }
337
338
339 return null;
340
341 }
342
343
344
345
346 public static Element getLastChildElementNS(Node parent,
347 String[][] elemNames) {
348
349
350 Node child = parent.getLastChild();
351 while (child != null) {
352 if (child.getNodeType() == Node.ELEMENT_NODE) {
353 for (int i = 0; i < elemNames.length; i++) {
354 String uri = child.getNamespaceURI();
355 if (uri != null && uri.equals(elemNames[i][0]) &&
356 child.getLocalName().equals(elemNames[i][1])) {
357 return (Element) child;
358 }
359 }
360 }
361 child = child.getPreviousSibling();
362 }
363
364
365 return null;
366
367 }
368
369
370
371
372 public static Element getNextSiblingElementNS(Node node,
373 String[][] elemNames) {
374
375
376 Node sibling = node.getNextSibling();
377 while (sibling != null) {
378 if (sibling.getNodeType() == Node.ELEMENT_NODE) {
379 for (int i = 0; i < elemNames.length; i++) {
380 String uri = sibling.getNamespaceURI();
381 if (uri != null && uri.equals(elemNames[i][0]) &&
382 sibling.getLocalName().equals(elemNames[i][1])) {
383 return (Element) sibling;
384 }
385 }
386 }
387 sibling = sibling.getNextSibling();
388 }
389
390
391 return null;
392
393 }
394
395
396
397
398
399 public static Element getFirstChildElement(Node parent,
400 String elemName,
401 String attrName,
402 String attrValue) {
403
404
405 Node child = parent.getFirstChild();
406 while (child != null) {
407 if (child.getNodeType() == Node.ELEMENT_NODE) {
408 Element element = (Element) child;
409 if (element.getNodeName().equals(elemName) &&
410 element.getAttribute(attrName).equals(attrValue)) {
411 return element;
412 }
413 }
414 child = child.getNextSibling();
415 }
416
417
418 return null;
419
420 }
421
422
423
424
425
426 public static Element getLastChildElement(Node parent,
427 String elemName,
428 String attrName,
429 String attrValue) {
430
431
432 Node child = parent.getLastChild();
433 while (child != null) {
434 if (child.getNodeType() == Node.ELEMENT_NODE) {
435 Element element = (Element) child;
436 if (element.getNodeName().equals(elemName) &&
437 element.getAttribute(attrName).equals(attrValue)) {
438 return element;
439 }
440 }
441 child = child.getPreviousSibling();
442 }
443
444
445 return null;
446
447 }
448
449
450
451
452
453
454 public static Element getNextSiblingElement(Node node,
455 String elemName,
456 String attrName,
457 String attrValue) {
458
459
460 Node sibling = node.getNextSibling();
461 while (sibling != null) {
462 if (sibling.getNodeType() == Node.ELEMENT_NODE) {
463 Element element = (Element) sibling;
464 if (element.getNodeName().equals(elemName) &&
465 element.getAttribute(attrName).equals(attrValue)) {
466 return element;
467 }
468 }
469 sibling = sibling.getNextSibling();
470 }
471
472
473 return null;
474
475 }
476
477
478
479
480
481
482
483
484
485
486 public static String getChildText(Node node) {
487
488
489 if (node == null) {
490 return null;
491 }
492
493
494 StringBuffer str = new StringBuffer();
495 Node child = node.getFirstChild();
496 while (child != null) {
497 short type = child.getNodeType();
498 if (type == Node.TEXT_NODE) {
499 str.append(child.getNodeValue());
500 } else if (type == Node.CDATA_SECTION_NODE) {
501 str.append(getChildText(child));
502 }
503 child = child.getNextSibling();
504 }
505
506
507 return str.toString();
508
509 }
510
511
512 public static String getName(Node node) {
513 return node.getNodeName();
514 }
515
516
517
518
519
520 public static String getLocalName(Node node) {
521 String name = node.getLocalName();
522 return (name != null) ? name : node.getNodeName();
523 }
524
525 public static Element getParent(Element elem) {
526 Node parent = elem.getParentNode();
527 if (parent instanceof Element)
528 return (Element) parent;
529 return null;
530 }
531
532
533 public static Document getDocument(Node node) {
534 return node.getOwnerDocument();
535 }
536
537
538 public static Element getRoot(Document doc) {
539 return doc.getDocumentElement();
540 }
541
542
543
544
545 public static Attr getAttr(Element elem, String name) {
546 return elem.getAttributeNode(name);
547 }
548
549
550 public static Attr getAttrNS(Element elem, String nsUri,
551 String localName) {
552 return elem.getAttributeNodeNS(nsUri, localName);
553 }
554
555
556 public static Attr[] getAttrs(Element elem) {
557 NamedNodeMap attrMap = elem.getAttributes();
558 Attr[] attrArray = new Attr[attrMap.getLength()];
559 for (int i = 0; i < attrMap.getLength(); i++)
560 attrArray[i] = (Attr) attrMap.item(i);
561 return attrArray;
562 }
563
564
565 public static String getValue(Attr attribute) {
566 return attribute.getValue();
567 }
568
569
570
571
572
573
574
575
576 public static String getAttrValue(Element elem, String name) {
577 return elem.getAttribute(name);
578 }
579
580
581
582 public static String getAttrValueNS(Element elem, String nsUri,
583 String localName) {
584 return elem.getAttributeNS(nsUri, localName);
585 }
586
587
588 public static String getNamespaceURI(Node node) {
589 return node.getNamespaceURI();
590 }
591
592
593
594
595
596
597
598
599 public static String getInputEncoding(Document doc) {
600 try {
601 Method m = Document.class.getMethod("getInputEncoding", new Class[]{});
602 return (String) m.invoke(doc, new Object[]{});
603 } catch (Exception e) {
604 return DEFAULT_ENCODING;
605 }
606 }
607
608
609
610
611
612
613
614
615
616 public static String getXmlEncoding(Document doc) {
617 try {
618 Method m = Document.class.getMethod("getXmlEncoding", new Class[]{});
619 return (String) m.invoke(doc, new Object[]{});
620 } catch (Exception e) {
621 return DEFAULT_ENCODING;
622 }
623 }
624 }