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.dom;
019:
020: import java.util.Locale;
021: import java.util.MissingResourceException;
022: import java.util.ResourceBundle;
023: import java.util.PropertyResourceBundle;
024:
025: /**
026: * Used to format DOM error messages, using the system locale.
027: *
028: * @xerces.internal
029: *
030: * @author Sandy Gao, IBM
031: * @version $Id: DOMMessageFormatter.java 449328 2006-09-23 22:58:23Z mrglavas $
032: */
033: public class DOMMessageFormatter {
034: public static final String DOM_DOMAIN = "http://www.w3.org/dom/DOMTR";
035: public static final String XML_DOMAIN = "http://www.w3.org/TR/1998/REC-xml-19980210";
036: public static final String SERIALIZER_DOMAIN = "http://apache.org/xml/serializer";
037:
038: private static ResourceBundle domResourceBundle = null;
039: private static ResourceBundle xmlResourceBundle = null;
040: private static ResourceBundle serResourceBundle = null;
041: private static Locale locale = null;
042:
043: DOMMessageFormatter() {
044: locale = Locale.getDefault();
045: }
046:
047: /**
048: * Formats a message with the specified arguments using the given
049: * locale information.
050: *
051: * @param domain domain from which error string is to come.
052: * @param key The message key.
053: * @param arguments The message replacement text arguments. The order
054: * of the arguments must match that of the placeholders
055: * in the actual message.
056: *
057: * @return the formatted message.
058: *
059: * @throws MissingResourceException Thrown if the message with the
060: * specified key cannot be found.
061: */
062: public static String formatMessage(String domain, String key,
063: Object[] arguments) throws MissingResourceException {
064: ResourceBundle resourceBundle = getResourceBundle(domain);
065: if (resourceBundle == null) {
066: init();
067: resourceBundle = getResourceBundle(domain);
068: if (resourceBundle == null)
069: throw new MissingResourceException("Unknown domain"
070: + domain, null, key);
071: }
072: // format message
073: String msg;
074: try {
075: msg = key + ": " + resourceBundle.getString(key);
076: if (arguments != null) {
077: try {
078: msg = java.text.MessageFormat
079: .format(msg, arguments);
080: } catch (Exception e) {
081: msg = resourceBundle.getString("FormatFailed");
082: msg += " " + resourceBundle.getString(key);
083: }
084: }
085: } // error
086: catch (MissingResourceException e) {
087: msg = resourceBundle.getString("BadMessageKey");
088: throw new MissingResourceException(key, msg, key);
089: }
090:
091: // no message
092: if (msg == null) {
093: msg = key;
094: if (arguments.length > 0) {
095: StringBuffer str = new StringBuffer(msg);
096: str.append('?');
097: for (int i = 0; i < arguments.length; i++) {
098: if (i > 0) {
099: str.append('&');
100: }
101: str.append(String.valueOf(arguments[i]));
102: }
103: }
104: }
105:
106: return msg;
107: }
108:
109: static ResourceBundle getResourceBundle(String domain) {
110: if (domain == DOM_DOMAIN || domain.equals(DOM_DOMAIN))
111: return domResourceBundle;
112: else if (domain == XML_DOMAIN || domain.equals(XML_DOMAIN))
113: return xmlResourceBundle;
114: else if (domain == SERIALIZER_DOMAIN
115: || domain.equals(SERIALIZER_DOMAIN))
116: return serResourceBundle;
117: return null;
118: }
119:
120: /**
121: * Initialize Message Formatter.
122: */
123: public static void init() {
124: if (locale != null) {
125: domResourceBundle = PropertyResourceBundle.getBundle(
126: "org.apache.xerces.impl.msg.DOMMessages", locale);
127: serResourceBundle = PropertyResourceBundle.getBundle(
128: "org.apache.xerces.impl.msg.XMLSerializerMessages",
129: locale);
130: xmlResourceBundle = PropertyResourceBundle.getBundle(
131: "org.apache.xerces.impl.msg.XMLMessages", locale);
132: } else {
133: domResourceBundle = PropertyResourceBundle
134: .getBundle("org.apache.xerces.impl.msg.DOMMessages");
135: serResourceBundle = PropertyResourceBundle
136: .getBundle("org.apache.xerces.impl.msg.XMLSerializerMessages");
137: xmlResourceBundle = PropertyResourceBundle
138: .getBundle("org.apache.xerces.impl.msg.XMLMessages");
139: }
140: }
141:
142: /**
143: * Set Locale to be used by the formatter.
144: * @param dlocale
145: */
146: public static void setLocale(Locale dlocale) {
147: locale = dlocale;
148: }
149: }
|