Source code: com/eireneh/bible/book/sword/SwordBibleDriver.java
1
2 package com.eireneh.bible.book.sword;
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 SwordBibles.
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 SwordBibleDriver extends AbstractBibleDriver
36 {
37 /**
38 * Some basic driver initialization
39 */
40 private SwordBibleDriver() throws MalformedURLException
41 {
42 }
43
44 /**
45 * Some basic info about who we are
46 * @param A short identifing string
47 */
48 public String getDriverName()
49 {
50 return "Sword";
51 }
52
53 /**
54 * Get a list of the Books available from the driver
55 * @return an array of book names
56 */
57 public String[] getBibleNames() throws BookException
58 {
59 try
60 {
61 if (!nested.getProtocol().equals("file"))
62 throw new BookException("sword_file_only", new Object[] { dir.getProtocol() });
63
64 File fdir = new File(nested.getFile());
65
66 // Check that the dir exists
67 if (!fdir.isDirectory())
68 {
69 log.fine("The directory '"+dir+"' does not exist.");
70 return new String[0];
71 }
72
73 // List all the versions
74 return fdir.list(new CustomFilenameFilter());
75 }
76 catch (Exception ex)
77 {
78 Reporter.informUser(this, ex);
79 return new String[0];
80 }
81 }
82
83 /**
84 * Does the named Bible exist?
85 * @param name The name of the version to test for
86 * @return true if the Bible exists
87 */
88 public boolean exists(String name)
89 {
90 try
91 {
92 URL url = NetUtil.lengthenURL(nested, name);
93 return NetUtil.isDirectory(url);
94 }
95 catch (MalformedURLException ex)
96 {
97 return false;
98 }
99 }
100
101 /**
102 * Featch a currently existing Bible, read-only
103 * @param name The name of the version to create
104 * @exception BookException If the name is not valid
105 */
106 public Bible getBible(String name) throws BookException
107 {
108 try
109 {
110 URL url = NetUtil.lengthenURL(nested, name);
111
112 if (!NetUtil.isDirectory(url))
113 throw new BookException("sword_driver_find", new Object[] { name });
114
115 return new SwordBible(name, url);
116 }
117 catch (MalformedURLException ex)
118 {
119 throw new BookException("sword_driver_dir", ex);
120 }
121 }
122
123 /**
124 * Create a new blank Bible read for writing
125 * @param name The name of the version to create
126 * @exception BookException If the name is not valid
127 */
128 public Bible createBible(String name) throws BookException
129 {
130 throw new BookException("sword_driver_readonly");
131 }
132
133 /**
134 * Rename this version
135 * @param old_name The current name for the version
136 * @param new_name The name we would like the driver to have
137 */
138 public void renameBible(String old_name, String new_name) throws BookException
139 {
140 throw new BookException("sword_driver_readonly");
141 }
142
143 /**
144 * Delete the set of files that make up this version.
145 * @param name The name of the version to delete
146 */
147 public void deleteBible(String name) throws BookException
148 {
149 throw new BookException("sword_driver_readonly");
150 }
151
152 /**
153 * Accessor for the Sword directory
154 * @param sword_dir The new Sword directory
155 */
156 public static void setSwordDir(String sword_dir) throws MalformedURLException
157 {
158 URL dir_temp = new URL("file:"+sword_dir);
159 URL nest_temp = NetUtil.lengthenURL(dir_temp, "modules", "texts", "rawtext");
160
161 if (!NetUtil.isDirectory(nest_temp))
162 throw new MalformedURLException("No sword source found under "+sword_dir);
163
164 driver.dir = dir_temp;
165 driver.nested = nest_temp;
166 }
167
168 /**
169 * Accessor for the Sword directory
170 * @return The new Sword directory
171 */
172 public static String getSwordDir()
173 {
174 if (driver.dir == null)
175 return "";
176
177 return driver.dir.toExternalForm().substring(5);
178 }
179
180 /** The directory URL */
181 private URL dir;
182
183 /** The directory URL */
184 private URL nested;
185
186 /** The singleton driver */
187 protected static SwordBibleDriver driver;
188
189 /** The log stream */
190 protected static Logger log = Logger.getLogger("bible.book");
191
192 /**
193 * Register ourselves with the Driver Manager
194 */
195 static
196 {
197 try
198 {
199 driver = new SwordBibleDriver();
200 BibleDriverManager.registerDriver(driver);
201 }
202 catch (MalformedURLException ex)
203 {
204 Reporter.informUser(SwordBibleDriver.class, ex);
205 }
206 }
207
208 /**
209 * Check that the directories in the version directory really
210 * represent versions.
211 */
212 static class CustomFilenameFilter implements FilenameFilter
213 {
214 public boolean accept(File parent, String name)
215 {
216 try
217 {
218 return new File(parent.getCanonicalPath() + File.separator + name).isDirectory();
219 }
220 catch (IOException ex)
221 {
222 Reporter.informUser(SwordBibleDriver.class, ex);
223 return false;
224 }
225 }
226 }
227 }