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, 2008 - 2016
18   *
19   */
20  package org.crosswire.jsword.bridge;
21  
22  import org.crosswire.jsword.book.Book;
23  import org.crosswire.jsword.book.BookCategory;
24  import org.crosswire.jsword.book.BookException;
25  import org.crosswire.jsword.book.Books;
26  import org.crosswire.jsword.passage.Key;
27  import org.crosswire.jsword.versification.BookName;
28  
29  /**
30   * Exports the Book in SWORD's imp format. This is identical to SWORD's mod2imp.
31   * Note: it does not work with GenBook.
32   * 
33   * @see gnu.lgpl.License The GNU Lesser General Public License for details.
34   * @author DM Smith
35   */
36  public class BookExporter {
37  
38      public BookExporter(Book book) {
39          this.book = book;
40      }
41  
42      public void mod2imp() throws BookException {
43          // Use short key names for Bibles.
44          if (BookCategory.BIBLE.equals(book.getBookCategory())) {
45              BookName.setFullBookName(false);
46          }
47  
48          Key keys = book.getGlobalKeyList();
49  
50          StringBuilder buf = new StringBuilder();
51          for (Key key : keys) {
52              //FIXME(CJB) iteration should be pushed down to benefit from performance improvement
53              String rawText = book.getRawText(key);
54              if (rawText != null && rawText.trim().length() > 0) {
55                  buf.delete(0, buf.length());
56                  buf.append("$$$").append(key).append('\n').append(rawText);
57                  System.out.println(buf.toString());
58              }
59          }
60      }
61  
62      private Book book;
63  
64      /**
65       * Call with <operation> book. Where operation can be one of:
66       * <ul>
67       * <li>check - returns "TRUE" or "FALSE" indicating whether the index exists
68       * or not</li>
69       * <li>create - (re)create the index</li>
70       * <li>delete - delete the index if it exists</li>
71       * </ul>
72       * And book is the initials of a book, e.g. KJV.
73       * 
74       * @param args
75       */
76      public static void main(String[] args) {
77          if (args.length != 1) {
78              usage();
79              return;
80          }
81  
82          System.err.println("BookExporter " + args[0]);
83  
84          Book b = Books.installed().getBook(args[0]);
85          if (b == null) {
86              System.err.println("Book not found");
87              return;
88          }
89  
90          BookExporter exporter = new BookExporter(b);
91          try {
92              exporter.mod2imp();
93          } catch (BookException e) {
94              System.err.println("Error while exporting");
95              e.printStackTrace();
96          }
97      }
98  
99      public static void usage() {
100         System.err.println("Usage: BookExporter book");
101     }
102 }
103