Search in sources :

Example 1 with TransformingSAXEventProvider

use of org.crosswire.common.xml.TransformingSAXEventProvider in project step by STEPBible.

the class JSwordPassageServiceImpl method executeStyleSheet.

/**
 * Executes the stylesheet
 *
 * @param masterVersification the versification of the top line
 * @param options             the list of options to pass in
 * @param interlinearVersion  the interlinear version(s)
 * @param bookData            the book data, containing book and reference
 * @param osissep             the XML SAX provider
 * @param displayMode         the display mode
 * @return a Transforming SAX event provider, from which can be transformed into HTML
 * @throws TransformerException an exception in the stylesheet that is being executed
 */
private TransformingSAXEventProvider executeStyleSheet(final Versification masterVersification, final List<LookupOption> options, final String interlinearVersion, final BookData bookData, final SAXEventProvider osissep, final InterlinearMode displayMode, final String userLanguage) throws TransformerException {
    final XslConversionType requiredTransformation = identifyStyleSheet(options, displayMode);
    return (TransformingSAXEventProvider) new Converter() {

        @Override
        public SAXEventProvider convert(final SAXEventProvider provider) throws TransformerException {
            try {
                final String file = requiredTransformation.getFile();
                final URI resourceURI = getClass().getResource(file).toURI();
                final TransformingSAXEventProvider tsep = new TransformingSAXEventProvider(resourceURI, osissep);
                // set parameters here
                String changeVersion = "";
                if (userLanguage.toLowerCase().startsWith("es"))
                    changeVersion = "SpaRV1909";
                else if (userLanguage.equalsIgnoreCase("zh"))
                    changeVersion = "CUns";
                else if (userLanguage.equalsIgnoreCase("zh_tw"))
                    changeVersion = "CUn";
                else if (userLanguage.toLowerCase().startsWith("bg"))
                    changeVersion = "BulProtRev";
                else if (userLanguage.toLowerCase().startsWith("hi"))
                    changeVersion = "HinULB";
                if (changeVersion.length() > 0) {
                    tsep.setParameter("defaultVersion", changeVersion);
                }
                setOptions(tsep, options, bookData.getBooks());
                setInterlinearOptions(tsep, bookData.getBooks()[0].getInitials(), masterVersification, getInterlinearVersion(interlinearVersion), bookData.getKey().getOsisID(), displayMode, bookData.getKey(), options);
                setInterleavingOptions(tsep, displayMode, bookData);
                return tsep;
            } catch (final URISyntaxException e) {
                throw new StepInternalException("Failed to load resource correctly", e);
            }
        }
    }.convert(osissep);
}
Also used : TransformingSAXEventProvider(org.crosswire.common.xml.TransformingSAXEventProvider) StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) Converter(org.crosswire.common.xml.Converter) TransformingSAXEventProvider(org.crosswire.common.xml.TransformingSAXEventProvider) JDOMSAXEventProvider(org.crosswire.common.xml.JDOMSAXEventProvider) SAXEventProvider(org.crosswire.common.xml.SAXEventProvider) XMLUtil.writeToString(org.crosswire.common.xml.XMLUtil.writeToString) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) XslConversionType(com.tyndalehouse.step.core.xsl.XslConversionType)

Example 2 with TransformingSAXEventProvider

use of org.crosswire.common.xml.TransformingSAXEventProvider 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 3 with TransformingSAXEventProvider

use of org.crosswire.common.xml.TransformingSAXEventProvider 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

StepInternalException (com.tyndalehouse.step.core.exceptions.StepInternalException)3 TransformingSAXEventProvider (org.crosswire.common.xml.TransformingSAXEventProvider)3 LocalisedException (com.tyndalehouse.step.core.exceptions.LocalisedException)2 OsisWrapper (com.tyndalehouse.step.core.models.OsisWrapper)2 TransformerException (javax.xml.transform.TransformerException)2 JDOMSAXEventProvider (org.crosswire.common.xml.JDOMSAXEventProvider)2 SAXEventProvider (org.crosswire.common.xml.SAXEventProvider)2 XMLUtil.writeToString (org.crosswire.common.xml.XMLUtil.writeToString)2 Book (org.crosswire.jsword.book.Book)2 BookException (org.crosswire.jsword.book.BookException)2 Key (org.crosswire.jsword.passage.Key)2 BibleBook (org.crosswire.jsword.versification.BibleBook)2 SAXException (org.xml.sax.SAXException)2 TranslatedException (com.tyndalehouse.step.core.exceptions.TranslatedException)1 XslConversionType (com.tyndalehouse.step.core.xsl.XslConversionType)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Converter (org.crosswire.common.xml.Converter)1 NoSuchKeyException (org.crosswire.jsword.passage.NoSuchKeyException)1 Passage (org.crosswire.jsword.passage.Passage)1