KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > msg > XMLMessageFormatter


1 /*
2  * Copyright 1999-2002,2004 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.impl.msg;
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
24 import org.apache.xerces.util.MessageFormatter;
25
26 /**
27  * XMLMessageFormatter provides error messages for the XML 1.0 Recommendation and for
28  * the Namespaces Recommendation
29  *
30  * @xerces.internal
31  *
32  * @author Eric Ye, IBM
33  * @version $Id: XMLMessageFormatter.java,v 1.6 2004/10/04 22:07:41 mrglavas Exp $
34  *
35  */

36 public class XMLMessageFormatter implements MessageFormatter {
37     /**
38      * The domain of messages concerning the XML 1.0 specification.
39      */

40     public static final String JavaDoc XML_DOMAIN = "http://www.w3.org/TR/1998/REC-xml-19980210";
41     public static final String JavaDoc XMLNS_DOMAIN = "http://www.w3.org/TR/1999/REC-xml-names-19990114";
42
43     // private objects to cache the locale and resource bundle
44
private Locale JavaDoc fLocale = null;
45     private ResourceBundle JavaDoc fResourceBundle = null;
46     
47     //
48
// MessageFormatter methods
49
//
50

51     /**
52      * Formats a message with the specified arguments using the given
53      * locale information.
54      *
55      * @param locale The locale of the message.
56      * @param key The message key.
57      * @param arguments The message replacement text arguments. The order
58      * of the arguments must match that of the placeholders
59      * in the actual message.
60      *
61      * @return Returns the formatted message.
62      *
63      * @throws MissingResourceException Thrown if the message with the
64      * specified key cannot be found.
65      */

66     public String JavaDoc formatMessage(Locale JavaDoc locale, String JavaDoc key, Object JavaDoc[] arguments)
67         throws MissingResourceException JavaDoc {
68         
69         if (fResourceBundle == null || locale != fLocale) {
70             if (locale != null) {
71                 fResourceBundle = PropertyResourceBundle.getBundle("org.apache.xerces.impl.msg.XMLMessages", locale);
72                 // memorize the most-recent locale
73
fLocale = locale;
74             }
75             if (fResourceBundle == null)
76                 fResourceBundle = PropertyResourceBundle.getBundle("org.apache.xerces.impl.msg.XMLMessages");
77         }
78         
79         // format message
80
String JavaDoc msg;
81         try {
82             msg = fResourceBundle.getString(key);
83             if (arguments != null) {
84                 try {
85                     msg = java.text.MessageFormat.format(msg, arguments);
86                 }
87                 catch (Exception JavaDoc e) {
88                     msg = fResourceBundle.getString("FormatFailed");
89                     msg += " " + fResourceBundle.getString(key);
90                 }
91             }
92         }
93
94         // error
95
catch (MissingResourceException JavaDoc e) {
96             msg = fResourceBundle.getString("BadMessageKey");
97             throw new MissingResourceException JavaDoc(key, msg, key);
98         }
99
100         // no message
101
if (msg == null) {
102             msg = key;
103             if (arguments.length > 0) {
104                 StringBuffer JavaDoc str = new StringBuffer JavaDoc(msg);
105                 str.append('?');
106                 for (int i = 0; i < arguments.length; i++) {
107                     if (i > 0) {
108                         str.append('&');
109                     }
110                     str.append(String.valueOf(arguments[i]));
111                 }
112             }
113         }
114
115         return msg;
116     }
117
118 }
119
Popular Tags