KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > util > xml > XmlUtils


1 /*
2  * Copyright 2004 The Apache Software Foundation.
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.apache.myfaces.util.xml;
17
18 import org.w3c.dom.Element JavaDoc;
19 import org.w3c.dom.Node JavaDoc;
20 import org.w3c.dom.NodeList JavaDoc;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.List JavaDoc;
25
26 /**
27  * @author Manfred Geiler (latest modification by $Author: matze $)
28  * @version $Revision: 1.2 $ $Date: 2004/10/13 11:51:01 $
29  */

30 public class XmlUtils
31 {
32     private XmlUtils()
33     {
34         // hide from public access
35
}
36
37     public static String JavaDoc getElementText(Element JavaDoc elem)
38     {
39         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
40         NodeList JavaDoc nodeList = elem.getChildNodes();
41         for (int i = 0, len = nodeList.getLength(); i < len; i++)
42         {
43             Node JavaDoc n = nodeList.item(i);
44             if (n.getNodeType() == Node.TEXT_NODE)
45             {
46                 buf.append(n.getNodeValue());
47             }
48             else
49             {
50                 //TODO see jsf-samples
51
//throw new FacesException("Unexpected node type " + n.getNodeType());
52
}
53         }
54         return buf.toString();
55     }
56
57
58
59     /**
60      * Return content of child element with given tag name.
61      * If more than one children with this name are present, the content of the last
62      * element is returned.
63      *
64      * @param elem
65      * @param childTagName
66      * @return content of child element or null if no child element with this name was found
67      */

68     public static String JavaDoc getChildText(Element JavaDoc elem, String JavaDoc childTagName)
69     {
70         NodeList JavaDoc nodeList = elem.getElementsByTagName(childTagName);
71         int len = nodeList.getLength();
72         if (len == 0)
73         {
74             return null;
75         }
76         else
77         {
78             return getElementText((Element JavaDoc)nodeList.item(len - 1));
79         }
80    }
81
82
83     /**
84      * Return list of content Strings of all child elements with given tag name.
85      * @param elem
86      * @param childTagName
87      * @return
88      */

89     public static List JavaDoc getChildTextList(Element JavaDoc elem, String JavaDoc childTagName)
90     {
91         NodeList JavaDoc nodeList = elem.getElementsByTagName(childTagName);
92         int len = nodeList.getLength();
93         if (len == 0)
94         {
95             return Collections.EMPTY_LIST;
96         }
97         else
98         {
99             List JavaDoc list = new ArrayList JavaDoc(len);
100             for (int i = 0; i < len; i++)
101             {
102                 list.add(getElementText((Element JavaDoc)nodeList.item(i)));
103             }
104             return list;
105         }
106    }
107
108 }
109
Popular Tags