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