KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > webrender > 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.webrender.util;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.List JavaDoc;
34
35 import javax.xml.parsers.DocumentBuilder JavaDoc;
36 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
37 import javax.xml.parsers.ParserConfigurationException JavaDoc;
38 import javax.xml.transform.TransformerFactory JavaDoc;
39
40 import org.w3c.dom.Element JavaDoc;
41 import org.w3c.dom.Node JavaDoc;
42 import org.w3c.dom.NodeList JavaDoc;
43 import org.w3c.dom.Text JavaDoc;
44
45 /**
46  * A utility class which provides methods for working with a W3C DOM.
47  */

48 public class DomUtil {
49
50     /**
51      * ThreadLocal cache of <code>DocumentBuilder</code> instances.
52      */

53     private static final ThreadLocal JavaDoc documentBuilders = new ThreadLocal JavaDoc() {
54     
55         /**
56          * @see java.lang.ThreadLocal#initialValue()
57          */

58         protected Object JavaDoc initialValue() {
59             try {
60                 DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
61                 factory.setNamespaceAware(true);
62                 DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
63                 return builder;
64             } catch (ParserConfigurationException JavaDoc ex) {
65                 throw new RuntimeException JavaDoc(ex);
66             }
67         }
68     };
69     
70     /**
71      * ThreadLocal cache of <code>TransformerFactory</code> instances.
72      */

73     private static final ThreadLocal JavaDoc transformerFactories = new ThreadLocal JavaDoc() {
74     
75         /**
76          * @see java.lang.ThreadLocal#initialValue()
77          */

78         protected Object JavaDoc initialValue() {
79             TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
80             return factory;
81         }
82     };
83     
84     /**
85      * Retrieves a thread-specific <code>DocumentBuilder</code>.
86      *
87      * @return the <code>DocumentBuilder</code> serving the current thread.
88      */

89     public static DocumentBuilder JavaDoc getDocumentBuilder() {
90         return (DocumentBuilder JavaDoc) documentBuilders.get();
91     }
92     
93     /**
94      * Retrieves a thread-specific <code>TransformerFactory</code>.
95      *
96      * @return the <code>TransformerFactory</code> serving the current thread.
97      */

98     public static TransformerFactory JavaDoc getTransformerFactory() {
99         return (TransformerFactory JavaDoc) transformerFactories.get();
100     }
101     
102     /**
103      * Determines whether a specific boolean flag is set on an element.
104      *
105      * @param element The element to analyze.
106      * @param attributeName The name of the boolean 'flag' attribute.
107      * @return True if the value of the attribute is 'true', false if it is
108      * not or if the attribute does not exist.
109      */

110     public static boolean getBooleanAttribute(Element JavaDoc element, String JavaDoc attributeName) {
111         String JavaDoc value = element.getAttribute(attributeName);
112         if (value == null) {
113             return false;
114         } else if (value.equals("true")) {
115             return true;
116         } else {
117             return false;
118         }
119     }
120
121     /**
122      * Retrieves the first immediate child element of the specified element
123      * whose name matches the provided <code>name</code> parameter.
124      *
125      * @param parentElement The element to search.
126      * @param name The name of the child element.
127      * @return The child element, or null if none was found.
128      */

129     public static Element JavaDoc getChildElementByTagName(Element JavaDoc parentElement, String JavaDoc name) {
130         NodeList JavaDoc nodes = parentElement.getChildNodes();
131         int length = nodes.getLength();
132         for (int index = 0; index < length; ++index) {
133             if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE
134                     && name.equals(nodes.item(index).getNodeName())) {
135                 return (Element JavaDoc) nodes.item(index);
136             }
137         }
138         return null;
139     }
140
141     /**
142      * Retrieves the first immediate child element of the specified element
143      * whose name matches the provided <code>name</code> parameter.
144      *
145      * @param parentElement The element to search.
146      * @param namespaceURI The namespace URI of the child element.
147      * @param localName The name of the child element.
148      * @return The child element, or null if none was found.
149      */

150     public static Element JavaDoc getChildElementByTagNameNS(Element JavaDoc parentElement, String JavaDoc namespaceURI, String JavaDoc localName) {
151         NodeList JavaDoc nodes = parentElement.getChildNodes();
152         int length = nodes.getLength();
153         for (int index = 0; index < length; ++index) {
154             if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE
155                     && namespaceURI.equals(nodes.item(index).getNamespaceURI())
156                     && localName.equals(nodes.item(index).getNodeName())) {
157                 return (Element JavaDoc) nodes.item(index);
158             }
159         }
160         return null;
161     }
162
163     /**
164      * Retrieves all immediate child elements of the specified element whose
165      * names match the provided <code>name</code> parameter.
166      *
167      * @param parentElement The element to search.
168      * @param name The name of the child element.
169      * @return An array of matching child elements.
170      */

171     public static Element JavaDoc[] getChildElementsByTagName(Element JavaDoc parentElement, String JavaDoc name) {
172         List JavaDoc children = new ArrayList JavaDoc();
173         NodeList JavaDoc nodes = parentElement.getChildNodes();
174         int length = nodes.getLength();
175         for (int index = 0; index < length; ++index) {
176             if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE
177                     && name.equals(nodes.item(index).getNodeName())) {
178                 children.add(nodes.item(index));
179             }
180         }
181         Element JavaDoc[] childElements = new Element JavaDoc[children.size()];
182         return (Element JavaDoc[]) children.toArray(childElements);
183     }
184
185     /**
186      * Retrieves all immediate child elements of the specified element whose
187      * names match the provided <code>name</code> parameter.
188      *
189      * @param parentElement The element to search.
190      * @param namespaceURI The namespace URI of the child element.
191      * @param localName The name of the child element.
192      * @return An array of matching child elements.
193      */

194     public static Element JavaDoc[] getChildElementsByTagNameNS(Element JavaDoc parentElement, String JavaDoc namespaceURI, String JavaDoc localName) {
195         List JavaDoc children = new ArrayList JavaDoc();
196         NodeList JavaDoc nodes = parentElement.getChildNodes();
197         int length = nodes.getLength();
198         for (int index = 0; index < length; ++index) {
199             if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE
200                     && namespaceURI.equals(nodes.item(index).getNamespaceURI())
201                     && localName.equals(nodes.item(index).getNodeName())) {
202                 children.add(nodes.item(index));
203             }
204         }
205         Element JavaDoc[] childElements = new Element JavaDoc[children.size()];
206         return (Element JavaDoc[]) children.toArray(childElements);
207     }
208
209     /**
210      * Counts the number of immediate child elements of the specified element
211      * whose names match the provided <code>name</code> parameter.
212      *
213      * @param parentElement The element to analyze.
214      * @param name The name of the child element.
215      * @return The number of matching child elements.
216      */

217     public static int getChildElementCountByTagName(Element JavaDoc parentElement, String JavaDoc name) {
218         NodeList JavaDoc nodes = parentElement.getChildNodes();
219         int length = nodes.getLength();
220         int count = 0;
221         for (int index = 0; index < length; ++index) {
222             if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE
223                     && name.equals(nodes.item(index).getNodeName())) {
224                 ++count;
225             }
226         }
227         return count;
228     }
229     
230     /**
231      * Counts the number of immediate child elements of the specified element
232      * whose names match the provided <code>name</code> parameter.
233      *
234      * @param parentElement The element to analyze.
235      * @param namespaceURI The namespace URI of the child element.
236      * @param localName The name of the child element.
237      * @return The number of matching child elements.
238      */

239     public static int getChildElementCountByTagNameNS(Element JavaDoc parentElement, String JavaDoc namespaceURI, String JavaDoc localName) {
240         NodeList JavaDoc nodes = parentElement.getChildNodes();
241         int length = nodes.getLength();
242         int count = 0;
243         for (int index = 0; index < length; ++index) {
244             if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE
245                     && namespaceURI.equals(nodes.item(index).getNamespaceURI())
246                     && localName.equals(nodes.item(index).getNodeName())) {
247                 ++count;
248             }
249         }
250         return count;
251     }
252     
253     /**
254      * Returns the text content of a DOM <code>Element</code>.
255      *
256      * @param element The <code>Element</code> to analyze.
257      */

258     public static String JavaDoc getElementText(Element JavaDoc element) {
259         NodeList JavaDoc children = element.getChildNodes();
260         int childCount = children.getLength();
261         for (int index = 0; index < childCount; ++index) {
262             if (children.item(index) instanceof Text JavaDoc) {
263                 Text JavaDoc text = (Text JavaDoc) children.item(index);
264                 return text.getData();
265             }
266         }
267         return null;
268     }
269     
270     /**
271      * Sets the text content of a DOM <code>Element</code>.
272      *
273      * @param element The <code>Element</code> to modify.
274      * @param value The new text value.
275      */

276     public static void setElementText(Element JavaDoc element, String JavaDoc value) {
277         NodeList JavaDoc children = element.getChildNodes();
278         int childCount = children.getLength();
279         for (int index = 0; index < childCount; ++index) {
280             if (children.item(index) instanceof Text JavaDoc) {
281                 Text JavaDoc text = (Text JavaDoc) children.item(index);
282                 text.setData(value);
283                 return;
284             }
285         }
286         Text JavaDoc text = element.getOwnerDocument().createTextNode(value);
287         element.appendChild(text);
288     }
289     
290     /** Non-instantiable class. */
291     private DomUtil() { }
292 }
293
Popular Tags