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);
}
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());
}
}
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());
}
}
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());
}
}
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);
}
}
Aggregations