Search in sources :

Example 16 with Bible

use of biblemulticonverter.data.Bible in project BibleMultiConverter by schierlm.

the class Diffable method doImport.

protected Bible doImport(BufferedReader br) throws IOException {
    String line = br.readLine();
    if (!line.startsWith(MAGIC))
        throw new IOException("Invalid header line: " + line);
    Bible result = new Bible(line.substring(MAGIC.length()));
    Map<String, Book> bookMap = new HashMap<String, Book>();
    while ((line = br.readLine()) != null) {
        line = line.trim();
        if (line.length() == 0 || line.startsWith("#"))
            continue;
        String[] parts = line.split(" ", 3);
        if (parts.length != 3)
            throw new IOException("Not enough fields: " + line);
        try {
            if (parts[1].equals("=")) {
                String[] fields = parts[2].split("\t");
                if (fields.length != 3)
                    throw new IOException("Malformed header line (not 3 fields): " + parts[3]);
                BookID id = BookID.fromOsisId(fields[0]);
                if (id == null)
                    throw new IOException("Unknown book ID: " + fields[0]);
                Book newBook = new Book(parts[0], id, fields[1], fields[2]);
                result.getBooks().add(newBook);
                Book oldBook = bookMap.get(parts[0]);
                if (oldBook != null) {
                    newBook.getChapters().addAll(oldBook.getChapters());
                    result.getBooks().remove(oldBook);
                }
                bookMap.put(parts[0], newBook);
                continue;
            }
            Book book = bookMap.get(parts[0]);
            if (book == null)
                throw new IOException("Unknown book prefix (header line missing?): " + parts[0]);
            if (parts[1].equals("->")) {
                if (!parts[2].equals("-")) {
                    new StrippedDiffable().renameBookInXref(result, parts[0], parts[2], false);
                    Book destBook = bookMap.get(parts[2]);
                    if (destBook == null)
                        throw new IOException("Unknown destination book (header line missing?): " + parts[2]);
                    destBook.getChapters().addAll(book.getChapters());
                }
                result.getBooks().remove(book);
                bookMap.remove(parts[0]);
                continue;
            } else if (parts[1].equals("^^")) {
                Book destBook = bookMap.get(parts[2]);
                if (destBook == null)
                    throw new IOException("Unknown destination book (header line missing?): " + parts[2]);
                result.getBooks().remove(book);
                result.getBooks().add(result.getBooks().indexOf(destBook), book);
                continue;
            }
            int chapterNumber;
            String verse;
            if (parts[1].contains(":")) {
                String[] chapVerse = parts[1].split(":", 2);
                chapterNumber = Integer.parseInt(chapVerse[0]);
                verse = chapVerse[1];
            } else {
                chapterNumber = Integer.parseInt(parts[1]);
                verse = null;
            }
            while (book.getChapters().size() < chapterNumber) {
                book.getChapters().add(new Chapter());
            }
            Chapter chapter = book.getChapters().get(chapterNumber - 1);
            FormattedText target;
            if (verse == null) {
                if (chapter.getProlog() == null)
                    chapter.setProlog(new FormattedText());
                target = chapter.getProlog();
            } else {
                int idx = chapter.getVerseIndex(verse);
                if (idx == -1) {
                    Verse v = new Verse(verse);
                    chapter.getVerses().add(v);
                    target = v;
                } else {
                    target = chapter.getVerses().get(idx);
                }
            }
            parseDiffable(target.getAppendVisitor(), parts[2]);
        } catch (Exception ex) {
            throw new IOException("Error while parsing line: " + line, ex);
        }
    }
    for (Book book : result.getBooks()) {
        for (Chapter chapter : book.getChapters()) {
            if (chapter.getProlog() != null)
                chapter.getProlog().finished();
            for (Verse v : chapter.getVerses()) v.finished();
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) Bible(biblemulticonverter.data.Bible) Chapter(biblemulticonverter.data.Chapter) IOException(java.io.IOException) FormattedText(biblemulticonverter.data.FormattedText) IOException(java.io.IOException) BookID(biblemulticonverter.data.BookID) Book(biblemulticonverter.data.Book) Verse(biblemulticonverter.data.Verse)

Example 17 with Bible

use of biblemulticonverter.data.Bible in project BibleMultiConverter by schierlm.

the class HaggaiXML method parseBible.

protected Bible parseBible(XMLBIBLE doc) throws Exception {
    Bible result = new Bible(doc.getBiblename());
    MetadataBook metadata = new MetadataBook();
    if (doc.getStatus() != null) {
        metadata.setValue(MetadataBookKey.status, doc.getStatus().value());
    }
    if (doc.getVersion() != null) {
        metadata.setValue(MetadataBookKey.version, doc.getVersion());
    }
    if (doc.getRevision() != null) {
        metadata.setValue(MetadataBookKey.revision, doc.getRevision().toString());
    }
    for (JAXBElement<?> elem : doc.getINFORMATION().getValue().getTitleOrCreatorOrDescription()) {
        if (elem.getValue() == null)
            continue;
        String value = normalize(elem.getValue().toString(), true).trim();
        if (value.length() != 0)
            metadata.setValue(elem.getName().getLocalPart(), value);
    }
    if (metadata.getKeys().size() > 0) {
        metadata.finished();
        result.getBooks().add(metadata.getBook());
    }
    Set<String> abbrs = new HashSet<String>();
    Set<String> shortnames = new HashSet<String>();
    Map<BookID, String> abbrMap = new EnumMap<BookID, String>(BookID.class);
    List<JAXBElement<BIBLEBOOK>> nl = doc.getBIBLEBOOK();
    for (JAXBElement<BIBLEBOOK> ee : nl) {
        BIBLEBOOK e = ee.getValue();
        String shortname = e.getBsname();
        int number = e.getBnumber().intValue();
        BookID bookID;
        try {
            bookID = BookID.fromZefId(number);
        } catch (IllegalArgumentException ex) {
            continue;
        }
        if (shortname == null || shortname.length() == 0)
            shortname = "_" + bookID.getOsisID();
        String abbr = shortname.replaceAll("[^A-Z0-9a-zäöü]++", "");
        if (abbr.length() == 0 || Character.isLowerCase(abbr.charAt(0)))
            abbr = "X" + abbr;
        if (abbr.length() == 1)
            abbr += "x";
        if (abbrs.contains(abbr)) {
            for (int i = 2; i < 100; i++) {
                if (!abbrs.contains(abbr + i)) {
                    abbr = abbr + i;
                    break;
                }
            }
        }
        abbrs.add(abbr);
        abbrMap.put(bookID, abbr);
    }
    abbrs.clear();
    EnumMap<BookID, Book> existingBooks = new EnumMap<BookID, Book>(BookID.class);
    for (JAXBElement<BIBLEBOOK> ee : nl) {
        BIBLEBOOK e = ee.getValue();
        String shortname = e.getBsname();
        String longname = e.getBname();
        int number = e.getBnumber().intValue();
        BookID bookID;
        try {
            bookID = BookID.fromZefId(number);
        } catch (IllegalArgumentException ex) {
            System.out.println("WARNING: Skipping book with unknown id " + number);
            continue;
        }
        if (shortname == null || shortname.length() == 0)
            shortname = "_" + bookID.getOsisID();
        if (longname == null || longname.length() == 0)
            longname = "_" + bookID.getEnglishName();
        else
            longname = longname.replaceAll("  ++", " ").trim();
        String abbr = shortname.replaceAll("[^A-Z0-9a-zäöü]++", "");
        if (abbr.length() == 0 || Character.isLowerCase(abbr.charAt(0)))
            abbr = "X" + abbr;
        if (abbr.length() == 1)
            abbr += "x";
        if (abbrs.contains(abbr)) {
            for (int i = 2; i < 100; i++) {
                if (!abbrs.contains(abbr + i)) {
                    abbr = abbr + i;
                    break;
                }
            }
        }
        abbrs.add(abbr);
        if (shortname.equals("Gen") && longname.equals("Genesis") && bookID == BookID.BOOK_Exod) {
            System.out.println("WARNING: Book number " + bookID.getZefID() + " has name " + longname);
            shortname = "Exo";
            longname = "Exodus";
        }
        if (shortname.equals("1Chr") && longname.equals("2 Chronicles")) {
            System.out.println("WARNING: Book name 2 Chronicles has short name 1Chr");
            shortname = "2Chr";
        }
        if (shortnames.contains(shortname)) {
            System.out.println("WARNING: Duplicate short name " + shortname);
            for (int i = 2; i < 100; i++) {
                if (!shortnames.contains(shortname + i)) {
                    shortname = shortname + i;
                    break;
                }
            }
        }
        shortnames.add(shortname);
        Book book = existingBooks.get(bookID);
        if (book == null) {
            book = new Book(abbr, bookID, shortname, longname);
            existingBooks.put(bookID, book);
            result.getBooks().add(book);
        }
        List<Headline> headlineBuffer = new ArrayList<Headline>();
        for (JAXBElement<?> cpr : e.getCAPTIONOrPROLOGOrREMARK()) {
            if (cpr.getValue() instanceof CAPTION) {
                CAPTION caption = (CAPTION) cpr.getValue();
                Headline h = new Headline(9);
                if (parseContent(h.getAppendVisitor(), caption.getContent(), abbrMap)) {
                    h.trimWhitespace();
                    h.finished();
                    headlineBuffer.add(h);
                }
            } else if (cpr.getValue() instanceof CHAPTER) {
                CHAPTER e2 = (CHAPTER) cpr.getValue();
                int chapterNumber = e2.getCnumber().intValue();
                while (book.getChapters().size() < chapterNumber) book.getChapters().add(new Chapter());
                Chapter chapter = book.getChapters().get(chapterNumber - 1);
                int existingVerses = chapter.getVerses().size();
                for (Object e3 : e2.getCAPTIONOrPARAGRAPHOrVERSE()) {
                    parseChapterObject(e3, chapter, abbrMap, headlineBuffer);
                }
                for (Verse v : chapter.getVerses()) {
                    if (existingVerses > 0) {
                        existingVerses--;
                        continue;
                    }
                    v.finished();
                }
            } else {
                throw new IOException(cpr.getValue().getClass().toString());
            }
        }
    }
    return result;
}
Also used : Bible(biblemulticonverter.data.Bible) ArrayList(java.util.ArrayList) BookID(biblemulticonverter.data.BookID) Book(biblemulticonverter.data.Book) MetadataBook(biblemulticonverter.data.MetadataBook) Headline(biblemulticonverter.data.FormattedText.Headline) EnumMap(java.util.EnumMap) HashSet(java.util.HashSet) MetadataBook(biblemulticonverter.data.MetadataBook) Chapter(biblemulticonverter.data.Chapter) JAXBElement(javax.xml.bind.JAXBElement) IOException(java.io.IOException) BIBLEBOOK(biblemulticonverter.schema.haggai.BIBLEBOOK) CHAPTER(biblemulticonverter.schema.haggai.CHAPTER) CAPTION(biblemulticonverter.schema.haggai.CAPTION) VirtualVerse(biblemulticonverter.data.VirtualVerse) Verse(biblemulticonverter.data.Verse)

Example 18 with Bible

use of biblemulticonverter.data.Bible in project BibleMultiConverter by schierlm.

the class MyBibleZone method doImport.

@Override
public Bible doImport(File inputFile) throws Exception {
    SqlJetDb db = SqlJetDb.open(inputFile, false);
    SqlJetDb footnoteDB = null;
    File footnoteFile = new File(inputFile.getParentFile(), inputFile.getName().replace(".SQLite3", ".commentaries.SQLite3"));
    if (inputFile.getName().endsWith(".SQLite3") && footnoteFile.exists()) {
        footnoteDB = SqlJetDb.open(footnoteFile, false);
        if (!footnoteDB.getTable("commentaries").getIndexesNames().contains("commentaries_index")) {
            footnoteDB.close();
            footnoteDB = SqlJetDb.open(footnoteFile, true);
            checkIndex(footnoteDB, "commentaries", "commentaries_index", "CREATE INDEX commentaries_index on commentaries(book_number, chapter_number_from, verse_number_from)");
        }
        footnoteDB.beginTransaction(SqlJetTransactionMode.READ_ONLY);
    }
    if (!db.getTable("verses").getIndexesNames().contains("versesIndex") || (db.getSchema().getTable("stories") != null && !db.getTable("stories").getIndexesNames().contains("stories_index"))) {
        db.close();
        db = SqlJetDb.open(inputFile, true);
        checkIndex(db, "verses", "verses_index", "CREATE UNIQUE INDEX verses_index on verses (book_number, chapter, verse)");
        if (db.getSchema().getTable("stories") != null)
            if (db.getSchema().getTable("stories").getColumn("order_if_several") == null)
                checkIndex(db, "stories", "stories_index", "CREATE UNIQUE INDEX stories_index on stories(book_number, chapter, verse)");
            else
                checkIndex(db, "stories", "stories_index", "CREATE UNIQUE INDEX stories_index on stories(book_number, chapter, verse, order_if_several)");
    }
    db.beginTransaction(SqlJetTransactionMode.READ_ONLY);
    String bibleName = null;
    MetadataBook mb = new MetadataBook();
    ISqlJetCursor cursor = db.getTable("info").open();
    while (!cursor.eof()) {
        String fn = cursor.getString("name");
        String fv = cursor.getString("value");
        if (fn.equals("description")) {
            bibleName = fv;
        } else if (!fv.isEmpty()) {
            fv = fv.replaceAll("[\r\n]+", "\n").replaceAll(" *\n *", "\n").replaceAll("\n$", "");
            try {
                mb.setValue("MyBible.zone@" + fn.replace('_', '.'), fv);
            } catch (IllegalArgumentException ex) {
                System.out.println("WARNING: Skipping malformed metadata property " + fn);
            }
        }
        cursor.next();
    }
    cursor.close();
    if (bibleName == null) {
        System.out.println("WARNING: No bible name in info table");
        bibleName = inputFile.getName();
    }
    Bible result = new Bible(bibleName.trim());
    if (!mb.getKeys().isEmpty()) {
        mb.finished();
        result.getBooks().add(mb.getBook());
    }
    Map<Integer, Book> bookIDMap = new HashMap<>();
    cursor = db.getTable("books").open();
    while (!cursor.eof()) {
        int num = (int) cursor.getInteger("book_number");
        String col = cursor.getString("book_color");
        String shortName = cursor.getString("short_name").trim().replace(" ", "").replaceAll("[^A-Z0-9a-zäöü]++", "");
        if (!shortName.isEmpty())
            shortName = shortName.substring(0, 1).toUpperCase() + shortName.substring(1);
        String longName = cursor.getString("long_name").trim();
        BookID bid = null;
        for (MyBibleZoneBook bi : BOOK_INFO) {
            if (bi.bookNumber == num) {
                bid = bi.bookID;
                if (!col.equals(bi.bookColor))
                    System.out.println("WARNING: Book " + bid.getOsisID() + " uses color " + col + " and not " + bi.bookColor);
            }
        }
        if (bid == null) {
            System.out.println("WARNING: Book number " + num + " unknown; skipping: " + shortName + "/" + longName);
            // generate dummy entry not stored in result object
            bookIDMap.put(num, new Book("Xxx", BookID.BOOK_Gen, "X", "X"));
        } else {
            if (shortName.length() < 2)
                shortName = bid.getOsisID().replaceAll("[^A-Z0-9a-zäöü]++", "");
            Book bk = new Book(shortName, bid, longName, longName);
            result.getBooks().add(bk);
            bookIDMap.put(num, bk);
        }
        cursor.next();
    }
    cursor.close();
    if (db.getSchema().getTable("introductions") != null) {
        cursor = db.getTable("introductions").open();
        while (!cursor.eof()) {
            int num = (int) cursor.getInteger("book_number");
            String intro = cursor.getString("introduction");
            Book bk;
            if (num == 0) {
                bk = new Book("Intro", BookID.INTRODUCTION, "_Introduction_", "_Introduction_");
                if (!result.getBooks().isEmpty() && result.getBooks().get(0).getId().equals(BookID.METADATA)) {
                    result.getBooks().add(1, bk);
                } else {
                    result.getBooks().add(0, bk);
                }
            } else {
                bk = bookIDMap.get(num);
            }
            if (bk == null) {
                System.out.println("WARNING: Skipping introduction for nonexisting book " + num);
            } else {
                FormattedText ft = new FormattedText();
                convertFromHTML(intro, ft.getAppendVisitor());
                ft.finished();
                if (bk.getChapters().isEmpty())
                    bk.getChapters().add(new Chapter());
                bk.getChapters().get(0).setProlog(ft);
            }
            cursor.next();
        }
        cursor.close();
    }
    cursor = db.getTable("verses").order("verses_index");
    while (!cursor.eof()) {
        int b = (int) cursor.getInteger("book_number");
        int c = (int) cursor.getInteger("chapter");
        int v = (int) cursor.getInteger("verse");
        String text = cursor.getString("text");
        if (text == null)
            text = "";
        text = text.trim();
        if (!text.isEmpty()) {
            Book bk = bookIDMap.get(b);
            if (bk == null) {
                System.out.println("WARNING: Verse for unknown book " + b + " skipped");
            } else {
                while (bk.getChapters().size() < c) bk.getChapters().add(new Chapter());
                Chapter ch = bk.getChapters().get(c - 1);
                Verse vv = new Verse("" + v);
                try {
                    String rest = convertFromVerse(text, vv.getAppendVisitor(), footnoteDB, new int[] { b, c, v });
                    if (!rest.isEmpty()) {
                        System.out.println("WARNING: Treating tags as plaintext: " + rest);
                        vv.getAppendVisitor().visitText(rest.replace('\t', ' ').replaceAll("  +", " "));
                    }
                } catch (RuntimeException ex) {
                    throw new RuntimeException(text, ex);
                }
                ch.getVerses().add(vv);
                vv.finished();
            }
        }
        cursor.next();
    }
    cursor.close();
    if (db.getSchema().getTable("stories") != null) {
        cursor = db.getTable("stories").order("stories_index");
        Map<Verse, List<FormattedText.Headline>> subheadings = new HashMap<>();
        Map<Verse, Chapter> subheadingChapters = new HashMap<>();
        while (!cursor.eof()) {
            int b = (int) cursor.getInteger("book_number");
            int c = (int) cursor.getInteger("chapter");
            int v = (int) cursor.getInteger("verse");
            String title = cursor.getString("title").trim();
            Book bk = bookIDMap.get(b);
            if (bk == null) {
                System.out.println("WARNING: Subheading for unknown book " + b + " skipped");
            } else if (bk.getChapters().size() < c) {
                System.out.println("WARNING: Subheading for unknown chapter " + b + " " + c + " skipped");
            } else {
                Chapter ch = bk.getChapters().get(c - 1);
                Verse vv = null;
                for (Verse vvv : ch.getVerses()) {
                    if (vvv.getNumber().equals("" + v))
                        vv = vvv;
                }
                if (vv == null) {
                    System.out.println("WARNING: Subheading for unknown verse " + b + " " + c + ":" + v + " skipped");
                } else {
                    List<FormattedText.Headline> hls = subheadings.get(vv);
                    if (hls == null) {
                        hls = new ArrayList<>();
                        subheadings.put(vv, hls);
                        subheadingChapters.put(vv, ch);
                    }
                    Headline hl = new Headline(1);
                    while (title.contains("<x>")) {
                        int pos = title.indexOf("<x>");
                        hl.getAppendVisitor().visitText(title.substring(0, pos));
                        title = title.substring(pos + 3);
                        pos = title.indexOf("</x>");
                        if (pos == -1)
                            System.out.println("WARNING: Unclosed cross reference: " + title);
                        else {
                            String ref = title.substring(0, pos);
                            title = title.substring(pos + 4);
                            hl.getAppendVisitor().visitFormattingInstruction(FormattingInstructionKind.BOLD).visitText(ref);
                        }
                    }
                    hl.getAppendVisitor().visitText(title);
                    hl.finished();
                    hls.add(hl);
                }
            }
            cursor.next();
        }
        cursor.close();
        for (Verse vv : subheadings.keySet()) {
            Chapter cc = subheadingChapters.get(vv);
            Verse vnew = new Verse(vv.getNumber());
            for (Headline hl : subheadings.get(vv)) {
                hl.accept(vnew.getAppendVisitor().visitHeadline(hl.getDepth()));
            }
            vv.accept(vnew.getAppendVisitor());
            vnew.finished();
            int pos = cc.getVerses().indexOf(vv);
            cc.getVerses().set(pos, vnew);
        }
    }
    if (footnoteDB != null) {
        footnoteDB.commit();
        footnoteDB.close();
    }
    db.commit();
    db.close();
    return result;
}
Also used : MetadataBook(biblemulticonverter.data.MetadataBook) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SqlJetDb(org.tmatesoft.sqljet.core.table.SqlJetDb) Bible(biblemulticonverter.data.Bible) Chapter(biblemulticonverter.data.Chapter) ArrayList(java.util.ArrayList) FormattedText(biblemulticonverter.data.FormattedText) BookID(biblemulticonverter.data.BookID) MetadataBook(biblemulticonverter.data.MetadataBook) Book(biblemulticonverter.data.Book) Headline(biblemulticonverter.data.FormattedText.Headline) ISqlJetCursor(org.tmatesoft.sqljet.core.table.ISqlJetCursor) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) VirtualVerse(biblemulticonverter.data.VirtualVerse) Verse(biblemulticonverter.data.Verse)

Example 19 with Bible

use of biblemulticonverter.data.Bible in project BibleMultiConverter by schierlm.

the class AbstractParatextFormat method doImport.

@Override
public Bible doImport(File inputFile) throws Exception {
    List<ParatextBook> books = doImportBooks(inputFile);
    String bibleName = null;
    for (ParatextBook book : books) {
        if (bibleName == null || book.getBibleName().isEmpty()) {
            bibleName = book.getBibleName();
        } else {
            String bookBibleName = book.getBibleName();
            // use common suffix
            if (bookBibleName.length() > bibleName.length()) {
                bookBibleName = bookBibleName.substring(bookBibleName.length() - bibleName.length());
            } else if (bibleName.length() > bookBibleName.length()) {
                bibleName = bibleName.substring(bibleName.length() - bookBibleName.length());
            }
            for (int i = bibleName.length() - 1; i >= 0; i--) {
                if (bibleName.charAt(i) != bookBibleName.charAt(i)) {
                    bibleName = bibleName.substring(i + 1);
                    break;
                }
            }
        }
    }
    Bible bible = new Bible((bibleName == null || bibleName.isEmpty()) ? "Imported Bible" : bibleName);
    for (ParatextBook book : books) {
        String longName = book.getAttributes().get("toc1");
        if (longName == null || longName.isEmpty())
            longName = book.getId().getEnglishName();
        String shortName = book.getAttributes().get("toc2");
        if (shortName == null || shortName.isEmpty())
            shortName = longName;
        String abbr = book.getAttributes().get("toc3"), fallbackAbbr = book.getId().getId().getOsisID().replace("x-", "").replace("-", "");
        if (abbr == null)
            abbr = fallbackAbbr;
        abbr = abbr.replace(" ", "");
        if (!Utils.compilePattern(Utils.BOOK_ABBR_REGEX).matcher(abbr).matches()) {
            System.out.println("WARNING: Unsupported book abbreviation " + abbr + ", using " + fallbackAbbr + " instead");
            abbr = fallbackAbbr;
        }
        final Book bk = new Book(abbr, book.getId().getId(), shortName, longName);
        bible.getBooks().add(bk);
        final boolean forceProlog = book.getId().getId().getZefID() < 0;
        final ParatextImportContext ctx = new ParatextImportContext();
        ctx.nt = book.getId().getId().isNT();
        book.accept(new ParatextBookContentVisitor<RuntimeException>() {

            @Override
            public void visitChapterStart(int newChapter) throws RuntimeException {
                if (ctx.cnum != -1 && !ctx.headlines.isEmpty()) {
                    System.out.println("WARNING: Ignoring unreferenced headlines");
                    ctx.headlines.clear();
                }
                if (ctx.cnum == 0 && newChapter == 1) {
                    // we are in prolog (chapter already exists)
                    ctx.cnum = newChapter;
                } else if (newChapter >= 1 && newChapter > ctx.cnum) {
                    if (ctx.cnum == -1)
                        ctx.cnum = 0;
                    while (ctx.cnum < newChapter - 1) {
                        bk.getChapters().add(new Chapter());
                        ctx.cnum++;
                    }
                    ctx.currentChapter = new Chapter();
                    bk.getChapters().add(ctx.currentChapter);
                    ctx.cnum = newChapter;
                } else {
                    System.out.println("WARNING: Ignoring chapter number " + newChapter + ", current chapter is " + ctx.cnum);
                }
                ctx.currentVisitor = null;
                ctx.currentVerse = null;
                ctx.currentParagraph = ParatextImportContext.CurrentParagraph.NONE;
            }

            @Override
            public void visitParagraphStart(ParagraphKind kind) throws RuntimeException {
                if (ctx.currentParagraph != ParatextImportContext.CurrentParagraph.NONE) {
                    if (ctx.currentParagraph == ParatextImportContext.CurrentParagraph.PROLOG || (ctx.currentParagraph == ParatextImportContext.CurrentParagraph.NORMAL && ctx.currentVisitor != null)) {
                        ctx.currentVisitor.visitLineBreak(LineBreakKind.PARAGRAPH);
                    }
                    ctx.currentParagraph = ParatextImportContext.CurrentParagraph.NONE;
                }
                if (kind.getCategory() == ParagraphKindCategory.SKIP) {
                // do nothing
                } else if (kind.getCategory() == ParagraphKindCategory.HEADLINE) {
                    Headline hl = null;
                    if (kind.isJoinHeadlines() && !ctx.headlines.isEmpty()) {
                        hl = ctx.headlines.get(ctx.headlines.size() - 1);
                        if (hl.getDepth() == kind.getHeadlineDepth() || kind.getHeadlineDepth() == 0) {
                            hl.getAppendVisitor().visitText(" ");
                        } else {
                            hl = null;
                        }
                    }
                    if (hl == null) {
                        hl = new Headline(kind.getHeadlineDepth());
                        ctx.headlines.add(hl);
                    }
                    ctx.currentParagraph = ParatextImportContext.CurrentParagraph.HEADLINE;
                    ctx.currentVisitor = hl.getAppendVisitor();
                    if (kind.getExtraFormatting() != null) {
                        ctx.currentVisitor = ctx.currentVisitor.visitFormattingInstruction(kind.getExtraFormatting());
                    }
                } else {
                    // BLANK_LINE, TABLE_ROW, TEXT
                    if (kind.isProlog() || forceProlog) {
                        if (ctx.cnum == -1) {
                            ctx.cnum = 0;
                            ctx.currentChapter = new Chapter();
                            bk.getChapters().add(ctx.currentChapter);
                        }
                        if (ctx.currentChapter.getProlog() == null) {
                            ctx.currentChapter.setProlog(new FormattedText());
                        }
                        if (!ctx.currentChapter.getVerses().isEmpty()) {
                            System.out.println("WARNING: Adding to prolog after verses have been added!");
                        }
                        ctx.currentVisitor = ctx.currentChapter.getProlog().getAppendVisitor();
                        ctx.currentParagraph = ParatextImportContext.CurrentParagraph.PROLOG;
                        ctx.flushHeadlines();
                    } else {
                        ctx.currentParagraph = ParatextImportContext.CurrentParagraph.NORMAL;
                    }
                }
            }

            @Override
            public void visitTableCellStart(String tag) throws RuntimeException {
                ctx.ensureParagraph();
                if (!tag.matches("t[hc]r?1") && ctx.currentParagraph != ParatextImportContext.CurrentParagraph.HEADLINE && ctx.currentVisitor != null) {
                    ctx.currentVisitor.visitLineBreak(LineBreakKind.NEWLINE_WITH_INDENT);
                }
            }

            @Override
            public void visitParatextCharacterContent(ParatextCharacterContent content) throws RuntimeException {
                ctx.ensureParagraph();
                content.accept(new ParatextImportVisitor(ctx));
            }
        });
        if (!ctx.headlines.isEmpty()) {
            System.out.println("WARNING: Ignoring unreferenced headlines");
            ctx.headlines.clear();
        }
        for (Chapter ch : bk.getChapters()) {
            if (ch.getProlog() != null)
                ch.getProlog().finished();
            for (Verse v : ch.getVerses()) v.finished();
        }
    }
    return bible;
}
Also used : Bible(biblemulticonverter.data.Bible) Chapter(biblemulticonverter.data.Chapter) FormattedText(biblemulticonverter.data.FormattedText) Book(biblemulticonverter.data.Book) ParagraphKind(biblemulticonverter.format.paratext.ParatextBook.ParagraphKind) Headline(biblemulticonverter.data.FormattedText.Headline) Verse(biblemulticonverter.data.Verse)

Example 20 with Bible

use of biblemulticonverter.data.Bible in project BibleMultiConverter by schierlm.

the class Main method main.

public static void main(String[] args) throws Exception {
    discoverModules();
    if (args.length > 0) {
        Module<Tool> toolModule = tools.get(args[0]);
        if (toolModule != null) {
            toolModule.getImplementationClass().newInstance().run(Arrays.copyOfRange(args, 1, args.length));
            return;
        }
    }
    if (args.length > 2) {
        Module<ImportFormat> importModule = importFormats.get(args[0]);
        Module<ExportFormat> exportModule = exportFormats.get(args[2]);
        if (importModule != null && exportModule != null) {
            Bible bible = importModule.getImplementationClass().newInstance().doImport(new File(args[1]));
            exportModule.getImplementationClass().newInstance().doExport(bible, Arrays.copyOfRange(args, 3, args.length));
            return;
        }
    }
    System.out.println("Usage:");
    System.out.println("java -jar BibleMultiConverter.jar <ImportFormat> <ImportFile> <ExportFormat> [<ExportArgs>...]");
    System.out.println("java -jar BibleMultiConverter.jar <Tool> [<ToolArgs>...]");
    printModules("import formats", importFormats);
    printModules("export formats", exportFormats);
    printModules("versification formats", versificationFormats);
    printModules("tools", tools);
}
Also used : Bible(biblemulticonverter.data.Bible) ExportFormat(biblemulticonverter.format.ExportFormat) File(java.io.File) Tool(biblemulticonverter.tools.Tool) ImportFormat(biblemulticonverter.format.ImportFormat)

Aggregations

Bible (biblemulticonverter.data.Bible)20 Book (biblemulticonverter.data.Book)18 Chapter (biblemulticonverter.data.Chapter)18 FormattedText (biblemulticonverter.data.FormattedText)15 Verse (biblemulticonverter.data.Verse)15 BookID (biblemulticonverter.data.BookID)12 MetadataBook (biblemulticonverter.data.MetadataBook)10 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)9 Headline (biblemulticonverter.data.FormattedText.Headline)6 VirtualVerse (biblemulticonverter.data.VirtualVerse)6 File (java.io.File)5 EnumMap (java.util.EnumMap)5 HashMap (java.util.HashMap)5 Matcher (java.util.regex.Matcher)5 BufferedReader (java.io.BufferedReader)4 Visitor (biblemulticonverter.data.FormattedText.Visitor)3 HashSet (java.util.HashSet)3 BIBLEBOOK (biblemulticonverter.schema.zef2005.BIBLEBOOK)2 CAPTION (biblemulticonverter.schema.zef2005.CAPTION)2