KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > util > xml > DomUtils


1 /*
2  * Copyright 2002-2006 the original author or authors.
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 org.springframework.util.xml;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.w3c.dom.CharacterData JavaDoc;
23 import org.w3c.dom.Comment JavaDoc;
24 import org.w3c.dom.Element JavaDoc;
25 import org.w3c.dom.EntityReference JavaDoc;
26 import org.w3c.dom.Node JavaDoc;
27 import org.w3c.dom.NodeList JavaDoc;
28
29 import org.springframework.util.Assert;
30
31 /**
32  * Convenience methods for working with the DOM API,
33  * in particular for working with DOM Nodes and DOM Elements.
34  *
35  * @author Juergen Hoeller
36  * @author Rob Harrop
37  * @author Costin Leau
38  * @since 1.2
39  * @see org.w3c.dom.Node
40  * @see org.w3c.dom.Element
41  */

42 public abstract class DomUtils {
43
44     /**
45      * Retrieve all child elements of the given DOM element that match
46      * the given element name. Only look at the direct child level of the
47      * given element; do not go into further depth (in contrast to the
48      * DOM API's <code>getElementsByTagName</code> method).
49      * @param ele the DOM element to analyze
50      * @param childEleName the child element name to look for
51      * @return a List of child <code>org.w3c.dom.Element</code> instances
52      * @see org.w3c.dom.Element
53      * @see org.w3c.dom.Element#getElementsByTagName
54      */

55     public static List JavaDoc getChildElementsByTagName(Element JavaDoc ele, String JavaDoc childEleName) {
56         NodeList JavaDoc nl = ele.getChildNodes();
57         List JavaDoc childEles = new ArrayList JavaDoc();
58         for (int i = 0; i < nl.getLength(); i++) {
59             Node JavaDoc node = nl.item(i);
60             if (node instanceof Element JavaDoc && nodeNameEquals(node, childEleName)) {
61                 childEles.add(node);
62             }
63         }
64         return childEles;
65     }
66
67     /**
68      * Utility method that returns the first child element
69      * identified by its name.
70      * @param ele the DOM element to analyze
71      * @param childEleName the child element name to look for
72      * @return the <code>org.w3c.dom.Element</code> instance,
73      * or <code>null</code> if none found
74      */

75     public static Element JavaDoc getChildElementByTagName(Element JavaDoc ele, String JavaDoc childEleName) {
76         NodeList JavaDoc nl = ele.getChildNodes();
77         for (int i = 0; i < nl.getLength(); i++) {
78             Node JavaDoc node = nl.item(i);
79             if (node instanceof Element JavaDoc && nodeNameEquals(node, childEleName)) {
80                 return (Element JavaDoc) node;
81             }
82         }
83         return null;
84     }
85
86     /**
87      * Utility method that returns the first child element value
88      * identified by its name.
89      * @param ele the DOM element to analyze
90      * @param childEleName the child element name to look for
91      * @return the extracted text value,
92      * or <code>null</code> if no child element found
93      */

94     public static String JavaDoc getChildElementValueByTagName(Element JavaDoc ele, String JavaDoc childEleName) {
95         Element JavaDoc child = getChildElementByTagName(ele, childEleName);
96         return (child != null ? getTextValue(child) : null);
97     }
98
99     /**
100      * Namespace-aware equals comparison. Returns <code>true</code> if either
101      * {@link Node#getLocalName} or {@link Node#getNodeName} equals <code>desiredName</code>,
102      * otherwise returns <code>false</code>.
103      */

104     public static boolean nodeNameEquals(Node JavaDoc node, String JavaDoc desiredName) {
105         Assert.notNull(node, "Node must not be null");
106         Assert.notNull(desiredName, "Desired name must not be null");
107         return desiredName.equals(node.getNodeName()) || desiredName.equals(node.getLocalName());
108     }
109
110     /**
111      * Extract the text value from the given DOM element, ignoring XML comments.
112      * <p>Appends all CharacterData nodes and EntityReference nodes
113      * into a single String value, excluding Comment nodes.
114      * @see CharacterData
115      * @see EntityReference
116      * @see Comment
117      */

118     public static String JavaDoc getTextValue(Element JavaDoc valueEle) {
119         StringBuffer JavaDoc value = new StringBuffer JavaDoc();
120         NodeList JavaDoc nl = valueEle.getChildNodes();
121         for (int i = 0; i < nl.getLength(); i++) {
122             Node JavaDoc item = nl.item(i);
123             if ((item instanceof CharacterData JavaDoc && !(item instanceof Comment JavaDoc)) ||
124                     item instanceof EntityReference JavaDoc) {
125                 value.append(item.getNodeValue());
126             }
127         }
128         return value.toString();
129     }
130
131 }
132
Popular Tags