1   /**
2    * Distribution License:
3    * JSword is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU Lesser General Public License, version 2.1 or later
5    * as published by the Free Software Foundation. This program is distributed
6    * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
7    * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8    * See the GNU Lesser General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *      http://www.gnu.org/copyleft/lgpl.html
12   * or by writing to:
13   *      Free Software Foundation, Inc.
14   *      59 Temple Place - Suite 330
15   *      Boston, MA 02111-1307, USA
16   *
17   * © CrossWire Bible Society, 2005 - 2016
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   * This represents all of the Sword Books (aka modules).
47   *
48   * @author Joe Walker
49   * @author DM Smith
50   * @see gnu.lgpl.License The GNU Lesser General Public License for details.<br>
51   * The copyright to this program is held by its authors.
52   */
53  public class SwordBookDriver extends AbstractBookDriver {
54      /**
55       * Some basic name initialization
56       */
57      public SwordBookDriver() {
58      }
59  
60      /* (non-Javadoc)
61       * @see org.crosswire.jsword.book.BookDriver#getDriverName()
62       */
63      public String getDriverName() {
64          return "Sword";
65      }
66  
67      /* (non-Javadoc)
68       * @see org.crosswire.jsword.book.BookProvider#getBooks()
69       */
70      public Book[] getBooks() {
71          File[] dirs = SwordBookPath.getSwordPath();
72          //initial size based on Guava's  newHashMapWithExpectedSize method:
73          //http://docs.guava-libraries.googlecode.com/git/javadoc/src-html/com/google/common/collect/Maps.html#line.201
74          Set<Book> valid = new HashSet<Book>(dirs.length + dirs.length / 3);
75          for (int j = 0; j < dirs.length; j++) {
76              getBooks(valid, dirs[j]);
77          }
78          return valid.toArray(new Book[valid.size()]);
79      }
80  
81      private void getBooks(Set<Book> valid, File bookDir) {
82          File mods = new File(bookDir, SwordConstants.DIR_CONF);
83          if (!mods.isDirectory()) {
84              LOGGER.debug("mods.d directory at {} does not exist", mods);
85              return;
86          }
87  
88          String[] bookConfs = SwordBookPath.getBookList(mods);
89  
90          // Loop through the entries in this mods.d directory
91          URI bookDirURI = NetUtil.getURI(bookDir);
92          for (int i = 0; i < bookConfs.length; i++) {
93              String bookConf = bookConfs[i];
94              try {
95                  SwordBookMetaData sbmd = null;
96  
97                  File configfile = new File(mods, bookConf);
98                  if (configfile.exists()) {
99                      // First time here chain is null, indicating that we are at the master BookMetaData
100                     sbmd = new SwordBookMetaData(configfile, bookDirURI);
101                 }
102 
103                 if (sbmd == null) {
104                     LOGGER.error("The book's configuration files is not supported.");
105                     continue;
106                 }
107 
108                 // skip any book that is not supported.
109                 if (!sbmd.isSupported()) {
110                     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                     continue;
113                 }
114 
115                 sbmd.setDriver(this);
116 
117                 // Only take the first "installation" of the Book
118                 Book book = createBook(sbmd);
119                 if (!valid.contains(book)) {
120                     valid.add(book);
121 
122                     IndexManager imanager = IndexManagerFactory.getIndexManager();
123                     if (imanager.isIndexed(book)) {
124                         sbmd.setIndexStatus(IndexStatus.DONE);
125                     } else {
126                         sbmd.setIndexStatus(IndexStatus.UNDONE);
127                     }
128                 }
129             } catch (IOException e) {
130                 LOGGER.warn("Couldn't create SwordBookMetaData", e);
131             } catch (BookException e) {
132                 LOGGER.warn("Couldn't create SwordBookMetaData", e);
133             }
134         }
135     }
136 
137     /* (non-Javadoc)
138      * @see org.crosswire.jsword.book.basic.AbstractBookDriver#isDeletable(org.crosswire.jsword.book.Book)
139      */
140     @Override
141     public boolean isDeletable(Book dead) {
142         SwordBookMetaData sbmd = (SwordBookMetaData) dead.getBookMetaData();
143         File confFile = sbmd.getConfigFile();
144         // We can only uninstall what we download into our download dir.
145         return confFile != null && confFile.exists();
146     }
147 
148     /* (non-Javadoc)
149      * @see org.crosswire.jsword.book.basic.AbstractBookDriver#delete(org.crosswire.jsword.book.Book)
150      */
151     @Override
152     public void delete(Book dead) throws BookException {
153         SwordBookMetaData sbmd = (SwordBookMetaData) dead.getBookMetaData();
154         File confFile = sbmd.getConfigFile();
155 
156         // We can only uninstall what we download into our download dir.
157         if (confFile == null || !confFile.exists()) {
158             // TRANSLATOR: Common error condition: The file could not be deleted. There can be many reasons.
159             // {0} is a placeholder for the file.
160             throw new BookException(JSMsg.gettext("Unable to delete: {0}", confFile));
161         }
162 
163         // Delete the conf
164         List<File> failures = FileUtil.delete(confFile);
165         if (failures.isEmpty()) {
166             URI loc = sbmd.getLocation();
167             if (loc != null) {
168                 File bookDir = new File(loc.getPath());
169                 failures = FileUtil.delete(bookDir);
170                 Books.installed().removeBook(dead);
171             }
172 
173         }
174 
175         // TODO(DM): list all that failed
176         if (!failures.isEmpty()) {
177             // TRANSLATOR: Common error condition: The file could not be deleted. There can be many reasons.
178             // {0} is a placeholder for the file.
179             throw new BookException(JSMsg.gettext("Unable to delete: {0}", failures.get(0)));
180         }
181     }
182 
183     /**
184      * Get the singleton instance of this driver.
185      * 
186      * @return this driver instance
187      */
188     public static BookDriver instance() {
189         return INSTANCE;
190     }
191 
192     /**
193      * A helper class for the SwordInstaller to tell us that it has copied a new
194      * Book into our install directory
195      * 
196      * @param sbmd
197      *            The SwordBookMetaData object for the new Book
198      * @throws BookException
199      */
200     public static void registerNewBook(SwordBookMetaData sbmd) throws BookException {
201         BookDriver[] drivers = Books.installed().getDriversByClass(SwordBookDriver.class);
202         for (int i = 0; i < drivers.length; i++) {
203             SwordBookDriver sdriver = (SwordBookDriver) drivers[i];
204             Book book = sdriver.createBook(sbmd);
205             Books.installed().addBook(book);
206         }
207     }
208 
209     /**
210      * Create a Book appropriate for the BookMetaData
211      */
212     private Book createBook(SwordBookMetaData sbmd) throws BookException {
213         BookType modtype = sbmd.getBookType();
214         if (modtype == null || modtype.getBookCategory() == null) {
215             // FIXME(DMS): missing parameter
216             throw new BookException(JSOtherMsg.lookupText("Unsupported type: {0} when reading {1}"));
217         }
218 
219         return modtype.createBook(sbmd);
220     }
221 
222     /**
223      * A shared instance of this driver.
224      */
225     private static final BookDriver INSTANCE = new SwordBookDriver();
226 
227     /**
228      * The log stream
229      */
230     private static final Logger LOGGER = LoggerFactory.getLogger(SwordBookDriver.class);
231 }
232