KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > util > Messages


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.compiler.util;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.lang.reflect.Field JavaDoc;
16 import java.lang.reflect.Modifier JavaDoc;
17 import java.text.MessageFormat JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Locale JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 public final class Messages {
25     private static class MessagesProperties extends Properties JavaDoc {
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 JavaDoc fields;
32
33         public MessagesProperties(Field JavaDoc[] fieldArray, String JavaDoc bundleName) {
34             super();
35             final int len = fieldArray.length;
36             fields = new HashMap JavaDoc(len * 2);
37             for (int i = 0; i < len; i++) {
38                 fields.put(fieldArray[i].getName(), fieldArray[i]);
39             }
40         }
41
42         /* (non-Javadoc)
43          * @see java.util.Hashtable#put(java.lang.Object, java.lang.Object)
44          */

45         public synchronized Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
46             try {
47                 Field JavaDoc field = (Field JavaDoc) fields.get(key);
48                 if (field == null) {
49                     return null;
50                 }
51                 //can only set value of public static non-final fields
52
if ((field.getModifiers() & MOD_MASK) != MOD_EXPECTED)
53                     return null;
54                 // Set the value into the field. We should never get an exception here because
55
// we know we have a public static non-final field. If we do get an exception, silently
56
// log it and continue. This means that the field will (most likely) be un-initialized and
57
// will fail later in the code and if so then we will see both the NPE and this error.
58
try {
59                     field.set(null, value);
60                 } catch (Exception JavaDoc e) {
61                     // ignore
62
}
63             } catch (SecurityException JavaDoc e) {
64                 // ignore
65
}
66             return null;
67         }
68     }
69
70     
71     private static String JavaDoc[] nlSuffixes;
72     private static final String JavaDoc EXTENSION = ".properties"; //$NON-NLS-1$
73

74     private static final String JavaDoc BUNDLE_NAME = "org.eclipse.jdt.internal.compiler.messages";//$NON-NLS-1$
75

76     private Messages() {
77         // Do not instantiate
78
}
79
80     public static String JavaDoc compilation_unresolvedProblem;
81     public static String JavaDoc compilation_unresolvedProblems;
82     public static String JavaDoc compilation_request;
83     public static String JavaDoc compilation_loadBinary;
84     public static String JavaDoc compilation_process;
85     public static String JavaDoc compilation_write;
86     public static String JavaDoc compilation_done;
87     public static String JavaDoc compilation_units;
88     public static String JavaDoc compilation_unit;
89     public static String JavaDoc compilation_internalError;
90     public static String JavaDoc output_isFile;
91     public static String JavaDoc output_notValidAll;
92     public static String JavaDoc output_notValid;
93     public static String JavaDoc problem_noSourceInformation;
94     public static String JavaDoc problem_atLine;
95     public static String JavaDoc abort_invalidAttribute;
96     public static String JavaDoc abort_invalidExceptionAttribute;
97     public static String JavaDoc abort_missingCode;
98     public static String JavaDoc abort_againstSourceModel;
99     public static String JavaDoc accept_cannot;
100     public static String JavaDoc parser_incorrectPath;
101     public static String JavaDoc parser_moveFiles;
102     public static String JavaDoc parser_syntaxRecovery;
103     public static String JavaDoc parser_regularParse;
104     public static String JavaDoc parser_missingFile;
105     public static String JavaDoc parser_corruptedFile;
106     public static String JavaDoc parser_endOfFile;
107     public static String JavaDoc parser_endOfConstructor;
108     public static String JavaDoc parser_endOfMethod;
109     public static String JavaDoc parser_endOfInitializer;
110     public static String JavaDoc ast_missingCode;
111     public static String JavaDoc constant_cannotCastedInto;
112     public static String JavaDoc constant_cannotConvertedTo;
113
114     static {
115         initializeMessages(BUNDLE_NAME, Messages.class);
116     }
117     
118     /**
119      * Bind the given message's substitution locations with the given string values.
120      *
121      * @param message the message to be manipulated
122      * @return the manipulated String
123      */

124     public static String JavaDoc bind(String JavaDoc message) {
125         return bind(message, null);
126     }
127     
128     /**
129      * Bind the given message's substitution locations with the given string values.
130      *
131      * @param message the message to be manipulated
132      * @param binding the object to be inserted into the message
133      * @return the manipulated String
134      */

135     public static String JavaDoc bind(String JavaDoc message, Object JavaDoc binding) {
136         return bind(message, new Object JavaDoc[] {binding});
137     }
138
139     /**
140      * Bind the given message's substitution locations with the given string values.
141      *
142      * @param message the message to be manipulated
143      * @param binding1 An object to be inserted into the message
144      * @param binding2 A second object to be inserted into the message
145      * @return the manipulated String
146      */

147     public static String JavaDoc bind(String JavaDoc message, Object JavaDoc binding1, Object JavaDoc binding2) {
148         return bind(message, new Object JavaDoc[] {binding1, binding2});
149     }
150
151     /**
152      * Bind the given message's substitution locations with the given string values.
153      *
154      * @param message the message to be manipulated
155      * @param bindings An array of objects to be inserted into the message
156      * @return the manipulated String
157      */

158     public static String JavaDoc bind(String JavaDoc message, Object JavaDoc[] bindings) {
159         return MessageFormat.format(message, bindings);
160     }
161     
162     /*
163      * Build an array of directories to search
164      */

165     private static String JavaDoc[] buildVariants(String JavaDoc root) {
166         if (nlSuffixes == null) {
167             //build list of suffixes for loading resource bundles
168
String JavaDoc nl = Locale.getDefault().toString();
169             ArrayList JavaDoc result = new ArrayList JavaDoc(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             //add the empty suffix last (most general)
179
result.add(EXTENSION);
180             nlSuffixes = (String JavaDoc[]) result.toArray(new String JavaDoc[result.size()]);
181         }
182         root = root.replace('.', '/');
183         String JavaDoc[] variants = new String JavaDoc[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 JavaDoc bundleName, Class JavaDoc clazz) {
189         // load the resource bundle and set the fields
190
final Field JavaDoc[] fields = clazz.getDeclaredFields();
191         load(bundleName, clazz.getClassLoader(), fields);
192
193         // iterate over the fields in the class to make sure that there aren't any empty ones
194
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 JavaDoc field = fields[i];
199             if ((field.getModifiers() & MOD_MASK) != MOD_EXPECTED)
200                 continue;
201             try {
202                 // Set the value into the field if its empty. We should never get an exception here because
203
// we know we have a public static non-final field. If we do get an exception, silently
204
// log it and continue. This means that the field will (most likely) be un-initialized and
205
// will fail later in the code and if so then we will see both the NPE and this error.
206
if (field.get(clazz) == null) {
207                     String JavaDoc value = "Missing message: " + field.getName() + " in: " + bundleName; //$NON-NLS-1$ //$NON-NLS-2$
208
field.set(null, value);
209                 }
210             } catch (IllegalArgumentException JavaDoc e) {
211                 // ignore
212
} catch (IllegalAccessException JavaDoc e) {
213                 // ignore
214
}
215         }
216     }
217     /**
218      * Load the given resource bundle using the specified class loader.
219      */

220     public static void load(final String JavaDoc bundleName, final ClassLoader JavaDoc loader, final Field JavaDoc[] fields) {
221         final String JavaDoc[] variants = buildVariants(bundleName);
222         // search the dirs in reverse order so the cascading defaults is set correctly
223
for (int i = variants.length; --i >= 0;) {
224             InputStream JavaDoc 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 JavaDoc e) {
232                 // ignore
233
} finally {
234                 try {
235                     input.close();
236                 } catch (IOException JavaDoc e) {
237                     // ignore
238
}
239             }
240         }
241     }
242 }
243
Popular Tags