Search in sources :

Example 1 with OsisWrapper

use of com.tyndalehouse.step.core.models.OsisWrapper in project step by STEPBible.

the class BibleInformationServiceImpl method getPassageText.

/**
 * Gets the passage text.
 *
 * @param version            the version
 * @param reference          the reference
 * @param options            the options
 * @param interlinearVersion the interlinear version
 * @param interlinearMode    the interlinear mode
 * @return the passage text
 */
// TODO: this could be optimized. last call to get options is very  similar to 'getLookupOptions'
// as they share some of the same stuff.
@Override
@Timed(name = "passage-lookup", group = "service", rateUnit = TimeUnit.SECONDS, durationUnit = TimeUnit.MILLISECONDS)
public OsisWrapper getPassageText(final String version, final String reference, final String options, final String interlinearVersion, final String interlinearMode, final String userLanguage) {
    final List<String> extraVersions = getExtraVersionsFromString(interlinearVersion);
    final InterlinearMode desiredModeOfDisplay = this.optionsValidationService.getDisplayMode(interlinearMode, version, extraVersions);
    OsisWrapper passageText;
    final List<TrimmedLookupOption> removedOptions = new ArrayList<TrimmedLookupOption>(4);
    final List<LookupOption> inputLookupOptions = this.optionsValidationService.getLookupOptions(options);
    final InterlinearMode realModeOfDisplay = this.optionsValidationService.determineDisplayMode(inputLookupOptions, desiredModeOfDisplay, true);
    final Set<LookupOption> lookupOptions = this.optionsValidationService.trim(inputLookupOptions, version, extraVersions, desiredModeOfDisplay, realModeOfDisplay, removedOptions);
    if (INTERLINEAR != desiredModeOfDisplay && NONE != desiredModeOfDisplay) {
        // split the versions
        lookupOptions.add(LookupOption.VERSE_NUMBERS);
        final String[] versions = getInterleavedVersions(version, interlinearVersion);
        passageText = this.jswordPassage.getInterleavedVersions(versions, reference, new ArrayList<>(lookupOptions), desiredModeOfDisplay, userLanguage);
    } else {
        passageText = this.jswordPassage.getOsisText(version, reference, new ArrayList(lookupOptions), interlinearVersion, desiredModeOfDisplay);
    }
    passageText.setRemovedOptions(removedOptions);
    passageText.setPreviousChapter(this.jswordPassage.getSiblingChapter(passageText.getOsisId(), version, true));
    passageText.setNextChapter(this.jswordPassage.getSiblingChapter(passageText.getOsisId(), version, false));
    passageText.setOptions(this.optionsValidationService.optionsToString(this.optionsValidationService.getAvailableFeaturesForVersion(version, extraVersions, interlinearMode, realModeOfDisplay).getOptions()));
    // the passage lookup wasn't made with the removed options, however, the client needs to think these were selected.
    passageText.setSelectedOptions(this.optionsValidationService.optionsToString(lookupOptions) + getRemovedOptions(removedOptions));
    return passageText;
}
Also used : TrimmedLookupOption(com.tyndalehouse.step.core.models.TrimmedLookupOption) LookupOption(com.tyndalehouse.step.core.models.LookupOption) TrimmedLookupOption(com.tyndalehouse.step.core.models.TrimmedLookupOption) EnrichedLookupOption(com.tyndalehouse.step.core.models.EnrichedLookupOption) ArrayList(java.util.ArrayList) InterlinearMode(com.tyndalehouse.step.core.models.InterlinearMode) OsisWrapper(com.tyndalehouse.step.core.models.OsisWrapper) Timed(com.yammer.metrics.annotation.Timed)

Example 2 with OsisWrapper

use of com.tyndalehouse.step.core.models.OsisWrapper in project step by STEPBible.

the class JSwordSearchServiceImpl method getPassagesForResults.

/**
 * Looks up all passages represented by the key
 *
 * @param result the results that we will be returning
 * @param versions   the bibles under examination
 * @param results the list of results
 * @param context amount of context to add
 * @param options to use to lookup the right parameterization of the text
 * @return the list of entries found
 */
private void getPassagesForResults(SearchResult result, String[] versions, final Key results, final int context, final List<LookupOption> options, String interlinearMode) {
    final List<SearchEntry> resultPassages = new ArrayList<SearchEntry>();
    final Iterator<Key> iterator = ((Passage) results).iterator();
    boolean interlinearModeCaptured = false;
    int count = 0;
    while (iterator.hasNext()) {
        final Key verse = iterator.next();
        final Key lookupKey;
        if (verse instanceof Verse) {
            // then we need to make it into a verse range
            final Verse verseAsVerse = (Verse) verse;
            final VerseRange vr = new VerseRange(verseAsVerse.getVersification(), verseAsVerse);
            vr.blur(context, RestrictionType.NONE);
            lookupKey = vr;
        } else {
            // assume blur is supported
            verse.blur(context, RestrictionType.NONE);
            lookupKey = verse;
        }
        if (count == 1) {
            options.add(LookupOption.HIDE_COMPARE_HEADERS);
        }
        // TODO this is not very efficient so requires refactoring
        final OsisWrapper peakOsisText = this.jsword.peakOsisText(versions, lookupKey, options, interlinearMode);
        resultPassages.add(new VerseSearchEntry(peakOsisText.getReference(), peakOsisText.getValue(), peakOsisText.getOsisId()));
        if (!interlinearModeCaptured) {
            result.setInterlinearMode(peakOsisText.getInterlinearMode());
            interlinearModeCaptured = true;
        }
        count++;
    }
    result.setResults(resultPassages);
}
Also used : VerseSearchEntry(com.tyndalehouse.step.core.models.search.VerseSearchEntry) VerseSearchEntry(com.tyndalehouse.step.core.models.search.VerseSearchEntry) SearchEntry(com.tyndalehouse.step.core.models.search.SearchEntry) OsisWrapper(com.tyndalehouse.step.core.models.OsisWrapper)

Example 3 with OsisWrapper

use of com.tyndalehouse.step.core.models.OsisWrapper in project step by STEPBible.

the class JSwordPassageServiceImpl method getPassageByDisplayMode.

public OsisWrapper getPassageByDisplayMode(List<String> versionsInput, Key reference, List<LookupOption> options, final String interlinearMode) {
    if (versionsInput.size() == 0) {
        throw new StepInternalException("No versions specified - app error?");
    }
    final String masterVersion = versionsInput.get(0);
    final List<String> extraVersions = this.getExtras(versionsInput);
    final InterlinearMode desiredModeOfDisplay = this.optionsValidationService.getDisplayMode(interlinearMode, masterVersion, extraVersions);
    final InterlinearMode realModeOfDisplay = this.optionsValidationService.determineDisplayMode(options, desiredModeOfDisplay, true);
    if (InterlinearMode.INTERLINEAR.equals(desiredModeOfDisplay) && options.contains(LookupOption.CHAPTER_BOOK_VERSE_NUMBER)) {
        // then we're in a search kind of lookup, so add proper verse numbers
        options.add(LookupOption.VERSE_NUMBERS);
    }
    OsisWrapper passageText;
    final Set<LookupOption> lookupOptions = this.optionsValidationService.trim(options, masterVersion, extraVersions, desiredModeOfDisplay, null);
    if (INTERLINEAR != desiredModeOfDisplay && NONE != desiredModeOfDisplay) {
        // split the versions
        passageText = this.getInterleavedVersions(versionsInput.toArray(new String[versionsInput.size()]), reference, new ArrayList<LookupOption>(lookupOptions), desiredModeOfDisplay, "en");
    } else {
        final String extraVersionsAsString = this.getVersionsAsStrings(extraVersions);
        passageText = this.getOsisText(masterVersion, reference, new ArrayList<LookupOption>(lookupOptions), extraVersionsAsString, desiredModeOfDisplay);
    }
    passageText.setOptions(this.optionsValidationService.optionsToString(this.optionsValidationService.getAvailableFeaturesForVersion(masterVersion, extraVersions, interlinearMode, realModeOfDisplay).getOptions()));
    // The following 15 lines are a patch for the lack of the Book name, chapter and verse for the deutrocanon books
    String tmp = passageText.getValue();
    int pos1 = tmp.indexOf("<span class='verseNumber'></span>");
    if (pos1 > -1) {
        String curOsisId = passageText.getOsisId();
        int pos2 = curOsisId.indexOf(".");
        if (pos2 > 0) {
            String OsisBookName = " " + curOsisId.substring(0, pos2).toLowerCase() + " ";
            String otherBooks = " 1esd 2esd tob jdt addesth wis sir bar prazar sus bel prman 1macc 2macc esthgr addps 3macc 4macc ";
            if (otherBooks.indexOf(OsisBookName) > -1) {
                pos1 += 26;
                passageText.setValue(tmp.substring(0, pos1) + curOsisId + tmp.substring(pos1));
            }
        }
    }
    passageText.setSelectedOptions(this.optionsValidationService.optionsToString(lookupOptions));
    return passageText;
}
Also used : StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) LookupOption(com.tyndalehouse.step.core.models.LookupOption) ArrayList(java.util.ArrayList) XMLUtil.writeToString(org.crosswire.common.xml.XMLUtil.writeToString) InterlinearMode(com.tyndalehouse.step.core.models.InterlinearMode) OsisWrapper(com.tyndalehouse.step.core.models.OsisWrapper)

Example 4 with OsisWrapper

use of com.tyndalehouse.step.core.models.OsisWrapper 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 OsisWrapper

use of com.tyndalehouse.step.core.models.OsisWrapper 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

OsisWrapper (com.tyndalehouse.step.core.models.OsisWrapper)13 LookupOption (com.tyndalehouse.step.core.models.LookupOption)7 ArrayList (java.util.ArrayList)6 StepInternalException (com.tyndalehouse.step.core.exceptions.StepInternalException)4 Book (org.crosswire.jsword.book.Book)4 Test (org.junit.Test)4 Key (org.crosswire.jsword.passage.Key)3 BibleBook (org.crosswire.jsword.versification.BibleBook)3 LocalisedException (com.tyndalehouse.step.core.exceptions.LocalisedException)2 EnrichedLookupOption (com.tyndalehouse.step.core.models.EnrichedLookupOption)2 InterlinearMode (com.tyndalehouse.step.core.models.InterlinearMode)2 TrimmedLookupOption (com.tyndalehouse.step.core.models.TrimmedLookupOption)2 StringReader (java.io.StringReader)2 TransformerException (javax.xml.transform.TransformerException)2 TransformingSAXEventProvider (org.crosswire.common.xml.TransformingSAXEventProvider)2 XMLUtil.writeToString (org.crosswire.common.xml.XMLUtil.writeToString)2 BookData (org.crosswire.jsword.book.BookData)2 BookException (org.crosswire.jsword.book.BookException)2 NoSuchKeyException (org.crosswire.jsword.passage.NoSuchKeyException)2 Passage (org.crosswire.jsword.passage.Passage)2