Search in sources :

Example 1 with JDOMSAXEventProvider

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

Aggregations

LocalisedException (com.tyndalehouse.step.core.exceptions.LocalisedException)1 HashMap (java.util.HashMap)1 JDOMSAXEventProvider (org.crosswire.common.xml.JDOMSAXEventProvider)1 XMLUtil.writeToString (org.crosswire.common.xml.XMLUtil.writeToString)1 BookData (org.crosswire.jsword.book.BookData)1 BookException (org.crosswire.jsword.book.BookException)1 Document (org.jdom2.Document)1 Element (org.jdom2.Element)1 ElementFilter (org.jdom2.filter.ElementFilter)1 XMLOutputter (org.jdom2.output.XMLOutputter)1