KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > util > xml > XMLUtils


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: XMLUtils.java 120 2006-03-05 19:51:32Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.util.xml;
27
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Properties JavaDoc;
31
32 import org.w3c.dom.Element JavaDoc;
33 import org.w3c.dom.NamedNodeMap JavaDoc;
34 import org.w3c.dom.Node JavaDoc;
35 import org.w3c.dom.NodeList JavaDoc;
36
37 /**
38  * Class with some useful methods on XML document.
39  */

40 public final class XMLUtils {
41
42     /**
43      * Utility class, no constructor.
44      */

45     private XMLUtils() {
46     }
47
48     /**
49      * Returns the value of the attribute of the given element.
50      * @param base the element from where to search.
51      * @param name of the attribute to get.
52      * @return the value of this element.
53      */

54     public static String JavaDoc getAttributeValue(final Element JavaDoc base, final String JavaDoc name) {
55
56         // get attribute of this element...
57
NamedNodeMap JavaDoc mapAttributes = base.getAttributes();
58         Node JavaDoc node = mapAttributes.getNamedItem(name);
59         if (node != null) {
60             return node.getNodeValue();
61         }
62         return null;
63     }
64
65     /**
66      * Returns the value of the given node.
67      * @param ns the namespace.
68      * @param base the element from where to search.
69      * @param name of the element to get.
70      * @return the value of this element.
71      */

72     public static String JavaDoc getStringValueElement(final String JavaDoc ns, final Element JavaDoc base, final String JavaDoc name) {
73         String JavaDoc value = null;
74
75         // Get element
76
NodeList JavaDoc list = base.getElementsByTagNameNS(ns, name);
77         if (list.getLength() == 1) {
78             Element JavaDoc element = (Element JavaDoc) list.item(0);
79             Node JavaDoc node = element.getFirstChild();
80             if (node != null) {
81                 value = node.getNodeValue();
82             }
83         } else if (list.getLength() > 1) {
84             throw new IllegalStateException JavaDoc("Element '" + name + "' on '" + base + "' should be unique but there are '"
85                     + list.getLength() + "' elements");
86         }
87
88         return value;
89     }
90
91     /**
92      * Returns a Properties object matching the given node.
93      * @param ns the namespace.
94      * @param base the element from where to search.
95      * @param name of the element to get.
96      * @return the value of this element.
97      */

98     public static Properties JavaDoc getPropertiesValueElement(final String JavaDoc ns, final Element JavaDoc base, final String JavaDoc name) {
99         Properties JavaDoc returnedProperties = new Properties JavaDoc();
100
101         // Get element
102
NodeList JavaDoc list = base.getElementsByTagNameNS(ns, name);
103         if (list.getLength() == 1) {
104             Element JavaDoc element = (Element JavaDoc) list.item(0);
105
106             // Get property element
107
NodeList JavaDoc properties = element.getElementsByTagNameNS(ns, "property");
108
109             // If properties is present, analyze them and add them
110
if (properties.getLength() > 0) {
111                 for (int i = 0; i < properties.getLength(); i++) {
112                     Element JavaDoc elemProperty = (Element JavaDoc) properties.item(i);
113                     String JavaDoc pName = getAttributeValue(elemProperty, "name");
114                     String JavaDoc pValue = getAttributeValue(elemProperty, "value");
115                     if (pName != null && pValue != null) {
116                         returnedProperties.setProperty(pName, pValue);
117                     }
118
119                 }
120             }
121         } else if (list.getLength() > 1) {
122             throw new IllegalStateException JavaDoc("Element '" + name + "' on '" + base + "' should be unique but there are '"
123                     + list.getLength() + "' elements");
124         }
125
126         return returnedProperties;
127     }
128
129     /**
130      * Returns a list of value for the given node.
131      * @param ns the namespace.
132      * @param base the element from where to search.
133      * @param name of the element to get.
134      * @return the list of value of this element.
135      */

136     public static List JavaDoc<String JavaDoc> getStringListValueElement(final String JavaDoc ns, final Element JavaDoc base, final String JavaDoc name) {
137         List JavaDoc<String JavaDoc> returnedlist = new ArrayList JavaDoc<String JavaDoc>();
138
139         // Get element
140
NodeList JavaDoc list = base.getElementsByTagNameNS(ns, name);
141         int length = list.getLength();
142
143         // Get all values of all elements
144
if (length > 0) {
145             for (int i = 0; i < length; i++) {
146                 Element JavaDoc element = (Element JavaDoc) list.item(i);
147                 Node JavaDoc node = element.getFirstChild();
148                 if (node != null) {
149                     returnedlist.add(node.getNodeValue());
150                 }
151             }
152         }
153         return returnedlist;
154     }
155
156 }
157
Popular Tags