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 <pm@jcake.com>
66 *
67 * @version $Id: DictionaryManager.java,v 1.2 2006/01/05 16:29:37 majek 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 void setRandom(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 Dictionary getDictionary(String schemaPath, String path) throws Exception {
101 File dictFile = new File(path);
102 if (!dictFile.isAbsolute()) {
103 dictFile = new File(schemaPath, path);
104 }
105 String key = dictFile.getCanonicalPath();
106 if (dictionaries.containsKey(key)) {
107 return (Dictionary) dictionaries.get(key);
108 } else {
109 loadDictionary(key);
110 return (Dictionary) dictionaries.get(key);
111 }
112 }
113
114 /***
115 * Loads a dictionary using specified url.
116 *
117 * @param url url
118 *
119 * @return dictionary
120 *
121 * @throws Exception if dictionary cannot be loded.
122 */
123 public Dictionary getDictionary(URL url) throws Exception {
124 if (dictionaries.containsKey(url.toString())) {
125 return (Dictionary) dictionaries.get(url.toString());
126 } else {
127 loadDictionary(url);
128 return (Dictionary) dictionaries.get(url.toString());
129 }
130 }
131
132 /***
133 * Loads a dictionary.
134 *
135 * @param path a canonical path of the file
136 *
137 * @throws Exception if dictionary could not be loaded.
138 */
139 private void loadDictionary(String path) throws Exception {
140 File f = new File(path);
141 if (!f.exists() || !f.canRead()) {
142 throw new Exception("Cannot access dictionary file + " + path);
143 }
144
145 InputStream is = null;
146 if (path.endsWith(".zip")) {
147 ZipFile zf = new ZipFile(f);
148 ZipEntry ze = (ZipEntry) zf.entries().nextElement();
149 is = zf.getInputStream(ze);
150 } else if (path.endsWith(".gz")) {
151 is = new GZIPInputStream(new FileInputStream(f));
152 } else {
153 is = new FileInputStream(f);
154 }
155 readDictionary(path, is);
156 }
157
158 /***
159 * Loads a dictionary.
160 *
161 * @param url url
162 *
163 * @throws Exception if dictionary could not be loaded.
164 */
165 private void loadDictionary(URL url) throws Exception {
166 InputStream is = new GZIPInputStream(url.openStream());
167 readDictionary(url.toString(), is);
168 }
169
170 /***
171 * Reads a dictionary and adds it to the list.
172 *
173 * @param key a key
174 * @param is input stream
175 *
176 * @throws Exception exception
177 */
178 private void readDictionary(String key, InputStream is)
179 throws Exception {
180
181 LineNumberReader reader = new LineNumberReader(
182 new InputStreamReader(is, "UTF-8"));
183 String line = null;
184
185 Dictionary dictionary = new Dictionary();
186 dictionary.setName(key);
187 dictionary.setRandom(random);
188 while ((line = reader.readLine()) != null) {
189 dictionary.addItem(line);
190 }
191 dictionaries.put(key, dictionary);
192 }
193 }