1 package be
.nikiroo
.utils
.resources
;
3 import java
.io
.BufferedWriter
;
5 import java
.io
.FileInputStream
;
6 import java
.io
.FileOutputStream
;
7 import java
.io
.IOException
;
8 import java
.io
.InputStreamReader
;
9 import java
.io
.OutputStreamWriter
;
10 import java
.io
.Reader
;
11 import java
.io
.Writer
;
12 import java
.lang
.reflect
.Field
;
13 import java
.util
.ArrayList
;
14 import java
.util
.List
;
15 import java
.util
.Locale
;
16 import java
.util
.MissingResourceException
;
17 import java
.util
.PropertyResourceBundle
;
18 import java
.util
.ResourceBundle
;
21 * This class encapsulate a {@link ResourceBundle} in UTF-8. It only allows to
22 * retrieve values associated to an enumeration, and allows some additional
28 * the enum to use to get values out of this class
30 public class Bundle
<E
extends Enum
<E
>> {
31 protected Class
<E
> type
;
32 protected Enum
<?
> name
;
33 private ResourceBundle map
;
36 * Create a new {@link Bundles} of the given name.
39 * a runtime instance of the class of E
42 * the name of the {@link Bundles}
44 protected Bundle(Class
<E
> type
, Enum
<?
> name
) {
47 setBundle(name
, Locale
.getDefault());
51 * Return the value associated to the given id as a {@link String}.
54 * the id of the value to get
56 * @return the associated value, or NULL if not found (not present in the
59 public String
getString(E id
) {
60 return getStringX(id
, null);
64 * Return the value associated to the given id as a {@link String} suffixed
65 * with the runtime value "_suffix" (that is, "_" and suffix).
68 * the id of the value to get
72 * @return the associated value, or NULL if not found (not present in the
75 public String
getStringX(E id
, String suffix
) {
76 String key
= id
.name()
77 + (suffix
== null ?
"" : "_" + suffix
.toUpperCase());
79 if (containsKey(key
)) {
80 return getString(key
).trim();
87 * Return the value associated to the given id as a {@link Boolean}.
90 * the id of the value to get
92 * @return the associated value
94 public Boolean
getBoolean(E id
) {
95 String str
= getString(id
);
96 if (str
!= null && str
.length() > 0) {
97 if (str
.equalsIgnoreCase("true") || str
.equalsIgnoreCase("on")
98 || str
.equalsIgnoreCase("yes"))
100 if (str
.equalsIgnoreCase("false") || str
.equalsIgnoreCase("off")
101 || str
.equalsIgnoreCase("no"))
110 * Return the value associated to the given id as a {@link boolean}.
113 * the id of the value to get
115 * the default value when it is not present in the config file or
116 * if it is not a boolean value
118 * @return the associated value
120 public boolean getBoolean(E id
, boolean def
) {
121 Boolean b
= getBoolean(id
);
129 * Return the value associated to the given id as an {@link Integer}.
132 * the id of the value to get
134 * @return the associated value
136 public Integer
getInteger(E id
) {
138 return Integer
.parseInt(getString(id
));
139 } catch (Exception e
) {
146 * Return the value associated to the given id as a {@link int}.
149 * the id of the value to get
151 * the default value when it is not present in the config file or
152 * if it is not a int value
154 * @return the associated value
156 public int getInteger(E id
, int def
) {
157 Integer i
= getInteger(id
);
165 * Return the value associated to the given id as a {@link Character}.
168 * the id of the value to get
170 * @return the associated value
172 public char getChar(E id
) {
173 String s
= getString(id
).trim();
174 if (s
.length() > 0) {
182 * Create/update the .properties file.
184 * Will use the most likely candidate as base if the file does not already
185 * exists and this resource is translatable (for instance, "en_US" will use
186 * "en" as a base if the resource is a translation file).
189 * the path where the .properties files are
191 * @throws IOException
192 * in case of IO errors
194 public void updateFile(String path
) throws IOException
{
195 File file
= getUpdateFile(path
);
197 BufferedWriter writer
= new BufferedWriter(new OutputStreamWriter(
198 new FileOutputStream(file
), "UTF-8"));
204 for (Field field
: type
.getDeclaredFields()) {
205 Meta meta
= field
.getAnnotation(Meta
.class);
207 E id
= E
.valueOf(type
, field
.getName());
208 String info
= getMetaInfo(meta
);
215 writeValue(writer
, id
);
223 * Reload the {@link Bundle} data files.
225 public void reload() {
226 setBundle(name
, null);
230 * Check if the internal map contains the given key.
233 * the key to check for
235 * @return true if it does
237 protected boolean containsKey(String key
) {
241 } catch (MissingResourceException e
) {
247 * Get the value for the given key if it exists in the internal map, or NULL
251 * the key to check for
253 * @return the value, or NULL
255 protected String
getString(String key
) {
256 if (containsKey(key
)) {
257 return map
.getString(key
);
264 * Return formated, display-able information from the {@link Meta} field
265 * given. Each line will always starts with a "#" character.
268 * the {@link Meta} field
270 * @return the information to display or NULL if none
272 protected String
getMetaInfo(Meta meta
) {
273 String what
= meta
.what();
274 String where
= meta
.where();
275 String format
= meta
.format();
276 String info
= meta
.info();
278 int opt
= what
.length() + where
.length() + format
.length();
279 if (opt
+ info
.length() == 0)
282 StringBuilder builder
= new StringBuilder();
283 builder
.append("# ");
287 if (what
.length() > 0) {
288 builder
.append("WHAT: " + what
);
289 if (where
.length() + format
.length() > 0)
290 builder
.append(", ");
293 if (where
.length() > 0) {
294 builder
.append("WHERE: " + where
);
295 if (format
.length() > 0)
296 builder
.append(", ");
299 if (format
.length() > 0) {
300 builder
.append("FORMAT: " + format
);
304 if (info
.length() > 0) {
305 builder
.append("\n# ");
309 builder
.append(info
);
311 return builder
.toString();
315 * The display name used in the <tt>.properties file</tt>.
319 protected String
getBundleDisplayName() {
320 return name
.toString();
324 * Write the header found in the configuration <tt>.properties</tt> file of
325 * this {@link Bundles}.
328 * the {@link Writer} to write the header in
330 * @throws IOException
331 * in case of IO error
333 protected void writeHeader(Writer writer
) throws IOException
{
334 writer
.write("# " + getBundleDisplayName() + "\n");
339 * Write the given id to the config file, i.e., "MY_ID = my_curent_value"
340 * followed by a new line
343 * the {@link Writer} to write into
347 * @throws IOException
348 * in case of IO error
350 protected void writeValue(Writer writer
, E id
) throws IOException
{
351 writeValue(writer
, id
.name(), getString(id
));
355 * Write the given data to the config file, i.e., "MY_ID = my_curent_value"
356 * followed by a new line
359 * the {@link Writer} to write into
365 * @throws IOException
366 * in case of IO error
368 protected void writeValue(Writer writer
, String id
, String value
)
377 String
[] lines
= value
.replaceAll("\t", "\\t").split("\n");
378 for (int i
= 0; i
< lines
.length
; i
++) {
379 writer
.write(lines
[i
]);
380 if (i
< lines
.length
- 1) {
381 writer
.write("\\n\\");
388 * Return the source file for this {@link Bundles} from the given path.
391 * the path where the .properties files are
393 * @return the source {@link File}
395 * @throws IOException
396 * in case of IO errors
398 protected File
getUpdateFile(String path
) {
399 return new File(path
, name
.name() + ".properties");
403 * Change the currently used bundle.
406 * the name of the bundle to load
408 * the {@link Locale} to use
410 protected void setBundle(Enum
<?
> name
, Locale locale
) {
412 String dir
= Bundles
.getDirectory();
416 File file
= getPropertyFile(dir
, name
.name(), locale
);
418 Reader reader
= new InputStreamReader(new FileInputStream(
420 map
= new PropertyResourceBundle(reader
);
422 } catch (IOException e
) {
428 map
= ResourceBundle
.getBundle(type
.getPackage().getName() + "."
429 + name
.name(), locale
, new FixedResourceBundleControl());
434 * Return the resource file that is closer to the {@link Locale}.
437 * the dirctory to look into
439 * the file basename (without <tt>.properties</tt>)
443 * @return the closest match or NULL if none
445 private File
getPropertyFile(String dir
, String name
, Locale locale
) {
446 List
<String
> locales
= new ArrayList
<String
>();
447 if (locale
!= null) {
448 String country
= locale
.getCountry() == null ?
"" : locale
450 String language
= locale
.getLanguage() == null ?
"" : locale
452 if (!language
.isEmpty() && !country
.isEmpty()) {
453 locales
.add("_" + language
+ "-" + country
);
455 if (!language
.isEmpty()) {
456 locales
.add("_" + language
);
463 for (String loc
: locales
) {
464 file
= new File(dir
, name
+ loc
+ ".properties");