Search in sources :

Example 1 with LocalisedException

use of com.tyndalehouse.step.core.exceptions.LocalisedException in project step by STEPBible.

the class AbstractAjaxController method getExceptionMessageAndLog.

/**
 * Gets the exception message.
 *
 * @param e the e
 * @return the exception message
 */
private String getExceptionMessageAndLog(final Throwable e) {
    LOGGER.trace("Tracing exception: ", e);
    final Locale locale = this.clientSessionProvider.get().getLocale();
    final ResourceBundle bundle = ResourceBundle.getBundle("ErrorBundle", locale);
    if (!(e instanceof StepInternalException)) {
        return returnInternalError(e, bundle);
    }
    // else we're looking at a STEP caught exception
    if (e instanceof LocalisedException) {
        return e.getMessage();
    }
    if (e instanceof TranslatedException) {
        final TranslatedException translatedException = (TranslatedException) e;
        LOGGER.warn(e.getMessage());
        LOGGER.debug(e.getMessage(), e);
        return format(bundle.getString(translatedException.getMessage()), translatedException.getArgs());
    }
    if (e instanceof ValidationException) {
        final ValidationException validationException = (ValidationException) e;
        switch(validationException.getExceptionType()) {
            case LOGIN_REQUIRED:
                return bundle.getString("error_login");
            case USER_MISSING_FIELD:
                return bundle.getString("error_missing_field");
            case USER_VALIDATION_ERROR:
                return bundle.getString("error_validation");
            case APP_MISSING_FIELD:
            case CONTROLLER_INITIALISATION_ERROR:
            case SERVICE_VALIDATION_ERROR:
            default:
                return returnInternalError(e, bundle);
        }
    }
    return returnInternalError(e, bundle);
}
Also used : Locale(java.util.Locale) LocalisedException(com.tyndalehouse.step.core.exceptions.LocalisedException) StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) ValidationException(com.tyndalehouse.step.core.exceptions.ValidationException) TranslatedException(com.tyndalehouse.step.core.exceptions.TranslatedException) ResourceBundle(java.util.ResourceBundle)

Example 2 with LocalisedException

use of com.tyndalehouse.step.core.exceptions.LocalisedException 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 LocalisedException

use of com.tyndalehouse.step.core.exceptions.LocalisedException 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 4 with LocalisedException

use of com.tyndalehouse.step.core.exceptions.LocalisedException in project step by STEPBible.

the class JSwordPassageServiceImpl method buildInterleavedVersions.

/**
 * Changes the input OSIS document to have extra verses, the ones from the other versions
 *
 * @param bookDatas the list of all book datas that we will be querying
 * @return the provider of events for the stylesheet to execute upon
 */
SAXEventProvider buildInterleavedVersions(final BookData... bookDatas) {
    final Map<String, Element> versions = new HashMap<String, Element>();
    try {
        // obtain OSIS from every version
        for (final BookData bookData : bookDatas) {
            final Element osis = bookData.getOsis();
            versions.put(bookData.getFirstBook().getInitials(), osis);
        }
        final Filter<Element> verseFilter = new ElementFilter(OSIS_ELEMENT_VERSE);
        // select one version and iterate through the others and change the OSIS
        boolean firstVersion = true;
        final Map<String, Element> versesFromMaster = new HashMap<String, Element>();
        // iterate through documents of every version
        for (final BookData data : bookDatas) {
            final String version = data.getFirstBook().getInitials();
            final Element element = versions.get(version);
            final Iterator<Element> docIterator = element.getDescendants(verseFilter);
            Element previousAppendedElement = null;
            // save the first version
            while (docIterator.hasNext()) {
                final Element e = docIterator.next();
                LOGGER.debug("Obtaining verse [{}]", e.getAttributeValue(OSIS_ATTR_OSISID));
                final String osisID = e.getAttributeValue(OSIS_ATTR_OSISID).toLowerCase();
                if (firstVersion) {
                    versesFromMaster.put(osisID, e);
                } else {
                    Element childVerse = versesFromMaster.get(osisID);
                    if (childVerse == null) {
                        LOGGER.debug("Orphaned row: [{}]", osisID);
                        childVerse = previousAppendedElement;
                    }
                    final Element parentElement = childVerse.getParentElement();
                    parentElement.addContent(parentElement.indexOf(childVerse), e.clone());
                    previousAppendedElement = childVerse;
                }
            }
            firstVersion = false;
        }
        final Element amendedOsis = versions.get(bookDatas[0].getFirstBook().getInitials());
        Document doc = amendedOsis.getDocument();
        if (doc == null) {
            doc = new Document(amendedOsis);
        }
        if (LOGGER.isDebugEnabled()) {
            final XMLOutputter xmlOutputter = new XMLOutputter(Format.getRawFormat());
            LOGGER.debug("\n {}", xmlOutputter.outputString(doc));
        }
        return new JDOMSAXEventProvider(doc);
    } catch (final BookException e) {
        throw new LocalisedException(e, e.getMessage());
    }
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) HashMap(java.util.HashMap) Element(org.jdom2.Element) BookException(org.crosswire.jsword.book.BookException) XMLUtil.writeToString(org.crosswire.common.xml.XMLUtil.writeToString) Document(org.jdom2.Document) LocalisedException(com.tyndalehouse.step.core.exceptions.LocalisedException) ElementFilter(org.jdom2.filter.ElementFilter) JDOMSAXEventProvider(org.crosswire.common.xml.JDOMSAXEventProvider) BookData(org.crosswire.jsword.book.BookData)

Example 5 with LocalisedException

use of com.tyndalehouse.step.core.exceptions.LocalisedException in project step by STEPBible.

the class JSwordPassageServiceImpl method getPlainText.

@Override
public String getPlainText(final String version, final String reference, final boolean firstVerse) {
    final Book book = this.versificationService.getBookFromVersion(version);
    try {
        Key key = book.getKey(reference);
        if (firstVerse) {
            key = getFirstVerseExcludingZero(key, book);
        }
        final BookData data = new BookData(book, key);
        return OSISUtil.getCanonicalText(data.getOsisFragment());
    } catch (final BookException e) {
        throw new LocalisedException(e, e.getMessage());
    } catch (final NoSuchKeyException e) {
        throw new TranslatedException(e, "invalid_reference_in_book", reference, version);
    }
}
Also used : LocalisedException(com.tyndalehouse.step.core.exceptions.LocalisedException) NoSuchKeyException(org.crosswire.jsword.passage.NoSuchKeyException) TranslatedException(com.tyndalehouse.step.core.exceptions.TranslatedException) BibleBook(org.crosswire.jsword.versification.BibleBook) Book(org.crosswire.jsword.book.Book) BookException(org.crosswire.jsword.book.BookException) BookData(org.crosswire.jsword.book.BookData) Key(org.crosswire.jsword.passage.Key)

Aggregations

LocalisedException (com.tyndalehouse.step.core.exceptions.LocalisedException)5 BookException (org.crosswire.jsword.book.BookException)4 StepInternalException (com.tyndalehouse.step.core.exceptions.StepInternalException)3 TranslatedException (com.tyndalehouse.step.core.exceptions.TranslatedException)3 Book (org.crosswire.jsword.book.Book)3 Key (org.crosswire.jsword.passage.Key)3 BibleBook (org.crosswire.jsword.versification.BibleBook)3 OsisWrapper (com.tyndalehouse.step.core.models.OsisWrapper)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 BookData (org.crosswire.jsword.book.BookData)2 NoSuchKeyException (org.crosswire.jsword.passage.NoSuchKeyException)2 SAXException (org.xml.sax.SAXException)2 ValidationException (com.tyndalehouse.step.core.exceptions.ValidationException)1 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1 ResourceBundle (java.util.ResourceBundle)1 SAXEventProvider (org.crosswire.common.xml.SAXEventProvider)1