KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > util > DatatypeMessageFormatter


1 /*
2  * DatatypeMessageFormatter.java
3  *
4  * Created on June 28, 2004, 9:15 AM
5  */

6
7 package com.sun.org.apache.xerces.internal.util;
8
9 import java.util.Locale JavaDoc;
10 import java.util.MissingResourceException JavaDoc;
11 import java.util.PropertyResourceBundle JavaDoc;
12 import java.util.ResourceBundle JavaDoc;
13
14 /**
15  *
16  * @author Neeraj Bajaj, Sun Microsystems
17  */

18
19 public class DatatypeMessageFormatter {
20     
21     static final String JavaDoc BASE_NAME = "com.sun.org.apache.xerces.internal.impl.msg.DatatypeMessages";
22     
23     /**
24      * Formats a message with the specified arguments using the given
25      * locale information.
26      *
27      * @param locale The locale of the message.
28      * @param key The message key.
29      * @param arguments The message replacement text arguments. The order
30      * of the arguments must match that of the placeholders
31      * in the actual message.
32      *
33      * @return the formatted message.
34      *
35      * @throws MissingResourceException Thrown if the message with the
36      * specified key cannot be found.
37      */

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