KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > error > ErrorRegistry


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.error;
8
9
10 import java.text.MessageFormat JavaDoc;
11 import java.util.Locale JavaDoc;
12 import java.util.MissingResourceException JavaDoc;
13 import java.util.ResourceBundle JavaDoc;
14
15
16 /**
17  * <p>
18  * This class loads all the errors from the properties files
19  * and stores them. It also allows classes to lookup errors
20  * by keys.
21  * </p>
22  *
23  * <p>
24  * This works on the principle of bundle names. A bundle name
25  * is passed in along with a key and an error message is
26  * returned. This allows multiple bundles to be used.
27  * </p>
28  *
29  * <p>
30  * This registry is read only
31  * </p>
32  *
33  * @author Brian Pontarelli
34  */

35 public class ErrorRegistry {
36
37     /**
38      * Returns the error using the key and the calling object to construct the
39      * full key to the error. So, if the caller is com.foo.Bar and the key is
40      * someError, then this method searchs in the properties file for an error
41      * with the key com.foo.Bar.someError. If the caller object is null, only
42      * the key is used. Once the error has been found, if the params array is
43      * not null, then the MessageFormat class is used to format the error string
44      * using the params and the error message from the bundle.
45      *
46      * @param bundleName The name of the resource bundle
47      * @param key The key of the error
48      * @param locale (Optional) The Locale of the bundle. If null, this uses
49      * the system default Locale
50      * @param caller (Optional) The class asking for the error or that the error
51      * is associated with. Prepended to the key when locating the error
52      * message in the bundle
53      * @param params (Optional) Parameter objects that will be used to format
54      * the errorString
55      * @return The error string, possible formatted, or null if the string wasn't
56      * in the bundle
57      */

58     public static String JavaDoc getError(String JavaDoc bundleName, String JavaDoc key, Locale JavaDoc locale,
59             Object JavaDoc caller, Object JavaDoc [] params) throws MissingResourceException JavaDoc {
60         // Grab the default locale if need be
61
if (locale == null) {
62             locale = Locale.getDefault();
63         }
64
65         ResourceBundle JavaDoc rb = ResourceBundle.getBundle(bundleName, locale);
66         String JavaDoc error = null;
67
68         if (caller != null) {
69             StringBuffer JavaDoc buf = new StringBuffer JavaDoc(caller.getClass().getName());
70             buf.append(".").append(key);
71             error = rb.getString(buf.toString());
72
73         } else {
74             error = rb.getString(key);
75         }
76
77         if (params != null) {
78             error = MessageFormat.format(error, params);
79         }
80
81         return error;
82     }
83
84     /**
85      * Calls getError(bundleName, key, locale, caller, null)
86      */

87     public static String JavaDoc getError(String JavaDoc bundleName, String JavaDoc key, Locale JavaDoc locale,
88             Object JavaDoc caller) {
89         return getError(bundleName, key, locale, caller, null);
90     }
91
92     /**
93      * Returns the error from a call to {@link #getError(String, String, Locale,
94      * Object, Object[]) getError(bundleName, key, locale, caller, null)} wrapped
95      * into a BasicError object
96      */

97     public static BasicError getBasicError(String JavaDoc bundleName, String JavaDoc key,
98             Locale JavaDoc locale, Object JavaDoc caller) {
99         String JavaDoc error = getError(bundleName, key, locale, caller, null);
100         BasicError be = new BasicError(error, locale);
101
102         return be;
103     }
104
105     /**
106      * Returns the error from a call to {@link #getError(String, String, Locale,
107      * Object, Object[]) getError(bundleName, key, locale, caller, params)}
108      * wrapped into a BasicError object
109      */

110     public static BasicError getBasicError(String JavaDoc bundleName, String JavaDoc key,
111             Locale JavaDoc locale, Object JavaDoc caller, Object JavaDoc [] params) {
112         String JavaDoc error = getError(bundleName, key, locale, caller, params);
113         BasicError be = new BasicError(error, locale);
114
115         return be;
116     }
117
118     /**
119      * Returns the error from a call to {@link #getError(String, String, Locale,
120      * Object, Object[]) getError(bundleName, key, locale, caller, null)} wrapped
121      * into a PropertyError object
122      */

123     public static PropertyError getPropertyError(String JavaDoc bundleName, String JavaDoc key,
124             Locale JavaDoc locale, String JavaDoc property, Object JavaDoc caller) {
125         String JavaDoc error = getError(bundleName, key, locale, caller, null);
126         PropertyError pe = new PropertyError(property, error, locale);
127
128         return pe;
129     }
130
131     /**
132      * Returns the error from a call to {@link #getError(String, String, Locale,
133      * Object, Object[]) getError(bundleName, key, locale, caller, params)}
134      * wrapped into a PropertyError object
135      */

136     public static PropertyError getPropertyError(String JavaDoc bundleName, String JavaDoc key,
137             Locale JavaDoc locale, String JavaDoc property, Object JavaDoc caller, Object JavaDoc [] params) {
138         String JavaDoc error = getError(bundleName, key, locale, caller, params);
139         PropertyError pe = new PropertyError(property, error, locale);
140
141         return pe;
142     }
143 }
Popular Tags