1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 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 "Xalan" 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 and was
  52. * originally based on software copyright (c) 1999, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xalan.res;
  58. import java.util.Locale;
  59. import java.util.ResourceBundle;
  60. import java.util.ListResourceBundle;
  61. import java.util.MissingResourceException;
  62. import org.apache.xpath.res.XPATHErrorResources;
  63. /**
  64. * <meta name="usage" content="internal"/>
  65. * Sets things up for issuing error messages. This class is misnamed, and
  66. * should be called XalanMessages, or some such.
  67. */
  68. public class XSLMessages
  69. {
  70. /** The local object to use. */
  71. private Locale fLocale = Locale.getDefault();
  72. /** The language specific resource object for Xalan messages. */
  73. private static ListResourceBundle XSLTBundle = null;
  74. /** The language specific resource object for XPath messages. */
  75. private static ListResourceBundle XPATHBundle = null;
  76. /** The class name of the Xalan error message string table. */
  77. private static final String XSLT_ERROR_RESOURCES =
  78. "org.apache.xalan.res.XSLTErrorResources";
  79. /** The class name of the XPath error message string table. */
  80. private static final String XPATH_ERROR_RESOURCES =
  81. "org.apache.xpath.res.XPATHErrorResources";
  82. /** String to use if a bad message code is used. */
  83. private static String BAD_CODE = "BAD_CODE";
  84. /** String to use if the message format operation failed. */
  85. private static String FORMAT_FAILED = "FORMAT_FAILED";
  86. /**
  87. * Set the Locale object to use.
  88. *
  89. * @param locale non-null reference to Locale object.
  90. */
  91. public void setLocale(Locale locale)
  92. {
  93. fLocale = locale;
  94. }
  95. /**
  96. * Get the Locale object that is being used.
  97. *
  98. * @return non-null reference to Locale object.
  99. */
  100. public Locale getLocale()
  101. {
  102. return fLocale;
  103. }
  104. /**
  105. * Creates a message from the specified key and replacement
  106. * arguments, localized to the given locale.
  107. *
  108. * @param msgKey The key for the message text.
  109. * @param args The arguments to be used as replacement text
  110. * in the message created.
  111. *
  112. * @return The formatted warning string.
  113. */
  114. public static final String createXPATHWarning(String msgKey, Object args[]) //throws Exception
  115. {
  116. if (XPATHBundle == null)
  117. XPATHBundle = loadResourceBundle(XPATH_ERROR_RESOURCES);
  118. if (XPATHBundle != null)
  119. {
  120. return createXPATHMsg(XPATHBundle, msgKey, args);
  121. }
  122. else
  123. return "Could not load any resource bundles.";
  124. }
  125. /**
  126. * Creates a message from the specified key and replacement
  127. * arguments, localized to the given locale.
  128. *
  129. * @param errorCode The key for the message text.
  130. * @param args The arguments to be used as replacement text
  131. * in the message created.
  132. *
  133. * @return The formatted message string.
  134. */
  135. public static final String createXPATHMessage(String msgKey, Object args[]) //throws Exception
  136. {
  137. if (XPATHBundle == null)
  138. XPATHBundle = loadResourceBundle(XPATH_ERROR_RESOURCES);
  139. if (XPATHBundle != null)
  140. {
  141. return createXPATHMsg(XPATHBundle, msgKey, args);
  142. }
  143. else
  144. return "Could not load any resource bundles.";
  145. }
  146. /**
  147. * Creates a message from the specified key and replacement
  148. * arguments, localized to the given locale.
  149. *
  150. * @param errorCode The key for the message text.
  151. *
  152. * @param fResourceBundle The resource bundle to use.
  153. * @param msgKey The message key to use.
  154. * @param args The arguments to be used as replacement text
  155. * in the message created.
  156. *
  157. * @return The formatted message string.
  158. */
  159. public static final String createXPATHMsg(ListResourceBundle fResourceBundle,
  160. String msgKey, Object args[]) //throws Exception
  161. {
  162. String fmsg = null;
  163. boolean throwex = false;
  164. String msg = null;
  165. if (msgKey != null)
  166. msg = fResourceBundle.getString( msgKey);
  167. if (msg == null)
  168. {
  169. msg = fResourceBundle.getString( XPATHErrorResources.BAD_CODE);
  170. throwex = true;
  171. }
  172. if (args != null)
  173. {
  174. try
  175. {
  176. // Do this to keep format from crying.
  177. // This is better than making a bunch of conditional
  178. // code all over the place.
  179. int n = args.length;
  180. for (int i = 0; i < n; i++)
  181. {
  182. if (null == args[i])
  183. args[i] = "";
  184. }
  185. fmsg = java.text.MessageFormat.format(msg, args);
  186. }
  187. catch (Exception e)
  188. {
  189. fmsg = fResourceBundle.getString(XPATHErrorResources.FORMAT_FAILED);
  190. fmsg += " " + msg;
  191. }
  192. }
  193. else
  194. fmsg = msg;
  195. if (throwex)
  196. {
  197. throw new RuntimeException(fmsg);
  198. }
  199. return fmsg;
  200. }
  201. /**
  202. * Creates a message from the specified key and replacement
  203. * arguments, localized to the given locale.
  204. *
  205. * @param errorCode The key for the message text.
  206. * @param args The arguments to be used as replacement text
  207. * in the message created.
  208. *
  209. * @return The formatted message string.
  210. */
  211. public static final String createMessage(String msgKey, Object args[]) //throws Exception
  212. {
  213. if (XSLTBundle == null)
  214. XSLTBundle = loadResourceBundle(XSLT_ERROR_RESOURCES);
  215. if (XSLTBundle != null)
  216. {
  217. return createMsg(XSLTBundle, msgKey, args);
  218. }
  219. else
  220. return "Could not load any resource bundles.";
  221. }
  222. /**
  223. * Creates a message from the specified key and replacement
  224. * arguments, localized to the given locale.
  225. *
  226. * @param msgKey The key for the message text.
  227. * @param args The arguments to be used as replacement text
  228. * in the message created.
  229. *
  230. * @return The formatted warning string.
  231. */
  232. public static final String createWarning(String msgKey, Object args[]) //throws Exception
  233. {
  234. if (XSLTBundle == null)
  235. XSLTBundle = loadResourceBundle(XSLT_ERROR_RESOURCES);
  236. if (XSLTBundle != null)
  237. {
  238. return createMsg(XSLTBundle, msgKey, args);
  239. }
  240. else
  241. return "Could not load any resource bundles.";
  242. }
  243. /**
  244. * Creates a message from the specified key and replacement
  245. * arguments, localized to the given locale.
  246. *
  247. * @param errorCode The key for the message text.
  248. *
  249. * @param fResourceBundle The resource bundle to use.
  250. * @param msgKey The message key to use.
  251. * @param args The arguments to be used as replacement text
  252. * in the message created.
  253. *
  254. * @return The formatted message string.
  255. */
  256. public static final String createMsg(ListResourceBundle fResourceBundle,
  257. String msgKey, Object args[]) //throws Exception
  258. {
  259. String fmsg = null;
  260. boolean throwex = false;
  261. String msg = null;
  262. if (msgKey != null)
  263. msg = fResourceBundle.getString( msgKey);
  264. if (msg == null)
  265. {
  266. msg = fResourceBundle.getString(BAD_CODE);
  267. throwex = true;
  268. }
  269. if (args != null)
  270. {
  271. try
  272. {
  273. // Do this to keep format from crying.
  274. // This is better than making a bunch of conditional
  275. // code all over the place.
  276. int n = args.length;
  277. for (int i = 0; i < n; i++)
  278. {
  279. if (null == args[i])
  280. args[i] = "";
  281. }
  282. fmsg = java.text.MessageFormat.format(msg, args);
  283. }
  284. catch (Exception e)
  285. {
  286. fmsg = fResourceBundle.getString(FORMAT_FAILED);
  287. fmsg += " " + msg;
  288. }
  289. }
  290. else
  291. fmsg = msg;
  292. if (throwex)
  293. {
  294. throw new RuntimeException(fmsg);
  295. }
  296. return fmsg;
  297. }
  298. /**
  299. * Return a named ResourceBundle for a particular locale. This method mimics the behavior
  300. * of ResourceBundle.getBundle().
  301. *
  302. * @param res the name of the resource to load.
  303. * @param locale the locale to prefer when searching for the bundle
  304. *
  305. * @param className The class name of the resource bundle.
  306. * @return the ResourceBundle
  307. * @throws MissingResourceException
  308. */
  309. public static final ListResourceBundle loadResourceBundle(String className)
  310. throws MissingResourceException
  311. {
  312. Locale locale = Locale.getDefault();
  313. // String suffix = getResourceSuffix(locale);
  314. try
  315. {
  316. //System.out.println("resource " +className+suffix);
  317. // first try with the given locale
  318. return (ListResourceBundle) ResourceBundle.getBundle(className, locale);
  319. }
  320. catch (MissingResourceException e)
  321. {
  322. try // try to fall back to en_US if we can't load
  323. {
  324. // Since we can't find the localized property file,
  325. // fall back to en_US.
  326. return (ListResourceBundle) ResourceBundle.getBundle(
  327. XSLT_ERROR_RESOURCES, new Locale("en", "US"));
  328. }
  329. catch (MissingResourceException e2)
  330. {
  331. // Now we are really in trouble.
  332. // very bad, definitely very bad...not going to get very far
  333. throw new MissingResourceException(
  334. "Could not load any resource bundles." + className, className, "");
  335. }
  336. }
  337. }
  338. /**
  339. * Return the resource file suffic for the indicated locale
  340. * For most locales, this will be based the language code. However
  341. * for Chinese, we do distinguish between Taiwan and PRC
  342. *
  343. * @param locale the locale
  344. * @return an String suffix which can be appended to a resource name
  345. */
  346. private static final String getResourceSuffix(Locale locale)
  347. {
  348. String suffix = "_" + locale.getLanguage();
  349. String country = locale.getCountry();
  350. if (country.equals("TW"))
  351. suffix += "_" + country;
  352. return suffix;
  353. }
  354. }