1 11 package org.eclipse.jdt.internal.compiler.util; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.lang.reflect.Field ; 16 import java.lang.reflect.Modifier ; 17 import java.text.MessageFormat ; 18 import java.util.ArrayList ; 19 import java.util.HashMap ; 20 import java.util.Locale ; 21 import java.util.Map ; 22 import java.util.Properties ; 23 24 public final class Messages { 25 private static class MessagesProperties extends Properties { 26 27 private static final int MOD_EXPECTED = Modifier.PUBLIC | Modifier.STATIC; 28 private static final int MOD_MASK = MOD_EXPECTED | Modifier.FINAL; 29 private static final long serialVersionUID = 1L; 30 31 private final Map fields; 32 33 public MessagesProperties(Field [] fieldArray, String bundleName) { 34 super(); 35 final int len = fieldArray.length; 36 fields = new HashMap (len * 2); 37 for (int i = 0; i < len; i++) { 38 fields.put(fieldArray[i].getName(), fieldArray[i]); 39 } 40 } 41 42 45 public synchronized Object put(Object key, Object value) { 46 try { 47 Field field = (Field ) fields.get(key); 48 if (field == null) { 49 return null; 50 } 51 if ((field.getModifiers() & MOD_MASK) != MOD_EXPECTED) 53 return null; 54 try { 59 field.set(null, value); 60 } catch (Exception e) { 61 } 63 } catch (SecurityException e) { 64 } 66 return null; 67 } 68 } 69 70 71 private static String [] nlSuffixes; 72 private static final String EXTENSION = ".properties"; 74 private static final String BUNDLE_NAME = "org.eclipse.jdt.internal.compiler.messages"; 76 private Messages() { 77 } 79 80 public static String compilation_unresolvedProblem; 81 public static String compilation_unresolvedProblems; 82 public static String compilation_request; 83 public static String compilation_loadBinary; 84 public static String compilation_process; 85 public static String compilation_write; 86 public static String compilation_done; 87 public static String compilation_units; 88 public static String compilation_unit; 89 public static String compilation_internalError; 90 public static String output_isFile; 91 public static String output_notValidAll; 92 public static String output_notValid; 93 public static String problem_noSourceInformation; 94 public static String problem_atLine; 95 public static String abort_invalidAttribute; 96 public static String abort_invalidExceptionAttribute; 97 public static String abort_missingCode; 98 public static String abort_againstSourceModel; 99 public static String accept_cannot; 100 public static String parser_incorrectPath; 101 public static String parser_moveFiles; 102 public static String parser_syntaxRecovery; 103 public static String parser_regularParse; 104 public static String parser_missingFile; 105 public static String parser_corruptedFile; 106 public static String parser_endOfFile; 107 public static String parser_endOfConstructor; 108 public static String parser_endOfMethod; 109 public static String parser_endOfInitializer; 110 public static String ast_missingCode; 111 public static String constant_cannotCastedInto; 112 public static String constant_cannotConvertedTo; 113 114 static { 115 initializeMessages(BUNDLE_NAME, Messages.class); 116 } 117 118 124 public static String bind(String message) { 125 return bind(message, null); 126 } 127 128 135 public static String bind(String message, Object binding) { 136 return bind(message, new Object [] {binding}); 137 } 138 139 147 public static String bind(String message, Object binding1, Object binding2) { 148 return bind(message, new Object [] {binding1, binding2}); 149 } 150 151 158 public static String bind(String message, Object [] bindings) { 159 return MessageFormat.format(message, bindings); 160 } 161 162 165 private static String [] buildVariants(String root) { 166 if (nlSuffixes == null) { 167 String nl = Locale.getDefault().toString(); 169 ArrayList result = new ArrayList (4); 170 int lastSeparator; 171 while (true) { 172 result.add('_' + nl + EXTENSION); 173 lastSeparator = nl.lastIndexOf('_'); 174 if (lastSeparator == -1) 175 break; 176 nl = nl.substring(0, lastSeparator); 177 } 178 result.add(EXTENSION); 180 nlSuffixes = (String []) result.toArray(new String [result.size()]); 181 } 182 root = root.replace('.', '/'); 183 String [] variants = new String [nlSuffixes.length]; 184 for (int i = 0; i < variants.length; i++) 185 variants[i] = root + nlSuffixes[i]; 186 return variants; 187 } 188 public static void initializeMessages(String bundleName, Class clazz) { 189 final Field [] fields = clazz.getDeclaredFields(); 191 load(bundleName, clazz.getClassLoader(), fields); 192 193 final int MOD_EXPECTED = Modifier.PUBLIC | Modifier.STATIC; 195 final int MOD_MASK = MOD_EXPECTED | Modifier.FINAL; 196 final int numFields = fields.length; 197 for (int i = 0; i < numFields; i++) { 198 Field field = fields[i]; 199 if ((field.getModifiers() & MOD_MASK) != MOD_EXPECTED) 200 continue; 201 try { 202 if (field.get(clazz) == null) { 207 String value = "Missing message: " + field.getName() + " in: " + bundleName; field.set(null, value); 209 } 210 } catch (IllegalArgumentException e) { 211 } catch (IllegalAccessException e) { 213 } 215 } 216 } 217 220 public static void load(final String bundleName, final ClassLoader loader, final Field [] fields) { 221 final String [] variants = buildVariants(bundleName); 222 for (int i = variants.length; --i >= 0;) { 224 InputStream input = (loader == null) 225 ? ClassLoader.getSystemResourceAsStream(variants[i]) 226 : loader.getResourceAsStream(variants[i]); 227 if (input == null) continue; 228 try { 229 final MessagesProperties properties = new MessagesProperties(fields, bundleName); 230 properties.load(input); 231 } catch (IOException e) { 232 } finally { 234 try { 235 input.close(); 236 } catch (IOException e) { 237 } 239 } 240 } 241 } 242 } 243 | Popular Tags |