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