KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > components > util > ConfigUtil


1 /*
2  * Copyright (C) The Loom Group. All rights reserved.
3  *
4  * This software is published under the terms of the Loom
5  * Software License version 1.1, a copy of which has been included
6  * with this distribution in the LICENSE.txt file.
7  */

8 package org.codehaus.loom.components.util;
9
10 import java.util.Map JavaDoc;
11 import org.codehaus.spice.configkit.PropertyExpander;
12 import org.codehaus.dna.Configuration;
13 import org.codehaus.dna.ConfigurationException;
14 import org.codehaus.dna.impl.DefaultConfiguration;
15
16 /**
17  * @author Peter Donald
18  * @version $Revision: 1.2 $ $Date: 2004/05/01 12:48:33 $
19  */

20 public class ConfigUtil
21 {
22     public static Configuration expandValues( final Configuration input,
23                                               final Map JavaDoc data )
24         throws Exception JavaDoc
25     {
26         final PropertyExpander expander = new PropertyExpander();
27         return expandValues( expander, input, data );
28     }
29
30     private static Configuration expandValues( final PropertyExpander expander,
31                                                final Configuration input,
32                                                final Map JavaDoc data )
33         throws Exception JavaDoc
34     {
35         final DefaultConfiguration output =
36             new DefaultConfiguration( input.getName(),
37                                       input.getPath(),
38                                       input.getLocation() );
39         final String JavaDoc[] names = input.getAttributeNames();
40         for( int i = 0; i < names.length; i++ )
41         {
42             final String JavaDoc name = names[ i ];
43             final String JavaDoc value = input.getAttribute( name );
44             final String JavaDoc newValue = expander.expandValues( value, data );
45             output.setAttribute( name, newValue );
46         }
47
48         final Configuration[] children = input.getChildren();
49         for( int i = 0; i < children.length; i++ )
50         {
51             final Configuration child =
52                 expandValues( expander, children[ i ], data );
53             output.addChild( child );
54         }
55
56         final String JavaDoc content = input.getValue( null );
57         if( null != content )
58         {
59             final String JavaDoc newValue =
60                 expander.expandValues( content, data );
61             output.setValue( newValue );
62         }
63
64         return output;
65     }
66
67     public static void copy( final DefaultConfiguration newConfiguration,
68                              final Configuration configuration )
69         throws ConfigurationException
70     {
71         final Configuration[] children = configuration.getChildren();
72         for( int i = 0; i < children.length; i++ )
73         {
74             newConfiguration.addChild( children[ i ] );
75         }
76         final String JavaDoc[] names = configuration.getAttributeNames();
77         for( int i = 0; i < names.length; i++ )
78         {
79             final String JavaDoc name = names[ i ];
80             final String JavaDoc value = configuration.getAttribute( name );
81             newConfiguration.setAttribute( name, value );
82         }
83         final String JavaDoc content = configuration.getValue( null );
84         if( null != content )
85         {
86             newConfiguration.setValue( content );
87         }
88     }
89 }
90
Popular Tags