KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xml > res > XMLMessages


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

16 /*
17  * $Id: XMLMessages.java,v 1.5 2004/02/17 04:14:26 minchau Exp $
18  */

19 package org.apache.xml.res;
20
21 import java.util.ListResourceBundle JavaDoc;
22 import java.util.Locale JavaDoc;
23 import java.util.MissingResourceException JavaDoc;
24 import java.util.ResourceBundle JavaDoc;
25
26 /**
27  * A utility class for issuing XML error messages.
28  * @xsl.usage internal
29  */

30 public class XMLMessages
31 {
32
33   /** The local object to use. */
34   protected Locale JavaDoc fLocale = Locale.getDefault();
35
36   /** The language specific resource object for XML messages. */
37   private static ListResourceBundle JavaDoc XMLBundle = null;
38
39   /** The class name of the XML error message string table. */
40   private static final String JavaDoc XML_ERROR_RESOURCES =
41     "org.apache.xml.res.XMLErrorResources";
42
43   /** String to use if a bad message code is used. */
44   protected static String JavaDoc BAD_CODE = "BAD_CODE";
45
46   /** String to use if the message format operation failed. */
47   protected static String JavaDoc FORMAT_FAILED = "FORMAT_FAILED";
48     
49   /**
50    * Set the Locale object to use.
51    *
52    * @param locale non-null reference to Locale object.
53    */

54    public void setLocale(Locale JavaDoc locale)
55   {
56     fLocale = locale;
57   }
58
59   /**
60    * Get the Locale object that is being used.
61    *
62    * @return non-null reference to Locale object.
63    */

64   public Locale JavaDoc getLocale()
65   {
66     return fLocale;
67   }
68     
69   /**
70    * Creates a message from the specified key and replacement
71    * arguments, localized to the given locale.
72    *
73    * @param errorCode The key for the message text.
74    * @param args The arguments to be used as replacement text
75    * in the message created.
76    *
77    * @return The formatted message string.
78    */

79   public static final String JavaDoc createXMLMessage(String JavaDoc msgKey, Object JavaDoc args[])
80   {
81     if (XMLBundle == null)
82       XMLBundle = loadResourceBundle(XML_ERROR_RESOURCES);
83     
84     if (XMLBundle != null)
85     {
86       return createMsg(XMLBundle, msgKey, args);
87     }
88     else
89       return "Could not load any resource bundles.";
90   }
91
92   /**
93    * Creates a message from the specified key and replacement
94    * arguments, localized to the given locale.
95    *
96    * @param errorCode The key for the message text.
97    *
98    * @param fResourceBundle The resource bundle to use.
99    * @param msgKey The message key to use.
100    * @param args The arguments to be used as replacement text
101    * in the message created.
102    *
103    * @return The formatted message string.
104    */

105   public static final String JavaDoc createMsg(ListResourceBundle JavaDoc fResourceBundle,
106     String JavaDoc msgKey, Object JavaDoc args[]) //throws Exception
107
{
108
109     String JavaDoc fmsg = null;
110     boolean throwex = false;
111     String JavaDoc msg = null;
112
113     if (msgKey != null)
114       msg = fResourceBundle.getString(msgKey);
115
116     if (msg == null)
117     {
118       msg = fResourceBundle.getString(BAD_CODE);
119       throwex = true;
120     }
121
122     if (args != null)
123     {
124       try
125       {
126
127         // Do this to keep format from crying.
128
// This is better than making a bunch of conditional
129
// code all over the place.
130
int n = args.length;
131
132         for (int i = 0; i < n; i++)
133         {
134           if (null == args[i])
135             args[i] = "";
136         }
137
138         fmsg = java.text.MessageFormat.format(msg, args);
139       }
140       catch (Exception JavaDoc e)
141       {
142         fmsg = fResourceBundle.getString(FORMAT_FAILED);
143         fmsg += " " + msg;
144       }
145     }
146     else
147       fmsg = msg;
148
149     if (throwex)
150     {
151       throw new RuntimeException JavaDoc(fmsg);
152     }
153
154     return fmsg;
155   }
156
157   /**
158    * Return a named ResourceBundle for a particular locale. This method mimics the behavior
159    * of ResourceBundle.getBundle().
160    *
161    * @param res the name of the resource to load.
162    * @param locale the locale to prefer when searching for the bundle
163    *
164    * @param className The class name of the resource bundle.
165    * @return the ResourceBundle
166    * @throws MissingResourceException
167    */

168   public static ListResourceBundle JavaDoc loadResourceBundle(String JavaDoc className)
169           throws MissingResourceException JavaDoc
170   {
171     Locale JavaDoc locale = Locale.getDefault();
172
173     try
174     {
175       return (ListResourceBundle JavaDoc)ResourceBundle.getBundle(className, locale);
176     }
177     catch (MissingResourceException JavaDoc e)
178     {
179       try // try to fall back to en_US if we can't load
180
{
181
182         // Since we can't find the localized property file,
183
// fall back to en_US.
184
return (ListResourceBundle JavaDoc)ResourceBundle.getBundle(
185           className, new Locale JavaDoc("en", "US"));
186       }
187       catch (MissingResourceException JavaDoc e2)
188       {
189
190         // Now we are really in trouble.
191
// very bad, definitely very bad...not going to get very far
192
throw new MissingResourceException JavaDoc(
193           "Could not load any resource bundles." + className, className, "");
194       }
195     }
196   }
197
198   /**
199    * Return the resource file suffic for the indicated locale
200    * For most locales, this will be based the language code. However
201    * for Chinese, we do distinguish between Taiwan and PRC
202    *
203    * @param locale the locale
204    * @return an String suffix which can be appended to a resource name
205    */

206   protected static String JavaDoc getResourceSuffix(Locale JavaDoc locale)
207   {
208
209     String JavaDoc suffix = "_" + locale.getLanguage();
210     String JavaDoc country = locale.getCountry();
211
212     if (country.equals("TW"))
213       suffix += "_" + country;
214
215     return suffix;
216   }
217 }
218
Popular Tags