Coverage Report - org.crosswire.jsword.book.sword.SwordBookDriver
 
Classes in this File Line Coverage Branch Coverage Complexity
SwordBookDriver
0%
0/70
0%
0/36
3.556
 
 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  0
     public SwordBookDriver() {
 58  0
     }
 59  
 
 60  
     /* (non-Javadoc)
 61  
      * @see org.crosswire.jsword.book.BookDriver#getDriverName()
 62  
      */
 63  
     public String getDriverName() {
 64  0
         return "Sword";
 65  
     }
 66  
 
 67  
     /* (non-Javadoc)
 68  
      * @see org.crosswire.jsword.book.BookProvider#getBooks()
 69  
      */
 70  
     public Book[] getBooks() {
 71  0
         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  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  
         // Loop through the entries in this mods.d directory
 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  
                     // First time here chain is null, indicating that we are at the master BookMetaData
 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  
                 // skip any book that is not supported.
 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  
                 // Only take the first "installation" of the Book
 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  
     /* (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  0
         SwordBookMetaData sbmd = (SwordBookMetaData) dead.getBookMetaData();
 143  0
         File confFile = sbmd.getConfigFile();
 144  
         // We can only uninstall what we download into our download dir.
 145  0
         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  0
         SwordBookMetaData sbmd = (SwordBookMetaData) dead.getBookMetaData();
 154  0
         File confFile = sbmd.getConfigFile();
 155  
 
 156  
         // We can only uninstall what we download into our download dir.
 157  0
         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  0
             throw new BookException(JSMsg.gettext("Unable to delete: {0}", confFile));
 161  
         }
 162  
 
 163  
         // Delete the conf
 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  
         // TODO(DM): list all that failed
 176  0
         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  0
             throw new BookException(JSMsg.gettext("Unable to delete: {0}", failures.get(0)));
 180  
         }
 181  0
     }
 182  
 
 183  
     /**
 184  
      * Get the singleton instance of this driver.
 185  
      * 
 186  
      * @return this driver instance
 187  
      */
 188  
     public static BookDriver instance() {
 189  0
         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  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  
      * Create a Book appropriate for the BookMetaData
 211  
      */
 212  
     private Book createBook(SwordBookMetaData sbmd) throws BookException {
 213  0
         BookType modtype = sbmd.getBookType();
 214  0
         if (modtype == null || modtype.getBookCategory() == null) {
 215  
             // FIXME(DMS): missing parameter
 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  
      * A shared instance of this driver.
 224  
      */
 225  0
     private static final BookDriver INSTANCE = new SwordBookDriver();
 226  
 
 227  
     /**
 228  
      * The log stream
 229  
      */
 230  0
     private static final Logger LOGGER = LoggerFactory.getLogger(SwordBookDriver.class);
 231  
 }