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

Quick Search    Search Deep

Source code: com/eireneh/bible/book/ser/SerBibleDriver.java


1   
2   package com.eireneh.bible.book.ser;
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 SerBibles.
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 SerBibleDriver extends AbstractBibleDriver
36  {
37      /**
38       * Some basic driver initialization
39       */
40      private SerBibleDriver() throws MalformedURLException
41      {
42          dir = NetUtil.lengthenURL(Project.getBiblesRoot(), "ser");
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 "Serialized";
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 
122             if (!NetUtil.isDirectory(url))
123                 throw new BookException("ser_driver_find", new Object[] { name });
124 
125             return new SerBible(name, url, false);
126         }
127         catch (MalformedURLException ex)
128         {
129             throw new BookException("ser_driver_dir", ex);
130         }
131     }
132 
133     /**
134      * Create a new blank Bible read for writing
135      * @param name The name of the version to create
136      * @exception BookException If the name is not valid
137      */
138     public Bible createBible(String name) throws BookException
139     {
140         try
141         {
142             URL url = NetUtil.lengthenURL(dir, name);
143 
144             if (NetUtil.isDirectory(url))
145                 throw new BookException("ser_driver_exists", new Object[] { name });
146 
147             NetUtil.makeDirectory(url);
148             return new SerBible(name, url, true);
149         }
150         catch (MalformedURLException ex)
151         {
152             throw new BookException("ser_driver_dir", ex);
153         }
154     }
155 
156     /**
157      * Rename this version
158      * @param old_name The current name for the version
159      * @param new_name The name we would like the driver to have
160      */
161     public void renameBible(String old_name, String new_name) throws BookException
162     {
163         try
164         {
165             URL old_url = NetUtil.lengthenURL(dir, old_name);
166             URL new_url = NetUtil.lengthenURL(dir, new_name);
167 
168             if (!NetUtil.isDirectory(old_url))
169                 throw new BookException("ser_driver_find", new Object[] { old_name });
170 
171             if (NetUtil.isDirectory(new_url))
172                 throw new BookException("ser_driver_exists", new Object[] { new_name });
173 
174             NetUtil.move(old_url, new_url);
175         }
176         catch (Exception ex)
177         {
178             throw new BookException("ser_driver_dir", ex);
179         }
180     }
181 
182     /**
183      * Delete the set of files that make up this version.
184      * @param name The name of the version to delete
185      */
186     public void deleteBible(String name) throws BookException
187     {
188         try
189         {
190             StringBuffer failures = new StringBuffer();
191             URL home = NetUtil.lengthenURL(dir, name);
192 
193             if (!NetUtil.isDirectory(home))
194                 throw new BookException("ser_driver_find", new Object[] { name });
195 
196             deleteFileURL(home, "ref.idx", failures);
197             deleteFileURL(home, "xml.idx", failures);
198             deleteFileURL(home, "ref.dat", failures);
199             deleteFileURL(home, "xml.dat", failures);
200             deleteFileURL(home, "bible.properties", failures);
201             deleteFileURL(home, "generate.log", failures);
202 
203             if (failures.length() != 0)
204             {
205                 throw new BookException("ser_driver_delfile", new Object[] { failures, home });
206             }
207             else
208             {
209                 if (!NetUtil.delete(home))
210                     throw new BookException("ser_driver_deldir", new Object[] { home });
211             }
212         }
213         catch (Exception ex)
214         {
215             throw new BookException("ser_driver_dir", ex);
216         }
217     }
218 
219     /**
220      * Convenience file delete and check routine.
221      * @param name The name of the file to delete
222      * @param errors The place to store an accumulated error message
223      */
224     private void deleteFileURL(URL home, String name, StringBuffer errors) throws IOException
225     {
226         URL url = NetUtil.lengthenURL(home, name);
227 
228         if (!NetUtil.isFile(url))
229             return;
230 
231         if (!NetUtil.delete(url))
232         {
233             if (errors.length() != 0)
234                 errors.append(", ");
235 
236             errors.append(name);
237         }
238     }
239 
240     /** The directory URL */
241     private URL dir;
242 
243     /** The singleton driver */
244     protected static SerBibleDriver driver;
245 
246     /** The log stream */
247     protected static Logger log = Logger.getLogger("bible.book");
248 
249     /**
250      * Register ourselves with the Driver Manager
251      */
252     static
253     {
254         try
255         {
256             driver = new SerBibleDriver();
257             BibleDriverManager.registerDriver(driver);
258         }
259         catch (MalformedURLException ex)
260         {
261             Reporter.informUser(SerBibleDriver.class, ex);
262         }
263     }
264 
265     /**
266      * Check that the directories in the version directory really
267      * represent versions.
268      */
269     static class CustomFilenameFilter implements FilenameFilter
270     {
271         public boolean accept(File parent, String name)
272         {
273             try
274             {
275                 return new File(parent.getCanonicalPath() + File.separator + name).isDirectory();
276             }
277             catch (IOException ex)
278             {
279                 Reporter.informUser(SerBibleDriver.class, ex);
280                 return false;
281             }
282         }
283     }
284 }