Coverage Report - org.crosswire.jsword.examples.StrongsAnalysis
 
Classes in this File Line Coverage Branch Coverage Complexity
StrongsAnalysis
0%
0/47
0%
0/20
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, 2007 - 2016
 18  
  *
 19  
  */
 20  
 package org.crosswire.jsword.examples;
 21  
 
 22  
 import java.util.ArrayList;
 23  
 import java.util.List;
 24  
 import java.util.regex.Matcher;
 25  
 import java.util.regex.Pattern;
 26  
 
 27  
 import org.crosswire.jsword.book.Book;
 28  
 import org.crosswire.jsword.book.BookData;
 29  
 import org.crosswire.jsword.book.BookException;
 30  
 import org.crosswire.jsword.book.BookFilters;
 31  
 import org.crosswire.jsword.book.Books;
 32  
 import org.crosswire.jsword.book.FeatureType;
 33  
 import org.crosswire.jsword.book.OSISUtil;
 34  
 import org.crosswire.jsword.book.study.StrongsMapSet;
 35  
 import org.crosswire.jsword.book.study.StrongsNumber;
 36  
 import org.crosswire.jsword.passage.Key;
 37  
 import org.jdom2.Content;
 38  
 import org.jdom2.Element;
 39  
 
 40  
 /**
 41  
  * Analyze Strong's Numbers in a module.
 42  
  * 
 43  
  * @see gnu.lgpl.License The GNU Lesser General Public License for details.
 44  
  * @author DM Smith
 45  
  */
 46  
 public class StrongsAnalysis {
 47  
     /**
 48  
      *
 49  
      */
 50  0
     public StrongsAnalysis() {
 51  0
         Book bible = Books.installed().getBook("KJV");
 52  0
         if (!bible.hasFeature(FeatureType.STRONGS_NUMBERS)) {
 53  0
             bible = null;
 54  0
             List<Book> bibles = Books.installed().getBooks(new BookFilters.BookFeatureFilter(FeatureType.STRONGS_NUMBERS));
 55  
 
 56  0
             if (!bibles.isEmpty()) {
 57  0
                 bible = bibles.get(0);
 58  
             }
 59  
         }
 60  
 
 61  0
         if (bible == null) {
 62  0
             return;
 63  
         }
 64  
 
 65  0
         List<Key> errors = new ArrayList<Key>();
 66  0
         StrongsMapSet sms = new StrongsMapSet();
 67  0
         analyze(sms, bible, errors, bible.getGlobalKeyList());
 68  0
     }
 69  
 
 70  
     /**
 71  
      * @param sms
 72  
      * @param book
 73  
      * @param errors
 74  
      * @param wholeBible
 75  
      */
 76  
     public void analyze(StrongsMapSet sms, Book book, List<Key> errors, Key wholeBible) {
 77  0
         BookData data = null;
 78  0
         Element osis = null;
 79  0
         StringBuilder buffer = new StringBuilder();
 80  0
         for (Key subkey : wholeBible) {
 81  0
             if (subkey.canHaveChildren()) {
 82  0
                 analyze(sms, book, errors, subkey);
 83  
             } else {
 84  0
                 data = new BookData(book, subkey);
 85  0
                 osis = null;
 86  
 
 87  
                 try {
 88  0
                     osis = data.getOsisFragment();
 89  0
                 } catch (BookException e) {
 90  0
                     errors.add(subkey);
 91  0
                     continue;
 92  0
                 }
 93  
 
 94  
                 // Do the actual indexing
 95  0
                 for (Content content : OSISUtil.getDeepContent(osis, OSISUtil.OSIS_ELEMENT_W)) {
 96  
                     // Clear out the buffer for re-use
 97  0
                     int len = buffer.length();
 98  0
                     if (len > 0) {
 99  0
                         buffer.delete(0, len);
 100  
                     }
 101  
 
 102  0
                     Element wElement = (Element) content;
 103  0
                     String snAttr = wElement.getAttributeValue(OSISUtil.ATTRIBUTE_W_LEMMA);
 104  
 
 105  0
                     String text = OSISUtil.getPlainText(wElement);
 106  
 
 107  0
                     Matcher matcher = strongsNumberPattern.matcher(snAttr);
 108  0
                     while (matcher.find()) {
 109  0
                         StrongsNumber strongsNumber = new StrongsNumber(matcher.group(1));
 110  0
                         if (strongsNumber.isValid()) {
 111  0
                             if (buffer.length() > 0) {
 112  0
                                 buffer.append(' ');
 113  
                             }
 114  0
                             buffer.append(strongsNumber.getStrongsNumber());
 115  
                         }
 116  0
                     }
 117  
 
 118  
                     // now we can actually store the mapping
 119  0
                     sms.add(buffer.toString(), text);
 120  0
                 }
 121  
             }
 122  
         }
 123  0
     }
 124  
 
 125  
     /**
 126  
      * @param args
 127  
      */
 128  
     public static void main(String[] args) {
 129  0
         new StrongsAnalysis();
 130  0
     }
 131  
 
 132  0
     private static Pattern strongsNumberPattern = Pattern.compile("strong:([GH][0-9]+)");
 133  
 }