01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.xerces.util;
18:
19: import java.util.Locale;
20: import java.util.MissingResourceException;
21: import java.util.ResourceBundle;
22: import java.util.PropertyResourceBundle;
23:
24: /**
25: * Used to format SAX error messages using a specified locale.
26: *
27: * @author Michael Glavassevich, IBM
28: *
29: * @version $Id: SAXMessageFormatter.java 447241 2006-09-18 05:12:57Z mrglavas $
30: */
31: public class SAXMessageFormatter {
32:
33: /**
34: * Formats a message with the specified arguments using the given
35: * locale information.
36: *
37: * @param locale The locale of the message.
38: * @param key The message key.
39: * @param arguments The message replacement text arguments. The order
40: * of the arguments must match that of the placeholders
41: * in the actual message.
42: *
43: * @return the formatted message.
44: *
45: * @throws MissingResourceException Thrown if the message with the
46: * specified key cannot be found.
47: */
48: public static String formatMessage(Locale locale, String key,
49: Object[] arguments) throws MissingResourceException {
50:
51: ResourceBundle resourceBundle = null;
52: if (locale != null) {
53: resourceBundle = PropertyResourceBundle.getBundle(
54: "org.apache.xerces.impl.msg.SAXMessages", locale);
55: } else {
56: resourceBundle = PropertyResourceBundle
57: .getBundle("org.apache.xerces.impl.msg.SAXMessages");
58: }
59:
60: // format message
61: String msg;
62: try {
63: msg = resourceBundle.getString(key);
64: if (arguments != null) {
65: try {
66: msg = java.text.MessageFormat
67: .format(msg, arguments);
68: } catch (Exception e) {
69: msg = resourceBundle.getString("FormatFailed");
70: msg += " " + resourceBundle.getString(key);
71: }
72: }
73: }
74:
75: // error
76: catch (MissingResourceException e) {
77: msg = resourceBundle.getString("BadMessageKey");
78: throw new MissingResourceException(key, msg, key);
79: }
80:
81: // no message
82: if (msg == null) {
83: msg = key;
84: if (arguments.length > 0) {
85: StringBuffer str = new StringBuffer(msg);
86: str.append('?');
87: for (int i = 0; i < arguments.length; i++) {
88: if (i > 0) {
89: str.append('&');
90: }
91: str.append(String.valueOf(arguments[i]));
92: }
93: }
94: }
95: return msg;
96: }
97: }
|