KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > teamkonzept > web > TKFormTypes


1 /*
2  * $Header: /cvsroot/webman-cms/source/webman/com/teamkonzept/web/TKFormTypes.java,v 1.7 2002/01/11 16:00:19 mischa Exp $
3  *
4  */

5 package com.teamkonzept.web;
6
7 import java.text.*;
8 import java.util.Date JavaDoc;
9 import java.util.Locale JavaDoc;
10
11 /**
12  * Die Klasse TKFormTypes dient der Sprachabhängigen Typüberprüfung
13  * und -umwandlung von (HTML-)Formparametern, d.h. von Parametern, die in
14  * Textform gegeben sind.
15  *
16  * @author Carsten
17  * @version $Revision: 1.7 $
18  */

19 public class TKFormTypes {
20
21     private static final String JavaDoc EMPTY_STRING_MSG = "empty string";
22     /*
23      * Types
24      */

25     public static final String JavaDoc INTEGER = "INT";
26     public static final String JavaDoc INT = INTEGER;
27     public static final String JavaDoc FLOAT = "FLOAT";
28     public static final String JavaDoc DATE = "DATE";
29     public static final String JavaDoc TIME = "TIME";
30     public static final String JavaDoc MONEY = "MONEY";
31
32     /*
33      * Styles
34      */

35
36     /**
37      * Formatstil, für ein sehr kompaktes Datums- und Zeitformat
38      *
39      * @see java.text.DateFormat#SHORT
40      */

41     public static final int SHORT = DateFormat.SHORT;
42     /**
43      * Formatstil, für ein mitel-kompaktes Datums- und Zeitformat
44      *
45      * @see java.text.DateFormat#MEDIUM
46      */

47     public static final int MEDIUM = DateFormat.MEDIUM;
48     /**
49      * Formatstil, für ein langes Datums- und Zeitformat.
50      *
51      * @see java.text.DateFormat#LONG
52      */

53     public static final int LONG = DateFormat.LONG;
54     /**
55      * Der default Formatstil, für die Datums- und Zeitdarstellung.
56      */

57     public static final int DEFAULT_STYLE = SHORT;
58     
59     // private static final ParsePosition FRACTION_FIELD = new ParsePosition( NumberFormat.FRACTION_FIELD );
60
private static final Locale JavaDoc 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 JavaDoc 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     /**
75      * Konstruiert ein neues TKFormTypes-Objekt für den default
76      * Sprachraum und Formatstil.
77      */

78     public TKFormTypes() {
79         this( DEFAULT_STYLE, DEFAULT_LOCALE );
80     }
81
82     /**
83      * Konstruiert ein neues TKFormTypes-Objekt für den gegebenen
84      * Sprachraum und dem default Formatstil.
85      *
86      * @param locale gibt den Sprachraum an, in dessen Kontext die
87      * Typen überprüft und umgewandelt werden sollen.
88      */

89     public TKFormTypes( final Locale JavaDoc locale ) {
90         this( DEFAULT_STYLE, locale );
91     }
92
93     /**
94      * Konstruiert ein neues TKFormTypes-Objekt für den gebenenen
95      * Sprachraum und Formatstil.
96      *
97      * @param style gibt den Formatstil an, in dessen Kontext die
98      * Typen überprüft und umgewandelt werden sollen.
99      * @param locale gibt den Sprachraum an, in dessen Kontext die
100      * Typen überprüft und umgewandelt werden sollen.
101      *
102      * @see com.teamkonzept.web.TKFormTypes#SHORT
103      * @see com.teamkonzept.web.TKFormTypes#MEDIUM
104      * @see com.teamkonzept.web.TKFormTypes#LONG
105      */

106     public TKFormTypes( final int style, final Locale JavaDoc locale ) {
107         setStyle( style );
108         setLocale( locale );
109     }
110
111     /**
112      * Setzt den Sprachraum in dessen Kontext die Typen überprüft
113      * und umgewandelt werden sollen.
114      *
115      * @param locale gibt den Sprachraum an
116      */

117     public void setLocale( final Locale JavaDoc 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     /**
127      * Setzt den Formatstil an, in dessen Kontext die Typen überprüft
128      * und umgewandelt werden sollen.
129      *
130      * @param style gibt den Formatstil an
131      */

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     /**
140      * Überprüft, ob der gegebene String sich in einen bestimmten
141      * Typen umwandeln läßt. Die Typüberprüfung liefert in
142      * vielen Fällen selbst dann false (=nicht interpretierbar) zurück,
143      * wenn ein Teilstring im Sinne des Typs interpretierbar wäre.
144      * Bsp.: checkType( "1e2", TKFormTypes.FLOAT ) == false, auch wenn
145      * der String als 1.0 interpretierbar gewesen wäre.
146      *
147      * @param value der zu überprüfende String
148      * @param type Typ, auf den der String hin überpr&uumlft werden soll
149      *
150      * @return true, falls value, sich als vom Typen type interpretieren läßt;
151      * false, sonst
152      *
153      * @see com.teamkonzept.web.TKFormTypes#INT
154      * @see com.teamkonzept.web.TKFormTypes#FLOAT
155      * @see com.teamkonzept.web.TKFormTypes#DATE
156      * @see com.teamkonzept.web.TKFormTypes#TIME
157      * @see com.teamkonzept.web.TKFormTypes#MONEY
158      */

159     public boolean checkType( final String JavaDoc value, final String JavaDoc 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     /**
176      * Überprüft, ob der gegebene String sich in einen bestimmten
177      * Typen umwandeln läßt. Es wird bei der Überprüfung
178      * der gegebene Sprachraum und der default Formatstil verwendet.
179      *
180      * @param value der zu überprüfende String
181      * @param type Typ, auf den der String hin überpr&uumlft werden soll
182      * @param locale gibt den Sprachraum an
183      *
184      * @return true, falls value, sich als vom Typen type interpretieren läßt;
185      * false, sonst
186      *
187      * @see com.teamkonzept.web.TKFormTypes#checkType( String, String )
188      */

189     public static final boolean checkType( final String JavaDoc value, final String JavaDoc type,
190                                            final Locale JavaDoc locale ) {
191         return checkType( value, type, DEFAULT_STYLE, locale );
192     }
193
194     /**
195      * Überprüft, ob der gegebene String sich in einen bestimmten
196      * Typen umwandeln läßt. Es wird bei der Überprüfung
197      * der gegebenen Sprachraum und Formatstil verwendet.
198      *
199      * @param value der zu überprüfende String
200      * @param type Typ, auf den der String hin überpr&uumlft werden soll
201      * @param style gibt den Formatstil an
202      * @param locale gibt den Sprachraum an
203      *
204      * @return true, falls value, sich als vom Typen type interpretieren läßt;
205      * false, sonst
206      *
207      * @see com.teamkonzept.web.TKFormTypes#checkType( String, String )
208      */

209     public static boolean checkType( final String JavaDoc value, final String JavaDoc type,
210                                      final int style, final Locale JavaDoc 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     /**
227      * Überprüft, ob der gegebene String sich in einen bestimmten
228      * Typen umwandeln läßt. Es wird bei der Überprüfung
229      * der default Sprachraum und Formatstil verwendet.
230      *
231      * @param value der zu überprüfende String
232      * @param type Typ, auf den der String hin überpr&uumlft werden soll
233      *
234      * @return true, falls value, sich als vom Typen type interpretieren läßt;
235      * false, sonst
236      *
237      * @see com.teamkonzept.web.TKFormTypes#checkType( String, String )
238      */

239     public static final boolean checkTypeDef( final String JavaDoc value, final String JavaDoc type ) {
240         return checkType( value, type, DEFAULT_STYLE, DEFAULT_LOCALE );
241     }
242
243
244     /**
245      * Überprüft, ob sich der gegebene String als Integer
246      * interpretieren läszlig;t.
247      *
248      * @param value der zu überprüfende String
249      *
250      * @return true, falls value, sich als Integer interpretieren läßt;
251      * false, sonst
252      *
253      */

254     public final static boolean checkInteger( final String JavaDoc value ) {
255         try {
256             asInteger( value );
257         } catch( ParseException e ) {
258             return false;
259         }
260         
261         return true;
262     }
263     
264     /**
265      * Wandelt den gegebenen String in einen Integer-Wert um
266      * und liefert diesen zurück.
267      *
268      * @param value der umzuwandelnde String
269      *
270      * @return value als int-Wert.
271      *
272      * @exception ParseException falls value nicht als Integer-Wert interpretierbar ist.
273      */

274     public static int asInteger( final String JavaDoc value )
275                                         throws ParseException {
276         if ( value.length() == 0 )
277             throw new ParseException( EMPTY_STRING_MSG, 0 );
278                                                                     
279         final String JavaDoc 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 JavaDoc e ) {
288             throw new ParseException( e.getMessage(), 0 );
289         }
290     }
291
292     /**
293      * Überprüft, ob sich der gegebene String als Fließkommazahl
294      * interpretieren läszlig;t.
295      *
296      * @param value der zu überprüfende String
297      *
298      * @return true, falls value, sich als Flie&sizlig;kommazahl interpretieren läßt;
299      * false, sonst
300      */

301     public final boolean checkFloat( final String JavaDoc value ) {
302         if ( float_format == null ) {
303             float_format = NumberFormat.getNumberInstance( locale );
304         }
305         
306         return TKFormTypes.checkFloat( value, float_format );
307     }
308     
309     /**
310      * Überprüft, ob sich der gegebene String für den
311      * gegebenen Sprachraum als Fließkommazahl interpretieren läszlig;t.
312      *
313      * @param value der zu überprüfende String
314      * @param locale gibt den Sprachraum an
315      *
316      * @return true, falls value, sich als Flie&sizlig;kommazahl interpretieren läßt;
317      * false, sonst
318      */

319     public final static boolean checkFloat( final String JavaDoc value,
320                                             final Locale JavaDoc locale ) {
321         try {
322             asFloat( value, locale );
323         } catch( ParseException e ) {
324             return false;
325         }
326         
327         return true;
328     }
329
330     /**
331      * Überprüft, ob sich der gegebene String im Sinne des
332      * gegebenen NumberFormat-Objektes als Fließkommazahl interpretieren
333      * läszlig;t.
334      *
335      * @param value der zu überprüfende String
336      * @param format Formatobjekt für Fließkommazahlen
337      *
338      * @return true, falls value, sich als Flie&sizlig;kommazahl interpretieren läßt;
339      * false, sonst
340      */

341     public final static boolean checkFloat( final String JavaDoc 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     /**
353      * Überprüft, ob sich der gegebene String für den
354      * default Sprachraum als Fließkommazahl interpretieren läszlig;t.
355      *
356      * @param value der zu überprüfende String
357      *
358      * @return true, falls value, sich als Flie&sizlig;kommazahl interpretieren läßt;
359      * false, sonst
360      */

361     public final static boolean checkFloatDef( final String JavaDoc value ) {
362         return checkFloat( value, DEFAULT_LOCALE );
363     }
364
365     
366     /**
367      * Wandelt den gegebenen String in einen Double-Wert um
368      * und liefert diesen zurück.
369      *
370      * @param value der umzuwandelnde String
371      *
372      * @return value als double-Wert.
373      *
374      * @exception ParseException falls value nicht als Fließkommazahl interpretierbar ist.
375      */

376     public final double asFloat( final String JavaDoc 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     /**
385      * Wandelt den gegebenen String in einen Double-Wert um
386      * und liefert diesen zurück. Bei der Umwandlung ist der
387      * gegebene Sprachraum maßgebend.
388      *
389      * @param value der umzuwandelnde String
390      * @param locale gibt den Sprachraum an
391      *
392      * @return value als double-Wert.
393      *
394      * @exception ParseException falls value nicht als Fließkommazahl interpretierbar ist.
395      */

396     public static final double asFloat( final String JavaDoc value,
397                                         final Locale JavaDoc 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     /**
407      * Wandelt den gegebenen String in einen Double-Wert um
408      * und liefert diesen zurück. Bei der Umwandlung ist das
409      * gegebene NumberFormat-Objekt maßgebend.
410      *
411      * @param value der umzuwandelnde String
412      * @param format Formatobjekt für Fließkommazahlen
413      *
414      * @return value als double-Wert.
415      *
416      * @exception ParseException falls value nicht als Fließkommazahl interpretierbar ist.
417      */

418     public static double asFloat( final String JavaDoc 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 JavaDoc 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     /**
438      * Wandelt den gegebenen String in einen Double-Wert um
439      * und liefert diesen zurück. Bei der Umwandlung ist der
440      * default Sprachraum maßgebend.
441      *
442      * @param value der umzuwandelnde String
443      *
444      * @return value als double-Wert.
445      *
446      * @exception ParseException falls value nicht als Fließkommazahl interpretierbar ist.
447      */

448     public static final double asFloatDef( final String JavaDoc value )
449                                             throws ParseException {
450         return asFloat( value, NUM_FORMAT );
451     }
452
453     
454     private static final String JavaDoc skipPrefix( final String JavaDoc 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 JavaDoc 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     /**
488      * &Uuml;berpr&uuml;ft, ob sich der gegebene String als Datum
489      * interpretieren l&auml;szlig;t.
490      *
491      * @param value der zu &uuml;berpr&uuml;fende String
492      *
493      * @return true, falls value, sich als Datum interpretieren l&auml;&szlig;t;
494      * false, sonst
495      */

496     public final boolean checkDate( final String JavaDoc value ) {
497         if ( date_format == null ) {
498             date_format = DateFormat.getDateInstance( style, locale );
499         }
500         return TKFormTypes.checkDate( value, date_format );
501     }
502     
503     /**
504      * &Uuml;berpr&uuml;ft, ob sich der gegebene String f&uuml;r den
505      * gegebenen Sprachraum und default Formatstil als Datum interpretieren l&auml;szlig;t.
506      *
507      * @param value der zu &uuml;berpr&uuml;fende String
508      * @param locale gibt den Sprachraum an
509      *
510      * @return true, falls value, sich als Datum interpretieren l&auml;&szlig;t;
511      * false, sonst
512      */

513     public final static boolean checkDate( final String JavaDoc value,
514                                            final Locale JavaDoc locale ) {
515         return checkDate( value, DEFAULT_STYLE, locale );
516     }
517
518     /**
519      * &Uuml;berpr&uuml;ft, ob sich der gegebene String f&uuml;r den
520      * gegebenen Sprachraum und Formatstil als Datum interpretieren l&auml;szlig;t.
521      *
522      * @param value der zu &uuml;berpr&uuml;fende String
523      * @param locale gibt den Sprachraum an
524      * @param style gibt den Formatstil an
525      *
526      * @return true, falls value, sich als Datum interpretieren l&auml;&szlig;t;
527      * false, sonst
528      */

529     public final static boolean checkDate( final String JavaDoc value,
530                                            final int style, final Locale JavaDoc locale ) {
531         try {
532             asDate( value, style, locale );
533         } catch( ParseException e ) {
534             return false;
535         }
536         
537         return true;
538     }
539     
540     /**
541      * &Uuml;berpr&uuml;ft, ob sich der gegebene String im Sinne des
542      * gegebenen DateFormat-Objektes als Datum interpretieren
543      * l&auml;szlig;t.
544      *
545      * @param value der zu &uuml;berpr&uuml;fende String
546      * @param format Formatobjekt f&uuml;r die Datumskonvertierung
547      *
548      * @return true, falls value, sich als Datum interpretieren l&auml;&szlig;t;
549      * false, sonst
550      */

551     public final static boolean checkDate( final String JavaDoc 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     /**
563      * &Uuml;berpr&uuml;ft, ob sich der gegebene String f&uuml;r den
564      * default Sprachraum und Formatstil als Datum interpretieren l&auml;szlig;t.
565      *
566      * @param value der zu &uuml;berpr&uuml;fende String
567      *
568      * @return true, falls value, sich als Datum interpretieren l&auml;&szlig;t;
569      * false, sonst
570      */

571     public final static boolean checkDateDef( final String JavaDoc value ) {
572         return checkDate( value, DATE_FORMAT );
573     }
574     
575     /**
576      * Wandelt den gegebenen String in einen Date-Objekt um
577      * und liefert dieses zur&uuml;ck. Im Date-Objekt wird lediglich das Datum
578      * gesetzt, nicht die Zeit.
579      *
580      * @param value der umzuwandelnde String
581      *
582      * @return value als Date-Objekt
583      *
584      * @exception ParseException falls value nicht als Datum interpretierbar ist.
585      */

586     public final java.util.Date JavaDoc asDate( final String JavaDoc 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     /**
595      * Wandelt den gegebenen String in einen Date-Objekt um
596      * und liefert dieses zur&uuml;ck. Bei der Umwandlung ist der
597      * gegebene Sprachraum und der default Formatstil ma&szlig;gebend.
598      * Im Date-Objekt wird lediglich das Datum gesetzt, nicht die Zeit.
599      *
600      * @param value der umzuwandelnde String
601      * @param locale gibt den Sprachraum an
602      *
603      * @return value als Date-Objekt.
604      *
605      * @exception ParseException falls value nicht als Datum interpretierbar ist.
606      */

607     public static final java.util.Date JavaDoc asDate( final String JavaDoc value,
608                                      final Locale JavaDoc locale )
609                                                     throws ParseException {
610         return asDate( value, DEFAULT_STYLE, locale );
611     }
612     
613     /**
614      * Wandelt den gegebenen String in einen Date-Objekt um
615      * und liefert dieses zur&uuml;ck. Bei der Umwandlung ist der
616      * gegebene Sprachraum und Formatstil ma&szlig;gebend.
617      * Im Date-Objekt wird lediglich das Datum gesetzt, nicht die Zeit.
618      *
619      * @param value der umzuwandelnde String
620      * @param style gibt den Formatstil an
621      * @param locale gibt den Sprachraum an
622      *
623      * @return value als Date-Objekt.
624      *
625      * @exception ParseException falls value nicht als Datum interpretierbar ist.
626      */

627     public static final java.util.Date JavaDoc asDate( final String JavaDoc value,
628                                      final int style, final Locale JavaDoc 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     /**
639      * Wandelt den gegebenen String in einen Date-Objekt um
640      * und liefert diesens zur&uuml;ck. Bei der Umwandlung ist das
641      * gegebene DateFormat-Objekt ma&szlig;gebend.
642      * Im Date-Objekt wird lediglich das Datum gesetzt, nicht die Zeit.
643      *
644      * @param value der umzuwandelnde String
645      * @param format Formatobjekt f&uuml;r das Datum
646      *
647      * @return value als Date-Objekt.
648      *
649      * @exception ParseException falls value nicht als Datum interpretierbar ist.
650      */

651     public static java.util.Date JavaDoc asDate( final String JavaDoc 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     /**
660      * Wandelt den gegebenen String in einen Date-Objekt um
661      * und liefert dieses zur&uuml;ck. Bei der Umwandlung ist der
662      * default Sprachraum ma&szlig;gebend. Im Date-Objekt wird lediglich das Datum
663      * gesetzt, nicht die Zeit.
664      *
665      * @param value der umzuwandelnde String
666      *
667      * @return value als Date-Objekt.
668      *
669      * @exception ParseException falls value nicht als Datum interpretierbar ist.
670      */

671     public static final java.util.Date JavaDoc asDateDef( final String JavaDoc value )
672                                                 throws ParseException {
673         return asDate( value, DATE_FORMAT );
674     }
675
676     /**
677      * &Uuml;berpr&uuml;ft, ob sich der gegebene String als Uhrzeit
678      * interpretieren l&auml;szlig;t.
679      *
680      * @param value der zu &uuml;berpr&uuml;fende String
681      *
682      * @return true, falls value, sich als Uhrzeit interpretieren l&auml;&szlig;t;
683      * false, sonst
684      */

685     public final boolean checkTime( final String JavaDoc value ) {
686         if ( time_format == null ) {
687             time_format = DateFormat.getTimeInstance( style, locale );
688         }
689         return TKFormTypes.checkTime( value, time_format );
690     }
691     
692     /**
693      * &Uuml;berpr&uuml;ft, ob sich der gegebene String f&uuml;r den
694      * gegebenen Sprachraum und default Formatstil als Uhrzeit interpretieren l&auml;szlig;t.
695      *
696      * @param value der zu &uuml;berpr&uuml;fende String
697      * @param locale gibt den Sprachraum an
698      *
699      * @return true, falls value, sich als Uhrzeit interpretieren l&auml;&szlig;t;
700      * false, sonst
701      */

702     public final static boolean checkTime( final String JavaDoc value,
703                                            final Locale JavaDoc locale ) {
704         return checkTime( value, DEFAULT_STYLE, locale );
705     }
706                                         
707     /**
708      * &Uuml;berpr&uuml;ft, ob sich der gegebene String f&uuml;r den
709      * gegebenen Sprachraum und Formatstil als Uhrzeit interpretieren l&auml;szlig;t.
710      *
711      * @param value der zu &uuml;berpr&uuml;fende String
712      * @param locale gibt den Sprachraum an
713      * @param style gibt den Formatstil an
714      *
715      * @return true, falls value, sich als Uhrzeit interpretieren l&auml;&szlig;t;
716      * false, sonst
717      */

718     public final static boolean checkTime( final String JavaDoc value,
719                                            final int style, final Locale JavaDoc locale ) {
720         try {
721             asTime( value, style, locale );
722         } catch( ParseException e ) {
723             return false;
724         }
725         
726         return true;
727     }
728     
729     /**
730      * &Uuml;berpr&uuml;ft, ob sich der gegebene String im Sinne des
731      * gegebenen DateFormat-Objektes als Uhrzeit interpretieren
732      * l&auml;szlig;t.
733      *
734      * @param value der zu &uuml;berpr&uuml;fende String
735      * @param format Formatobjekt f&uuml;r die Zeitkonvertierung
736      *
737      * @return true, falls value, sich als Uhrzeit interpretieren l&auml;&szlig;t;
738      * false, sonst
739      */

740     public final static boolean checkTime( final String JavaDoc 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     /**
752      * &Uuml;berpr&uuml;ft, ob sich der gegebene String f&uuml;r den
753      * default Sprachraum und Formatstil als Uhrzeit interpretieren l&auml;szlig;t.
754      *
755      * @param value der zu &uuml;berpr&uuml;fende String
756      *
757      * @return true, falls value, sich als Uhrzeit interpretieren l&auml;&szlig;t;
758      * false, sonst
759      */

760     public final static boolean checkTimeDef( final String JavaDoc value ) {
761         return checkTime( value, TIME_FORMAT );
762     }
763
764     public final java.util.Date JavaDoc asTime( final String JavaDoc 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 JavaDoc asTime( final String JavaDoc value,
773                                      final Locale JavaDoc locale )
774                                                     throws ParseException {
775         return asTime( value, DEFAULT_STYLE, locale );
776     }
777         
778     public static final java.util.Date JavaDoc asTime( final String JavaDoc value,
779                                      final int style, final Locale JavaDoc 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 JavaDoc asTime( final String JavaDoc 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 JavaDoc asTimeDef( final String JavaDoc value )
799                                                 throws ParseException {
800         return asTime( value, TIME_FORMAT );
801     }
802
803     /**
804      * &Uuml;berpr&uuml;ft, ob sich der gegebene String als Flie&szlig;kommazahl
805      * interpretieren l&auml;szlig;t.
806      *
807      * @param value der zu &uuml;berpr&uuml;fende String
808      *
809      * @return true, falls value, sich als Flie&sizlig;kommazahl interpretieren l&auml;&szlig;t;
810      * false, sonst
811      */

812     public final boolean checkMoney( final String JavaDoc value ) {
813         if ( currency_format == null ) {
814             currency_format = NumberFormat.getCurrencyInstance( locale );
815         }
816         return TKFormTypes.checkMoney( value, currency_format );
817     }
818         
819     /**
820      * &Uuml;berpr&uuml;ft, ob sich der gegebene String f&uuml;r den
821      * gegebenen Sprachraum als Flie&szlig;kommazahl interpretieren l&auml;szlig;t.
822      *
823      * @param value der zu &uuml;berpr&uuml;fende String
824      * @param locale gibt den Sprachraum an
825      *
826      * @return true, falls value, sich als Flie&sizlig;kommazahl interpretieren l&auml;&szlig;t;
827      * false, sonst
828      */

829     public final static boolean checkMoney( final String JavaDoc value,
830                                             final Locale JavaDoc locale ) {
831         try {
832             asMoney( value, locale );
833         } catch( ParseException e ) {
834             return false;
835         }
836         
837         return true;
838     }
839         
840     /**
841      * &Uuml;berpr&uuml;ft, ob sich der gegebene String im Sinne des
842      * gegebenen NumberFormat-Objektes als Flie&szlig;kommazahl interpretieren
843      * l&auml;szlig;t.
844      *
845      * @param value der zu &uuml;berpr&uuml;fende String
846      * @param format Formatobjekt f&uuml;r Flie&szlig;kommazahlen
847      *
848      * @return true, falls value, sich als Flie&sizlig;kommazahl interpretieren l&auml;&szlig;t;
849      * false, sonst
850      */

851     public final static boolean checkMoney( final String JavaDoc 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     /**
863      * &Uuml;berpr&uuml;ft, ob sich der gegebene String f&uuml;r den
864      * default Sprachraum als Flie&szlig;kommazahl interpretieren l&auml;szlig;t.
865      *
866      * @param value der zu &uuml;berpr&uuml;fende String
867      *
868      * @return true, falls value, sich als Flie&sizlig;kommazahl interpretieren l&auml;&szlig;t;
869      * false, sonst
870      */

871     public final static boolean checkMoneyDef( final String JavaDoc value ) {
872         return checkMoney( value, DEFAULT_LOCALE );
873     }
874
875     /**
876      * Wandelt den gegebenen String in einen Double-Wert um
877      * und liefert diesen zur&uuml;ck.
878      *
879      * @param value der umzuwandelnde String
880      *
881      * @return value als double-Wert.
882      *
883      * @exception ParseException falls value nicht als Flie&szlig;kommazahl interpretierbar ist.
884      */

885     public final double asMoney( final String JavaDoc 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     /**
894      * Wandelt den gegebenen String in einen Double-Wert um
895      * und liefert diesen zur&uuml;ck. Bei der Umwandlung ist der
896      * gegebene Sprachraum ma&szlig;gebend.
897      *
898      * @param value der umzuwandelnde String
899      * @param locale gibt den Sprachraum an
900      *
901      * @return value als double-Wert.
902      *
903      * @exception ParseException falls value nicht als Flie&szlig;kommazahl interpretierbar ist.
904      */

905     public static final double asMoney( final String JavaDoc value,
906                                         final Locale JavaDoc 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     /**
916      * Wandelt den gegebenen String in einen Double-Wert um
917      * und liefert diesen zur&uuml;ck. Bei der Umwandlung ist das
918      * gegebene NumberFormat-Objekt ma&szlig;gebend.
919      *
920      * @param value der umzuwandelnde String
921      * @param format Formatobjekt f&uuml;r Flie&szlig;kommazahlen
922      *
923      * @return value als double-Wert.
924      *
925      * @exception ParseException falls value nicht als Flie&szlig;kommazahl interpretierbar ist.
926      */

927     public static double asMoney( final String JavaDoc 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 JavaDoc 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     /**
947      * Wandelt den gegebenen String in einen Double-Wert um
948      * und liefert diesen zur&uuml;ck. Bei der Umwandlung ist der
949      * default Sprachraum ma&szlig;gebend.
950      *
951      * @param value der umzuwandelnde String
952      *
953      * @return value als double-Wert.
954      *
955      * @exception ParseException falls value nicht als Flie&szlig;kommazahl interpretierbar ist.
956      */

957     public static final double asMoneyDef( final String JavaDoc value )
958                                             throws ParseException {
959         return asMoney( value, CURRENCY_FORMAT );
960     }
961     
962     //{{DECLARE_CONTROLS
963
//}}
964
}
965
966
Popular Tags