KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > util > DOMUtil


1 /*
2  * Copyright 1999-2002,2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.util;
18
19 import java.util.Hashtable JavaDoc;
20
21 import org.apache.xerces.dom.AttrImpl;
22 import org.apache.xerces.dom.DocumentImpl;
23 import org.apache.xerces.impl.xs.opti.ElementImpl;
24
25 import org.w3c.dom.Attr JavaDoc;
26 import org.w3c.dom.Document JavaDoc;
27 import org.w3c.dom.DOMException JavaDoc;
28 import org.w3c.dom.Element JavaDoc;
29 import org.w3c.dom.NamedNodeMap JavaDoc;
30 import org.w3c.dom.Node JavaDoc;
31
32 /**
33  * Some useful utility methods.
34  * This class was modified in Xerces2 with a view to abstracting as
35  * much as possible away from the representation of the underlying
36  * parsed structure (i.e., the DOM). This was done so that, if Xerces
37  * ever adopts an in-memory representation more efficient than the DOM
38  * (such as a DTM), we should easily be able to convert our schema
39  * parsing to utilize it.
40  *
41  * @version $Id: DOMUtil.java,v 1.11 2005/05/30 04:17:12 mrglavas Exp $
42  */

43 public class DOMUtil {
44     
45     //
46
// Constructors
47
//
48

49     /** This class cannot be instantiated. */
50     protected DOMUtil() {}
51     
52     //
53
// Public static methods
54
//
55

56     /**
57      * Copies the source tree into the specified place in a destination
58      * tree. The source node and its children are appended as children
59      * of the destination node.
60      * <p>
61      * <em>Note:</em> This is an iterative implementation.
62      */

63     public static void copyInto(Node JavaDoc src, Node JavaDoc dest) throws DOMException JavaDoc {
64         
65         // get node factory
66
Document JavaDoc factory = dest.getOwnerDocument();
67         boolean domimpl = factory instanceof DocumentImpl;
68         
69         // placement variables
70
Node JavaDoc start = src;
71         Node JavaDoc parent = src;
72         Node JavaDoc place = src;
73         
74         // traverse source tree
75
while (place != null) {
76             
77             // copy this node
78
Node JavaDoc node = null;
79             int type = place.getNodeType();
80             switch (type) {
81             case Node.CDATA_SECTION_NODE: {
82                 node = factory.createCDATASection(place.getNodeValue());
83                 break;
84             }
85             case Node.COMMENT_NODE: {
86                 node = factory.createComment(place.getNodeValue());
87                 break;
88             }
89             case Node.ELEMENT_NODE: {
90                 Element JavaDoc element = factory.createElement(place.getNodeName());
91                 node = element;
92                 NamedNodeMap JavaDoc attrs = place.getAttributes();
93                 int attrCount = attrs.getLength();
94                 for (int i = 0; i < attrCount; i++) {
95                     Attr JavaDoc attr = (Attr JavaDoc)attrs.item(i);
96                     String JavaDoc attrName = attr.getNodeName();
97                     String JavaDoc attrValue = attr.getNodeValue();
98                     element.setAttribute(attrName, attrValue);
99                     if (domimpl && !attr.getSpecified()) {
100                         ((AttrImpl)element.getAttributeNode(attrName)).setSpecified(false);
101                     }
102                 }
103                 break;
104             }
105             case Node.ENTITY_REFERENCE_NODE: {
106                 node = factory.createEntityReference(place.getNodeName());
107                 break;
108             }
109             case Node.PROCESSING_INSTRUCTION_NODE: {
110                 node = factory.createProcessingInstruction(place.getNodeName(),
111                         place.getNodeValue());
112                 break;
113             }
114             case Node.TEXT_NODE: {
115                 node = factory.createTextNode(place.getNodeValue());
116                 break;
117             }
118             default: {
119                 throw new IllegalArgumentException JavaDoc("can't copy node type, "+
120                         type+" ("+
121                         node.getNodeName()+')');
122             }
123             }
124             dest.appendChild(node);
125             
126             // iterate over children
127
if (place.hasChildNodes()) {
128                 parent = place;
129                 place = place.getFirstChild();
130                 dest = node;
131             }
132             
133             // advance
134
else {
135                 place = place.getNextSibling();
136                 while (place == null && parent != start) {
137                     place = parent.getNextSibling();
138                     parent = parent.getParentNode();
139                     dest = dest.getParentNode();
140                 }
141             }
142             
143         }
144         
145     } // copyInto(Node,Node)
146

147     /** Finds and returns the first child element node. */
148     public static Element JavaDoc getFirstChildElement(Node JavaDoc parent) {
149         
150         // search for node
151
Node JavaDoc child = parent.getFirstChild();
152         while (child != null) {
153             if (child.getNodeType() == Node.ELEMENT_NODE) {
154                 return (Element JavaDoc)child;
155             }
156             child = child.getNextSibling();
157         }
158         
159         // not found
160
return null;
161         
162     } // getFirstChildElement(Node):Element
163

164     /** Finds and returns the first visible child element node. */
165     public static Element JavaDoc getFirstVisibleChildElement(Node JavaDoc parent) {
166         
167         // search for node
168
Node JavaDoc child = parent.getFirstChild();
169         while (child != null) {
170             if (child.getNodeType() == Node.ELEMENT_NODE &&
171                     !isHidden(child)) {
172                 return (Element JavaDoc)child;
173             }
174             child = child.getNextSibling();
175         }
176         
177         // not found
178
return null;
179         
180     } // getFirstChildElement(Node):Element
181

182     /** Finds and returns the first visible child element node. */
183     public static Element JavaDoc getFirstVisibleChildElement(Node JavaDoc parent, Hashtable JavaDoc hiddenNodes) {
184         
185         // search for node
186
Node JavaDoc child = parent.getFirstChild();
187         while (child != null) {
188             if (child.getNodeType() == Node.ELEMENT_NODE &&
189                     !isHidden(child, hiddenNodes)) {
190                 return (Element JavaDoc)child;
191             }
192             child = child.getNextSibling();
193         }
194         
195         // not found
196
return null;
197         
198     } // getFirstChildElement(Node):Element
199

200     /** Finds and returns the last child element node.
201      * Overload previous method for non-Xerces node impl.
202      */

203     public static Element JavaDoc getLastChildElement(Node JavaDoc parent) {
204         
205         // search for node
206
Node JavaDoc child = parent.getLastChild();
207         while (child != null) {
208             if (child.getNodeType() == Node.ELEMENT_NODE) {
209                 return (Element JavaDoc)child;
210             }
211             child = child.getPreviousSibling();
212         }
213         
214         // not found
215
return null;
216         
217     } // getLastChildElement(Node):Element
218

219     /** Finds and returns the last visible child element node. */
220     public static Element JavaDoc getLastVisibleChildElement(Node JavaDoc parent) {
221         
222         // search for node
223
Node JavaDoc child = parent.getLastChild();
224         while (child != null) {
225             if (child.getNodeType() == Node.ELEMENT_NODE &&
226                     !isHidden(child)) {
227                 return (Element JavaDoc)child;
228             }
229             child = child.getPreviousSibling();
230         }
231         
232         // not found
233
return null;
234         
235     } // getLastChildElement(Node):Element
236

237     /** Finds and returns the last visible child element node.
238      * Overload previous method for non-Xerces node impl
239      */

240     public static Element JavaDoc getLastVisibleChildElement(Node JavaDoc parent, Hashtable JavaDoc hiddenNodes) {
241         
242         // search for node
243
Node JavaDoc child = parent.getLastChild();
244         while (child != null) {
245             if (child.getNodeType() == Node.ELEMENT_NODE &&
246                     !isHidden(child, hiddenNodes)) {
247                 return (Element JavaDoc)child;
248             }
249             child = child.getPreviousSibling();
250         }
251         
252         // not found
253
return null;
254         
255     } // getLastChildElement(Node):Element
256
/** Finds and returns the next sibling element node. */
257     public static Element JavaDoc getNextSiblingElement(Node JavaDoc node) {
258         
259         // search for node
260
Node JavaDoc sibling = node.getNextSibling();
261         while (sibling != null) {
262             if (sibling.getNodeType() == Node.ELEMENT_NODE) {
263                 return (Element JavaDoc)sibling;
264             }
265             sibling = sibling.getNextSibling();
266         }
267         
268         // not found
269
return null;
270         
271     } // getNextSiblingElement(Node):Element
272

273     // get next visible (un-hidden) node.
274
public static Element JavaDoc getNextVisibleSiblingElement(Node JavaDoc node) {
275         
276         // search for node
277
Node JavaDoc sibling = node.getNextSibling();
278         while (sibling != null) {
279             if (sibling.getNodeType() == Node.ELEMENT_NODE &&
280                     !isHidden(sibling)) {
281                 return (Element JavaDoc)sibling;
282             }
283             sibling = sibling.getNextSibling();
284         }
285         
286         // not found
287
return null;
288         
289     } // getNextSiblingdElement(Node):Element
290

291     // get next visible (un-hidden) node, overload previous method for non Xerces node impl
292
public static Element JavaDoc getNextVisibleSiblingElement(Node JavaDoc node, Hashtable JavaDoc hiddenNodes) {
293         
294         // search for node
295
Node JavaDoc sibling = node.getNextSibling();
296         while (sibling != null) {
297             if (sibling.getNodeType() == Node.ELEMENT_NODE &&
298                     !isHidden(sibling, hiddenNodes)) {
299                 return (Element JavaDoc)sibling;
300             }
301             sibling = sibling.getNextSibling();
302         }
303         
304         // not found
305
return null;
306         
307     } // getNextSiblingdElement(Node):Element
308

309     // set this Node as being hidden
310
public static void setHidden(Node JavaDoc node) {
311         if (node instanceof org.apache.xerces.impl.xs.opti.NodeImpl)
312             ((org.apache.xerces.impl.xs.opti.NodeImpl)node).setReadOnly(true, false);
313         else if (node instanceof org.apache.xerces.dom.NodeImpl)
314             ((org.apache.xerces.dom.NodeImpl)node).setReadOnly(true, false);
315     } // setHidden(node):void
316

317     // set this Node as being hidden, overloaded method
318
public static void setHidden(Node JavaDoc node, Hashtable JavaDoc hiddenNodes) {
319         if (node instanceof org.apache.xerces.impl.xs.opti.NodeImpl)
320             ((org.apache.xerces.impl.xs.opti.NodeImpl)node).setReadOnly(true, false);
321         else if (node instanceof org.apache.xerces.dom.NodeImpl)
322             ((org.apache.xerces.dom.NodeImpl)node).setReadOnly(true, false);
323         else
324             hiddenNodes.put(node, "");
325     } // setHidden(node):void
326

327     // set this Node as being visible
328
public static void setVisible(Node JavaDoc node) {
329         if (node instanceof org.apache.xerces.impl.xs.opti.NodeImpl)
330             ((org.apache.xerces.impl.xs.opti.NodeImpl)node).setReadOnly(false, false);
331         else if (node instanceof org.apache.xerces.dom.NodeImpl)
332             ((org.apache.xerces.dom.NodeImpl)node).setReadOnly(false, false);
333     } // setVisible(node):void
334

335     // set this Node as being visible, overloaded method
336
public static void setVisible(Node JavaDoc node, Hashtable JavaDoc hiddenNodes) {
337         if (node instanceof org.apache.xerces.impl.xs.opti.NodeImpl)
338             ((org.apache.xerces.impl.xs.opti.NodeImpl)node).setReadOnly(false, false);
339         else if (node instanceof org.apache.xerces.dom.NodeImpl)
340             ((org.apache.xerces.dom.NodeImpl)node).setReadOnly(false, false);
341         else
342             hiddenNodes.remove(node);
343     } // setVisible(node):void
344

345     // is this node hidden?
346
public static boolean isHidden(Node JavaDoc node) {
347         if (node instanceof org.apache.xerces.impl.xs.opti.NodeImpl)
348             return ((org.apache.xerces.impl.xs.opti.NodeImpl)node).getReadOnly();
349         else if (node instanceof org.apache.xerces.dom.NodeImpl)
350             return ((org.apache.xerces.dom.NodeImpl)node).getReadOnly();
351         return false;
352     } // isHidden(Node):boolean
353

354     // is this node hidden? overloaded method
355
public static boolean isHidden(Node JavaDoc node, Hashtable JavaDoc hiddenNodes) {
356         if (node instanceof org.apache.xerces.impl.xs.opti.NodeImpl)
357             return ((org.apache.xerces.impl.xs.opti.NodeImpl)node).getReadOnly();
358         else if (node instanceof org.apache.xerces.dom.NodeImpl)
359             return ((org.apache.xerces.dom.NodeImpl)node).getReadOnly();
360         else
361             return hiddenNodes.containsKey(node);
362     } // isHidden(Node):boolean
363

364     /** Finds and returns the first child node with the given name. */
365     public static Element JavaDoc getFirstChildElement(Node JavaDoc parent, String JavaDoc elemName) {
366         
367         // search for node
368
Node JavaDoc child = parent.getFirstChild();
369         while (child != null) {
370             if (child.getNodeType() == Node.ELEMENT_NODE) {
371                 if (child.getNodeName().equals(elemName)) {
372                     return (Element JavaDoc)child;
373                 }
374             }
375             child = child.getNextSibling();
376         }
377         
378         // not found
379
return null;
380         
381     } // getFirstChildElement(Node,String):Element
382

383     /** Finds and returns the last child node with the given name. */
384     public static Element JavaDoc getLastChildElement(Node JavaDoc parent, String JavaDoc elemName) {
385         
386         // search for node
387
Node JavaDoc child = parent.getLastChild();
388         while (child != null) {
389             if (child.getNodeType() == Node.ELEMENT_NODE) {
390                 if (child.getNodeName().equals(elemName)) {
391                     return (Element JavaDoc)child;
392                 }
393             }
394             child = child.getPreviousSibling();
395         }
396         
397         // not found
398
return null;
399         
400     } // getLastChildElement(Node,String):Element
401

402     /** Finds and returns the next sibling node with the given name. */
403     public static Element JavaDoc getNextSiblingElement(Node JavaDoc node, String JavaDoc elemName) {
404         
405         // search for node
406
Node JavaDoc sibling = node.getNextSibling();
407         while (sibling != null) {
408             if (sibling.getNodeType() == Node.ELEMENT_NODE) {
409                 if (sibling.getNodeName().equals(elemName)) {
410                     return (Element JavaDoc)sibling;
411                 }
412             }
413             sibling = sibling.getNextSibling();
414         }
415         
416         // not found
417
return null;
418         
419     } // getNextSiblingdElement(Node,String):Element
420

421     /** Finds and returns the first child node with the given qualified name. */
422     public static Element JavaDoc getFirstChildElementNS(Node JavaDoc parent,
423             String JavaDoc uri, String JavaDoc localpart) {
424         
425         // search for node
426
Node JavaDoc child = parent.getFirstChild();
427         while (child != null) {
428             if (child.getNodeType() == Node.ELEMENT_NODE) {
429                 String JavaDoc childURI = child.getNamespaceURI();
430                 if (childURI != null && childURI.equals(uri) &&
431                         child.getLocalName().equals(localpart)) {
432                     return (Element JavaDoc)child;
433                 }
434             }
435             child = child.getNextSibling();
436         }
437         
438         // not found
439
return null;
440         
441     } // getFirstChildElementNS(Node,String,String):Element
442

443     /** Finds and returns the last child node with the given qualified name. */
444     public static Element JavaDoc getLastChildElementNS(Node JavaDoc parent,
445             String JavaDoc uri, String JavaDoc localpart) {
446         
447         // search for node
448
Node JavaDoc child = parent.getLastChild();
449         while (child != null) {
450             if (child.getNodeType() == Node.ELEMENT_NODE) {
451                 String JavaDoc childURI = child.getNamespaceURI();
452                 if (childURI != null && childURI.equals(uri) &&
453                         child.getLocalName().equals(localpart)) {
454                     return (Element JavaDoc)child;
455                 }
456             }
457             child = child.getPreviousSibling();
458         }
459         
460         // not found
461
return null;
462         
463     } // getLastChildElementNS(Node,String,String):Element
464

465     /** Finds and returns the next sibling node with the given qualified name. */
466     public static Element JavaDoc getNextSiblingElementNS(Node JavaDoc node,
467             String JavaDoc uri, String JavaDoc localpart) {
468         
469         // search for node
470
Node JavaDoc sibling = node.getNextSibling();
471         while (sibling != null) {
472             if (sibling.getNodeType() == Node.ELEMENT_NODE) {
473                 String JavaDoc siblingURI = sibling.getNamespaceURI();
474                 if (siblingURI != null && siblingURI.equals(uri) &&
475                         sibling.getLocalName().equals(localpart)) {
476                     return (Element JavaDoc)sibling;
477                 }
478             }
479             sibling = sibling.getNextSibling();
480         }
481         
482         // not found
483
return null;
484         
485     } // getNextSiblingdElementNS(Node,String,String):Element
486

487     /** Finds and returns the first child node with the given name. */
488     public static Element JavaDoc getFirstChildElement(Node JavaDoc parent, String JavaDoc elemNames[]) {
489         
490         // search for node
491
Node JavaDoc child = parent.getFirstChild();
492         while (child != null) {
493             if (child.getNodeType() == Node.ELEMENT_NODE) {
494                 for (int i = 0; i < elemNames.length; i++) {
495                     if (child.getNodeName().equals(elemNames[i])) {
496                         return (Element JavaDoc)child;
497                     }
498                 }
499             }
500             child = child.getNextSibling();
501         }
502         
503         // not found
504
return null;
505         
506     } // getFirstChildElement(Node,String[]):Element
507

508     /** Finds and returns the last child node with the given name. */
509     public static Element JavaDoc getLastChildElement(Node JavaDoc parent, String JavaDoc elemNames[]) {
510         
511         // search for node
512
Node JavaDoc child = parent.getLastChild();
513         while (child != null) {
514             if (child.getNodeType() == Node.ELEMENT_NODE) {
515                 for (int i = 0; i < elemNames.length; i++) {
516                     if (child.getNodeName().equals(elemNames[i])) {
517                         return (Element JavaDoc)child;
518                     }
519                 }
520             }
521             child = child.getPreviousSibling();
522         }
523         
524         // not found
525
return null;
526         
527     } // getLastChildElement(Node,String[]):Element
528

529     /** Finds and returns the next sibling node with the given name. */
530     public static Element JavaDoc getNextSiblingElement(Node JavaDoc node, String JavaDoc elemNames[]) {
531         
532         // search for node
533
Node JavaDoc sibling = node.getNextSibling();
534         while (sibling != null) {
535             if (sibling.getNodeType() == Node.ELEMENT_NODE) {
536                 for (int i = 0; i < elemNames.length; i++) {
537                     if (sibling.getNodeName().equals(elemNames[i])) {
538                         return (Element JavaDoc)sibling;
539                     }
540                 }
541             }
542             sibling = sibling.getNextSibling();
543         }
544         
545         // not found
546
return null;
547         
548     } // getNextSiblingdElement(Node,String[]):Element
549

550     /** Finds and returns the first child node with the given qualified name. */
551     public static Element JavaDoc getFirstChildElementNS(Node JavaDoc parent,
552             String JavaDoc[][] elemNames) {
553         
554         // search for node
555
Node JavaDoc child = parent.getFirstChild();
556         while (child != null) {
557             if (child.getNodeType() == Node.ELEMENT_NODE) {
558                 for (int i = 0; i < elemNames.length; i++) {
559                     String JavaDoc uri = child.getNamespaceURI();
560                     if (uri != null && uri.equals(elemNames[i][0]) &&
561                             child.getLocalName().equals(elemNames[i][1])) {
562                         return (Element JavaDoc)child;
563                     }
564                 }
565             }
566             child = child.getNextSibling();
567         }
568         
569         // not found
570
return null;
571         
572     } // getFirstChildElementNS(Node,String[][]):Element
573

574     /** Finds and returns the last child node with the given qualified name. */
575     public static Element JavaDoc getLastChildElementNS(Node JavaDoc parent,
576             String JavaDoc[][] elemNames) {
577         
578         // search for node
579
Node JavaDoc child = parent.getLastChild();
580         while (child != null) {
581             if (child.getNodeType() == Node.ELEMENT_NODE) {
582                 for (int i = 0; i < elemNames.length; i++) {
583                     String JavaDoc uri = child.getNamespaceURI();
584                     if (uri != null && uri.equals(elemNames[i][0]) &&
585                             child.getLocalName().equals(elemNames[i][1])) {
586                         return (Element JavaDoc)child;
587                     }
588                 }
589             }
590             child = child.getPreviousSibling();
591         }
592         
593         // not found
594
return null;
595         
596     } // getLastChildElementNS(Node,String[][]):Element
597

598     /** Finds and returns the next sibling node with the given qualified name. */
599     public static Element JavaDoc getNextSiblingElementNS(Node JavaDoc node,
600             String JavaDoc[][] elemNames) {
601         
602         // search for node
603
Node JavaDoc sibling = node.getNextSibling();
604         while (sibling != null) {
605             if (sibling.getNodeType() == Node.ELEMENT_NODE) {
606                 for (int i = 0; i < elemNames.length; i++) {
607                     String JavaDoc uri = sibling.getNamespaceURI();
608                     if (uri != null && uri.equals(elemNames[i][0]) &&
609                             sibling.getLocalName().equals(elemNames[i][1])) {
610                         return (Element JavaDoc)sibling;
611                     }
612                 }
613             }
614             sibling = sibling.getNextSibling();
615         }
616         
617         // not found
618
return null;
619         
620     } // getNextSiblingdElementNS(Node,String[][]):Element
621

622     /**
623      * Finds and returns the first child node with the given name and
624      * attribute name, value pair.
625      */

626     public static Element JavaDoc getFirstChildElement(Node JavaDoc parent,
627             String JavaDoc elemName,
628             String JavaDoc attrName,
629             String JavaDoc attrValue) {
630         
631         // search for node
632
Node JavaDoc child = parent.getFirstChild();
633         while (child != null) {
634             if (child.getNodeType() == Node.ELEMENT_NODE) {
635                 Element JavaDoc element = (Element JavaDoc)child;
636                 if (element.getNodeName().equals(elemName) &&
637                         element.getAttribute(attrName).equals(attrValue)) {
638                     return element;
639                 }
640             }
641             child = child.getNextSibling();
642         }
643         
644         // not found
645
return null;
646         
647     } // getFirstChildElement(Node,String,String,String):Element
648

649     /**
650      * Finds and returns the last child node with the given name and
651      * attribute name, value pair.
652      */

653     public static Element JavaDoc getLastChildElement(Node JavaDoc parent,
654             String JavaDoc elemName,
655             String JavaDoc attrName,
656             String JavaDoc attrValue) {
657         
658         // search for node
659
Node JavaDoc child = parent.getLastChild();
660         while (child != null) {
661             if (child.getNodeType() == Node.ELEMENT_NODE) {
662                 Element JavaDoc element = (Element JavaDoc)child;
663                 if (element.getNodeName().equals(elemName) &&
664                         element.getAttribute(attrName).equals(attrValue)) {
665                     return element;
666                 }
667             }
668             child = child.getPreviousSibling();
669         }
670         
671         // not found
672
return null;
673         
674     } // getLastChildElement(Node,String,String,String):Element
675

676     /**
677      * Finds and returns the next sibling node with the given name and
678      * attribute name, value pair. Since only elements have attributes,
679      * the node returned will be of type Node.ELEMENT_NODE.
680      */

681     public static Element JavaDoc getNextSiblingElement(Node JavaDoc node,
682             String JavaDoc elemName,
683             String JavaDoc attrName,
684             String JavaDoc attrValue) {
685         
686         // search for node
687
Node JavaDoc sibling = node.getNextSibling();
688         while (sibling != null) {
689             if (sibling.getNodeType() == Node.ELEMENT_NODE) {
690                 Element JavaDoc element = (Element JavaDoc)sibling;
691                 if (element.getNodeName().equals(elemName) &&
692                         element.getAttribute(attrName).equals(attrValue)) {
693                     return element;
694                 }
695             }
696             sibling = sibling.getNextSibling();
697         }
698         
699         // not found
700
return null;
701         
702     } // getNextSiblingElement(Node,String,String,String):Element
703

704     /**
705      * Returns the concatenated child text of the specified node.
706      * This method only looks at the immediate children of type
707      * <code>Node.TEXT_NODE</code> or the children of any child
708      * node that is of type <code>Node.CDATA_SECTION_NODE</code>
709      * for the concatenation.
710      *
711      * @param node The node to look at.
712      */

713     public static String JavaDoc getChildText(Node JavaDoc node) {
714         
715         // is there anything to do?
716
if (node == null) {
717             return null;
718         }
719         
720         // concatenate children text
721
StringBuffer JavaDoc str = new StringBuffer JavaDoc();
722         Node JavaDoc child = node.getFirstChild();
723         while (child != null) {
724             short type = child.getNodeType();
725             if (type == Node.TEXT_NODE) {
726                 str.append(child.getNodeValue());
727             }
728             else if (type == Node.CDATA_SECTION_NODE) {
729                 str.append(getChildText(child));
730             }
731             child = child.getNextSibling();
732         }
733         
734         // return text value
735
return str.toString();
736         
737     } // getChildText(Node):String
738

739     // return the name of this element
740
public static String JavaDoc getName(Node JavaDoc node) {
741         return node.getNodeName();
742     } // getLocalName(Element): String
743

744     /** returns local name of this element if not null, otherwise
745      returns the name of the node
746      */

747     public static String JavaDoc getLocalName(Node JavaDoc node) {
748         String JavaDoc name = node.getLocalName();
749         return (name!=null)? name:node.getNodeName();
750     } // getLocalName(Element): String
751

752     public static Element JavaDoc getParent(Element JavaDoc elem) {
753         Node JavaDoc parent = elem.getParentNode();
754         if (parent instanceof Element JavaDoc)
755             return (Element JavaDoc)parent;
756         return null;
757     } // getParent(Element):Element
758

759     // get the Document of which this Node is a part
760
public static Document JavaDoc getDocument(Node JavaDoc node) {
761         return node.getOwnerDocument();
762     } // getDocument(Node):Document
763

764     // return this Document's root node
765
public static Element JavaDoc getRoot(Document JavaDoc doc) {
766         return doc.getDocumentElement();
767     } // getRoot(Document(: Element
768

769     // some methods for handling attributes:
770

771     // return the right attribute node
772
public static Attr JavaDoc getAttr(Element JavaDoc elem, String JavaDoc name) {
773         return elem.getAttributeNode(name);
774     } // getAttr(Element, String):Attr
775

776     // return the right attribute node
777
public static Attr JavaDoc getAttrNS(Element JavaDoc elem, String JavaDoc nsUri,
778             String JavaDoc localName) {
779         return elem.getAttributeNodeNS(nsUri, localName);
780     } // getAttrNS(Element, String):Attr
781

782     // get all the attributes for an Element
783
public static Attr JavaDoc[] getAttrs(Element JavaDoc elem) {
784         NamedNodeMap JavaDoc attrMap = elem.getAttributes();
785         Attr JavaDoc [] attrArray = new Attr JavaDoc[attrMap.getLength()];
786         for (int i=0; i<attrMap.getLength(); i++)
787             attrArray[i] = (Attr JavaDoc)attrMap.item(i);
788         return attrArray;
789     } // getAttrs(Element): Attr[]
790

791     // get attribute's value
792
public static String JavaDoc getValue(Attr JavaDoc attribute) {
793         return attribute.getValue();
794     } // getValue(Attr):String
795

796     // It is noteworthy that, because of the way the DOM specs
797
// work, the next two methods return the empty string (not
798
// null!) when the attribute with the specified name does not
799
// exist on an element. Beware!
800

801     // return the value of the attribute of the given element
802
// with the given name
803
public static String JavaDoc getAttrValue(Element JavaDoc elem, String JavaDoc name) {
804         return elem.getAttribute(name);
805     } // getAttr(Element, String):Attr
806

807     // return the value of the attribute of the given element
808
// with the given name
809
public static String JavaDoc getAttrValueNS(Element JavaDoc elem, String JavaDoc nsUri,
810             String JavaDoc localName) {
811         return elem.getAttributeNS(nsUri, localName);
812     } // getAttrValueNS(Element, String):Attr
813

814     // return the prefix
815
public static String JavaDoc getPrefix(Node JavaDoc node) {
816         return node.getPrefix();
817     }
818     
819     // return the namespace URI
820
public static String JavaDoc getNamespaceURI(Node JavaDoc node) {
821         return node.getNamespaceURI();
822     }
823     
824     //return synthetic annotation
825
public static String JavaDoc getSyntheticAnnotation(Node JavaDoc node) {
826         if(node instanceof ElementImpl) {
827             return ((ElementImpl)node).getSyntheticAnnotation();
828         }
829         return null;
830     }
831 } // class XUtil
832
Popular Tags