1 5 package com.teamkonzept.web; 6 7 import java.io.*; 8 import java.util.*; 9 10 import com.teamkonzept.lib.*; 11 12 public class TKForm { 13 14 public static final String PARAM = "PAR"; 16 public static final String REQUIRED = "REQ"; 17 public static final String TYPE = "TYPE"; 18 public static final String REQUIREDSELECTOR = "REQSEL"; 19 public static final String SWAP = "SWAP"; 20 21 public static final String CLASS_LIST [] = { 22 PARAM, REQUIRED, REQUIREDSELECTOR, TYPE, SWAP 23 }; 24 25 public static final String ERRORCASE = "ERROR"; 26 27 protected TKParams classes = new TKParams( null ); 28 protected TKHTMLTemplate template; 29 protected TKFormTypes formTypes; 30 31 protected final Hashtable errors = new Hashtable(); 32 33 38 public TKForm() 39 { 40 this( (TKParams)null ); 41 } 42 43 50 public TKForm( final TKFormTypes formTypes ) 51 { 52 this( null, formTypes ); 53 } 54 55 61 public TKForm( final TKParams params ) 62 { 63 this( params, (TKHTMLTemplate)null ); 64 } 65 66 73 public TKForm( final TKParams params, final TKFormTypes formTypes ) 74 { 75 this( params, null, formTypes ); 76 } 77 78 79 86 public TKForm( final TKParams params, final TKHTMLTemplate template ) 87 { 88 this( params, template, CLASS_LIST ); 89 } 90 91 99 public TKForm( final TKParams params, final TKHTMLTemplate template, final TKFormTypes formTypes ) 100 { 101 this( params, template, CLASS_LIST, formTypes ); 102 } 103 104 113 public TKForm( final TKParams params, final TKHTMLTemplate template, final String classList[] ) 114 { 115 this( params, template, classList, null ); 116 } 117 118 128 public TKForm( final TKParams params, final TKHTMLTemplate template, final String classList[], final TKFormTypes formTypes ) 129 { 130 if( params != null ) 131 setClasses( params, classList ); 132 this.template = template; 133 this.formTypes = ( formTypes == null ? new TKFormTypes() : formTypes ); 134 } 135 140 public TKTemplate getTemplate() 141 { 142 return template; 143 } 144 145 206 protected void setClasses( final TKParams params, final String classList[] ) 207 { 208 for( int i=0; i<classList.length; i++ ) { 209 String className = classList[i]; 210 TKHashtable classHash; 211 212 if( (classHash = params.getClass( className ) ) != null ) { 214 classes.put( className, classHash.clone() ); 215 } 216 else { 217 classes.put( className, new TKHashtable() ); 218 } 219 } 220 doSwapReqs(); 221 } 222 223 239 protected final TKHashtable getClass( final String classId ) 240 { 241 return classes.getClass( classId ); 242 } 243 244 258 public final TKHashtable getParams() 259 { 260 return getClass( PARAM ); 261 } 262 263 270 public final TKHashtable getRequired() 271 { 272 return getClass( REQUIRED ); 273 } 274 275 282 public final TKHashtable getRequiredSelector() 283 { 284 return getClass( REQUIREDSELECTOR ); 285 } 286 287 294 public final TKHashtable getSwapReqs() 295 { 296 return getClass( SWAP ); 297 } 298 299 318 public void doSwapReqs() { 319 final TKHashtable swap = getSwapReqs(); 320 final TKHashtable req = getRequired(); 321 final TKHashtable reqsel = getRequiredSelector(); 322 final TKHashtable params = getParams(); 323 324 final Enumeration swap_enum = swap.keys(); 325 while ( swap_enum.hasMoreElements() ) { 326 final Object key = swap_enum.nextElement(); 327 328 final Object swap_oval = swap.get( key ); 329 if ( swap_oval == null 330 || !( swap_oval instanceof String ) ) { 331 continue; 332 } 333 334 final String swap_val = (String )swap_oval; 335 final int eq_ndx = swap_val.indexOf( '=' ); 336 if ( eq_ndx >= 0 ) { 337 final String dest = swap_val.substring( 0, eq_ndx ); 338 339 final Object parm_oval = params.get( dest ); 340 if ( parm_oval == null 341 || !( parm_oval instanceof String ) ) { 342 continue; 343 } 344 345 final StringTokenizer tokenizer = 346 new StringTokenizer( swap_val.substring( 347 eq_ndx + 1, 348 swap_val.length() 349 ), "," 350 ); 351 final String parm_val = (String )parm_oval; 352 while ( tokenizer.hasMoreTokens() ) { 353 if ( parm_val.equals( tokenizer.nextToken() ) ) { 354 final Object reqsel_val = reqsel.get( key ); 355 final Object req_val = req.get( dest ); 356 357 if ( reqsel_val != null && req_val != null ) { 358 req.remove( dest ); 359 reqsel.remove( key ); 360 361 req.put( key, reqsel_val ); 362 reqsel.put( dest, req_val ); 363 } 364 break; 365 } 366 } 367 } else { 368 final String dest = swap_val; 369 370 final Object parm_oval = params.get( dest ); 371 if ( parm_oval == null 372 || !( parm_oval instanceof String ) ) { 373 continue; 374 } 375 376 final Object reqsel_val = reqsel.get( key ); 377 final Object req_val = req.get( dest ); 378 379 if ( reqsel_val != null && req_val != null ) { 380 req.remove( dest ); 381 reqsel.remove( key ); 382 383 req.put( key, reqsel_val ); 384 reqsel.put( dest, req_val ); 385 } 386 } 387 } 388 } 389 390 404 public final TKHashtable getTypes() 405 { 406 return getClass( TYPE ); 407 } 408 409 416 public void addErrorParams( final TKVector errorList, final String errorType ) 417 { 418 if( errorType != null ) { 419 420 errors.put( errorType, errorList ); 421 422 if( errorType.length() > 0 ) { 423 template.set( ERRORCASE + "_" + errorType, Boolean.TRUE ); 424 template.set( ERRORCASE, Boolean.TRUE ); 425 } 426 } 427 } 428 429 439 public void printErrorParams( final PrintStream out ) throws TKTemplateSyntaxException 440 { 441 442 TKListIterator prev_iter = template.getListIterator(); 443 final Enumeration error_enum = errors.keys(); 444 while ( error_enum.hasMoreElements() ) { 445 final String errorType = (String ) error_enum.nextElement(); 446 447 prev_iter = new ErrorListIterator( errorType, (TKVector) errors.get( errorType ), prev_iter ); 448 } 449 450 template.setListIterator( new ErrorListIterator( errors, prev_iter ) ); 451 } 452 453 460 public TKVector checkRequiredParams( final TKVector reqSelectors ) 461 { 462 final TKHashtable paramHash = getParams(); 463 final TKHashtable reqHash = getRequired(); 464 465 final TKVector missing = new TKVector(); 466 467 final Enumeration reqEnum = reqHash.keys(); 470 while( reqEnum.hasMoreElements() ) { 471 final Object key = reqEnum.nextElement(); 472 final Object reqVal = reqHash.get( key ); 473 final Object parVal = paramHash.get( key ); 474 475 if( reqVal == null || reqVal instanceof TKNull 476 || ( reqVal instanceof String && isEmptyString( (String ) reqVal ) ) ) { 477 if( parVal == null || parVal instanceof TKNull 478 || ( parVal instanceof String && isEmptyString( (String ) parVal ) ) ) { 479 missing.addElement( key ); 480 } 481 } 482 else if( reqSelectors != null && !reqSelectors.isEmpty() ){ 483 484 487 final TKVector reqConditions = new TKVector(); 488 if( reqVal instanceof TKVector ) { 489 final Enumeration reqValEnum = ((TKVector)reqVal).elements(); 490 while( reqValEnum.hasMoreElements() ) { 491 reqConditions.fill( new StringTokenizer( reqValEnum.nextElement().toString(), "," ) ); 492 } 493 } 494 else 495 reqConditions.fill( new StringTokenizer( reqVal.toString(), "," ) ); 496 497 500 final Enumeration condEnum = reqConditions.unique().elements(); 501 boolean noError = true; 502 while( condEnum.hasMoreElements() && noError ) { 503 final Enumeration selEnum = reqSelectors.elements(); 504 final String currCond = condEnum.nextElement().toString(); 505 while( selEnum.hasMoreElements() && noError ) { 506 final String selCond = selEnum.nextElement().toString(); 507 if( selCond.equalsIgnoreCase( currCond ) ) { 508 if( parVal == null || parVal instanceof TKNull 509 || ( parVal instanceof String && ((String ) parVal).length() == 0 ) ) { 510 missing.addElement( key ); 511 noError = false; 512 } 513 } 514 } 515 } 516 } 517 } 518 return missing; 519 } 520 521 529 public boolean checkRequired( final PrintStream out ) 530 throws TKTemplateSyntaxException { 531 if ( !checkRequired() ) { 532 printForm( out ); 533 return false; 534 } 535 return true; 536 } 537 538 545 public boolean checkRequired() throws TKTemplateSyntaxException 546 { 547 final TKHashtable reqSelectors = getRequiredSelector(); 548 final TKHashtable par = getParams(); 549 final TKVector reqList = new TKVector(); 550 551 final Enumeration reqKeys = reqSelectors.keys(); 552 while( reqKeys.hasMoreElements() ) { 553 final Object key = reqKeys.nextElement(); 554 final Object parVal = par.get( key ); 555 if( ! (parVal == null || parVal instanceof TKNull ) ) { 556 final StringTokenizer selEnum = new StringTokenizer( reqSelectors.get( key ).toString(), "," ); 557 while ( selEnum.hasMoreElements() ) { 558 reqList.addElement( selEnum.nextElement().toString() ); 559 } 560 } 561 } 562 563 return checkRequired( reqList.unique() ); 564 } 565 566 573 public boolean checkRequired( TKVector reqSelector ) throws TKTemplateSyntaxException 574 { 575 final TKVector missing = checkRequiredParams( reqSelector ); 576 if( !missing.isEmpty() ) { 577 addErrorParams( missing, REQUIRED ); 578 return false; 579 } 580 return true; 581 } 582 583 590 public boolean checkType( final PrintStream out ) 591 throws TKTemplateSyntaxException { 592 if ( !checkType() ) { 593 printForm( out ); 594 return false; 595 } 596 return true; 597 } 598 599 606 public boolean checkType() 607 { 608 final Hashtable typeHash = getTypes(); 609 final Hashtable valHash = getParams(); 610 final Enumeration parEnum = typeHash.keys(); 611 final TKVector errors = new TKVector(); 612 613 while ( parEnum.hasMoreElements() ) { 614 final String parName = (String ) parEnum.nextElement(); 615 final String type = (String ) typeHash.get( parName ); 616 617 final Object value = valHash.get( parName ); 618 619 if ( value == null 620 || !( value instanceof String ) 621 || !formTypes.checkType( (String ) value, type ) ) { 622 errors.addElement( parName ); 623 } 624 } 625 626 if ( !errors.isEmpty() ) { 627 addErrorParams( errors, TYPE ); 628 return false; 629 } else { 630 return true; 631 } 632 } 633 634 640 public boolean checkForm( PrintStream out ) throws TKTemplateSyntaxException 641 { 642 final boolean req = checkRequired(); 643 final boolean type = checkType(); 644 645 if ( !( req && type ) ) { 646 printForm( out ); 647 return false; 648 } else { 649 return true; 650 } 651 } 652 653 657 public void printEmptyForm( final PrintStream out ) throws TKTemplateSyntaxException 658 { 659 template.doTagSubstitution(); 660 template.doCleanup(); 661 template.printTemplate( out ); 662 } 663 664 667 public void printForm( final PrintStream out ) throws TKTemplateSyntaxException 668 { 669 final TKHashtable params = getParams(); 670 printErrorParams( out ); 671 template.set( params ); 672 printEmptyForm( out ); 673 } 674 675 678 public final void setLocale( final Locale locale ) { 679 formTypes.setLocale( locale ); 680 } 681 682 686 public final void setLocale( final int style, final Locale locale ) { 687 formTypes.setLocale( locale ); 688 formTypes.setStyle( style ); 689 } 690 691 694 public final void setFormTypes( final TKFormTypes formTypes ) { 695 this.formTypes = formTypes; 696 } 697 698 701 public final void setStyle( final int style ) { 702 formTypes.setStyle( style ); 703 } 704 705 public final static boolean isEmptyString( final String text ) { 706 if ( text == null ) { 707 return true; 708 } 709 final int len = text.length(); 710 for ( int i = 0; i < len; i++ ) { 711 if ( !Character.isWhitespace( text.charAt( i ) ) ) { 712 return false; 713 } 714 } 715 716 return true; 717 } 718 } 719 720 726 class ErrorListIterator implements TKListIterator { 727 728 public static final String ERRORLIST = "ERROR"; 729 730 protected String listName; 731 protected Enumeration typeEnum; 732 protected Hashtable errorList; 733 protected Enumeration errorEnum; 734 protected String errorType; 735 736 protected TKListIterator primaryIterator; 737 738 public ErrorListIterator( final Hashtable errorList, final TKListIterator primaryIterator ) 739 { 740 this( ERRORLIST, errorList, primaryIterator ); 741 } 742 743 public ErrorListIterator( final String errorType, 744 final Vector errorList, final TKListIterator primaryIterator ) 745 { 746 this( ERRORLIST + "_" + errorType, buildHash( errorType, errorList ), primaryIterator ); 747 748 } 749 750 private ErrorListIterator( final String listName, 751 final Hashtable errorList, final TKListIterator primaryIterator ) 752 { 753 this.listName = listName; 754 this.primaryIterator = primaryIterator; 755 756 this.errorList = errorList; 757 this.typeEnum = errorList.keys(); 758 this.errorType = (String ) typeEnum.nextElement(); 759 this.errorEnum = ((Vector) errorList.get( errorType )).elements(); 760 } 761 762 private static final Hashtable buildHash( final String errorType, final Vector errorList ) 763 { 764 final Hashtable typeList = new Hashtable( 1 ); 765 typeList.put( errorType, errorList ); 766 return typeList; 767 } 768 769 public boolean apply( final TKTemplate template, final int count, final String listName) 770 { 771 if( this.listName.equalsIgnoreCase( listName ) ) { 772 return substitute( template ); 773 } else if( primaryIterator == null ) { 774 return false; 775 } else { 776 return primaryIterator.apply( template, count, listName ); 777 } 778 } 779 780 protected boolean substitute( final TKTemplate template ) { 781 if ( errorEnum.hasMoreElements() ) { 782 final String par = (String ) errorEnum.nextElement(); 783 784 template.set( errorType + '_' + par, Boolean.TRUE ); 785 template.set( errorType, par ); 786 787 return true; 788 } else if ( typeEnum.hasMoreElements() ) { 789 errorType = (String ) typeEnum.nextElement(); 790 errorEnum = ((Vector) errorList.get( errorType )).elements(); 791 792 return substitute( template ); 793 } else { 794 return false; 795 } 796 } 797 } 798 799 | Popular Tags |