1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 package pl.kernelpanic.dbmonster;
48
49 import java.io.File;
50 import java.io.FileInputStream;
51 import java.io.InputStream;
52 import java.io.InputStreamReader;
53 import java.io.LineNumberReader;
54 import java.net.URL;
55 import java.util.HashMap;
56 import java.util.Map;
57 import java.util.Random;
58 import java.util.zip.GZIPInputStream;
59 import java.util.zip.ZipEntry;
60 import java.util.zip.ZipFile;
61
62 /***
63 * Dictionary Manager.
64 *
65 * @author Piotr Maj <piotr.maj@kernelpanic.pl>
66 *
67 * @version $Id: DictionaryManager.html,v 1.1 2007/06/21 08:38:14 sbahloul Exp $
68 */
69 public class DictionaryManager {
70
71 /***
72 * Holds dictionaries. As keys canonical pathnamea are used.
73 */
74 private Map dictionaries = new HashMap();
75
76 /***
77 * Random number generator.
78 */
79 private Random random = null;
80
81 /***
82 * Sets the random number generator.
83 *
84 * @param rnd random number generator
85 */
86 public final void setRandom(final Random rnd) {
87 random = rnd;
88 }
89
90 /***
91 * Returns a dictionary.
92 *
93 * @param schemaPath the schema file home
94 * @param path pathname
95 *
96 * @return a dictionary
97 *
98 * @throws Exception if dictionary cannot be obtained
99 */
100 public final Dictionary getDictionary(
101 final String schemaPath,
102 final String path) throws Exception {
103 File dictFile = new File(path);
104 if (!dictFile.isAbsolute()) {
105 dictFile = new File(schemaPath, path);
106 }
107 String key = dictFile.getCanonicalPath();
108 if (dictionaries.containsKey(key)) {
109 return (Dictionary) dictionaries.get(key);
110 } else {
111 loadDictionary(key);
112 return (Dictionary) dictionaries.get(key);
113 }
114 }
115
116 /***
117 * Loads a dictionary using specified url.
118 *
119 * @param url url
120 *
121 * @return dictionary
122 *
123 * @throws Exception if dictionary cannot be loded.
124 */
125 public final Dictionary getDictionary(final URL url) throws Exception {
126 if (dictionaries.containsKey(url.toString())) {
127 return (Dictionary) dictionaries.get(url.toString());
128 } else {
129 loadDictionary(url);
130 return (Dictionary) dictionaries.get(url.toString());
131 }
132 }
133
134 /***
135 * Loads a dictionary.
136 *
137 * @param path a canonical path of the file
138 *
139 * @throws Exception if dictionary could not be loaded.
140 */
141 private final void loadDictionary(final String path) throws Exception {
142 File f = new File(path);
143 if (!f.exists() || !f.canRead()) {
144 throw new Exception("Cannot access dictionary file + " + path);
145 }
146
147 InputStream is = null;
148 if (path.endsWith(".zip")) {
149 ZipFile zf = new ZipFile(f);
150 ZipEntry ze = (ZipEntry) zf.entries().nextElement();
151 is = zf.getInputStream(ze);
152 } else if (path.endsWith(".gz")) {
153 is = new GZIPInputStream(new FileInputStream(f));
154 } else {
155 is = new FileInputStream(f);
156 }
157 readDictionary(path, is);
158 }
159
160 /***
161 * Loads a dictionary.
162 *
163 * @param url url
164 *
165 * @throws Exception if dictionary could not be loaded.
166 */
167 private final void loadDictionary(final URL url) throws Exception {
168 InputStream is = new GZIPInputStream(url.openStream());
169 readDictionary(url.toString(), is);
170 }
171
172 /***
173 * Reads a dictionary and adds it to the list.
174 *
175 * @param key a key
176 * @param is input stream
177 *
178 * @throws Exception exception
179 */
180 private final void readDictionary(final String key, final InputStream is)
181 throws Exception {
182
183 LineNumberReader reader = new LineNumberReader(
184 new InputStreamReader(is, "UTF-8"));
185 String line = null;
186
187 Dictionary dictionary = new Dictionary();
188 dictionary.setName(key);
189 dictionary.setRandom(random);
190 while ((line = reader.readLine()) != null) {
191 dictionary.addItem(line);
192 }
193 dictionaries.put(key, dictionary);
194 }
195 }