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