Skip to content
Tom L edited this page Dec 21, 2019 · 2 revisions

Tool to merge a commentary (formatted like an OSIS Bible) into another OSIS Bible text.

This was quickly hacked together. It can obviously be done way more efficiently by someone who is more familiar with these libraries.

import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import java.io.File;

public class Main {

    public static void main(String[] args) throws Exception {
        File bible = new File("bible.xml");
        File comment = new File("commentary.xml");
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document bibleDoc = db.parse(bible);
        Document commentDoc = db.parse(comment);

        String[] bibleBookNames = {"Gen", "Exod", "Lev", "Num", "Deut", "Josh", "Judg", "Ruth", "1Sam", "2Sam", "1Kgs", "2Kgs", "1Chr", "2Chr",
                "Ezra", "Neh", "Esth", "Job", "Ps", "Prov", "Eccl", "Song", "Isa", "Jer", "Lam", "Ezek", "Dan", "Hos", "Joel", "Amos", "Obad",
                "Jonah", "Mic", "Nah", "Hab", "Zeph", "Hag", "Zech", "Mal", "Matt", "Mark", "Luke", "John", "Acts", "Rom", "1Cor", "2Cor", "Gal",
                "Eph", "Phil", "Col", "1Thess", "2Thess", "1Tim", "2Tim", "Titus", "Phlm", "Heb", "Jas", "1Pet", "2Pet", "1John", "2John", "3John", "Jude", "Rev"};
        Integer[] bibleBookLengths = {50, 40, 27, 36, 34, 24, 21, 4, 31, 24, 22, 25, 29, 36, 10, 13, 10, 42, 150, 31, 12, 8, 66, 52, 5, 48, 12, 14, 3, 9, 1, 4, 7,
                3, 3, 3, 2, 14, 4, 28, 16, 24, 21, 28, 16, 16, 13, 6, 6, 4, 4, 5, 3, 6, 4, 3, 1, 13, 5, 5, 3, 5, 1, 1, 1, 22};
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();

        int counter = 0;
        for (String bookName : bibleBookNames) {
            for (int chapter = 1; chapter < bibleBookLengths[counter] + 1; chapter++) {
                int emptyVerses = 0;
                for (int verse = 1; verse < 177; verse++) {
                    XPathExpression expr = xpath.compile(String.format("//verse[@osisID=\"%s.%d.%d\"]", bookName, chapter, verse));
                    NodeList verseNode = (NodeList) expr.evaluate(bibleDoc, XPathConstants.NODESET);
                    NodeList commentNode = (NodeList) expr.evaluate(commentDoc, XPathConstants.NODESET);

                    if (verseNode.getLength() > 0 && commentNode.getLength() > 0) {
                        emptyVerses = 0;
                        for (int j = 0; j < commentNode.item(0).getChildNodes().getLength(); j++) {
                            Node newNode = commentNode.item(0).getChildNodes().item(j);
                            newNode = bibleDoc.importNode(newNode, true);
                            verseNode.item(0).appendChild(newNode);
                        }
                    } else if (verseNode.getLength() > 0 || commentNode.getLength() > 0) {
                        emptyVerses = 0;
                    } else {
                        emptyVerses++;
                        if (emptyVerses > 3) {
                            break;
                        }
                    }
                }
                System.out.println(bookName + " " + chapter + " is done.");
            }
            counter++;
        }
        print(bibleDoc);
    }

    private static void print(Document doc) throws Exception {
        TransformerFactory transformerFactory = TransformerFactory
                .newInstance();
        Transformer transformer = transformerFactory
                .newTransformer();
        DOMSource source = new DOMSource(doc);
        //Result result = new StreamResult(System.out);
        Result result = new StreamResult(new File("text.xml"));
        transformer.transform(source, result);
    }
}