001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.impl.msg;
019:
020: import java.util.Locale;
021: import java.util.MissingResourceException;
022: import java.util.ResourceBundle;
023: import java.util.PropertyResourceBundle;
024:
025: import org.apache.xerces.util.MessageFormatter;
026:
027: /**
028: * XMLMessageFormatter provides error messages for the XML 1.0 Recommendation and for
029: * the Namespaces Recommendation
030: *
031: * @xerces.internal
032: *
033: * @author Eric Ye, IBM
034: * @version $Id: XMLMessageFormatter.java 446717 2006-09-15 20:29:45Z mrglavas $
035: *
036: */
037: public class XMLMessageFormatter implements MessageFormatter {
038: /**
039: * The domain of messages concerning the XML 1.0 specification.
040: */
041: public static final String XML_DOMAIN = "http://www.w3.org/TR/1998/REC-xml-19980210";
042: public static final String XMLNS_DOMAIN = "http://www.w3.org/TR/1999/REC-xml-names-19990114";
043:
044: // private objects to cache the locale and resource bundle
045: private Locale fLocale = null;
046: private ResourceBundle fResourceBundle = null;
047:
048: //
049: // MessageFormatter methods
050: //
051:
052: /**
053: * Formats a message with the specified arguments using the given
054: * locale information.
055: *
056: * @param locale The locale of the message.
057: * @param key The message key.
058: * @param arguments The message replacement text arguments. The order
059: * of the arguments must match that of the placeholders
060: * in the actual message.
061: *
062: * @return Returns the formatted message.
063: *
064: * @throws MissingResourceException Thrown if the message with the
065: * specified key cannot be found.
066: */
067: public String formatMessage(Locale locale, String key,
068: Object[] arguments) throws MissingResourceException {
069:
070: if (fResourceBundle == null || locale != fLocale) {
071: if (locale != null) {
072: fResourceBundle = PropertyResourceBundle.getBundle(
073: "org.apache.xerces.impl.msg.XMLMessages",
074: locale);
075: // memorize the most-recent locale
076: fLocale = locale;
077: }
078: if (fResourceBundle == null)
079: fResourceBundle = PropertyResourceBundle
080: .getBundle("org.apache.xerces.impl.msg.XMLMessages");
081: }
082:
083: // format message
084: String msg;
085: try {
086: msg = fResourceBundle.getString(key);
087: if (arguments != null) {
088: try {
089: msg = java.text.MessageFormat
090: .format(msg, arguments);
091: } catch (Exception e) {
092: msg = fResourceBundle.getString("FormatFailed");
093: msg += " " + fResourceBundle.getString(key);
094: }
095: }
096: }
097:
098: // error
099: catch (MissingResourceException e) {
100: msg = fResourceBundle.getString("BadMessageKey");
101: throw new MissingResourceException(key, msg, key);
102: }
103:
104: // no message
105: if (msg == null) {
106: msg = key;
107: if (arguments.length > 0) {
108: StringBuffer str = new StringBuffer(msg);
109: str.append('?');
110: for (int i = 0; i < arguments.length; i++) {
111: if (i > 0) {
112: str.append('&');
113: }
114: str.append(String.valueOf(arguments[i]));
115: }
116: }
117: }
118:
119: return msg;
120: }
121:
122: }
|