1 17 package org.apache.excalibur.source; 18 19 import java.io.Serializable ; 20 import java.util.ArrayList ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.StringTokenizer ; 25 26 import org.apache.avalon.framework.configuration.Configuration; 27 import org.apache.avalon.framework.configuration.ConfigurationException; 28 import org.apache.avalon.framework.parameters.Parameters; 29 30 45 public final class SourceParameters 46 implements Serializable , Cloneable 47 { 48 49 private Map names = new HashMap ( 5 ); 50 51 54 private String parseName( String s ) 55 { 56 StringBuffer sb = new StringBuffer (); 57 for( int i = 0; i < s.length(); i++ ) 58 { 59 char c = s.charAt( i ); 60 switch( c ) 61 { 62 case '+': 63 sb.append( ' ' ); 64 break; 65 case '%': 66 try 67 { 68 sb.append( (char)Integer.parseInt( s.substring( i + 1, i + 3 ), 69 16 ) ); 70 i += 2; 71 } 72 catch( NumberFormatException e ) 73 { 74 throw new IllegalArgumentException (); 75 } 76 catch( StringIndexOutOfBoundsException e ) 77 { 78 String rest = s.substring( i ); 79 sb.append( rest ); 80 if( rest.length() == 2 ) 81 i++; 82 } 83 84 break; 85 default: 86 sb.append( c ); 87 break; 88 } 89 } 90 return sb.toString(); 91 } 92 93 99 public static SourceParameters create( Configuration conf ) 100 { 101 Configuration[] children = conf.getChildren(); 102 if( children != null && children.length > 0 ) 103 { 104 SourceParameters pars = new SourceParameters(); 105 String name; 106 String value; 107 for( int i = 0; i < children.length; i++ ) 108 { 109 name = children[ i ].getName(); 110 try 111 { 112 value = children[ i ].getValue(); 113 } 114 catch( ConfigurationException local ) 115 { 116 value = ""; } 118 pars.setParameter( name, value ); 119 } 120 return pars; 121 } 122 return null; 123 } 124 125 128 public SourceParameters() 129 { 130 } 131 132 135 public SourceParameters( String queryString ) 136 { 137 if( queryString != null ) 138 { 139 StringTokenizer st = new StringTokenizer ( queryString, "&" ); 140 while( st.hasMoreTokens() ) 141 { 142 String pair = st.nextToken(); 143 int pos = pair.indexOf( '=' ); 144 if( pos != -1 ) 145 { 146 setParameter( parseName( pair.substring( 0, pos ) ), 147 parseName( pair.substring( pos + 1, pair.length() ) ) ); 148 } 149 } 150 } 151 } 152 153 159 public void setParameter( String name, String value ) 160 { 161 ArrayList list; 162 if( names.containsKey( name ) == true ) 163 { 164 list = (ArrayList )names.get( name ); 165 } 166 else 167 { 168 list = new ArrayList ( 3 ); 169 names.put( name, list ); 170 } 171 list.add( value ); 172 } 173 174 180 public String getParameter( String name ) 181 { 182 if( names.containsKey( name ) == true ) 183 { 184 return (String )( (ArrayList )names.get( name ) ).get( 0 ); 185 } 186 return null; 187 } 188 189 196 public String getParameter( String name, String defaultValue ) 197 { 198 if( names.containsKey( name ) == true ) 199 { 200 return (String )( (ArrayList )names.get( name ) ).get( 0 ); 201 } 202 return defaultValue; 203 } 204 205 212 public int getParameterAsInteger( String name, int defaultValue ) 213 { 214 if( names.containsKey( name ) == true ) 215 { 216 return new Integer ( (String )( (ArrayList )names.get( name ) ).get( 0 ) ).intValue(); 217 } 218 return defaultValue; 219 } 220 221 228 public boolean getParameterAsBoolean( String name, boolean defaultValue ) 229 { 230 if( names.containsKey( name ) == true ) 231 { 232 return new Boolean ( (String )( (ArrayList )names.get( name ) ).get( 0 ) ).booleanValue(); 233 } 234 return defaultValue; 235 } 236 237 242 public boolean containsParameter( String name ) 243 { 244 return names.containsKey( name ); 245 } 246 247 253 public Iterator getParameterValues( String name ) 254 { 255 if( names.containsKey( name ) == true ) 256 { 257 ArrayList list = (ArrayList )names.get( name ); 258 return list.iterator(); 259 } 260 return null; 261 } 262 263 267 public Iterator getParameterNames() 268 { 269 return names.keySet().iterator(); 270 } 271 272 278 public Parameters getFirstParameters() 279 { 280 Parameters result = new Parameters(); 281 Iterator iter = this.getParameterNames(); 282 String parName; 283 while( iter.hasNext() ) 284 { 285 parName = (String )iter.next(); 286 result.setParameter( parName, this.getParameter( parName ) ); 287 } 288 return result; 289 } 290 291 298 public String getQueryString() 299 { 300 StringBuffer result = new StringBuffer (); 301 Iterator iter = this.names.keySet().iterator(); 302 Iterator listIterator; 303 String key; 304 String value; 305 boolean first = true; 306 while( iter.hasNext() == true ) 307 { 308 key = (String )iter.next(); 309 listIterator = ( (ArrayList )names.get( key ) ).iterator(); 310 while( listIterator.hasNext() == true ) 311 { 312 if( first == false ) result.append( '&' ); 313 value = (String )listIterator.next(); 314 result.append( key ).append( '=' ).append( value ); 315 first = false; 316 } 317 } 318 return ( result.length() == 0 ? null : result.toString() ); 319 } 320 321 328 public String getEncodedQueryString() 329 { 330 StringBuffer result = new StringBuffer (); 331 Iterator iter = this.names.keySet().iterator(); 332 Iterator listIterator; 333 String key; 334 String value; 335 boolean first = true; 336 while( iter.hasNext() == true ) 337 { 338 key = (String )iter.next(); 339 listIterator = ( (ArrayList )names.get( key ) ).iterator(); 340 while( listIterator.hasNext() == true ) 341 { 342 if( first == false ) result.append( '&' ); 343 value = (String )listIterator.next(); 344 result.append( key ).append( '=' ).append( SourceUtil.encode( value ) ); 345 first = false; 346 } 347 } 348 return ( result.length() == 0 ? null : result.toString() ); 349 } 350 351 354 public void add( SourceParameters parameters ) 355 { 356 if( null != parameters ) 357 { 358 Iterator names = parameters.getParameterNames(); 359 Iterator values; 360 String name; 361 String value; 362 while( names.hasNext() == true ) 363 { 364 name = (String )names.next(); 365 values = parameters.getParameterValues( name ); 366 while( values.hasNext() == true ) 367 { 368 value = (String )values.next(); 369 this.setParameter( name, value ); 370 } 371 } 372 } 373 } 374 375 378 public String toString() 379 { 380 StringBuffer buffer = new StringBuffer ( "SourceParameters: {" ); 381 Iterator names = this.getParameterNames(); 382 String name; 383 boolean firstName = true; 384 Iterator values; 385 String value; 386 boolean firstValue; 387 while( names.hasNext() == true ) 388 { 389 name = (String )names.next(); 390 if( firstName == false ) 391 { 392 buffer.append( ", " ); 393 } 394 else 395 { 396 firstName = false; 397 } 398 buffer.append( name ).append( " = (" ); 399 values = this.getParameterValues( name ); 400 firstValue = true; 401 while( values.hasNext() == true ) 402 { 403 value = (String )values.next(); 404 if( firstValue == false ) 405 { 406 buffer.append( ", " ); 407 } 408 else 409 { 410 firstValue = false; 411 } 412 buffer.append( value ); 413 } 414 buffer.append( ')' ); 415 } 416 buffer.append( '}' ); 417 return buffer.toString(); 418 } 419 420 423 public Object clone() 424 { 425 SourceParameters newObject = new SourceParameters(); 426 Iterator names = this.getParameterNames(); 427 Iterator values; 428 String name, value; 429 while( names.hasNext() ) 430 { 431 name = (String )names.next(); 432 values = this.getParameterValues( name ); 433 while( values.hasNext() ) 434 { 435 value = (String )values.next(); 436 newObject.setParameter( name, value ); 437 } 438 } 439 return newObject; 440 } 441 442 445 public boolean hasParameters() 446 { 447 return ( this.names.size() > 0 ); 448 } 449 450 454 public void setSingleParameterValue( String name, String value ) 455 { 456 this.removeParameter( name ); 457 this.setParameter( name, value ); 458 } 459 460 463 public void removeParameter( String name ) 464 { 465 if( this.names.containsKey( name ) ) 466 { 467 this.names.remove( name ); 468 } 469 } 470 } 471 | Popular Tags |