KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > utils > DomUtils


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.utils;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Random JavaDoc;
19
20 import org.w3c.dom.Document JavaDoc;
21 import org.w3c.dom.Element JavaDoc;
22 import org.w3c.dom.Node JavaDoc;
23 import org.w3c.dom.NodeList JavaDoc;
24 import org.w3c.dom.Text JavaDoc;
25
26 public class DomUtils {
27
28   /**
29    * returns Vector of all Child elements, specified by tag name.
30    * same as Element.getElementsByTagName, but not recursive.
31    */

32   public static List JavaDoc getChildElemsByTagName(Element JavaDoc parent, String JavaDoc tagName) {
33     ArrayList JavaDoc retVec = new ArrayList JavaDoc();
34
35     NodeList JavaDoc children = parent.getChildNodes();
36     for (int i = 0; i < children.getLength(); ++i) {
37       if (children.item(i).getNodeType() == Node.ELEMENT_NODE) {
38         Element JavaDoc child = (Element JavaDoc) children.item(i);
39         if (child.getTagName().equals(tagName))
40           retVec.add(child);
41       }
42     }
43     return retVec;
44   }
45
46   /**
47    * returns List of all direct child elements
48    */

49   public static List JavaDoc getChildElements(Node JavaDoc parent) {
50     ArrayList JavaDoc retVec = new ArrayList JavaDoc();
51
52     NodeList JavaDoc children = parent.getChildNodes();
53     for (int i = 0; i < children.getLength(); ++i) {
54       if (children.item(i).getNodeType() == Node.ELEMENT_NODE) {
55         retVec.add(children.item(i));
56       }
57     }
58     return retVec;
59   }
60
61   /**
62    * returns List of all direct child elements exept Attribute nodes.
63    * Among others this will contain Element nodes and Text children.
64    */

65   public static List JavaDoc getChildNodesExceptAttributes(Node JavaDoc parent) {
66     ArrayList JavaDoc retVec = new ArrayList JavaDoc();
67     NodeList JavaDoc children = parent.getChildNodes();
68     for (int i = 0; i < children.getLength(); ++i) {
69       if (children.item(i).getNodeType() == Node.TEXT_NODE
70           || children.item(i).getNodeType() == Node.ELEMENT_NODE) {
71         retVec.add(children.item(i));
72       }
73     }
74     return retVec;
75   }
76
77   /**
78    * removes all children of element type
79    */

80   public static void removeChildElements(Element JavaDoc parent) {
81     List JavaDoc v = getChildElements(parent);
82     Iterator JavaDoc en = v.iterator();
83     while (en.hasNext())
84       parent.removeChild((Node JavaDoc) en.next());
85   }
86
87   /**
88    * removes all children of element ant text type
89    */

90   public static void removeChildNodesExceptAttributes(Element JavaDoc parent) {
91     List JavaDoc v = getChildNodesExceptAttributes(parent);
92     Iterator JavaDoc en = v.iterator();
93     while (en.hasNext())
94       parent.removeChild((Node JavaDoc) en.next());
95   }
96
97   /** removes all element/text children, then adds a new single text child */
98   public static Text JavaDoc setText(Element JavaDoc parent, String JavaDoc text) {
99     removeChildNodesExceptAttributes(parent);
100     Document JavaDoc doc = parent.getOwnerDocument();
101     Text JavaDoc child = doc.createTextNode(text);
102     parent.appendChild(child);
103     return child;
104   }
105
106   /**
107    * returns first child element with fitting tag name.
108    */

109   public static Element JavaDoc getChildElemByTagName(Element JavaDoc parent, String JavaDoc tagName) {
110     NodeList JavaDoc children = parent.getChildNodes();
111     for (int i = 0; i < children.getLength(); ++i) {
112       if (children.item(i).getNodeType() == Node.ELEMENT_NODE) {
113         Element JavaDoc child = (Element JavaDoc) children.item(i);
114         if (child.getTagName().equals(tagName))
115           return child;
116       }
117     }
118     return null;
119   }
120
121   /**
122    * returns first child element with fitting id.
123    */

124   public static Element JavaDoc getChildElemById(Element JavaDoc parent, String JavaDoc id) {
125     NodeList JavaDoc children = parent.getChildNodes();
126     for (int i = 0; i < children.getLength(); ++i) {
127       if (children.item(i).getNodeType() == Node.ELEMENT_NODE) {
128         Element JavaDoc child = (Element JavaDoc) children.item(i);
129         if (XoplonNS.getAttribute(child, "id").equals(id))
130           return child;
131       }
132     }
133     return null;
134   }
135
136   /**
137    * returns the Document. node itself may be a Document node in which case
138    * node.getOwnerDocument() will return null
139    */

140   public static Document JavaDoc getDocument(Node JavaDoc node) {
141     if (node.getNodeType() == Node.DOCUMENT_NODE)
142       return (Document JavaDoc) node;
143     return node.getOwnerDocument();
144   }
145
146   /** creates a new element of type and appends it to parent */
147   public static Element JavaDoc appendElement(Element JavaDoc parent, String JavaDoc type) {
148     Document JavaDoc doc = parent.getOwnerDocument();
149     Element JavaDoc elem = doc.createElement(type);
150     parent.appendChild(elem);
151     return elem;
152   }
153
154   public static Text JavaDoc appendNbsp(Element JavaDoc parent) {
155     return appendText(parent, "\u00a0");
156   }
157
158   public static Text JavaDoc appendText(Element JavaDoc parent, String JavaDoc text) {
159     Document JavaDoc doc = parent.getOwnerDocument();
160     Text JavaDoc x = doc.createTextNode(text);
161     parent.appendChild(x);
162     return x;
163   }
164
165   static final Random JavaDoc random = new Random JavaDoc();
166
167   public static synchronized String JavaDoc randomId() {
168     return "wcf" + Integer.toHexString(random.nextInt());
169   }
170
171   public static synchronized void setRandomSeed(long seed) {
172     random.setSeed(seed);
173   }
174
175   /**
176    * adds an attribute with random value to all elements, that do not
177    * have an attribute with the specified name.
178    * @param root the root element
179    * @param attrName the name of the attribute, e.g. "id"
180    */

181   public static void generateIds(Node JavaDoc root, String JavaDoc attrName) {
182     if (root.getNodeType() == Node.ELEMENT_NODE) {
183       Element JavaDoc e = (Element JavaDoc) root;
184       String JavaDoc id = e.getAttribute(attrName);
185       if (id == null || id.length() == 0)
186         e.setAttribute(attrName, randomId());
187     }
188     NodeList JavaDoc list = root.getChildNodes();
189     int N = list.getLength();
190     for (int i = 0; i < N; i++)
191       generateIds(list.item(i), attrName);
192   }
193
194   public static void generateIds(Node JavaDoc root) {
195     generateIds(root, "id");
196   }
197
198   /**
199    * fast search for Element with id attribute
200    * @param root the root Element of the search. search will cover root and
201    * its descendants
202    * @param id the id to search for
203    * @return null or the element
204    */

205   public static Element JavaDoc findElementWithId(String JavaDoc id, Element JavaDoc root) {
206     if (id.equals(root.getAttribute("id")))
207       return root;
208     NodeList JavaDoc list = root.getChildNodes();
209     int len = list.getLength();
210     for (int i = 0; i < len; i++) {
211       Node JavaDoc n = list.item(i);
212       if (n.getNodeType() != Node.ELEMENT_NODE)
213         continue;
214       Element JavaDoc child = (Element JavaDoc) list.item(i);
215       Element JavaDoc found = findElementWithId(id, child);
216       if (found != null)
217         return found;
218     }
219     return null;
220   }
221
222   /**
223    * Oracle 10i does not allow to remove an attribute that does
224    * not exist. This method will silently ignore any exceptions
225    * that occur while removing the attribute.
226    */

227   public static void removeAttribute(Element JavaDoc elem, String JavaDoc name) {
228     try {
229       elem.removeAttribute(name);
230     } catch (Exception JavaDoc e) {
231       // ignore
232
}
233   }
234 }
235
Popular Tags