KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > addr > DOMUtils


1 /*
2  * Copyright 2001-2004 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 samples.addr;
18
19 import org.w3c.dom.Attr JavaDoc;
20 import org.w3c.dom.CharacterData JavaDoc;
21 import org.w3c.dom.Element JavaDoc;
22 import org.w3c.dom.Node JavaDoc;
23 import org.w3c.dom.NodeList JavaDoc;
24
25 /**
26  * @author Matthew J. Duftler
27  * @author Sanjiva Weerawarana
28  */

29 public class DOMUtils {
30     /**
31      * The namespaceURI represented by the prefix <code>xmlns</code>.
32      */

33     private static String JavaDoc NS_URI_XMLNS = "http://www.w3.org/2000/xmlns/";
34     
35     /**
36      * Returns the value of an attribute of an element. Returns null
37      * if the attribute is not found (whereas Element.getAttribute
38      * returns "" if an attrib is not found).
39      *
40      * @param el Element whose attrib is looked for
41      * @param attrName name of attribute to look for
42      * @return the attribute value
43      */

44     static public String JavaDoc getAttribute (Element JavaDoc el, String JavaDoc attrName) {
45         String JavaDoc sRet = null;
46         Attr JavaDoc attr = el.getAttributeNode(attrName);
47         
48         if (attr != null) {
49             sRet = attr.getValue();
50         }
51         return sRet;
52     }
53     
54     /**
55      * Returns the value of an attribute of an element. Returns null
56      * if the attribute is not found (whereas Element.getAttributeNS
57      * returns "" if an attrib is not found).
58      *
59      * @param el Element whose attrib is looked for
60      * @param namespaceURI namespace URI of attribute to look for
61      * @param localPart local part of attribute to look for
62      * @return the attribute value
63      */

64     static public String JavaDoc getAttributeNS (Element JavaDoc el,
65                                          String JavaDoc namespaceURI,
66                                          String JavaDoc localPart) {
67         String JavaDoc sRet = null;
68         Attr JavaDoc attr = el.getAttributeNodeNS (namespaceURI, localPart);
69         
70         if (attr != null) {
71             sRet = attr.getValue ();
72         }
73         
74         return sRet;
75     }
76     
77     /**
78      * Concat all the text and cdata node children of this elem and return
79      * the resulting text.
80      *
81      * @param parentEl the element whose cdata/text node values are to
82      * be combined.
83      * @return the concatanated string.
84      */

85     static public String JavaDoc getChildCharacterData (Element JavaDoc parentEl) {
86         if (parentEl == null) {
87             return null;
88         }
89         Node JavaDoc tempNode = parentEl.getFirstChild();
90         StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc();
91         CharacterData JavaDoc charData;
92         
93         while (tempNode != null) {
94             switch (tempNode.getNodeType()) {
95                 case Node.TEXT_NODE :
96                 case Node.CDATA_SECTION_NODE : charData = (CharacterData JavaDoc)tempNode;
97                     strBuf.append(charData.getData());
98                     break;
99             }
100             tempNode = tempNode.getNextSibling();
101         }
102         return strBuf.toString();
103     }
104     
105     /**
106      * Return the first child element of the given element. Null if no
107      * children are found.
108      *
109      * @param elem Element whose child is to be returned
110      * @return the first child element.
111      */

112     public static Element JavaDoc getFirstChildElement (Element JavaDoc elem) {
113         for (Node JavaDoc n = elem.getFirstChild (); n != null; n = n.getNextSibling ()) {
114             if (n.getNodeType () == Node.ELEMENT_NODE) {
115                 return (Element JavaDoc) n;
116             }
117         }
118         return null;
119     }
120     
121     /**
122      * Return the next sibling element of the given element. Null if no
123      * more sibling elements are found.
124      *
125      * @param elem Element whose sibling element is to be returned
126      * @return the next sibling element.
127      */

128     public static Element JavaDoc getNextSiblingElement (Element JavaDoc elem) {
129         for (Node JavaDoc n = elem.getNextSibling (); n != null; n = n.getNextSibling ()) {
130             if (n.getNodeType () == Node.ELEMENT_NODE) {
131                 return (Element JavaDoc) n;
132             }
133         }
134         return null;
135     }
136     
137     /**
138      * Return the first child element of the given element which has the
139      * given attribute with the given value.
140      *
141      * @param elem the element whose children are to be searched
142      * @param attrName the attrib that must be present
143      * @param attrValue the desired value of the attribute
144      *
145      * @return the first matching child element.
146      */

147     public static Element JavaDoc findChildElementWithAttribute (Element JavaDoc elem,
148                                                          String JavaDoc attrName,
149                                                          String JavaDoc attrValue) {
150         for (Node JavaDoc n = elem.getFirstChild (); n != null; n = n.getNextSibling ()) {
151             if (n.getNodeType () == Node.ELEMENT_NODE) {
152                 if (attrValue.equals (DOMUtils.getAttribute ((Element JavaDoc) n, attrName))) {
153                     return (Element JavaDoc) n;
154                 }
155             }
156         }
157         return null;
158     }
159     
160     /**
161      * Count number of children of a certain type of the given element.
162      *
163      * @param elem the element whose kids are to be counted
164      *
165      * @return the number of matching kids.
166      */

167     public static int countKids (Element JavaDoc elem, short nodeType) {
168         int nkids = 0;
169         for (Node JavaDoc n = elem.getFirstChild (); n != null; n = n.getNextSibling ()) {
170             if (n.getNodeType () == nodeType) {
171                 nkids++;
172             }
173         }
174         return nkids;
175     }
176     
177     /**
178      * Given a prefix and a node, return the namespace URI that the prefix
179      * has been associated with. This method is useful in resolving the
180      * namespace URI of attribute values which are being interpreted as
181      * QNames. If prefix is null, this method will return the default
182      * namespace.
183      *
184      * @param context the starting node (looks up recursively from here)
185      * @param prefix the prefix to find an xmlns:prefix=uri for
186      *
187      * @return the namespace URI or null if not found
188      */

189     public static String JavaDoc getNamespaceURIFromPrefix (Node JavaDoc context,
190                                                     String JavaDoc prefix) {
191         short nodeType = context.getNodeType ();
192         Node JavaDoc tempNode = null;
193         
194         switch (nodeType)
195         {
196             case Node.ATTRIBUTE_NODE :
197                 {
198                     tempNode = ((Attr JavaDoc) context).getOwnerElement ();
199                     break;
200                 }
201             case Node.ELEMENT_NODE :
202                 {
203                     tempNode = context;
204                     break;
205                 }
206             default :
207                 {
208                     tempNode = context.getParentNode ();
209                     break;
210                 }
211         }
212         
213         while (tempNode != null && tempNode.getNodeType () == Node.ELEMENT_NODE)
214         {
215             Element JavaDoc tempEl = (Element JavaDoc) tempNode;
216             String JavaDoc namespaceURI = (prefix == null)
217                 ? getAttribute (tempEl, "xmlns")
218                 : getAttributeNS (tempEl, NS_URI_XMLNS, prefix);
219             
220             if (namespaceURI != null)
221             {
222                 return namespaceURI;
223             }
224             else
225             {
226                 tempNode = tempEl.getParentNode ();
227             }
228         }
229         
230         return null;
231     }
232     
233     public static Element JavaDoc getElementByID(Element JavaDoc el, String JavaDoc id)
234     {
235         if (el == null)
236             return null;
237         String JavaDoc thisId = el.getAttribute("id");
238         if (id.equals(thisId))
239             return el;
240         
241         NodeList JavaDoc list = el.getChildNodes();
242         for (int i = 0; i < list.getLength(); i++) {
243             Node JavaDoc node = list.item(i);
244             if (node instanceof Element JavaDoc) {
245                 Element JavaDoc ret = getElementByID((Element JavaDoc)node, id);
246                 if (ret != null)
247                     return ret;
248             }
249         }
250         
251         return null;
252     }
253 }
254
Popular Tags