Coverage Report - org.crosswire.jsword.bridge.BookLookup
 
Classes in this File Line Coverage Branch Coverage Complexity
BookLookup
0%
0/42
0%
0/14
3.75
 
 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.BookException;
 24  
 import org.crosswire.jsword.book.Books;
 25  
 import org.crosswire.jsword.passage.Key;
 26  
 import org.crosswire.jsword.passage.NoSuchKeyException;
 27  
 
 28  
 /**
 29  
  * BookLookup allows one to lookup via keys. Note: it does not work with
 30  
  * GenBook.
 31  
  * 
 32  
  * @see gnu.lgpl.License The GNU Lesser General Public License for details.
 33  
  * @author DM Smith
 34  
  */
 35  
 public class BookLookup {
 36  
 
 37  0
     public BookLookup(Book book) {
 38  0
         this.book = book;
 39  0
     }
 40  
 
 41  
     public String locate(Key key) throws BookException {
 42  0
         StringBuilder buf = new StringBuilder();
 43  
 
 44  
         //FIXME(iteration should be pushed down for performanace gains
 45  0
         for (Key currentKey : key) {
 46  0
             String osisID = currentKey.getOsisID();
 47  0
             if (buf.length() > 0) {
 48  0
                 buf.append('\n');
 49  
             }
 50  0
             buf.append(book.getInitials());
 51  0
             buf.append(':');
 52  0
             buf.append(osisID);
 53  0
             buf.append(" - ");
 54  0
             String rawText = book.getRawText(currentKey);
 55  0
             if (rawText != null && rawText.trim().length() > 0) {
 56  0
                 buf.append(rawText);
 57  
             } else {
 58  0
                 buf.append("Not found");
 59  
             }
 60  0
         }
 61  
 
 62  0
         return buf.toString();
 63  
     }
 64  
 
 65  
     private Book book;
 66  
 
 67  
     /**
 68  
      * Call with book, key. And book is the initials of a book, e.g. KJV. And
 69  
      * key is a reference for the work, e.g. "Genesis 1:5-ff". Note: if the key
 70  
      * has spaces, it must be quoted.
 71  
      * 
 72  
      * @param args
 73  
      */
 74  
     public static void main(String[] args) {
 75  0
         if (args.length != 2) {
 76  0
             usage();
 77  0
             return;
 78  
         }
 79  
 
 80  0
         System.err.print("BookLookup");
 81  0
         for (int i = 0; i < args.length; i++) {
 82  0
             System.err.print(' ');
 83  0
             System.err.print(args[i]);
 84  
         }
 85  0
         System.err.print('\n');
 86  
 
 87  0
         Book b = Books.installed().getBook(args[0]);
 88  0
         if (b == null) {
 89  0
             System.err.println("Book not found");
 90  0
             return;
 91  
         }
 92  
 
 93  0
         BookLookup lookup = new BookLookup(b);
 94  
         try {
 95  0
             System.out.println(lookup.locate(b.getKey(args[1])));
 96  0
         } catch (BookException e) {
 97  0
             System.err.println("Error while doing lookup");
 98  0
             e.printStackTrace();
 99  0
         } catch (NoSuchKeyException e) {
 100  0
             System.err.println("Error while doing lookup");
 101  0
             e.printStackTrace();
 102  0
         }
 103  0
     }
 104  
 
 105  
     public static void usage() {
 106  0
         System.err.println("Usage: BookLookup book key");
 107  0
     }
 108  
 }