Search in sources :

Example 1 with VerseRange

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

the class JSwordPassageServiceImpl method getOsisTextByVerseNumbers.

@Override
public OsisWrapper getOsisTextByVerseNumbers(final String version, final String numberedVersion, final int startVerseId, final int endVerseId, final List<LookupOption> lookupOptions, final String interlinearVersion, final Boolean roundReference, final boolean ignoreVerse0) {
    // coded from numbered version.
    final Versification versificationForNumberedVersion = this.versificationService.getVersificationForVersion(numberedVersion);
    final Verse s = versificationForNumberedVersion.decodeOrdinal(startVerseId);
    final Verse e = versificationForNumberedVersion.decodeOrdinal(endVerseId);
    // convert it over to target versification
    final Book lookupVersion = this.versificationService.getBookFromVersion(version);
    final VerseRange range = this.versificationService.getVerseRangeForSelectedVerses(version, numberedVersion, versificationForNumberedVersion, s, e, lookupVersion, roundReference, ignoreVerse0);
    final BookData lookupBookData = new BookData(lookupVersion, range);
    return getTextForBookData(lookupOptions, interlinearVersion, lookupBookData, NONE);
}
Also used : VerseRange(org.crosswire.jsword.passage.VerseRange) BibleBook(org.crosswire.jsword.versification.BibleBook) Book(org.crosswire.jsword.book.Book) Versification(org.crosswire.jsword.versification.Versification) BookData(org.crosswire.jsword.book.BookData) Verse(org.crosswire.jsword.passage.Verse)

Example 2 with VerseRange

use of org.crosswire.jsword.passage.VerseRange 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 3 with VerseRange

use of org.crosswire.jsword.passage.VerseRange 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 4 with VerseRange

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

the class SubjectEntryServiceImpl method isCloseVerse.

/**
 * @param av11n     the versification
 * @param range     the range/verse
 * @param lastVerse the last verse
 * @return true if the verse should be wrapped in with the range before
 */
private boolean isCloseVerse(final Versification av11n, final Verse lastVerse, final Key range) {
    Verse startOfNextRange = null;
    if (range instanceof VerseRange) {
        final VerseRange verseRange = (VerseRange) range;
        startOfNextRange = verseRange.getStart();
    } else if (range instanceof Verse) {
        startOfNextRange = (Verse) range;
    } else {
        // unable to determine whether the verses are close or not...
        return false;
    }
    final int distance = Math.abs(av11n.distance(lastVerse, startOfNextRange));
    if (distance < AGGREGATING_VERSE_DISTANCE) {
        return true;
    }
    return false;
}
Also used : VerseRange(org.crosswire.jsword.passage.VerseRange) Verse(org.crosswire.jsword.passage.Verse)

Example 5 with VerseRange

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

the class JSwordPassageServiceImpl method getFirstVersesFromRange.

@Override
public Key getFirstVersesFromRange(final Key range, final int context) {
    if (range instanceof VerseRange) {
        final VerseRange verseRange = (VerseRange) range;
        final Iterator<Key> iterator = verseRange.iterator();
        if (!iterator.hasNext()) {
            // empty range
            return range;
        }
        Passage p = KeyUtil.getPassage(range);
        final int totalWantedVerses = context + 1;
        final int currentBefore = range.getCardinality();
        p.trimVerses(totalWantedVerses);
        final int currentNow = p.getCardinality();
        final int totalAdded = currentNow - currentBefore;
        final int leftToCollect = totalWantedVerses - currentNow;
        if (leftToCollect > 0) {
            p.blur(context, RestrictionType.NONE, true, false);
            if (totalAdded < context) {
                p.blur(context - totalAdded, RestrictionType.NONE, true, false);
            }
        }
        return p;
    }
    return range;
}
Also used : VerseRange(org.crosswire.jsword.passage.VerseRange) RocketPassage(org.crosswire.jsword.passage.RocketPassage) Passage(org.crosswire.jsword.passage.Passage) Key(org.crosswire.jsword.passage.Key)

Aggregations

VerseRange (org.crosswire.jsword.passage.VerseRange)7 Verse (org.crosswire.jsword.passage.Verse)5 Key (org.crosswire.jsword.passage.Key)4 Passage (org.crosswire.jsword.passage.Passage)4 Versification (org.crosswire.jsword.versification.Versification)4 Book (org.crosswire.jsword.book.Book)3 RocketPassage (org.crosswire.jsword.passage.RocketPassage)3 StepInternalException (com.tyndalehouse.step.core.exceptions.StepInternalException)2 OsisWrapper (com.tyndalehouse.step.core.models.OsisWrapper)2 NoSuchKeyException (org.crosswire.jsword.passage.NoSuchKeyException)2 BibleBook (org.crosswire.jsword.versification.BibleBook)2 LocalisedException (com.tyndalehouse.step.core.exceptions.LocalisedException)1 TranslatedException (com.tyndalehouse.step.core.exceptions.TranslatedException)1 LookupOption (com.tyndalehouse.step.core.models.LookupOption)1 ArrayList (java.util.ArrayList)1 TransformerException (javax.xml.transform.TransformerException)1 JDOMSAXEventProvider (org.crosswire.common.xml.JDOMSAXEventProvider)1 SAXEventProvider (org.crosswire.common.xml.SAXEventProvider)1 TransformingSAXEventProvider (org.crosswire.common.xml.TransformingSAXEventProvider)1 BookData (org.crosswire.jsword.book.BookData)1