1 17 package org.apache.geronimo.validator; 18 19 import java.text.MessageFormat ; 20 import java.util.Enumeration ; 21 import java.util.Hashtable ; 22 import java.util.Locale ; 23 import java.util.MissingResourceException ; 24 import java.util.ResourceBundle ; 25 26 31 public class Messages { 32 static private Hashtable bundles = new Hashtable (); 33 static private Hashtable rbFormats = new Hashtable (); 34 static private Locale globalLocale; 35 36 private ResourceBundle messages; 37 private Hashtable formats; 38 private Locale locale; 39 private String resourceName; 40 41 public Messages(String resourceName) { 42 synchronized (Messages.class) { 43 locale = globalLocale; 44 this.resourceName = resourceName + ".Messages"; 45 46 ResourceBundle rb = (ResourceBundle ) bundles.get(this.resourceName); 47 if (rb == null) { 48 init(); } else { 50 messages = rb; 51 formats = (Hashtable ) rbFormats.get(this.resourceName); 52 } 53 } 54 55 } 56 57 protected void init() { 58 try { 59 if (locale == null) 60 messages = ResourceBundle.getBundle(resourceName); 61 else 62 messages = ResourceBundle.getBundle(resourceName, locale); 63 } catch (Exception except) { 64 messages = new EmptyResourceBundle(); 65 } 66 67 formats = new Hashtable (); 68 69 bundles.put(resourceName, messages); 70 rbFormats.put(resourceName, formats); 71 } 72 73 public String format(String message, Object arg1) { 74 return format(message, new Object [] { arg1 }); 75 } 76 77 public String format(String message, Object arg1, Object arg2) { 78 return format(message, new Object [] { arg1, arg2 }); 79 } 80 81 public String format(String message, Object arg1, Object arg2, Object arg3) { 82 return format(message, new Object [] { arg1, arg2, arg3 }); 83 } 84 85 public String format(String message, Object arg1, Object arg2, Object arg3, Object arg4) { 86 return format(message, new Object [] { arg1, arg2, arg3, arg4 }); 87 } 88 89 public String format(String message, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5) { 90 return format(message, new Object [] { arg1, arg2, arg3, arg4, arg5 }); 91 } 92 93 public String format(String message) { 94 return message(message); 95 } 96 97 public String format(String message, Object [] args) { 98 if (locale != globalLocale) { 99 synchronized (Messages.class) { 100 init(); } 102 } 103 104 MessageFormat mf; 105 String msg; 106 107 try { 108 mf = (MessageFormat ) formats.get(message); 109 if (mf == null) { 110 try { 111 msg = messages.getString(message); 112 } catch (MissingResourceException except) { 113 return message; 114 } 115 mf = new MessageFormat (msg); 116 formats.put(message, mf); 117 } 118 return mf.format(args); 119 } catch (Exception except) { 120 return "An internal error occured while processing message " + message; 121 } 122 } 123 124 public String message(String message) { 125 if (locale != globalLocale) { 126 synchronized (Messages.class) { 127 init(); 128 } 129 } 130 131 try { 132 return messages.getString(message); 133 } catch (MissingResourceException except) { 134 return message; 135 } 136 } 137 138 static public void setLocale(Locale locale) { 139 synchronized (Messages.class) { 140 globalLocale = locale; 141 bundles = new Hashtable (); 142 rbFormats = new Hashtable (); 143 } 144 } 145 146 static { 147 setLocale(Locale.getDefault()); 148 } 149 150 private static final class EmptyResourceBundle extends ResourceBundle implements Enumeration { 151 152 public Enumeration getKeys() { 153 return this; 154 } 155 156 protected Object handleGetObject(String name) { 157 return "[Missing message " + name + "]"; 158 } 159 160 public boolean hasMoreElements() { 161 return false; 162 } 163 164 public Object nextElement() { 165 return null; 166 } 167 168 } 169 170 } 171 | Popular Tags |