1 5 package com.teamkonzept.web; 6 7 import java.text.*; 8 import java.util.Date ; 9 import java.util.Locale ; 10 11 19 public class TKFormTypes { 20 21 private static final String EMPTY_STRING_MSG = "empty string"; 22 25 public static final String INTEGER = "INT"; 26 public static final String INT = INTEGER; 27 public static final String FLOAT = "FLOAT"; 28 public static final String DATE = "DATE"; 29 public static final String TIME = "TIME"; 30 public static final String MONEY = "MONEY"; 31 32 35 36 41 public static final int SHORT = DateFormat.SHORT; 42 47 public static final int MEDIUM = DateFormat.MEDIUM; 48 53 public static final int LONG = DateFormat.LONG; 54 57 public static final int DEFAULT_STYLE = SHORT; 58 59 private static final Locale DEFAULT_LOCALE = Locale.GERMANY; 61 private static final NumberFormat NUM_FORMAT = NumberFormat.getNumberInstance( DEFAULT_LOCALE ); 62 private static final NumberFormat CURRENCY_FORMAT = NumberFormat.getCurrencyInstance( DEFAULT_LOCALE ); 63 private static final DateFormat DATE_FORMAT = DateFormat.getDateInstance( DEFAULT_STYLE, 64 DEFAULT_LOCALE ); 65 private static final DateFormat TIME_FORMAT = DateFormat.getTimeInstance( DEFAULT_STYLE, 66 DEFAULT_LOCALE ); 67 private Locale locale; 68 private int style; 69 private NumberFormat float_format = null; 70 private NumberFormat currency_format = null; 71 private DateFormat date_format = null; 72 private DateFormat time_format = null; 73 74 78 public TKFormTypes() { 79 this( DEFAULT_STYLE, DEFAULT_LOCALE ); 80 } 81 82 89 public TKFormTypes( final Locale locale ) { 90 this( DEFAULT_STYLE, locale ); 91 } 92 93 106 public TKFormTypes( final int style, final Locale locale ) { 107 setStyle( style ); 108 setLocale( locale ); 109 } 110 111 117 public void setLocale( final Locale locale ) { 118 this.locale = locale; 119 120 this.float_format = null; 121 this.currency_format = null; 122 this.date_format = null; 123 this.time_format = null; 124 } 125 126 132 public void setStyle( final int style ) { 133 this.style = style; 134 135 this.date_format = null; 136 this.time_format = null; 137 } 138 139 159 public boolean checkType( final String value, final String type ) { 160 if ( type.equals( INTEGER ) ) { 161 return checkInteger( value ); 162 } else if ( type.equals( FLOAT ) ) { 163 return checkFloat( value ); 164 } else if ( type.equals( DATE ) ) { 165 return checkDate( value ); 166 } else if ( type.equals( TIME ) ) { 167 return checkTime( value ); 168 } else if ( type.equals( MONEY ) ) { 169 return checkMoney( value ); 170 } else { 171 return false; 172 } 173 } 174 175 189 public static final boolean checkType( final String value, final String type, 190 final Locale locale ) { 191 return checkType( value, type, DEFAULT_STYLE, locale ); 192 } 193 194 209 public static boolean checkType( final String value, final String type, 210 final int style, final Locale locale ) { 211 if ( type.equals( INTEGER ) ) { 212 return checkInteger( value ); 213 } else if ( type.equals( FLOAT ) ) { 214 return checkFloat( value, locale ); 215 } else if ( type.equals( DATE ) ) { 216 return checkDate( value, style, locale ); 217 } else if ( type.equals( TIME ) ) { 218 return checkTime( value, style, locale ); 219 } else if ( type.equals( MONEY ) ) { 220 return checkMoney( value, locale ); 221 } else { 222 return false; 223 } 224 } 225 226 239 public static final boolean checkTypeDef( final String value, final String type ) { 240 return checkType( value, type, DEFAULT_STYLE, DEFAULT_LOCALE ); 241 } 242 243 244 254 public final static boolean checkInteger( final String value ) { 255 try { 256 asInteger( value ); 257 } catch( ParseException e ) { 258 return false; 259 } 260 261 return true; 262 } 263 264 274 public static int asInteger( final String value ) 275 throws ParseException { 276 if ( value.length() == 0 ) 277 throw new ParseException( EMPTY_STRING_MSG, 0 ); 278 279 final String buf = value.trim(); 280 281 try { 282 if ( buf.charAt( 0 ) == '+' ) { 283 return Integer.parseInt( buf.substring( 1 ) ); 284 } else { 285 return Integer.parseInt( buf ); 286 } 287 } catch ( NumberFormatException e ) { 288 throw new ParseException( e.getMessage(), 0 ); 289 } 290 } 291 292 301 public final boolean checkFloat( final String value ) { 302 if ( float_format == null ) { 303 float_format = NumberFormat.getNumberInstance( locale ); 304 } 305 306 return TKFormTypes.checkFloat( value, float_format ); 307 } 308 309 319 public final static boolean checkFloat( final String value, 320 final Locale locale ) { 321 try { 322 asFloat( value, locale ); 323 } catch( ParseException e ) { 324 return false; 325 } 326 327 return true; 328 } 329 330 341 public final static boolean checkFloat( final String value, 342 final NumberFormat format ) { 343 try { 344 asFloat( value, format ); 345 } catch( ParseException e ) { 346 return false; 347 } 348 349 return true; 350 } 351 352 361 public final static boolean checkFloatDef( final String value ) { 362 return checkFloat( value, DEFAULT_LOCALE ); 363 } 364 365 366 376 public final double asFloat( final String value ) 377 throws ParseException { 378 if ( float_format == null ) { 379 float_format = NumberFormat.getNumberInstance( locale ); 380 } 381 return TKFormTypes.asFloat( value, float_format ); 382 } 383 384 396 public static final double asFloat( final String value, 397 final Locale locale ) 398 throws ParseException { 399 if ( locale.equals( DEFAULT_LOCALE ) ) { 400 return asFloat( value, NUM_FORMAT ); 401 } else { 402 return asFloat( value, NumberFormat.getNumberInstance( locale ) ); 403 } 404 } 405 406 418 public static double asFloat( final String value, 419 final NumberFormat format ) 420 throws ParseException { 421 if ( value.length() == 0 ) 422 throw new ParseException( EMPTY_STRING_MSG, 0 ); 423 424 final String buf; 425 if ( format instanceof DecimalFormat ) { 426 buf = skipPrefix( value.trim(), '+' ); 427 if ( !checkNumber( buf, (DecimalFormat) format ) ) { 428 throw new ParseException( "unable to parse number", 0 ); 429 } 430 } else { 431 buf = value.trim(); 432 } 433 434 return format.parse( buf ).doubleValue(); 435 } 436 437 448 public static final double asFloatDef( final String value ) 449 throws ParseException { 450 return asFloat( value, NUM_FORMAT ); 451 } 452 453 454 private static final String skipPrefix( final String value, final char ch ) { 455 if ( value.charAt( 0 ) == ch ) { 456 return value.substring( 1 ); 457 } else { 458 return value; 459 } 460 } 461 462 private static boolean checkNumber( final String value, final DecimalFormat format ) { 463 final DecimalFormatSymbols symbols = format.getDecimalFormatSymbols(); 464 final int len = value.length(); 465 final char grp_sep = symbols.getGroupingSeparator(); 466 final char dec_sep = symbols.getDecimalSeparator(); 467 468 boolean fraction = false; 469 int i = 0; 470 char ch = value.charAt( 0 ); 471 if ( i < len && ch == symbols.getMinusSign() ) 472 i++; 473 while ( i < len ) { 474 ch = value.charAt( i ); 475 if ( !Character.isDigit( ch ) && ch != grp_sep ) { 476 if ( ch == dec_sep && !fraction) { 477 fraction = true; 478 } else { 479 return false; 480 } 481 } 482 i += 1; 483 } 484 return true; 485 } 486 487 496 public final boolean checkDate( final String value ) { 497 if ( date_format == null ) { 498 date_format = DateFormat.getDateInstance( style, locale ); 499 } 500 return TKFormTypes.checkDate( value, date_format ); 501 } 502 503 513 public final static boolean checkDate( final String value, 514 final Locale locale ) { 515 return checkDate( value, DEFAULT_STYLE, locale ); 516 } 517 518 529 public final static boolean checkDate( final String value, 530 final int style, final Locale locale ) { 531 try { 532 asDate( value, style, locale ); 533 } catch( ParseException e ) { 534 return false; 535 } 536 537 return true; 538 } 539 540 551 public final static boolean checkDate( final String value, 552 final DateFormat format ) { 553 try { 554 asDate( value, format ); 555 } catch( ParseException e ) { 556 return false; 557 } 558 559 return true; 560 } 561 562 571 public final static boolean checkDateDef( final String value ) { 572 return checkDate( value, DATE_FORMAT ); 573 } 574 575 586 public final java.util.Date asDate( final String value ) 587 throws ParseException { 588 if ( date_format == null ) { 589 date_format = DateFormat.getDateInstance( style, locale ); 590 } 591 return TKFormTypes.asDate( value, date_format ); 592 } 593 594 607 public static final java.util.Date asDate( final String value, 608 final Locale locale ) 609 throws ParseException { 610 return asDate( value, DEFAULT_STYLE, locale ); 611 } 612 613 627 public static final java.util.Date asDate( final String value, 628 final int style, final Locale locale ) 629 throws ParseException { 630 final DateFormat format = ( locale.equals( DEFAULT_LOCALE ) && style == DEFAULT_STYLE 631 ? DATE_FORMAT 632 : DateFormat.getDateInstance( style, locale ) 633 ); 634 635 return asDate( value, format ); 636 } 637 638 651 public static java.util.Date asDate( final String value, 652 final DateFormat format ) 653 throws ParseException { 654 if ( value.length() == 0 ) 655 throw new ParseException( EMPTY_STRING_MSG, 0 ); 656 657 return format.parse( value.trim() ); 658 } 659 671 public static final java.util.Date asDateDef( final String value ) 672 throws ParseException { 673 return asDate( value, DATE_FORMAT ); 674 } 675 676 685 public final boolean checkTime( final String value ) { 686 if ( time_format == null ) { 687 time_format = DateFormat.getTimeInstance( style, locale ); 688 } 689 return TKFormTypes.checkTime( value, time_format ); 690 } 691 692 702 public final static boolean checkTime( final String value, 703 final Locale locale ) { 704 return checkTime( value, DEFAULT_STYLE, locale ); 705 } 706 707 718 public final static boolean checkTime( final String value, 719 final int style, final Locale locale ) { 720 try { 721 asTime( value, style, locale ); 722 } catch( ParseException e ) { 723 return false; 724 } 725 726 return true; 727 } 728 729 740 public final static boolean checkTime( final String value, 741 final DateFormat format ) { 742 try { 743 asTime( value, format ); 744 } catch( ParseException e ) { 745 return false; 746 } 747 748 return true; 749 } 750 751 760 public final static boolean checkTimeDef( final String value ) { 761 return checkTime( value, TIME_FORMAT ); 762 } 763 764 public final java.util.Date asTime( final String value ) 765 throws ParseException { 766 if ( time_format == null ) { 767 time_format = DateFormat.getTimeInstance( style, locale ); 768 } 769 return TKFormTypes.asTime( value, time_format ); 770 } 771 772 public static final java.util.Date asTime( final String value, 773 final Locale locale ) 774 throws ParseException { 775 return asTime( value, DEFAULT_STYLE, locale ); 776 } 777 778 public static final java.util.Date asTime( final String value, 779 final int style, final Locale locale ) 780 throws ParseException { 781 final DateFormat format = ( locale.equals( DEFAULT_LOCALE ) && style == DEFAULT_STYLE 782 ? TIME_FORMAT 783 : DateFormat.getTimeInstance( style, locale ) 784 ); 785 786 return asTime( value, format ); 787 } 788 789 public static java.util.Date asTime( final String value, 790 final DateFormat format ) 791 throws ParseException { 792 if ( value.length() == 0 ) 793 throw new ParseException( EMPTY_STRING_MSG, 0 ); 794 795 return format.parse( value.trim() ); 796 } 797 798 public static final java.util.Date asTimeDef( final String value ) 799 throws ParseException { 800 return asTime( value, TIME_FORMAT ); 801 } 802 803 812 public final boolean checkMoney( final String value ) { 813 if ( currency_format == null ) { 814 currency_format = NumberFormat.getCurrencyInstance( locale ); 815 } 816 return TKFormTypes.checkMoney( value, currency_format ); 817 } 818 819 829 public final static boolean checkMoney( final String value, 830 final Locale locale ) { 831 try { 832 asMoney( value, locale ); 833 } catch( ParseException e ) { 834 return false; 835 } 836 837 return true; 838 } 839 840 851 public final static boolean checkMoney( final String value, 852 final NumberFormat format ) { 853 try { 854 asMoney( value, format ); 855 } catch( ParseException e ) { 856 return false; 857 } 858 859 return true; 860 } 861 862 871 public final static boolean checkMoneyDef( final String value ) { 872 return checkMoney( value, DEFAULT_LOCALE ); 873 } 874 875 885 public final double asMoney( final String value ) 886 throws ParseException { 887 if ( currency_format == null ) { 888 currency_format = NumberFormat.getCurrencyInstance( locale ); 889 } 890 return TKFormTypes.asMoney( value, currency_format ); 891 } 892 893 905 public static final double asMoney( final String value, 906 final Locale locale ) 907 throws ParseException { 908 if ( locale.equals( DEFAULT_LOCALE ) ) { 909 return asMoney( value, CURRENCY_FORMAT ); 910 } else { 911 return asMoney( value, NumberFormat.getCurrencyInstance( locale ) ); 912 } 913 } 914 915 927 public static double asMoney( final String value, 928 final NumberFormat format ) 929 throws ParseException { 930 if ( value.length() == 0 ) 931 throw new ParseException( EMPTY_STRING_MSG, 0 ); 932 933 final String buf; 934 if ( format instanceof DecimalFormat ) { 935 buf = skipPrefix( value.trim(), '+' ); 936 if ( !checkNumber( buf, (DecimalFormat) format ) ) { 937 throw new ParseException( "unable to parse number", 0 ); 938 } 939 } else { 940 buf = value.trim(); 941 } 942 943 return format.parse( buf ).doubleValue(); 944 } 945 946 957 public static final double asMoneyDef( final String value ) 958 throws ParseException { 959 return asMoney( value, CURRENCY_FORMAT ); 960 } 961 962 } 965 966 | Popular Tags |