KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > framework > configuration > ConfigurationUtil


1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  * if any, must include the following acknowledgment:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowledgment may appear in the software
24  * itself, if and wherever such third-party acknowledgments
25  * normally appear.
26  *
27  * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
28  * must not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55
56 package org.apache.avalon.framework.configuration;
57
58 import javax.xml.parsers.DocumentBuilder JavaDoc;
59 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
60 import javax.xml.parsers.ParserConfigurationException JavaDoc;
61 import org.w3c.dom.Document JavaDoc;
62 import org.w3c.dom.Element JavaDoc;
63 import org.w3c.dom.Text JavaDoc;
64 import java.util.ArrayList JavaDoc;
65 import java.util.Arrays JavaDoc;
66 import java.util.Iterator JavaDoc;
67
68 /**
69  * This class has a bunch of utility methods to work
70  * with configuration objects.
71  *
72  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
73  * @version CVS $Revision: 1.11 $ $Date: 2003/02/11 15:58:38 $
74  */

75 public class ConfigurationUtil
76 {
77     /**
78      * Private constructor to block instantiation.
79      */

80     private ConfigurationUtil()
81     {
82     }
83
84     /**
85      * Convert a configuration tree into a DOM Element tree.
86      *
87      * @param configuration the configuration object
88      * @return the DOM Element
89      */

90     public static Element JavaDoc toElement( final Configuration configuration )
91     {
92         try
93         {
94             final DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
95             final DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
96             final Document JavaDoc document = builder.newDocument();
97
98             return createElement( document, configuration );
99         }
100         catch( final ParserConfigurationException JavaDoc pce )
101         {
102             throw new IllegalStateException JavaDoc( pce.toString() );
103         }
104     }
105
106     /**
107      * Test to see if two Configuration's can be considered the same. Name, value, attributes
108      * and children are test. The <b>order</b> of children is not taken into consideration
109      * for equality.
110      *
111      * @param c1 Configuration to test
112      * @param c2 Configuration to test
113      * @return true if the configurations can be considered equals
114      */

115     public static boolean equals( final Configuration c1, final Configuration c2 )
116     {
117         return c1.getName().equals( c2.getName() )
118             && areValuesEqual( c1, c2 )
119             && areAttributesEqual( c1, c2 )
120             && areChildrenEqual( c1, c2 );
121     }
122
123     /**
124      * Return true if the children of both configurations are equal.
125      *
126      * @param c1 configuration1
127      * @param c2 configuration2
128      * @return true if the children of both configurations are equal.
129      */

130     private static boolean areChildrenEqual( final Configuration c1,
131                                              final Configuration c2 )
132     {
133         final Configuration[] kids1 = c1.getChildren();
134         final ArrayList JavaDoc kids2 = new ArrayList JavaDoc( Arrays.asList( c2.getChildren() ) );
135         if( kids1.length != kids2.size() )
136         {
137             return false;
138         }
139
140         for( int i = 0; i < kids1.length; i++ )
141         {
142             if( !findMatchingChild( kids1[ i ], kids2 ) )
143             {
144                 return false;
145             }
146         }
147
148         return kids2.isEmpty() ? true : false;
149     }
150
151     /**
152      * Return true if find a matching child and remove child from list.
153      *
154      * @param c the configuration
155      * @param matchAgainst the list of items to match against
156      * @return true if the found.
157      */

158     private static boolean findMatchingChild( final Configuration c,
159                                               final ArrayList JavaDoc matchAgainst )
160     {
161         final Iterator JavaDoc i = matchAgainst.iterator();
162         while( i.hasNext() )
163         {
164             if( equals( c, (Configuration)i.next() ) )
165             {
166                 i.remove();
167                 return true;
168             }
169         }
170
171         return false;
172     }
173
174     /**
175      * Return true if the attributes of both configurations are equal.
176      *
177      * @param c1 configuration1
178      * @param c2 configuration2
179      * @return true if the attributes of both configurations are equal.
180      */

181     private static boolean areAttributesEqual( final Configuration c1,
182                                                final Configuration c2 )
183     {
184         final String JavaDoc[] names1 = c1.getAttributeNames();
185         final String JavaDoc[] names2 = c2.getAttributeNames();
186         if( names1.length != names2.length )
187         {
188             return false;
189         }
190
191         for( int i = 0; i < names1.length; i++ )
192         {
193             final String JavaDoc name = names1[ i ];
194             final String JavaDoc value1 = c1.getAttribute( name, null );
195             final String JavaDoc value2 = c2.getAttribute( name, null );
196             if( !value1.equals( value2 ) )
197             {
198                 return false;
199             }
200         }
201
202         return true;
203     }
204
205     /**
206      * Return true if the values of two configurations are equal.
207      *
208      * @param c1 configuration1
209      * @param c2 configuration2
210      * @return true if the values of two configurations are equal.
211      */

212     private static boolean areValuesEqual( final Configuration c1,
213                                            final Configuration c2 )
214     {
215         final String JavaDoc value1 = c1.getValue( null );
216         final String JavaDoc value2 = c2.getValue( null );
217         return ( value1 == null && value2 == null )
218             || ( value1 != null && value1.equals( value2 ) );
219     }
220
221     /**
222      * Create an DOM {@link Element} from a {@link Configuration}
223      * object.
224      *
225      * @param document the DOM document
226      * @param configuration the configuration to convert
227      * @return the DOM Element
228      */

229     private static Element JavaDoc createElement( final Document JavaDoc document,
230                                           final Configuration configuration )
231     {
232         final Element JavaDoc element = document.createElement( configuration.getName() );
233
234         final String JavaDoc content = configuration.getValue( null );
235         if( null != content )
236         {
237             final Text JavaDoc child = document.createTextNode( content );
238             element.appendChild( child );
239         }
240
241         final String JavaDoc[] names = configuration.getAttributeNames();
242         for( int i = 0; i < names.length; i++ )
243         {
244             final String JavaDoc name = names[ i ];
245             final String JavaDoc value = configuration.getAttribute( name, null );
246             element.setAttribute( name, value );
247         }
248         final Configuration[] children = configuration.getChildren();
249         for( int i = 0; i < children.length; i++ )
250         {
251             final Element JavaDoc child = createElement( document, children[ i ] );
252             element.appendChild( child );
253         }
254         return element;
255     }
256 }
257
Popular Tags