use of org.crosswire.common.xml.JDOMSAXEventProvider 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());
}
}
Aggregations