1 8 package org.codehaus.loom.components.util; 9 10 import java.util.Map ; 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 20 public class ConfigUtil 21 { 22 public static Configuration expandValues( final Configuration input, 23 final Map data ) 24 throws Exception 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 data ) 33 throws Exception 34 { 35 final DefaultConfiguration output = 36 new DefaultConfiguration( input.getName(), 37 input.getPath(), 38 input.getLocation() ); 39 final String [] names = input.getAttributeNames(); 40 for( int i = 0; i < names.length; i++ ) 41 { 42 final String name = names[ i ]; 43 final String value = input.getAttribute( name ); 44 final String 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 content = input.getValue( null ); 57 if( null != content ) 58 { 59 final String 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 [] names = configuration.getAttributeNames(); 77 for( int i = 0; i < names.length; i++ ) 78 { 79 final String name = names[ i ]; 80 final String value = configuration.getAttribute( name ); 81 newConfiguration.setAttribute( name, value ); 82 } 83 final String content = configuration.getValue( null ); 84 if( null != content ) 85 { 86 newConfiguration.setValue( content ); 87 } 88 } 89 } 90 | Popular Tags |