KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > xs > XSMessageFormatter


1 /*
2  * Copyright 2001, 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.xs;
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
26 /**
27  * SchemaMessageProvider implements an XMLMessageProvider that
28  * provides localizable error messages for the W3C XML Schema Language
29  *
30  * @xerces.internal
31  *
32  * @author Elena Litani, IBM
33  * @version $Id: XSMessageFormatter.java,v 1.4 2004/10/06 15:14:55 mrglavas Exp $
34  */

35 public class XSMessageFormatter implements MessageFormatter {
36     /**
37      * The domain of messages concerning the XML Schema: Structures specification.
38      */

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

61      public String JavaDoc formatMessage(Locale JavaDoc locale, String JavaDoc key, Object JavaDoc[] arguments)
62         throws MissingResourceException JavaDoc {
63         
64         if (fResourceBundle == null || locale != fLocale) {
65             if (locale != null) {
66                 fResourceBundle = PropertyResourceBundle.getBundle("org.apache.xerces.impl.msg.XMLSchemaMessages", locale);
67                 // memorize the most-recent locale
68
fLocale = locale;
69             }
70             if (fResourceBundle == null)
71                 fResourceBundle = PropertyResourceBundle.getBundle("org.apache.xerces.impl.msg.XMLSchemaMessages");
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, "org.apache.xerces.impl.msg.SchemaMessages", key);
87         }
88
89         return msg;
90     }
91 }
92
Popular Tags