KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > xinclude > XIncludeMessageFormatter


1 /*
2  * Copyright 2003-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
17 package org.apache.xerces.xinclude;
18
19 import java.util.Locale JavaDoc;
20 import java.util.MissingResourceException JavaDoc;
21 import java.util.ResourceBundle JavaDoc;
22 import java.util.PropertyResourceBundle JavaDoc;
23 import org.apache.xerces.util.MessageFormatter;
24
25 // TODO: fix error messages in XIncludeMessages.properties
26
/**
27  * XIncludeMessageFormatter provides error messages for the XInclude 1.0 Candidate Recommendation
28  *
29  * @author Peter McCracken, IBM
30  *
31  * @version $Id: XIncludeMessageFormatter.java,v 1.5 2005/01/11 13:40:28 mrglavas Exp $
32  */

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

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