KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > xhtml > DOMUtil


1 /***************************************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others. All rights reserved. This program and the
3  * accompanying materials are made available under the terms of the Eclipse Public License v1.0
4  * which accompanies this distribution, and is available at
5  * http://www.eclipse.org/legal/epl-v10.html
6  *
7  * Contributors: IBM Corporation - initial API and implementation
8  **************************************************************************************************/

9
10 package org.eclipse.help.internal.xhtml;
11
12 import java.util.Enumeration JavaDoc;
13 import java.util.Properties JavaDoc;
14 import java.util.Vector JavaDoc;
15
16 import org.w3c.dom.Document JavaDoc;
17 import org.w3c.dom.Element JavaDoc;
18 import org.w3c.dom.Node JavaDoc;
19 import org.w3c.dom.NodeList JavaDoc;
20
21
22 /**
23  * Util class for model. Has methods for resolving model attributes, and methods for manipulating
24  * XHTML DOM.
25  */

26 public class DOMUtil {
27
28     public static String JavaDoc TAG_BODY = "body"; //$NON-NLS-1$
29
private static String JavaDoc TAG_HEAD = "head"; //$NON-NLS-1$
30
public static String JavaDoc TAG_BASE = "base"; //$NON-NLS-1$
31
public static String JavaDoc TAG_DIV = "div"; //$NON-NLS-1$
32
public static String JavaDoc TAG_HEAD_LINK = "link"; //$NON-NLS-1$
33
public static String JavaDoc TAG_FILTER = "filter"; //$NON-NLS-1$
34
public static String JavaDoc ATT_HREF = "href"; //$NON-NLS-1$
35
public static String JavaDoc ATT_REL = "rel"; //$NON-NLS-1$
36
public static String JavaDoc ATT_TYPE = "type"; //$NON-NLS-1$
37
public static String JavaDoc ATT_FILTER = "filter"; //$NON-NLS-1$
38

39
40
41
42     public static void insertBase(Document JavaDoc dom, String JavaDoc baseURL) {
43         // there should only be one head and one base element dom.
44
NodeList JavaDoc headList = dom.getElementsByTagName(TAG_HEAD);
45         Element JavaDoc head = (Element JavaDoc) headList.item(0);
46         NodeList JavaDoc baseList = head.getElementsByTagName(TAG_BASE);
47         if (baseList.getLength() == 0) {
48             // insert a base element, since one is not defined already.
49
Element JavaDoc base = dom.createElement(TAG_BASE);
50             base.setAttribute(ATT_HREF, baseURL);
51             head.insertBefore(base, head.getFirstChild());
52         }
53     }
54
55
56     public static Element JavaDoc getBase(Document JavaDoc dom) {
57         // there should only be one head and one base element dom.
58
NodeList JavaDoc headList = dom.getElementsByTagName(TAG_HEAD);
59         Element JavaDoc head = (Element JavaDoc) headList.item(0);
60         NodeList JavaDoc baseList = head.getElementsByTagName(TAG_BASE);
61         if (baseList.getLength() == 0)
62             // no base defined, signal failure.
63
return null;
64
65         return (Element JavaDoc) baseList.item(baseList.getLength() - 1);
66
67     }
68
69     public static void insertStyle(Document JavaDoc dom, String JavaDoc cssUrl) {
70         // there should only be one head and one base element dom.
71
NodeList JavaDoc headList = dom.getElementsByTagName(TAG_HEAD);
72         Element JavaDoc head = null;
73         // Element base = getBase(dom);
74
NodeList JavaDoc styleList = null;
75         // there can be more than one style. DO not add style if it exists.
76
if (headList.getLength() >= 1) {
77             head = (Element JavaDoc) headList.item(0);
78             styleList = head.getElementsByTagName(TAG_HEAD_LINK);
79             for (int i = 0; i < styleList.getLength(); i++) {
80                 Element JavaDoc style = (Element JavaDoc) styleList.item(0);
81                 String JavaDoc styleString = style.getAttribute(ATT_HREF);
82                 if (styleString.equals(cssUrl))
83                     return;
84             }
85         }
86
87         // insert the style, since it is not defined.
88
Element JavaDoc styleToAdd = dom.createElement(TAG_HEAD_LINK);
89         styleToAdd.setAttribute(ATT_HREF, cssUrl);
90         styleToAdd.setAttribute(ATT_REL, "stylesheet"); //$NON-NLS-1$
91
styleToAdd.setAttribute(ATT_TYPE, "text/css"); //$NON-NLS-1$
92
if (styleList != null && styleList.getLength() >= 1)
93             styleList.item(0).getParentNode().insertBefore(styleToAdd, styleList.item(0));
94         else
95             head.appendChild(styleToAdd);
96
97     }
98
99     /**
100      * Returns a reference to the body of the DOM.
101      *
102      * @param dom
103      * @return
104      */

105     public static Element JavaDoc getBodyElement(Document JavaDoc dom) {
106         // there should only be one body element dom.
107
NodeList JavaDoc bodyList = dom.getElementsByTagName(TAG_BODY);
108         Element JavaDoc body = (Element JavaDoc) bodyList.item(0);
109         return body;
110     }
111
112
113
114     public static Element JavaDoc createElement(Document JavaDoc dom, String JavaDoc elementName, Properties JavaDoc attributes) {
115
116         // make sure to create element with any namespace uri to enable finding
117
// it again using Dom.getElementsByTagNameNS()
118
Element JavaDoc element = dom.createElementNS("", elementName); //$NON-NLS-1$
119
if (attributes != null) {
120             Enumeration JavaDoc e = attributes.keys();
121             while (e.hasMoreElements()) {
122                 String JavaDoc key = (String JavaDoc) e.nextElement();
123                 element.setAttribute(key, attributes.getProperty(key));
124             }
125         }
126         return element;
127     }
128
129     public static Element JavaDoc createAndAppendChild(Element JavaDoc parentElement, String JavaDoc elementName,
130             Properties JavaDoc attributes) {
131
132         Element JavaDoc element = createElement(parentElement.getOwnerDocument(), elementName, attributes);
133         parentElement.appendChild(element);
134         return element;
135     }
136
137
138
139     /**
140      * Returns an Element array of all first level descendant Elements with a given tag name, in the
141      * order in which they are encountered in the DOM. Unlike the JAXP apis, which returns preorder
142      * traversal of this Element tree, this method filters out children deeper than first level
143      * child nodes.
144      */

145     public static Element JavaDoc[] getElementsByTagName(Element JavaDoc parent, String JavaDoc tagName) {
146         NodeList JavaDoc allChildElements = parent.getElementsByTagName(tagName);
147         Vector JavaDoc vector = new Vector JavaDoc();
148         for (int i = 0; i < allChildElements.getLength(); i++) {
149             // we know that the nodelist is of elements.
150
Element JavaDoc aElement = (Element JavaDoc) allChildElements.item(i);
151             if (aElement.getParentNode().equals(parent))
152                 // first level child element. add it.
153
vector.add(aElement);
154         }
155         Element JavaDoc[] filteredElements = new Element JavaDoc[vector.size()];
156         vector.copyInto(filteredElements);
157         return filteredElements;
158     }
159
160     /**
161      * Same as getElementsByTagName(Element parent, String tagName) but the parent element is
162      * assumed to be the root of the document.
163      *
164      * @see getElementsByTagName(Element parent, String tagName)
165      */

166     public static Element JavaDoc[] getElementsByTagName(Document JavaDoc dom, String JavaDoc tagName) {
167         NodeList JavaDoc allChildElements = dom.getElementsByTagName(tagName);
168         Vector JavaDoc vector = new Vector JavaDoc();
169         for (int i = 0; i < allChildElements.getLength(); i++) {
170             // we know that the nodelist is of elements.
171
Element JavaDoc aElement = (Element JavaDoc) allChildElements.item(i);
172             if (aElement.getParentNode().equals(dom.getDocumentElement()))
173                 // first level child element. add it. Cant use getParent
174
// here.
175
vector.add(aElement);
176         }
177         Element JavaDoc[] filteredElements = new Element JavaDoc[vector.size()];
178         vector.copyInto(filteredElements);
179         return filteredElements;
180     }
181
182
183     /*
184      * Util method similar to DOM getElementById() method, but it works without an id attribute
185      * being specified. Deep searches all children in this container's DOM for the first child with
186      * the given id. The element retrieved must have the passed local name. Note that in an XHTML
187      * file (aka DOM) elements should have a unique id within the scope of a document. We use local
188      * name because this allows for finding anchors and includes regardless of whether or not an
189      * xmlns was used in the xml.
190      */

191     public static Element JavaDoc getElementById(Document JavaDoc dom, String JavaDoc id, String JavaDoc localElementName) {
192
193         NodeList JavaDoc children = dom.getElementsByTagNameNS("*", localElementName); //$NON-NLS-1$
194
for (int i = 0; i < children.getLength(); i++) {
195             Element JavaDoc element = (Element JavaDoc) children.item(i);
196             if (element.getAttribute("id").equals(id)) //$NON-NLS-1$
197
return element;
198         }
199         // non found.
200
return null;
201
202     }
203
204
205
206
207     /**
208      * Returns an array version of the passed NodeList. Used to work around DOM design issues.
209      */

210     public static Node JavaDoc[] getArray(NodeList JavaDoc nodeList) {
211         Node JavaDoc[] nodes = new Node JavaDoc[nodeList.getLength()];
212         for (int i = 0; i < nodeList.getLength(); i++)
213             nodes[i] = nodeList.item(i);
214         return nodes;
215     }
216
217
218     /**
219      * Remove all instances of the element from the DOM.
220      *
221      */

222     public static void removeAllElements(Document JavaDoc dom, String JavaDoc elementLocalName) {
223         // get all elements in DOM and remove them.
224
NodeList JavaDoc elements = dom.getElementsByTagNameNS("*", //$NON-NLS-1$
225
elementLocalName);
226         // get the array version of the nodelist to work around DOM api design.
227
Node JavaDoc[] elementsArray = DOMUtil.getArray(elements);
228         for (int i = 0; i < elementsArray.length; i++) {
229             Node JavaDoc element = elementsArray[i];
230             element.getParentNode().removeChild(element);
231         }
232
233     }
234
235
236
237 }
238
Popular Tags