KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > ejb > packaging > XmlHelper


1 package org.hibernate.ejb.packaging;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5
6 import org.w3c.dom.Element JavaDoc;
7 import org.w3c.dom.Node JavaDoc;
8 import org.w3c.dom.NodeList JavaDoc;
9
10 /**
11  * A utility class to cover up the rough bits of xml parsing
12  *
13  * @author <a HREF="mailto:chris@kimptoc.net">Chris Kimpton</a>
14  * @version $Revision: 1.3 $
15  */

16 public final class XmlHelper {
17     private XmlHelper() {}
18     /**
19      * Returns an iterator over the children of the given element with
20      * the given tag name.
21      *
22      * @param element The parent element
23      * @param tagName The name of the desired child
24      * @return An interator of children or null if element is null.
25      */

26     public static Iterator JavaDoc getChildrenByTagName(
27             Element JavaDoc element,
28             String JavaDoc tagName
29             ) {
30         if ( element == null ) return null;
31         // getElementsByTagName gives the corresponding elements in the whole
32
// descendance. We want only children
33

34         NodeList JavaDoc children = element.getChildNodes();
35         ArrayList JavaDoc goodChildren = new ArrayList JavaDoc();
36         for ( int i = 0 ; i < children.getLength() ; i++ ) {
37             Node JavaDoc currentChild = children.item( i );
38             if ( currentChild.getNodeType() == Node.ELEMENT_NODE &&
39                     ( (Element JavaDoc) currentChild ).getTagName().equals( tagName ) ) {
40                 goodChildren.add( (Element JavaDoc) currentChild );
41             }
42         }
43         return goodChildren.iterator();
44     }
45
46     /**
47      * Gets the child of the specified element having the specified unique
48      * name. If there are more than one children elements with the same name
49      * and exception is thrown.
50      *
51      * @param element The parent element
52      * @param tagName The name of the desired child
53      * @return The named child.
54      * @throws Exception Child was not found or was not unique.
55      */

56     public static Element JavaDoc getUniqueChild(Element JavaDoc element, String JavaDoc tagName)
57             throws Exception JavaDoc {
58         Iterator JavaDoc goodChildren = getChildrenByTagName( element, tagName );
59
60         if ( goodChildren != null && goodChildren.hasNext() ) {
61             Element JavaDoc child = (Element JavaDoc) goodChildren.next();
62             if ( goodChildren.hasNext() ) {
63                 throw new Exception JavaDoc
64                         ( "expected only one " + tagName + " tag" );
65             }
66             return child;
67         }
68         else {
69             throw new Exception JavaDoc
70                     ( "expected one " + tagName + " tag" );
71         }
72     }
73
74     /**
75      * Gets the child of the specified element having the
76      * specified name. If the child with this name doesn't exist
77      * then null is returned instead.
78      *
79      * @param element the parent element
80      * @param tagName the name of the desired child
81      * @return either the named child or null
82      */

83     public static Element JavaDoc getOptionalChild(Element JavaDoc element, String JavaDoc tagName)
84             throws Exception JavaDoc {
85         return getOptionalChild( element, tagName, null );
86     }
87
88     /**
89      * Gets the child of the specified element having the
90      * specified name. If the child with this name doesn't exist
91      * then the supplied default element is returned instead.
92      *
93      * @param element the parent element
94      * @param tagName the name of the desired child
95      * @param defaultElement the element to return if the child
96      * doesn't exist
97      * @return either the named child or the supplied default
98      */

99     public static Element JavaDoc getOptionalChild(
100             Element JavaDoc element,
101             String JavaDoc tagName,
102             Element JavaDoc defaultElement
103             )
104             throws Exception JavaDoc {
105         Iterator JavaDoc goodChildren = getChildrenByTagName( element, tagName );
106
107         if ( goodChildren != null && goodChildren.hasNext() ) {
108             Element JavaDoc child = (Element JavaDoc) goodChildren.next();
109             if ( goodChildren.hasNext() ) {
110                 throw new Exception JavaDoc
111                         ( "expected only one " + tagName + " tag" );
112             }
113             return child;
114         }
115         else {
116             return defaultElement;
117         }
118     }
119
120     /**
121      * Get the content of the given element.
122      *
123      * @param element The element to get the content for.
124      * @return The content of the element or null.
125      */

126     public static String JavaDoc getElementContent(final Element JavaDoc element)
127             throws Exception JavaDoc {
128         return getElementContent( element, null );
129     }
130
131     /**
132      * Get the content of the given element.
133      *
134      * @param element The element to get the content for.
135      * @param defaultStr The default to return when there is no content.
136      * @return The content of the element or the default.
137      */

138     public static String JavaDoc getElementContent(Element JavaDoc element, String JavaDoc defaultStr)
139             throws Exception JavaDoc {
140         if ( element == null ) {
141             return defaultStr;
142         }
143
144         NodeList JavaDoc children = element.getChildNodes();
145         String JavaDoc result = "";
146         for ( int i = 0 ; i < children.getLength() ; i++ ) {
147             if ( children.item( i ).getNodeType() == Node.TEXT_NODE ||
148                     children.item( i ).getNodeType() == Node.CDATA_SECTION_NODE ) {
149                 result += children.item( i ).getNodeValue();
150             }
151             else if ( children.item( i ).getNodeType() == Node.COMMENT_NODE ) {
152                 // Ignore comment nodes
153
}
154         }
155         return result.trim();
156     }
157
158     /**
159      * Macro to get the content of a unique child element.
160      *
161      * @param element The parent element.
162      * @param tagName The name of the desired child.
163      * @return The element content or null.
164      */

165     public static String JavaDoc getUniqueChildContent(
166             Element JavaDoc element,
167             String JavaDoc tagName
168             )
169             throws Exception JavaDoc {
170         return getElementContent( getUniqueChild( element, tagName ) );
171     }
172
173     /**
174      * Macro to get the content of an optional child element.
175      *
176      * @param element The parent element.
177      * @param tagName The name of the desired child.
178      * @return The element content or null.
179      */

180     public static String JavaDoc getOptionalChildContent(
181             Element JavaDoc element,
182             String JavaDoc tagName
183             )
184             throws Exception JavaDoc {
185         return getElementContent( getOptionalChild( element, tagName ) );
186     }
187
188     public static boolean getOptionalChildBooleanContent(Element JavaDoc element, String JavaDoc name) throws Exception JavaDoc {
189         Element JavaDoc child = getOptionalChild( element, name );
190         if ( child != null ) {
191             String JavaDoc value = getElementContent( child ).toLowerCase();
192             return value.equals( "true" ) || value.equals( "yes" );
193         }
194
195         return false;
196     }
197
198
199 }
200
201
202
Popular Tags