1 16 17 18 package org.apache.xerces.dom; 19 import java.util.Locale ; 20 import java.util.MissingResourceException ; 21 import java.util.ResourceBundle ; 22 import java.util.PropertyResourceBundle ; 23 24 32 public class DOMMessageFormatter { 33 public static final String DOM_DOMAIN = "http://www.w3.org/dom/DOMTR"; 34 public static final String XML_DOMAIN = "http://www.w3.org/TR/1998/REC-xml-19980210"; 35 public static final String SERIALIZER_DOMAIN = "http://apache.org/xml/serializer"; 36 37 private static ResourceBundle domResourceBundle = null; 38 private static ResourceBundle xmlResourceBundle = null; 39 private static ResourceBundle serResourceBundle = null; 40 private static Locale locale = null; 41 42 43 DOMMessageFormatter(){ 44 locale = Locale.getDefault(); 45 } 46 61 public static String formatMessage(String domain, 62 String key, Object [] arguments) 63 throws MissingResourceException { 64 ResourceBundle resourceBundle = getResourceBundle(domain); 65 if(resourceBundle == null){ 66 init(); 67 resourceBundle = getResourceBundle(domain); 68 if(resourceBundle == null) 69 throw new MissingResourceException ("Unknown domain" + domain, null, key); 70 } 71 String 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 e) { 80 msg = resourceBundle.getString("FormatFailed"); 81 msg += " " + resourceBundle.getString(key); 82 } 83 } 84 } catch (MissingResourceException e) { 86 msg = resourceBundle.getString("BadMessageKey"); 87 throw new MissingResourceException (key, msg, key); 88 } 89 90 if (msg == null) { 92 msg = key; 93 if (arguments.length > 0) { 94 StringBuffer str = new StringBuffer (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 getResourceBundle(String 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 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 136 public static void setLocale(Locale dlocale){ 137 locale = dlocale; 138 } 139 } 140 | Popular Tags |