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
, "");
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
.isEmpty()) ?
"" : "_"
78 + 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 * Create/update the .properties file. Will use the most likely candidate as
184 * base if the file does not already exists and this resource is
185 * translatable (for instance, "en_US" will use "en" as a base if the
186 * 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 * Check if the internal map contains the given key.
226 * the key to check for
228 * @return true if it does
230 protected boolean containsKey(String key
) {
234 } catch (MissingResourceException e
) {
240 * Get the value for the given key if it exists in the internal map, or NULL
244 * the key to check for
246 * @return the value, or NULL
248 protected String
getString(String key
) {
249 if (containsKey(key
)) {
250 return map
.getString(key
);
257 * Return formated, display-able information from the {@link Meta} field
258 * given. Each line will always starts with a "#" character.
261 * the {@link Meta} field
263 * @return the information to display or NULL if none
265 protected String
getMetaInfo(Meta meta
) {
266 String what
= meta
.what();
267 String where
= meta
.where();
268 String format
= meta
.format();
269 String info
= meta
.info();
271 int opt
= what
.length() + where
.length() + format
.length();
272 if (opt
+ info
.length() == 0)
275 StringBuilder builder
= new StringBuilder();
276 builder
.append("# ");
280 if (what
.length() > 0) {
281 builder
.append("WHAT: " + what
);
282 if (where
.length() + format
.length() > 0)
283 builder
.append(", ");
286 if (where
.length() > 0) {
287 builder
.append("WHERE: " + where
);
288 if (format
.length() > 0)
289 builder
.append(", ");
292 if (format
.length() > 0) {
293 builder
.append("FORMAT: " + format
);
297 if (info
.length() > 0) {
298 builder
.append("\n# ");
302 builder
.append(info
);
304 return builder
.toString();
308 * The display name used in the <tt>.properties file</tt>.
312 protected String
getBundleDisplayName() {
313 return name
.toString();
317 * Write the header found in the configuration <tt>.properties</tt> file of
318 * this {@link Bundles}.
321 * the {@link Writer} to write the header in
323 * @throws IOException
324 * in case of IO error
326 protected void writeHeader(Writer writer
) throws IOException
{
327 writer
.write("# " + getBundleDisplayName() + "\n");
332 * Write the given id to the config file, i.e., "MY_ID = my_curent_value"
333 * followed by a new line
336 * the {@link Writer} to write into
340 * @throws IOException
341 * in case of IO error
343 protected void writeValue(Writer writer
, E id
) throws IOException
{
344 writeValue(writer
, id
.name(), getString(id
));
348 * Write the given data to the config file, i.e., "MY_ID = my_curent_value"
349 * followed by a new line
352 * the {@link Writer} to write into
358 * @throws IOException
359 * in case of IO error
361 protected void writeValue(Writer writer
, String id
, String value
)
370 String
[] lines
= value
.replaceAll("\\\t", "\\\\\\t").split("\n");
371 for (int i
= 0; i
< lines
.length
; i
++) {
372 writer
.write(lines
[i
]);
373 if (i
< lines
.length
- 1) {
374 writer
.write("\\n\\");
381 * Return the source file for this {@link Bundles} from the given path.
384 * the path where the .properties files are
386 * @return the source {@link File}
388 * @throws IOException
389 * in case of IO errors
391 protected File
getUpdateFile(String path
) {
392 return new File(path
, name
.name() + ".properties");
396 * Change the currently used bundle.
399 * the name of the bundle to load
401 * the {@link Locale} to use
403 protected void setBundle(Enum
<?
> name
, Locale locale
) {
405 String dir
= Bundles
.getDirectory();
409 File file
= getPropertyFile(dir
, name
.name(), locale
);
411 Reader reader
= new InputStreamReader(new FileInputStream(
413 map
= new PropertyResourceBundle(reader
);
415 } catch (IOException e
) {
421 map
= ResourceBundle
.getBundle(type
.getPackage().getName() + "."
422 + name
.name(), locale
, new FixedResourceBundleControl());
427 * Return the resource file that is closer to the {@link Locale}.
430 * the dirctory to look into
432 * the file basename (without <tt>.properties</tt>)
436 * @return the closest match or NULL if none
438 private File
getPropertyFile(String dir
, String name
, Locale locale
) {
439 List
<String
> locales
= new ArrayList
<String
>();
440 if (locale
!= null) {
441 String country
= locale
.getCountry() == null ?
"" : locale
443 String language
= locale
.getLanguage() == null ?
"" : locale
445 if (!language
.isEmpty() && !country
.isEmpty()) {
446 locales
.add("_" + language
+ "-" + country
);
448 if (!language
.isEmpty()) {
449 locales
.add("_" + language
);
456 for (String loc
: locales
) {
457 file
= new File(dir
, name
+ loc
+ ".properties");