1 5 47 package com.teamkonzept.web; 48 49 import java.util.*; 50 import com.teamkonzept.lib.*; 51 import com.oroinc.text.regex.*; 52 import org.apache.log4j.Category; 53 54 60 public class TKParams extends TKHashtable 61 { 62 private static final Category cat = Category.getInstance(TKParams.class); 63 64 static boolean initialized = false; 65 static private final String patTrailer ="TK_"; 66 static private Pattern patParName; 67 static private Pattern patSubClass; 68 69 80 public TKParams() 81 { 82 if( !initialized ) init(); 83 initParams( TKHttp.getInterface().getParams() ); 84 } 85 86 99 public TKParams( TKHashtable pureParams ) 100 { 101 if( !initialized ) init(); 102 if( pureParams != null ) initParams( pureParams ); 103 } 104 105 118 public Enumeration classes() 119 { 120 return keys(); 121 } 122 123 138 public final TKHashtable getClass( Object classId ) 139 { 140 Object classHash = get( classId ); 141 if( classHash == null || !( classHash instanceof TKHashtable ) ) return null; 142 return (TKHashtable) classHash; 143 } 144 145 152 public boolean hasClass( Object classId ) 153 { 154 return getClass( classId ) != null; 155 } 156 157 172 public Object getObject( Object classId , Object key ) 173 { 174 TKHashtable classHash = getClass( classId ); 175 if( classHash == null ) return null; 176 return classHash.get( key ); 177 } 178 179 193 public String get( Object classId , Object key ) 194 { 195 Object result = getObject( classId, key ); 196 if( result == null || result instanceof TKVector ) return null; 197 return result.toString(); 198 } 199 200 217 public String get( Object classId , Object key, int idx ) 218 { 219 Object result = getObject( classId, key ); 220 if( result == null || !(result instanceof TKVector) ) return null; 221 return ((TKVector) result).get( idx ).toString(); 222 } 223 224 239 public TKVector getVector( Object classId , Object key ) 240 { 241 Object result = getObject( classId, key ); 242 if( result == null || !(result instanceof TKVector) ) return null; 243 return (TKVector) result; 244 } 245 246 262 public boolean has( Object classId , Object key ) 263 { 264 Object val = getObject( classId, key ); 265 if( val == null ) return false; 266 if( val instanceof TKVector ) return true; 267 return ( ! (val instanceof TKNull) ); 268 } 269 270 289 public boolean hasMultiple( Object classId , Object key ) 290 { 291 Object val = getObject( classId, key ); 292 if( val == null ) return false; 293 return ( val instanceof TKVector ); 294 } 295 296 314 public boolean defined( Object classId , Object key ) 315 { 316 Object val = get( classId, key ); 317 return ( val != null ); 318 } 319 320 336 public void extend( Object classId, Object key, Object val ) 337 { 338 TKHashtable classHash = getClass( classId ); 339 if( classHash == null ) { 340 classHash = new TKHashtable( 1 ); 341 classHash.put( key, val ); 342 put( classId, classHash ); 343 } 344 else { 345 classHash.extend( key, val ); 346 } 347 } 348 349 363 public void put( Object classId, Object key, Object val ) 364 { 365 TKHashtable classHash = getClass( classId ); 366 if( classHash == null ) { 367 classHash = new TKHashtable( 1 ); 368 classHash.put( key, val ); 369 put( classId, classHash ); 370 } 371 else { 372 classHash.put( key, val ); 373 } 374 } 375 376 391 protected void initParams( TKHashtable pureParams ) 392 { 393 Enumeration keys = pureParams.keys(); 394 395 PatternMatcher matcher = TKReg.getMatcher(); 396 397 while( keys.hasMoreElements() ) { 398 Object key = keys.nextElement(); 399 Object val = pureParams.get( key ); 400 401 if( matcher.matches( key.toString(), patParName ) ) { 403 MatchResult match = matcher.getMatch(); 405 if( match.groups() == 4 ) { 406 String third = match.group(3); 407 if( third != null && third.equalsIgnoreCase(".y") ) { 408 continue; 409 } 410 } 411 412 420 String classId = match.group(1).toUpperCase(); 422 423 StringTokenizer parts = new StringTokenizer( match.group(2), ";!" ); 426 427 String name = parts.nextToken().toUpperCase(); 429 430 441 extend( classId, name, val ); 442 443 while( parts.hasMoreTokens() ) { 459 if( matcher.matches( parts.nextToken(), patSubClass ) ){ 460 MatchResult classMatch = matcher.getMatch(); 461 String classVal = classMatch.group(2); 462 extend( 463 classMatch.group(1).toUpperCase(), 464 name, 465 ( classVal.length() == 0 ? (Object ) TKNull.NULL : (Object ) classVal ) 466 ); 467 } 468 } 469 } 470 else { 473 put( key, val ); 474 } 475 } 476 } 477 478 487 public TKHashtable extractClass( Object srcId, Object refId ) 488 { 489 TKHashtable refHash = getClass( refId ); 490 TKHashtable srcHash = getClass( srcId ); 491 492 if( srcHash == null || refHash == null || srcHash.size() == 0 || refHash.size() == 0 ) { 493 return new TKHashtable(); 494 } 495 496 TKHashtable res = new TKHashtable( refHash.size() ); 497 Enumeration keys = refHash.keys(); 498 499 while( keys.hasMoreElements() ) { 500 Object key = keys.nextElement(); 501 Object val = srcHash.get( key ); 502 if( val != null ) res.put( key, val ); 503 } 504 return res; 505 } 506 507 512 static void init() { 513 if( initialized ) return; 514 515 PatternCompiler compiler = TKReg.getCompiler(); 516 517 try { 518 patParName = compiler.compile( "^"+patTrailer+"(\\w+)\\[(.*)\\](\\.[xy])?$", Perl5Compiler.CASE_INSENSITIVE_MASK ); 519 patSubClass = compiler.compile("^(\\w+)\\b:?(.*)$" ); 520 } catch(MalformedPatternException e) { 521 cat.error("Bad pattern.", e); 522 } 523 524 initialized = true; 525 } 526 527 static { 529 init(); 530 } 531 532 } 533 534 | Popular Tags |