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: XMLMessages.java,v 1.7 2005/01/23 00:52:40 mcnamara Exp $
018: */
019: package org.apache.xml.res;
020:
021: import java.util.ListResourceBundle;
022: import java.util.Locale;
023: import java.util.MissingResourceException;
024: import java.util.ResourceBundle;
025:
026: /**
027: * A utility class for issuing XML error messages.
028: * @xsl.usage internal
029: */
030: public class XMLMessages {
031:
032: /** The local object to use. */
033: protected Locale fLocale = Locale.getDefault();
034:
035: /** The language specific resource object for XML messages. */
036: private static ListResourceBundle XMLBundle = null;
037:
038: /** The class name of the XML error message string table. */
039: private static final String XML_ERROR_RESOURCES = "org.apache.xml.res.XMLErrorResources";
040:
041: /** String to use if a bad message code is used. */
042: protected static final String BAD_CODE = "BAD_CODE";
043:
044: /** String to use if the message format operation failed. */
045: protected static final String FORMAT_FAILED = "FORMAT_FAILED";
046:
047: /**
048: * Set the Locale object to use.
049: *
050: * @param locale non-null reference to Locale object.
051: */
052: public void setLocale(Locale locale) {
053: fLocale = locale;
054: }
055:
056: /**
057: * Get the Locale object that is being used.
058: *
059: * @return non-null reference to Locale object.
060: */
061: public Locale getLocale() {
062: return fLocale;
063: }
064:
065: /**
066: * Creates a message from the specified key and replacement
067: * arguments, localized to the given locale.
068: *
069: * @param msgKey The key for the message text.
070: * @param args The arguments to be used as replacement text
071: * in the message created.
072: *
073: * @return The formatted message string.
074: */
075: public static final String createXMLMessage(String msgKey,
076: Object args[]) {
077: if (XMLBundle == null)
078: XMLBundle = loadResourceBundle(XML_ERROR_RESOURCES);
079:
080: if (XMLBundle != null) {
081: return createMsg(XMLBundle, msgKey, args);
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 createMsg(
098: ListResourceBundle fResourceBundle, String msgKey,
099: Object args[]) //throws Exception
100: {
101:
102: String fmsg = null;
103: boolean throwex = false;
104: String msg = null;
105:
106: if (msgKey != null)
107: msg = fResourceBundle.getString(msgKey);
108:
109: if (msg == null) {
110: msg = fResourceBundle.getString(BAD_CODE);
111: throwex = true;
112: }
113:
114: if (args != null) {
115: try {
116:
117: // Do this to keep format from crying.
118: // This is better than making a bunch of conditional
119: // code all over the place.
120: int n = args.length;
121:
122: for (int i = 0; i < n; i++) {
123: if (null == args[i])
124: args[i] = "";
125: }
126:
127: fmsg = java.text.MessageFormat.format(msg, args);
128: } catch (Exception e) {
129: fmsg = fResourceBundle.getString(FORMAT_FAILED);
130: fmsg += " " + msg;
131: }
132: } else
133: fmsg = msg;
134:
135: if (throwex) {
136: throw new RuntimeException(fmsg);
137: }
138:
139: return fmsg;
140: }
141:
142: /**
143: * Return a named ResourceBundle for a particular locale. This method mimics the behavior
144: * of ResourceBundle.getBundle().
145: *
146: * @param className The class name of the resource bundle.
147: * @return the ResourceBundle
148: * @throws MissingResourceException
149: */
150: public static ListResourceBundle loadResourceBundle(String className)
151: throws MissingResourceException {
152: Locale locale = Locale.getDefault();
153:
154: try {
155: return (ListResourceBundle) ResourceBundle.getBundle(
156: className, locale);
157: } catch (MissingResourceException e) {
158: try // try to fall back to en_US if we can't load
159: {
160:
161: // Since we can't find the localized property file,
162: // fall back to en_US.
163: return (ListResourceBundle) ResourceBundle.getBundle(
164: className, new Locale("en", "US"));
165: } catch (MissingResourceException e2) {
166:
167: // Now we are really in trouble.
168: // very bad, definitely very bad...not going to get very far
169: throw new MissingResourceException(
170: "Could not load any resource bundles."
171: + className, className, "");
172: }
173: }
174: }
175:
176: /**
177: * Return the resource file suffic for the indicated locale
178: * For most locales, this will be based the language code. However
179: * for Chinese, we do distinguish between Taiwan and PRC
180: *
181: * @param locale the locale
182: * @return an String suffix which can be appended to a resource name
183: */
184: protected static String getResourceSuffix(Locale locale) {
185:
186: String suffix = "_" + locale.getLanguage();
187: String country = locale.getCountry();
188:
189: if (country.equals("TW"))
190: suffix += "_" + country;
191:
192: return suffix;
193: }
194: }
|