1 21 package oracle.toplink.essentials.exceptions.i18n; 23 24 import java.text.MessageFormat ; 25 import java.util.Locale ; 26 import java.util.ResourceBundle ; 27 import java.util.Vector ; 28 import oracle.toplink.essentials.internal.helper.Helper; 29 30 37 public class ExceptionMessageGenerator { 38 39 42 public static String buildMessage(Class exceptionClass, int errorNumber, Object [] arguments) { 43 final String CR = System.getProperty("line.separator"); 44 45 String shortClassName = Helper.getShortClassName(exceptionClass); 46 String message = ""; 47 ResourceBundle bundle = null; 48 49 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 mre) { 61 bundle = ResourceBundle.getBundle("oracle.toplink.essentials.exceptions.i18n.ExceptionResource", Locale.getDefault()); 64 String noTranslationMessage = bundle.getString("NoExceptionTranslationForThisLocale"); 65 Object [] args = { CR }; 66 return format(message, arguments) + format(noTranslationMessage, args); 67 } 68 return format(message, arguments); 69 } 70 71 74 protected static String format(String message, Object [] arguments) { 76 try { 77 return MessageFormat.format(message, arguments); 78 } catch (Exception ex) { 79 ResourceBundle bundle = null; 80 bundle = ResourceBundle.getBundle("oracle.toplink.essentials.exceptions.i18n.ExceptionResource", Locale.getDefault()); 81 String errorMessage = bundle.getString("ErrorFormattingMessage"); 82 Vector vec = new Vector (); 83 if (arguments != null) { 84 for (int index = 0; index < arguments.length; index++) { 85 try { 86 vec.add(arguments[index].toString()); 87 } catch (Exception ex2) { 88 vec.add(ex2); 89 } 90 } 91 } 92 return MessageFormat.format(errorMessage, new Object [] {message, vec}); 93 } 94 } 95 96 101 public static String getHeader(String headerLabel) { 102 ResourceBundle 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 mre) { 107 return "[" + headerLabel + "]"; 108 } 109 } 110 } 111 | Popular Tags |