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 com.sun.org.apache.xml.internal.res;
  20. import java.util.ListResourceBundle;
  21. import java.util.Locale;
  22. import java.util.MissingResourceException;
  23. import java.util.ResourceBundle;
  24. /**
  25. * A utility class for issuing XML error messages.
  26. * @xsl.usage internal
  27. */
  28. public class XMLMessages
  29. {
  30. /** The local object to use. */
  31. protected Locale fLocale = Locale.getDefault();
  32. /** The language specific resource object for XML messages. */
  33. private static ListResourceBundle XMLBundle = null;
  34. /** The class name of the XML error message string table. */
  35. private static final String XML_ERROR_RESOURCES =
  36. "com.sun.org.apache.xml.internal.res.XMLErrorResources";
  37. /** String to use if a bad message code is used. */
  38. protected static String BAD_CODE = "BAD_CODE";
  39. /** String to use if the message format operation failed. */
  40. protected static String FORMAT_FAILED = "FORMAT_FAILED";
  41. /**
  42. * Set the Locale object to use.
  43. *
  44. * @param locale non-null reference to Locale object.
  45. */
  46. public void setLocale(Locale locale)
  47. {
  48. fLocale = locale;
  49. }
  50. /**
  51. * Get the Locale object that is being used.
  52. *
  53. * @return non-null reference to Locale object.
  54. */
  55. public Locale getLocale()
  56. {
  57. return fLocale;
  58. }
  59. /**
  60. * Creates a message from the specified key and replacement
  61. * arguments, localized to the given locale.
  62. *
  63. * @param errorCode The key for the message text.
  64. * @param args The arguments to be used as replacement text
  65. * in the message created.
  66. *
  67. * @return The formatted message string.
  68. */
  69. public static final String createXMLMessage(String msgKey, Object args[])
  70. {
  71. if (XMLBundle == null)
  72. XMLBundle = loadResourceBundle(XML_ERROR_RESOURCES);
  73. if (XMLBundle != null)
  74. {
  75. return createMsg(XMLBundle, msgKey, args);
  76. }
  77. else
  78. return "Could not load any resource bundles.";
  79. }
  80. /**
  81. * Creates a message from the specified key and replacement
  82. * arguments, localized to the given locale.
  83. *
  84. * @param errorCode The key for the message text.
  85. *
  86. * @param fResourceBundle The resource bundle to use.
  87. * @param msgKey The message key to use.
  88. * @param args The arguments to be used as replacement text
  89. * in the message created.
  90. *
  91. * @return The formatted message string.
  92. */
  93. public static final String createMsg(ListResourceBundle fResourceBundle,
  94. String msgKey, Object args[]) //throws Exception
  95. {
  96. String fmsg = null;
  97. boolean throwex = false;
  98. String msg = null;
  99. if (msgKey != null)
  100. msg = fResourceBundle.getString(msgKey);
  101. if (msg == null)
  102. {
  103. msg = fResourceBundle.getString(BAD_CODE);
  104. throwex = true;
  105. }
  106. if (args != null)
  107. {
  108. try
  109. {
  110. // Do this to keep format from crying.
  111. // This is better than making a bunch of conditional
  112. // code all over the place.
  113. int n = args.length;
  114. for (int i = 0; i < n; i++)
  115. {
  116. if (null == args[i])
  117. args[i] = "";
  118. }
  119. fmsg = java.text.MessageFormat.format(msg, args);
  120. }
  121. catch (Exception e)
  122. {
  123. fmsg = fResourceBundle.getString(FORMAT_FAILED);
  124. fmsg += " " + msg;
  125. }
  126. }
  127. else
  128. fmsg = msg;
  129. if (throwex)
  130. {
  131. throw new RuntimeException(fmsg);
  132. }
  133. return fmsg;
  134. }
  135. /**
  136. * Return a named ResourceBundle for a particular locale. This method mimics the behavior
  137. * of ResourceBundle.getBundle().
  138. *
  139. * @param res the name of the resource to load.
  140. * @param locale the locale to prefer when searching for the bundle
  141. *
  142. * @param className The class name of the resource bundle.
  143. * @return the ResourceBundle
  144. * @throws MissingResourceException
  145. */
  146. public static ListResourceBundle loadResourceBundle(String className)
  147. throws MissingResourceException
  148. {
  149. Locale locale = Locale.getDefault();
  150. try
  151. {
  152. return (ListResourceBundle)ResourceBundle.getBundle(className, locale);
  153. }
  154. catch (MissingResourceException e)
  155. {
  156. try // try to fall back to en_US if we can't load
  157. {
  158. // Since we can't find the localized property file,
  159. // fall back to en_US.
  160. return (ListResourceBundle)ResourceBundle.getBundle(
  161. className, new Locale("en", "US"));
  162. }
  163. catch (MissingResourceException e2)
  164. {
  165. // Now we are really in trouble.
  166. // very bad, definitely very bad...not going to get very far
  167. throw new MissingResourceException(
  168. "Could not load any resource bundles." + className, className, "");
  169. }
  170. }
  171. }
  172. /**
  173. * Return the resource file suffic for the indicated locale
  174. * For most locales, this will be based the language code. However
  175. * for Chinese, we do distinguish between Taiwan and PRC
  176. *
  177. * @param locale the locale
  178. * @return an String suffix which can be appended to a resource name
  179. */
  180. protected static String getResourceSuffix(Locale locale)
  181. {
  182. String suffix = "_" + locale.getLanguage();
  183. String country = locale.getCountry();
  184. if (country.equals("TW"))
  185. suffix += "_" + country;
  186. return suffix;
  187. }
  188. }