View Javadoc
1 /*
2 * Copyright (c) 2003 Scott Howlett & Paul Libbrecht.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. The end-user documentation included with the redistribution,
18 * if any, must include the following acknowledgment:
19 * "This product includes software developed by the Xwing
20 * Project ( http://xwing.sourceforge.net/ )."
21 * Alternately, this acknowledgment may appear in the software itself,
22 * if and wherever such third-party acknowledgments normally appear.
23 *
24 * 4. The name "Xwing" must not be used to endorse or promote products
25 * derived from this software without prior written permission. For
26 * written permission, please contact the project authors via
27 * the Xwing project site, http://xwing.sourceforge.net/ .
28 *
29 * 5. Products derived from this software may not be called "Xwing",
30 * nor may "Xwing" appear in their name, without prior written
31 * permission of the Xwing project.
32 *
33 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36 * DISCLAIMED. IN NO EVENT SHALL THE XWING AUTHORS OR THE PROJECT
37 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
40 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
43 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 * SUCH DAMAGE.
45 *
46 * For more information on Xwing, please see
47 * < http://xwing.sourceforge.net/ >.
48 */
49
50 package net.sourceforge.xwing.dom;
51
52 import org.w3c.dom.CharacterData;
53 import org.w3c.dom.Attr;
54 import org.w3c.dom.Element;
55 import org.w3c.dom.Node;
56 import org.w3c.dom.NodeList;
57 import org.w3c.dom.Text;
58
59 public class NodeTextUtils {
60
61 private NodeTextUtils() {
62 }
63
64 /***
65 * Sets the text of the given node.The exact
66 * behavior depends on the type of node passed in.
67 *
68 * If the node is an Element, its first child Text node is set.
69 * If there is no child text node, a new one containing the specified
70 * text is appended to the element. If the specified text is null,
71 * the first Text child (if any) is removed.
72 *
73 * If the node is an Attr, its value is set. If the specified text
74 * is null, the attr is removed from its owning element.
75 *
76 * If the node is any CharacterData type, its value is set.
77 * If the specified text is null, the node is removed from its parent.
78 *
79 * No action is taken for any other type of Node.
80 *
81 * @param node The node whose text should be set.
82 * @param text The text to set.
83 */
84 public static void setText(Node node, String text) {
85 if (node == null) {
86 throw new NullPointerException("node must not be null");
87 }
88
89 if (node instanceof Element) {
90 Node textKid = getFirstTextChild(node);
91 if (textKid == null) {
92 if (text != null) {
93 node.appendChild(
94 node.getOwnerDocument().createTextNode(text));
95 }
96 } else if (text == null) {
97 node.removeChild(textKid);
98 } else if (!text.equals(textKid.getNodeValue())) {
99 textKid.setNodeValue(text);
100 }
101 } else if (node instanceof Attr) {
102
103 Attr attr = (Attr) node;
104 if (text == null) {
105 Element elem = attr.getOwnerElement();
106 if (elem == null) {
107 elem.removeAttributeNode(attr);
108 }
109 } else {
110 attr.setNodeValue(text);
111 }
112 } else if (node instanceof CharacterData) {
113 if (text == null) {
114 Node parent = node.getParentNode();
115 if (parent != null) {
116 parent.removeChild(node);
117 }
118 } else {
119 node.setNodeValue(text);
120 }
121 }
122 }
123
124 /***
125 * Retrieves the text of the given node. The exact
126 * behavior depends on the type of node passed in.
127 *
128 * If the node is an Element, the value of its first child
129 * Text node is returned. if the element has no Text children,
130 * null is returned.
131 *
132 * If the node is an Attr or any CharacterData type, its value
133 * is returned.
134 *
135 * Any other type of Node will return null.
136 *
137 * @param node
138 * @return The corresponding node text
139 */
140 public static String getText(Node node) {
141 Node textNode = null;
142 if (node instanceof Element) {
143 textNode = getFirstTextChild(node);
144 } else if (node instanceof Attr || node instanceof CharacterData) {
145 textNode = node;
146 }
147 return textNode == null ? null : textNode.getNodeValue();
148 }
149
150 private static Node getFirstTextChild(Node node) {
151 NodeList kids = node.getChildNodes();
152 for (int i = 0; i < kids.getLength(); ++i) {
153 Node kid = kids.item(i);
154 if (kid instanceof Text) {
155 return kid;
156 }
157 }
158 return null;
159 }
160
161 }
This page was automatically generated by Maven