KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > xml > 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.util.xml;
23
24 import java.io.Writer JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import org.w3c.dom.Document JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30 import org.w3c.dom.Node JavaDoc;
31 import org.w3c.dom.NodeList JavaDoc;
32
33 /**
34  * A utility class to cover up the rough bits of xml parsing
35  *
36  * @author <a HREF="mailto:chris@kimptoc.net">Chris Kimpton</a>
37  * @version $Revision: 1958 $
38  */

39 public class XmlHelper
40 {
41    public static void write(Writer JavaDoc out, Document JavaDoc dom)
42       throws Exception JavaDoc
43    {
44       new DOMWriter(out).setPrettyprint(true).print(dom);
45    }
46
47    /**
48     * Returns an iterator over the children of the given element with
49     * the given tag name.
50     *
51     * @param element The parent element
52     * @param tagName The name of the desired child
53     * @return An interator of children or null if element is null.
54     */

55    public static Iterator JavaDoc getChildrenByTagName(Element JavaDoc element,
56                                                String JavaDoc tagName)
57    {
58       if (element == null) return null;
59       // getElementsByTagName gives the corresponding elements in the whole
60
// descendance. We want only children
61

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

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

112    public static Element JavaDoc getOptionalChild(Element JavaDoc element, String JavaDoc tagName)
113       throws Exception JavaDoc
114    {
115       return getOptionalChild(element, tagName, null);
116    }
117
118    /**
119     * Gets the child of the specified element having the
120     * specified name. If the child with this name doesn't exist
121     * then the supplied default element is returned instead.
122     *
123     * @param element the parent element
124     * @param tagName the name of the desired child
125     * @param defaultElement the element to return if the child
126     * doesn't exist
127     * @return either the named child or the supplied default
128     */

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

154    public static String JavaDoc getElementContent(final Element JavaDoc element)
155       throws Exception JavaDoc
156    {
157       return getElementContent(element, null);
158    }
159
160    /**
161     * Get the content of the given element.
162     *
163     * @param element The element to get the content for.
164     * @param defaultStr The default to return when there is no content.
165     * @return The content of the element or the default.
166     */

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

197    public static String JavaDoc getUniqueChildContent(Element JavaDoc element,
198                                               String JavaDoc tagName)
199       throws Exception JavaDoc
200    {
201       return getElementContent(getUniqueChild(element, tagName));
202    }
203
204    /**
205     * Macro to get the content of an optional child element.
206     *
207     * @param element The parent element.
208     * @param tagName The name of the desired child.
209     * @return The element content or null.
210     */

211    public static String JavaDoc getOptionalChildContent(Element JavaDoc element,
212                                                 String JavaDoc tagName)
213       throws Exception JavaDoc
214    {
215       return getElementContent(getOptionalChild(element, tagName));
216    }
217
218    public static boolean getOptionalChildBooleanContent(Element JavaDoc element, String JavaDoc name) throws Exception JavaDoc
219    {
220       Element JavaDoc child = getOptionalChild(element, name);
221       if(child != null)
222       {
223          String JavaDoc value = getElementContent(child).toLowerCase();
224          return value.equals("true") || value.equals("yes");
225       }
226
227       return false;
228    }
229
230
231 }
232
233
234
Popular Tags