1 package net.argius.stew;
6 import java.util.concurrent.*;
9 * ResourceManager provides a function like a ResourceBundle used UTF-8 instead Unicode escapes.
11 public final class ResourceManager {
13 public static final ResourceManager Default = ResourceManager.getInstance(ResourceManager0.class);
15 private List<Map<String, String>> list;
17 private ResourceManager(List<Map<String, String>> list) {
22 * Creates an instance.
24 * It used as a bundle name.
25 * If String, it will use as a bundle name directly.
26 * Else if Package, it will use its package name + "messages".
27 * Otherwise, it will use as its FQCN.
30 public static ResourceManager getInstance(Object o) {
31 Locale loc = Locale.getDefault();
32 String[] suffixes = {"_" + loc, "_" + loc.getLanguage(), ""};
33 List<Map<String, String>> a = new ArrayList<Map<String, String>>();
34 for (final String name : getResourceNames(o)) {
35 for (final String suffix : suffixes) {
36 final String key = name + suffix;
37 Map<String, String> m = ResourceManager0.map.get(key);
39 m = loadResource(key, "u8p", "utf-8");
43 ResourceManager0.map.putIfAbsent(key, m);
48 return new ResourceManager(a);
51 private static Set<String> getResourceNames(Object o) {
52 Set<String> set = new LinkedHashSet<String>();
55 if (o instanceof String) {
57 } else if (o instanceof Package) {
58 pn = ((Package)o).getName();
59 } else if (o != null) {
60 final Class<?> c = (o instanceof Class) ? (Class<?>)o : o.getClass();
62 pn = c.getPackage().getName();
68 set.add(pn + ".messages");
70 set.add(ResourceManager0.getPackageName() + ".messages");
74 private static Map<String, String> loadResource(String name, String extension, String encname) {
75 final String path = "/" + name.replace('.', '/') + '.' + extension;
76 InputStream is = ResourceManager0.getResourceAsStream(path);
80 List<String> lines = new ArrayList<String>();
81 Scanner r = new Scanner(is, encname);
83 StringBuilder buffer = new StringBuilder();
84 while (r.hasNextLine()) {
85 final String s = r.nextLine();
86 if (s.matches("^\\s*#.*")) {
89 buffer.append(s.replace("\\t", "\t").replace("\\n", "\n").replace("\\=", "="));
90 if (s.endsWith("\\")) {
91 buffer.setLength(buffer.length() - 1);
94 lines.add(buffer.toString());
97 if (buffer.length() > 0) {
98 lines.add(buffer.toString());
103 Map<String, String> m = new HashMap<String, String>();
104 for (final String s : lines) {
105 if (s.contains("=")) {
106 String[] a = s.split("=", 2);
107 m.put(a[0].trim(), a[1].trim().replaceFirst("\\\\$", " ").replace("\\ ", " "));
116 * @param path resource's path
117 * @param defaultValue defalut value if a resource not found
120 public String read(String path, String defaultValue) {
121 InputStream in = ResourceManager0.getResourceAsStream(path);
125 StringBuilder buffer = new StringBuilder();
126 Scanner r = new Scanner(in);
128 if (r.hasNextLine()) {
129 buffer.append(r.nextLine());
131 while (r.hasNextLine()) {
132 buffer.append(String.format("%n"));
133 buffer.append(r.nextLine());
138 return buffer.toString();
141 private String s(String key) {
142 for (final Map<String, String> m : this.list) {
143 final String s = m.get(key);
152 * Returns true if this resource contains a value specified by key.
156 public boolean containsKey(String key) {
157 for (final Map<String, String> m : this.list) {
158 if (m.containsKey(key)) {
166 * Returns the value specified by key as a String.
171 public String get(String key, Object... args) {
172 final String s = s(key);
173 return (s.length() == 0) ? key : MessageFormat.format(s, args);
177 * Returns the value specified by key as a boolean.
181 public boolean isTrue(String key) {
182 return s(key).matches("(?i)true|on|yes");
186 * Returns the (initial char) value specified by key as a char.
190 public char getChar(String key) {
191 final String s = s(key);
192 return (s.length() == 0) ? ' ' : s.charAt(0);
196 * Returns the value specified by key as a int.
200 public int getInt(String key) {
201 return getInt(key, 0);
205 * Returns the value specified by key as a int.
207 * @param defaultValue
210 public int getInt(String key, int defaultValue) {
211 final String s = s(key);
213 return Integer.parseInt(s);
214 } catch (NumberFormatException ex) {
222 class ResourceManager0 {
224 static final ConcurrentHashMap<String, Map<String, String>> map = new ConcurrentHashMap<String, Map<String, String>>();
226 static String getPackageName() {
227 return ResourceManager0.class.getPackage().getName();
230 static InputStream getResourceAsStream(String path) {
231 return ResourceManager0.class.getResourceAsStream(path);