1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 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 /** 27 * 28 * 29 * @version $Revision: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $ 30 */ 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(); // TODO Remove lazy call to init 49 } 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(); // TODO Remove lazy call to init 101 } 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 }