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.containsKey(key))
38 ? ResourceManager0.map.get(key)
39 : loadResource(key, "u8p", "utf-8");
41 ResourceManager0.map.put(key, m);
46 return new ResourceManager(a);
49 private static Set<String> getResourceNames(Object o) {
50 Set<String> set = new LinkedHashSet<String>();
53 if (o instanceof String) {
55 } else if (o instanceof Package) {
56 pn = ((Package)o).getName();
57 } else if (o != null) {
58 final Class<?> c = (o instanceof Class) ? (Class<?>)o : o.getClass();
60 pn = c.getPackage().getName();
66 set.add(pn + ".messages");
68 set.add(ResourceManager0.getPackageName() + ".messages");
72 private static Map<String, String> loadResource(String name, String extension, String encname) {
73 final String path = "/" + name.replace('.', '/') + '.' + extension;
74 InputStream is = ResourceManager0.getResourceAsStream(path);
78 List<String> lines = new ArrayList<String>();
79 Scanner r = new Scanner(is, encname);
81 StringBuilder buffer = new StringBuilder();
82 while (r.hasNextLine()) {
83 final String s = r.nextLine();
84 if (s.matches("^\\s*#.*")) {
87 buffer.append(s.replace("\\t", "\t").replace("\\n", "\n").replace("\\=", "="));
88 if (s.endsWith("\\")) {
89 buffer.setLength(buffer.length() - 1);
92 lines.add(buffer.toString());
95 if (buffer.length() > 0) {
96 lines.add(buffer.toString());
101 Map<String, String> m = new HashMap<String, String>();
102 for (final String s : lines) {
103 if (s.contains("=")) {
104 String[] a = s.split("=", 2);
105 m.put(a[0].trim(), a[1].trim().replaceFirst("\\\\$", " ").replace("\\ ", " "));
114 * @param path resource's path
115 * @param defaultValue defalut value if a resource not found
118 public String read(String path, String defaultValue) {
119 InputStream in = ResourceManager0.getResourceAsStream(path);
123 StringBuilder buffer = new StringBuilder();
124 Scanner r = new Scanner(in);
126 if (r.hasNextLine()) {
127 buffer.append(r.nextLine());
129 while (r.hasNextLine()) {
130 buffer.append(String.format("%n"));
131 buffer.append(r.nextLine());
136 return buffer.toString();
139 private String s(String key) {
140 for (final Map<String, String> m : this.list) {
141 final String s = m.get(key);
150 * Returns true if this resource contains a value specified by key.
154 public boolean containsKey(String key) {
155 for (final Map<String, String> m : this.list) {
156 if (m.containsKey(key)) {
164 * Returns the value specified by key as a String.
169 public String get(String key, Object... args) {
170 final String s = s(key);
171 return (s.length() == 0) ? key : MessageFormat.format(s, args);
175 * Returns the value specified by key as a boolean.
179 public boolean isTrue(String key) {
180 return s(key).matches("(?i)true|on|yes");
184 * Returns the (initial char) value specified by key as a char.
188 public char getChar(String key) {
189 final String s = s(key);
190 return (s.length() == 0) ? ' ' : s.charAt(0);
194 * Returns the value specified by key as a int.
198 public int getInt(String key) {
199 return getInt(key, 0);
203 * Returns the value specified by key as a int.
205 * @param defaultValue
208 public int getInt(String key, int defaultValue) {
209 final String s = s(key);
211 return Integer.parseInt(s);
212 } catch (NumberFormatException ex) {
220 class ResourceManager0 {
222 static final ConcurrentHashMap<String, Map<String, String>> map = new ConcurrentHashMap<String, Map<String, String>>();
224 static String getPackageName() {
225 return ResourceManager0.class.getPackage().getName();
228 static InputStream getResourceAsStream(String path) {
229 return ResourceManager0.class.getResourceAsStream(path);