1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
package org.crosswire.jsword.book.sword; |
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.io.IOException; |
24 | |
import java.net.URI; |
25 | |
import java.util.HashSet; |
26 | |
import java.util.List; |
27 | |
import java.util.Set; |
28 | |
|
29 | |
import org.crosswire.common.util.FileUtil; |
30 | |
import org.crosswire.common.util.NetUtil; |
31 | |
import org.crosswire.jsword.JSMsg; |
32 | |
import org.crosswire.jsword.JSOtherMsg; |
33 | |
import org.crosswire.jsword.book.Book; |
34 | |
import org.crosswire.jsword.book.BookDriver; |
35 | |
import org.crosswire.jsword.book.BookException; |
36 | |
import org.crosswire.jsword.book.BookMetaData; |
37 | |
import org.crosswire.jsword.book.Books; |
38 | |
import org.crosswire.jsword.book.basic.AbstractBookDriver; |
39 | |
import org.crosswire.jsword.index.IndexManager; |
40 | |
import org.crosswire.jsword.index.IndexManagerFactory; |
41 | |
import org.crosswire.jsword.index.IndexStatus; |
42 | |
import org.slf4j.Logger; |
43 | |
import org.slf4j.LoggerFactory; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public class SwordBookDriver extends AbstractBookDriver { |
54 | |
|
55 | |
|
56 | |
|
57 | 0 | public SwordBookDriver() { |
58 | 0 | } |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
public String getDriverName() { |
64 | 0 | return "Sword"; |
65 | |
} |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
public Book[] getBooks() { |
71 | 0 | File[] dirs = SwordBookPath.getSwordPath(); |
72 | |
|
73 | |
|
74 | 0 | Set<Book> valid = new HashSet<Book>(dirs.length + dirs.length / 3); |
75 | 0 | for (int j = 0; j < dirs.length; j++) { |
76 | 0 | getBooks(valid, dirs[j]); |
77 | |
} |
78 | 0 | return valid.toArray(new Book[valid.size()]); |
79 | |
} |
80 | |
|
81 | |
private void getBooks(Set<Book> valid, File bookDir) { |
82 | 0 | File mods = new File(bookDir, SwordConstants.DIR_CONF); |
83 | 0 | if (!mods.isDirectory()) { |
84 | 0 | LOGGER.debug("mods.d directory at {} does not exist", mods); |
85 | 0 | return; |
86 | |
} |
87 | |
|
88 | 0 | String[] bookConfs = SwordBookPath.getBookList(mods); |
89 | |
|
90 | |
|
91 | 0 | URI bookDirURI = NetUtil.getURI(bookDir); |
92 | 0 | for (int i = 0; i < bookConfs.length; i++) { |
93 | 0 | String bookConf = bookConfs[i]; |
94 | |
try { |
95 | 0 | SwordBookMetaData sbmd = null; |
96 | |
|
97 | 0 | File configfile = new File(mods, bookConf); |
98 | 0 | if (configfile.exists()) { |
99 | |
|
100 | 0 | sbmd = new SwordBookMetaData(configfile, bookDirURI); |
101 | |
} |
102 | |
|
103 | 0 | if (sbmd == null) { |
104 | 0 | LOGGER.error("The book's configuration files is not supported."); |
105 | 0 | continue; |
106 | |
} |
107 | |
|
108 | |
|
109 | 0 | if (!sbmd.isSupported()) { |
110 | 0 | LOGGER.error("The book's configuration files is not supported. -> Initials [{}], Driver=[{}], Versification=[{}], Book type=[{}], Book category=[{}]", |
111 | |
sbmd.getInitials(), sbmd.getDriver(), sbmd.getProperty(BookMetaData.KEY_VERSIFICATION), sbmd.getBookType(), sbmd.getBookCategory()); |
112 | 0 | continue; |
113 | |
} |
114 | |
|
115 | 0 | sbmd.setDriver(this); |
116 | |
|
117 | |
|
118 | 0 | Book book = createBook(sbmd); |
119 | 0 | if (!valid.contains(book)) { |
120 | 0 | valid.add(book); |
121 | |
|
122 | 0 | IndexManager imanager = IndexManagerFactory.getIndexManager(); |
123 | 0 | if (imanager.isIndexed(book)) { |
124 | 0 | sbmd.setIndexStatus(IndexStatus.DONE); |
125 | |
} else { |
126 | 0 | sbmd.setIndexStatus(IndexStatus.UNDONE); |
127 | |
} |
128 | |
} |
129 | 0 | } catch (IOException e) { |
130 | 0 | LOGGER.warn("Couldn't create SwordBookMetaData", e); |
131 | 0 | } catch (BookException e) { |
132 | 0 | LOGGER.warn("Couldn't create SwordBookMetaData", e); |
133 | 0 | } |
134 | |
} |
135 | 0 | } |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
@Override |
141 | |
public boolean isDeletable(Book dead) { |
142 | 0 | SwordBookMetaData sbmd = (SwordBookMetaData) dead.getBookMetaData(); |
143 | 0 | File confFile = sbmd.getConfigFile(); |
144 | |
|
145 | 0 | return confFile != null && confFile.exists(); |
146 | |
} |
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
@Override |
152 | |
public void delete(Book dead) throws BookException { |
153 | 0 | SwordBookMetaData sbmd = (SwordBookMetaData) dead.getBookMetaData(); |
154 | 0 | File confFile = sbmd.getConfigFile(); |
155 | |
|
156 | |
|
157 | 0 | if (confFile == null || !confFile.exists()) { |
158 | |
|
159 | |
|
160 | 0 | throw new BookException(JSMsg.gettext("Unable to delete: {0}", confFile)); |
161 | |
} |
162 | |
|
163 | |
|
164 | 0 | List<File> failures = FileUtil.delete(confFile); |
165 | 0 | if (failures.isEmpty()) { |
166 | 0 | URI loc = sbmd.getLocation(); |
167 | 0 | if (loc != null) { |
168 | 0 | File bookDir = new File(loc.getPath()); |
169 | 0 | failures = FileUtil.delete(bookDir); |
170 | 0 | Books.installed().removeBook(dead); |
171 | |
} |
172 | |
|
173 | |
} |
174 | |
|
175 | |
|
176 | 0 | if (!failures.isEmpty()) { |
177 | |
|
178 | |
|
179 | 0 | throw new BookException(JSMsg.gettext("Unable to delete: {0}", failures.get(0))); |
180 | |
} |
181 | 0 | } |
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
public static BookDriver instance() { |
189 | 0 | return INSTANCE; |
190 | |
} |
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
public static void registerNewBook(SwordBookMetaData sbmd) throws BookException { |
201 | 0 | BookDriver[] drivers = Books.installed().getDriversByClass(SwordBookDriver.class); |
202 | 0 | for (int i = 0; i < drivers.length; i++) { |
203 | 0 | SwordBookDriver sdriver = (SwordBookDriver) drivers[i]; |
204 | 0 | Book book = sdriver.createBook(sbmd); |
205 | 0 | Books.installed().addBook(book); |
206 | |
} |
207 | 0 | } |
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
private Book createBook(SwordBookMetaData sbmd) throws BookException { |
213 | 0 | BookType modtype = sbmd.getBookType(); |
214 | 0 | if (modtype == null || modtype.getBookCategory() == null) { |
215 | |
|
216 | 0 | throw new BookException(JSOtherMsg.lookupText("Unsupported type: {0} when reading {1}")); |
217 | |
} |
218 | |
|
219 | 0 | return modtype.createBook(sbmd); |
220 | |
} |
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | 0 | private static final BookDriver INSTANCE = new SwordBookDriver(); |
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | 0 | private static final Logger LOGGER = LoggerFactory.getLogger(SwordBookDriver.class); |
231 | |
} |