KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > util > XmlUtil


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.util;
4
5 import org.w3c.dom.NamedNodeMap JavaDoc;
6 import org.w3c.dom.Node JavaDoc;
7 import org.w3c.dom.NodeList JavaDoc;
8 import org.w3c.dom.Element JavaDoc;
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Map JavaDoc;
14
15 /**
16  * Some XML and XPath utilities.
17  */

18 public class XmlUtil {
19
20
21     // ---------------------------------------------------------------- attributes
22

23     /**
24      * Returns a map of all node's attributes. All non-attribute nodes are ignored.
25      */

26     public static Map JavaDoc getAllAttributes(Node JavaDoc node) {
27         HashMap JavaDoc attrs = new HashMap JavaDoc();
28         NamedNodeMap JavaDoc nmm = node.getAttributes();
29         for (int j = 0; j < nmm.getLength(); j++) {
30             Node JavaDoc attribute = nmm.item(j);
31             if (attribute.getNodeType() != Node.ATTRIBUTE_NODE) {
32                 continue;
33             }
34             attrs.put(attribute.getNodeName(), attribute.getNodeValue());
35         }
36         return attrs;
37     }
38
39     /**
40      * Returns attribute value of a node or <code>null</code> if attribute name not found.
41      * Specified attribute is searched on every call.
42      * Consider {@link #getAllAttributes(org.w3c.dom.Node)} for better performances.
43      */

44     public static String JavaDoc getAttributeValue(Node JavaDoc node, String JavaDoc attrName) {
45         NamedNodeMap JavaDoc nmm = node.getAttributes();
46         for (int j = 0; j < nmm.getLength(); j++) {
47             Node JavaDoc attribute = nmm.item(j);
48             if (attribute.getNodeType() != Node.ATTRIBUTE_NODE) {
49                 continue;
50             }
51             String JavaDoc nodeName = attribute.getNodeName();
52             if (nodeName.equals(attrName)) {
53                 return attribute.getNodeValue();
54             }
55         }
56         return null;
57     }
58
59     /**
60      * Get element's attribute value or <code>null</code> if attribute not found or empty.
61      */

62     public static String JavaDoc getAttributeValue(Element JavaDoc element, String JavaDoc name) {
63         String JavaDoc value = element.getAttribute(name);
64         if (value.length() == 0) {
65             value = null;
66         }
67         return value;
68     }
69
70
71     // ---------------------------------------------------------------- nodelist
72

73     /**
74      * Filters node list by keeping nodes of specified type.
75      */

76     public static List JavaDoc filterNodeList(NodeList JavaDoc nodeList, short keepNodeType) {
77         return filterNodeList(nodeList, keepNodeType, null);
78     }
79
80     /**
81      * Filters node list by keeping nodes of specified type and node name.
82      */

83     public static List JavaDoc filterNodeList(NodeList JavaDoc nodeList, short keepNodeType, String JavaDoc nodeName) {
84         List JavaDoc nodes = new ArrayList JavaDoc();
85         for (int k = 0; k < nodeList.getLength(); k++) {
86             Node JavaDoc node = nodeList.item(k);
87             if (node.getNodeType() != keepNodeType) {
88                 continue;
89             }
90             if (nodeName != null && (node.getNodeName().equals(nodeName) == false)) {
91                 continue;
92             }
93             nodes.add(node);
94         }
95         return nodes;
96     }
97
98     /**
99      * Filter node list for all Element nodes.
100      */

101     public static List JavaDoc filterNodeListElements(NodeList JavaDoc nodeList) {
102         return filterNodeListElements(nodeList, null);
103     }
104
105     /**
106      * Filter node list for Element nodes of specified name.
107      */

108     public static List JavaDoc filterNodeListElements(NodeList JavaDoc nodeList, String JavaDoc nodeName) {
109         List JavaDoc nodes = new ArrayList JavaDoc();
110         for (int k = 0; k < nodeList.getLength(); k++) {
111             Node JavaDoc node = nodeList.item(k);
112             if (node.getNodeType() != Node.ELEMENT_NODE) {
113                 continue;
114             }
115             if (nodeName != null && (node.getNodeName().equals(nodeName) == false)) {
116                 continue;
117             }
118             nodes.add(node);
119         }
120         return nodes;
121     }
122
123
124     /**
125      * Returns a list of all child Elements,
126      */

127     public static List JavaDoc getChildElements(Node JavaDoc node) {
128         return getChildElements(node, null);
129     }
130
131
132     /**
133      * Returns a list of child Elements of specified name.
134      */

135     public static List JavaDoc getChildElements(Node JavaDoc node, String JavaDoc nodeName) {
136         NodeList JavaDoc childs = node.getChildNodes();
137         return filterNodeListElements(childs, nodeName);
138     }
139
140     // ---------------------------------------------------------------- node
141

142
143     /**
144      * Returns value of first availiable child text node or <code>null</code> if not found.
145      */

146     public static String JavaDoc getFirstChildTextNodeValue(Node JavaDoc node) {
147         NodeList JavaDoc children = node.getChildNodes();
148         int len = children.getLength();
149         for (int i = 0; i < len; i++) {
150             Node JavaDoc n = children.item(i);
151             if (n.getNodeType() == Node.TEXT_NODE) {
152                 return n.getNodeValue();
153             }
154         }
155         return null;
156     }
157
158     /**
159      * Returns value of single child text node or <code>null</code>.
160      */

161     public static String JavaDoc getChildTextNodeValue(Node JavaDoc node) {
162         if (node.getChildNodes().getLength() != 1) {
163             return null;
164         }
165         Node JavaDoc item0 = node.getChildNodes().item(0);
166         if (item0.getNodeType() != Node.TEXT_NODE) {
167             return null;
168         }
169         return item0.getNodeValue();
170     }
171
172
173
174 }
175
Popular Tags