KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > app > util > DomUtil


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.app.util;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.List JavaDoc;
34
35 import org.w3c.dom.Element JavaDoc;
36 import org.w3c.dom.Node JavaDoc;
37 import org.w3c.dom.NodeList JavaDoc;
38 import org.w3c.dom.Text JavaDoc;
39
40 /**
41  * A utility class which provides methods for working with a W3C DOM.
42  */

43 public class DomUtil {
44
45     /**
46      * Determines whether a specific boolean flag is set on an element.
47      *
48      * @param element The element to analyze.
49      * @param attributeName The name of the boolean 'flag' attribute.
50      * @return True if the value of the attribute is 'true', false if it is
51      * not or if the attribute does not exist.
52      */

53     public static boolean getBooleanAttribute(Element JavaDoc element, String JavaDoc attributeName) {
54         String JavaDoc value = element.getAttribute(attributeName);
55         if (value == null) {
56             return false;
57         } else if (value.equals("true")) {
58             return true;
59         } else {
60             return false;
61         }
62     }
63
64     /**
65      * Retrieves the first immediate child element of the specified element
66      * whose name matches the provided <code>name</code> parameter.
67      *
68      * @param parentElement The element to search.
69      * @param name The name of the child element.
70      * @return The child element, or null if none was found.
71      */

72     public static Element JavaDoc getChildElementByTagName(Element JavaDoc parentElement, String JavaDoc name) {
73         NodeList JavaDoc nodes = parentElement.getChildNodes();
74         int length = nodes.getLength();
75         for (int index = 0; index < length; ++index) {
76             if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE
77                     && name.equals(nodes.item(index).getNodeName())) {
78                 return (Element JavaDoc) nodes.item(index);
79             }
80         }
81         return null;
82     }
83
84     /**
85      * Retrieves the first immediate child element of the specified element
86      * whose name matches the provided <code>name</code> parameter.
87      *
88      * @param parentElement The element to search.
89      * @param namespaceURI The namespace URI of the child element.
90      * @param localName The name of the child element.
91      * @return The child element, or null if none was found.
92      */

93     public static Element JavaDoc getChildElementByTagNameNS(Element JavaDoc parentElement, String JavaDoc namespaceURI, String JavaDoc localName) {
94         NodeList JavaDoc nodes = parentElement.getChildNodes();
95         int length = nodes.getLength();
96         for (int index = 0; index < length; ++index) {
97             if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE
98                     && namespaceURI.equals(nodes.item(index).getNamespaceURI())
99                     && localName.equals(nodes.item(index).getNodeName())) {
100                 return (Element JavaDoc) nodes.item(index);
101             }
102         }
103         return null;
104     }
105
106     /**
107      * Retrieves all immediate child elements of the specified element whose
108      * names match the provided <code>name</code> parameter.
109      *
110      * @param parentElement The element to search.
111      * @param name The name of the child element.
112      * @return An array of matching child elements.
113      */

114     public static Element JavaDoc[] getChildElementsByTagName(Element JavaDoc parentElement, String JavaDoc name) {
115         List JavaDoc children = new ArrayList JavaDoc();
116         NodeList JavaDoc nodes = parentElement.getChildNodes();
117         int length = nodes.getLength();
118         for (int index = 0; index < length; ++index) {
119             if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE
120                     && name.equals(nodes.item(index).getNodeName())) {
121                 children.add(nodes.item(index));
122             }
123         }
124         Element JavaDoc[] childElements = new Element JavaDoc[children.size()];
125         return (Element JavaDoc[]) children.toArray(childElements);
126     }
127
128     /**
129      * Retrieves all immediate child elements of the specified element whose
130      * names match the provided <code>name</code> parameter.
131      *
132      * @param parentElement The element to search.
133      * @param namespaceURI The namespace URI of the child element.
134      * @param localName The name of the child element.
135      * @return An array of matching child elements.
136      */

137     public static Element JavaDoc[] getChildElementsByTagNameNS(Element JavaDoc parentElement, String JavaDoc namespaceURI, String JavaDoc localName) {
138         List JavaDoc children = new ArrayList JavaDoc();
139         NodeList JavaDoc nodes = parentElement.getChildNodes();
140         int length = nodes.getLength();
141         for (int index = 0; index < length; ++index) {
142             if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE
143                     && namespaceURI.equals(nodes.item(index).getNamespaceURI())
144                     && localName.equals(nodes.item(index).getNodeName())) {
145                 children.add(nodes.item(index));
146             }
147         }
148         Element JavaDoc[] childElements = new Element JavaDoc[children.size()];
149         return (Element JavaDoc[]) children.toArray(childElements);
150     }
151
152     /**
153      * Counts the number of immediate child elements of the specified element
154      * whose names match the provided <code>name</code> parameter.
155      *
156      * @param parentElement The element to analyze.
157      * @param name The name of the child element.
158      * @return The number of matching child elements.
159      */

160     public static int getChildElementCountByTagName(Element JavaDoc parentElement, String JavaDoc name) {
161         NodeList JavaDoc nodes = parentElement.getChildNodes();
162         int length = nodes.getLength();
163         int count = 0;
164         for (int index = 0; index < length; ++index) {
165             if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE
166                     && name.equals(nodes.item(index).getNodeName())) {
167                 ++count;
168             }
169         }
170         return count;
171     }
172     
173     /**
174      * Counts the number of immediate child elements of the specified element
175      * whose names match the provided <code>name</code> parameter.
176      *
177      * @param parentElement The element to analyze.
178      * @param namespaceURI The namespace URI of the child element.
179      * @param localName The name of the child element.
180      * @return The number of matching child elements.
181      */

182     public static int getChildElementCountByTagNameNS(Element JavaDoc parentElement, String JavaDoc namespaceURI, String JavaDoc localName) {
183         NodeList JavaDoc nodes = parentElement.getChildNodes();
184         int length = nodes.getLength();
185         int count = 0;
186         for (int index = 0; index < length; ++index) {
187             if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE
188                     && namespaceURI.equals(nodes.item(index).getNamespaceURI())
189                     && localName.equals(nodes.item(index).getNodeName())) {
190                 ++count;
191             }
192         }
193         return count;
194     }
195     
196     /**
197      * Returns the text content of a DOM <code>Element</code>.
198      *
199      * @param element The <code>Element</code> to analyze.
200      */

201     public static String JavaDoc getElementText(Element JavaDoc element) {
202         NodeList JavaDoc children = element.getChildNodes();
203         int childCount = children.getLength();
204         for (int index = 0; index < childCount; ++index) {
205             if (children.item(index) instanceof Text JavaDoc) {
206                 Text JavaDoc text = (Text JavaDoc) children.item(index);
207                 return text.getData();
208             }
209         }
210         return null;
211     }
212     
213     /**
214      * Sets the text content of a DOM <code>Element</code>.
215      *
216      * @param element The <code>Element</code> to modify.
217      * @param value The new text value.
218      */

219     public static void setElementText(Element JavaDoc element, String JavaDoc value) {
220         NodeList JavaDoc children = element.getChildNodes();
221         int childCount = children.getLength();
222         for (int index = 0; index < childCount; ++index) {
223             if (children.item(index) instanceof Text JavaDoc) {
224                 Text JavaDoc text = (Text JavaDoc) children.item(index);
225                 text.setData(value);
226                 return;
227             }
228         }
229         Text JavaDoc text = element.getOwnerDocument().createTextNode(value);
230         element.appendChild(text);
231     }
232     
233     /** Non-instantiable class. */
234     private DomUtil() { }
235 }
236
Popular Tags