Search in sources :

Example 1 with Passage

use of org.crosswire.jsword.passage.Passage 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)

Example 2 with Passage

use of org.crosswire.jsword.passage.Passage in project step by STEPBible.

the class SubjectEntryServiceImpl method collectVersesFromReferences.

/**
 * Collects individual ranges
 *
 * @param verses                 the verses
 * @param inputVersions          the versions
 * @param references             the list of resultsInKJV that form the results
 * @param limitingScopeReference the limiting scope for the reference
 * @param context                the context to expand with the reference
 */
private boolean collectVersesFromReferences(final List<OsisWrapper> verses, final String[] inputVersions, final String references, final String limitingScopeReference, final int context) {
    final String originalMaster = inputVersions[0];
    Passage combinedScopeInKJVv11n = this.getCombinedBookScope(inputVersions);
    // now let's retain the verses that are of interest in the selected books
    Key resultsInKJV = null;
    try {
        resultsInKJV = this.versificationService.getBookFromVersion(JSwordPassageService.BEST_VERSIFICATION).getKey(references);
    } catch (NoSuchKeyException e) {
        throw new StepInternalException("Unable to parse resultsInKJV from Nave", e);
    }
    resultsInKJV.retainAll(combinedScopeInKJVv11n);
    trimResultsToInputSearchRange(inputVersions[0], limitingScopeReference, resultsInKJV);
    // then calculate what the best version order is
    GetBestVersionOrderAndKey getBestVersionOrderAndKey = new GetBestVersionOrderAndKey(inputVersions, resultsInKJV).invoke();
    Book book = getBestVersionOrderAndKey.getBook();
    String[] versions = getBestVersionOrderAndKey.getVersions();
    final Passage resultsInProperV11n = getBestVersionOrderAndKey.getVerseRanges();
    final Iterator<VerseRange> rangeIterator = resultsInProperV11n.rangeIterator(RestrictionType.NONE);
    final List<LookupOption> options = new ArrayList<LookupOption>();
    options.add(LookupOption.HIDE_XGEN);
    options.add(LookupOption.GREEK_ACCENTS);
    options.add(LookupOption.HEBREW_VOWELS);
    if (context > 0) {
        // add verse numbers
        // options.add(LookupOption.TINY_VERSE_NUMBERS);
        options.add(LookupOption.VERSE_NUMBERS);
    }
    final Versification av11n = this.versificationService.getVersificationForVersion(book);
    Verse lastVerse = null;
    while (rangeIterator.hasNext()) {
        final Key range = rangeIterator.next();
        // get the distance between the first verse in the range and the last verse
        if (lastVerse != null && isCloseVerse(av11n, lastVerse, range)) {
            final OsisWrapper osisWrapper = verses.get(verses.size() - 1);
            final StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(osisWrapper.getReference());
            stringBuilder.append("; ");
            stringBuilder.append(range.getName());
            osisWrapper.setFragment(true);
            try {
                osisWrapper.setReference(book.getKey(stringBuilder.toString()).getName());
            } catch (final NoSuchKeyException e) {
                // fail to get a key, let's log and continue
                LOGGER.warn("Unable to get key for reference: [{}]", osisWrapper.getReference());
                LOGGER.trace("Root cause is", e);
            }
        } else {
            final Key firstVerse = this.jsword.getFirstVersesFromRange(range, context);
            final OsisWrapper passage = this.jsword.peakOsisText(versions, firstVerse, options, InterlinearMode.INTERLEAVED_COMPARE.name());
            passage.setReference(range.getName());
            if (range.getCardinality() > 1) {
                passage.setFragment(true);
            }
            verses.add(passage);
        }
        // record last verse
        if (range instanceof VerseRange) {
            final VerseRange verseRange = (VerseRange) range;
            lastVerse = verseRange.getEnd();
        } else if (range instanceof Verse) {
            lastVerse = (Verse) range;
        }
    }
    return !getBestVersionOrderAndKey.versions[0].equals(originalMaster);
}
Also used : VerseRange(org.crosswire.jsword.passage.VerseRange) LookupOption(com.tyndalehouse.step.core.models.LookupOption) ArrayList(java.util.ArrayList) Versification(org.crosswire.jsword.versification.Versification) Passage(org.crosswire.jsword.passage.Passage) RangedPassage(org.crosswire.jsword.passage.RangedPassage) NoSuchKeyException(org.crosswire.jsword.passage.NoSuchKeyException) StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) Book(org.crosswire.jsword.book.Book) Key(org.crosswire.jsword.passage.Key) VerseKey(org.crosswire.jsword.passage.VerseKey) Verse(org.crosswire.jsword.passage.Verse) OsisWrapper(com.tyndalehouse.step.core.models.OsisWrapper)

Example 3 with Passage

use of org.crosswire.jsword.passage.Passage in project step by STEPBible.

the class SubjectEntryServiceImpl method trimResultsToInputSearchRange.

/**
 * Reduces the results so far to what is contained in the v11n
 *
 * @param inputVersion           input version
 * @param limitingScopeReference the limiting scope
 * @param resultsInKJV           the results retrieved so far.
 */
private void trimResultsToInputSearchRange(final String inputVersion, final String limitingScopeReference, final Key resultsInKJV) {
    if (StringUtils.isNotBlank(limitingScopeReference)) {
        final Book limitingBook;
        limitingBook = this.versificationService.getBookFromVersion(inputVersion);
        try {
            final Key key = KeyUtil.getPassage(limitingBook.getKey(limitingScopeReference));
            // now map to the KJV versification
            Passage p = VersificationsMapper.instance().map(KeyUtil.getPassage(key), ((VerseKey) resultsInKJV).getVersification());
            // now convert retain against existing resultsInKJV
            resultsInKJV.retainAll(p);
        } catch (NoSuchKeyException ex) {
            throw new TranslatedException(ex, "invalid_reference_in_book", limitingScopeReference, limitingBook.getInitials());
        }
    }
}
Also used : NoSuchKeyException(org.crosswire.jsword.passage.NoSuchKeyException) TranslatedException(com.tyndalehouse.step.core.exceptions.TranslatedException) Book(org.crosswire.jsword.book.Book) Passage(org.crosswire.jsword.passage.Passage) RangedPassage(org.crosswire.jsword.passage.RangedPassage) Key(org.crosswire.jsword.passage.Key) VerseKey(org.crosswire.jsword.passage.VerseKey)

Example 4 with Passage

use of org.crosswire.jsword.passage.Passage in project step by STEPBible.

the class SubjectEntryServiceImpl method getCombinedBookScope.

/**
 * Gets a key in the KJV versification that represents the total combined key for all search resutls.
 *
 * @param inputVersions the input version the kjv versified keys
 * @return
 */
private Passage getCombinedBookScope(String[] inputVersions) {
    final Versification bestVersification = this.versificationService.getVersificationForVersion(JSwordPassageService.BEST_VERSIFICATION);
    Passage range = new RangedPassage(bestVersification);
    for (final String v : inputVersions) {
        final Book bookFromVersion = this.versificationService.getBookFromVersion(v);
        final VerseKey scope = bookFromVersion.getBookMetaData().getScope();
        range.addAll(VersificationsMapper.instance().map(KeyUtil.getPassage(scope), bestVersification));
    }
    return range;
}
Also used : RangedPassage(org.crosswire.jsword.passage.RangedPassage) Book(org.crosswire.jsword.book.Book) Versification(org.crosswire.jsword.versification.Versification) Passage(org.crosswire.jsword.passage.Passage) RangedPassage(org.crosswire.jsword.passage.RangedPassage) VerseKey(org.crosswire.jsword.passage.VerseKey)

Example 5 with Passage

use of org.crosswire.jsword.passage.Passage in project step by STEPBible.

the class SubjectSearchServiceImpl method searchSimple.

/**
 * runs a simple subject search
 *
 * @param sq the search query
 * @return the results
 */
private SearchResult searchSimple(final SearchQuery sq) {
    // ensure we're using the latest range
    final IndividualSearch currentSearch = sq.getCurrentSearch();
    currentSearch.setQuery(currentSearch.getQuery(), true);
    final String[] originalVersions = currentSearch.getVersions();
    final String[] searchableVersions = prepareSearchForHeadings(sq);
    final Key allTopics = this.jswordSearch.searchKeys(sq);
    // we will need to restrict the results by the scope of the versions, in the ESV v11n
    final Passage maxScope = getScopeForVersions(originalVersions);
    allTopics.retainAll(VersificationsMapper.instance().map(maxScope, ((VerseKey) allTopics).getVersification()));
    SearchResult resultsAsHeadings = getResultsAsHeadings(sq, searchableVersions, allTopics);
    cleanUpSearchFromHeadingsSearch(sq, originalVersions);
    return resultsAsHeadings;
}
Also used : IndividualSearch(com.tyndalehouse.step.core.service.impl.IndividualSearch) SearchResult(com.tyndalehouse.step.core.models.search.SearchResult) Passage(org.crosswire.jsword.passage.Passage) RangedPassage(org.crosswire.jsword.passage.RangedPassage) VerseKey(org.crosswire.jsword.passage.VerseKey) VerseKey(org.crosswire.jsword.passage.VerseKey) Key(org.crosswire.jsword.passage.Key)

Aggregations

Passage (org.crosswire.jsword.passage.Passage)8 Key (org.crosswire.jsword.passage.Key)6 RangedPassage (org.crosswire.jsword.passage.RangedPassage)5 Book (org.crosswire.jsword.book.Book)4 VerseKey (org.crosswire.jsword.passage.VerseKey)4 VerseRange (org.crosswire.jsword.passage.VerseRange)4 Versification (org.crosswire.jsword.versification.Versification)4 NoSuchKeyException (org.crosswire.jsword.passage.NoSuchKeyException)3 RocketPassage (org.crosswire.jsword.passage.RocketPassage)3 StepInternalException (com.tyndalehouse.step.core.exceptions.StepInternalException)2 TranslatedException (com.tyndalehouse.step.core.exceptions.TranslatedException)2 OsisWrapper (com.tyndalehouse.step.core.models.OsisWrapper)2 Verse (org.crosswire.jsword.passage.Verse)2 LocalisedException (com.tyndalehouse.step.core.exceptions.LocalisedException)1 LookupOption (com.tyndalehouse.step.core.models.LookupOption)1 SearchResult (com.tyndalehouse.step.core.models.search.SearchResult)1 IndividualSearch (com.tyndalehouse.step.core.service.impl.IndividualSearch)1 ArrayList (java.util.ArrayList)1 TransformerException (javax.xml.transform.TransformerException)1 JDOMSAXEventProvider (org.crosswire.common.xml.JDOMSAXEventProvider)1