1 package org.bouncycastle.i18n; 2 3 import java.text.DateFormat; 4 import java.text.Format; 5 import java.text.MessageFormat; 6 import java.util.Locale; 7 import java.util.MissingResourceException; 8 import java.util.ResourceBundle; 9 import java.util.TimeZone; 10 11 import org.bouncycastle.i18n.filter.Filter; 12 import org.bouncycastle.i18n.filter.UntrustedInput; 13 14 public class LocalizedMessage 15 { 16 17 protected final String id; 18 protected final String resource; 19 20 protected Object[] arguments; 21 protected Object[] filteredArguments; 22 23 protected Filter filter = null; 24 25 /** 26 * Constructs a new LocalizedMessage using <code>resource</code> as the base name for the 27 * RessourceBundle and <code>id</code> as the message bundle id the resource file. 28 * @param resource base name of the resource file 29 * @param id the id of the corresponding bundle in the resource file 30 * @throws NullPointerException if <code>resource</code> or <code>id</code> is <code>null</code> 31 */ 32 public LocalizedMessage(String resource,String id) throws NullPointerException 33 { 34 if (resource == null || id == null) 35 { 36 throw new NullPointerException(); 37 } 38 this.id = id; 39 this.resource = resource; 40 this.arguments = new Object[0]; 41 this.filteredArguments = arguments; 42 } 43 44 /** 45 * Constructs a new LocalizedMessage using <code>resource</code> as the base name for the 46 * RessourceBundle and <code>id</code> as the message bundle id the resource file. 47 * @param resource base name of the resource file 48 * @param id the id of the corresponding bundle in the resource file 49 * @param arguments an array containing the arguments for the message 50 * @throws NullPointerException if <code>resource</code> or <code>id</code> is <code>null</code> 51 */ 52 public LocalizedMessage(String resource, String id, Object[] arguments) throws NullPointerException 53 { 54 if (resource == null || id == null || arguments == null) 55 { 56 throw new NullPointerException(); 57 } 58 this.id = id; 59 this.resource = resource; 60 this.arguments = arguments; 61 this.filteredArguments = arguments; 62 } 63 64 /** 65 * Reads the entry <code>id + "." + key</code> from the resource file and returns a 66 * formatted message for the given Locale and TimeZone. 67 * @param key second part of the entry id 68 * @param loc the used {@link Locale} 69 * @param timezone the used {@link TimeZone} 70 * @return a Strng containing the localized message 71 * @throws MissingEntryException if the resource file is not available or the entry does not exist. 72 */ 73 public String getEntry(String key,Locale loc, TimeZone timezone) throws MissingEntryException 74 { 75 String entry = id + "." + key; 76 77 try 78 { 79 ResourceBundle bundle = ResourceBundle.getBundle(resource,loc); 80 String template = bundle.getString(entry); 81 if (arguments == null || arguments.length == 0) 82 { 83 return template; 84 } 85 else 86 { 87 return formatWithTimeZone(template,filteredArguments,loc,timezone); 88 } 89 } 90 catch (MissingResourceException mre) 91 { 92 throw new MissingEntryException("Can't find entry " + entry + " in resource file " + resource + ".", 93 resource, 94 entry); 95 } 96 } 97 98 protected String formatWithTimeZone( 99 String template, 100 Object[] arguments, 101 Locale locale, 102 TimeZone timezone) 103 { 104 MessageFormat mf = new MessageFormat(" "); 105 mf.setLocale(locale); 106 mf.applyPattern(template); 107 if (!timezone.equals(TimeZone.getDefault())) 108 { 109 Format[] formats = mf.getFormats(); 110 for (int i = 0; i < formats.length; i++) 111 { 112 if (formats[i] instanceof DateFormat) 113 { 114 DateFormat temp = (DateFormat) formats[i]; 115 temp.setTimeZone(timezone); 116 mf.setFormat(i,temp); 117 } 118 } 119 } 120 return mf.format(arguments); 121 } 122 123 /** 124 * Sets the {@link Filter} that is used to filter the arguments of this message 125 * @param filter the {@link Filter} to use. <code>null</code> to disable filtering. 126 */ 127 public void setFilter(Filter filter) 128 { 129 if (filter == null) 130 { 131 filteredArguments = arguments; 132 } 133 else if (!filter.equals(this.filter)) 134 { 135 filteredArguments = new Object[arguments.length]; 136 for (int i = 0; i < arguments.length; i++) 137 { 138 if (arguments[i] instanceof UntrustedInput) 139 { 140 filteredArguments[i] = filter.doFilter(((UntrustedInput) arguments[i]).getString()); 141 } 142 else 143 { 144 filteredArguments[i] = arguments[i]; 145 } 146 } 147 } 148 this.filter = filter; 149 } 150 151 /** 152 * Returns the current filter. 153 * @return the current filter 154 */ 155 public Filter getFilter() 156 { 157 return filter; 158 } 159 160 /** 161 * Returns the id of the message in the resource bundle. 162 * @return the id of the message 163 */ 164 public String getId() 165 { 166 return id; 167 } 168 169 /** 170 * Returns the name of the resource bundle for this message 171 * @return name of the resource file 172 */ 173 public String getResource() 174 { 175 return resource; 176 } 177 178 /** 179 * Returns an <code>Object[]</code> containing the message arguments. 180 * @return the message arguments 181 */ 182 public Object[] getArguments() 183 { 184 return arguments; 185 } 186 187 } 188