1 package be
.nikiroo
.utils
.resources
;
4 import java
.io
.BufferedWriter
;
6 import java
.io
.FileInputStream
;
7 import java
.io
.FileOutputStream
;
8 import java
.io
.IOException
;
9 import java
.io
.InputStreamReader
;
10 import java
.io
.OutputStreamWriter
;
11 import java
.io
.Reader
;
12 import java
.io
.Writer
;
13 import java
.lang
.reflect
.Field
;
14 import java
.util
.ArrayList
;
15 import java
.util
.List
;
16 import java
.util
.Locale
;
17 import java
.util
.MissingResourceException
;
18 import java
.util
.PropertyResourceBundle
;
19 import java
.util
.ResourceBundle
;
22 * This class encapsulate a {@link ResourceBundle} in UTF-8. It only allows to
23 * retrieve values associated to an enumeration, and allows some additional
29 * the enum to use to get values out of this class
31 public class Bundle
<E
extends Enum
<E
>> {
32 protected Class
<E
> type
;
33 protected Enum
<?
> name
;
34 private ResourceBundle map
;
37 * Create a new {@link Bundles} of the given name.
40 * a runtime instance of the class of E
43 * the name of the {@link Bundles}
45 protected Bundle(Class
<E
> type
, Enum
<?
> name
) {
48 setBundle(name
, Locale
.getDefault());
52 * Return the value associated to the given id as a {@link String}.
55 * the id of the value to get
57 * @return the associated value, or NULL if not found (not present in the
60 public String
getString(E id
) {
61 return getStringX(id
, null);
65 * Return the value associated to the given id as a {@link String} suffixed
66 * with the runtime value "_suffix" (that is, "_" and suffix).
69 * the id of the value to get
73 * @return the associated value, or NULL if not found (not present in the
76 public String
getStringX(E id
, String suffix
) {
77 String key
= id
.name()
78 + (suffix
== null ?
"" : "_" + suffix
.toUpperCase());
80 if (containsKey(key
)) {
81 return getString(key
).trim();
88 * Return the value associated to the given id as a {@link Boolean}.
91 * the id of the value to get
93 * @return the associated value
95 public Boolean
getBoolean(E id
) {
96 String str
= getString(id
);
97 if (str
!= null && str
.length() > 0) {
98 if (str
.equalsIgnoreCase("true") || str
.equalsIgnoreCase("on")
99 || str
.equalsIgnoreCase("yes"))
101 if (str
.equalsIgnoreCase("false") || str
.equalsIgnoreCase("off")
102 || str
.equalsIgnoreCase("no"))
111 * Return the value associated to the given id as a {@link boolean}.
114 * the id of the value to get
116 * the default value when it is not present in the config file or
117 * if it is not a boolean value
119 * @return the associated value
121 public boolean getBoolean(E id
, boolean def
) {
122 Boolean b
= getBoolean(id
);
130 * Return the value associated to the given id as an {@link Integer}.
133 * the id of the value to get
135 * @return the associated value
137 public Integer
getInteger(E id
) {
139 return Integer
.parseInt(getString(id
));
140 } catch (Exception e
) {
147 * Return the value associated to the given id as a {@link int}.
150 * the id of the value to get
152 * the default value when it is not present in the config file or
153 * if it is not a int value
155 * @return the associated value
157 public int getInteger(E id
, int def
) {
158 Integer i
= getInteger(id
);
166 * Return the value associated to the given id as a {@link Character}.
169 * the id of the value to get
171 * @return the associated value
173 public char getChar(E id
) {
174 String s
= getString(id
).trim();
175 if (s
.length() > 0) {
183 * Return the value associated to the given id as a {@link Color}.
186 * id of the value to get
188 * @return the associated value
190 public Color
getColor(E id
) {
193 String bg
= getString(id
).trim();
194 if (bg
.startsWith("#") && bg
.length() == 7) {
196 color
= new Color(Integer
.parseInt(bg
.substring(1, 3), 16),
197 Integer
.parseInt(bg
.substring(3, 5), 16),
198 Integer
.parseInt(bg
.substring(5, 7), 16));
199 } catch (NumberFormatException e
) {
200 color
= null; // no changes
208 * Create/update the .properties file.
210 * Will use the most likely candidate as base if the file does not already
211 * exists and this resource is translatable (for instance, "en_US" will use
212 * "en" as a base if the resource is a translation file).
215 * the path where the .properties files are
217 * @throws IOException
218 * in case of IO errors
220 public void updateFile(String path
) throws IOException
{
221 File file
= getUpdateFile(path
);
223 BufferedWriter writer
= new BufferedWriter(new OutputStreamWriter(
224 new FileOutputStream(file
), "UTF-8"));
230 for (Field field
: type
.getDeclaredFields()) {
231 Meta meta
= field
.getAnnotation(Meta
.class);
233 E id
= E
.valueOf(type
, field
.getName());
234 String info
= getMetaInfo(meta
);
241 writeValue(writer
, id
);
249 * Reload the {@link Bundle} data files.
251 public void reload() {
252 setBundle(name
, null);
256 * Check if the internal map contains the given key.
259 * the key to check for
261 * @return true if it does
263 protected boolean containsKey(String key
) {
267 } catch (MissingResourceException e
) {
273 * Get the value for the given key if it exists in the internal map, or NULL
277 * the key to check for
279 * @return the value, or NULL
281 protected String
getString(String key
) {
282 if (containsKey(key
)) {
283 return map
.getString(key
);
290 * Return formated, display-able information from the {@link Meta} field
291 * given. Each line will always starts with a "#" character.
294 * the {@link Meta} field
296 * @return the information to display or NULL if none
298 protected String
getMetaInfo(Meta meta
) {
299 String what
= meta
.what();
300 String where
= meta
.where();
301 String format
= meta
.format();
302 String info
= meta
.info();
304 int opt
= what
.length() + where
.length() + format
.length();
305 if (opt
+ info
.length() == 0)
308 StringBuilder builder
= new StringBuilder();
309 builder
.append("# ");
313 if (what
.length() > 0) {
314 builder
.append("WHAT: " + what
);
315 if (where
.length() + format
.length() > 0)
316 builder
.append(", ");
319 if (where
.length() > 0) {
320 builder
.append("WHERE: " + where
);
321 if (format
.length() > 0)
322 builder
.append(", ");
325 if (format
.length() > 0) {
326 builder
.append("FORMAT: " + format
);
330 if (info
.length() > 0) {
331 builder
.append("\n# ");
335 builder
.append(info
);
337 return builder
.toString();
341 * The display name used in the <tt>.properties file</tt>.
345 protected String
getBundleDisplayName() {
346 return name
.toString();
350 * Write the header found in the configuration <tt>.properties</tt> file of
351 * this {@link Bundles}.
354 * the {@link Writer} to write the header in
356 * @throws IOException
357 * in case of IO error
359 protected void writeHeader(Writer writer
) throws IOException
{
360 writer
.write("# " + getBundleDisplayName() + "\n");
365 * Write the given id to the config file, i.e., "MY_ID = my_curent_value"
366 * followed by a new line
369 * the {@link Writer} to write into
373 * @throws IOException
374 * in case of IO error
376 protected void writeValue(Writer writer
, E id
) throws IOException
{
377 writeValue(writer
, id
.name(), getString(id
));
381 * Write the given data to the config file, i.e., "MY_ID = my_curent_value"
382 * followed by a new line
385 * the {@link Writer} to write into
391 * @throws IOException
392 * in case of IO error
394 protected void writeValue(Writer writer
, String id
, String value
)
403 String
[] lines
= value
.replaceAll("\t", "\\t").split("\n");
404 for (int i
= 0; i
< lines
.length
; i
++) {
405 writer
.write(lines
[i
]);
406 if (i
< lines
.length
- 1) {
407 writer
.write("\\n\\");
414 * Return the source file for this {@link Bundles} from the given path.
417 * the path where the .properties files are
419 * @return the source {@link File}
421 * @throws IOException
422 * in case of IO errors
424 protected File
getUpdateFile(String path
) {
425 return new File(path
, name
.name() + ".properties");
429 * Change the currently used bundle.
432 * the name of the bundle to load
434 * the {@link Locale} to use
436 protected void setBundle(Enum
<?
> name
, Locale locale
) {
438 String dir
= Bundles
.getDirectory();
442 File file
= getPropertyFile(dir
, name
.name(), locale
);
444 Reader reader
= new InputStreamReader(new FileInputStream(
446 map
= new PropertyResourceBundle(reader
);
448 } catch (IOException e
) {
454 map
= ResourceBundle
.getBundle(type
.getPackage().getName() + "."
455 + name
.name(), locale
, new FixedResourceBundleControl());
460 * Return the resource file that is closer to the {@link Locale}.
463 * the dirctory to look into
465 * the file basename (without <tt>.properties</tt>)
469 * @return the closest match or NULL if none
471 private File
getPropertyFile(String dir
, String name
, Locale locale
) {
472 List
<String
> locales
= new ArrayList
<String
>();
473 if (locale
!= null) {
474 String country
= locale
.getCountry() == null ?
"" : locale
476 String language
= locale
.getLanguage() == null ?
"" : locale
478 if (!language
.isEmpty() && !country
.isEmpty()) {
479 locales
.add("_" + language
+ "-" + country
);
481 if (!language
.isEmpty()) {
482 locales
.add("_" + language
);
489 for (String loc
: locales
) {
490 file
= new File(dir
, name
+ loc
+ ".properties");