1
20 package org.crosswire.jsword.examples;
21
22 import java.net.URL;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Map;
26
27 import javax.xml.transform.TransformerException;
28
29 import org.crosswire.common.util.NetUtil;
30 import org.crosswire.common.util.ResourceUtil;
31 import org.crosswire.common.xml.Converter;
32 import org.crosswire.common.xml.SAXEventProvider;
33 import org.crosswire.common.xml.TransformingSAXEventProvider;
34 import org.crosswire.common.xml.XMLUtil;
35 import org.crosswire.jsword.book.Book;
36 import org.crosswire.jsword.book.BookCategory;
37 import org.crosswire.jsword.book.BookData;
38 import org.crosswire.jsword.book.BookException;
39 import org.crosswire.jsword.book.BookFilter;
40 import org.crosswire.jsword.book.BookFilters;
41 import org.crosswire.jsword.book.BookMetaData;
42 import org.crosswire.jsword.book.Books;
43 import org.crosswire.jsword.book.BooksEvent;
44 import org.crosswire.jsword.book.BooksListener;
45 import org.crosswire.jsword.book.OSISUtil;
46 import org.crosswire.jsword.book.install.InstallException;
47 import org.crosswire.jsword.book.install.InstallManager;
48 import org.crosswire.jsword.book.install.Installer;
49 import org.crosswire.jsword.index.search.DefaultSearchModifier;
50 import org.crosswire.jsword.index.search.DefaultSearchRequest;
51 import org.crosswire.jsword.passage.Key;
52 import org.crosswire.jsword.passage.NoSuchKeyException;
53 import org.crosswire.jsword.passage.Passage;
54 import org.crosswire.jsword.passage.PassageTally;
55 import org.crosswire.jsword.passage.RestrictionType;
56 import org.crosswire.jsword.passage.VerseRange;
57 import org.crosswire.jsword.util.ConverterFactory;
58 import org.xml.sax.SAXException;
59
60
67 public class APIExamples {
68
71 private static final String BIBLE_NAME = "KJV";
72
73
80 public Book getBook(String bookInitials) {
81 return Books.installed().getBook(bookInitials);
82 }
83
84
96 public String getPlainText(String bookInitials, String reference) throws BookException, NoSuchKeyException {
97 Book book = getBook(bookInitials);
98 if (book == null) {
99 return "";
100 }
101
102 Key key = book.getKey(reference);
103 BookData data = new BookData(book, key);
104 return OSISUtil.getCanonicalText(data.getOsisFragment());
105 }
106
107
120 public SAXEventProvider getOSIS(String bookInitials, String reference, int maxKeyCount) throws BookException, NoSuchKeyException {
121 if (bookInitials == null || reference == null) {
122 return null;
123 }
124
125 Book book = getBook(bookInitials);
126
127 Key key = null;
128 if (BookCategory.BIBLE.equals(book.getBookCategory())) {
129 key = book.getKey(reference);
130 ((Passage) key).trimVerses(maxKeyCount);
131 } else {
132 key = book.createEmptyKeyList();
133
134 int count = 0;
135 for (Key aKey : book.getKey(reference)) {
136 if (++count >= maxKeyCount) {
137 break;
138 }
139 key.addAll(aKey);
140 }
141 }
142
143 BookData data = new BookData(book, key);
144
145 return data.getSAXEventProvider();
146 }
147
148
164 public String readStyledText(String bookInitials, String reference, int maxKeyCount) throws NoSuchKeyException, BookException, TransformerException,
165 SAXException
166 {
167 Book book = getBook(bookInitials);
168 SAXEventProvider osissep = getOSIS(bookInitials, reference, maxKeyCount);
169 if (osissep == null) {
170 return "";
171 }
172
173 Converter styler = ConverterFactory.getConverter();
174
175 TransformingSAXEventProvider htmlsep = (TransformingSAXEventProvider) styler.convert(osissep);
176
177 BookMetaData bmd = book.getBookMetaData();
180 boolean direction = bmd.isLeftToRight();
181 htmlsep.setParameter("direction", direction ? "ltr" : "rtl");
182
183 return XMLUtil.writeToString(htmlsep);
185 }
186
187
195 public void readDictionary() throws BookException {
196 List<Book> dicts = Books.installed().getBooks(BookFilters.getDictionaries());
200 Book dict = dicts.get(0);
201
202 Key keys = dict.getGlobalKeyList();
206 Key first = keys.iterator().next();
207
208 System.out.println("The first Key in the default dictionary is " + first);
209
210 BookData data = new BookData(dict, first);
211 System.out.println("And the text against that key is " + OSISUtil.getPlainText(data.getOsisFragment()));
212 }
213
214
219 public void search() throws BookException {
220 Book bible = Books.installed().getBook(BIBLE_NAME);
221
222 Key key = bible.find("+moses +aaron");
225
226 System.out.println("The following verses contain both moses and aaron: " + key.getName());
227
228 if (key instanceof Passage) {
233 Passage remaining = ((Passage) key).trimVerses(5);
234 System.out.println("The first 5 verses containing both moses and aaron: " + key.getName());
235 if (remaining != null) {
236 System.out.println("The rest of the verses are: " + remaining.getName());
237 } else {
238 System.out.println("There are only 5 verses containing both moses and aaron");
239 }
240 }
241 }
242
243
248 void rankedSearch() throws BookException {
249 Book bible = Books.installed().getBook(BIBLE_NAME);
250
251 boolean rank = true;
254 int max = 20;
255
256 DefaultSearchModifier modifier = new DefaultSearchModifier();
257 modifier.setRanked(rank);
258 modifier.setMaxResults(max);
259
260 Key results = bible.find(new DefaultSearchRequest("for god so loved the world", modifier));
261 int total = results.getCardinality();
262 int partial = total;
263
264 if (results instanceof PassageTally || rank) {
266 PassageTally tally = (PassageTally) results;
267 tally.setOrdering(PassageTally.Order.TALLY);
268 int rankCount = max;
269 if (rankCount > 0 && rankCount < total) {
270 tally.trimRanges(rankCount, RestrictionType.NONE);
273 partial = rankCount;
274 }
275 }
276 System.out.println("Showing the first " + partial + " of " + total + " verses.");
277 System.out.println(results);
278 }
279
280
287 void searchAndShow() throws BookException, SAXException {
288 Book bible = Books.installed().getBook(BIBLE_NAME);
289
290 Key key = bible.find("melchesidec~");
292
293
297 String path = "xsl/cswing/simple.xsl";
299 URL xslurl = ResourceUtil.getResource(path);
300 Iterator<VerseRange> rangeIter = ((Passage) key).rangeIterator(RestrictionType.CHAPTER);
302 while (rangeIter.hasNext()) {
304 Key range = rangeIter.next();
305 BookData data = new BookData(bible, range);
306 SAXEventProvider osissep = data.getSAXEventProvider();
307 SAXEventProvider htmlsep = new TransformingSAXEventProvider(NetUtil.toURI(xslurl), osissep);
308 String text = XMLUtil.writeToString(htmlsep);
309 System.out.println("The html text of " + range.getName() + " is " + text);
310 }
311 }
312
313
320 public void pickBible() {
321 Book book = Books.installed().getBook(BIBLE_NAME);
323
324 System.out.println(book.getLanguage());
326
327 List<Book> books = Books.installed().getBooks();
329 book = books.get(0);
330
331 books = Books.installed().getBooks(BookFilters.getOnlyBibles());
333 book = books.get(0);
334
335
338 List<Book> test = Books.installed().getBooks(new MyBookFilter("ESV"));
341 book = test.get(0);
342
343 if (book != null) {
344 System.out.println(book.getInitials());
345 }
346
347 Books.installed().addBooksListener(new MyBooksListener());
349 }
350
351 public void installBook() {
352 Installer installer = null;
354
355 InstallManager imanager = new InstallManager();
356
357 Map<String, Installer> installers = imanager.getInstallers();
359
360 String name = null;
362 for (Map.Entry<String, Installer> mapEntry : installers.entrySet()) {
363 name = mapEntry.getKey();
364 installer = mapEntry.getValue();
365 System.out.println(name + ": " + installer.getInstallerDefinition());
366 }
367
368 name = "CrossWire";
369 installer = imanager.getInstaller(name);
371
372 try {
374 installer.reloadBookList();
375 } catch (InstallException e) {
376 e.printStackTrace();
377 }
378
379 List<Book> availableBooks = installer.getBooks();
381
382 Book book = availableBooks.get(0);
383 if (book != null) {
384 System.out.println("Book " + book.getInitials() + " is available");
385 }
386
387 availableBooks = installer.getBooks(new MyBookFilter("ESV"));
389
390 book = availableBooks.get(0);
391
392 if (book != null) {
393 System.out.println("Book " + book.getInitials() + " is available");
394
395 try {
399 if (Books.installed().getBook("ESV") != null) {
400 Books.installed().removeBook(book);
403
404 book.getDriver().delete(book);
407 }
408 } catch (BookException ex) {
409 ex.printStackTrace();
410 }
411
412 try {
413 installer.install(book);
415 } catch (InstallException ex) {
416 ex.printStackTrace();
417 }
418 }
419 }
420
421
424 static class MyBookFilter implements BookFilter {
425 MyBookFilter(String bookName) {
426 name = bookName;
427 }
428
429 public boolean test(Book bk) {
430 return bk.getInitials().equals(name);
431 }
432
433 private String name;
434 }
435
436
439 static class MyBooksListener implements BooksListener {
440
447 public void bookAdded(BooksEvent ev) {
448 }
449
450
457 public void bookRemoved(BooksEvent ev) {
458 }
459 }
460
461
470 public static void main(String[] args) throws BookException, NoSuchKeyException, TransformerException, SAXException {
471 APIExamples examples = new APIExamples();
472
473 examples.installBook();
474 System.out.println("The plain text of Gen 1:1 is " + examples.getPlainText(BIBLE_NAME, "Gen 1:1"));
475 System.out.println("The html text of Gen 1:1 is " + examples.readStyledText(BIBLE_NAME, "Gen 1:1", 100));
476 examples.readDictionary();
477 examples.search();
478 examples.rankedSearch();
479 examples.searchAndShow();
480 }
481 }
482