Skip to main content
summaryrefslogtreecommitdiffstats
blob: 75b0f11977c03315bc05f93b4b8ff05187b9d601 (plain) (blame)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsf.common.ui.internal.utils;

import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.eclipse.jst.jsf.common.ui.JSFUICommonPlugin;
import org.eclipse.jst.jsf.common.ui.internal.logging.Logger;

/**
 * Give easy access to a resource bundle.
 * 
 * @author mengbo
 */
public abstract class ResourceUtils {
	private static Logger _log = JSFUICommonPlugin.getLogger(ResourceUtils.class);
	/**
	 * the resource bundle accessible by all children
	 */
	protected ResourceBundle _resources;

	/**
	 * Empty Constructor.
	 */
	protected ResourceUtils() {
        // restrict instantiation
	}

	/**
	 * Easy way to set the bundle and get a fatal log messages and an alert if
	 * the resource bundle is not found.
	 * 
	 * @param resource
	 * @param bundleLocation
	 */
	protected void setBundle(ResourceBundle resource, String bundleLocation) {
		_resources = resource;
		if (_resources == null) {
			// log.ResourceUtils=Missing Resource Bundle "{0}".
			_log.error("log.ResourceUtils", bundleLocation);
			// pluginName=Web Application Development Common
			JSFUICommonPlugin.getAlerts().error("pluginName", "log.ResourceUtils",
					bundleLocation);
		}
	}

	/**
	 * Get the property defined in the resource bundle for the given key. This
	 * property should be an integer. If none is defined
	 * (MissingResourceException), return the default.
	 * 
	 * @param key
	 *            the key in the resource bundle.
	 * @param defaultValue
	 *            default int to return if no value is found for the key.
	 * @return the integer value for key or defaultValue if none
	 */
	public int getValue(String key, int defaultValue) {
		String stringValue = getString(key);
		if (stringValue != null) {
			try {
				return Integer.parseInt(stringValue);
			} catch (NumberFormatException ee)// NOPMD
			{
				// the property value maybe an invalid value, the editor should
				// show these to user.
			}
		}
		return defaultValue;
	}

	/**
	 * Get the property defined in the resource bundle for the given key. This
	 * property should be an long. If none is defined
	 * (MissingResourceException), return the default.
	 * 
	 * @param key
	 *            the key in the resource bundle.
	 * @param defaultValue
	 *            default long to return if no value is found for the key.
	 * @return the long value for key or defaultValue if none
	 */
	public long getValue(String key, long defaultValue) {
		String stringValue = getString(key);
		if (stringValue != null) {
			try {
				return Long.parseLong(stringValue);
			} catch (NumberFormatException ee)// NOPMD
			{
				// the property value maybe an invalid value, the editor should
				// show these to user.
			}
		}
		return defaultValue;
	}

	/**
	 * Get the property defined in the resource bundle for the given key. This
	 * property should be boolean ("true" of "false"). If none is defined
	 * (MissingResourceException), return the default.
	 * 
	 * @param key
	 *            the key in the resource bundle.
	 * @return true if there is a resource corresponding to key
	 */
	public boolean isResource(String key) {
		return getString(key).equals("" + true);
	}

	/**
	 * Get the property defined in the resource bundle for the given key. If
	 * none is defined (MissingResourceException), return null.
	 * 
	 * @param key
	 *            the key in the resource bundle.
	 * @return the string value for key or key if not found
	 */
	public String getString(String key) {
		try {
			return _resources.getString(key);
		} catch (MissingResourceException ee) {
			return key;
		}
	}

	/**
	 * Build a formated string from the resource bundle.
	 * 
	 * @param key
	 *            the key into the resource bundle that has the formated string.
	 * @param arg0
	 *            the first argument.
	 * @return the formated string with the argument inline.
	 */
	public String getString(String key, Object arg0) {
		Object[] args = new Object[1];
		args[0] = arg0;

		MessageFormat formatter = new MessageFormat(getString(key));
		return formatter.format(args);
	}

	/**
	 * Build a formated string from the resource bundle.
	 * 
	 * @param key
	 *            the key into the resource bundle that has the formated string.
	 * @param arg0
	 *            the first argument.
	 * @param arg1
	 *            the second argument.
	 * @return the formated string with the argument inline.
	 */
	public String getString(String key, Object arg0, Object arg1) {
		Object[] args = new Object[2];
		args[0] = arg0;
		args[1] = arg1;

		MessageFormat formatter = new MessageFormat(getString(key));
		return formatter.format(args);
	}

	/**
	 * Build a formated string from the resource bundle.
	 * 
	 * @param key
	 *            the key into the resource bundle that has the formated string.
	 * @param arg0
	 *            the first argument.
	 * @param arg1
	 *            the second argument.
	 * @param arg2
	 *            the third argument.
	 * @return the formated string with the argument inline.
	 */
	public String getString(String key, Object arg0, Object arg1, Object arg2) {
		Object[] args = new Object[3];
		args[0] = arg0;
		args[1] = arg1;
		args[2] = arg2;

		MessageFormat formatter = new MessageFormat(getString(key));
		return formatter.format(args);
	}

	/**
	 * Build a formated string from the resource bundle.
	 * 
	 * @param key
	 *            the key into the resource bundle that has the formated string.
	 * @param arg0
	 *            the first argument.
	 * @param arg1
	 *            the second argument.
	 * @param arg2
	 *            the third argument.
	 * @param arg3
	 *            the forth argument.
	 * @return the formated string with the argument inline.
	 */
	public String getString(String key, Object arg0, Object arg1, Object arg2,
			Object arg3) {
		Object[] args = new Object[4];
		args[0] = arg0;
		args[1] = arg1;
		args[2] = arg2;
		args[3] = arg3;

		MessageFormat formatter = new MessageFormat(getString(key));
		return formatter.format(args);
	}

	/**
	 * Build a formated string from the resource bundle.
	 * 
	 * @param key
	 *            the key into the resource bundle that has the formated string.
	 * @param arg0
	 *            the first argument.
	 * @param arg1
	 *            the second argument.
	 * @param arg2
	 *            the third argument.
	 * @param arg3
	 *            the forth argument.
	 * @param arg4
	 *            the forth argument.
	 * @return the formated string with the argument inline.
	 */
	public String getString(String key, Object arg0, Object arg1, Object arg2,
			Object arg3, Object arg4) {
		Object[] args = new Object[5];
		args[0] = arg0;
		args[1] = arg1;
		args[2] = arg2;
		args[3] = arg3;
		args[4] = arg4;

		MessageFormat formatter = new MessageFormat(getString(key));
		return formatter.format(args);
	}

	/**
	 * Build a formated string from the resource bundle.
	 * 
	 * @param key
	 *            the key into the resource bundle that has the formated string.
	 * @param args
	 *            an array of arguments
	 * @return the formated string with the argument inline.
	 */
	public String getString(String key, Object[] args) {
		MessageFormat formatter = new MessageFormat(getString(key));
		return formatter.format(args);
	}

	/**
	 * Use in the try-finally idiom for inputStream to ensure close
	 * and suppress exceptions on close
	 * @param inputStream
	 */
	public static void ensureClosed(InputStream inputStream) {
		if (inputStream != null) {
			try {
				inputStream.close();
			} catch (IOException e) {
				// Ignore
			}
		}

	}
}

Back to the top