1
2
3
4
5
6
7
8
9
10
11
12 package org.hyphenType.util;
13
14 import java.util.Enumeration;
15 import java.util.Locale;
16 import java.util.MissingResourceException;
17 import java.util.PropertyResourceBundle;
18 import java.util.ResourceBundle;
19
20 import org.hyphenType.datastructure.Options;
21 import org.hyphenType.datastructure.annotations.ArgumentsObject;
22 import org.hyphenType.debug.HTLogger;
23 import org.hyphenType.documentation.OptionListResourceBundle;
24 import org.hyphenType.util.resourcebundles.AliasResourceBundle;
25 import org.hyphenType.util.resourcebundles.GenericEncodingControl;
26
27
28
29
30 public class I18NResourceBundle extends ResourceBundle {
31
32 private ResourceBundle rb;
33
34 public I18NResourceBundle(Class<? extends Options<?>> clazz) {
35 String bundlePath = "";
36
37 ArgumentsObject ao = (ArgumentsObject) clazz.getAnnotation(ArgumentsObject.class);
38
39 if (ao == null || ao.resourceBundlesLocation().equals(""))
40 bundlePath = clazz.getName();
41 else
42 bundlePath = ao.resourceBundlesLocation();
43
44 try {
45 HTLogger.log("Trying to read " + bundlePath + "...");
46 ResourceBundle result;
47 if (ao == null)
48 result = PropertyResourceBundle.getBundle(bundlePath, Locale.getDefault(), I18NResourceBundle.class.getClassLoader(), new GenericEncodingControl(ArgumentsObject.DEFAULT_RB_ENCODING));
49 else
50 result = PropertyResourceBundle.getBundle(bundlePath, Locale.getDefault(), I18NResourceBundle.class.getClassLoader(), new GenericEncodingControl(ao.resourceBundlesEncoding()));
51 rb = AliasResourceBundle.convert(result);
52 } catch (MissingResourceException e) {
53 HTLogger.log(e);
54 rb = new OptionListResourceBundle(clazz);
55 }
56 if(HTLogger.debugMode()) {
57 for(String key :rb.keySet()) {
58 HTLogger.log(String.format("%s = %s", key, rb.getString(key)));
59 }
60 }
61 }
62
63 @Override
64 public Enumeration<String> getKeys() {
65 return rb.getKeys();
66 }
67
68 @Override
69 protected Object handleGetObject(String key) {
70 return rb.getObject(key);
71 }
72 }