comparison org.eclipse.osgi/supplement/src/org/eclipse/osgi/util/NLS.d @ 105:bbe49769ec18

...
author Frank Benoit <benoit@tionex.de>
date Sun, 08 Nov 2009 12:42:30 +0100
parents 8594250b1d1c
children
comparison
equal deleted inserted replaced
104:88652073d1c2 105:bbe49769ec18
5 * which accompanies this distribution, and is available at 5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html 6 * http://www.eclipse.org/legal/epl-v10.html
7 * 7 *
8 * Contributors: 8 * Contributors:
9 * IBM - Initial API and implementation 9 * IBM - Initial API and implementation
10 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/ 10 *******************************************************************************/
11 // Port to the D programming language:
12 // Frank Benoit <benoit@tionex.de>
13 module org.eclipse.osgi.util.NLS; 13 module org.eclipse.osgi.util.NLS;
14 14
15 import java.lang.all; 15 import java.lang.all;
16 16
17 // import java.io.IOException; 17
18 // import java.io.InputStream; 18 import java.io.IOException;
19 // import java.lang.reflect.Field; 19 import java.io.InputStream;
20 // import java.lang.reflect.Modifier; 20 import java.lang.reflect.Field;
21 // import java.security.AccessController; 21 import java.lang.reflect.Modifier;
22 // import java.security.PrivilegedAction; 22 import java.security.AccessController;
23 // import java.util.ArrayList; 23 import java.security.PrivilegedAction;
24 // import java.util.HashMap; 24 import java.util.ArrayList;
25 // import java.util.Locale; 25 import java.util.HashMap;
26 // import java.util.Map; 26 import java.util.Locale;
27 // import java.util.Properties; 27 import java.util.Map;
28 // 28 import java.util.Properties;
29 // import org.eclipse.osgi.framework.debug.Debug; 29
30 // import org.eclipse.osgi.framework.log.FrameworkLog; 30 import org.eclipse.osgi.framework.debug_.Debug;
31 // import org.eclipse.osgi.framework.log.FrameworkLogEntry; 31 import org.eclipse.osgi.framework.log.FrameworkLog;
32 import org.eclipse.osgi.framework.log.FrameworkLogEntry;
32 33
33 /** 34 /**
34 * Common superclass for all message bundle classes. Provides convenience 35 * Common superclass for all message bundle classes. Provides convenience
35 * methods for manipulating messages. 36 * methods for manipulating messages.
36 * <p> 37 * <p>
58 * 59 *
59 * @since 3.1 60 * @since 3.1
60 */ 61 */
61 public abstract class NLS { 62 public abstract class NLS {
62 63
63 // private static final Object[] EMPTY_ARGS = new Object[0]; 64 private static final Object[] EMPTY_ARGS = null;
64 // private static final String EXTENSION = ".properties"; //$NON-NLS-1$ 65 private static final String EXTENSION = ".properties"; //$NON-NLS-1$
65 // private static String[] nlSuffixes; 66 private static String[] nlSuffixes;
66 // /* 67 /*
67 // * NOTE do not change the name of this field; it is set by the Framework using reflection 68 * NOTE do not change the name of this field; it is set by the Framework using reflection
68 // */ 69 */
69 // private static FrameworkLog frameworkLog; 70 private static FrameworkLog frameworkLog;
70 // 71
71 // static final int SEVERITY_ERROR = 0x04; 72 static final int SEVERITY_ERROR = 0x04;
72 // static final int SEVERITY_WARNING = 0x02; 73 static final int SEVERITY_WARNING = 0x02;
73 // /* 74 /*
74 // * This object is assigned to the value of a field map to indicate 75 * This object is assigned to the value of a field map to indicate
75 // * that a translated message has already been assigned to that field. 76 * that a translated message has already been assigned to that field.
76 // */ 77 */
77 // static final Object ASSIGNED = new Object(); 78 static final Object ASSIGNED = new Object();
78 // 79
79 // /** 80 /**
80 // * Creates a new NLS instance. 81 * Creates a new NLS instance.
81 // */ 82 */
82 // protected NLS() { 83 protected this() {
83 // super(); 84 super();
84 // } 85 }
85 86
86 /** 87 /**
87 * Bind the given message's substitution locations with the given string value. 88 * Bind the given message's substitution locations with the given string value.
88 * 89 *
89 * @param message the message to be manipulated 90 * @param message the message to be manipulated
90 * @param binding the object to be inserted into the message 91 * @param binding the object to be inserted into the message
91 * @return the manipulated String 92 * @return the manipulated String
92 * @throws IllegalArgumentException if the text appearing within curly braces in the given message does not map to an integer 93 * @throws IllegalArgumentException if the text appearing within curly braces in the given message does not map to an integer
93 */ 94 */
94 public static String bind(String message, Object binding) { 95 public static String bind(String message, Object binding) {
95 implMissing( __FILE__, __LINE__ ); 96 return internalBind(message, null, String.valueOf(binding), null);
96 return null;
97 // return internalBind(message, null, String.valueOf(binding), null);
98 }
99 public static String bind(String message, String binding) {
100 implMissing( __FILE__, __LINE__ );
101 return null;
102 // return internalBind(message, null, String.valueOf(binding), null);
103 } 97 }
104 98
105 /** 99 /**
106 * Bind the given message's substitution locations with the given string values. 100 * Bind the given message's substitution locations with the given string values.
107 * 101 *
110 * @param binding2 A second object to be inserted into the message 104 * @param binding2 A second object to be inserted into the message
111 * @return the manipulated String 105 * @return the manipulated String
112 * @throws IllegalArgumentException if the text appearing within curly braces in the given message does not map to an integer 106 * @throws IllegalArgumentException if the text appearing within curly braces in the given message does not map to an integer
113 */ 107 */
114 public static String bind(String message, Object binding1, Object binding2) { 108 public static String bind(String message, Object binding1, Object binding2) {
115 implMissing( __FILE__, __LINE__ ); 109 return internalBind(message, null, String.valueOf(binding1), String.valueOf(binding2));
116 return null;
117 // return internalBind(message, null, String.valueOf(binding1), String.valueOf(binding2));
118 }
119 public static String bind(String message, String binding1, String binding2) {
120 implMissing( __FILE__, __LINE__ );
121 return null;
122 // return internalBind(message, null, String.valueOf(binding1), String.valueOf(binding2));
123 } 110 }
124 111
125 /** 112 /**
126 * Bind the given message's substitution locations with the given string values. 113 * Bind the given message's substitution locations with the given string values.
127 * 114 *
129 * @param bindings An array of objects to be inserted into the message 116 * @param bindings An array of objects to be inserted into the message
130 * @return the manipulated String 117 * @return the manipulated String
131 * @throws IllegalArgumentException if the text appearing within curly braces in the given message does not map to an integer 118 * @throws IllegalArgumentException if the text appearing within curly braces in the given message does not map to an integer
132 */ 119 */
133 public static String bind(String message, Object[] bindings) { 120 public static String bind(String message, Object[] bindings) {
134 implMissing( __FILE__, __LINE__ ); 121 return internalBind(message, bindings, null, null);
135 return null; 122 }
136 // return internalBind(message, bindings, null, null); 123
137 } 124 /**
138 public static String bind(String message, String[] bindings) { 125 * Initialize the given class with the values from the specified message bundle.
139 implMissing( __FILE__, __LINE__ ); 126 *
140 return null; 127 * @param bundleName fully qualified path of the class name
141 // return internalBind(message, bindings, null, null); 128 * @param clazz the class where the constants will exist
142 } 129 */
143 130 public static void initializeMessages(/+FIXFINAL+/ String bundleName, /+FIXFINAL+/ Class clazz) {
144 // /** 131 if (System.getSecurityManager() is null) {
145 // * Initialize the given class with the values from the specified message bundle. 132 load(bundleName, clazz);
146 // * 133 return;
147 // * @param bundleName fully qualified path of the class name 134 }
148 // * @param clazz the class where the constants will exist 135 AccessController.doPrivileged(new class() PrivilegedAction {
149 // */ 136 public Object run() {
150 // public static void initializeMessages(final String bundleName, final Class clazz) { 137 load(bundleName, clazz);
151 // if (System.getSecurityManager() is null) { 138 return null;
152 // load(bundleName, clazz); 139 }
153 // return; 140 });
154 // } 141 }
155 // AccessController.doPrivileged(new PrivilegedAction() { 142
156 // public Object run() { 143 /*
157 // load(bundleName, clazz); 144 * Perform the string substitution on the given message with the specified args.
158 // return null; 145 * See the class comment for exact details.
159 // } 146 */
160 // }); 147 private static String internalBind(String message, Object[] args, String argZero, String argOne) {
161 // } 148 if (message is null)
162 // 149 return "No message available."; //$NON-NLS-1$
163 // /* 150 if (args is null || args.length_ is 0)
164 // * Perform the string substitution on the given message with the specified args. 151 args = EMPTY_ARGS;
165 // * See the class comment for exact details. 152
166 // */ 153 int length_ = message.length();
167 // private static String internalBind(String message, Object[] args, String argZero, String argOne) { 154 //estimate correct size of string buffer to avoid growth
168 // if (message is null) 155 int bufLen = length_ + (args.length_ * 5);
169 // return "No message available."; //$NON-NLS-1$ 156 if (argZero !is null)
170 // if (args is null || args.length is 0) 157 bufLen += argZero.length() - 3;
171 // args = EMPTY_ARGS; 158 if (argOne !is null)
172 // 159 bufLen += argOne.length() - 3;
173 // int length = message.length(); 160 StringBuffer buffer = new StringBuffer(bufLen < 0 ? 0 : bufLen);
174 // //estimate correct size of string buffer to avoid growth 161 for (int i = 0; i < length_; i++) {
175 // int bufLen = length + (args.length * 5); 162 char c = message.charAt(i);
176 // if (argZero !is null) 163 switch (c) {
177 // bufLen += argZero.length() - 3; 164 case '{' :
178 // if (argOne !is null) 165 int index = message.indexOf('}', i);
179 // bufLen += argOne.length() - 3; 166 // if we don't have a matching closing brace then...
180 // StringBuffer buffer = new StringBuffer(bufLen < 0 ? 0 : bufLen); 167 if (index is -1) {
181 // for (int i = 0; i < length; i++) { 168 buffer.append(c);
182 // char c = message.charAt(i); 169 break;
183 // switch (c) { 170 }
184 // case '{' : 171 i++;
185 // int index = message.indexOf('}', i); 172 if (i >= length_) {
186 // // if we don't have a matching closing brace then... 173 buffer.append(c);
187 // if (index is -1) { 174 break;
188 // buffer.append(c); 175 }
189 // break; 176 // look for a substitution
190 // } 177 int number = -1;
191 // i++; 178 try {
192 // if (i >= length) { 179 number = Integer.parseInt(message.substring(i, index));
193 // buffer.append(c); 180 } catch (NumberFormatException e) {
194 // break; 181 throw new IllegalArgumentException();
195 // } 182 }
196 // // look for a substitution 183 if (number is 0 && argZero !is null)
197 // int number = -1; 184 buffer.append(argZero);
198 // try { 185 else if (number is 1 && argOne !is null)
199 // number = Integer.parseInt(message.substring(i, index)); 186 buffer.append(argOne);
200 // } catch (NumberFormatException e) { 187 else {
201 // throw new IllegalArgumentException(); 188 if (number >= args.length_ || number < 0) {
202 // } 189 buffer.append("<missing argument>"); //$NON-NLS-1$
203 // if (number is 0 && argZero !is null) 190 i = index;
204 // buffer.append(argZero); 191 break;
205 // else if (number is 1 && argOne !is null) 192 }
206 // buffer.append(argOne); 193 buffer.append(args[number]);
207 // else { 194 }
208 // if (number >= args.length || number < 0) { 195 i = index;
209 // buffer.append("<missing argument>"); //$NON-NLS-1$ 196 break;
210 // i = index; 197 case '\'' :
211 // break; 198 // if a single quote is the last char on the line then skip it
212 // } 199 int nextIndex = i + 1;
213 // buffer.append(args[number]); 200 if (nextIndex >= length_) {
214 // } 201 buffer.append(c);
215 // i = index; 202 break;
216 // break; 203 }
217 // case '\'' : 204 char next = message.charAt(nextIndex);
218 // // if a single quote is the last char on the line then skip it 205 // if the next char is another single quote then write out one
219 // int nextIndex = i + 1; 206 if (next is '\'') {
220 // if (nextIndex >= length) { 207 i++;
221 // buffer.append(c); 208 buffer.append(c);
222 // break; 209 break;
223 // } 210 }
224 // char next = message.charAt(nextIndex); 211 // otherwise we want to read until we get to the next single quote
225 // // if the next char is another single quote then write out one 212 index = message.indexOf('\'', nextIndex);
226 // if (next is '\'') { 213 // if there are no more in the string, then skip it
227 // i++; 214 if (index is -1) {
228 // buffer.append(c); 215 buffer.append(c);
229 // break; 216 break;
230 // } 217 }
231 // // otherwise we want to read until we get to the next single quote 218 // otherwise write out the chars inside the quotes
232 // index = message.indexOf('\'', nextIndex); 219 buffer.append(message.substring(nextIndex, index));
233 // // if there are no more in the string, then skip it 220 i = index;
234 // if (index is -1) { 221 break;
235 // buffer.append(c); 222 default :
236 // break; 223 buffer.append(c);
237 // } 224 }
238 // // otherwise write out the chars inside the quotes 225 }
239 // buffer.append(message.substring(nextIndex, index)); 226 return buffer.toString();
240 // i = index; 227 }
241 // break; 228
242 // default : 229 /*
243 // buffer.append(c); 230 * Build an array of property files to search. The returned array contains
244 // } 231 * the property fields in order from most specific to most generic.
245 // } 232 * So, in the FR_fr locale, it will return file_fr_FR.properties, then
246 // return buffer.toString(); 233 * file_fr.properties, and finally file.properties.
247 // } 234 */
248 // 235 private static String[] buildVariants(String root) {
249 // /* 236 if (nlSuffixes is null) {
250 // * Build an array of property files to search. The returned array contains 237 //build list of suffixes for loading resource bundles
251 // * the property fields in order from most specific to most generic. 238 String nl = Locale.getDefault().toString();
252 // * So, in the FR_fr locale, it will return file_fr_FR.properties, then 239 ArrayList result = new ArrayList(4);
253 // * file_fr.properties, and finally file.properties. 240 int lastSeparator;
254 // */ 241 while (true) {
255 // private static String[] buildVariants(String root) { 242 result.add('_' + nl + EXTENSION);
256 // if (nlSuffixes is null) { 243 lastSeparator = nl.lastIndexOf('_');
257 // //build list of suffixes for loading resource bundles 244 if (lastSeparator is -1)
258 // String nl = Locale.getDefault().toString(); 245 break;
259 // ArrayList result = new ArrayList(4); 246 nl = nl.substring(0, lastSeparator);
260 // int lastSeparator; 247 }
261 // while (true) { 248 //add the empty suffix last (most general)
262 // result.add('_' + nl + EXTENSION); 249 result.add(EXTENSION);
263 // lastSeparator = nl.lastIndexOf('_'); 250 nlSuffixes = stringcast( result.toArray)(new String[result.size()]);
264 // if (lastSeparator is -1) 251 }
265 // break; 252 root = root.replace('.', '/');
266 // nl = nl.substring(0, lastSeparator); 253 String[] variants = new String[nlSuffixes.length_];
267 // } 254 for (int i = 0; i < variants.length_; i++)
268 // //add the empty suffix last (most general) 255 variants[i] = root + nlSuffixes[i];
269 // result.add(EXTENSION); 256 return variants;
270 // nlSuffixes = (String[]) result.toArray(new String[result.size()]); 257 }
271 // } 258
272 // root = root.replace('.', '/'); 259 private static void computeMissingMessages(String bundleName, Class clazz, Map fieldMap, Field[] fieldArray, bool isAccessible) {
273 // String[] variants = new String[nlSuffixes.length]; 260 // iterate over the fields in the class to make sure that there aren't any empty ones
274 // for (int i = 0; i < variants.length; i++) 261 final int MOD_EXPECTED = Modifier.PUBLIC | Modifier.STATIC;
275 // variants[i] = root + nlSuffixes[i]; 262 final int MOD_MASK = MOD_EXPECTED | Modifier.FINAL;
276 // return variants; 263 final int numFields = fieldArray.length_;
277 // } 264 for (int i = 0; i < numFields; i++) {
278 // 265 Field field = fieldArray[i];
279 // private static void computeMissingMessages(String bundleName, Class clazz, Map fieldMap, Field[] fieldArray, bool isAccessible) { 266 if ((field.getModifiers() & MOD_MASK) !is MOD_EXPECTED)
280 // // iterate over the fields in the class to make sure that there aren't any empty ones 267 continue;
281 // final int MOD_EXPECTED = Modifier.PUBLIC | Modifier.STATIC; 268 //if the field has a a value assigned, there is nothing to do
282 // final int MOD_MASK = MOD_EXPECTED | Modifier.FINAL; 269 if (fieldMap.get(field.getName()) is ASSIGNED)
283 // final int numFields = fieldArray.length; 270 continue;
284 // for (int i = 0; i < numFields; i++) { 271 try {
285 // Field field = fieldArray[i]; 272 // Set a value for this empty field. We should never get an exception here because
286 // if ((field.getModifiers() & MOD_MASK) !is MOD_EXPECTED) 273 // we know we have a public static non-final field. If we do get an exception, silently
287 // continue; 274 // log it and continue. This means that the field will (most likely) be un-initialized and
288 // //if the field has a a value assigned, there is nothing to do 275 // will fail later in the code and if so then we will see both the NPE and this error.
289 // if (fieldMap.get(field.getName()) is ASSIGNED) 276 String value = "NLS missing message: " + field.getName() + " in_: " + bundleName; //$NON-NLS-1$ //$NON-NLS-2$
290 // continue; 277 if (Debug.DEBUG_MESSAGE_BUNDLES)
291 // try { 278 System.out_.println(value);
292 // // Set a value for this empty field. We should never get an exception here because 279 log(SEVERITY_WARNING, value, null);
293 // // we know we have a public static non-final field. If we do get an exception, silently 280 if (!isAccessible)
294 // // log it and continue. This means that the field will (most likely) be un-initialized and 281 field.setAccessible(true);
295 // // will fail later in the code and if so then we will see both the NPE and this error. 282 field.set(null, value);
296 // String value = "NLS missing message: " + field.getName() + " in: " + bundleName; //$NON-NLS-1$ //$NON-NLS-2$ 283 } catch (Exception e) {
297 // if (Debug.DEBUG_MESSAGE_BUNDLES) 284 log(SEVERITY_ERROR, "Error setting the missing message value for: " + field.getName(), e); //$NON-NLS-1$
298 // System.out.println(value); 285 }
299 // log(SEVERITY_WARNING, value, null); 286 }
300 // if (!isAccessible) 287 }
301 // field.setAccessible(true); 288
302 // field.set(null, value); 289 /*
303 // } catch (Exception e) { 290 * Load the given resource bundle using the specified class loader.
304 // log(SEVERITY_ERROR, "Error setting the missing message value for: " + field.getName(), e); //$NON-NLS-1$ 291 */
305 // } 292 static void load(/+FIXFINAL+/ String bundleName, Class clazz) {
306 // } 293 long start = System.currentTimeMillis();
307 // } 294 final Field[] fieldArray = clazz.getDeclaredFields();
308 // 295 ClassLoader loader = clazz.getClassLoader();
309 // /* 296
310 // * Load the given resource bundle using the specified class loader. 297 bool isAccessible = (clazz.getModifiers() & Modifier.PUBLIC) !is 0;
311 // */ 298
312 // static void load(final String bundleName, Class clazz) { 299 //build a map of field names to Field objects
313 // long start = System.currentTimeMillis(); 300 final int len = fieldArray.length_;
314 // final Field[] fieldArray = clazz.getDeclaredFields(); 301 Map fields = new HashMap(len * 2);
315 // ClassLoader loader = clazz.getClassLoader(); 302 for (int i = 0; i < len; i++)
316 // 303 fields.put(fieldArray[i].getName(), fieldArray[i]);
317 // bool isAccessible = (clazz.getModifiers() & Modifier.PUBLIC) !is 0; 304
318 // 305 // search the variants from most specific to most general, since
319 // //build a map of field names to Field objects 306 // the MessagesProperties.put method will mark assigned fields
320 // final int len = fieldArray.length; 307 // to prevent them from being assigned twice
321 // Map fields = new HashMap(len * 2); 308 final String[] variants = buildVariants(bundleName);
322 // for (int i = 0; i < len; i++) 309 for (int i = 0; i < variants.length_; i++) {
323 // fields.put(fieldArray[i].getName(), fieldArray[i]); 310 // loader==null if we're launched off the Java boot classpath
324 // 311 final InputStream input = loader is null ? ClassLoader.getSystemResourceAsStream(variants[i]) : loader.getResourceAsStream(variants[i]);
325 // // search the variants from most specific to most general, since 312 if (input is null)
326 // // the MessagesProperties.put method will mark assigned fields 313 continue;
327 // // to prevent them from being assigned twice 314 try {
328 // final String[] variants = buildVariants(bundleName); 315 final MessagesProperties properties = new MessagesProperties(fields, bundleName, isAccessible);
329 // for (int i = 0; i < variants.length; i++) { 316 properties.load(input);
330 // // loader is null if we're launched off the Java boot classpath 317 } catch (IOException e) {
331 // final InputStream input = loader is null ? ClassLoader.getSystemResourceAsStream(variants[i]) : loader.getResourceAsStream(variants[i]); 318 log(SEVERITY_ERROR, "Error loading " + variants[i], e); //$NON-NLS-1$
332 // if (input is null) 319 } finally {
333 // continue; 320 if (input !is null)
334 // try { 321 try {
335 // final MessagesProperties properties = new MessagesProperties(fields, bundleName, isAccessible); 322 input.close();
336 // properties.load(input); 323 } catch (IOException e) {
337 // } catch (IOException e) { 324 // ignore
338 // log(SEVERITY_ERROR, "Error loading " + variants[i], e); //$NON-NLS-1$ 325 }
339 // } finally { 326 }
340 // if (input !is null) 327 }
341 // try { 328 computeMissingMessages(bundleName, clazz, fields, fieldArray, isAccessible);
342 // input.close(); 329 if (Debug.DEBUG_MESSAGE_BUNDLES)
343 // } catch (IOException e) { 330 System.out_.println("Time to load message bundle: " + bundleName + " was " + (System.currentTimeMillis() - start) + "ms."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
344 // // ignore 331 }
345 // } 332
346 // } 333 /*
347 // } 334 * The method adds a log entry based on the error message and exception.
348 // computeMissingMessages(bundleName, clazz, fields, fieldArray, isAccessible); 335 * The output is written to the System.err.
349 // if (Debug.DEBUG_MESSAGE_BUNDLES) 336 *
350 // System.out.println("Time to load message bundle: " + bundleName + " was " + (System.currentTimeMillis() - start) + "ms."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ 337 * This method is only expected to be called if there is a problem in
351 // } 338 * the NLS mechanism. As a result, translation facility is not available
352 // 339 * here and messages coming out of this log are generally not translated.
353 // /* 340 *
354 // * The method adds a log entry based on the error message and exception. 341 * @param severity - severity of the message (SEVERITY_ERROR or SEVERITY_WARNING)
355 // * The output is written to the System.err. 342 * @param message - message to log
356 // * 343 * @param e - exception to log
357 // * This method is only expected to be called if there is a problem in 344 */
358 // * the NLS mechanism. As a result, translation facility is not available 345 static void log(int severity, String message, Exception e) {
359 // * here and messages coming out of this log are generally not translated. 346 if (frameworkLog !is null) {
360 // * 347 frameworkLog.log(new FrameworkLogEntry("org.eclipse.osgi", severity, 1, message, 0, e, null)); //$NON-NLS-1$
361 // * @param severity - severity of the message (SEVERITY_ERROR or SEVERITY_WARNING) 348 return;
362 // * @param message - message to log 349 }
363 // * @param e - exception to log 350 String statusMsg;
364 // */ 351 switch (severity) {
365 // static void log(int severity, String message, Exception e) { 352 case SEVERITY_ERROR :
366 // if (frameworkLog !is null) { 353 statusMsg = "Error: "; //$NON-NLS-1$
367 // frameworkLog.log(new FrameworkLogEntry("org.eclipse.osgi", severity, 1, message, 0, e, null)); //$NON-NLS-1$ 354 break;
368 // return; 355 case SEVERITY_WARNING :
369 // } 356 // intentionally fall through:
370 // String statusMsg; 357 default :
371 // switch (severity) { 358 statusMsg = "Warning: "; //$NON-NLS-1$
372 // case SEVERITY_ERROR : 359 }
373 // statusMsg = "Error: "; //$NON-NLS-1$ 360 if (message !is null)
374 // break; 361 statusMsg += message;
375 // case SEVERITY_WARNING : 362 if (e !is null)
376 // // intentionally fall through: 363 statusMsg += ": " + e.getMessage(); //$NON-NLS-1$
377 // default : 364 System.err.println(statusMsg);
378 // statusMsg = "Warning: "; //$NON-NLS-1$ 365 if (e !is null)
379 // } 366 e.printStackTrace();
380 // if (message !is null) 367 }
381 // statusMsg += message; 368
382 // if (e !is null) 369 /*
383 // statusMsg += ": " + e.getMessage(); //$NON-NLS-1$ 370 * Class which sub-classes java.util.Properties and uses the #put method
384 // System.err.println(statusMsg); 371 * to set field values rather than storing the values in the table.
385 // if (e !is null) 372 */
386 // e.printStackTrace(); 373 private static class MessagesProperties : Properties {
387 // } 374
388 // 375 private static final int MOD_EXPECTED = Modifier.PUBLIC | Modifier.STATIC;
389 // /* 376 private static final int MOD_MASK = MOD_EXPECTED | Modifier.FINAL;
390 // * Class which sub-classes java.util.Properties and uses the #put method 377 private static final long serialVersionUID = 1L;
391 // * to set field values rather than storing the values in the table. 378
392 // */ 379 private final String bundleName;
393 // private static class MessagesProperties extends Properties { 380 private final Map fields;
394 // 381 private final bool isAccessible;
395 // private static final int MOD_EXPECTED = Modifier.PUBLIC | Modifier.STATIC; 382
396 // private static final int MOD_MASK = MOD_EXPECTED | Modifier.FINAL; 383 public this(Map fieldMap, String bundleName, bool isAccessible) {
397 // private static final long serialVersionUID = 1L; 384 super();
398 // 385 this.fields = fieldMap;
399 // private final String bundleName; 386 this.bundleName = bundleName;
400 // private final Map fields; 387 this.isAccessible = isAccessible;
401 // private final bool isAccessible; 388 }
402 // 389
403 // public MessagesProperties(Map fieldMap, String bundleName, bool isAccessible) { 390 /* (non-Javadoc)
404 // super(); 391 * @see java.util.Hashtable#put(java.lang.Object, java.lang.Object)
405 // this.fields = fieldMap; 392 */
406 // this.bundleName = bundleName; 393 public synchronized Object put(Object key, Object value) {
407 // this.isAccessible = isAccessible; 394 Object fieldObject = fields.put(key, ASSIGNED);
408 // } 395 // if already assigned, there is nothing to do
409 // 396 if (fieldObject is ASSIGNED)
410 // /* (non-Javadoc) 397 return null;
411 // * @see java.util.Hashtable#put(java.lang.Object, java.lang.Object) 398 if (fieldObject is null) {
412 // */ 399 final String msg = "NLS unused message: " + key + " in_: " + bundleName;//$NON-NLS-1$ //$NON-NLS-2$
413 // public synchronized Object put(Object key, Object value) { 400 if (Debug.DEBUG_MESSAGE_BUNDLES)
414 // Object fieldObject = fields.put(key, ASSIGNED); 401 System.out_.println(msg);
415 // // if already assigned, there is nothing to do 402 log(SEVERITY_WARNING, msg, null);
416 // if (fieldObject is ASSIGNED) 403 return null;
417 // return null; 404 }
418 // if (fieldObject is null) { 405 final Field field = cast(Field) fieldObject;
419 // final String msg = "NLS unused message: " + key + " in: " + bundleName;//$NON-NLS-1$ //$NON-NLS-2$ 406 //can only set value of public static non-final fields
420 // if (Debug.DEBUG_MESSAGE_BUNDLES) 407 if ((field.getModifiers() & MOD_MASK) !is MOD_EXPECTED)
421 // System.out.println(msg); 408 return null;
422 // log(SEVERITY_WARNING, msg, null); 409 try {
423 // return null; 410 // Check to see if we are allowed to modify the field. If we aren't (for instance
424 // } 411 // if the class is not public) then change the accessible attribute of the field
425 // final Field field = (Field) fieldObject; 412 // before trying to set the value.
426 // //can only set value of public static non-final fields 413 if (!isAccessible)
427 // if ((field.getModifiers() & MOD_MASK) !is MOD_EXPECTED) 414 field.setAccessible(true);
428 // return null; 415 // Set the value into the field. We should never get an exception here because
429 // try { 416 // we know we have a public static non-final field. If we do get an exception, silently
430 // // Check to see if we are allowed to modify the field. If we aren't (for instance 417 // log it and continue. This means that the field will (most likely) be un-initialized and
431 // // if the class is not public) then change the accessible attribute of the field 418 // will fail later in the code and if so then we will see both the NPE and this error.
432 // // before trying to set the value. 419 field.set(null, value);
433 // if (!isAccessible) 420 } catch (Exception e) {
434 // field.setAccessible(true); 421 log(SEVERITY_ERROR, "Exception setting field value.", e); //$NON-NLS-1$
435 // // Set the value into the field. We should never get an exception here because 422 }
436 // // we know we have a public static non-final field. If we do get an exception, silently 423 return null;
437 // // log it and continue. This means that the field will (most likely) be un-initialized and 424 }
438 // // will fail later in the code and if so then we will see both the NPE and this error. 425 }
439 // field.set(null, value);
440 // } catch (Exception e) {
441 // log(SEVERITY_ERROR, "Exception setting field value.", e); //$NON-NLS-1$
442 // }
443 // return null;
444 // }
445 // }
446 } 426 }