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: XPATHMessages.java,v 1.5 2004/02/17 04:36:46 minchau Exp $
  18. */
  19. package com.sun.org.apache.xpath.internal.res;
  20. import java.util.ListResourceBundle;
  21. import com.sun.org.apache.xml.internal.res.XMLMessages;
  22. /**
  23. * A utility class for issuing XPath error messages.
  24. * @xsl.usage internal
  25. */
  26. public class XPATHMessages extends XMLMessages
  27. {
  28. /** The language specific resource object for XPath messages. */
  29. private static ListResourceBundle XPATHBundle = null;
  30. /** The class name of the XPath error message string table. */
  31. private static final String XPATH_ERROR_RESOURCES =
  32. "com.sun.org.apache.xpath.internal.res.XPATHErrorResources";
  33. /**
  34. * Creates a message from the specified key and replacement
  35. * arguments, localized to the given locale.
  36. *
  37. * @param errorCode The key for the message text.
  38. * @param args The arguments to be used as replacement text
  39. * in the message created.
  40. *
  41. * @return The formatted message string.
  42. */
  43. public static final String createXPATHMessage(String msgKey, Object args[]) //throws Exception
  44. {
  45. if (XPATHBundle == null)
  46. XPATHBundle = loadResourceBundle(XPATH_ERROR_RESOURCES);
  47. if (XPATHBundle != null)
  48. {
  49. return createXPATHMsg(XPATHBundle, msgKey, args);
  50. }
  51. else
  52. return "Could not load any resource bundles.";
  53. }
  54. /**
  55. * Creates a message from the specified key and replacement
  56. * arguments, localized to the given locale.
  57. *
  58. * @param msgKey The key for the message text.
  59. * @param args The arguments to be used as replacement text
  60. * in the message created.
  61. *
  62. * @return The formatted warning string.
  63. */
  64. public static final String createXPATHWarning(String msgKey, Object args[]) //throws Exception
  65. {
  66. if (XPATHBundle == null)
  67. XPATHBundle = loadResourceBundle(XPATH_ERROR_RESOURCES);
  68. if (XPATHBundle != null)
  69. {
  70. return createXPATHMsg(XPATHBundle, msgKey, args);
  71. }
  72. else
  73. return "Could not load any resource bundles.";
  74. }
  75. /**
  76. * Creates a message from the specified key and replacement
  77. * arguments, localized to the given locale.
  78. *
  79. * @param errorCode The key for the message text.
  80. *
  81. * @param fResourceBundle The resource bundle to use.
  82. * @param msgKey The message key to use.
  83. * @param args The arguments to be used as replacement text
  84. * in the message created.
  85. *
  86. * @return The formatted message string.
  87. */
  88. public static final String createXPATHMsg(ListResourceBundle fResourceBundle,
  89. String msgKey, Object args[]) //throws Exception
  90. {
  91. String fmsg = null;
  92. boolean throwex = false;
  93. String msg = null;
  94. if (msgKey != null)
  95. msg = fResourceBundle.getString(msgKey);
  96. if (msg == null)
  97. {
  98. msg = fResourceBundle.getString(XPATHErrorResources.BAD_CODE);
  99. throwex = true;
  100. }
  101. if (args != null)
  102. {
  103. try
  104. {
  105. // Do this to keep format from crying.
  106. // This is better than making a bunch of conditional
  107. // code all over the place.
  108. int n = args.length;
  109. for (int i = 0; i < n; i++)
  110. {
  111. if (null == args[i])
  112. args[i] = "";
  113. }
  114. fmsg = java.text.MessageFormat.format(msg, args);
  115. }
  116. catch (Exception e)
  117. {
  118. fmsg = fResourceBundle.getString(XPATHErrorResources.FORMAT_FAILED);
  119. fmsg += " " + msg;
  120. }
  121. }
  122. else
  123. fmsg = msg;
  124. if (throwex)
  125. {
  126. throw new RuntimeException(fmsg);
  127. }
  128. return fmsg;
  129. }
  130. }