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