1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
/**********************************************************************
* Copyright (c) 2005 IBM Corporation and others. All rights reserved. This
* program and the accompanying materials are made available under the terms of
* the Common Public License v1.0 which accompanies this distribution, and is
* available at http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM - Initial API and implementation
**********************************************************************/
package org.eclipse.osgi.util;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import org.eclipse.osgi.framework.internal.core.MessageResourceBundle;
import org.eclipse.osgi.framework.msg.MessageFormat;
/**
* Common superclass for all message bundle classes. Provides convenience
* methods for manipulating messages.
*
* @since 3.1
*/
public abstract class NLS {
public static boolean DEBUG_MESSAGE_BUNDLES = false;
/**
* Creates a new NLS instance.
*/
protected NLS() {
super();
}
/**
* Bind the given message's substitution locations with the given string values.
*
* @param message the message to be manipulated
* @param binding the object to be inserted into the message
* @return the manipulated String
*/
public static String bind(String message, Object binding) {
return bind(message, new Object[] {binding});
}
/**
* Bind the given message's substitution locations with the given string values.
*
* @param message the message to be manipulated
* @param binding1 An object to be inserted into the message
* @param binding2 A second object to be inserted into the message
* @return the manipulated String
*/
public static String bind(String message, Object binding1, Object binding2) {
return bind(message, new Object[] {binding1, binding2});
}
/**
* Bind the given message's substitution locations with the given string values.
*
* @param message the message to be manipulated
* @param bindings[] An array of objects to be inserted into the message
* @return the manipulated String
*/
public static String bind(String message, Object[] bindings) {
if (message == null)
return "No message available"; //$NON-NLS-1$
if (bindings == null)
return message;
return MessageFormat.format(message, bindings);
}
/**
* Initialize the given class with the values from the specified message bundle.
* <p>
* Note this is interim API and may change before the 3.1 release.
* </p>
*
* @param bundleName fully qualified path of the class name
* @param clazz the class where the constants will exist
*/
public static void initializeMessages(String bundleName, Class clazz) {
long start = System.currentTimeMillis();
// load the resource bundle and set the fields
final Field[] fields = clazz.getDeclaredFields();
MessageResourceBundle.load(bundleName, clazz.getClassLoader(), fields);
// iterate over the fields in the class to make sure that there aren't any empty ones
final int MOD_EXPECTED = Modifier.PUBLIC | Modifier.STATIC;
final int MOD_MASK = MOD_EXPECTED | Modifier.FINAL;
final int numFields = fields.length;
for (int i = 0; i < numFields; i++) {
Field field = fields[i];
if ((field.getModifiers() & MOD_MASK) != MOD_EXPECTED)
continue;
try {
// Set the value into the field if its empty. We should never get an exception here because
// we know we have a public static non-final field. If we do get an exception, silently
// log it and continue. This means that the field will (most likely) be un-initialized and
// will fail later in the code and if so then we will see both the NPE and this error.
if (field.get(clazz) == null) {
String value = "Missing message: " + field.getName() + " in: " + bundleName; //$NON-NLS-1$ //$NON-NLS-2$
if (DEBUG_MESSAGE_BUNDLES)
System.out.println(value);
field.set(null, value);
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (DEBUG_MESSAGE_BUNDLES)
System.out.println("Time to load message bundle: " + bundleName + " was " + (System.currentTimeMillis() - start) + "ms."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
}
|