Search in sources :

Example 1 with BookData

use of org.crosswire.jsword.book.BookData in project BibleMultiConverter by schierlm.

the class SWORD method doImport.

protected Bible doImport(Book book) throws Exception {
    OSISHelper helper = new OSISHelper();
    Bible result = new Bible(book.getName());
    TransformerHandler th = ((SAXTransformerFactory) SAXTransformerFactory.newInstance()).newTransformerHandler();
    Map<BookID, biblemulticonverter.data.Book> parsedBooks = new EnumMap<>(BookID.class);
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    List<Verse> allVerses = new ArrayList<>();
    Verse nextCandidate = null;
    for (Iterator<?> iter = book.getGlobalKeyList().iterator(); iter.hasNext(); ) {
        Verse v = (Verse) iter.next();
        while (nextCandidate != null && !nextCandidate.equals(v)) {
            if (book.contains(nextCandidate)) {
                System.out.println("WARNING: Verse (after) skipped by iterator: " + nextCandidate);
                allVerses.add(nextCandidate);
            }
            nextCandidate = nextCandidate.getVersification().add(nextCandidate, 1);
        }
        Verse prevCandidate = v.getVersification().subtract(v, 1);
        List<Verse> versesSkippedBefore = new ArrayList<>();
        while (prevCandidate != null && !allVerses.contains(prevCandidate) && !versesSkippedBefore.contains(prevCandidate)) {
            versesSkippedBefore.add(0, prevCandidate);
            prevCandidate = prevCandidate.getVersification().subtract(prevCandidate, 1);
        }
        for (Verse vv : versesSkippedBefore) {
            if (book.contains(vv)) {
                System.out.println("WARNING: Verse (before) skipped by iterator: " + vv);
                allVerses.add(vv);
            }
        }
        allVerses.add(v);
        nextCandidate = v.getVersification().add(v, 1);
    }
    while (nextCandidate != null) {
        if (book.contains(nextCandidate)) {
            System.out.println("WARNING: Verse (at end) skipped by iterator: " + nextCandidate);
            allVerses.add(nextCandidate);
        }
        Verse nextNextCandidate = nextCandidate.getVersification().add(nextCandidate, 1);
        if (nextCandidate.equals(nextNextCandidate))
            break;
        nextCandidate = nextNextCandidate;
    }
    for (Verse v : allVerses) {
        BookID bkid = biblemulticonverter.sword.BookMapping.MAPPING.get(v.getBook());
        biblemulticonverter.data.Book bk = parsedBooks.get(bkid);
        if (!parsedBooks.containsKey(bkid)) {
            bk = new biblemulticonverter.data.Book(bkid.getOsisID().replace("x-Intr", "Intr"), bkid, bkid.getEnglishName(), bkid.getEnglishName());
            parsedBooks.put(bkid, bk);
            bk.getChapters().add(new Chapter());
            result.getBooks().add(bk);
        }
        int chapterNum = v.getChapter(), verseNum = v.getVerse();
        while (bk.getChapters().size() < chapterNum) bk.getChapters().add(new Chapter());
        Chapter chapter = bk.getChapters().get(chapterNum == 0 ? 0 : chapterNum - 1);
        FormattedText verse;
        if (verseNum == 0) {
            verse = new FormattedText();
            if (chapter.getProlog() != null) {
                chapter.getProlog().accept(verse.getAppendVisitor());
            }
            chapter.setProlog(verse);
        } else {
            if (chapterNum == 0)
                throw new IllegalStateException("Verse " + verseNum + " in chapter 0 is invalid");
            verse = new biblemulticonverter.data.Verse("" + verseNum);
            chapter.getVerses().add((biblemulticonverter.data.Verse) verse);
        }
        Element root = doc.createElement("verse");
        th.setResult(new DOMResult(root));
        new BookData(book, v).getSAXEventProvider().provideSAXEvents(th);
        if (root.getChildNodes().getLength() == 1 && root.getFirstChild() instanceof Element && root.getFirstChild().getNodeName().equals("div") && root.getFirstChild().getChildNodes().getLength() >= 1 && root.getFirstChild().getFirstChild().getNodeName().equals("title")) {
            Element div = (Element) root.getFirstChild();
            root.removeChild(div);
            div.removeChild(div.getFirstChild());
            while (div.getFirstChild() != null) {
                Node child = div.getFirstChild();
                div.removeChild(child);
                root.appendChild(child);
            }
        } else {
            throw new RuntimeException("Unexpected OSIS structure!");
        }
        helper.handleVerse(root, verse);
        if (verse.getElementTypes(1).length() == 0) {
            System.out.println("WARNING: Empty verse " + bk.getAbbr() + " " + chapterNum + ":" + verseNum);
            if (verse instanceof biblemulticonverter.data.Verse)
                chapter.getVerses().remove(verse);
            else
                chapter.setProlog(null);
        }
    }
    for (biblemulticonverter.data.Book bk : parsedBooks.values()) {
        while (!bk.getChapters().isEmpty()) {
            Chapter ch = bk.getChapters().get(bk.getChapters().size() - 1);
            if (ch.getProlog() == null && ch.getVerses().isEmpty()) {
                bk.getChapters().remove(ch);
            } else {
                break;
            }
        }
        if (bk.getChapters().isEmpty()) {
            result.getBooks().remove(bk);
        }
    }
    return result;
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) DOMResult(javax.xml.transform.dom.DOMResult) Bible(biblemulticonverter.data.Bible) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) BookID(biblemulticonverter.data.BookID) Book(org.crosswire.jsword.book.Book) BookData(org.crosswire.jsword.book.BookData) EnumMap(java.util.EnumMap) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) Chapter(biblemulticonverter.data.Chapter) FormattedText(biblemulticonverter.data.FormattedText) Verse(org.crosswire.jsword.passage.Verse)

Aggregations

Bible (biblemulticonverter.data.Bible)1 BookID (biblemulticonverter.data.BookID)1 Chapter (biblemulticonverter.data.Chapter)1 FormattedText (biblemulticonverter.data.FormattedText)1 ArrayList (java.util.ArrayList)1 EnumMap (java.util.EnumMap)1 DOMResult (javax.xml.transform.dom.DOMResult)1 SAXTransformerFactory (javax.xml.transform.sax.SAXTransformerFactory)1 TransformerHandler (javax.xml.transform.sax.TransformerHandler)1 Book (org.crosswire.jsword.book.Book)1 BookData (org.crosswire.jsword.book.BookData)1 Verse (org.crosswire.jsword.passage.Verse)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1