KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > dom > DOMMessageFormatter


1 /*
2  * Copyright 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
18 package org.apache.xerces.dom;
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 /**
25  * Used to format DOM error messages, using the system locale.
26  *
27  * @xerces.internal
28  *
29  * @author Sandy Gao, IBM
30  * @version $Id: DOMMessageFormatter.java,v 1.7 2004/10/05 17:12:51 mrglavas Exp $
31  */

32 public class DOMMessageFormatter {
33     public static final String JavaDoc DOM_DOMAIN = "http://www.w3.org/dom/DOMTR";
34     public static final String JavaDoc XML_DOMAIN = "http://www.w3.org/TR/1998/REC-xml-19980210";
35     public static final String JavaDoc SERIALIZER_DOMAIN = "http://apache.org/xml/serializer";
36     
37     private static ResourceBundle JavaDoc domResourceBundle = null;
38     private static ResourceBundle JavaDoc xmlResourceBundle = null;
39     private static ResourceBundle JavaDoc serResourceBundle = null;
40     private static Locale JavaDoc locale = null;
41     
42     
43     DOMMessageFormatter(){
44         locale = Locale.getDefault();
45     }
46     /**
47      * Formats a message with the specified arguments using the given
48      * locale information.
49      *
50      * @param domain domain from which error string is to come.
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 the formatted message.
57      *
58      * @throws MissingResourceException Thrown if the message with the
59      * specified key cannot be found.
60      */

61     public static String JavaDoc formatMessage(String JavaDoc domain,
62     String JavaDoc key, Object JavaDoc[] arguments)
63     throws MissingResourceException JavaDoc {
64         ResourceBundle JavaDoc resourceBundle = getResourceBundle(domain);
65         if(resourceBundle == null){
66             init();
67             resourceBundle = getResourceBundle(domain);
68             if(resourceBundle == null)
69                 throw new MissingResourceException JavaDoc("Unknown domain" + domain, null, key);
70         }
71         // format message
72
String JavaDoc msg;
73         try {
74             msg = key + ": " + resourceBundle.getString(key);
75             if (arguments != null) {
76                 try {
77                     msg = java.text.MessageFormat.format(msg, arguments);
78                 }
79                 catch (Exception JavaDoc e) {
80                     msg = resourceBundle.getString("FormatFailed");
81                     msg += " " + resourceBundle.getString(key);
82                 }
83             }
84         } // error
85
catch (MissingResourceException JavaDoc e) {
86             msg = resourceBundle.getString("BadMessageKey");
87             throw new MissingResourceException JavaDoc(key, msg, key);
88         }
89         
90         // no message
91
if (msg == null) {
92             msg = key;
93             if (arguments.length > 0) {
94                 StringBuffer JavaDoc str = new StringBuffer JavaDoc(msg);
95                 str.append('?');
96                 for (int i = 0; i < arguments.length; i++) {
97                     if (i > 0) {
98                         str.append('&');
99                     }
100                     str.append(String.valueOf(arguments[i]));
101                 }
102             }
103         }
104         
105         return msg;
106     }
107     
108     static ResourceBundle JavaDoc getResourceBundle(String JavaDoc domain){
109         if(domain == DOM_DOMAIN || domain.equals(DOM_DOMAIN))
110             return domResourceBundle;
111         else if( domain == XML_DOMAIN || domain.equals(XML_DOMAIN))
112             return xmlResourceBundle;
113         else if(domain == SERIALIZER_DOMAIN || domain.equals(SERIALIZER_DOMAIN))
114             return serResourceBundle;
115         return null;
116     }
117     /**
118      * Initialize Message Formatter.
119      */

120     public static void init(){
121         if (locale != null) {
122             domResourceBundle = PropertyResourceBundle.getBundle("org.apache.xerces.impl.msg.DOMMessages", locale);
123             serResourceBundle = PropertyResourceBundle.getBundle("org.apache.xerces.impl.msg.XMLSerializerMessages", locale);
124             xmlResourceBundle = PropertyResourceBundle.getBundle("org.apache.xerces.impl.msg.XMLMessages", locale);
125         }else{
126             domResourceBundle = PropertyResourceBundle.getBundle("org.apache.xerces.impl.msg.DOMMessages");
127             serResourceBundle = PropertyResourceBundle.getBundle("org.apache.xerces.impl.msg.XMLSerializerMessages");
128             xmlResourceBundle = PropertyResourceBundle.getBundle("org.apache.xerces.impl.msg.XMLMessages");
129         }
130     }
131     
132     /**
133      * setLocale to be used by the formatter.
134      * @param locale
135      */

136     public static void setLocale(Locale JavaDoc dlocale){
137         locale = dlocale;
138     }
139 }
140
Popular Tags