1 16 19 package org.apache.xml.res; 20 21 import java.util.ListResourceBundle ; 22 import java.util.Locale ; 23 import java.util.MissingResourceException ; 24 import java.util.ResourceBundle ; 25 26 30 public class XMLMessages 31 { 32 33 34 protected Locale fLocale = Locale.getDefault(); 35 36 37 private static ListResourceBundle XMLBundle = null; 38 39 40 private static final String XML_ERROR_RESOURCES = 41 "org.apache.xml.res.XMLErrorResources"; 42 43 44 protected static String BAD_CODE = "BAD_CODE"; 45 46 47 protected static String FORMAT_FAILED = "FORMAT_FAILED"; 48 49 54 public void setLocale(Locale locale) 55 { 56 fLocale = locale; 57 } 58 59 64 public Locale getLocale() 65 { 66 return fLocale; 67 } 68 69 79 public static final String createXMLMessage(String msgKey, Object args[]) 80 { 81 if (XMLBundle == null) 82 XMLBundle = loadResourceBundle(XML_ERROR_RESOURCES); 83 84 if (XMLBundle != null) 85 { 86 return createMsg(XMLBundle, msgKey, args); 87 } 88 else 89 return "Could not load any resource bundles."; 90 } 91 92 105 public static final String createMsg(ListResourceBundle fResourceBundle, 106 String msgKey, Object args[]) { 108 109 String fmsg = null; 110 boolean throwex = false; 111 String msg = null; 112 113 if (msgKey != null) 114 msg = fResourceBundle.getString(msgKey); 115 116 if (msg == null) 117 { 118 msg = fResourceBundle.getString(BAD_CODE); 119 throwex = true; 120 } 121 122 if (args != null) 123 { 124 try 125 { 126 127 int n = args.length; 131 132 for (int i = 0; i < n; i++) 133 { 134 if (null == args[i]) 135 args[i] = ""; 136 } 137 138 fmsg = java.text.MessageFormat.format(msg, args); 139 } 140 catch (Exception e) 141 { 142 fmsg = fResourceBundle.getString(FORMAT_FAILED); 143 fmsg += " " + msg; 144 } 145 } 146 else 147 fmsg = msg; 148 149 if (throwex) 150 { 151 throw new RuntimeException (fmsg); 152 } 153 154 return fmsg; 155 } 156 157 168 public static ListResourceBundle loadResourceBundle(String className) 169 throws MissingResourceException 170 { 171 Locale locale = Locale.getDefault(); 172 173 try 174 { 175 return (ListResourceBundle )ResourceBundle.getBundle(className, locale); 176 } 177 catch (MissingResourceException e) 178 { 179 try { 181 182 return (ListResourceBundle )ResourceBundle.getBundle( 185 className, new Locale ("en", "US")); 186 } 187 catch (MissingResourceException e2) 188 { 189 190 throw new MissingResourceException ( 193 "Could not load any resource bundles." + className, className, ""); 194 } 195 } 196 } 197 198 206 protected static String getResourceSuffix(Locale locale) 207 { 208 209 String suffix = "_" + locale.getLanguage(); 210 String country = locale.getCountry(); 211 212 if (country.equals("TW")) 213 suffix += "_" + country; 214 215 return suffix; 216 } 217 } 218 | Popular Tags |