KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > util > DomUtil


1 /*
2  * Copyright 2005 Joe Walker
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 package org.directwebremoting.util;
17
18 import org.w3c.dom.CharacterData JavaDoc;
19 import org.w3c.dom.Comment JavaDoc;
20 import org.w3c.dom.EntityReference JavaDoc;
21 import org.w3c.dom.Node JavaDoc;
22 import org.w3c.dom.NodeList JavaDoc;
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     /**
32      * Extract the textual content from a Node.
33      * This is rather like the XPath value of a Node.
34      * @param node The node to extract the text from
35      * @return The textual value of the node
36      */

37     public static String JavaDoc getText(Node JavaDoc node)
38     {
39         StringBuffer JavaDoc reply = new StringBuffer JavaDoc();
40
41         NodeList JavaDoc children = node.getChildNodes();
42         for (int i = 0; i < children.getLength(); i++)
43         {
44             Node JavaDoc child = children.item(i);
45
46             if ((child instanceof CharacterData JavaDoc && !(child instanceof Comment JavaDoc)) || child instanceof EntityReference JavaDoc)
47             {
48                 reply.append(child.getNodeValue());
49             }
50             else if (child.getNodeType() == Node.ELEMENT_NODE)
51             {
52                 reply.append(getText(child));
53             }
54         }
55
56         return reply.toString();
57     }
58 }
59
Popular Tags