Search in sources :

Example 1 with BookException

use of org.crosswire.jsword.book.BookException in project step by STEPBible.

the class JSwordAnalysisServiceImpl method getTextStats.

@Override
public PassageStat getTextStats(final String version, final Key reference, final ScopeType scopeType) {
    try {
        final Book book = this.versification.getBookFromVersion(version);
        final Versification av11n = this.versification.getVersificationForVersion(book);
        final BookData bookData = getExpandedBookData(reference, scopeType, av11n, book);
        final String canonicalText = OSISUtil.getCanonicalText(bookData.getOsisFragment());
        final String[] words = split(canonicalText, WORD_SPLIT);
        Set<String> languageStopWords = getLanguageStopList(book);
        final PassageStat stat = new PassageStat();
        for (final String word : words) {
            // only add word if not in STOP list
            if (!languageStopWords.contains(StringConversionUtils.unAccent(word.toUpperCase(), true))) {
                stat.addWord(word);
            }
        }
        return stat;
    } catch (final BookException e) {
        throw new StepInternalException("Unable to read passage text", e);
    }
}
Also used : StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) PassageStat(com.tyndalehouse.step.core.models.stats.PassageStat) Book(org.crosswire.jsword.book.Book) Versification(org.crosswire.jsword.versification.Versification) BookException(org.crosswire.jsword.book.BookException) BookData(org.crosswire.jsword.book.BookData)

Example 2 with BookException

use of org.crosswire.jsword.book.BookException in project step by STEPBible.

the class JSwordSearchServiceImpl method doSearch.

private void doSearch(final DefaultSearchModifier modifier, final Map<String, Key> resultsPerVersion, final IndividualSearch currentSearch, final Book bible) {
    String version = bible.getInitials();
    if (bible.getIndexStatus().equals(IndexStatus.DONE)) {
        final Key luceneSearchResults;
        try {
            String query = currentSearch.getQuery();
            // small optimization and cater for versions that don't support Gen-Rev as a range:
            query = GEN_REV_RANGE.matcher(query).replaceAll("");
            luceneSearchResults = bible.find(new DefaultSearchRequest(query, modifier));
        } catch (final BookException e) {
            throw new LuceneSearchException("Unable to search for " + currentSearch.getQuery() + " with Bible " + version, e);
        }
        resultsPerVersion.put(version, luceneSearchResults);
    } else {
        LOGGER.error("Module [{}] is not indexed.", version);
        resultsPerVersion.put(version, PassageKeyFactory.instance().createEmptyKeyList(av11nService.getVersificationForVersion(bible)));
    }
}
Also used : DefaultSearchRequest(org.crosswire.jsword.index.search.DefaultSearchRequest) BookException(org.crosswire.jsword.book.BookException) LuceneSearchException(com.tyndalehouse.step.core.exceptions.LuceneSearchException)

Example 3 with BookException

use of org.crosswire.jsword.book.BookException in project step by STEPBible.

the class JSwordSearchServiceImpl method getIndexSearcher.

/**
 * Retrieves the index from JSword
 * @param bookName the book name
 * @return the index searcher responsible for carrying out operations on JSword data.
 */
public IndexSearcher getIndexSearcher(String bookName) {
    final IndexManager indexManager = IndexManagerFactory.getIndexManager();
    Index index;
    try {
        index = indexManager.getIndex(this.av11nService.getBookFromVersion(bookName));
    } catch (BookException e) {
        throw new StepInternalException(e.getMessage(), e);
    }
    if (!(index instanceof LuceneIndex)) {
        LOGGER.warn("Unsupported Lucene Index type [{}]", index.getClass());
        throw new StepInternalException("Unable to obtain index");
    }
    @SuppressWarnings("resource") final LuceneIndex li = (LuceneIndex) index;
    return (IndexSearcher) li.getSearcher();
}
Also used : IndexManager(org.crosswire.jsword.index.IndexManager) IndexSearcher(org.apache.lucene.search.IndexSearcher) LuceneIndex(org.crosswire.jsword.index.lucene.LuceneIndex) StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) Index(org.crosswire.jsword.index.Index) LuceneIndex(org.crosswire.jsword.index.lucene.LuceneIndex) BookException(org.crosswire.jsword.book.BookException)

Example 4 with BookException

use of org.crosswire.jsword.book.BookException in project step by STEPBible.

the class JSwordPassageServiceImpl method doInterleavedVersionsLookup.

private OsisWrapper doInterleavedVersionsLookup(String[] versions, final BookData data, final Versification v11n, final List<LookupOption> options, final InterlinearMode displayMode, final String userLanguage) {
    Book[] books = data.getBooks();
    try {
        setUnaccenter(data, displayMode);
        final TransformingSAXEventProvider transformer = executeStyleSheet(v11n, options, null, data, data.getSAXEventProvider(), displayMode, userLanguage);
        String[] languages = new String[books.length];
        for (int ii = 0; ii < books.length; ii++) {
            languages[ii] = books[ii].getLanguage().getCode();
        }
        final Key key = data.getKey();
        return new OsisWrapper(writeToString(transformer), key, languages, v11n, resolver.getShortName(versions[0]), displayMode, StringUtils.join(versions, 1));
    } catch (final TransformerException e) {
        throw new StepInternalException(e.getMessage(), e);
    } catch (final SAXException e) {
        throw new StepInternalException(e.getMessage(), e);
    } catch (final BookException e) {
        throw new LocalisedException(e, e.getMessage());
    }
}
Also used : BookException(org.crosswire.jsword.book.BookException) XMLUtil.writeToString(org.crosswire.common.xml.XMLUtil.writeToString) SAXException(org.xml.sax.SAXException) LocalisedException(com.tyndalehouse.step.core.exceptions.LocalisedException) TransformingSAXEventProvider(org.crosswire.common.xml.TransformingSAXEventProvider) StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) BibleBook(org.crosswire.jsword.versification.BibleBook) Book(org.crosswire.jsword.book.Book) Key(org.crosswire.jsword.passage.Key) OsisWrapper(com.tyndalehouse.step.core.models.OsisWrapper) TransformerException(javax.xml.transform.TransformerException)

Example 5 with BookException

use of org.crosswire.jsword.book.BookException in project step by STEPBible.

the class JSwordPassageServiceImpl method getTextForBookData.

/**
 * Gets the osis text
 *
 * @param options            the list of lookup options
 * @param interlinearVersion the interlinear version if applicable
 * @param bookData           the bookdata to use to look up the required version/reference combo
 * @param displayMode        the mode to display the text with
 * @return the html text
 */
private OsisWrapper getTextForBookData(final List<LookupOption> options, final String interlinearVersion, final BookData bookData, final InterlinearMode displayMode) {
    // check we have a book in mind and a reference
    notNull(bookData, "An internal error occurred", UserExceptionType.SERVICE_VALIDATION_ERROR);
    notNull(bookData.getFirstBook(), "An internal error occurred", UserExceptionType.SERVICE_VALIDATION_ERROR);
    Key key = bookData.getKey();
    notNull(key, "An internal error occurred", UserExceptionType.SERVICE_VALIDATION_ERROR);
    // the original book
    final Book book = bookData.getFirstBook();
    final Versification versification = this.versificationService.getVersificationForVersion(book);
    try {
        // first check whether the key is contained in the book
        key = normalize(key, versification);
        final SAXEventProvider osissep = bookData.getSAXEventProvider();
        final TransformingSAXEventProvider htmlsep = executeStyleSheet(versification, options, interlinearVersion, bookData, osissep, displayMode, "en");
        final OsisWrapper osisWrapper = new OsisWrapper(writeToString(htmlsep), key, getLanguages(book, displayMode, htmlsep, options), versification, resolver.getShortName(bookData.getFirstBook().getInitials()), displayMode, interlinearVersion);
        if (key instanceof Passage) {
            final Passage p = (Passage) key;
            final boolean hasMultipleRanges = p.hasRanges(RestrictionType.NONE);
            osisWrapper.setMultipleRanges(hasMultipleRanges);
            if (hasMultipleRanges) {
                // get the first "range" and set up the start and ends
                final VerseRange r = p.rangeIterator(RestrictionType.NONE).next();
                osisWrapper.setStartRange(versification.getOrdinal(r.getStart()));
                osisWrapper.setEndRange(versification.getOrdinal(r.getEnd()));
            } else {
                Iterator<Key> keys = p.iterator();
                Verse start = null;
                Verse end = null;
                while (keys.hasNext()) {
                    if (start == null) {
                        start = (Verse) keys.next();
                    } else {
                        end = (Verse) keys.next();
                    }
                }
                if (start != null) {
                    osisWrapper.setStartRange(start.getOrdinal());
                }
                if (end != null) {
                    osisWrapper.setEndRange(end.getOrdinal());
                } else if (start != null) {
                    osisWrapper.setEndRange(start.getOrdinal());
                }
            }
        } else if (key instanceof VerseRange) {
            final VerseRange vr = (VerseRange) key;
            osisWrapper.setStartRange(versification.getOrdinal(vr.getStart()));
            osisWrapper.setEndRange(versification.getOrdinal(vr.getEnd()));
            osisWrapper.setMultipleRanges(false);
        }
        return osisWrapper;
    } catch (final BookException e) {
        throw new LocalisedException(e, e.getMessage());
    } catch (final SAXException e) {
        throw new StepInternalException(e.getMessage(), e);
    } catch (final TransformerException e) {
        throw new StepInternalException(e.getMessage(), e);
    } catch (final NoSuchKeyException e) {
        throw new TranslatedException(e, "invalid_reference_in_book", bookData.getKey().getName(), book.getInitials());
    }
}
Also used : VerseRange(org.crosswire.jsword.passage.VerseRange) TranslatedException(com.tyndalehouse.step.core.exceptions.TranslatedException) Versification(org.crosswire.jsword.versification.Versification) BookException(org.crosswire.jsword.book.BookException) RocketPassage(org.crosswire.jsword.passage.RocketPassage) Passage(org.crosswire.jsword.passage.Passage) SAXException(org.xml.sax.SAXException) LocalisedException(com.tyndalehouse.step.core.exceptions.LocalisedException) TransformingSAXEventProvider(org.crosswire.common.xml.TransformingSAXEventProvider) NoSuchKeyException(org.crosswire.jsword.passage.NoSuchKeyException) StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) BibleBook(org.crosswire.jsword.versification.BibleBook) Book(org.crosswire.jsword.book.Book) TransformingSAXEventProvider(org.crosswire.common.xml.TransformingSAXEventProvider) JDOMSAXEventProvider(org.crosswire.common.xml.JDOMSAXEventProvider) SAXEventProvider(org.crosswire.common.xml.SAXEventProvider) Key(org.crosswire.jsword.passage.Key) OsisWrapper(com.tyndalehouse.step.core.models.OsisWrapper) Verse(org.crosswire.jsword.passage.Verse) TransformerException(javax.xml.transform.TransformerException)

Aggregations

BookException (org.crosswire.jsword.book.BookException)9 Book (org.crosswire.jsword.book.Book)6 BookData (org.crosswire.jsword.book.BookData)5 BibleBook (org.crosswire.jsword.versification.BibleBook)5 LocalisedException (com.tyndalehouse.step.core.exceptions.LocalisedException)4 StepInternalException (com.tyndalehouse.step.core.exceptions.StepInternalException)4 Key (org.crosswire.jsword.passage.Key)4 NoSuchKeyException (org.crosswire.jsword.passage.NoSuchKeyException)3 Versification (org.crosswire.jsword.versification.Versification)3 TranslatedException (com.tyndalehouse.step.core.exceptions.TranslatedException)2 OsisWrapper (com.tyndalehouse.step.core.models.OsisWrapper)2 HashMap (java.util.HashMap)2 TransformerException (javax.xml.transform.TransformerException)2 JDOMSAXEventProvider (org.crosswire.common.xml.JDOMSAXEventProvider)2 TransformingSAXEventProvider (org.crosswire.common.xml.TransformingSAXEventProvider)2 XMLUtil.writeToString (org.crosswire.common.xml.XMLUtil.writeToString)2 Element (org.jdom2.Element)2 SAXException (org.xml.sax.SAXException)2 EntityDoc (com.tyndalehouse.step.core.data.EntityDoc)1 LuceneSearchException (com.tyndalehouse.step.core.exceptions.LuceneSearchException)1