View Javadoc

1   /* Version 1.0 based on Apache Software License 1.1
2    *
3    * Copyright (c) 2003 Piotr Maj and DBMonster developers. All rights
4    * reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions are
8    * met:
9    *
10   * 1. Redistributions of source code must retain the above copyright
11   *    notice, this list of conditions and the following disclaimer.
12   *
13   * 2. Redistributions in binary form must reproduce the above copyright
14   *    notice, this list of conditions and the following disclaimer in the
15   *    documentation and/or other materials provided with the distribution.
16   *
17   * 3. The end-user documentation included with the redistribution, if any,
18   *    must include the following acknowledgment:
19   *
20   *    "This product includes software developed by DBMonster developers
21   *    (http://dbmonster.kernelpanic.pl/)."
22   *
23   *  Alternately, this acknowledgment may appear in the software itself,
24   *  if and wherever such third-party acknowledgments normally appear.
25   *
26   * 4. The name "DBMonster" must not be used to endorse or promote products
27   *    derived from this software without prior written permission. For
28   *    written permission, please contact piotr.maj@kernelpanic.pl.
29   *
30   * 5. Products derived from this software may not be called "DBMonster",
31   *    nor may "DBMonster" appear in their name, without prior written
32   *    permission of Piotr Maj.
33   *
34   * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
35   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
37   * IN NO EVENT SHALL THE DBMONSTER DEVELOPERS BE LIABLE FOR ANY DIRECT,
38   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
40   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
42   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
43   * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44   * POSSIBILITY OF SUCH DAMAGE.
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 }