1 57 58 package com.sun.org.apache.xerces.internal.dom; 59 import java.util.Locale ; 60 import java.util.MissingResourceException ; 61 import java.util.ResourceBundle ; 62 import java.util.PropertyResourceBundle ; 63 64 70 public class DOMMessageFormatter { 71 public static final String DOM_DOMAIN = "http://www.w3.org/dom/DOMTR"; 72 public static final String XML_DOMAIN = "http://www.w3.org/TR/1998/REC-xml-19980210"; 73 public static final String SERIALIZER_DOMAIN = "http://apache.org/xml/serializer"; 74 75 private static ResourceBundle domResourceBundle = null; 76 private static ResourceBundle xmlResourceBundle = null; 77 private static ResourceBundle serResourceBundle = null; 78 private static Locale locale = null; 79 80 81 DOMMessageFormatter(){ 82 locale = Locale.getDefault(); 83 } 84 99 public static String formatMessage(String domain, 100 String key, Object [] arguments) 101 throws MissingResourceException { 102 ResourceBundle resourceBundle = getResourceBundle(domain); 103 if(resourceBundle == null){ 104 init(); 105 resourceBundle = getResourceBundle(domain); 106 if(resourceBundle == null) 107 throw new MissingResourceException ("Unknown domain" + domain, null, key); 108 } 109 String msg; 111 try { 112 msg = key + ": " + resourceBundle.getString(key); 113 if (arguments != null) { 114 try { 115 msg = java.text.MessageFormat.format(msg, arguments); 116 } 117 catch (Exception e) { 118 msg = resourceBundle.getString("FormatFailed"); 119 msg += " " + resourceBundle.getString(key); 120 } 121 } 122 } catch (MissingResourceException e) { 124 msg = resourceBundle.getString("BadMessageKey"); 125 throw new MissingResourceException (key, msg, key); 126 } 127 128 if (msg == null) { 130 msg = key; 131 if (arguments.length > 0) { 132 StringBuffer str = new StringBuffer (msg); 133 str.append('?'); 134 for (int i = 0; i < arguments.length; i++) { 135 if (i > 0) { 136 str.append('&'); 137 } 138 str.append(String.valueOf(arguments[i])); 139 } 140 } 141 } 142 143 return msg; 144 } 145 146 static ResourceBundle getResourceBundle(String domain){ 147 if(domain == DOM_DOMAIN || domain.equals(DOM_DOMAIN)) 148 return domResourceBundle; 149 else if( domain == XML_DOMAIN || domain.equals(XML_DOMAIN)) 150 return xmlResourceBundle; 151 else if(domain == SERIALIZER_DOMAIN || domain.equals(SERIALIZER_DOMAIN)) 152 return serResourceBundle; 153 return null; 154 } 155 158 public static void init(){ 159 if (locale != null) { 160 domResourceBundle = PropertyResourceBundle.getBundle("com.sun.org.apache.xerces.internal.impl.msg.DOMMessages", locale); 161 serResourceBundle = PropertyResourceBundle.getBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLSerializerMessages", locale); 162 xmlResourceBundle = PropertyResourceBundle.getBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLMessages", locale); 163 }else{ 164 domResourceBundle = PropertyResourceBundle.getBundle("com.sun.org.apache.xerces.internal.impl.msg.DOMMessages"); 165 serResourceBundle = PropertyResourceBundle.getBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLSerializerMessages"); 166 xmlResourceBundle = PropertyResourceBundle.getBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLMessages"); 167 } 168 } 169 170 174 public static void setLocale(Locale dlocale){ 175 locale = dlocale; 176 } 177 } 178 | Popular Tags |