KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.i18n;
9
10 import java.text.MessageFormat JavaDoc;
11 import java.util.Locale JavaDoc;
12 import java.util.MissingResourceException JavaDoc;
13 import java.util.Random JavaDoc;
14 import java.util.ResourceBundle JavaDoc;
15 import java.text.DateFormat JavaDoc;
16 import java.text.ParseException JavaDoc;
17 import java.util.Date JavaDoc;
18
19 /**
20  * A class to simplify extracting localized strings, icons
21  * and other common resources from a ResourceBundle.
22  *
23  * Reworked to mirror behaviour of StringManager from Tomcat (format() to getString()).
24  *
25  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
26  */

27 public class Resources
28 {
29     private final static Random JavaDoc RANDOM = new Random JavaDoc();
30
31     ///Local of Resources
32
private final Locale JavaDoc m_locale;
33
34     ///Resource bundle referenced by manager
35
private ResourceBundle JavaDoc m_bundle;
36
37     ///Base name of resource bundle
38
private String JavaDoc m_baseName;
39
40     ///ClassLoader from which to load resources
41
private ClassLoader JavaDoc m_classLoader;
42
43     /**
44      * Constructor that builds a manager in default locale.
45      *
46      * @param baseName the base name of ResourceBundle
47      */

48     public Resources( final String JavaDoc baseName )
49     {
50         this( baseName, Locale.getDefault(), null );
51     }
52
53     /**
54      * Constructor that builds a manager in default locale
55      * using specified ClassLoader.
56      *
57      * @param baseName the base name of ResourceBundle
58      * @param classLoader the classLoader to load ResourceBundle from
59      */

60     public Resources( final String JavaDoc baseName, final ClassLoader JavaDoc classLoader )
61     {
62         this( baseName, Locale.getDefault(), classLoader );
63     }
64
65     /**
66      * Constructor that builds a manager in specified locale.
67      *
68      * @param baseName the base name of ResourceBundle
69      * @param locale the Locale for resource bundle
70      */

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

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

109     public boolean getBoolean( final String JavaDoc key, final boolean defaultValue )
110         throws MissingResourceException JavaDoc
111     {
112         try
113         {
114             return getBoolean( key );
115         }
116         catch( final MissingResourceException JavaDoc mre )
117         {
118             return defaultValue;
119         }
120     }
121
122     /**
123      * Retrieve a boolean from bundle.
124      *
125      * @param key the key of resource
126      * @return the resource boolean
127      */

128     public boolean getBoolean( final String JavaDoc key )
129         throws MissingResourceException JavaDoc
130     {
131         final ResourceBundle JavaDoc bundle = getBundle();
132         final String JavaDoc value = bundle.getString( key );
133         return value.equalsIgnoreCase( "true" );
134     }
135
136     /**
137      * Retrieve a byte from bundle.
138      *
139      * @param key the key of resource
140      * @param defaultValue the default value if key is missing
141      * @return the resource byte
142      */

143     public byte getByte( final String JavaDoc key, final byte defaultValue )
144         throws MissingResourceException JavaDoc
145     {
146         try
147         {
148             return getByte( key );
149         }
150         catch( final MissingResourceException JavaDoc mre )
151         {
152             return defaultValue;
153         }
154     }
155
156     /**
157      * Retrieve a byte from bundle.
158      *
159      * @param key the key of resource
160      * @return the resource byte
161      */

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

186     public char getChar( final String JavaDoc key, final char defaultValue )
187         throws MissingResourceException JavaDoc
188     {
189         try
190         {
191             return getChar( key );
192         }
193         catch( final MissingResourceException JavaDoc mre )
194         {
195             return defaultValue;
196         }
197     }
198
199     /**
200      * Retrieve a char from bundle.
201      *
202      * @param key the key of resource
203      * @return the resource char
204      */

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

230     public short getShort( final String JavaDoc key, final short defaultValue )
231         throws MissingResourceException JavaDoc
232     {
233         try
234         {
235             return getShort( key );
236         }
237         catch( final MissingResourceException JavaDoc mre )
238         {
239             return defaultValue;
240         }
241     }
242
243     /**
244      * Retrieve a short from bundle.
245      *
246      * @param key the key of resource
247      * @return the resource short
248      */

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

273     public int getInteger( final String JavaDoc key, final int defaultValue )
274         throws MissingResourceException JavaDoc
275     {
276         try
277         {
278             return getInteger( key );
279         }
280         catch( final MissingResourceException JavaDoc mre )
281         {
282             return defaultValue;
283         }
284     }
285
286     /**
287      * Retrieve a integer from bundle.
288      *
289      * @param key the key of resource
290      * @return the resource integer
291      */

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

316     public long getLong( final String JavaDoc key, final long defaultValue )
317         throws MissingResourceException JavaDoc
318     {
319         try
320         {
321             return getLong( key );
322         }
323         catch( final MissingResourceException JavaDoc mre )
324         {
325             return defaultValue;
326         }
327     }
328
329     /**
330      * Retrieve a long from bundle.
331      *
332      * @param key the key of resource
333      * @return the resource long
334      */

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

359     public float getFloat( final String JavaDoc key, final float defaultValue )
360         throws MissingResourceException JavaDoc
361     {
362         try
363         {
364             return getFloat( key );
365         }
366         catch( final MissingResourceException JavaDoc mre )
367         {
368             return defaultValue;
369         }
370     }
371
372     /**
373      * Retrieve a float from bundle.
374      *
375      * @param key the key of resource
376      * @return the resource float
377      */

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

402     public double getDouble( final String JavaDoc key, final double defaultValue )
403         throws MissingResourceException JavaDoc
404     {
405         try
406         {
407             return getDouble( key );
408         }
409         catch( final MissingResourceException JavaDoc mre )
410         {
411             return defaultValue;
412         }
413     }
414
415     /**
416      * Retrieve a double from bundle.
417      *
418      * @param key the key of resource
419      * @return the resource double
420      */

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

445     public Date JavaDoc getDate( final String JavaDoc key, final Date JavaDoc defaultValue )
446         throws MissingResourceException JavaDoc
447     {
448         try
449         {
450             return getDate( key );
451         }
452         catch( final MissingResourceException JavaDoc mre )
453         {
454             return defaultValue;
455         }
456     }
457
458     /**
459      * Retrieve a date from bundle.
460      *
461      * @param key the key of resource
462      * @return the resource date
463      */

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

490     public Date JavaDoc getTime( final String JavaDoc key, final Date JavaDoc defaultValue )
491         throws MissingResourceException JavaDoc
492     {
493         try
494         {
495             return getTime( key );
496         }
497         catch( final MissingResourceException JavaDoc mre )
498         {
499             return defaultValue;
500         }
501     }
502
503     /**
504      * Retrieve a time from bundle.
505      *
506      * @param key the key of resource
507      * @return the resource time
508      */

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

535     public Date JavaDoc getDateTime( final String JavaDoc key, final Date JavaDoc defaultValue )
536         throws MissingResourceException JavaDoc
537     {
538         try
539         {
540             return getDateTime( key );
541         }
542         catch( final MissingResourceException JavaDoc mre )
543         {
544             return defaultValue;
545         }
546     }
547
548     /**
549      * Retrieve a date + time from bundle.
550      *
551      * @param key the key of resource
552      * @return the resource date + time
553      */

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

579     public String JavaDoc getString( final String JavaDoc key )
580         throws MissingResourceException JavaDoc
581     {
582         final ResourceBundle JavaDoc bundle = getBundle();
583         return bundle.getString( key );
584     }
585
586     /**
587      * Retrieve a string from resource bundle and format it with specified args.
588      *
589      * @param key the key for resource
590      * @param arg1 an arg
591      * @return the formatted string
592      */

593     public String JavaDoc getString( final String JavaDoc key, final Object JavaDoc arg1 )
594     {
595         final Object JavaDoc[] args = new Object JavaDoc[] { arg1 };
596         return format( key, args );
597     }
598
599     /**
600      * Retrieve a string from resource bundle and format it with specified args.
601      *
602      * @param key the key for resource
603      * @param arg1 an arg
604      * @param arg2 an arg
605      * @return the formatted string
606      */

607     public String JavaDoc getString( final String JavaDoc key, final Object JavaDoc arg1, final Object JavaDoc arg2 )
608     {
609         final Object JavaDoc[] args = new Object JavaDoc[] { arg1, arg2 };
610         return format( key, args );
611     }
612
613     /**
614      * Retrieve a string from resource bundle and format it with specified args.
615      *
616      * @param key the key for resource
617      * @param arg1 an arg
618      * @param arg2 an arg
619      * @param arg3 an arg
620      * @return the formatted string
621      */

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

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

662     public String JavaDoc getString( final String JavaDoc key,
663                              final Object JavaDoc arg1,
664                              final Object JavaDoc arg2,
665                              final Object JavaDoc arg3,
666                              final Object JavaDoc arg4,
667                              final Object JavaDoc arg5 )
668     {
669         final Object JavaDoc[] args = new Object JavaDoc[] { arg1, arg2, arg3, arg4, arg5 };
670         return format( key, args );
671     }
672
673     /**
674      * Retrieve a string from resource bundle and format it with specified args.
675      *
676      * @param key the key for resource
677      * @param args an array of args
678      * @return the formatted string
679      */

680     public String JavaDoc format( final String JavaDoc key, final Object JavaDoc[] args )
681     {
682         try
683         {
684             final String JavaDoc pattern = getPatternString( key );
685             return MessageFormat.format( pattern, args );
686         }
687         catch( final MissingResourceException JavaDoc mre )
688         {
689             final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
690             sb.append( "Unknown resource. Bundle: '" );
691             sb.append( m_baseName );
692             sb.append( "' Key: '" );
693             sb.append( key );
694             sb.append( "' Args: '" );
695
696             for( int i = 0; i < args.length; i++ )
697             {
698                 if( 0 != i ) sb.append( "', '" );
699                 sb.append( args[ i ] );
700             }
701
702             sb.append( "' Reason: " );
703             sb.append( mre );
704
705             return sb.toString();
706         }
707     }
708
709     /**
710      * Retrieve underlying ResourceBundle.
711      * If bundle has not been loaded it will be loaded by this method.
712      * Access is given in case other resources need to be extracted
713      * that this Manager does not provide simplified access to.
714      *
715      * @return the ResourceBundle
716      * @exception MissingResourceException if an error occurs
717      */

718     public final ResourceBundle JavaDoc getBundle()
719         throws MissingResourceException JavaDoc
720     {
721         if( null == m_bundle )
722         {
723             // bundle wasn't cached, so load it, cache it, and return it.
724
ClassLoader JavaDoc classLoader = m_classLoader;
725             if( null == classLoader )
726             {
727                 classLoader = Thread.currentThread().getContextClassLoader();
728             }
729             if( null != classLoader )
730             {
731                 m_bundle = ResourceBundle.getBundle( m_baseName, m_locale, classLoader );
732             }
733             else
734             {
735                 m_bundle = ResourceBundle.getBundle( m_baseName, m_locale );
736             }
737         }
738         return m_bundle;
739     }
740
741     /**
742      * Utility method to retrieve a string from ResourceBundle.
743      * If the key is a single string then that will be returned.
744      * If key refers to string array then a random string will be chosen.
745      * Other types cause an exception.
746      *
747      * @param key the key to resource
748      * @return the string resource
749      * @exception MissingResourceException if an error occurs
750      */

751     private String JavaDoc getPatternString( final String JavaDoc key )
752         throws MissingResourceException JavaDoc
753     {
754         final ResourceBundle JavaDoc bundle = getBundle();
755         final Object JavaDoc object = bundle.getObject( key );
756
757         // is the resource a single string
758
if( object instanceof String JavaDoc )
759         {
760             return (String JavaDoc)object;
761         }
762         else if( object instanceof String JavaDoc[] )
763         {
764             //if string array then randomly pick one
765
final String JavaDoc[] strings = (String JavaDoc[])object;
766             return strings[ RANDOM.nextInt( strings.length ) ];
767         }
768         else
769         {
770             throw new MissingResourceException JavaDoc( "Unable to find resource of appropriate type.",
771                                                 "java.lang.String",
772                                                 key );
773         }
774     }
775 }
776
Popular Tags