Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
XmlUtils |
|
| 2.75;2.75 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
18 | */ | |
19 | package org.apache.myfaces.shared_impl.util.xml; | |
20 | ||
21 | import org.w3c.dom.Element; | |
22 | import org.w3c.dom.Node; | |
23 | import org.w3c.dom.NodeList; | |
24 | ||
25 | import java.util.ArrayList; | |
26 | import java.util.Collections; | |
27 | import java.util.List; | |
28 | ||
29 | /** | |
30 | * @author Manfred Geiler (latest modification by $Author: matzew $) | |
31 | * @version $Revision: 557350 $ $Date: 2007-07-18 13:19:50 -0500 (Wed, 18 Jul 2007) $ | |
32 | */ | |
33 | public class XmlUtils | |
34 | { | |
35 | private XmlUtils() | |
36 | 0 | { |
37 | // hide from public access | |
38 | 0 | } |
39 | ||
40 | public static String getElementText(Element elem) | |
41 | { | |
42 | 0 | StringBuffer buf = new StringBuffer(); |
43 | 0 | NodeList nodeList = elem.getChildNodes(); |
44 | 0 | for (int i = 0, len = nodeList.getLength(); i < len; i++) |
45 | { | |
46 | 0 | Node n = nodeList.item(i); |
47 | 0 | if (n.getNodeType() == Node.TEXT_NODE) |
48 | { | |
49 | 0 | buf.append(n.getNodeValue()); |
50 | } | |
51 | else | |
52 | { | |
53 | //TODO see jsf-samples | |
54 | //throw new FacesException("Unexpected node type " + n.getNodeType()); | |
55 | } | |
56 | } | |
57 | 0 | return buf.toString(); |
58 | } | |
59 | ||
60 | ||
61 | ||
62 | /** | |
63 | * Return content of child element with given tag name. | |
64 | * If more than one children with this name are present, the content of the last | |
65 | * element is returned. | |
66 | * | |
67 | * @param elem | |
68 | * @param childTagName | |
69 | * @return content of child element or null if no child element with this name was found | |
70 | */ | |
71 | public static String getChildText(Element elem, String childTagName) | |
72 | { | |
73 | 0 | NodeList nodeList = elem.getElementsByTagName(childTagName); |
74 | 0 | int len = nodeList.getLength(); |
75 | 0 | if (len == 0) |
76 | { | |
77 | 0 | return null; |
78 | } | |
79 | ||
80 | 0 | return getElementText((Element)nodeList.item(len - 1)); |
81 | ||
82 | } | |
83 | ||
84 | ||
85 | /** | |
86 | * Return list of content Strings of all child elements with given tag name. | |
87 | * @param elem | |
88 | * @param childTagName | |
89 | * @return List | |
90 | */ | |
91 | public static List getChildTextList(Element elem, String childTagName) | |
92 | { | |
93 | 0 | NodeList nodeList = elem.getElementsByTagName(childTagName); |
94 | 0 | int len = nodeList.getLength(); |
95 | 0 | if (len == 0) |
96 | { | |
97 | 0 | return Collections.EMPTY_LIST; |
98 | } | |
99 | ||
100 | 0 | List list = new ArrayList(len); |
101 | 0 | for (int i = 0; i < len; i++) |
102 | { | |
103 | 0 | list.add(getElementText((Element)nodeList.item(i))); |
104 | } | |
105 | 0 | return list; |
106 | ||
107 | } | |
108 | ||
109 | } |