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:
18: package org.apache.xerces.jaxp.validation;
19:
20: import java.util.Locale;
21: import java.util.MissingResourceException;
22: import java.util.ResourceBundle;
23: import java.util.PropertyResourceBundle;
24:
25: /**
26: * <p>Used to format JAXP Validation API error messages using a specified locale.</p>
27: *
28: * @author Michael Glavassevich, IBM
29: * @version $Id: JAXPValidationMessageFormatter.java 447235 2006-09-18 05:01:44Z mrglavas $
30: */
31: final class JAXPValidationMessageFormatter {
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
54: .getBundle(
55: "org.apache.xerces.impl.msg.JAXPValidationMessages",
56: locale);
57: } else {
58: resourceBundle = PropertyResourceBundle
59: .getBundle("org.apache.xerces.impl.msg.JAXPValidationMessages");
60: }
61:
62: // format message
63: String msg;
64: try {
65: msg = resourceBundle.getString(key);
66: if (arguments != null) {
67: try {
68: msg = java.text.MessageFormat
69: .format(msg, arguments);
70: } catch (Exception e) {
71: msg = resourceBundle.getString("FormatFailed");
72: msg += " " + resourceBundle.getString(key);
73: }
74: }
75: }
76:
77: // error
78: catch (MissingResourceException e) {
79: msg = resourceBundle.getString("BadMessageKey");
80: throw new MissingResourceException(key, msg, key);
81: }
82:
83: // no message
84: if (msg == null) {
85: msg = key;
86: if (arguments.length > 0) {
87: StringBuffer str = new StringBuffer(msg);
88: str.append('?');
89: for (int i = 0; i < arguments.length; i++) {
90: if (i > 0) {
91: str.append('&');
92: }
93: str.append(String.valueOf(arguments[i]));
94: }
95: }
96: }
97: return msg;
98: }
99: }
|