001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 /*
019 * $Id: XPATHMessages.java 468655 2006-10-28 07:12:06Z minchau $
020 */
021 package org.apache.xpath.res;
022
023 import java.util.ListResourceBundle;
024
025 import org.apache.xml.res.XMLMessages;
026
027 /**
028 * A utility class for issuing XPath error messages.
029 * @xsl.usage internal
030 */
031 public class XPATHMessages extends XMLMessages
032 {
033 /** The language specific resource object for XPath messages. */
034 private static ListResourceBundle XPATHBundle = null;
035
036 /** The class name of the XPath error message string table. */
037 private static final String XPATH_ERROR_RESOURCES =
038 "org.apache.xpath.res.XPATHErrorResources";
039
040 /**
041 * Creates a message from the specified key and replacement
042 * arguments, localized to the given locale.
043 *
044 * @param msgKey The key for the message text.
045 * @param args The arguments to be used as replacement text
046 * in the message created.
047 *
048 * @return The formatted message string.
049 */
050 public static final String createXPATHMessage(String msgKey, Object args[]) //throws Exception
051 {
052 if (XPATHBundle == null)
053 XPATHBundle = loadResourceBundle(XPATH_ERROR_RESOURCES);
054
055 if (XPATHBundle != null)
056 {
057 return createXPATHMsg(XPATHBundle, msgKey, args);
058 }
059 else
060 return "Could not load any resource bundles.";
061 }
062
063 /**
064 * Creates a message from the specified key and replacement
065 * arguments, localized to the given locale.
066 *
067 * @param msgKey The key for the message text.
068 * @param args The arguments to be used as replacement text
069 * in the message created.
070 *
071 * @return The formatted warning string.
072 */
073 public static final String createXPATHWarning(String msgKey, Object args[]) //throws Exception
074 {
075 if (XPATHBundle == null)
076 XPATHBundle = loadResourceBundle(XPATH_ERROR_RESOURCES);
077
078 if (XPATHBundle != null)
079 {
080 return createXPATHMsg(XPATHBundle, msgKey, args);
081 }
082 else
083 return "Could not load any resource bundles.";
084 }
085
086 /**
087 * Creates a message from the specified key and replacement
088 * arguments, localized to the given locale.
089 *
090 * @param fResourceBundle The resource bundle to use.
091 * @param msgKey The message key to use.
092 * @param args The arguments to be used as replacement text
093 * in the message created.
094 *
095 * @return The formatted message string.
096 */
097 public static final String createXPATHMsg(ListResourceBundle fResourceBundle,
098 String msgKey, Object args[]) //throws Exception
099 {
100
101 String fmsg = null;
102 boolean throwex = false;
103 String msg = null;
104
105 if (msgKey != null)
106 msg = fResourceBundle.getString(msgKey);
107
108 if (msg == null)
109 {
110 msg = fResourceBundle.getString(XPATHErrorResources.BAD_CODE);
111 throwex = true;
112 }
113
114 if (args != null)
115 {
116 try
117 {
118
119 // Do this to keep format from crying.
120 // This is better than making a bunch of conditional
121 // code all over the place.
122 int n = args.length;
123
124 for (int i = 0; i < n; i++)
125 {
126 if (null == args[i])
127 args[i] = "";
128 }
129
130 fmsg = java.text.MessageFormat.format(msg, args);
131 }
132 catch (Exception e)
133 {
134 fmsg = fResourceBundle.getString(XPATHErrorResources.FORMAT_FAILED);
135 fmsg += " " + msg;
136 }
137 }
138 else
139 fmsg = msg;
140
141 if (throwex)
142 {
143 throw new RuntimeException(fmsg);
144 }
145
146 return fmsg;
147 }
148
149 }