1
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
49 public class StrongsAnalysis {
50
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
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 for (Content content : OSISUtil.getDeepContent(osis, OSISUtil.OSIS_ELEMENT_W)) {
99 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 sms.add(buffer.toString(), text);
126 }
127 }
128 }
129 }
130
131
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