KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > i18n > RB


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55
56 package org.jboss.axis.i18n;
57
58 import java.io.IOException JavaDoc;
59 import java.io.InputStream JavaDoc;
60 import java.text.MessageFormat JavaDoc;
61 import java.util.Enumeration JavaDoc;
62 import java.util.Hashtable JavaDoc;
63 import java.util.Locale JavaDoc;
64 import java.util.MissingResourceException JavaDoc;
65 import java.util.Properties JavaDoc;
66
67 /**
68  * CURRENTLY NOT USED
69  * KEEPING FOR REFERENCE 9/19/2002
70  * <p/>
71  * <p>Wrapper class for resource bundles. Property files are used to store
72  * resource strings, which are the only types of resources available.
73  * Property files can inherit properties from other files so that
74  * a base property file can be used and a small number of properties
75  * can be over-ridden by another property file. For example you may
76  * create an english version of a resource file named "resource.properties".
77  * You then decide that the British English version of all of the properties
78  * except one are the same, so there is no need to redefine all of the
79  * properties in "resource_en_GB", just the one that is different.</p>
80  * <p>The property file lookup searches for classes with various suffixes
81  * on the basis if the desired local and the current default local
82  * (as returned by Local.getDefault()). As property files are found the
83  * property values are merged so that inheritance is preserved.</p>
84  * <p>The order of searching is:</p>
85  * <dir>
86  * basename + "_" + langage + "_" + country + "_" + variant
87  * basename + "_" + langage + "_" + country
88  * basename + "_" + langage
89  * basename + "_" + defaultLanguage + "_" + defaultCountry + "_" + defaultVariant
90  * basename + "_" + defaultLanguage + "_" + defaultCountry
91  * basename + "_" + defaultLanguage
92  * basename
93  * </dir>
94  * <p>The basename is the name of the property file without the ".properties"
95  * extension.</p>
96  * <p>Properties will be cached for performance.<p>
97  * <p>Property values stored in the property files can also contain dynamic
98  * variables. Any dynamic variable defined in PropertiesUtil.getVariableValue()
99  * can be used (such as {date}), as well as arguments in the form {0}, {1}, etc.
100  * Argument values are specified in the various overloaded getString() methods.</p>
101  *
102  * @author Karl Moss (kmoss@macromedia.com)
103  * @author Glen Daniels (gdaniels@macromedia.com)
104  */

105 public class RB
106 {
107    // The static cache of properties. The key is the basename + the local +
108
// the default local and the element is the Properties object containing
109
// the resources
110
static Hashtable JavaDoc propertyCache = new Hashtable JavaDoc();
111
112    // The default base name
113
public static final String JavaDoc BASE_NAME = "resource";
114
115    // The property file extension
116
public static final String JavaDoc PROPERTY_EXT = ".properties";
117
118    // The name of the current base property file (with extension)
119
protected String JavaDoc basePropertyFileName;
120
121    // The properties for the current resource bundle
122
protected Properties JavaDoc resourceProperties;
123
124    /**
125     * Construct a new RB
126     *
127     * @param name The name of the property file without the ".properties" extension
128     */

129    public RB(String JavaDoc name) throws MissingResourceException JavaDoc
130    {
131       this(null, name, null);
132    }
133
134    /**
135     * Construct a new RB
136     *
137     * @param caller The calling object. This is used to get the package name
138     * to further construct the basename as well as to get the proper ClassLoader
139     * @param name The name of the property file without the ".properties" extension
140     */

141    public RB(Object JavaDoc caller, String JavaDoc name) throws MissingResourceException JavaDoc
142    {
143       this(caller, name, null);
144    }
145
146    /**
147     * Construct a new RB
148     *
149     * @param caller The calling object. This is used to get the package name
150     * to further construct the basename as well as to get the proper ClassLoader
151     * @param name The name of the property file without the ".properties" extension
152     * @param local The local
153     */

154    public RB(Object JavaDoc caller, String JavaDoc name, Locale JavaDoc locale) throws MissingResourceException JavaDoc
155    {
156       ClassLoader JavaDoc cl = null;
157
158       if (caller != null)
159       {
160
161          Class JavaDoc c;
162          if (caller instanceof Class JavaDoc)
163          {
164             c = (Class JavaDoc)caller;
165          }
166          else
167          {
168             c = caller.getClass();
169          }
170
171          // Get the appropriate class loader
172
cl = c.getClassLoader();
173
174          if (name.indexOf("/") == -1)
175          {
176
177             // Create the full basename only if not given
178
String JavaDoc fullName = c.getName();
179
180             int pos = fullName.lastIndexOf(".");
181             if (pos > 0)
182             {
183                name = fullName.substring(0, pos + 1).replace('.', '/') + name;
184             }
185          }
186       }
187       else
188       {
189          // Try the shared default properties file...
190
if (name.indexOf("/") == -1)
191          {
192             name = "org/jboss/axis/default-resource";
193          }
194       }
195
196       Locale JavaDoc defaultLocale = Locale.getDefault();
197
198       // If the locale given is the same as the default locale, ignore it
199
if (locale != null)
200       {
201          if (locale.equals(defaultLocale))
202          {
203             locale = null;
204          }
205       }
206
207       // Load the properties. If no property files exist then a
208
// MissingResourceException will be thrown
209
loadProperties(name, cl, locale, defaultLocale);
210    }
211
212    /**
213     * Gets a string message from the resource bundle for the given key
214     *
215     * @param key The resource key
216     * @return The message
217     */

218    public String JavaDoc getString(String JavaDoc key) throws MissingResourceException JavaDoc
219    {
220       return getString(key, (Object JavaDoc[])null);
221    }
222
223    /**
224     * <p>Gets a string message from the resource bundle for the given key. The
225     * message may contain variables that will be substituted with the given
226     * arguments. Variables have the format:</p>
227     * <dir>
228     * This message has two variables: {0} and {1}
229     * </dir>
230     *
231     * @param key The resource key
232     * @param arg0 The argument to place in variable {0}
233     * @return The message
234     */

235    public String JavaDoc getString(String JavaDoc key, Object JavaDoc arg0) throws MissingResourceException JavaDoc
236    {
237       Object JavaDoc[] o = new Object JavaDoc[1];
238       o[0] = arg0;
239       return getString(key, o);
240    }
241
242    /**
243     * <p>Gets a string message from the resource bundle for the given key. The
244     * message may contain variables that will be substituted with the given
245     * arguments. Variables have the format:</p>
246     * <dir>
247     * This message has two variables: {0} and {1}
248     * </dir>
249     *
250     * @param key The resource key
251     * @param arg0 The argument to place in variable {0}
252     * @param arg1 The argument to place in variable {1}
253     * @return The message
254     */

255    public String JavaDoc getString(String JavaDoc key, Object JavaDoc arg0, Object JavaDoc arg1) throws MissingResourceException JavaDoc
256    {
257       Object JavaDoc[] o = new Object JavaDoc[2];
258       o[0] = arg0;
259       o[1] = arg1;
260       return getString(key, o);
261    }
262
263    /**
264     * <p>Gets a string message from the resource bundle for the given key. The
265     * message may contain variables that will be substituted with the given
266     * arguments. Variables have the format:</p>
267     * <dir>
268     * This message has two variables: {0} and {1}
269     * </dir>
270     *
271     * @param key The resource key
272     * @param arg0 The argument to place in variable {0}
273     * @param arg1 The argument to place in variable {1}
274     * @param arg2 The argument to place in variable {1}
275     * @return The message
276     */

277    public String JavaDoc getString(String JavaDoc key, Object JavaDoc arg0, Object JavaDoc arg1, Object JavaDoc arg2) throws MissingResourceException JavaDoc
278    {
279       Object JavaDoc[] o = new Object JavaDoc[3];
280       o[0] = arg0;
281       o[1] = arg1;
282       o[2] = arg2;
283       return getString(key, o);
284    }
285
286    /**
287     * <p>Gets a string message from the resource bundle for the given key. The
288     * message may contain variables that will be substituted with the given
289     * arguments. Variables have the format:</p>
290     * <dir>
291     * This message has two variables: {0} and {1}
292     * </dir>
293     *
294     * @param key The resource key
295     * @param array An array of objects to place in corresponding variables
296     * @return The message
297     */

298    public String JavaDoc getString(String JavaDoc key, Object JavaDoc[] array) throws MissingResourceException JavaDoc
299    {
300       String JavaDoc msg = null;
301       if (resourceProperties != null)
302       {
303          msg = resourceProperties.getProperty(key);
304       }
305
306       if (msg == null)
307       {
308          throw new MissingResourceException JavaDoc("Cannot find resource key \"" + key +
309                  "\" in base name " + basePropertyFileName,
310                  basePropertyFileName, key);
311       }
312
313       msg = MessageFormat.format(msg, array);
314       return msg;
315    }
316
317    protected void loadProperties(String JavaDoc basename, ClassLoader JavaDoc loader, Locale JavaDoc locale,
318                                  Locale JavaDoc defaultLocale)
319            throws MissingResourceException JavaDoc
320    {
321       // Check the cache first
322
String JavaDoc loaderName = "";
323       if (loader != null)
324       {
325          loaderName = ":" + loader.hashCode();
326       }
327       String JavaDoc cacheKey = basename + ":" + locale + ":" + defaultLocale + loaderName;
328       Properties JavaDoc p = (Properties JavaDoc)propertyCache.get(cacheKey);
329       basePropertyFileName = basename + PROPERTY_EXT;
330
331       if (p == null)
332       {
333          // The properties were not found in the cache. Search the given locale
334
// first
335
if (locale != null)
336          {
337             p = loadProperties(basename, loader, locale, p);
338          }
339
340          // Search the default locale
341
if (defaultLocale != null)
342          {
343             p = loadProperties(basename, loader, defaultLocale, p);
344          }
345
346          // Search for the basename
347
p = merge(p, loadProperties(basePropertyFileName, loader));
348
349          if (p == null)
350          {
351             throw new MissingResourceException JavaDoc("Cannot find resource for base name " +
352                     basePropertyFileName, basePropertyFileName, "");
353          }
354
355          // Cache the properties
356
propertyCache.put(cacheKey, p);
357
358       }
359
360       resourceProperties = p;
361    }
362
363    protected Properties JavaDoc loadProperties(String JavaDoc basename, ClassLoader JavaDoc loader, Locale JavaDoc locale,
364                                        Properties JavaDoc props)
365    {
366
367       String JavaDoc language = locale.getLanguage();
368       String JavaDoc country = locale.getCountry();
369       String JavaDoc variant = locale.getVariant();
370       if (variant != null)
371       {
372          if (variant.trim().length() == 0)
373          {
374             variant = null;
375          }
376       }
377
378       if (language != null)
379       {
380
381          if (country != null)
382          {
383
384             if (variant != null)
385             {
386                props = merge(props, loadProperties(basename + "_" + language + "_" + country + "_" + variant +
387                        PROPERTY_EXT, loader));
388             }
389             props = merge(props, loadProperties(basename + "_" + language + "_" + country +
390                     PROPERTY_EXT, loader));
391          }
392          props = merge(props, loadProperties(basename + "_" + language + PROPERTY_EXT, loader));
393       }
394       return props;
395    }
396
397    protected Properties JavaDoc loadProperties(String JavaDoc resname, ClassLoader JavaDoc loader)
398    {
399       Properties JavaDoc props = null;
400
401       // Attempt to open and load the properties
402
InputStream JavaDoc in = null;
403       try
404       {
405          if (loader != null)
406          {
407             in = loader.getResourceAsStream(resname);
408          }
409
410          // Either we're using the system class loader or we didn't find the
411
// resource using the given class loader
412
if (in == null)
413          {
414             in = ClassLoader.getSystemResourceAsStream(resname);
415          }
416          if (in != null)
417          {
418             props = new Properties JavaDoc();
419             try
420             {
421                props.load(in);
422             }
423             catch (IOException JavaDoc ex)
424             {
425                // On error, clear the props
426
props = null;
427             }
428          }
429       }
430       finally
431       {
432          if (in != null)
433          {
434             try
435             {
436                in.close();
437             }
438             catch (Exception JavaDoc ex)
439             {
440                // Ignore error on close
441
}
442          }
443       }
444       return props;
445    }
446
447    /**
448     * Merge two Properties objects
449     */

450    protected Properties JavaDoc merge(Properties JavaDoc p1, Properties JavaDoc p2)
451    {
452       if ((p1 == null) &&
453               (p2 == null))
454       {
455          return null;
456       }
457       else if (p1 == null)
458       {
459          return p2;
460       }
461       else if (p2 == null)
462       {
463          return p1;
464       }
465
466       // Now merge. p1 takes precedence
467
Enumeration JavaDoc en = p2.keys();
468       while (en.hasMoreElements())
469       {
470          String JavaDoc key = (String JavaDoc)en.nextElement();
471          if (p1.getProperty(key) == null)
472          {
473             p1.put(key, p2.getProperty(key));
474          }
475       }
476
477       return p1;
478    }
479
480    /**
481     * Get the underlying properties
482     */

483    public Properties JavaDoc getProperties()
484    {
485       return resourceProperties;
486    }
487
488    // STATIC ACCESSORS
489

490    /**
491     * Get a message from resource.properties from the package of the given object.
492     *
493     * @param caller The calling object, used to get the package name and class loader
494     * @param key The resource key
495     * @return The formatted message
496     */

497    public static String JavaDoc getString(Object JavaDoc caller, String JavaDoc key)
498            throws MissingResourceException JavaDoc
499    {
500       return getMessage(caller, BASE_NAME, null, key, null);
501    }
502
503    /**
504     * Get a message from resource.properties from the package of the given object.
505     *
506     * @param caller The calling object, used to get the package name and class loader
507     * @param key The resource key
508     * @param arg0 The argument to place in variable {0}
509     * @return The formatted message
510     */

511    public static String JavaDoc getString(Object JavaDoc caller, String JavaDoc key, Object JavaDoc arg0)
512            throws MissingResourceException JavaDoc
513    {
514       Object JavaDoc[] o = new Object JavaDoc[1];
515       o[0] = arg0;
516       return getMessage(caller, BASE_NAME, null, key, o);
517    }
518
519    /**
520     * Get a message from resource.properties from the package of the given object.
521     *
522     * @param caller The calling object, used to get the package name and class loader
523     * @param key The resource key
524     * @param arg0 The argument to place in variable {0}
525     * @param arg1 The argument to place in variable {1}
526     * @return The formatted message
527     */

528    public static String JavaDoc getString(Object JavaDoc caller, String JavaDoc key, Object JavaDoc arg0, Object JavaDoc arg1)
529            throws MissingResourceException JavaDoc
530    {
531       Object JavaDoc[] o = new Object JavaDoc[2];
532       o[0] = arg0;
533       o[1] = arg1;
534       return getMessage(caller, BASE_NAME, null, key, o);
535    }
536
537    /**
538     * Get a message from resource.properties from the package of the given object.
539     *
540     * @param caller The calling object, used to get the package name and class loader
541     * @param key The resource key
542     * @param arg0 The argument to place in variable {0}
543     * @param arg1 The argument to place in variable {1}
544     * @param arg2 The argument to place in variable {2}
545     * @return The formatted message
546     */

547    public static String JavaDoc getString(Object JavaDoc caller, String JavaDoc key, Object JavaDoc arg0, Object JavaDoc arg1, Object JavaDoc arg2)
548            throws MissingResourceException JavaDoc
549    {
550       Object JavaDoc[] o = new Object JavaDoc[3];
551       o[0] = arg0;
552       o[1] = arg1;
553       o[2] = arg2;
554       return getMessage(caller, BASE_NAME, null, key, o);
555    }
556
557    /**
558     * Get a message from resource.properties from the package of the given object.
559     *
560     * @param caller The calling object, used to get the package name and class loader
561     * @param key The resource key
562     * @param arg0 The argument to place in variable {0}
563     * @param arg1 The argument to place in variable {1}
564     * @param arg2 The argument to place in variable {2}
565     * @param arg3 The argument to place in variable {3}
566     * @return The formatted message
567     */

568    public static String JavaDoc getString(Object JavaDoc caller, String JavaDoc key, Object JavaDoc arg0, Object JavaDoc arg1, Object JavaDoc arg2, Object JavaDoc arg3)
569            throws MissingResourceException JavaDoc
570    {
571       Object JavaDoc[] o = new Object JavaDoc[4];
572       o[0] = arg0;
573       o[1] = arg1;
574       o[2] = arg2;
575       o[3] = arg3;
576       return getMessage(caller, BASE_NAME, null, key, o);
577    }
578
579
580    /**
581     * Get a message from resource.properties from the package of the given object.
582     *
583     * @param caller The calling object, used to get the package name and class loader
584     * @param key The resource key
585     * @param arg0 The argument to place in variable {0}
586     * @param arg1 The argument to place in variable {1}
587     * @param arg2 The argument to place in variable {2}
588     * @param arg3 The argument to place in variable {3}
589     * @param arg4 The argument to place in variable {4}
590     * @return The formatted message
591     */

592    public static String JavaDoc getString(Object JavaDoc caller, String JavaDoc key, Object JavaDoc arg0, Object JavaDoc arg1, Object JavaDoc arg2, Object JavaDoc arg3, Object JavaDoc arg4)
593            throws MissingResourceException JavaDoc
594    {
595       Object JavaDoc[] o = new Object JavaDoc[5];
596       o[0] = arg0;
597       o[1] = arg1;
598       o[2] = arg2;
599       o[3] = arg3;
600       o[4] = arg4;
601       return getMessage(caller, BASE_NAME, null, key, o);
602    }
603
604
605    /**
606     * Get a message from resource.properties from the package of the given object.
607     *
608     * @param caller The calling object, used to get the package name and class loader
609     * @param key The resource key
610     * @param array An array of objects to place in corresponding variables
611     * @return The formatted message
612     */

613    public static String JavaDoc getString(Object JavaDoc caller, String JavaDoc key, Object JavaDoc[] args)
614            throws MissingResourceException JavaDoc
615    {
616       return getMessage(caller, BASE_NAME, null, key, args);
617    }
618
619
620    /**
621     * Get a message from resource.properties from the package of the given object.
622     *
623     * @param caller The calling object, used to get the package name and class loader
624     * @param locale The locale
625     * @param key The resource key
626     * @return The formatted message
627     */

628    public static String JavaDoc getString(Object JavaDoc caller, Locale JavaDoc locale, String JavaDoc key)
629            throws MissingResourceException JavaDoc
630    {
631       return getMessage(caller, BASE_NAME, locale, key, null);
632    }
633
634    /**
635     * Get a message from resource.properties from the package of the given object.
636     *
637     * @param caller The calling object, used to get the package name and class loader
638     * @param locale The locale
639     * @param key The resource key
640     * @param arg0 The argument to place in variable {0}
641     * @return The formatted message
642     */

643    public static String JavaDoc getString(Object JavaDoc caller, Locale JavaDoc locale, String JavaDoc key, Object JavaDoc arg0)
644            throws MissingResourceException JavaDoc
645    {
646       Object JavaDoc[] o = new Object JavaDoc[1];
647       o[0] = arg0;
648       return getMessage(caller, BASE_NAME, locale, key, o);
649    }
650
651    /**
652     * Get a message from resource.properties from the package of the given object.
653     *
654     * @param caller The calling object, used to get the package name and class loader
655     * @param locale The locale
656     * @param key The resource key
657     * @param arg0 The argument to place in variable {0}
658     * @param arg1 The argument to place in variable {1}
659     * @return The formatted message
660     */

661    public static String JavaDoc getString(Object JavaDoc caller, Locale JavaDoc locale, String JavaDoc key, Object JavaDoc arg0, Object JavaDoc arg1)
662            throws MissingResourceException JavaDoc
663    {
664       Object JavaDoc[] o = new Object JavaDoc[2];
665       o[0] = arg0;
666       o[1] = arg1;
667       return getMessage(caller, BASE_NAME, locale, key, o);
668    }
669
670    /**
671     * Get a message from resource.properties from the package of the given object.
672     *
673     * @param caller The calling object, used to get the package name and class loader
674     * @param locale The locale
675     * @param key The resource key
676     * @param arg0 The argument to place in variable {0}
677     * @param arg1 The argument to place in variable {1}
678     * @param arg2 The argument to place in variable {2}
679     * @return The formatted message
680     */

681    public static String JavaDoc getString(Object JavaDoc caller, Locale JavaDoc locale, String JavaDoc key, Object JavaDoc arg0, Object JavaDoc arg1, Object JavaDoc arg2)
682            throws MissingResourceException JavaDoc
683    {
684       Object JavaDoc[] o = new Object JavaDoc[3];
685       o[0] = arg0;
686       o[1] = arg1;
687       o[2] = arg2;
688       return getMessage(caller, BASE_NAME, locale, key, o);
689    }
690
691    /**
692     * Get a message from resource.properties from the package of the given object.
693     *
694     * @param caller The calling object, used to get the package name and class loader
695     * @param locale The locale
696     * @param key The resource key
697     * @param arg0 The argument to place in variable {0}
698     * @param arg1 The argument to place in variable {1}
699     * @param arg2 The argument to place in variable {2}
700     * @param arg3 The argument to place in variable {3}
701     * @return The formatted message
702     */

703    public static String JavaDoc getString(Object JavaDoc caller, Locale JavaDoc locale, String JavaDoc key, Object JavaDoc arg0, Object JavaDoc arg1, Object JavaDoc arg2, Object JavaDoc arg3)
704            throws MissingResourceException JavaDoc
705    {
706       Object JavaDoc[] o = new Object JavaDoc[4];
707       o[0] = arg0;
708       o[1] = arg1;
709       o[2] = arg2;
710       o[3] = arg3;
711       return getMessage(caller, BASE_NAME, locale, key, o);
712    }
713
714    /**
715     * Get a message from resource.properties from the package of the given object.
716     *
717     * @param caller The calling object, used to get the package name and class loader
718     * @param locale The locale
719     * @param key The resource key
720     * @param arg0 The argument to place in variable {0}
721     * @param arg1 The argument to place in variable {1}
722     * @param arg2 The argument to place in variable {2}
723     * @param arg3 The argument to place in variable {3}
724     * @return The formatted message
725     */

726    public static String JavaDoc getString(Object JavaDoc caller, Locale JavaDoc locale, String JavaDoc key, Object JavaDoc arg0, Object JavaDoc arg1, Object JavaDoc arg2, Object JavaDoc arg3, Object JavaDoc arg4)
727            throws MissingResourceException JavaDoc
728    {
729       Object JavaDoc[] o = new Object JavaDoc[5];
730       o[0] = arg0;
731       o[1] = arg1;
732       o[2] = arg2;
733       o[3] = arg3;
734       o[4] = arg4;
735       return getMessage(caller, BASE_NAME, locale, key, o);
736    }
737
738    /**
739     * Get a message from resource.properties from the package of the given object.
740     *
741     * @param caller The calling object, used to get the package name and class loader
742     * @param locale The locale
743     * @param key The resource key
744     * @param array An array of objects to place in corresponding variables
745     * @return The formatted message
746     */

747    public static String JavaDoc getString(Object JavaDoc caller, Locale JavaDoc locale, String JavaDoc key, Object JavaDoc[] args)
748            throws MissingResourceException JavaDoc
749    {
750       return getMessage(caller, BASE_NAME, locale, key, args);
751    }
752
753    // Workhorse that does the resource loading and key lookup
754
public static String JavaDoc getMessage(Object JavaDoc caller, String JavaDoc basename, Locale JavaDoc locale, String JavaDoc key,
755                                    Object JavaDoc[] args)
756            throws MissingResourceException JavaDoc
757    {
758       String JavaDoc msg = null;
759       MissingResourceException JavaDoc firstEx = null;
760       String JavaDoc fullName = null;
761       Class JavaDoc curClass = null;
762       boolean didNull = false;
763
764       if (caller != null)
765       {
766          if (caller instanceof Class JavaDoc)
767             curClass = (Class JavaDoc)caller;
768          else
769             curClass = caller.getClass();
770       }
771
772       while (msg == null)
773       {
774
775          // Get the full name of the resource
776
if (curClass != null)
777          {
778
779             // Create the full basename
780
String JavaDoc pkgName = curClass.getName();
781
782             int pos = pkgName.lastIndexOf(".");
783             if (pos > 0)
784             {
785                fullName = pkgName.substring(0, pos + 1).replace('.', '/') + basename;
786             }
787             else
788             {
789                fullName = basename;
790             }
791          }
792          else
793          {
794             fullName = basename;
795          }
796
797          try
798          {
799             RB rb = new RB(caller, fullName, locale);
800             msg = rb.getString(key, args);
801          }
802          catch (MissingResourceException JavaDoc ex)
803          {
804             if (curClass == null)
805             {
806                throw ex;
807             }
808
809             // Save the first exception
810
if (firstEx == null)
811             {
812                firstEx = ex;
813             }
814
815             // Get the superclass
816
curClass = curClass.getSuperclass();
817             if (curClass == null)
818             {
819                if (didNull)
820                   throw firstEx;
821                didNull = true;
822                caller = null;
823             }
824             else
825             {
826                String JavaDoc cname = curClass.getName();
827                if (cname.startsWith("java.") ||
828                        cname.startsWith("javax."))
829                {
830                   if (didNull)
831                      throw firstEx;
832                   didNull = true;
833                   caller = null;
834                   curClass = null;
835                }
836             }
837          }
838
839       }
840       return msg;
841    }
842
843    /**
844     * Clears the internal cache
845     */

846    public static void clearCache()
847    {
848       propertyCache.clear();
849    }
850 }
851
Popular Tags