OSDN Git Service

fix 3 potential problems detected by FindBugs
[stew/Stew4.git] / src / net / argius / stew / ResourceManager.java
1 package net.argius.stew;
2
3 import java.io.*;
4 import java.text.*;
5 import java.util.*;
6 import java.util.concurrent.*;
7
8 /**
9  * ResourceManager provides a function like a ResourceBundle used UTF-8 instead Unicode escapes.
10  */
11 public final class ResourceManager {
12
13     public static final ResourceManager Default = ResourceManager.getInstance(ResourceManager0.class);
14
15     private List<Map<String, String>> list;
16
17     private ResourceManager(List<Map<String, String>> list) {
18         this.list = list;
19     }
20
21     /**
22      * Creates an instance.
23      * @param o
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.
28      * @return
29      */
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");
40                 if (m != null) {
41                     ResourceManager0.map.put(key, m);
42                     a.add(m);
43                 }
44             }
45         }
46         return new ResourceManager(a);
47     }
48
49     private static Set<String> getResourceNames(Object o) {
50         Set<String> set = new LinkedHashSet<String>();
51         String cn = null;
52         String pn = null;
53         if (o instanceof String) {
54             cn = (String)o;
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();
59             cn = c.getName();
60             pn = c.getPackage().getName();
61         }
62         if (cn != null) {
63             set.add(cn);
64         }
65         if (pn != null) {
66             set.add(pn + ".messages");
67         }
68         set.add(ResourceManager0.getPackageName() + ".messages");
69         return set;
70     }
71
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);
75         if (is == null) {
76             return null;
77         }
78         List<String> lines = new ArrayList<String>();
79         Scanner r = new Scanner(is, encname);
80         try {
81             StringBuilder buffer = new StringBuilder();
82             while (r.hasNextLine()) {
83                 final String s = r.nextLine();
84                 if (s.matches("^\\s*#.*")) {
85                     continue;
86                 }
87                 buffer.append(s.replace("\\t", "\t").replace("\\n", "\n").replace("\\=", "="));
88                 if (s.endsWith("\\")) {
89                     buffer.setLength(buffer.length() - 1);
90                     continue;
91                 }
92                 lines.add(buffer.toString());
93                 buffer.setLength(0);
94             }
95             if (buffer.length() > 0) {
96                 lines.add(buffer.toString());
97             }
98         } finally {
99             r.close();
100         }
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("\\ ", " "));
106             } else {
107                 m.put(s.trim(), "");
108             }
109         }
110         return m;
111     }
112
113     /**
114      * @param path resource's path
115      * @param defaultValue defalut value if a resource not found
116      * @return
117      */
118     public String read(String path, String defaultValue) {
119         InputStream in = ResourceManager0.getResourceAsStream(path);
120         if (in == null) {
121             return defaultValue;
122         }
123         StringBuilder buffer = new StringBuilder();
124         Scanner r = new Scanner(in);
125         try {
126             if (r.hasNextLine()) {
127                 buffer.append(r.nextLine());
128             }
129             while (r.hasNextLine()) {
130                 buffer.append(String.format("%n"));
131                 buffer.append(r.nextLine());
132             }
133         } finally {
134             r.close();
135         }
136         return buffer.toString();
137     }
138
139     private String s(String key) {
140         for (final Map<String, String> m : this.list) {
141             final String s = m.get(key);
142             if (s != null) {
143                 return s;
144             }
145         }
146         return "";
147     }
148
149     /**
150      * Returns true if this resource contains a value specified by key.
151      * @param key
152      * @return
153      */
154     public boolean containsKey(String key) {
155         for (final Map<String, String> m : this.list) {
156             if (m.containsKey(key)) {
157                 return true;
158             }
159         }
160         return false;
161     }
162
163     /**
164      * Returns the value specified by key as a String.
165      * @param key
166      * @param args
167      * @return
168      */
169     public String get(String key, Object... args) {
170         final String s = s(key);
171         return (s.length() == 0) ? key : MessageFormat.format(s, args);
172     }
173
174     /**
175      * Returns the value specified by key as a boolean.
176      * @param key
177      * @return
178      */
179     public boolean isTrue(String key) {
180         return s(key).matches("(?i)true|on|yes");
181     }
182
183     /**
184      * Returns the (initial char) value specified by key as a char.
185      * @param key
186      * @return
187      */
188     public char getChar(String key) {
189         final String s = s(key);
190         return (s.length() == 0) ? ' ' : s.charAt(0);
191     }
192
193     /**
194      * Returns the value specified by key as a int.
195      * @param key
196      * @return
197      */
198     public int getInt(String key) {
199         return getInt(key, 0);
200     }
201
202     /**
203      * Returns the value specified by key as a int.
204      * @param key
205      * @param defaultValue
206      * @return
207      */
208     public int getInt(String key, int defaultValue) {
209         final String s = s(key);
210         try {
211             return Integer.parseInt(s);
212         } catch (NumberFormatException ex) {
213             // ignore
214         }
215         return defaultValue;
216     }
217
218 }
219
220 class ResourceManager0 {
221
222     static final ConcurrentHashMap<String, Map<String, String>> map = new ConcurrentHashMap<String, Map<String, String>>();
223
224     static String getPackageName() {
225         return ResourceManager0.class.getPackage().getName();
226     }
227
228     static InputStream getResourceAsStream(String path) {
229         return ResourceManager0.class.getResourceAsStream(path);
230     }
231
232 }