Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/eireneh/bible/book/raw/RawBibleDriver.java


1   
2   package com.eireneh.bible.book.raw;
3   
4   import java.io.*;
5   import java.net.*;
6   import java.util.*;
7   
8   import com.eireneh.util.*;
9   import com.eireneh.bible.util.Project;
10  import com.eireneh.bible.book.*;
11  
12  /**
13   * This represents all of the RawBibles.
14   *
15   * <table border='1' cellPadding='3' cellSpacing='0' width="100%">
16   * <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
17   * Distribution Licence:<br />
18   * Project B is free software; you can redistribute it
19   * and/or modify it under the terms of the GNU General Public License,
20   * version 2 as published by the Free Software Foundation.<br />
21   * This program is distributed in the hope that it will be useful,
22   * but WITHOUT ANY WARRANTY; without even the implied warranty of
23   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24   * General Public License for more details.<br />
25   * The License is available on the internet
26   * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
27   * <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28   * MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
29   * The copyright to this program is held by it's authors.
30   * </font></td></tr></table>
31   * @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
32   * @see docs.Licence
33   * @author Joe Walker
34   */
35  public class RawBibleDriver extends AbstractBibleDriver
36  {
37      /**
38       * Some basic driver initialization
39       */
40      private RawBibleDriver() throws MalformedURLException
41      {
42          dir = NetUtil.lengthenURL(Project.getBiblesRoot(), "raw");
43      }
44  
45      /**
46       * Some basic info about who we are
47       * @param A short identifing string
48       */
49      public String getDriverName()
50      {
51          return "Raw";
52      }
53  
54      /**
55       * Get a list of the Books available from the driver
56       * @return an array of book names
57       */
58      public String[] getBibleNames() throws BookException
59      {
60          URL url = null;
61  
62          try
63          {
64              if (dir.getProtocol().equals("file"))
65              {
66                  File fdir = new File(dir.getFile());
67  
68                  // Check that the dir exists
69                  if (!fdir.isDirectory())
70                  {
71                      log.fine("The directory '"+dir+"' does not exist.");
72                      return new String[0];
73                  }
74  
75                  // List all the versions
76                  return fdir.list(new CustomFilenameFilter());
77              }
78              else
79              {
80                  URL search = NetUtil.lengthenURL(dir, "list.txt");
81                  InputStream in = search.openStream();
82                  String contents = StringUtil.read(new InputStreamReader(in));
83                  return StringUtil.tokenize(contents, "\n");
84              }
85          }
86          catch (IOException ex)
87          {
88              Reporter.informUser(this, ex);
89              return new String[0];
90          }
91      }
92  
93      /**
94       * Does the named Bible exist?
95       * @param name The name of the version to test for
96       * @return true if the Bible exists
97       */
98      public boolean exists(String name)
99      {
100         try
101         {
102             URL url = NetUtil.lengthenURL(dir, name);
103             return NetUtil.isDirectory(url);
104         }
105         catch (MalformedURLException ex)
106         {
107             return false;
108         }
109     }
110 
111     /**
112      * Featch a currently existing Bible, read-only
113      * @param name The name of the version to create
114      * @exception BookException If the name is not valid
115      */
116     public Bible getBible(String name) throws BookException
117     {
118         try
119         {
120             URL url = NetUtil.lengthenURL(dir, name);
121             if (!NetUtil.isDirectory(url))
122                 throw new BookException("raw_driver_find", new Object[] { name });
123 
124             if (memory) return new RawBible(name, url, RawBible.MODE_READ_MEMORY);
125             else        return new RawBible(name, url, RawBible.MODE_READ_DISK);
126         }
127         catch (Exception ex)
128         {
129             if (ex instanceof BookException)
130                 throw (BookException) ex;
131 
132             throw new BookException("raw_driver_dir", ex);
133         }
134     }
135 
136     /**
137      * Create a new blank Bible read for writing
138      * @param name The name of the version to create
139      * @exception BookException If the name is not valid
140      */
141     public Bible createBible(String name) throws BookException
142     {
143         try
144         {
145             URL url = NetUtil.lengthenURL(dir, name);
146 
147             if (NetUtil.isDirectory(url))
148                 throw new BookException("raw_driver_exists", new Object[] { name });
149 
150             NetUtil.makeDirectory(url);
151             return new RawBible(name, url, RawBible.MODE_WRITE);
152         }
153         catch (MalformedURLException ex)
154         {
155             throw new BookException("raw_driver_dir", ex);
156         }
157     }
158 
159     /**
160      * Rename this version
161      * @param old_name The current name for the version
162      * @param new_name The name we would like the driver to have
163      */
164     public void renameBible(String old_name, String new_name) throws BookException
165     {
166         try
167         {
168             URL old_url = NetUtil.lengthenURL(dir, old_name);
169             URL new_url = NetUtil.lengthenURL(dir, new_name);
170 
171             if (!NetUtil.isDirectory(old_url))
172                 throw new BookException("raw_driver_find", new Object[] { old_name });
173 
174             if (NetUtil.isDirectory(new_url))
175                 throw new BookException("raw_driver_exists", new Object[] { new_name });
176 
177             NetUtil.move(old_url, new_url);
178         }
179         catch (Exception ex)
180         {
181             throw new BookException("raw_driver_dir", ex);
182         }
183     }
184 
185     /**
186      * Delete the set of files that make up this version.
187      * @param name The name of the version to delete
188      */
189     public void deleteBible(String name) throws BookException
190     {
191         try
192         {
193             StringBuffer failures = new StringBuffer();
194             URL home = NetUtil.lengthenURL(dir, name);
195 
196             if (!NetUtil.isDirectory(home))
197                 throw new BookException("raw_driver_find", new Object[] { name });
198 
199             deleteFileURL(home, "caseinst.idx", failures);
200             deleteFileURL(home, "parainst.idx", failures);
201             deleteFileURL(home, "punc.idx", failures);
202             deleteFileURL(home, "puncinst.idx", failures);
203             deleteFileURL(home, "word.idx", failures);
204             deleteFileURL(home, "wordinst.idx", failures);
205             deleteFileURL(home, "bible.properties", failures);
206             deleteFileURL(home, "generate.log", failures);
207 
208             if (failures.length() != 0)
209             {
210                 throw new BookException("raw_driver_delfile", new Object[] { failures, home });
211             }
212             else
213             {
214                 if (!NetUtil.delete(home))
215                     throw new BookException("raw_driver_deldir", new Object[] { home });
216             }
217         }
218         catch (Exception ex)
219         {
220             throw new BookException("raw_driver_dir", ex);
221         }
222     }
223 
224     /**
225      * Convenience file delete and check routine.
226      * @param name The name of the file to delete
227      * @param errors The place to store an accumulated error message
228      */
229     private void deleteFileURL(URL home, String name, StringBuffer errors) throws IOException
230     {
231         URL url = NetUtil.lengthenURL(home, name);
232 
233         if (!NetUtil.isFile(url))
234             return;
235 
236         if (!NetUtil.delete(url))
237         {
238             if (errors.length() != 0)
239                 errors.append(", ");
240 
241             errors.append(name);
242         }
243     }
244 
245     /** The directory URL */
246     private URL dir;
247 
248     /** The singleton driver */
249     protected static RawBibleDriver driver;
250 
251     /** Do we instruct new RawBibles to cache data in memory? */
252     private static boolean memory = true;
253 
254     /** The log stream */
255     protected static Logger log = Logger.getLogger("bible.book");
256 
257     /**
258      * Do the Bibles we create cache everything in memory or leave it on
259      * disk and then read it at query time.
260      * @return True if we are cacheing data in memory
261      */
262     public static boolean getDefaultCacheData()
263     {
264         return memory;
265     }
266 
267     /**
268      * Do the Bibles we create cache everything in memory or leave it on
269      * disk and then read it at query time.
270      * @param memory True if we are cacheing data in memory
271      */
272     public static void setDefaultCacheData(boolean memory)
273     {
274         RawBibleDriver.memory = memory;
275     }
276 
277     /**
278      * Register ourselves with the Driver Manager
279      */
280     static
281     {
282         try
283         {
284             driver = new RawBibleDriver();
285             BibleDriverManager.registerDriver(driver);
286         }
287         catch (MalformedURLException ex)
288         {
289             log.log(Level.INFO, "RawBibleDriver init failure", ex);
290         }
291     }
292 
293     /**
294      * Check that the directories in the version directory really
295      * represent versions.
296      */
297     static class CustomFilenameFilter implements FilenameFilter
298     {
299         public boolean accept(File parent, String name)
300         {
301             try
302             {
303                 return new File(parent.getCanonicalPath() + File.separator +
304                                 name + File.separator + "wordinst.idx").isFile();
305             }
306             catch (IOException ex)
307             {
308                 Reporter.informUser(this, ex);
309                 return false;
310             }
311         }
312     }
313 }