KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > util > i18n > Resources


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.avalon.util.i18n;
18
19 import java.text.DateFormat JavaDoc;
20 import java.text.MessageFormat JavaDoc;
21 import java.text.ParseException JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.Locale JavaDoc;
24 import java.util.MissingResourceException JavaDoc;
25 import java.util.Random JavaDoc;
26 import java.util.ResourceBundle JavaDoc;
27
28 /**
29  * A class to simplify extracting localized strings, icons
30  * and other common resources from a ResourceBundle.
31  *
32  * Reworked to mirror behaviour of StringManager from Tomcat (format() to getString()).
33  *
34  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
35  */

36 public class Resources
37 {
38     private static final Random JavaDoc RANDOM = new Random JavaDoc();
39
40     ///Local of Resources
41
private final Locale JavaDoc m_locale;
42
43     ///Resource bundle referenced by manager
44
private ResourceBundle JavaDoc m_bundle;
45
46     ///Base name of resource bundle
47
private String JavaDoc m_baseName;
48
49     ///ClassLoader from which to load resources
50
private ClassLoader JavaDoc m_classLoader;
51
52     /**
53      * Constructor that builds a manager in default locale.
54      *
55      * @param baseName the base name of ResourceBundle
56      */

57     public Resources( final String JavaDoc baseName )
58     {
59         this( baseName, Locale.getDefault(), null );
60     }
61
62     /**
63      * Constructor that builds a manager in default locale
64      * using specified ClassLoader.
65      *
66      * @param baseName the base name of ResourceBundle
67      * @param classLoader the classLoader to load ResourceBundle from
68      */

69     public Resources( final String JavaDoc baseName, final ClassLoader JavaDoc classLoader )
70     {
71         this( baseName, Locale.getDefault(), classLoader );
72     }
73
74     /**
75      * Constructor that builds a manager in specified locale.
76      *
77      * @param baseName the base name of ResourceBundle
78      * @param locale the Locale for resource bundle
79      */

80     public Resources( final String JavaDoc baseName, final Locale JavaDoc locale )
81     {
82         this( baseName, locale, null );
83     }
84
85     /**
86      * Constructor that builds a manager in specified locale.
87      *
88      * @param baseName the base name of ResourceBundle
89      * @param locale the Locale for resource bundle
90      * @param classLoader the classLoader to load ResourceBundle from
91      */

92     public Resources( final String JavaDoc baseName,
93                       final Locale JavaDoc locale,
94                       final ClassLoader JavaDoc classLoader )
95     {
96         if( null == baseName )
97         {
98             throw new NullPointerException JavaDoc( "baseName property is null" );
99         }
100         if( null == locale )
101         {
102             throw new NullPointerException JavaDoc( "locale property is null" );
103         }
104         m_baseName = baseName;
105         m_locale = locale;
106         m_classLoader = classLoader;
107     }
108
109     /**
110      * Retrieve a boolean from bundle.
111      *
112      * @param key the key of resource
113      * @param defaultValue the default value if key is missing
114      * @return the resource boolean
115      */

116     public boolean getBoolean( final String JavaDoc key, final boolean defaultValue )
117         throws MissingResourceException JavaDoc
118     {
119         try
120         {
121             return getBoolean( key );
122         }
123         catch( final MissingResourceException JavaDoc mre )
124         {
125             return defaultValue;
126         }
127     }
128
129     /**
130      * Retrieve a boolean from bundle.
131      *
132      * @param key the key of resource
133      * @return the resource boolean
134      */

135     public boolean getBoolean( final String JavaDoc key )
136         throws MissingResourceException JavaDoc
137     {
138         final ResourceBundle JavaDoc bundle = getBundle();
139         final String JavaDoc value = bundle.getString( key );
140         return value.equalsIgnoreCase( "true" );
141     }
142
143     /**
144      * Retrieve a byte from bundle.
145      *
146      * @param key the key of resource
147      * @param defaultValue the default value if key is missing
148      * @return the resource byte
149      */

150     public byte getByte( final String JavaDoc key, final byte defaultValue )
151         throws MissingResourceException JavaDoc
152     {
153         try
154         {
155             return getByte( key );
156         }
157         catch( final MissingResourceException JavaDoc mre )
158         {
159             return defaultValue;
160         }
161     }
162
163     /**
164      * Retrieve a byte from bundle.
165      *
166      * @param key the key of resource
167      * @return the resource byte
168      */

169     public byte getByte( final String JavaDoc key )
170         throws MissingResourceException JavaDoc
171     {
172         final ResourceBundle JavaDoc bundle = getBundle();
173         final String JavaDoc value = bundle.getString( key );
174         try
175         {
176             return Byte.parseByte( value );
177         }
178         catch( final NumberFormatException JavaDoc nfe )
179         {
180             throw new MissingResourceException JavaDoc( "Expecting a byte value but got " + value,
181                                                 "java.lang.String",
182                                                 key );
183         }
184     }
185
186     /**
187      * Retrieve a char from bundle.
188      *
189      * @param key the key of resource
190      * @param defaultValue the default value if key is missing
191      * @return the resource char
192      */

193     public char getChar( final String JavaDoc key, final char defaultValue )
194         throws MissingResourceException JavaDoc
195     {
196         try
197         {
198             return getChar( key );
199         }
200         catch( final MissingResourceException JavaDoc mre )
201         {
202             return defaultValue;
203         }
204     }
205
206     /**
207      * Retrieve a char from bundle.
208      *
209      * @param key the key of resource
210      * @return the resource char
211      */

212     public char getChar( final String JavaDoc key )
213         throws MissingResourceException JavaDoc
214     {
215         final ResourceBundle JavaDoc bundle = getBundle();
216         final String JavaDoc value = bundle.getString( key );
217
218         if( 1 == value.length() )
219         {
220             return value.charAt( 0 );
221         }
222         else
223         {
224             throw new MissingResourceException JavaDoc( "Expecting a char value but got " + value,
225                                                 "java.lang.String",
226                                                 key );
227         }
228     }
229
230     /**
231      * Retrieve a short from bundle.
232      *
233      * @param key the key of resource
234      * @param defaultValue the default value if key is missing
235      * @return the resource short
236      */

237     public short getShort( final String JavaDoc key, final short defaultValue )
238         throws MissingResourceException JavaDoc
239     {
240         try
241         {
242             return getShort( key );
243         }
244         catch( final MissingResourceException JavaDoc mre )
245         {
246             return defaultValue;
247         }
248     }
249
250     /**
251      * Retrieve a short from bundle.
252      *
253      * @param key the key of resource
254      * @return the resource short
255      */

256     public short getShort( final String JavaDoc key )
257         throws MissingResourceException JavaDoc
258     {
259         final ResourceBundle JavaDoc bundle = getBundle();
260         final String JavaDoc value = bundle.getString( key );
261         try
262         {
263             return Short.parseShort( value );
264         }
265         catch( final NumberFormatException JavaDoc nfe )
266         {
267             throw new MissingResourceException JavaDoc( "Expecting a short value but got " + value,
268                                                 "java.lang.String",
269                                                 key );
270         }
271     }
272
273     /**
274      * Retrieve a integer from bundle.
275      *
276      * @param key the key of resource
277      * @param defaultValue the default value if key is missing
278      * @return the resource integer
279      */

280     public int getInteger( final String JavaDoc key, final int defaultValue )
281         throws MissingResourceException JavaDoc
282     {
283         try
284         {
285             return getInteger( key );
286         }
287         catch( final MissingResourceException JavaDoc mre )
288         {
289             return defaultValue;
290         }
291     }
292
293     /**
294      * Retrieve a integer from bundle.
295      *
296      * @param key the key of resource
297      * @return the resource integer
298      */

299     public int getInteger( final String JavaDoc key )
300         throws MissingResourceException JavaDoc
301     {
302         final ResourceBundle JavaDoc bundle = getBundle();
303         final String JavaDoc value = bundle.getString( key );
304         try
305         {
306             return Integer.parseInt( value );
307         }
308         catch( final NumberFormatException JavaDoc nfe )
309         {
310             throw new MissingResourceException JavaDoc( "Expecting a integer value but got " + value,
311                                                 "java.lang.String",
312                                                 key );
313         }
314     }
315
316     /**
317      * Retrieve a long from bundle.
318      *
319      * @param key the key of resource
320      * @param defaultValue the default value if key is missing
321      * @return the resource long
322      */

323     public long getLong( final String JavaDoc key, final long defaultValue )
324         throws MissingResourceException JavaDoc
325     {
326         try
327         {
328             return getLong( key );
329         }
330         catch( final MissingResourceException JavaDoc mre )
331         {
332             return defaultValue;
333         }
334     }
335
336     /**
337      * Retrieve a long from bundle.
338      *
339      * @param key the key of resource
340      * @return the resource long
341      */

342     public long getLong( final String JavaDoc key )
343         throws MissingResourceException JavaDoc
344     {
345         final ResourceBundle JavaDoc bundle = getBundle();
346         final String JavaDoc value = bundle.getString( key );
347         try
348         {
349             return Long.parseLong( value );
350         }
351         catch( final NumberFormatException JavaDoc nfe )
352         {
353             throw new MissingResourceException JavaDoc( "Expecting a long value but got " + value,
354                                                 "java.lang.String",
355                                                 key );
356         }
357     }
358
359     /**
360      * Retrieve a float from bundle.
361      *
362      * @param key the key of resource
363      * @param defaultValue the default value if key is missing
364      * @return the resource float
365      */

366     public float getFloat( final String JavaDoc key, final float defaultValue )
367         throws MissingResourceException JavaDoc
368     {
369         try
370         {
371             return getFloat( key );
372         }
373         catch( final MissingResourceException JavaDoc mre )
374         {
375             return defaultValue;
376         }
377     }
378
379     /**
380      * Retrieve a float from bundle.
381      *
382      * @param key the key of resource
383      * @return the resource float
384      */

385     public float getFloat( final String JavaDoc key )
386         throws MissingResourceException JavaDoc
387     {
388         final ResourceBundle JavaDoc bundle = getBundle();
389         final String JavaDoc value = bundle.getString( key );
390         try
391         {
392             return Float.parseFloat( value );
393         }
394         catch( final NumberFormatException JavaDoc nfe )
395         {
396             throw new MissingResourceException JavaDoc( "Expecting a float value but got " + value,
397                                                 "java.lang.String",
398                                                 key );
399         }
400     }
401
402     /**
403      * Retrieve a double from bundle.
404      *
405      * @param key the key of resource
406      * @param defaultValue the default value if key is missing
407      * @return the resource double
408      */

409     public double getDouble( final String JavaDoc key, final double defaultValue )
410         throws MissingResourceException JavaDoc
411     {
412         try
413         {
414             return getDouble( key );
415         }
416         catch( final MissingResourceException JavaDoc mre )
417         {
418             return defaultValue;
419         }
420     }
421
422     /**
423      * Retrieve a double from bundle.
424      *
425      * @param key the key of resource
426      * @return the resource double
427      */

428     public double getDouble( final String JavaDoc key )
429         throws MissingResourceException JavaDoc
430     {
431         final ResourceBundle JavaDoc bundle = getBundle();
432         final String JavaDoc value = bundle.getString( key );
433         try
434         {
435             return Double.parseDouble( value );
436         }
437         catch( final NumberFormatException JavaDoc nfe )
438         {
439             throw new MissingResourceException JavaDoc( "Expecting a double value but got " + value,
440                                                 "java.lang.String",
441                                                 key );
442         }
443     }
444
445     /**
446      * Retrieve a date from bundle.
447      *
448      * @param key the key of resource
449      * @param defaultValue the default value if key is missing
450      * @return the resource date
451      */

452     public Date JavaDoc getDate( final String JavaDoc key, final Date JavaDoc defaultValue )
453         throws MissingResourceException JavaDoc
454     {
455         try
456         {
457             return getDate( key );
458         }
459         catch( final MissingResourceException JavaDoc mre )
460         {
461             return defaultValue;
462         }
463     }
464
465     /**
466      * Retrieve a date from bundle.
467      *
468      * @param key the key of resource
469      * @return the resource date
470      */

471     public Date JavaDoc getDate( final String JavaDoc key )
472         throws MissingResourceException JavaDoc
473     {
474         final ResourceBundle JavaDoc bundle = getBundle();
475         final String JavaDoc value = bundle.getString( key );
476         try
477         {
478             final DateFormat JavaDoc format =
479                 DateFormat.getDateInstance( DateFormat.DEFAULT, m_locale );
480             return format.parse( value );
481         }
482         catch( final ParseException JavaDoc pe )
483         {
484             throw new MissingResourceException JavaDoc( "Expecting a date value but got " + value,
485                                                 "java.lang.String",
486                                                 key );
487         }
488     }
489
490     /**
491      * Retrieve a time from bundle.
492      *
493      * @param key the key of resource
494      * @param defaultValue the default value if key is missing
495      * @return the resource time
496      */

497     public Date JavaDoc getTime( final String JavaDoc key, final Date JavaDoc defaultValue )
498         throws MissingResourceException JavaDoc
499     {
500         try
501         {
502             return getTime( key );
503         }
504         catch( final MissingResourceException JavaDoc mre )
505         {
506             return defaultValue;
507         }
508     }
509
510     /**
511      * Retrieve a time from bundle.
512      *
513      * @param key the key of resource
514      * @return the resource time
515      */

516     public Date JavaDoc getTime( final String JavaDoc key )
517         throws MissingResourceException JavaDoc
518     {
519         final ResourceBundle JavaDoc bundle = getBundle();
520         final String JavaDoc value = bundle.getString( key );
521         try
522         {
523             final DateFormat JavaDoc format =
524                 DateFormat.getTimeInstance( DateFormat.DEFAULT, m_locale );
525             return format.parse( value );
526         }
527         catch( final ParseException JavaDoc pe )
528         {
529             throw new MissingResourceException JavaDoc( "Expecting a time value but got " + value,
530                                                 "java.lang.String",
531                                                 key );
532         }
533     }
534
535     /**
536      * Retrieve a time from bundle.
537      *
538      * @param key the key of resource
539      * @param defaultValue the default value if key is missing
540      * @return the resource time
541      */

542     public Date JavaDoc getDateTime( final String JavaDoc key, final Date JavaDoc defaultValue )
543         throws MissingResourceException JavaDoc
544     {
545         try
546         {
547             return getDateTime( key );
548         }
549         catch( final MissingResourceException JavaDoc mre )
550         {
551             return defaultValue;
552         }
553     }
554
555     /**
556      * Retrieve a date + time from bundle.
557      *
558      * @param key the key of resource
559      * @return the resource date + time
560      */

561     public Date JavaDoc getDateTime( final String JavaDoc key )
562         throws MissingResourceException JavaDoc
563     {
564         final ResourceBundle JavaDoc bundle = getBundle();
565         final String JavaDoc value = bundle.getString( key );
566         try
567         {
568             final DateFormat JavaDoc format =
569                 DateFormat.getDateTimeInstance( DateFormat.DEFAULT, DateFormat.DEFAULT, m_locale );
570             return format.parse( value );
571         }
572         catch( final ParseException JavaDoc pe )
573         {
574             throw new MissingResourceException JavaDoc( "Expecting a time value but got " + value,
575                                                 "java.lang.String",
576                                                 key );
577         }
578     }
579
580     /**
581      * Retrieve a raw string from bundle.
582      *
583      * @param key the key of resource
584      * @return the resource string
585      */

586     public String JavaDoc getString( final String JavaDoc key )
587         throws MissingResourceException JavaDoc
588     {
589         final ResourceBundle JavaDoc bundle = getBundle();
590         return bundle.getString( key );
591     }
592
593     /**
594      * Retrieve a string from resource bundle and format it with specified args.
595      *
596      * @param key the key for resource
597      * @param arg1 an arg
598      * @return the formatted string
599      */

600     public String JavaDoc getString( final String JavaDoc key, final Object JavaDoc arg1 )
601     {
602         final Object JavaDoc[] args = new Object JavaDoc[]{arg1};
603         return format( key, args );
604     }
605
606     /**
607      * Retrieve a string from resource bundle and format it with specified args.
608      *
609      * @param key the key for resource
610      * @param arg1 an arg
611      * @param arg2 an arg
612      * @return the formatted string
613      */

614     public String JavaDoc getString( final String JavaDoc key, final Object JavaDoc arg1, final Object JavaDoc arg2 )
615     {
616         final Object JavaDoc[] args = new Object JavaDoc[]{arg1, arg2};
617         return format( key, args );
618     }
619
620     /**
621      * Retrieve a string from resource bundle and format it with specified args.
622      *
623      * @param key the key for resource
624      * @param arg1 an arg
625      * @param arg2 an arg
626      * @param arg3 an arg
627      * @return the formatted string
628      */

629     public String JavaDoc getString( final String JavaDoc key,
630                              final Object JavaDoc arg1,
631                              final Object JavaDoc arg2,
632                              final Object JavaDoc arg3 )
633     {
634         final Object JavaDoc[] args = new Object JavaDoc[]{arg1, arg2, arg3};
635         return format( key, args );
636     }
637
638     /**
639      * Retrieve a string from resource bundle and format it with specified args.
640      *
641      * @param key the key for resource
642      * @param arg1 an arg
643      * @param arg2 an arg
644      * @param arg3 an arg
645      * @param arg4 an arg
646      * @return the formatted string
647      */

648     public String JavaDoc getString( final String JavaDoc key,
649                              final Object JavaDoc arg1,
650                              final Object JavaDoc arg2,
651                              final Object JavaDoc arg3,
652                              final Object JavaDoc arg4 )
653     {
654         final Object JavaDoc[] args = new Object JavaDoc[]{arg1, arg2, arg3, arg4};
655         return format( key, args );
656     }
657
658     /**
659      * Retrieve a string from resource bundle and format it with specified args.
660      *
661      * @param key the key for resource
662      * @param arg1 an arg
663      * @param arg2 an arg
664      * @param arg3 an arg
665      * @param arg4 an arg
666      * @param arg5 an arg
667      * @return the formatted string
668      */

669     public String JavaDoc getString( final String JavaDoc key,
670                              final Object JavaDoc arg1,
671                              final Object JavaDoc arg2,
672                              final Object JavaDoc arg3,
673                              final Object JavaDoc arg4,
674                              final Object JavaDoc arg5 )
675     {
676         final Object JavaDoc[] args = new Object JavaDoc[]{arg1, arg2, arg3, arg4, arg5};
677         return format( key, args );
678     }
679
680     /**
681      * Retrieve a string from resource bundle and format it with specified args.
682      *
683      * @param key the key for resource
684      * @param arg1 an arg
685      * @param arg2 an arg
686      * @param arg3 an arg
687      * @param arg4 an arg
688      * @param arg5 an arg
689      * @param arg6 an arg
690      * @return the formatted string
691      */

692     public String JavaDoc getString( final String JavaDoc key,
693                              final Object JavaDoc arg1,
694                              final Object JavaDoc arg2,
695                              final Object JavaDoc arg3,
696                              final Object JavaDoc arg4,
697                              final Object JavaDoc arg5,
698                              final Object JavaDoc arg6 )
699     {
700         final Object JavaDoc[] args = new Object JavaDoc[]{arg1, arg2, arg3, arg4, arg5, arg6};
701         return format( key, args );
702     }
703
704     /**
705      * Retrieve a string from resource bundle and format it with specified args.
706      *
707      * @param key the key for resource
708      * @param arg1 an arg
709      * @param arg2 an arg
710      * @param arg3 an arg
711      * @param arg4 an arg
712      * @param arg5 an arg
713      * @param arg6 an arg
714      * @param arg7 an arg
715      * @return the formatted string
716      */

717     public String JavaDoc getString( final String JavaDoc key,
718                              final Object JavaDoc arg1,
719                              final Object JavaDoc arg2,
720                              final Object JavaDoc arg3,
721                              final Object JavaDoc arg4,
722                              final Object JavaDoc arg5,
723                              final Object JavaDoc arg6,
724                              final Object JavaDoc arg7 )
725     {
726         final Object JavaDoc[] args = new Object JavaDoc[]{arg1, arg2, arg3, arg4, arg5, arg6, arg7};
727         return format( key, args );
728     }
729
730     /**
731      * Retrieve a string from resource bundle and format it with specified args.
732      *
733      * @param key the key for resource
734      * @param args an array of args
735      * @return the formatted string
736      */

737     public String JavaDoc format( final String JavaDoc key, final Object JavaDoc[] args )
738     {
739         try
740         {
741             final String JavaDoc pattern = getPatternString( key );
742             return MessageFormat.format( pattern, args );
743         }
744         catch( final MissingResourceException JavaDoc mre )
745         {
746             final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
747             sb.append( "Unknown resource. Bundle: '" );
748             sb.append( m_baseName );
749             sb.append( "' Key: '" );
750             sb.append( key );
751             sb.append( "' Args: '" );
752
753             for( int i = 0; i < args.length; i++ )
754             {
755                 if( 0 != i ) sb.append( "', '" );
756                 sb.append( args[ i ] );
757             }
758
759             sb.append( "' Reason: " );
760             sb.append( mre );
761
762             return sb.toString();
763         }
764     }
765
766     /**
767      * Retrieve underlying ResourceBundle.
768      * If bundle has not been loaded it will be loaded by this method.
769      * Access is given in case other resources need to be extracted
770      * that this Manager does not provide simplified access to.
771      *
772      * @return the ResourceBundle
773      * @throws MissingResourceException if an error occurs
774      */

775     public final ResourceBundle JavaDoc getBundle()
776         throws MissingResourceException JavaDoc
777     {
778         if( null == m_bundle )
779         {
780             // bundle wasn't cached, so load it, cache it, and return it.
781
ClassLoader JavaDoc classLoader = m_classLoader;
782             if( null == classLoader )
783             {
784                 classLoader = Thread.currentThread().getContextClassLoader();
785             }
786             if( null != classLoader )
787             {
788                 m_bundle = ResourceBundle.getBundle( m_baseName, m_locale, classLoader );
789             }
790             else
791             {
792                 m_bundle = ResourceBundle.getBundle( m_baseName, m_locale );
793             }
794         }
795         return m_bundle;
796     }
797
798     /**
799      * Utility method to retrieve a string from ResourceBundle.
800      * If the key is a single string then that will be returned.
801      * If key refers to string array then a random string will be chosen.
802      * Other types cause an exception.
803      *
804      * @param key the key to resource
805      * @return the string resource
806      * @throws MissingResourceException if an error occurs
807      */

808     private String JavaDoc getPatternString( final String JavaDoc key )
809         throws MissingResourceException JavaDoc
810     {
811         final ResourceBundle JavaDoc bundle = getBundle();
812         final Object JavaDoc object = bundle.getObject( key );
813
814         // is the resource a single string
815
if( object instanceof String JavaDoc )
816         {
817             return (String JavaDoc)object;
818         }
819         else if( object instanceof String JavaDoc[] )
820         {
821             //if string array then randomly pick one
822
final String JavaDoc[] strings = (String JavaDoc[])object;
823             return strings[ RANDOM.nextInt( strings.length ) ];
824         }
825         else
826         {
827             throw new MissingResourceException JavaDoc( "Unable to find resource of appropriate type.",
828                                                 "java.lang.String",
829                                                 key );
830         }
831     }
832 }
833
Popular Tags