KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > util > DOMUtil


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package com.sun.org.apache.xerces.internal.util;
59
60 import com.sun.org.apache.xerces.internal.dom.AttrImpl;
61 import com.sun.org.apache.xerces.internal.dom.DocumentImpl;
62
63 import org.w3c.dom.Attr JavaDoc;
64 import org.w3c.dom.Document JavaDoc;
65 import org.w3c.dom.DOMException JavaDoc;
66 import org.w3c.dom.Element JavaDoc;
67 import org.w3c.dom.NamedNodeMap JavaDoc;
68 import org.w3c.dom.Node JavaDoc;
69
70 /**
71  * Some useful utility methods.
72  * This class was modified in Xerces2 with a view to abstracting as
73  * much as possible away from the representation of the underlying
74  * parsed structure (i.e., the DOM). This was done so that, if Xerces
75  * ever adopts an in-memory representation more efficient than the DOM
76  * (such as a DTM), we should easily be able to convert our schema
77  * parsing to utilize it.
78  *
79  * @version $ID DOMUtil
80  */

81 public class DOMUtil {
82
83     //
84
// Constructors
85
//
86

87     /** This class cannot be instantiated. */
88     protected DOMUtil() {}
89
90     //
91
// Public static methods
92
//
93

94     /**
95      * Copies the source tree into the specified place in a destination
96      * tree. The source node and its children are appended as children
97      * of the destination node.
98      * <p>
99      * <em>Note:</em> This is an iterative implementation.
100      */

101     public static void copyInto(Node JavaDoc src, Node JavaDoc dest) throws DOMException JavaDoc {
102
103         // get node factory
104
Document JavaDoc factory = dest.getOwnerDocument();
105         boolean domimpl = factory instanceof DocumentImpl;
106
107         // placement variables
108
Node JavaDoc start = src;
109         Node JavaDoc parent = src;
110         Node JavaDoc place = src;
111
112         // traverse source tree
113
while (place != null) {
114
115             // copy this node
116
Node JavaDoc node = null;
117             int type = place.getNodeType();
118             switch (type) {
119                 case Node.CDATA_SECTION_NODE: {
120                     node = factory.createCDATASection(place.getNodeValue());
121                     break;
122                 }
123                 case Node.COMMENT_NODE: {
124                     node = factory.createComment(place.getNodeValue());
125                     break;
126                 }
127                 case Node.ELEMENT_NODE: {
128                     Element JavaDoc element = factory.createElement(place.getNodeName());
129                     node = element;
130                     NamedNodeMap JavaDoc attrs = place.getAttributes();
131                     int attrCount = attrs.getLength();
132                     for (int i = 0; i < attrCount; i++) {
133                         Attr JavaDoc attr = (Attr JavaDoc)attrs.item(i);
134                         String JavaDoc attrName = attr.getNodeName();
135                         String JavaDoc attrValue = attr.getNodeValue();
136                         element.setAttribute(attrName, attrValue);
137                         if (domimpl && !attr.getSpecified()) {
138                             ((AttrImpl)element.getAttributeNode(attrName)).setSpecified(false);
139                         }
140                     }
141                     break;
142                 }
143                 case Node.ENTITY_REFERENCE_NODE: {
144                     node = factory.createEntityReference(place.getNodeName());
145                     break;
146                 }
147                 case Node.PROCESSING_INSTRUCTION_NODE: {
148                     node = factory.createProcessingInstruction(place.getNodeName(),
149                                                                place.getNodeValue());
150                     break;
151                 }
152                 case Node.TEXT_NODE: {
153                     node = factory.createTextNode(place.getNodeValue());
154                     break;
155                 }
156                 default: {
157                     throw new IllegalArgumentException JavaDoc("can't copy node type, "+
158                                                        type+" ("+
159                                                        node.getNodeName()+')');
160                 }
161             }
162             dest.appendChild(node);
163
164             // iterate over children
165
if (place.hasChildNodes()) {
166                 parent = place;
167                 place = place.getFirstChild();
168                 dest = node;
169             }
170
171             // advance
172
else {
173                 place = place.getNextSibling();
174                 while (place == null && parent != start) {
175                     place = parent.getNextSibling();
176                     parent = parent.getParentNode();
177                     dest = dest.getParentNode();
178                 }
179             }
180
181         }
182
183     } // copyInto(Node,Node)
184

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

202     /** Finds and returns the first visible child element node. */
203     public static Element JavaDoc getFirstVisibleChildElement(Node JavaDoc parent) {
204
205         // search for node
206
Node JavaDoc child = parent.getFirstChild();
207         while (child != null) {
208             if (child.getNodeType() == Node.ELEMENT_NODE &&
209                 !isHidden(child)) {
210                 return (Element JavaDoc)child;
211             }
212             child = child.getNextSibling();
213         }
214
215         // not found
216
return null;
217
218     } // getFirstChildElement(Node):Element
219

220     /** Finds and returns the last child element node. */
221     public static Element JavaDoc getLastChildElement(Node JavaDoc parent) {
222
223         // search for node
224
Node JavaDoc child = parent.getLastChild();
225         while (child != null) {
226             if (child.getNodeType() == Node.ELEMENT_NODE) {
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     public static Element JavaDoc getLastVisibleChildElement(Node JavaDoc parent) {
239
240         // search for node
241
Node JavaDoc child = parent.getLastChild();
242         while (child != null) {
243             if (child.getNodeType() == Node.ELEMENT_NODE &&
244                 !isHidden(child)) {
245                 return (Element JavaDoc)child;
246             }
247             child = child.getPreviousSibling();
248         }
249
250         // not found
251
return null;
252
253     } // getLastChildElement(Node):Element
254

255     /** Finds and returns the next sibling element node. */
256     public static Element JavaDoc getNextSiblingElement(Node JavaDoc node) {
257
258         // search for node
259
Node JavaDoc sibling = node.getNextSibling();
260         while (sibling != null) {
261             if (sibling.getNodeType() == Node.ELEMENT_NODE) {
262                 return (Element JavaDoc)sibling;
263             }
264             sibling = sibling.getNextSibling();
265         }
266
267         // not found
268
return null;
269
270     } // getNextSiblingElement(Node):Element
271

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

290     // set this Node as being hidden
291
public static void setHidden(Node JavaDoc node) {
292         if (node instanceof com.sun.org.apache.xerces.internal.impl.xs.opti.NodeImpl)
293             ((com.sun.org.apache.xerces.internal.impl.xs.opti.NodeImpl)node).setReadOnly(true, false);
294         else if (node instanceof com.sun.org.apache.xerces.internal.dom.NodeImpl)
295             ((com.sun.org.apache.xerces.internal.dom.NodeImpl)node).setReadOnly(true, false);
296     } // setHidden(node):void
297

298     // set this Node as being visible
299
public static void setVisible(Node JavaDoc node) {
300         if (node instanceof com.sun.org.apache.xerces.internal.impl.xs.opti.NodeImpl)
301             ((com.sun.org.apache.xerces.internal.impl.xs.opti.NodeImpl)node).setReadOnly(false, false);
302         else if (node instanceof com.sun.org.apache.xerces.internal.dom.NodeImpl)
303             ((com.sun.org.apache.xerces.internal.dom.NodeImpl)node).setReadOnly(false, false);
304     } // setVisible(node):void
305

306     // is this node hidden?
307
public static boolean isHidden(Node JavaDoc node) {
308         if (node instanceof com.sun.org.apache.xerces.internal.impl.xs.opti.NodeImpl)
309             return ((com.sun.org.apache.xerces.internal.impl.xs.opti.NodeImpl)node).getReadOnly();
310         else if (node instanceof com.sun.org.apache.xerces.internal.dom.NodeImpl)
311             return ((com.sun.org.apache.xerces.internal.dom.NodeImpl)node).getReadOnly();
312         return false;
313     } // isHidden(Node):boolean
314

315     /** Finds and returns the first child node with the given name. */
316     public static Element JavaDoc getFirstChildElement(Node JavaDoc parent, String JavaDoc elemName) {
317
318         // search for node
319
Node JavaDoc child = parent.getFirstChild();
320         while (child != null) {
321             if (child.getNodeType() == Node.ELEMENT_NODE) {
322                 if (child.getNodeName().equals(elemName)) {
323                     return (Element JavaDoc)child;
324                 }
325             }
326             child = child.getNextSibling();
327         }
328
329         // not found
330
return null;
331
332     } // getFirstChildElement(Node,String):Element
333

334     /** Finds and returns the last child node with the given name. */
335     public static Element JavaDoc getLastChildElement(Node JavaDoc parent, String JavaDoc elemName) {
336
337         // search for node
338
Node JavaDoc child = parent.getLastChild();
339         while (child != null) {
340             if (child.getNodeType() == Node.ELEMENT_NODE) {
341                 if (child.getNodeName().equals(elemName)) {
342                     return (Element JavaDoc)child;
343                 }
344             }
345             child = child.getPreviousSibling();
346         }
347
348         // not found
349
return null;
350
351     } // getLastChildElement(Node,String):Element
352

353     /** Finds and returns the next sibling node with the given name. */
354     public static Element JavaDoc getNextSiblingElement(Node JavaDoc node, String JavaDoc elemName) {
355
356         // search for node
357
Node JavaDoc sibling = node.getNextSibling();
358         while (sibling != null) {
359             if (sibling.getNodeType() == Node.ELEMENT_NODE) {
360                 if (sibling.getNodeName().equals(elemName)) {
361                     return (Element JavaDoc)sibling;
362                 }
363             }
364             sibling = sibling.getNextSibling();
365         }
366
367         // not found
368
return null;
369
370     } // getNextSiblingdElement(Node,String):Element
371

372     /** Finds and returns the first child node with the given qualified name. */
373     public static Element JavaDoc getFirstChildElementNS(Node JavaDoc parent,
374                                                  String JavaDoc uri, String JavaDoc localpart) {
375
376         // search for node
377
Node JavaDoc child = parent.getFirstChild();
378         while (child != null) {
379             if (child.getNodeType() == Node.ELEMENT_NODE) {
380                 String JavaDoc childURI = child.getNamespaceURI();
381                 if (childURI != null && childURI.equals(uri) &&
382                     child.getLocalName().equals(localpart)) {
383                     return (Element JavaDoc)child;
384                 }
385             }
386             child = child.getNextSibling();
387         }
388
389         // not found
390
return null;
391
392     } // getFirstChildElementNS(Node,String,String):Element
393

394     /** Finds and returns the last child node with the given qualified name. */
395     public static Element JavaDoc getLastChildElementNS(Node JavaDoc parent,
396                                                 String JavaDoc uri, String JavaDoc localpart) {
397
398         // search for node
399
Node JavaDoc child = parent.getLastChild();
400         while (child != null) {
401             if (child.getNodeType() == Node.ELEMENT_NODE) {
402                 String JavaDoc childURI = child.getNamespaceURI();
403                 if (childURI != null && childURI.equals(uri) &&
404                     child.getLocalName().equals(localpart)) {
405                     return (Element JavaDoc)child;
406                 }
407             }
408             child = child.getPreviousSibling();
409         }
410
411         // not found
412
return null;
413
414     } // getLastChildElementNS(Node,String,String):Element
415

416     /** Finds and returns the next sibling node with the given qualified name. */
417     public static Element JavaDoc getNextSiblingElementNS(Node JavaDoc node,
418                                                   String JavaDoc uri, String JavaDoc localpart) {
419
420         // search for node
421
Node JavaDoc sibling = node.getNextSibling();
422         while (sibling != null) {
423             if (sibling.getNodeType() == Node.ELEMENT_NODE) {
424                 String JavaDoc siblingURI = sibling.getNamespaceURI();
425                 if (siblingURI != null && siblingURI.equals(uri) &&
426                     sibling.getLocalName().equals(localpart)) {
427                     return (Element JavaDoc)sibling;
428                 }
429             }
430             sibling = sibling.getNextSibling();
431         }
432
433         // not found
434
return null;
435
436     } // getNextSiblingdElementNS(Node,String,String):Element
437

438     /** Finds and returns the first child node with the given name. */
439     public static Element JavaDoc getFirstChildElement(Node JavaDoc parent, String JavaDoc elemNames[]) {
440
441         // search for node
442
Node JavaDoc child = parent.getFirstChild();
443         while (child != null) {
444             if (child.getNodeType() == Node.ELEMENT_NODE) {
445                 for (int i = 0; i < elemNames.length; i++) {
446                     if (child.getNodeName().equals(elemNames[i])) {
447                         return (Element JavaDoc)child;
448                     }
449                 }
450             }
451             child = child.getNextSibling();
452         }
453
454         // not found
455
return null;
456
457     } // getFirstChildElement(Node,String[]):Element
458

459     /** Finds and returns the last child node with the given name. */
460     public static Element JavaDoc getLastChildElement(Node JavaDoc parent, String JavaDoc elemNames[]) {
461
462         // search for node
463
Node JavaDoc child = parent.getLastChild();
464         while (child != null) {
465             if (child.getNodeType() == Node.ELEMENT_NODE) {
466                 for (int i = 0; i < elemNames.length; i++) {
467                     if (child.getNodeName().equals(elemNames[i])) {
468                         return (Element JavaDoc)child;
469                     }
470                 }
471             }
472             child = child.getPreviousSibling();
473         }
474
475         // not found
476
return null;
477
478     } // getLastChildElement(Node,String[]):Element
479

480     /** Finds and returns the next sibling node with the given name. */
481     public static Element JavaDoc getNextSiblingElement(Node JavaDoc node, String JavaDoc elemNames[]) {
482
483         // search for node
484
Node JavaDoc sibling = node.getNextSibling();
485         while (sibling != null) {
486             if (sibling.getNodeType() == Node.ELEMENT_NODE) {
487                 for (int i = 0; i < elemNames.length; i++) {
488                     if (sibling.getNodeName().equals(elemNames[i])) {
489                         return (Element JavaDoc)sibling;
490                     }
491                 }
492             }
493             sibling = sibling.getNextSibling();
494         }
495
496         // not found
497
return null;
498
499     } // getNextSiblingdElement(Node,String[]):Element
500

501     /** Finds and returns the first child node with the given qualified name. */
502     public static Element JavaDoc getFirstChildElementNS(Node JavaDoc parent,
503                                                  String JavaDoc[][] elemNames) {
504
505         // search for node
506
Node JavaDoc child = parent.getFirstChild();
507         while (child != null) {
508             if (child.getNodeType() == Node.ELEMENT_NODE) {
509                 for (int i = 0; i < elemNames.length; i++) {
510                     String JavaDoc uri = child.getNamespaceURI();
511                     if (uri != null && uri.equals(elemNames[i][0]) &&
512                         child.getLocalName().equals(elemNames[i][1])) {
513                         return (Element JavaDoc)child;
514                     }
515                 }
516             }
517             child = child.getNextSibling();
518         }
519
520         // not found
521
return null;
522
523     } // getFirstChildElementNS(Node,String[][]):Element
524

525     /** Finds and returns the last child node with the given qualified name. */
526     public static Element JavaDoc getLastChildElementNS(Node JavaDoc parent,
527                                                 String JavaDoc[][] elemNames) {
528
529         // search for node
530
Node JavaDoc child = parent.getLastChild();
531         while (child != null) {
532             if (child.getNodeType() == Node.ELEMENT_NODE) {
533                 for (int i = 0; i < elemNames.length; i++) {
534                     String JavaDoc uri = child.getNamespaceURI();
535                     if (uri != null && uri.equals(elemNames[i][0]) &&
536                         child.getLocalName().equals(elemNames[i][1])) {
537                         return (Element JavaDoc)child;
538                     }
539                 }
540             }
541             child = child.getPreviousSibling();
542         }
543
544         // not found
545
return null;
546
547     } // getLastChildElementNS(Node,String[][]):Element
548

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

573     /**
574      * Finds and returns the first child node with the given name and
575      * attribute name, value pair.
576      */

577     public static Element JavaDoc getFirstChildElement(Node JavaDoc parent,
578                                                String JavaDoc elemName,
579                                                String JavaDoc attrName,
580                                                String JavaDoc attrValue) {
581
582         // search for node
583
Node JavaDoc child = parent.getFirstChild();
584         while (child != null) {
585             if (child.getNodeType() == Node.ELEMENT_NODE) {
586                 Element JavaDoc element = (Element JavaDoc)child;
587                 if (element.getNodeName().equals(elemName) &&
588                     element.getAttribute(attrName).equals(attrValue)) {
589                     return element;
590                 }
591             }
592             child = child.getNextSibling();
593         }
594
595         // not found
596
return null;
597
598     } // getFirstChildElement(Node,String,String,String):Element
599

600     /**
601      * Finds and returns the last child node with the given name and
602      * attribute name, value pair.
603      */

604     public static Element JavaDoc getLastChildElement(Node JavaDoc parent,
605                                                String JavaDoc elemName,
606                                                String JavaDoc attrName,
607                                                String JavaDoc attrValue) {
608
609         // search for node
610
Node JavaDoc child = parent.getLastChild();
611         while (child != null) {
612             if (child.getNodeType() == Node.ELEMENT_NODE) {
613                 Element JavaDoc element = (Element JavaDoc)child;
614                 if (element.getNodeName().equals(elemName) &&
615                     element.getAttribute(attrName).equals(attrValue)) {
616                     return element;
617                 }
618             }
619             child = child.getPreviousSibling();
620         }
621
622         // not found
623
return null;
624
625     } // getLastChildElement(Node,String,String,String):Element
626

627     /**
628      * Finds and returns the next sibling node with the given name and
629      * attribute name, value pair. Since only elements have attributes,
630      * the node returned will be of type Node.ELEMENT_NODE.
631      */

632     public static Element JavaDoc getNextSiblingElement(Node JavaDoc node,
633                                                 String JavaDoc elemName,
634                                                 String JavaDoc attrName,
635                                                 String JavaDoc attrValue) {
636
637         // search for node
638
Node JavaDoc sibling = node.getNextSibling();
639         while (sibling != null) {
640             if (sibling.getNodeType() == Node.ELEMENT_NODE) {
641                 Element JavaDoc element = (Element JavaDoc)sibling;
642                 if (element.getNodeName().equals(elemName) &&
643                     element.getAttribute(attrName).equals(attrValue)) {
644                     return element;
645                 }
646             }
647             sibling = sibling.getNextSibling();
648         }
649
650         // not found
651
return null;
652
653     } // getNextSiblingElement(Node,String,String,String):Element
654

655     /**
656      * Returns the concatenated child text of the specified node.
657      * This method only looks at the immediate children of type
658      * <code>Node.TEXT_NODE</code> or the children of any child
659      * node that is of type <code>Node.CDATA_SECTION_NODE</code>
660      * for the concatenation.
661      *
662      * @param node The node to look at.
663      */

664     public static String JavaDoc getChildText(Node JavaDoc node) {
665
666         // is there anything to do?
667
if (node == null) {
668             return null;
669         }
670
671         // concatenate children text
672
StringBuffer JavaDoc str = new StringBuffer JavaDoc();
673         Node JavaDoc child = node.getFirstChild();
674         while (child != null) {
675             short type = child.getNodeType();
676             if (type == Node.TEXT_NODE) {
677                 str.append(child.getNodeValue());
678             }
679             else if (type == Node.CDATA_SECTION_NODE) {
680                 str.append(getChildText(child));
681             }
682             child = child.getNextSibling();
683         }
684
685         // return text value
686
return str.toString();
687
688     } // getChildText(Node):String
689

690     // return the name of this element
691
public static String JavaDoc getName(Node JavaDoc node) {
692         return node.getNodeName();
693     } // getLocalName(Element): String
694

695     /** returns local name of this element if not null, otherwise
696         returns the name of the node
697     */

698     public static String JavaDoc getLocalName(Node JavaDoc node) {
699         String JavaDoc name = node.getLocalName();
700         return (name!=null)? name:node.getNodeName();
701     } // getLocalName(Element): String
702

703     public static Element JavaDoc getParent(Element JavaDoc elem) {
704         Node JavaDoc parent = elem.getParentNode();
705         if (parent instanceof Element JavaDoc)
706             return (Element JavaDoc)parent;
707         return null;
708     } // getParent(Element):Element
709

710     // get the Document of which this Node is a part
711
public static Document JavaDoc getDocument(Node JavaDoc node) {
712         return node.getOwnerDocument();
713     } // getDocument(Node):Document
714

715     // return this Document's root node
716
public static Element JavaDoc getRoot(Document JavaDoc doc) {
717         return doc.getDocumentElement();
718      } // getRoot(Document(: Element
719

720    // some methods for handling attributes:
721

722     // return the right attribute node
723
public static Attr JavaDoc getAttr(Element JavaDoc elem, String JavaDoc name) {
724         return elem.getAttributeNode(name);
725     } // getAttr(Element, String):Attr
726

727     // return the right attribute node
728
public static Attr JavaDoc getAttrNS(Element JavaDoc elem, String JavaDoc nsUri,
729             String JavaDoc localName) {
730         return elem.getAttributeNodeNS(nsUri, localName);
731     } // getAttrNS(Element, String):Attr
732

733     // get all the attributes for an Element
734
public static Attr JavaDoc[] getAttrs(Element JavaDoc elem) {
735         NamedNodeMap JavaDoc attrMap = elem.getAttributes();
736         Attr JavaDoc [] attrArray = new Attr JavaDoc[attrMap.getLength()];
737         for (int i=0; i<attrMap.getLength(); i++)
738             attrArray[i] = (Attr JavaDoc)attrMap.item(i);
739         return attrArray;
740     } // getAttrs(Element): Attr[]
741

742     // get attribute's value
743
public static String JavaDoc getValue(Attr JavaDoc attribute) {
744         return attribute.getValue();
745     } // getValue(Attr):String
746

747     // It is noteworthy that, because of the way the DOM specs
748
// work, the next two methods return the empty string (not
749
// null!) when the attribute with the specified name does not
750
// exist on an element. Beware!
751

752     // return the value of the attribute of the given element
753
// with the given name
754
public static String JavaDoc getAttrValue(Element JavaDoc elem, String JavaDoc name) {
755         return elem.getAttribute(name);
756     } // getAttr(Element, String):Attr
757

758     // return the value of the attribute of the given element
759
// with the given name
760
public static String JavaDoc getAttrValueNS(Element JavaDoc elem, String JavaDoc nsUri,
761             String JavaDoc localName) {
762         return elem.getAttributeNS(nsUri, localName);
763     } // getAttrValueNS(Element, String):Attr
764

765     // return the namespace URI
766
public static String JavaDoc getNamespaceURI(Node JavaDoc node) {
767         return node.getNamespaceURI();
768     }
769 } // class XUtil
770
Popular Tags