| 1 | package be.nikiroo.utils.resources; |
| 2 | |
| 3 | import java.io.BufferedWriter; |
| 4 | import java.io.File; |
| 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; |
| 19 | |
| 20 | /** |
| 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 |
| 23 | * methods. |
| 24 | * |
| 25 | * @author niki |
| 26 | * |
| 27 | * @param <E> |
| 28 | * the enum to use to get values out of this class |
| 29 | */ |
| 30 | public class Bundle<E extends Enum<E>> { |
| 31 | protected Class<E> type; |
| 32 | protected Enum<?> name; |
| 33 | private ResourceBundle map; |
| 34 | |
| 35 | /** |
| 36 | * Create a new {@link Bundles} of the given name. |
| 37 | * |
| 38 | * @param type |
| 39 | * a runtime instance of the class of E |
| 40 | * |
| 41 | * @param name |
| 42 | * the name of the {@link Bundles} |
| 43 | */ |
| 44 | protected Bundle(Class<E> type, Enum<?> name) { |
| 45 | this.type = type; |
| 46 | this.name = name; |
| 47 | setBundle(name, Locale.getDefault()); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Return the value associated to the given id as a {@link String}. |
| 52 | * |
| 53 | * @param mame |
| 54 | * the id of the value to get |
| 55 | * |
| 56 | * @return the associated value, or NULL if not found (not present in the |
| 57 | * resource file) |
| 58 | */ |
| 59 | public String getString(E id) { |
| 60 | return getStringX(id, null); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Return the value associated to the given id as a {@link String} suffixed |
| 65 | * with the runtime value "_suffix" (that is, "_" and suffix). |
| 66 | * |
| 67 | * @param mame |
| 68 | * the id of the value to get |
| 69 | * @param suffix |
| 70 | * the runtime suffix |
| 71 | * |
| 72 | * @return the associated value, or NULL if not found (not present in the |
| 73 | * resource file) |
| 74 | */ |
| 75 | public String getStringX(E id, String suffix) { |
| 76 | String key = id.name() |
| 77 | + (suffix == null ? "" : "_" + suffix.toUpperCase()); |
| 78 | |
| 79 | if (containsKey(key)) { |
| 80 | return getString(key).trim(); |
| 81 | } |
| 82 | |
| 83 | return null; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Return the value associated to the given id as a {@link Boolean}. |
| 88 | * |
| 89 | * @param mame |
| 90 | * the id of the value to get |
| 91 | * |
| 92 | * @return the associated value |
| 93 | */ |
| 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")) |
| 99 | return true; |
| 100 | if (str.equalsIgnoreCase("false") || str.equalsIgnoreCase("off") |
| 101 | || str.equalsIgnoreCase("no")) |
| 102 | return false; |
| 103 | |
| 104 | } |
| 105 | |
| 106 | return null; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Return the value associated to the given id as a {@link boolean}. |
| 111 | * |
| 112 | * @param mame |
| 113 | * the id of the value to get |
| 114 | * @param def |
| 115 | * the default value when it is not present in the config file or |
| 116 | * if it is not a boolean value |
| 117 | * |
| 118 | * @return the associated value |
| 119 | */ |
| 120 | public boolean getBoolean(E id, boolean def) { |
| 121 | Boolean b = getBoolean(id); |
| 122 | if (b != null) |
| 123 | return b; |
| 124 | |
| 125 | return def; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Return the value associated to the given id as an {@link Integer}. |
| 130 | * |
| 131 | * @param mame |
| 132 | * the id of the value to get |
| 133 | * |
| 134 | * @return the associated value |
| 135 | */ |
| 136 | public Integer getInteger(E id) { |
| 137 | try { |
| 138 | return Integer.parseInt(getString(id)); |
| 139 | } catch (Exception e) { |
| 140 | } |
| 141 | |
| 142 | return null; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Return the value associated to the given id as a {@link int}. |
| 147 | * |
| 148 | * @param mame |
| 149 | * the id of the value to get |
| 150 | * @param def |
| 151 | * the default value when it is not present in the config file or |
| 152 | * if it is not a int value |
| 153 | * |
| 154 | * @return the associated value |
| 155 | */ |
| 156 | public int getInteger(E id, int def) { |
| 157 | Integer i = getInteger(id); |
| 158 | if (i != null) |
| 159 | return i; |
| 160 | |
| 161 | return def; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Return the value associated to the given id as a {@link Character}. |
| 166 | * |
| 167 | * @param mame |
| 168 | * the id of the value to get |
| 169 | * |
| 170 | * @return the associated value |
| 171 | */ |
| 172 | public char getChar(E id) { |
| 173 | String s = getString(id).trim(); |
| 174 | if (s.length() > 0) { |
| 175 | return s.charAt(0); |
| 176 | } |
| 177 | |
| 178 | return ' '; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Create/update the .properties file. |
| 183 | * <p> |
| 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). |
| 187 | * |
| 188 | * @param path |
| 189 | * the path where the .properties files are |
| 190 | * |
| 191 | * @throws IOException |
| 192 | * in case of IO errors |
| 193 | */ |
| 194 | public void updateFile(String path) throws IOException { |
| 195 | File file = getUpdateFile(path); |
| 196 | |
| 197 | BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( |
| 198 | new FileOutputStream(file), "UTF-8")); |
| 199 | |
| 200 | writeHeader(writer); |
| 201 | writer.write("\n"); |
| 202 | writer.write("\n"); |
| 203 | |
| 204 | for (Field field : type.getDeclaredFields()) { |
| 205 | Meta meta = field.getAnnotation(Meta.class); |
| 206 | if (meta != null) { |
| 207 | E id = E.valueOf(type, field.getName()); |
| 208 | String info = getMetaInfo(meta); |
| 209 | |
| 210 | if (info != null) { |
| 211 | writer.write(info); |
| 212 | writer.write("\n"); |
| 213 | } |
| 214 | |
| 215 | writeValue(writer, id); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | writer.close(); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Reload the {@link Bundle} data files. |
| 224 | */ |
| 225 | public void reload() { |
| 226 | setBundle(name, null); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Check if the internal map contains the given key. |
| 231 | * |
| 232 | * @param key |
| 233 | * the key to check for |
| 234 | * |
| 235 | * @return true if it does |
| 236 | */ |
| 237 | protected boolean containsKey(String key) { |
| 238 | try { |
| 239 | map.getObject(key); |
| 240 | return true; |
| 241 | } catch (MissingResourceException e) { |
| 242 | return false; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Get the value for the given key if it exists in the internal map, or NULL |
| 248 | * if not. |
| 249 | * |
| 250 | * @param key |
| 251 | * the key to check for |
| 252 | * |
| 253 | * @return the value, or NULL |
| 254 | */ |
| 255 | protected String getString(String key) { |
| 256 | if (containsKey(key)) { |
| 257 | return map.getString(key); |
| 258 | } |
| 259 | |
| 260 | return null; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Return formated, display-able information from the {@link Meta} field |
| 265 | * given. Each line will always starts with a "#" character. |
| 266 | * |
| 267 | * @param meta |
| 268 | * the {@link Meta} field |
| 269 | * |
| 270 | * @return the information to display or NULL if none |
| 271 | */ |
| 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(); |
| 277 | |
| 278 | int opt = what.length() + where.length() + format.length(); |
| 279 | if (opt + info.length() == 0) |
| 280 | return null; |
| 281 | |
| 282 | StringBuilder builder = new StringBuilder(); |
| 283 | builder.append("# "); |
| 284 | |
| 285 | if (opt > 0) { |
| 286 | builder.append("("); |
| 287 | if (what.length() > 0) { |
| 288 | builder.append("WHAT: " + what); |
| 289 | if (where.length() + format.length() > 0) |
| 290 | builder.append(", "); |
| 291 | } |
| 292 | |
| 293 | if (where.length() > 0) { |
| 294 | builder.append("WHERE: " + where); |
| 295 | if (format.length() > 0) |
| 296 | builder.append(", "); |
| 297 | } |
| 298 | |
| 299 | if (format.length() > 0) { |
| 300 | builder.append("FORMAT: " + format); |
| 301 | } |
| 302 | |
| 303 | builder.append(")"); |
| 304 | if (info.length() > 0) { |
| 305 | builder.append("\n# "); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | builder.append(info); |
| 310 | |
| 311 | return builder.toString(); |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * The display name used in the <tt>.properties file</tt>. |
| 316 | * |
| 317 | * @return the name |
| 318 | */ |
| 319 | protected String getBundleDisplayName() { |
| 320 | return name.toString(); |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Write the header found in the configuration <tt>.properties</tt> file of |
| 325 | * this {@link Bundles}. |
| 326 | * |
| 327 | * @param writer |
| 328 | * the {@link Writer} to write the header in |
| 329 | * |
| 330 | * @throws IOException |
| 331 | * in case of IO error |
| 332 | */ |
| 333 | protected void writeHeader(Writer writer) throws IOException { |
| 334 | writer.write("# " + getBundleDisplayName() + "\n"); |
| 335 | writer.write("#\n"); |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Write the given id to the config file, i.e., "MY_ID = my_curent_value" |
| 340 | * followed by a new line |
| 341 | * |
| 342 | * @param writer |
| 343 | * the {@link Writer} to write into |
| 344 | * @param id |
| 345 | * the id to write |
| 346 | * |
| 347 | * @throws IOException |
| 348 | * in case of IO error |
| 349 | */ |
| 350 | protected void writeValue(Writer writer, E id) throws IOException { |
| 351 | writeValue(writer, id.name(), getString(id)); |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Write the given data to the config file, i.e., "MY_ID = my_curent_value" |
| 356 | * followed by a new line |
| 357 | * |
| 358 | * @param writer |
| 359 | * the {@link Writer} to write into |
| 360 | * @param id |
| 361 | * the id to write |
| 362 | * @param value |
| 363 | * the id's value |
| 364 | * |
| 365 | * @throws IOException |
| 366 | * in case of IO error |
| 367 | */ |
| 368 | protected void writeValue(Writer writer, String id, String value) |
| 369 | throws IOException { |
| 370 | writer.write(id); |
| 371 | writer.write(" = "); |
| 372 | |
| 373 | if (value == null) { |
| 374 | value = ""; |
| 375 | } |
| 376 | |
| 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\\"); |
| 382 | } |
| 383 | writer.write("\n"); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Return the source file for this {@link Bundles} from the given path. |
| 389 | * |
| 390 | * @param path |
| 391 | * the path where the .properties files are |
| 392 | * |
| 393 | * @return the source {@link File} |
| 394 | * |
| 395 | * @throws IOException |
| 396 | * in case of IO errors |
| 397 | */ |
| 398 | protected File getUpdateFile(String path) { |
| 399 | return new File(path, name.name() + ".properties"); |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Change the currently used bundle. |
| 404 | * |
| 405 | * @param name |
| 406 | * the name of the bundle to load |
| 407 | * @param locale |
| 408 | * the {@link Locale} to use |
| 409 | */ |
| 410 | protected void setBundle(Enum<?> name, Locale locale) { |
| 411 | map = null; |
| 412 | String dir = Bundles.getDirectory(); |
| 413 | |
| 414 | if (dir != null) { |
| 415 | try { |
| 416 | File file = getPropertyFile(dir, name.name(), locale); |
| 417 | if (file != null) { |
| 418 | Reader reader = new InputStreamReader(new FileInputStream( |
| 419 | file), "UTF8"); |
| 420 | map = new PropertyResourceBundle(reader); |
| 421 | } |
| 422 | } catch (IOException e) { |
| 423 | e.printStackTrace(); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | if (map == null) { |
| 428 | map = ResourceBundle.getBundle(type.getPackage().getName() + "." |
| 429 | + name.name(), locale, new FixedResourceBundleControl()); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * Return the resource file that is closer to the {@link Locale}. |
| 435 | * |
| 436 | * @param dir |
| 437 | * the dirctory to look into |
| 438 | * @param name |
| 439 | * the file basename (without <tt>.properties</tt>) |
| 440 | * @param locale |
| 441 | * the {@link Locale} |
| 442 | * |
| 443 | * @return the closest match or NULL if none |
| 444 | */ |
| 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 |
| 449 | .getCountry(); |
| 450 | String language = locale.getLanguage() == null ? "" : locale |
| 451 | .getLanguage(); |
| 452 | if (!language.isEmpty() && !country.isEmpty()) { |
| 453 | locales.add("_" + language + "-" + country); |
| 454 | } |
| 455 | if (!language.isEmpty()) { |
| 456 | locales.add("_" + language); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | locales.add(""); |
| 461 | |
| 462 | File file = null; |
| 463 | for (String loc : locales) { |
| 464 | file = new File(dir, name + loc + ".properties"); |
| 465 | if (file.exists()) { |
| 466 | break; |
| 467 | } else { |
| 468 | file = null; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | return file; |
| 473 | } |
| 474 | } |