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