KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > jaxp > validation > JAXPValidationMessageFormatter


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 com.sun.org.apache.xerces.internal.jaxp.validation;
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 /**
25  * <p>Used to format JAXP Validation API error messages using a specified locale.</p>
26  *
27  * @author Michael Glavassevich, IBM
28  * @version $Id: JAXPValidationMessageFormatter.java,v 1.1.4.1 2005/09/05 11:52:09 sunithareddy Exp $
29  */

30 final class JAXPValidationMessageFormatter {
31
32     /**
33      * Formats a message with the specified arguments using the given
34      * locale information.
35      *
36      * @param locale The locale of the message.
37      * @param key The message key.
38      * @param arguments The message replacement text arguments. The order
39      * of the arguments must match that of the placeholders
40      * in the actual message.
41      *
42      * @return the formatted message.
43      *
44      * @throws MissingResourceException Thrown if the message with the
45      * specified key cannot be found.
46      */

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