KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > xpointer > XPointerMessageFormatter


1 /*
2  * Copyright 2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.sun.org.apache.xerces.internal.xpointer;
17
18 import java.util.Locale JavaDoc;
19 import java.util.MissingResourceException JavaDoc;
20 import java.util.ResourceBundle JavaDoc;
21 import java.util.PropertyResourceBundle JavaDoc;
22 import com.sun.org.apache.xerces.internal.util.MessageFormatter;
23
24 /**
25  * XPointerMessageFormatter provides error messages for the XPointer Framework
26  * and element() Scheme Recommendations.
27  *
28  * @xerces.internal
29  *
30  * @version $Id: XPointerMessageFormatter.java,v 1.1.4.1 2005/09/08 05:25:45 sunithareddy Exp $
31  */

32 class XPointerMessageFormatter implements MessageFormatter {
33
34     public static final String JavaDoc XPOINTER_DOMAIN = "http://www.w3.org/TR/XPTR";
35
36     // private objects to cache the locale and resource bundle
37
private Locale JavaDoc fLocale = null;
38
39     private ResourceBundle JavaDoc fResourceBundle = null;
40
41     /**
42      * Formats a message with the specified arguments using the given locale
43      * information.
44      *
45      * @param locale
46      * The locale of the message.
47      * @param key
48      * The message key.
49      * @param arguments
50      * The message replacement text arguments. The order of the
51      * arguments must match that of the placeholders in the actual
52      * message.
53      *
54      * @return Returns the formatted message.
55      *
56      * @throws MissingResourceException
57      * Thrown if the message with the specified key cannot be found.
58      */

59     public String JavaDoc formatMessage(Locale JavaDoc locale, String JavaDoc key, Object JavaDoc[] arguments)
60             throws MissingResourceException JavaDoc {
61
62         if (fResourceBundle == null || locale != fLocale) {
63             if (locale != null) {
64                 fResourceBundle = PropertyResourceBundle.getBundle(
65                         "com.sun.org.apache.xerces.internal.impl.msg.XPointerMessages", locale);
66                 // memorize the most-recent locale
67
fLocale = locale;
68             }
69             if (fResourceBundle == null)
70                 fResourceBundle = PropertyResourceBundle
71                         .getBundle("com.sun.org.apache.xerces.internal.impl.msg.XPointerMessages");
72         }
73
74         String JavaDoc msg = fResourceBundle.getString(key);
75         if (arguments != null) {
76             try {
77                 msg = java.text.MessageFormat.format(msg, arguments);
78             } catch (Exception JavaDoc e) {
79                 msg = fResourceBundle.getString("FormatFailed");
80                 msg += " " + fResourceBundle.getString(key);
81             }
82         }
83
84         if (msg == null) {
85             msg = fResourceBundle.getString("BadMessageKey");
86             throw new MissingResourceException JavaDoc(msg,
87                     "com.sun.org.apache.xerces.internal.impl.msg.XPointerMessages", key);
88         }
89
90         return msg;
91     }
92 }
Popular Tags