1 16 package org.apache.commons.vfs.util; 17 18 import java.text.MessageFormat ; 19 import java.util.HashMap ; 20 import java.util.Map ; 21 import java.util.MissingResourceException ; 22 import java.util.ResourceBundle ; 23 24 29 public class Messages 30 { 31 34 private static Map messages = new HashMap (); 35 private static ResourceBundle resources; 36 37 private Messages() 38 { 39 } 40 41 47 public static String getString(final String code) 48 { 49 return getString(code, new Object [0]); 50 } 51 52 59 public static String getString(final String code, final Object param) 60 { 61 return getString(code, new Object []{param}); 62 } 63 64 71 public static String getString(final String code, final Object [] params) 72 { 73 try 74 { 75 if (code == null) 76 { 77 return null; 78 } 79 80 final MessageFormat msg = findMessage(code); 81 return msg.format(params); 82 } 83 catch (final MissingResourceException mre) 84 { 85 return "Unknown message with code \"" + code + "\"."; 86 } 87 } 88 89 92 private static synchronized MessageFormat findMessage(final String code) 93 throws MissingResourceException 94 { 95 MessageFormat msg = (MessageFormat ) messages.get(code); 97 if (msg != null) 98 { 99 return msg; 100 } 101 102 if (resources == null) 104 { 105 resources = ResourceBundle.getBundle("org.apache.commons.vfs.Resources"); 106 } 107 final String msgText = resources.getString(code); 108 msg = new MessageFormat (msgText); 109 messages.put(code, msg); 110 return msg; 111 } 112 } 113 | Popular Tags |