Coverage Report - org.crosswire.jsword.examples.GatherAllReferences
 
Classes in this File Line Coverage Branch Coverage Complexity
GatherAllReferences
0%
0/50
0%
0/24
4.5
 
 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.examples;
 21  
 
 22  
 import java.io.BufferedWriter;
 23  
 import java.io.FileWriter;
 24  
 import java.io.IOException;
 25  
 import java.io.PrintWriter;
 26  
 import java.util.List;
 27  
 import java.util.regex.Matcher;
 28  
 import java.util.regex.Pattern;
 29  
 
 30  
 import org.crosswire.jsword.book.Book;
 31  
 import org.crosswire.jsword.book.BookException;
 32  
 import org.crosswire.jsword.book.BookFilter;
 33  
 import org.crosswire.jsword.book.BookFilters;
 34  
 import org.crosswire.jsword.book.BookMetaData;
 35  
 import org.crosswire.jsword.book.Books;
 36  
 import org.crosswire.jsword.passage.Key;
 37  
 import org.crosswire.jsword.passage.TreeKey;
 38  
 import org.slf4j.Logger;
 39  
 import org.slf4j.LoggerFactory;
 40  
 
 41  
 /**
 42  
  * Gather all references.
 43  
  * 
 44  
  * @see gnu.lgpl.License The GNU Lesser General Public License for details.
 45  
  * @author DM Smith
 46  
  */
 47  
 public final class GatherAllReferences {
 48  
     /**
 49  
      * Prevent instantiation
 50  
      */
 51  0
     private GatherAllReferences() {
 52  0
     }
 53  
 
 54  
     /**
 55  
      * Read all the books that we can get our hands on.
 56  
      * 
 57  
      * @param args 
 58  
      * @throws IOException 
 59  
      */
 60  
     public static void main(String[] args) throws IOException {
 61  0
         out = new PrintWriter(new BufferedWriter(new FileWriter("passages.log")));
 62  
         // Loop through all the Books
 63  0
         log.warn("*** Reading all known Books");
 64  0
         BookFilter filter = BookFilters.getCustom("GlobalOptionFilter=ThMLScripref;Category=Biblical Texts");
 65  0
         List<Book> comments = Books.installed().getBooks(filter);
 66  0
         for (Book book : comments) {
 67  
 
 68  0
             if (!book.isLocked()) {
 69  0
                 BookMetaData bmd = book.getBookMetaData();
 70  
                 // Skip PlainText as they do not have references marked up
 71  0
                 if (bmd.getProperty("SourceType") != null) {
 72  0
                     Key set = book.getGlobalKeyList();
 73  
 
 74  0
                     readBook(book, set);
 75  
                 }
 76  0
             }
 77  
         }
 78  0
         out.flush();
 79  0
         out.close();
 80  0
     }
 81  
 
 82  
     /**
 83  
      * Perform a test read on an iterator over a set of keys
 84  
      */
 85  
     private static void readBook(Book book, Key set) {
 86  0
         int[] stats = new int[] {
 87  
                 0, 0
 88  
         };
 89  
 
 90  0
         boolean first = true;
 91  0
         for (Key key : set) {
 92  
             // skip the root of a TreeKey as it often is not addressable.
 93  0
             if (first) {
 94  0
                 first = false;
 95  0
                 if (key instanceof TreeKey && key.getName().length() == 0) {
 96  0
                     continue;
 97  
                 }
 98  
             }
 99  0
             readKey(book, key, stats);
 100  
         }
 101  0
         log.warn(book.getInitials() + ':' + stats[0] + ':' + stats[1]);
 102  
 
 103  0
     }
 104  
 
 105  
     /**
 106  
      * Perform a test read on a single key
 107  
      */
 108  
     private static void readKey(Book book, Key key, int[] stats) {
 109  
         String orig;
 110  
         try {
 111  0
             orig = book.getRawText(key);
 112  0
         } catch (BookException ex) {
 113  0
             log.warn("Failed to read: {}({}):{}", book.getInitials(), key.getOsisID(), ex.getMessage(), ex);
 114  0
             return;
 115  0
         }
 116  
 
 117  0
         Matcher matcher = null;
 118  0
         if (orig.indexOf("passage=\"") != -1) {
 119  0
             matcher = thmlPassagePattern.matcher(orig);
 120  0
         } else if (orig.indexOf("osisRef=\"") != -1) {
 121  0
             matcher = osisPassagePattern.matcher(orig);
 122  0
         } else if (orig.indexOf("<RX>") != -1) {
 123  0
             matcher = gbfPassagePattern.matcher(orig);
 124  
         }
 125  
 
 126  0
         if (matcher != null) {
 127  0
             while (matcher.find()) {
 128  0
                 String rawRef = matcher.group(2);
 129  0
                 stats[0]++;
 130  0
                 String message = book.getInitials() + ':' + key.getOsisRef() + '/' + rawRef;
 131  
                 /*
 132  
                     try {
 133  
                         Key ref = book.getKey(rawRef);
 134  
                         message += '/' + ref.getOsisRef();
 135  
                     } catch (NoSuchKeyException e) {
 136  
                         message += '!' + e.getMessage();
 137  
                         stats[1]++;
 138  
                     }
 139  
                  */
 140  
 
 141  0
                 out.println(message);
 142  0
             }
 143  
         }
 144  0
     }
 145  
 
 146  0
     private static Pattern thmlPassagePattern = Pattern.compile("(osisRef|passage)=\"([^\"]*)");
 147  0
     private static Pattern gbfPassagePattern = Pattern.compile("(<RX>)([^<]*)");
 148  0
     private static Pattern osisPassagePattern = Pattern.compile("(osisRef)=\"([^\"]*)");
 149  
     private static PrintWriter out;
 150  
 
 151  
     /**
 152  
      * The log stream
 153  
      */
 154  0
     private static final Logger log = LoggerFactory.getLogger(GatherAllReferences.class);
 155  
 }