01: /*
02: * Copyright 2005 Joe Walker
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: package org.directwebremoting.util;
17:
18: import org.w3c.dom.CharacterData;
19: import org.w3c.dom.Comment;
20: import org.w3c.dom.EntityReference;
21: import org.w3c.dom.Node;
22: import org.w3c.dom.NodeList;
23:
24: /**
25: * Various utilities to make up for the fact that DOM isn't as useful as it
26: * could be.
27: * @author Joe Walker [joe at getahead dot ltd dot uk]
28: */
29: public class DomUtil {
30: /**
31: * Extract the textual content from a Node.
32: * This is rather like the XPath value of a Node.
33: * @param node The node to extract the text from
34: * @return The textual value of the node
35: */
36: public static String getText(Node node) {
37: StringBuffer reply = new StringBuffer();
38:
39: NodeList children = node.getChildNodes();
40: for (int i = 0; i < children.getLength(); i++) {
41: Node child = children.item(i);
42:
43: if ((child instanceof CharacterData && !(child instanceof Comment))
44: || child instanceof EntityReference) {
45: reply.append(child.getNodeValue());
46: } else if (child.getNodeType() == Node.ELEMENT_NODE) {
47: reply.append(getText(child));
48: }
49: }
50:
51: return reply.toString();
52: }
53: }
|