01: /* Nodes.java
02:
03: {{IS_NOTE
04:
05: Purpose:
06: Description:
07: History:
08: 2001/09/27 17:15:27, Create, Tom M. Yeh.
09: }}IS_NOTE
10:
11: Copyright (C) 2001 Potix Corporation. All Rights Reserved.
12:
13: {{IS_RIGHT
14: This program is distributed under GPL Version 2.0 in the hope that
15: it will be useful, but WITHOUT ANY WARRANTY.
16: }}IS_RIGHT
17: */
18: package org.zkoss.xml;
19:
20: import java.util.Collections;
21: import org.w3c.dom.Node;
22: import org.w3c.dom.NodeList;
23:
24: import org.zkoss.idom.Item;
25:
26: /**
27: * Node related utilities.
28: * It supports iDOM.
29: *
30: * @author tomyeh
31: */
32: public class Nodes {
33: /** The empty node list. */
34: public static final NodeList EMPTY_NODELIST = new FacadeNodeList(
35: Collections.EMPTY_LIST);
36:
37: /**
38: * Get the text value of a node.
39: *
40: * <p>If the node is <i>not</i> an element, Node.getNodeValue is called.
41: * If the node is an element, the returned string is a catenation of
42: * all values of TEXT_NODE and CDATA_SECTION_NODE.
43: *
44: * <p>Textual nodes include Text, CDATA and Binary (iDOM's extension).
45: */
46: public static final String valueOf(Node node) {
47: if (node instanceof Item) {
48: String v = ((Item) node).getText();
49: return v != null ? v : "";
50: }
51:
52: if (node.getNodeType() == Node.ELEMENT_NODE) {
53: StringBuffer sb = new StringBuffer();
54: NodeList nl = node.getChildNodes();
55: Node child;
56: for (int j = 0; (child = nl.item(j)) != null; ++j) {
57: int type = child.getNodeType();
58: if (type == Node.TEXT_NODE
59: || type == Node.CDATA_SECTION_NODE)
60: sb.append(valueOf(child));
61: }
62: return sb.toString();
63: }
64: String v = node.getNodeValue();
65: return v != null ? v : "";
66: }
67: }
|