KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > util > DatatypeMessageFormatter


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
17 package org.apache.xerces.util;
18
19 import java.util.Locale JavaDoc;
20 import java.util.MissingResourceException JavaDoc;
21 import java.util.PropertyResourceBundle JavaDoc;
22 import java.util.ResourceBundle JavaDoc;
23
24 /**
25  * <p>Used to format JAXP 1.3 Datatype API error messages using a specified locale.</p>
26  *
27  * @author Neeraj Bajaj, Sun Microsystems
28  * @version $Id: DatatypeMessageFormatter.java,v 1.2 2005/05/06 16:06:56 mrglavas Exp $
29  */

30 public class DatatypeMessageFormatter {
31     
32     private static final String JavaDoc BASE_NAME = "org.apache.xerces.impl.msg.DatatypeMessages";
33     
34     /**
35      * Formats a message with the specified arguments using the given
36      * locale information.
37      *
38      * @param locale The locale of the message.
39      * @param key The message key.
40      * @param arguments The message replacement text arguments. The order
41      * of the arguments must match that of the placeholders
42      * in the actual message.
43      *
44      * @return the formatted message.
45      *
46      * @throws MissingResourceException Thrown if the message with the
47      * specified key cannot be found.
48      */

49     public static String JavaDoc formatMessage(Locale JavaDoc locale,
50         String JavaDoc key, Object JavaDoc[] arguments)
51         throws MissingResourceException JavaDoc {
52
53         ResourceBundle JavaDoc resourceBundle = null;
54         if (locale != null) {
55             resourceBundle =
56                 PropertyResourceBundle.getBundle(BASE_NAME, locale);
57         }
58         else {
59             resourceBundle =
60                 PropertyResourceBundle.getBundle(BASE_NAME);
61         }
62
63         // format message
64
String JavaDoc msg;
65         try {
66             msg = resourceBundle.getString(key);
67             if (arguments != null) {
68                 try {
69                     msg = java.text.MessageFormat.format(msg, arguments);
70                 }
71                 catch (Exception JavaDoc e) {
72                     msg = resourceBundle.getString("FormatFailed");
73                     msg += " " + resourceBundle.getString(key);
74                 }
75             }
76         }
77         
78         // error
79
catch (MissingResourceException JavaDoc e) {
80             msg = resourceBundle.getString("BadMessageKey");
81             throw new MissingResourceException JavaDoc(key, msg, key);
82         }
83
84         // no message
85
if (msg == null) {
86             msg = key;
87             if (arguments.length > 0) {
88                 StringBuffer JavaDoc str = new StringBuffer JavaDoc(msg);
89                 str.append('?');
90                 for (int i = 0; i < arguments.length; i++) {
91                     if (i > 0) {
92                         str.append('&');
93                     }
94                     str.append(String.valueOf(arguments[i]));
95                 }
96             }
97         }
98         return msg;
99     }
100 }
101
Popular Tags