KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > util > XmlHelper


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.aop.util;
23
24 import org.w3c.dom.Element JavaDoc;
25 import org.w3c.dom.Node JavaDoc;
26 import org.w3c.dom.NodeList JavaDoc;
27
28 import java.util.ArrayList JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 /**
32  * A utility class to cover up the rough bits of xml parsing
33  *
34  * @author <a HREF="mailto:chris@kimptoc.net">Chris Kimpton</a>
35  * @version $Revision: 57391 $
36  */

37 public class XmlHelper
38 {
39    /**
40     * Returns an iterator over the children of the given element with
41     * the given tag name.
42     *
43     * @param element The parent element
44     * @param tagName The name of the desired child
45     * @return An interator of children or null if element is null.
46     */

47    public static Iterator JavaDoc getChildrenByTagName(Element JavaDoc element,
48                                                String JavaDoc tagName)
49    {
50       if (element == null) return null;
51       // getElementsByTagName gives the corresponding elements in the whole
52
// descendance. We want only children
53

54       NodeList JavaDoc children = element.getChildNodes();
55       ArrayList JavaDoc goodChildren = new ArrayList JavaDoc();
56       for (int i=0; i<children.getLength(); i++) {
57          Node JavaDoc currentChild = children.item(i);
58          if (currentChild.getNodeType() == Node.ELEMENT_NODE &&
59              ((Element JavaDoc)currentChild).getTagName().equals(tagName)) {
60             goodChildren.add(currentChild);
61          }
62       }
63       return goodChildren.iterator();
64    }
65
66    /**
67     * Gets the child of the specified element having the specified unique
68     * name. If there are more than one children elements with the same name
69     * and exception is thrown.
70     *
71     * @param element The parent element
72     * @param tagName The name of the desired child
73     * @return The named child.
74     *
75     * @throws Exception Child was not found or was not unique.
76     */

77    public static Element JavaDoc getUniqueChild(Element JavaDoc element, String JavaDoc tagName)
78       throws Exception JavaDoc
79    {
80       Iterator JavaDoc goodChildren = getChildrenByTagName(element, tagName);
81
82       if (goodChildren != null && goodChildren.hasNext()) {
83          Element JavaDoc child = (Element JavaDoc)goodChildren.next();
84          if (goodChildren.hasNext()) {
85             throw new Exception JavaDoc
86                ("expected only one " + tagName + " tag");
87          }
88          return child;
89       } else {
90          throw new Exception JavaDoc
91             ("expected one " + tagName + " tag");
92       }
93    }
94
95    /**
96     * Gets the child of the specified element having the
97     * specified name. If the child with this name doesn't exist
98     * then null is returned instead.
99     *
100     * @param element the parent element
101     * @param tagName the name of the desired child
102     * @return either the named child or null
103     */

104    public static Element JavaDoc getOptionalChild(Element JavaDoc element, String JavaDoc tagName)
105       throws Exception JavaDoc
106    {
107       return getOptionalChild(element, tagName, null);
108    }
109
110    /**
111     * Gets the child of the specified element having the
112     * specified name. If the child with this name doesn't exist
113     * then the supplied default element is returned instead.
114     *
115     * @param element the parent element
116     * @param tagName the name of the desired child
117     * @param defaultElement the element to return if the child
118     * doesn't exist
119     * @return either the named child or the supplied default
120     */

121    public static Element JavaDoc getOptionalChild(Element JavaDoc element,
122                                           String JavaDoc tagName,
123                                           Element JavaDoc defaultElement)
124       throws Exception JavaDoc
125    {
126       Iterator JavaDoc goodChildren = getChildrenByTagName(element, tagName);
127
128       if (goodChildren != null && goodChildren.hasNext()) {
129          Element JavaDoc child = (Element JavaDoc)goodChildren.next();
130          if (goodChildren.hasNext()) {
131             throw new Exception JavaDoc
132                ("expected only one " + tagName + " tag");
133          }
134          return child;
135       } else {
136          return defaultElement;
137       }
138    }
139
140    /**
141     * Get the content of the given element.
142     *
143     * @param element The element to get the content for.
144     * @return The content of the element or null.
145     */

146    public static String JavaDoc getElementContent(final Element JavaDoc element)
147       throws Exception JavaDoc
148    {
149       return getElementContent(element, null);
150    }
151
152    /**
153     * Get the content of the given element.
154     *
155     * @param element The element to get the content for.
156     * @param defaultStr The default to return when there is no content.
157     * @return The content of the element or the default.
158     */

159    public static String JavaDoc getElementContent(Element JavaDoc element, String JavaDoc defaultStr)
160       throws Exception JavaDoc
161    {
162       if (element == null)
163          return defaultStr;
164
165       NodeList JavaDoc children = element.getChildNodes();
166       String JavaDoc result = "";
167       for (int i = 0; i < children.getLength(); i++)
168       {
169          if (children.item(i).getNodeType() == Node.TEXT_NODE ||
170              children.item(i).getNodeType() == Node.CDATA_SECTION_NODE)
171          {
172             result += children.item(i).getNodeValue();
173          }
174          else if( children.item(i).getNodeType() == Node.COMMENT_NODE )
175          {
176             // Ignore comment nodes
177
}
178       }
179       return result.trim();
180    }
181
182    /**
183     * Macro to get the content of a unique child element.
184     *
185     * @param element The parent element.
186     * @param tagName The name of the desired child.
187     * @return The element content or null.
188     */

189    public static String JavaDoc getUniqueChildContent(Element JavaDoc element,
190                                               String JavaDoc tagName)
191       throws Exception JavaDoc
192    {
193       return getElementContent(getUniqueChild(element, tagName));
194    }
195
196    /**
197     * Macro to get the content of an optional child element.
198     *
199     * @param element The parent element.
200     * @param tagName The name of the desired child.
201     * @return The element content or null.
202     */

203    public static String JavaDoc getOptionalChildContent(Element JavaDoc element,
204                                                 String JavaDoc tagName)
205       throws Exception JavaDoc
206    {
207       return getElementContent(getOptionalChild(element, tagName));
208    }
209
210    public static boolean getOptionalChildBooleanContent(Element JavaDoc element, String JavaDoc name) throws Exception JavaDoc
211    {
212       Element JavaDoc child = getOptionalChild(element, name);
213       if(child != null)
214       {
215          String JavaDoc value = getElementContent(child).toLowerCase();
216          return value.equals("true") || value.equals("yes");
217       }
218
219       return false;
220    }
221
222
223 }
224
225
226
Popular Tags