KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > alt > assembler > classic > xml > DomTools


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 1999 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: DomTools.java 1921 2005-06-19 22:40:34Z jlaskowski $
44  */

45
46 package org.openejb.alt.assembler.classic.xml;
47
48 import java.util.Properties JavaDoc;
49 import java.util.Vector JavaDoc;
50
51 import org.openejb.OpenEJBException;
52 import org.openejb.util.SafeToolkit;
53 import org.w3c.dom.DOMException JavaDoc;
54 import org.w3c.dom.Element JavaDoc;
55 import org.w3c.dom.NamedNodeMap JavaDoc;
56 import org.w3c.dom.Node JavaDoc;
57 import org.w3c.dom.NodeList JavaDoc;
58 import org.w3c.dom.Text JavaDoc;
59
60 /**
61  *
62  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
63  * @author <a HREF="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
64  */

65 public class DomTools{
66
67     public static final SafeToolkit toolkit = SafeToolkit.getToolkit("XML configuration loader");
68
69     /**
70      * Represents the <tt>properties</tt> element in the XML config file.
71      */

72     public static final String JavaDoc PROPERTIES = "properties";
73
74     /**
75      * Represents the <tt>property</tt> element in the XML config file.
76      */

77     public static final String JavaDoc PROPERTY = "property";
78
79     /**
80      * Represents the <tt>property-name</tt> element in the XML config file.
81      */

82     public static final String JavaDoc PROPERTY_NAME = "property-name";
83
84     /**
85      * Represents the <tt>property-value</tt> element in the XML config file.
86      */

87     public static final String JavaDoc PROPERTY_VALUE = "property-value";
88
89
90
91     public static Properties JavaDoc readProperties(Node JavaDoc node) {
92         Node JavaDoc propertiesElement = getChildElement(node, PROPERTIES);
93
94         if(propertiesElement == null) return new Properties JavaDoc();
95
96         Node JavaDoc[] property = getChildElements(propertiesElement,PROPERTY);
97         Properties JavaDoc properties = new Properties JavaDoc();
98         String JavaDoc name = null, value = null;
99
100         for (int i=0; i< property.length; i++){
101             name = getChildElementPCData(property[i], PROPERTY_NAME);
102             value = getChildElementPCData(property[i], PROPERTY_VALUE);
103             if (name == null || value == null) continue;
104             properties.setProperty( name, value );
105         }
106         return properties;
107     }
108
109     /**
110      * If true debug data will be printed to the System.out containing the data in the
111      * XML config file being parsed.
112      */

113     public static final boolean debug = false;
114
115     public static int debugRecursionDepth = 0;
116
117
118     /**
119      * Convenience method for obtaining all the child elements of the node passed in.
120      * When a child element with a name matching the <tt>elementType</tt> is found in the <tt>node</tt>
121      * a new instance of <tt>classType</tt> is created, cast to <tt>DomObject</tt>, then <tt>initializeFromDOM</tt> is called
122      * on the new instance and the child element is passed in as the parameter.
123      *
124      * @param node the node in the DOM containing the child elements needed.
125      * @param classType the subclass of <tt>DomObject</tt> that will parse the data in the child elements.
126      * @param elementType the name of the child element as it appears in the DTD.
127      * @return an array of the <tt>DomObject</tt> subclasses initialized with the child elements.
128      * @see org.w3c.dom.Node
129      */

130     protected static DomObject[] collectChildElementsByType(Node JavaDoc node, Class JavaDoc classType, String JavaDoc elementType) throws OpenEJBException{
131
132         if (debug){/*--------------------------------- * Debug Block * ------*/
133             debugRecursionDepth++;
134             for(int i=0;i<debugRecursionDepth;i++)System.out.print("\t");
135             System.out.println(node.getNodeName()+"."+ elementType);
136         }/*------------------------------------------- * Debug Block * ------*/
137
138         if (node == null) return null;
139
140         NodeList JavaDoc list = node.getChildNodes();
141         Vector JavaDoc tmp = new Vector JavaDoc();
142         Node JavaDoc child = null;
143
144         for (int i=0; i< list.getLength(); i++){
145             child = list.item(i);
146             if (child.getNodeType() == Node.ELEMENT_NODE){
147                 Element JavaDoc element = (Element JavaDoc)child;
148                 if (element.getTagName().equals(elementType)){
149                     DomObject info = (DomObject)toolkit.newInstance(classType);
150                     tmp.addElement(info);
151                     info.initializeFromDOM(element);
152                 }
153             }
154         }
155
156         if (debug){/*--------------------------------- * Debug Block * ------*/
157             debugRecursionDepth--;
158         }/*------------------------------------------- * Debug Block * ------*/
159
160         DomObject[] domObjects = new DomObject[tmp.size()];
161         tmp.copyInto(domObjects);
162         return domObjects;
163     }
164
165     /**
166      * Convenience method for obtaining a single child element from the node passed in.
167      * When a child element with a name matching the <tt>elementType</tt> is found in the <tt>node</tt>
168      * a new instance of <tt>classType</tt> is created, cast to <tt>DomObject</tt>, then <tt>initializeFromDOM</tt> is called
169      * on the new instance and the child element is passed in as the parameter.
170      *
171      * @param node the node in the DOM containing the child elements needed.
172      * @param classType the subclass of <tt>DomObject</tt> that will parse the data in the child elements.
173      * @param elementType the name of the child element as it appears in the DTD.
174      * @return an <tt>DomObject</tt> subclass of type <tt>classType</tt> initialized with the child element.
175      * @see org.w3c.dom.Node
176      */

177     protected static DomObject collectChildElementByType(Node JavaDoc node, Class JavaDoc classType, String JavaDoc elementType) throws OpenEJBException{
178         try{
179
180
181         if (debug){/*--------------------------------- * Debug Block * ------*/
182             debugRecursionDepth++;
183             for(int i=0;i<debugRecursionDepth;i++)System.out.print("\t");
184             System.out.println(node.getNodeName()+"."+ elementType);
185         }/*------------------------------------------- * Debug Block * ------*/
186
187         NodeList JavaDoc list = node.getChildNodes();
188         Node JavaDoc child = null;
189         DomObject domObject = null;
190         for (int i=0; i< list.getLength(); i++){
191             child = list.item(i);
192             if (child.getNodeType() == Node.ELEMENT_NODE){
193                 Element JavaDoc element = (Element JavaDoc)child;
194                 if (element.getTagName().equals(elementType)){
195                     domObject = (DomObject)toolkit.newInstance(classType);
196                     domObject.initializeFromDOM(element);
197                     break;
198                 }
199             }
200         }
201
202         if (debug){/*--------------------------------- * Debug Block * ------*/
203             debugRecursionDepth--;
204         }/*------------------------------------------- * Debug Block * ------*/
205
206         return domObject;
207         } catch(Exception JavaDoc e){
208             e.printStackTrace();
209             throw new OpenEJBException(e);
210         }
211
212     }
213
214     /**
215      * Returns the PCDATA of all child elements to the <tt>node</tt> passed in.
216      * A child elements PCDATA will be collected if its name matches the <tt>elementType</tt> specified.
217      *
218      * @param node the node in the DOM containing the child element.
219      * @param elementType the name of the child element as it appears in the DTD.
220      * @return an array of <tt>String</tt> containing the PCDATA of the child elements.
221      */

222     protected static String JavaDoc[] getChildElementsPCData(Node JavaDoc node, String JavaDoc elementType) {
223
224         if (debug){/*--------------------------------- * Debug Block * ------*/
225             debugRecursionDepth++;
226             for(int i=0;i<debugRecursionDepth;i++)System.out.print("\t");
227             System.out.print(node.getNodeName()+"."+ elementType);
228         }/*------------------------------------------- * Debug Block * ------*/
229
230         NodeList JavaDoc list = node.getChildNodes();
231
232         Node JavaDoc child = null;
233         Vector JavaDoc tmp = new Vector JavaDoc();
234
235         for (int i=0; i< list.getLength(); i++){
236             child = list.item(i);
237             if (child.getNodeType() == Node.ELEMENT_NODE){
238                 Element JavaDoc element = (Element JavaDoc)child;
239                 if (element.getTagName().equals(elementType)){
240                     tmp.addElement(getElementPCData(element));
241                 }
242             }
243         }
244         String JavaDoc[] pcdata = new String JavaDoc[tmp.size()];
245         tmp.copyInto(pcdata);
246
247         if (debug){/*--------------------------------- * Debug Block * ------*/
248
249             String JavaDoc tabs = "";
250             for(int i=0;i<debugRecursionDepth;i++){tabs+="\t";}
251             System.out.println(".length = "+pcdata.length);
252             for(int i=0;i<pcdata.length;i++) System.out.println(tabs + node.getNodeName()+"."+ elementType + "["+ i +"] = " +pcdata[i]);
253             debugRecursionDepth--;
254         }/*------------------------------------------- * Debug Block * ------*/
255
256         return pcdata;
257     }
258
259     /**
260      * Returns the PCDATA of a child element in the <tt>node</tt> passed in.
261      * A child elements PCDATA will be returned if its name matches the <tt>elementType</tt> specified.
262      *
263      * @param node the node in the DOM containing the child element.
264      * @param elementType the name of the child element as it appears in the DTD.
265      * @return the PCDATA of the child elements.
266      */

267     protected static String JavaDoc getChildElementPCData(Node JavaDoc node, String JavaDoc elementType) {
268
269         if (debug){/*--------------------------------- * Debug Block * ------*/
270             debugRecursionDepth++;
271             for(int i=0;i<debugRecursionDepth;i++)System.out.print("\t");
272             System.out.print(node.getNodeName()+"."+ elementType);
273         }/*------------------------------------------- * Debug Block * ------*/
274
275         NodeList JavaDoc list = node.getChildNodes();
276         Node JavaDoc child = null;
277         String JavaDoc pcdata = null;
278         for (int i=0; i< list.getLength(); i++){
279             child = list.item(i);
280             if (child.getNodeType() == Node.ELEMENT_NODE){
281                 Element JavaDoc element = (Element JavaDoc)child;
282                 if (element.getTagName().equals(elementType)){
283                     pcdata = getElementPCData(element);
284                     break;
285                 }
286             }
287         }
288
289         if (debug){/*--------------------------------- * Debug Block * ------*/
290             System.out.println(" = "+pcdata);
291             debugRecursionDepth--;
292         }/*------------------------------------------- * Debug Block * ------*/
293
294         return pcdata;
295     }
296
297     /**
298      * Returns the PCDATA of the <tt>node</tt> passed in.
299      *
300      * @param node the node in the DOM containing the PCDATA.
301      * @return the PCDATA of the node.
302      */

303     protected static String JavaDoc getElementPCData(Node JavaDoc node) {
304         Node JavaDoc child = node.getFirstChild();
305         if (child == null || child.getNodeType() != Node.TEXT_NODE) return null;
306
307         try{
308             Text JavaDoc text = (Text JavaDoc)child;
309             String JavaDoc pcdata = text.getData();
310             return (pcdata!=null)?pcdata.trim():null;
311         } catch (DOMException JavaDoc e) {
312             throw e;
313         }
314     }
315
316     /**
317      * Returns the named attributes of the <tt>node</tt> passed in.
318      *
319      * @param node the node in the DOM containing the attributes.
320      * @return a Properties object containing the attributes of the node.
321      */

322     protected static Properties JavaDoc getElementAttributes(Node JavaDoc node){
323         NamedNodeMap JavaDoc nodeMap = node.getAttributes();
324         int size = nodeMap.getLength();
325         Properties JavaDoc attributes = new Properties JavaDoc();
326         for(int i = 0; i < size; i++){
327             node = nodeMap.item(i);
328             attributes.setProperty(node.getNodeName(), node.getNodeValue());
329         }
330         return attributes;
331     }
332
333     /**
334      * Returns the child element of the <tt>node</tt> passed in that matches the element name passed in.
335      *
336      * @param node the node in the DOM containing the PCDATA.
337      * @param childName the element name of the desired child element as defined in the DTD.
338      * @return the desired child element. OR null if the child element is not present
339      */

340     protected static Node JavaDoc getChildElement(Node JavaDoc node, String JavaDoc childName) {
341
342         NodeList JavaDoc list = node.getChildNodes();
343         Node JavaDoc child = null;
344
345         for (int i=0; i< list.getLength(); i++){
346             child = list.item(i);
347             if (child.getNodeType() == Node.ELEMENT_NODE){
348                 Element JavaDoc element = (Element JavaDoc)child;
349                 if (element.getTagName().equals(childName))
350                     return child;
351             }
352         }
353         return null;
354     }
355
356     /**
357      * Returns the child elements of the <tt>node</tt> passed in that match the element name passed in.
358      *
359      * @param node the node in the DOM containing the PCDATA.
360      * @param childName the element name of the desired child element as defined in the DTD.
361      * @return an array of <tt>Node</tt> containing all the desired child elements.
362      */

363     protected static Node JavaDoc[] getChildElements(Node JavaDoc node, String JavaDoc childName) {
364
365         NodeList JavaDoc list = node.getChildNodes();
366         Node JavaDoc child = null;
367         Vector JavaDoc tmp = new Vector JavaDoc();
368
369         for (int i=0; i< list.getLength(); i++){
370             child = list.item(i);
371             if (child.getNodeType() == Node.ELEMENT_NODE){
372                 Element JavaDoc element = (Element JavaDoc)child;
373                 if (element.getTagName().equals(childName)){
374                     tmp.addElement(element);
375                 }
376             }
377         }
378
379         Node JavaDoc[] children = new Node JavaDoc[tmp.size()];
380         tmp.copyInto(children);
381         return children;
382     }
383
384 }
Popular Tags