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 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * 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   * Copyright: 2008
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: BookIndexer.java 1466 2007-07-02 02:48:09Z dmsmith $
21   */
22  package org.crosswire.jsword.bridge;
23  
24  import org.crosswire.jsword.book.Book;
25  import org.crosswire.jsword.book.BookException;
26  import org.crosswire.jsword.book.Books;
27  import org.crosswire.jsword.passage.Key;
28  import org.crosswire.jsword.passage.NoSuchKeyException;
29  
30  /**
31   * BookLookup allows one to lookup via keys. Note: it does not work with
32   * GenBook.
33   * 
34   * @see gnu.lgpl.License for license details.<br>
35   *      The copyright to this program is held by it's authors.
36   * @author DM Smith [dmsmith555 at yahoo dot com]
37   */
38  public class BookLookup {
39  
40      public BookLookup(Book book) {
41          this.book = book;
42      }
43  
44      public String locate(Key key) throws BookException {
45          StringBuilder buf = new StringBuilder();
46  
47          for (Key currentKey : key) {
48              String osisID = currentKey.getOsisID();
49              if (buf.length() > 0) {
50                  buf.append('\n');
51              }
52              buf.append(book.getInitials());
53              buf.append(':');
54              buf.append(osisID);
55              buf.append(" - ");
56              String rawText = book.getRawText(currentKey);
57              if (rawText != null && rawText.trim().length() > 0) {
58                  buf.append(rawText);
59              } else {
60                  buf.append("Not found");
61              }
62          }
63  
64          return buf.toString();
65      }
66  
67      private Book book;
68  
69      /**
70       * Call with book, key. And book is the initials of a book, e.g. KJV. And
71       * key is a reference for the work, e.g. "Genesis 1:5-ff". Note: if the key
72       * has spaces, it must be quoted.
73       * 
74       * @param args
75       */
76      public static void main(String[] args) {
77          if (args.length != 2) {
78              usage();
79              return;
80          }
81  
82          System.err.print("BookLookup");
83          for (int i = 0; i < args.length; i++) {
84              System.err.print(' ');
85              System.err.print(args[i]);
86          }
87          System.err.print('\n');
88  
89          Book b = Books.installed().getBook(args[0]);
90          if (b == null) {
91              System.err.println("Book not found");
92              return;
93          }
94  
95          BookLookup lookup = new BookLookup(b);
96          try {
97              System.out.println(lookup.locate(b.getKey(args[1])));
98          } catch (BookException e) {
99              System.err.println("Error while doing lookup");
100             e.printStackTrace();
101         } catch (NoSuchKeyException e) {
102             System.err.println("Error while doing lookup");
103             e.printStackTrace();
104         }
105     }
106 
107     public static void usage() {
108         System.err.println("Usage: BookLookup book key");
109     }
110 }
111