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.readings;
21  
22  import java.io.IOException;
23  import java.net.URL;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.crosswire.common.util.NetUtil;
28  import org.crosswire.common.util.ResourceUtil;
29  import org.crosswire.common.util.URIFilter;
30  import org.crosswire.jsword.book.Book;
31  import org.crosswire.jsword.book.BookCategory;
32  import org.crosswire.jsword.book.BookDriver;
33  import org.crosswire.jsword.book.basic.AbstractBookDriver;
34  
35  /**
36   * A driver for the readings dictionary.
37   * 
38   * @see gnu.lgpl.License The GNU Lesser General Public License for details.
39   * @author Joe Walker
40   */
41  public class ReadingsBookDriver extends AbstractBookDriver {
42      /**
43       * Setup the array of BookMetaDatas
44       */
45      public ReadingsBookDriver() {
46          List<Book> bookList = new ArrayList<Book>();
47          String[] installedBooks = getInstalledReadingsSets();
48          for (int i = 0; i < installedBooks.length; i++) {
49              bookList.add(new ReadingsBook(this, installedBooks[i], BookCategory.DAILY_DEVOTIONS));
50          }
51  
52          books = bookList.toArray(new Book[bookList.size()]);
53      }
54  
55      /*
56       * (non-Javadoc)
57       * 
58       * @see org.crosswire.jsword.book.BookDriver#getBooks()
59       */
60      public Book[] getBooks() {
61          return books == null ? null : (Book[]) books.clone();
62      }
63  
64      /*
65       * (non-Javadoc)
66       * 
67       * @see org.crosswire.jsword.book.BookDriver#getDriverName()
68       */
69      public String getDriverName() {
70          return "Readings";
71      }
72  
73      /**
74       * Get the singleton instance of this driver.
75       * 
76       * @return this driver instance
77       */
78      public static BookDriver instance() {
79          return INSTANCE;
80      }
81  
82      /**
83       * Get a list of the available readings sets
84       *
85       * @return all the installed reading sets
86       */
87      public String[] getInstalledReadingsSets() {
88          try {
89              URL index = ResourceUtil.getResource(ReadingsBookDriver.class, "readings.txt");
90              return NetUtil.listByIndexFile(NetUtil.toURI(index), new ReadingsFilter());
91          } catch (IOException ex) {
92              return new String[0];
93          }
94      }
95  
96      /**
97       * Get all files mentioned by readings.txt
98       */
99      static final class ReadingsFilter implements URIFilter {
100         public boolean accept(String name) {
101             return true;
102         }
103     }
104 
105     /**
106      * The meta data array
107      */
108     private Book[] books;
109 
110     /**
111      * Resources subdir for readings sets
112      */
113     public static final String DIR_READINGS = "readings";
114 
115     /**
116      * A shared instance of this driver.
117      */
118     private static final BookDriver INSTANCE = new ReadingsBookDriver();
119 }
120