1 50 51 package org.apache.excalibur.configuration; 52 53 import java.util.ArrayList ; 54 55 import org.apache.avalon.framework.configuration.Configuration; 56 import org.apache.avalon.framework.configuration.DefaultConfiguration; 57 58 64 public class ConfigurationUtil 65 { 66 72 public static String list( Configuration config ) 73 { 74 final StringBuffer buffer = new StringBuffer (); 75 list( buffer, " ", config ); 76 buffer.append( "\n" ); 77 return buffer.toString(); 78 } 79 80 88 public static void list( StringBuffer buffer, String lead, Configuration config ) 89 { 90 91 buffer.append( "\n" + lead + "<" + config.getName() ); 92 String [] 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 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 ce ) 127 { 128 buffer.append( "/>" ); 129 } 130 } 131 } 132 133 140 public static Configuration[] match( final Configuration config, 141 final String element, 142 final String attribute ) 143 { 144 return match( config, element, attribute, null ); 145 } 146 147 155 public static Configuration[] match( final Configuration config, 156 final String element, 157 final String attribute, 158 final String value ) 159 { 160 final ArrayList list = new ArrayList (); 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 v = children[ i ].getAttribute( attribute, null ); 172 173 if( v != null ) 174 { 175 if( ( value == null ) || v.equals( value ) ) 176 { 177 list.add( children[ i ] ); 179 } 180 } 181 } 182 } 183 184 return (Configuration[])list.toArray( new Configuration[ list.size() ] ); 185 } 186 187 196 public static Configuration matchFirstOccurance( 197 Configuration config, String element, String attribute, String value ) 198 { 199 return matchFirstOccurance( config, element, attribute, value, true ); 200 } 201 202 213 public static Configuration matchFirstOccurance( 214 Configuration config, String element, String attribute, String value, boolean create ) 215 { 216 Configuration[] children = config.getChildren( element ); 217 for( int i = 0; i < children.length; i++ ) 218 { 219 String v = children[ i ].getAttribute( attribute, null ); 220 if( v != null ) 221 { 222 if( ( value == null ) || v.equals( value ) ) 223 { 224 return children[ i ]; 226 } 227 } 228 } 229 230 return create ? new DefaultConfiguration( element, null ) : null; 231 } 232 233 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 |