001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: XMLUtils.java 2049 2007-11-20 14:32:56Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.util.xml;
025:
026: import java.util.ArrayList;
027: import java.util.List;
028: import java.util.Properties;
029:
030: import org.w3c.dom.Element;
031: import org.w3c.dom.NamedNodeMap;
032: import org.w3c.dom.Node;
033: import org.w3c.dom.NodeList;
034:
035: /**
036: * Class with some useful methods on XML document.
037: */
038: public final class XMLUtils {
039:
040: /**
041: * Utility class, no constructor.
042: */
043: private XMLUtils() {
044: }
045:
046: /**
047: * Returns the value of the attribute of the given element.
048: * @param base the element from where to search.
049: * @param name of the attribute to get.
050: * @return the value of this element.
051: */
052: public static String getAttributeValue(final Element base,
053: final String name) {
054:
055: // get attribute of this element...
056: NamedNodeMap mapAttributes = base.getAttributes();
057: Node node = mapAttributes.getNamedItem(name);
058: if (node != null) {
059: return node.getNodeValue();
060: }
061: return null;
062: }
063:
064: /**
065: * Returns the value of the given node.
066: * @param ns the namespace.
067: * @param base the element from where to search.
068: * @param name of the element to get.
069: * @return the value of this element.
070: */
071: public static String getStringValueElement(final String ns,
072: final Element base, final String name) {
073: String value = null;
074:
075: // Get element
076: NodeList list = base.getElementsByTagNameNS(ns, name);
077: if (list.getLength() == 1) {
078: Element element = (Element) list.item(0);
079: Node node = element.getFirstChild();
080: if (node != null) {
081: value = node.getNodeValue();
082: }
083: } else if (list.getLength() > 1) {
084: throw new IllegalStateException("Element '" + name
085: + "' on '" + base
086: + "' should be unique but there are '"
087: + list.getLength() + "' elements");
088: }
089:
090: if (value != null) {
091: value = value.trim();
092: }
093: return value;
094: }
095:
096: /**
097: * Returns the value of the given node.
098: * @param base the element from where to search.
099: * @param name of the element to get.
100: * @return the value of this element.
101: */
102: public static String getStringValueElement(final Element base,
103: final String name) {
104: String value = null;
105:
106: // Get element
107: NodeList list = base.getElementsByTagName(name);
108: if (list.getLength() == 1) {
109: Element element = (Element) list.item(0);
110: Node node = element.getFirstChild();
111: if (node != null) {
112: value = node.getNodeValue();
113: }
114: } else if (list.getLength() > 1) {
115: throw new IllegalStateException("Element '" + name
116: + "' on '" + base
117: + "' should be unique but there are '"
118: + list.getLength() + "' elements");
119: }
120:
121: if (value != null) {
122: value = value.trim();
123: }
124: return value;
125: }
126:
127: /**
128: * Returns the value of the child node with the given name.
129: * @param base the element from where to search.
130: * @param name of the element to get.
131: * @return the value of this element.
132: */
133: public static String getChildStringValueForElement(
134: final Element base, final String name) {
135: String value = null;
136: NodeList nodeList = base.getChildNodes();
137: if (nodeList.getLength() > 0) {
138: int length = nodeList.getLength();
139: for (int i = 0; i < length; i++) {
140: Node node = nodeList.item(i);
141:
142: // Get an element, create an instance of the element
143: if (Node.ELEMENT_NODE == node.getNodeType()) {
144: if (name.equals(node.getNodeName())) {
145: // Get value of this child
146: Node elNode = ((Element) node).getFirstChild();
147: if (elNode != null) {
148: value = elNode.getNodeValue();
149: break;
150: }
151: }
152: }
153: }
154: }
155: return value;
156: }
157:
158: /**
159: * Returns a Properties object matching the given node.
160: * @param ns the namespace.
161: * @param base the element from where to search.
162: * @param name of the element to get.
163: * @return the value of this element.
164: */
165: public static Properties getPropertiesValueElement(final String ns,
166: final Element base, final String name) {
167: Properties returnedProperties = new Properties();
168:
169: // Get element
170: NodeList list = base.getElementsByTagNameNS(ns, name);
171: if (list.getLength() == 1) {
172: Element element = (Element) list.item(0);
173:
174: // Get property element
175: NodeList properties = element.getElementsByTagNameNS(ns,
176: "property");
177:
178: // If properties is present, analyze them and add them
179: if (properties.getLength() > 0) {
180: for (int i = 0; i < properties.getLength(); i++) {
181: Element elemProperty = (Element) properties.item(i);
182: String pName = getAttributeValue(elemProperty,
183: "name");
184: String pValue = getAttributeValue(elemProperty,
185: "value");
186: if (pName != null && pValue != null) {
187: returnedProperties.setProperty(pName, pValue);
188: }
189:
190: }
191: }
192: } else if (list.getLength() > 1) {
193: throw new IllegalStateException("Element '" + name
194: + "' on '" + base
195: + "' should be unique but there are '"
196: + list.getLength() + "' elements");
197: }
198:
199: return returnedProperties;
200: }
201:
202: /**
203: * Returns a list of value for the given node.
204: * @param ns the namespace.
205: * @param base the element from where to search.
206: * @param name of the element to get.
207: * @return the list of value of this element.
208: */
209: public static List<String> getStringListValueElement(
210: final String ns, final Element base, final String name) {
211: List<String> returnedlist = new ArrayList<String>();
212:
213: // Get element
214: NodeList list = base.getElementsByTagNameNS(ns, name);
215: int length = list.getLength();
216:
217: // Get all values of all elements
218: if (length > 0) {
219: for (int i = 0; i < length; i++) {
220: Element element = (Element) list.item(i);
221: Node node = element.getFirstChild();
222: if (node != null) {
223: returnedlist.add(node.getNodeValue());
224: }
225: }
226: }
227: return returnedlist;
228: }
229:
230: }
|