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      public StrongsAnalysis() {
51          Book bible = Books.installed().getBook("KJV");
52          if (!bible.hasFeature(FeatureType.STRONGS_NUMBERS)) {
53              bible = null;
54              List<Book> bibles = Books.installed().getBooks(new BookFilters.BookFeatureFilter(FeatureType.STRONGS_NUMBERS));
55  
56              if (!bibles.isEmpty()) {
57                  bible = bibles.get(0);
58              }
59          }
60  
61          if (bible == null) {
62              return;
63          }
64  
65          List<Key> errors = new ArrayList<Key>();
66          StrongsMapSet sms = new StrongsMapSet();
67          analyze(sms, bible, errors, bible.getGlobalKeyList());
68      }
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          BookData data = null;
78          Element osis = null;
79          StringBuilder buffer = new StringBuilder();
80          for (Key subkey : wholeBible) {
81              if (subkey.canHaveChildren()) {
82                  analyze(sms, book, errors, subkey);
83              } else {
84                  data = new BookData(book, subkey);
85                  osis = null;
86  
87                  try {
88                      osis = data.getOsisFragment();
89                  } catch (BookException e) {
90                      errors.add(subkey);
91                      continue;
92                  }
93  
94                  // Do the actual indexing
95                  for (Content content : OSISUtil.getDeepContent(osis, OSISUtil.OSIS_ELEMENT_W)) {
96                      // Clear out the buffer for re-use
97                      int len = buffer.length();
98                      if (len > 0) {
99                          buffer.delete(0, len);
100                     }
101 
102                     Element wElement = (Element) content;
103                     String snAttr = wElement.getAttributeValue(OSISUtil.ATTRIBUTE_W_LEMMA);
104 
105                     String text = OSISUtil.getPlainText(wElement);
106 
107                     Matcher matcher = strongsNumberPattern.matcher(snAttr);
108                     while (matcher.find()) {
109                         StrongsNumber strongsNumber = new StrongsNumber(matcher.group(1));
110                         if (strongsNumber.isValid()) {
111                             if (buffer.length() > 0) {
112                                 buffer.append(' ');
113                             }
114                             buffer.append(strongsNumber.getStrongsNumber());
115                         }
116                     }
117 
118                     // now we can actually store the mapping
119                     sms.add(buffer.toString(), text);
120                 }
121             }
122         }
123     }
124 
125     /**
126      * @param args
127      */
128     public static void main(String[] args) {
129         new StrongsAnalysis();
130     }
131 
132     private static Pattern strongsNumberPattern = Pattern.compile("strong:([GH][0-9]+)");
133 }
134