~sword-devel/jsword/trunk

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package org.crosswire.jsword.book.sword;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.log4j.Logger;
import org.crosswire.common.util.NetUtil;
import org.crosswire.common.util.Reporter;
import org.crosswire.jsword.book.Bible;
import org.crosswire.jsword.book.BibleDriverManager;
import org.crosswire.jsword.book.BookException;
import org.crosswire.jsword.book.basic.AbstractBibleDriver;

/**
 * This represents all of the SwordBibles.
 * 
 * <p><table border='1' cellPadding='3' cellSpacing='0'>
 * <tr><td bgColor='white' class='TableRowColor'><font size='-7'>
 *
 * Distribution Licence:<br />
 * JSword is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General Public License,
 * version 2 as published by the Free Software Foundation.<br />
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.<br />
 * The License is available on the internet
 * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, or by writing to:
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA<br />
 * The copyright to this program is held by it's authors.
 * </font></td></tr></table>
 * @see docs.Licence
 * @author Joe Walker [joe at eireneh dot com]
 * @version $Id$
 */
public class SwordBibleDriver extends AbstractBibleDriver
{
    /**
     * Some basic driver initialization
     */
    private SwordBibleDriver() throws MalformedURLException
    {
    }

    /**
     * Some basic info about who we are
     * @param A short identifing string
     */
    public String getDriverName()
    {
        return "Sword";
    }

    /**
     * Get a list of the Books available from the driver
     * @return an array of book names
     */
    public String[] getBibleNames()
    {
        if (nested == null)
        {
            log.warn("No directory set");
            return new String[0];
        }

        if (!nested.getProtocol().equals("file"))
        {
            log.warn("SwordBibleDriver has file: URL. Ignoring.");
            return new String[0];
        }

        File fdir = new File(nested.getFile());

        // Check that the dir exists
        if (!fdir.isDirectory())
        {
            log.debug("The directory '"+dir+"' does not exist.");
            return new String[0];
        }

        // List all the versions
        return fdir.list(new CustomFilenameFilter());
    }

    /**
     * Does the named Bible exist?
     * @param name The name of the version to test for
     * @return true if the Bible exists
     */
    public boolean exists(String name)
    {
        try
        {
            URL url = NetUtil.lengthenURL(nested, name);
            return NetUtil.isDirectory(url);
        }
        catch (Exception ex)
        {
            return false;
        }
    }

    /**
     * Featch a currently existing Bible, read-only
     * @param name The name of the version to create
     * @exception BookException If the name is not valid
     */
    public Bible getBible(String name) throws BookException
    {
        try
        {
            URL url = NetUtil.lengthenURL(nested, name);

            if (!NetUtil.isDirectory(url))
                throw new BookException("sword_driver_find", new Object[] { name });

            return new SwordBible(name, url);
        }
        catch (MalformedURLException ex)
        {
            throw new BookException("sword_driver_dir", ex);
        }
    }

    /**
     * Create a new blank Bible read for writing
     * @param name The name of the version to create
     * @exception BookException If the name is not valid
     */
    public Bible createBible(String name) throws BookException
    {
        throw new BookException("sword_driver_readonly");
    }

    /**
     * Rename this version
     * @param old_name The current name for the version
     * @param new_name The name we would like the driver to have
     */
    public void renameBible(String old_name, String new_name) throws BookException
    {
        throw new BookException("sword_driver_readonly");
    }

    /**
     * Delete the set of files that make up this version.
     * @param name The name of the version to delete
     */
    public void deleteBible(String name) throws BookException
    {
        throw new BookException("sword_driver_readonly");
    }

    /**
     * Accessor for the Sword directory
     * @param sword_dir The new Sword directory
     */
    public static void setSwordDir(String sword_dir) throws MalformedURLException
    {
        // Just accept that we're not supposed to work ...
        if (sword_dir == null || sword_dir.trim().length() == 0)
        {
            driver.dir = null;
            driver.nested = null;
            log.info("No sword dir set.");
            return;
        }

        URL dir_temp = new URL("file:"+sword_dir);
        URL nest_temp = NetUtil.lengthenURL(dir_temp, "modules", "texts", "rawtext");

        if (!NetUtil.isDirectory(nest_temp))
            throw new MalformedURLException("No sword source found under "+sword_dir);

        driver.dir = dir_temp;
        driver.nested = nest_temp;
    }

    /**
     * Accessor for the Sword directory
     * @return The new Sword directory
     */
    public static String getSwordDir()
    {
        if (driver.dir == null)
            return "";

        return driver.dir.toExternalForm().substring(5);
    }

    /** The directory URL */
    private URL dir;

    /** The directory URL */
    private URL nested;

    /** The singleton driver */
    protected static SwordBibleDriver driver;

    /** The log stream */
    protected static Logger log = Logger.getLogger(SwordBibleDriver.class);

    /**
     * Register ourselves with the Driver Manager
     */
    static
    {
        try
        {
            driver = new SwordBibleDriver();
            BibleDriverManager.registerDriver(driver);
        }
        catch (MalformedURLException ex)
        {
            Reporter.informUser(SwordBibleDriver.class, ex);
        }
    }

    /**
     * Check that the directories in the version directory really
     * represent versions.
     */
    static class CustomFilenameFilter implements FilenameFilter
    {
        public boolean accept(File parent, String name)
        {
            try
            {
                return new File(parent.getCanonicalPath() + File.separator + name).isDirectory();
            }
            catch (IOException ex)
            {
                Reporter.informUser(SwordBibleDriver.class, ex);
                return false;
            }
        }
    }
}