| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 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 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | 0 | public class APIExamples { |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
private static final String BIBLE_NAME = "KJV"; |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
public Book getBook(String bookInitials) { |
| 81 | 0 | return Books.installed().getBook(bookInitials); |
| 82 | |
} |
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
public String getPlainText(String bookInitials, String reference) throws BookException, NoSuchKeyException { |
| 97 | 0 | Book book = getBook(bookInitials); |
| 98 | 0 | if (book == null) { |
| 99 | 0 | return ""; |
| 100 | |
} |
| 101 | |
|
| 102 | 0 | Key key = book.getKey(reference); |
| 103 | 0 | BookData data = new BookData(book, key); |
| 104 | 0 | return OSISUtil.getCanonicalText(data.getOsisFragment()); |
| 105 | |
} |
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
public SAXEventProvider getOSIS(String bookInitials, String reference, int maxKeyCount) throws BookException, NoSuchKeyException { |
| 121 | 0 | if (bookInitials == null || reference == null) { |
| 122 | 0 | return null; |
| 123 | |
} |
| 124 | |
|
| 125 | 0 | Book book = getBook(bookInitials); |
| 126 | |
|
| 127 | 0 | Key key = null; |
| 128 | 0 | if (BookCategory.BIBLE.equals(book.getBookCategory())) { |
| 129 | 0 | key = book.getKey(reference); |
| 130 | 0 | ((Passage) key).trimVerses(maxKeyCount); |
| 131 | |
} else { |
| 132 | 0 | key = book.createEmptyKeyList(); |
| 133 | |
|
| 134 | 0 | int count = 0; |
| 135 | 0 | for (Key aKey : book.getKey(reference)) { |
| 136 | 0 | if (++count >= maxKeyCount) { |
| 137 | 0 | break; |
| 138 | |
} |
| 139 | 0 | key.addAll(aKey); |
| 140 | |
} |
| 141 | |
} |
| 142 | |
|
| 143 | 0 | BookData data = new BookData(book, key); |
| 144 | |
|
| 145 | 0 | return data.getSAXEventProvider(); |
| 146 | |
} |
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
public String readStyledText(String bookInitials, String reference, int maxKeyCount) throws NoSuchKeyException, BookException, TransformerException, |
| 165 | |
SAXException |
| 166 | |
{ |
| 167 | 0 | Book book = getBook(bookInitials); |
| 168 | 0 | SAXEventProvider osissep = getOSIS(bookInitials, reference, maxKeyCount); |
| 169 | 0 | if (osissep == null) { |
| 170 | 0 | return ""; |
| 171 | |
} |
| 172 | |
|
| 173 | 0 | Converter styler = ConverterFactory.getConverter(); |
| 174 | |
|
| 175 | 0 | TransformingSAXEventProvider htmlsep = (TransformingSAXEventProvider) styler.convert(osissep); |
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | 0 | BookMetaData bmd = book.getBookMetaData(); |
| 180 | 0 | boolean direction = bmd.isLeftToRight(); |
| 181 | 0 | htmlsep.setParameter("direction", direction ? "ltr" : "rtl"); |
| 182 | |
|
| 183 | |
|
| 184 | 0 | return XMLUtil.writeToString(htmlsep); |
| 185 | |
} |
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
public void readDictionary() throws BookException { |
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | 0 | List<Book> dicts = Books.installed().getBooks(BookFilters.getDictionaries()); |
| 200 | 0 | Book dict = dicts.get(0); |
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | 0 | Key keys = dict.getGlobalKeyList(); |
| 206 | 0 | Key first = keys.iterator().next(); |
| 207 | |
|
| 208 | 0 | System.out.println("The first Key in the default dictionary is " + first); |
| 209 | |
|
| 210 | 0 | BookData data = new BookData(dict, first); |
| 211 | 0 | System.out.println("And the text against that key is " + OSISUtil.getPlainText(data.getOsisFragment())); |
| 212 | 0 | } |
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
public void search() throws BookException { |
| 220 | 0 | Book bible = Books.installed().getBook(BIBLE_NAME); |
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | 0 | Key key = bible.find("+moses +aaron"); |
| 225 | |
|
| 226 | 0 | System.out.println("The following verses contain both moses and aaron: " + key.getName()); |
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | 0 | if (key instanceof Passage) { |
| 233 | 0 | Passage remaining = ((Passage) key).trimVerses(5); |
| 234 | 0 | System.out.println("The first 5 verses containing both moses and aaron: " + key.getName()); |
| 235 | 0 | if (remaining != null) { |
| 236 | 0 | System.out.println("The rest of the verses are: " + remaining.getName()); |
| 237 | |
} else { |
| 238 | 0 | System.out.println("There are only 5 verses containing both moses and aaron"); |
| 239 | |
} |
| 240 | |
} |
| 241 | 0 | } |
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
void rankedSearch() throws BookException { |
| 249 | 0 | Book bible = Books.installed().getBook(BIBLE_NAME); |
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | 0 | boolean rank = true; |
| 254 | 0 | int max = 20; |
| 255 | |
|
| 256 | 0 | DefaultSearchModifier modifier = new DefaultSearchModifier(); |
| 257 | 0 | modifier.setRanked(rank); |
| 258 | 0 | modifier.setMaxResults(max); |
| 259 | |
|
| 260 | 0 | Key results = bible.find(new DefaultSearchRequest("for god so loved the world", modifier)); |
| 261 | 0 | int total = results.getCardinality(); |
| 262 | 0 | int partial = total; |
| 263 | |
|
| 264 | |
|
| 265 | 0 | if (results instanceof PassageTally || rank) { |
| 266 | 0 | PassageTally tally = (PassageTally) results; |
| 267 | 0 | tally.setOrdering(PassageTally.Order.TALLY); |
| 268 | 0 | int rankCount = max; |
| 269 | 0 | if (rankCount > 0 && rankCount < total) { |
| 270 | |
|
| 271 | |
|
| 272 | 0 | tally.trimRanges(rankCount, RestrictionType.NONE); |
| 273 | 0 | partial = rankCount; |
| 274 | |
} |
| 275 | |
} |
| 276 | 0 | System.out.println("Showing the first " + partial + " of " + total + " verses."); |
| 277 | 0 | System.out.println(results); |
| 278 | 0 | } |
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
void searchAndShow() throws BookException, SAXException { |
| 288 | 0 | Book bible = Books.installed().getBook(BIBLE_NAME); |
| 289 | |
|
| 290 | |
|
| 291 | 0 | Key key = bible.find("melchesidec~"); |
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | 0 | String path = "xsl/cswing/simple.xsl"; |
| 299 | 0 | URL xslurl = ResourceUtil.getResource(path); |
| 300 | |
|
| 301 | 0 | Iterator<VerseRange> rangeIter = ((Passage) key).rangeIterator(RestrictionType.CHAPTER); |
| 302 | |
|
| 303 | 0 | while (rangeIter.hasNext()) { |
| 304 | 0 | Key range = rangeIter.next(); |
| 305 | 0 | BookData data = new BookData(bible, range); |
| 306 | 0 | SAXEventProvider osissep = data.getSAXEventProvider(); |
| 307 | 0 | SAXEventProvider htmlsep = new TransformingSAXEventProvider(NetUtil.toURI(xslurl), osissep); |
| 308 | 0 | String text = XMLUtil.writeToString(htmlsep); |
| 309 | 0 | System.out.println("The html text of " + range.getName() + " is " + text); |
| 310 | 0 | } |
| 311 | 0 | } |
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
public void pickBible() { |
| 321 | |
|
| 322 | 0 | Book book = Books.installed().getBook(BIBLE_NAME); |
| 323 | |
|
| 324 | |
|
| 325 | 0 | System.out.println(book.getLanguage()); |
| 326 | |
|
| 327 | |
|
| 328 | 0 | List<Book> books = Books.installed().getBooks(); |
| 329 | 0 | book = books.get(0); |
| 330 | |
|
| 331 | |
|
| 332 | 0 | books = Books.installed().getBooks(BookFilters.getOnlyBibles()); |
| 333 | 0 | book = books.get(0); |
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | 0 | List<Book> test = Books.installed().getBooks(new MyBookFilter("ESV")); |
| 341 | 0 | book = test.get(0); |
| 342 | |
|
| 343 | 0 | if (book != null) { |
| 344 | 0 | System.out.println(book.getInitials()); |
| 345 | |
} |
| 346 | |
|
| 347 | |
|
| 348 | 0 | Books.installed().addBooksListener(new MyBooksListener()); |
| 349 | 0 | } |
| 350 | |
|
| 351 | |
public void installBook() { |
| 352 | |
|
| 353 | 0 | Installer installer = null; |
| 354 | |
|
| 355 | 0 | InstallManager imanager = new InstallManager(); |
| 356 | |
|
| 357 | |
|
| 358 | 0 | Map<String, Installer> installers = imanager.getInstallers(); |
| 359 | |
|
| 360 | |
|
| 361 | 0 | String name = null; |
| 362 | 0 | for (Map.Entry<String, Installer> mapEntry : installers.entrySet()) { |
| 363 | 0 | name = mapEntry.getKey(); |
| 364 | 0 | installer = mapEntry.getValue(); |
| 365 | 0 | System.out.println(name + ": " + installer.getInstallerDefinition()); |
| 366 | |
} |
| 367 | |
|
| 368 | 0 | name = "CrossWire"; |
| 369 | |
|
| 370 | 0 | installer = imanager.getInstaller(name); |
| 371 | |
|
| 372 | |
|
| 373 | |
try { |
| 374 | 0 | installer.reloadBookList(); |
| 375 | 0 | } catch (InstallException e) { |
| 376 | 0 | e.printStackTrace(); |
| 377 | 0 | } |
| 378 | |
|
| 379 | |
|
| 380 | 0 | List<Book> availableBooks = installer.getBooks(); |
| 381 | |
|
| 382 | 0 | Book book = availableBooks.get(0); |
| 383 | 0 | if (book != null) { |
| 384 | 0 | System.out.println("Book " + book.getInitials() + " is available"); |
| 385 | |
} |
| 386 | |
|
| 387 | |
|
| 388 | 0 | availableBooks = installer.getBooks(new MyBookFilter("ESV")); |
| 389 | |
|
| 390 | 0 | book = availableBooks.get(0); |
| 391 | |
|
| 392 | 0 | if (book != null) { |
| 393 | 0 | System.out.println("Book " + book.getInitials() + " is available"); |
| 394 | |
|
| 395 | |
|
| 396 | |
|
| 397 | |
|
| 398 | |
try { |
| 399 | 0 | if (Books.installed().getBook("ESV") != null) { |
| 400 | |
|
| 401 | |
|
| 402 | 0 | Books.installed().removeBook(book); |
| 403 | |
|
| 404 | |
|
| 405 | |
|
| 406 | 0 | book.getDriver().delete(book); |
| 407 | |
} |
| 408 | 0 | } catch (BookException ex) { |
| 409 | 0 | ex.printStackTrace(); |
| 410 | 0 | } |
| 411 | |
|
| 412 | |
try { |
| 413 | |
|
| 414 | 0 | installer.install(book); |
| 415 | 0 | } catch (InstallException ex) { |
| 416 | 0 | ex.printStackTrace(); |
| 417 | 0 | } |
| 418 | |
} |
| 419 | 0 | } |
| 420 | |
|
| 421 | |
|
| 422 | |
|
| 423 | |
|
| 424 | |
static class MyBookFilter implements BookFilter { |
| 425 | 0 | MyBookFilter(String bookName) { |
| 426 | 0 | name = bookName; |
| 427 | 0 | } |
| 428 | |
|
| 429 | |
public boolean test(Book bk) { |
| 430 | 0 | return bk.getInitials().equals(name); |
| 431 | |
} |
| 432 | |
|
| 433 | |
private String name; |
| 434 | |
} |
| 435 | |
|
| 436 | |
|
| 437 | |
|
| 438 | |
|
| 439 | 0 | static class MyBooksListener implements BooksListener { |
| 440 | |
|
| 441 | |
|
| 442 | |
|
| 443 | |
|
| 444 | |
|
| 445 | |
|
| 446 | |
|
| 447 | |
public void bookAdded(BooksEvent ev) { |
| 448 | 0 | } |
| 449 | |
|
| 450 | |
|
| 451 | |
|
| 452 | |
|
| 453 | |
|
| 454 | |
|
| 455 | |
|
| 456 | |
|
| 457 | |
public void bookRemoved(BooksEvent ev) { |
| 458 | 0 | } |
| 459 | |
} |
| 460 | |
|
| 461 | |
|
| 462 | |
|
| 463 | |
|
| 464 | |
|
| 465 | |
|
| 466 | |
|
| 467 | |
|
| 468 | |
|
| 469 | |
|
| 470 | |
public static void main(String[] args) throws BookException, NoSuchKeyException, TransformerException, SAXException { |
| 471 | 0 | APIExamples examples = new APIExamples(); |
| 472 | |
|
| 473 | 0 | examples.installBook(); |
| 474 | 0 | System.out.println("The plain text of Gen 1:1 is " + examples.getPlainText(BIBLE_NAME, "Gen 1:1")); |
| 475 | 0 | System.out.println("The html text of Gen 1:1 is " + examples.readStyledText(BIBLE_NAME, "Gen 1:1", 100)); |
| 476 | 0 | examples.readDictionary(); |
| 477 | 0 | examples.search(); |
| 478 | 0 | examples.rankedSearch(); |
| 479 | 0 | examples.searchAndShow(); |
| 480 | 0 | } |
| 481 | |
} |