KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > configuration > ConfigurationUtil


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

50
51 package org.apache.excalibur.configuration;
52
53 import java.util.ArrayList JavaDoc;
54
55 import org.apache.avalon.framework.configuration.Configuration;
56 import org.apache.avalon.framework.configuration.DefaultConfiguration;
57
58 /**
59  * General utility supporting static operations for generating string
60  * representations of a configuration suitable for debugging.
61  * @author Stephen McConnell <mcconnell@osm.net>
62  * @author <a HREF="mailto:proyal@apache.org">Peter Royal</a>
63  */

64 public class ConfigurationUtil
65 {
66     /**
67      * Returns a simple string representation of the the supplied configuration.
68      * @param config a configuration
69      * @return a simplified text representation of a configuration suitable
70      * for debugging
71      */

72     public static String JavaDoc list( Configuration config )
73     {
74         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
75         list( buffer, " ", config );
76         buffer.append( "\n" );
77         return buffer.toString();
78     }
79
80     /**
81      * populates a string buffer with an XML representation of a supplied configuration.
82      * @param buffer the string buffer
83      * @param lead padding offset
84      * @param config a configuration
85      * @return a simplified text representation of a configuration suitable
86      * for debugging
87      */

88     public static void list( StringBuffer JavaDoc buffer, String JavaDoc lead, Configuration config )
89     {
90
91         buffer.append( "\n" + lead + "<" + config.getName() );
92         String JavaDoc[] names = config.getAttributeNames();
93         if( names.length > 0 )
94         {
95             for( int i = 0; i < names.length; i++ )
96             {
97                 buffer.append( " "
98                                + names[ i ] + "=\""
99                                + config.getAttribute( names[ i ], "???" ) + "\"" );
100             }
101         }
102         Configuration[] children = config.getChildren();
103         if( children.length > 0 )
104         {
105             buffer.append( ">" );
106             for( int j = 0; j < children.length; j++ )
107             {
108                 list( buffer, lead + " ", children[ j ] );
109             }
110             buffer.append( "\n" + lead + "</" + config.getName() + ">" );
111         }
112         else
113         {
114             try
115             {
116                 String JavaDoc value = config.getValue();
117                 if( !value.equals( "" ) )
118                 {
119                     buffer.append( ">" + value + "</" + config.getName() + ">" );
120                 }
121                 else
122                 {
123                     buffer.append( "/>" );
124                 }
125             }
126             catch( Throwable JavaDoc ce )
127             {
128                 buffer.append( "/>" );
129             }
130         }
131     }
132
133     /**
134      * Return all occurance of a configuration child containing the supplied attribute name.
135      * @param config the configuration
136      * @param element the name of child elements to select from the configuration
137      * @param attribute the attribute name to filter (null will match any attribute name)
138      * @return an array of configuration instances matching the query
139      */

140     public static Configuration[] match( final Configuration config,
141                                          final String JavaDoc element,
142                                          final String JavaDoc attribute )
143     {
144         return match( config, element, attribute, null );
145     }
146
147     /**
148      * Return occurance of a configuration child containing the supplied attribute name and value.
149      * @param config the configuration
150      * @param element the name of child elements to select from the configuration
151      * @param attribute the attribute name to filter (null will match any attribute name )
152      * @param value the attribute value to match (null will match any attribute value)
153      * @return an array of configuration instances matching the query
154      */

155     public static Configuration[] match( final Configuration config,
156                                          final String JavaDoc element,
157                                          final String JavaDoc attribute,
158                                          final String JavaDoc value )
159     {
160         final ArrayList JavaDoc list = new ArrayList JavaDoc();
161         final Configuration[] children = config.getChildren( element );
162
163         for( int i = 0; i < children.length; i++ )
164         {
165             if( null == attribute )
166             {
167                 list.add( children[ i ] );
168             }
169             else
170             {
171                 String JavaDoc v = children[ i ].getAttribute( attribute, null );
172
173                 if( v != null )
174                 {
175                     if( ( value == null ) || v.equals( value ) )
176                     {
177                         // it's a match
178
list.add( children[ i ] );
179                     }
180                 }
181             }
182         }
183
184         return (Configuration[])list.toArray( new Configuration[ list.size() ] );
185     }
186
187     /**
188      * Return the first occurance of a configuration child containing the supplied attribute name
189      * and value or create a new empty configuration if no match found.
190      * @param config the configuration
191      * @param element the name of child elements to select from the configuration
192      * @param attribute the attribute name to filter
193      * @param value the attribute value to match (null will match any attribute value)
194      * @return a configuration instances matching the query or empty configuration
195      */

196     public static Configuration matchFirstOccurance(
197         Configuration config, String JavaDoc element, String JavaDoc attribute, String JavaDoc value )
198     {
199         return matchFirstOccurance( config, element, attribute, value, true );
200     }
201
202     /**
203      * Return the first occurance of a configuration child containing the supplied attribute
204      * name and value. If the supplied creation policy if TRUE and no match is found, an
205      * empty configuration instance is returned, otherwise a null will returned.
206      * @param config the configuration
207      * @param element the name of child elements to select from the configuration
208      * @param attribute the attribute name to filter
209      * @param value the attribute value to match (null will match any attribute value)
210      * @param create the creation policy if no match
211      * @return a configuration instances matching the query
212      */

213     public static Configuration matchFirstOccurance(
214         Configuration config, String JavaDoc element, String JavaDoc attribute, String JavaDoc value, boolean create )
215     {
216         Configuration[] children = config.getChildren( element );
217         for( int i = 0; i < children.length; i++ )
218         {
219             String JavaDoc v = children[ i ].getAttribute( attribute, null );
220             if( v != null )
221             {
222                 if( ( value == null ) || v.equals( value ) )
223                 {
224                     // it's a match
225
return children[ i ];
226                 }
227             }
228         }
229
230         return create ? new DefaultConfiguration( element, null ) : null;
231     }
232
233     /**
234      * Test to see if two Configuration's can be considered the same. Name, value, attributes
235      * and children are test. The <b>order</b> of children is not taken into consideration
236      * for equality.
237      *
238      * @param c1 Configuration to test
239      * @param c2 Configuration to test
240      * @return true if the configurations can be considered equals
241      */

242     public static boolean equals( final Configuration c1, final Configuration c2 )
243     {
244         return org.apache.avalon.framework.configuration.ConfigurationUtil.equals(c1, c2 );
245     }
246 }
247
Popular Tags