1
21 package org.crosswire.jsword.examples;
22
23 import java.net.URL;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Map;
27
28 import javax.xml.transform.TransformerException;
29
30 import org.crosswire.common.util.NetUtil;
31 import org.crosswire.common.util.ResourceUtil;
32 import org.crosswire.common.xml.Converter;
33 import org.crosswire.common.xml.SAXEventProvider;
34 import org.crosswire.common.xml.TransformingSAXEventProvider;
35 import org.crosswire.common.xml.XMLUtil;
36 import org.crosswire.jsword.book.Book;
37 import org.crosswire.jsword.book.BookCategory;
38 import org.crosswire.jsword.book.BookData;
39 import org.crosswire.jsword.book.BookException;
40 import org.crosswire.jsword.book.BookFilter;
41 import org.crosswire.jsword.book.BookFilters;
42 import org.crosswire.jsword.book.BookMetaData;
43 import org.crosswire.jsword.book.Books;
44 import org.crosswire.jsword.book.BooksEvent;
45 import org.crosswire.jsword.book.BooksListener;
46 import org.crosswire.jsword.book.OSISUtil;
47 import org.crosswire.jsword.book.install.InstallException;
48 import org.crosswire.jsword.book.install.InstallManager;
49 import org.crosswire.jsword.book.install.Installer;
50 import org.crosswire.jsword.index.search.DefaultSearchModifier;
51 import org.crosswire.jsword.index.search.DefaultSearchRequest;
52 import org.crosswire.jsword.passage.Key;
53 import org.crosswire.jsword.passage.NoSuchKeyException;
54 import org.crosswire.jsword.passage.Passage;
55 import org.crosswire.jsword.passage.PassageTally;
56 import org.crosswire.jsword.passage.RestrictionType;
57 import org.crosswire.jsword.util.ConverterFactory;
58 import org.xml.sax.SAXException;
59
60
68 public class APIExamples {
69
72 private static final String BIBLE_NAME = "KJV";
73
74
81 public Book getBook(String bookInitials) {
82 return Books.installed().getBook(bookInitials);
83 }
84
85
94 public String getPlainText(String bookInitials, String reference) throws BookException, NoSuchKeyException {
95 Book book = getBook(bookInitials);
96 if (book == null) {
97 return "";
98 }
99
100 Key key = book.getKey(reference);
101 BookData data = new BookData(book, key);
102 return OSISUtil.getCanonicalText(data.getOsisFragment());
103 }
104
105
114 public SAXEventProvider getOSIS(String bookInitials, String reference, int maxKeyCount) throws BookException, NoSuchKeyException {
115 if (bookInitials == null || reference == null) {
116 return null;
117 }
118
119 Book book = getBook(bookInitials);
120
121 Key key = null;
122 if (BookCategory.BIBLE.equals(book.getBookCategory())) {
123 key = book.getKey(reference);
124 ((Passage) key).trimVerses(maxKeyCount);
125 } else {
126 key = book.createEmptyKeyList();
127
128 int count = 0;
129 for (Key aKey : book.getKey(reference)) {
130 if (++count >= maxKeyCount) {
131 break;
132 }
133 key.addAll(aKey);
134 }
135 }
136
137 BookData data = new BookData(book, key);
138
139 return data.getSAXEventProvider();
140 }
141
142
153 public String readStyledText(String bookInitials, String reference, int maxKeyCount) throws NoSuchKeyException, BookException, TransformerException,
154 SAXException
155 {
156 Book book = getBook(bookInitials);
157 SAXEventProvider osissep = getOSIS(bookInitials, reference, maxKeyCount);
158 if (osissep == null) {
159 return "";
160 }
161
162 Converter styler = ConverterFactory.getConverter();
163
164 TransformingSAXEventProvider htmlsep = (TransformingSAXEventProvider) styler.convert(osissep);
165
166 BookMetaData bmd = book.getBookMetaData();
169 boolean direction = bmd.isLeftToRight();
170 htmlsep.setParameter("direction", direction ? "ltr" : "rtl");
171
172 return XMLUtil.writeToString(htmlsep);
174 }
175
176
183 public void readDictionary() throws BookException {
184 List<Book> dicts = Books.installed().getBooks(BookFilters.getDictionaries());
188 Book dict = dicts.get(0);
189
190 Key keys = dict.getGlobalKeyList();
194 Key first = keys.iterator().next();
195
196 System.out.println("The first Key in the default dictionary is " + first);
197
198 BookData data = new BookData(dict, first);
199 System.out.println("And the text against that key is " + OSISUtil.getPlainText(data.getOsisFragment()));
200 }
201
202
205 public void search() throws BookException {
206 Book bible = Books.installed().getBook(BIBLE_NAME);
207
208 Key key = bible.find("+moses +aaron");
211
212 System.out.println("The following verses contain both moses and aaron: " + key.getName());
213
214 if (key instanceof Passage) {
219 Passage remaining = ((Passage) key).trimVerses(5);
220 System.out.println("The first 5 verses containing both moses and aaron: " + key.getName());
221 if (remaining != null) {
222 System.out.println("The rest of the verses are: " + remaining.getName());
223 } else {
224 System.out.println("There are only 5 verses containing both moses and aaron");
225 }
226 }
227 }
228
229
234 void rankedSearch() throws BookException {
235 Book bible = Books.installed().getBook(BIBLE_NAME);
236
237 boolean rank = true;
240 int max = 20;
241
242 DefaultSearchModifier modifier = new DefaultSearchModifier();
243 modifier.setRanked(rank);
244 modifier.setMaxResults(max);
245
246 Key results = bible.find(new DefaultSearchRequest("for god so loved the world", modifier));
247 int total = results.getCardinality();
248 int partial = total;
249
250 if (results instanceof PassageTally || rank) {
252 PassageTally tally = (PassageTally) results;
253 tally.setOrdering(PassageTally.Order.TALLY);
254 int rankCount = max;
255 if (rankCount > 0 && rankCount < total) {
256 tally.trimRanges(rankCount, RestrictionType.NONE);
259 partial = rankCount;
260 }
261 }
262 System.out.println("Showing the first " + partial + " of " + total + " verses.");
263 System.out.println(results);
264 }
265
266
273 void searchAndShow() throws BookException, SAXException {
274 Book bible = Books.installed().getBook(BIBLE_NAME);
275
276 Key key = bible.find("melchesidec~");
278
279
283 String path = "xsl/cswing/simple.xsl";
285 URL xslurl = ResourceUtil.getResource(path);
286 Iterator<Key> rangeIter = ((Passage) key).rangeIterator(RestrictionType.CHAPTER);
288 while (rangeIter.hasNext()) {
290 Key range = rangeIter.next();
291 BookData data = new BookData(bible, range);
292 SAXEventProvider osissep = data.getSAXEventProvider();
293 SAXEventProvider htmlsep = new TransformingSAXEventProvider(NetUtil.toURI(xslurl), osissep);
294 String text = XMLUtil.writeToString(htmlsep);
295 System.out.println("The html text of " + range.getName() + " is " + text);
296 }
297 }
298
299
306 public void pickBible() {
307 Book book = Books.installed().getBook(BIBLE_NAME);
309
310 System.out.println(book.getLanguage());
312
313 List<Book> books = Books.installed().getBooks();
315 book = books.get(0);
316
317 books = Books.installed().getBooks(BookFilters.getOnlyBibles());
319 book = books.get(0);
320
321
324 List<Book> test = Books.installed().getBooks(new MyBookFilter("ESV"));
327 book = test.get(0);
328
329 if (book != null) {
330 System.out.println(book.getInitials());
331 }
332
333 Books.installed().addBooksListener(new MyBooksListener());
335 }
336
337 public void installBook() {
338 Installer installer = null;
340
341 InstallManager imanager = new InstallManager();
342
343 Map<String, Installer> installers = imanager.getInstallers();
345
346 String name = null;
348 for (Map.Entry<String, Installer> mapEntry : installers.entrySet()) {
349 name = mapEntry.getKey();
350 installer = mapEntry.getValue();
351 System.out.println(name + ": " + installer.getInstallerDefinition());
352 }
353
354 name = "CrossWire";
355 installer = imanager.getInstaller(name);
357
358 try {
360 installer.reloadBookList();
361 } catch (InstallException e) {
362 e.printStackTrace();
363 }
364
365 List<Book> availableBooks = installer.getBooks();
367
368 Book book = availableBooks.get(0);
369 if (book != null) {
370 System.out.println("Book " + book.getInitials() + " is available");
371 }
372
373 availableBooks = installer.getBooks(new MyBookFilter("ESV"));
375
376 book = availableBooks.get(0);
377
378 if (book != null) {
379 System.out.println("Book " + book.getInitials() + " is available");
380
381 try {
385 if (Books.installed().getBook("ESV") != null) {
386 Books.installed().removeBook(book);
389
390 book.getDriver().delete(book);
393 }
394 } catch (BookException e1) {
395 e1.printStackTrace();
396 }
397
398 try {
399 installer.install(book);
401 } catch (InstallException e) {
402 e.printStackTrace();
403 }
404 }
405 }
406
407
410 static class MyBookFilter implements BookFilter {
411 public MyBookFilter(String bookName) {
412 name = bookName;
413 }
414
415 public boolean test(Book bk) {
416 return bk.getInitials().equals(name);
417 }
418
419 private String name;
420 }
421
422
425 static class MyBooksListener implements BooksListener {
426
433 public void bookAdded(BooksEvent ev) {
434 }
435
436
443 public void bookRemoved(BooksEvent ev) {
444 }
445 }
446
447
455 public static void main(String[] args) throws BookException, NoSuchKeyException, TransformerException, SAXException {
456 APIExamples examples = new APIExamples();
457
458 examples.installBook();
459 System.out.println("The plain text of Gen 1:1 is " + examples.getPlainText(BIBLE_NAME, "Gen 1:1"));
460 System.out.println("The html text of Gen 1:1 is " + examples.readStyledText(BIBLE_NAME, "Gen 1:1", 100));
461 examples.readDictionary();
462 examples.search();
463 examples.rankedSearch();
464 examples.searchAndShow();
465 }
466 }
467