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.xinclude;
19:
20: import java.util.Locale;
21: import java.util.MissingResourceException;
22: import java.util.ResourceBundle;
23: import java.util.PropertyResourceBundle;
24: import org.apache.xerces.util.MessageFormatter;
25:
26: // TODO: fix error messages in XIncludeMessages.properties
27: /**
28: * XIncludeMessageFormatter provides error messages for the XInclude 1.0 Candidate Recommendation
29: *
30: * @author Peter McCracken, IBM
31: *
32: * @version $Id: XIncludeMessageFormatter.java 447243 2006-09-18 05:15:27Z mrglavas $
33: */
34: public class XIncludeMessageFormatter implements MessageFormatter {
35:
36: public static final String XINCLUDE_DOMAIN = "http://www.w3.org/TR/xinclude";
37:
38: // private objects to cache the locale and resource bundle
39: private Locale fLocale = null;
40: private ResourceBundle fResourceBundle = null;
41:
42: /**
43: * Formats a message with the specified arguments using the given
44: * locale information.
45: *
46: * @param locale The locale of the message.
47: * @param key The message key.
48: * @param arguments The message replacement text arguments. The order
49: * of the arguments must match that of the placeholders
50: * in the actual message.
51: *
52: * @return Returns the formatted message.
53: *
54: * @throws MissingResourceException Thrown if the message with the
55: * specified key cannot be found.
56: */
57: public String formatMessage(Locale locale, String key,
58: Object[] arguments) throws MissingResourceException {
59:
60: if (fResourceBundle == null || locale != fLocale) {
61: if (locale != null) {
62: fResourceBundle = PropertyResourceBundle.getBundle(
63: "org.apache.xerces.impl.msg.XIncludeMessages",
64: locale);
65: // memorize the most-recent locale
66: fLocale = locale;
67: }
68: if (fResourceBundle == null)
69: fResourceBundle = PropertyResourceBundle
70: .getBundle("org.apache.xerces.impl.msg.XIncludeMessages");
71: }
72:
73: String msg = fResourceBundle.getString(key);
74: if (arguments != null) {
75: try {
76: msg = java.text.MessageFormat.format(msg, arguments);
77: } catch (Exception e) {
78: msg = fResourceBundle.getString("FormatFailed");
79: msg += " " + fResourceBundle.getString(key);
80: }
81: }
82:
83: if (msg == null) {
84: msg = fResourceBundle.getString("BadMessageKey");
85: throw new MissingResourceException(msg,
86: "org.apache.xerces.impl.msg.XIncludeMessages", key);
87: }
88:
89: return msg;
90: }
91: }
|