1 17 18 package org.apache.avalon.util.defaults ; 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.util.ArrayList ; 23 import java.util.Properties ; 24 import java.util.Enumeration ; 25 26 27 34 public class Defaults extends Properties 35 { 36 37 private final String [] m_singles ; 38 39 private final String [] m_enumerated ; 40 41 private final DefaultsFinder [] m_finders ; 42 43 44 48 49 56 public Defaults( String [] a_singles, String [] a_enumerated, 57 DefaultsFinder [] a_finders ) 58 { 59 m_finders = a_finders ; 60 m_singles = a_singles ; 61 m_enumerated = a_enumerated ; 62 63 for ( int ii = 0; ii < m_finders.length; ii++ ) 64 { 65 m_finders[ii].find( this ) ; 66 } 67 } 68 69 70 74 75 84 public String [] getEnumerated() 85 { 86 return m_enumerated ; 87 } 88 89 90 95 public DefaultsFinder[] getFinders() 96 { 97 return m_finders ; 98 } 99 100 101 106 public String [] getSingles() 107 { 108 return m_singles ; 109 } 110 111 112 118 public String [] getEnumerated( String a_base ) 119 { 120 ArrayList l_values = new ArrayList () ; 121 Enumeration l_list = keys() ; 122 123 while ( l_list.hasMoreElements() ) 124 { 125 String l_key = ( String ) l_list.nextElement() ; 126 if ( l_key.startsWith( a_base ) ) 127 { 128 l_values.add( getProperty( l_key ) ) ; 129 } 130 } 131 return ( String [] ) l_values.toArray( new String [0] ) ; 132 } 133 134 135 142 public boolean getBoolean( String a_key ) 143 { 144 String l_value = getProperty( a_key ) ; 145 l_value = l_value.trim().toLowerCase() ; 146 147 if ( l_value.equals( "1" ) || 148 l_value.equals( "on" ) || 149 l_value.equals( "yes" ) || 150 l_value.equals( "true" ) ) 151 { 152 return true ; 153 } 154 155 return false ; 156 } 157 158 159 163 164 173 public static void discover( Defaults a_defaults, Properties [] a_sources, 174 boolean a_haltOnDiscovery ) 175 { 176 if ( null == a_sources || null == a_defaults ) 177 { 178 return ; 179 } 180 181 184 String [] l_keys = a_defaults.getSingles() ; 185 for ( int ii = 0; ii < l_keys.length; ii++ ) 186 { 187 String l_key = l_keys[ii] ; 188 String l_value = discover( l_key, a_sources, a_haltOnDiscovery ) ; 189 190 if ( l_value != null ) 191 { 192 a_defaults.setProperty( l_key, l_value ) ; 193 } 194 } 195 196 199 l_keys = a_defaults.getEnumerated() ; 200 for ( int ii = 0; ii < l_keys.length; ii++ ) 201 { 202 String l_base = l_keys[ii] ; 203 204 for ( int jj = 0; jj < a_sources.length; jj++ ) 205 { 206 Enumeration l_list = a_sources[jj].propertyNames() ; 207 208 while ( l_list.hasMoreElements() ) 209 { 210 String l_key = ( String ) l_list.nextElement() ; 211 if ( ! l_key.startsWith( l_base ) ) 212 { 213 continue ; 214 } 215 216 String l_value = 217 discover( l_key, a_sources, a_haltOnDiscovery ) ; 218 219 if ( l_value != null ) 220 { 221 a_defaults.setProperty( l_key, l_value ) ; 222 } 223 } 224 } 225 } 226 } 227 228 229 240 public static String discover( String l_key, Properties [] a_sources, 241 boolean a_haltOnDiscovery ) 242 { 243 String l_retval = null ; 244 245 for( int ii = 0; ii < a_sources.length; ii++ ) 246 { 247 if ( a_sources[ii].containsKey( l_key ) ) 248 { 249 l_retval = a_sources[ii].getProperty( l_key ) ; 250 251 if ( a_haltOnDiscovery ) 252 { 253 break ; 254 } 255 } 256 } 257 258 return l_retval ; 259 } 260 261 262 286 public static void macroExpand( Properties a_expanded, 287 Properties [] a_optionals ) 288 { 289 if ( null == a_optionals ) 291 { 292 a_optionals = new Properties [ 0 ] ; 293 } 294 295 Enumeration l_list = a_expanded.propertyNames() ; 296 while ( l_list.hasMoreElements() ) 297 { 298 String l_key = ( String ) l_list.nextElement() ; 299 String l_macro = a_expanded.getProperty( l_key ) ; 300 301 int n = l_macro.indexOf( "${" ); 302 if( n < 0 ) 303 { 304 continue; 305 } 306 307 int m = l_macro.indexOf( "}", n+2 ); 308 if( m < 0 ) 309 { 310 continue; 311 } 312 313 final String symbol = l_macro.substring( n+2, m ); 314 315 if ( a_expanded.containsKey( symbol ) ) 316 { 317 final String value = a_expanded.getProperty( symbol ); 318 final String head = l_macro.substring( 0, n ); 319 final String tail = l_macro.substring( m+1 ); 320 final String resolved = head + value + tail; 321 322 a_expanded.put( l_key, resolved ) ; 323 continue ; 324 } 325 326 331 for ( int ii = 0; ii < a_optionals.length; ii++ ) 332 { 333 if ( a_optionals[ii].containsKey( symbol ) ) 334 { 335 String value = a_optionals[ii].getProperty( symbol ) ; 336 final String head = l_macro.substring( 0, n ); 337 final String tail = l_macro.substring( m+1 ); 338 final String resolved = head + value + tail; 339 340 a_expanded.put( l_key, resolved ) ; 341 break ; 342 } 343 } 344 } 345 } 346 347 356 public static Properties getStaticProperties( Class ref ) throws IOException 357 { 358 final Properties properties = new Properties (); 359 final String address = ref.toString().replace( '.','/' ); 360 final String path = address + ".properties"; 361 InputStream input = ref.getResourceAsStream( path ); 362 if( null != input ) 363 { 364 properties.load( input ); 365 } 366 return properties; 367 } 368 369 379 public static Properties getStaticProperties( Class ref, String path ) throws IOException 380 { 381 Properties bootstrap = new Properties (); 382 InputStream input = ref.getResourceAsStream( path ); 383 if( input == null ) 384 { 385 final String error = 386 "Internal error, unable to locate enbedded resource: " 387 + path 388 + " from the resource: " 389 + ref.getProtectionDomain().getCodeSource().getLocation(); 390 throw new IllegalStateException ( error ); 391 } 392 bootstrap.load( input ); 393 return bootstrap; 394 } 395 } 396 397 398 | Popular Tags |