KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > exceptions > i18n > ExceptionMessageGenerator


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.exceptions.i18n;
23
24 import java.text.MessageFormat JavaDoc;
25 import java.util.Locale JavaDoc;
26 import java.util.ResourceBundle JavaDoc;
27 import java.util.Vector JavaDoc;
28 import oracle.toplink.essentials.internal.helper.Helper;
29
30 /**
31  * INTERNAL:
32  * Utility class to generate exception messages using ResourceBundles.
33  *
34  * Creation date: (12/7/00 10:30:38 AM)
35  * @author: Rick Barkhouse
36  */

37 public class ExceptionMessageGenerator {
38
39     /**
40      * Return the message for the given exception class and error number.
41      */

42     public static String JavaDoc buildMessage(Class JavaDoc exceptionClass, int errorNumber, Object JavaDoc[] arguments) {
43         final String JavaDoc CR = System.getProperty("line.separator");
44
45         String JavaDoc shortClassName = Helper.getShortClassName(exceptionClass);
46         String JavaDoc message = "";
47         ResourceBundle JavaDoc bundle = null;
48
49         // JDK 1.1 MessageFormat can't handle null arguments
50
for (int i = 0; i < arguments.length; i++) {
51             if (arguments[i] == null) {
52                 arguments[i] = "null";
53             }
54         }
55
56         bundle = ResourceBundle.getBundle("oracle.toplink.essentials.exceptions.i18n." + shortClassName + "Resource", Locale.getDefault());
57
58         try {
59             message = bundle.getString(String.valueOf(errorNumber));
60         } catch (java.util.MissingResourceException JavaDoc mre) {
61             // Found bundle, but couldn't find exception translation.
62
// Get the current language's NoExceptionTranslationForThisLocale message.
63
bundle = ResourceBundle.getBundle("oracle.toplink.essentials.exceptions.i18n.ExceptionResource", Locale.getDefault());
64             String JavaDoc noTranslationMessage = bundle.getString("NoExceptionTranslationForThisLocale");
65             Object JavaDoc[] args = { CR };
66             return format(message, arguments) + format(noTranslationMessage, args);
67         }
68         return format(message, arguments);
69     }
70
71     /**
72      * Return the formatted message for the given exception class and error number.
73      */

74     //Bug#4619864 Catch any exception during formatting and try to throw that exception. One possibility is toString() to an argument
75
protected static String JavaDoc format(String JavaDoc message, Object JavaDoc[] arguments) {
76         try {
77             return MessageFormat.format(message, arguments);
78         } catch (Exception JavaDoc ex) {
79             ResourceBundle JavaDoc bundle = null;
80             bundle = ResourceBundle.getBundle("oracle.toplink.essentials.exceptions.i18n.ExceptionResource", Locale.getDefault());
81             String JavaDoc errorMessage = bundle.getString("ErrorFormattingMessage");
82             Vector JavaDoc vec = new Vector JavaDoc();
83             if (arguments != null) {
84                 for (int index = 0; index < arguments.length; index++) {
85                     try {
86                         vec.add(arguments[index].toString());
87                     } catch (Exception JavaDoc ex2) {
88                         vec.add(ex2);
89                     }
90                 }
91             }
92             return MessageFormat.format(errorMessage, new Object JavaDoc[] {message, vec});
93         }
94     }
95
96     /**
97      * Get one of the generic headers used for the exception's toString().
98      *
99      * E.g., "EXCEPTION DESCRIPTION: ", "ERROR CODE: ", etc.
100      */

101     public static String JavaDoc getHeader(String JavaDoc headerLabel) {
102         ResourceBundle JavaDoc bundle = null;
103         try {
104             bundle = ResourceBundle.getBundle("oracle.toplink.essentials.exceptions.i18n.ExceptionResource", Locale.getDefault());
105             return bundle.getString(headerLabel);
106         } catch (java.util.MissingResourceException JavaDoc mre) {
107             return "[" + headerLabel + "]";
108         }
109     }
110 }
111
Popular Tags