Search in sources :

Example 36 with Chapter

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

the class StrippedDiffable method strip.

protected void strip(Bible bible, EnumSet<Feature> chosenFeatures) {
    EnumSet<Feature> foundFeatures = EnumSet.noneOf(Feature.class);
    for (int i = 0; i < bible.getBooks().size(); i++) {
        Book book = bible.getBooks().get(i);
        BookID bid = book.getId();
        boolean stripBook;
        switch(bid) {
            case METADATA:
                stripBook = isEnabled(Feature.StripMetadataBook, chosenFeatures, foundFeatures);
                break;
            case INTRODUCTION:
                stripBook = isEnabled(Feature.StripIntroductionBooks, chosenFeatures, foundFeatures);
                break;
            case INTRODUCTION_OT:
                // no "shortcut" or here due to side effects of isEnabled!
                stripBook = isEnabled(Feature.StripIntroductionBooks, chosenFeatures, foundFeatures) | isEnabled(Feature.StripOldTestament, chosenFeatures, foundFeatures);
                break;
            case INTRODUCTION_NT:
                // no "shortcut" or here due to side effects of isEnabled!
                stripBook = isEnabled(Feature.StripIntroductionBooks, chosenFeatures, foundFeatures) | isEnabled(Feature.StripNewTestament, chosenFeatures, foundFeatures);
                break;
            case APPENDIX:
                stripBook = isEnabled(Feature.StripAppendixBook, chosenFeatures, foundFeatures);
                break;
            case DICTIONARY_ENTRY:
                stripBook = isEnabled(Feature.StripDictionaryEntries, chosenFeatures, foundFeatures);
                break;
            default:
                stripBook = isEnabled(bid.isNT() ? Feature.StripNewTestament : Feature.StripOldTestament, chosenFeatures, foundFeatures);
                if (bid.isDeuterocanonical())
                    stripBook = stripBook | isEnabled(Feature.StripDeuterocanonicalBooks, chosenFeatures, foundFeatures);
                break;
        }
        if (stripBook) {
            bible.getBooks().remove(i);
            i--;
            continue;
        }
        for (Chapter chap : book.getChapters()) {
            if (chap.getProlog() != null) {
                if (isEnabled(Feature.StripPrologs, chosenFeatures, foundFeatures)) {
                    chap.setProlog(null);
                } else {
                    FormattedText newProlog = new FormattedText();
                    chap.getProlog().accept(new StripVisitor(newProlog.getAppendVisitor(), chosenFeatures, foundFeatures));
                    newProlog.finished();
                    chap.setProlog(newProlog);
                }
            }
            for (int j = 0; j < chap.getVerses().size(); j++) {
                Verse v = chap.getVerses().get(j);
                Verse nv = new Verse(v.getNumber());
                v.accept(new StripVisitor(nv.getAppendVisitor(), chosenFeatures, foundFeatures));
                nv.finished();
                chap.getVerses().set(j, nv);
            }
        }
    }
    EnumSet<Feature> stripped = EnumSet.copyOf(chosenFeatures);
    stripped.retainAll(foundFeatures);
    chosenFeatures.removeAll(stripped);
    foundFeatures.removeAll(stripped);
    printFeatureSet("Features stripped:", stripped);
    printFeatureSet("Features to strip not found:", chosenFeatures);
    printFeatureSet("Features still present after stripping:", foundFeatures);
}
Also used : BookID(biblemulticonverter.data.BookID) Book(biblemulticonverter.data.Book) Chapter(biblemulticonverter.data.Chapter) FormattedText(biblemulticonverter.data.FormattedText) Verse(biblemulticonverter.data.Verse)

Example 37 with Chapter

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

the class StrippedDiffable method mergeIntroductionPrologs.

protected void mergeIntroductionPrologs(Bible bible) {
    List<FormattedText> prologBuffer = new ArrayList<FormattedText>();
    for (int i = 0; i < bible.getBooks().size(); i++) {
        Book book = bible.getBooks().get(i);
        if (book.getId().getZefID() < 0) {
            if (book.getChapters().size() == 1) {
                Chapter ch = book.getChapters().get(0);
                if (ch.getVerses().size() > 0)
                    System.out.println("WARNING: Book " + book.getAbbr() + " has verses; not merged.");
                if (ch.getProlog() != null)
                    prologBuffer.add(ch.getProlog());
                else
                    System.out.println("WARNING: Book " + book.getAbbr() + " does not have a prolog; not merged.");
            } else {
                System.out.println("WARNING: Book " + book.getAbbr() + " has " + book.getChapters().size() + " chapters; not merged.");
            }
            bible.getBooks().remove(i);
            i--;
        } else if (prologBuffer.size() > 0 && book.getChapters().size() > 0) {
            Chapter ch = book.getChapters().get(0);
            if (ch.getProlog() != null)
                prologBuffer.add(ch.getProlog());
            FormattedText newProlog = new FormattedText();
            Visitor<RuntimeException> v = newProlog.getAppendVisitor();
            ch.setProlog(newProlog);
            boolean first = true;
            for (FormattedText oldProlog : prologBuffer) {
                if (!first)
                    v.visitLineBreak(LineBreakKind.PARAGRAPH);
                first = false;
                oldProlog.accept(v);
            }
            prologBuffer.clear();
        }
    }
    if (prologBuffer.size() > 0) {
        System.out.println("WARNING: " + prologBuffer.size() + " introduction prologs after last bible book were merged to first bible book!");
        for (int i = 0; i < bible.getBooks().size(); i++) {
            Book book = bible.getBooks().get(i);
            if (book.getId().getZefID() > 0 && prologBuffer.size() > 0 && book.getChapters().size() > 0) {
                Chapter ch = book.getChapters().get(0);
                Visitor<RuntimeException> v = ch.getProlog().getAppendVisitor();
                for (FormattedText oldProlog : prologBuffer) {
                    v.visitLineBreak(LineBreakKind.PARAGRAPH);
                    oldProlog.accept(v);
                }
                break;
            }
        }
    }
}
Also used : Visitor(biblemulticonverter.data.FormattedText.Visitor) Book(biblemulticonverter.data.Book) ArrayList(java.util.ArrayList) Chapter(biblemulticonverter.data.Chapter) FormattedText(biblemulticonverter.data.FormattedText)

Example 38 with Chapter

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

the class StrippedDiffable method rearrangeChapterBoundaries.

protected void rearrangeChapterBoundaries(Bible bible) {
    for (Book book : bible.getBooks()) {
        for (int i = 0; i < book.getChapters().size(); i++) {
            Chapter chap = book.getChapters().get(i);
            while (chap.getVerses().size() >= 2) {
                if (i >= 1 && chap.getVerses().get(0).getNumber().startsWith(i + ",")) {
                    Verse v = chap.getVerses().remove(0);
                    Verse nv = new Verse(v.getNumber().substring((i + ",").length()));
                    v.accept(nv.getAppendVisitor());
                    nv.finished();
                    book.getChapters().get(i - 1).getVerses().add(nv);
                } else if (i < book.getChapters().size() - 1 && chap.getVerses().get(chap.getVerses().size() - 1).getNumber().startsWith((i + 2) + ",")) {
                    Verse v = chap.getVerses().remove(chap.getVerses().size() - 1);
                    Verse nv = new Verse(v.getNumber().substring(((i + 2) + ",").length()));
                    v.accept(nv.getAppendVisitor());
                    nv.finished();
                    book.getChapters().get(i + 1).getVerses().add(0, nv);
                } else {
                    break;
                }
            }
        }
    }
}
Also used : Book(biblemulticonverter.data.Book) Chapter(biblemulticonverter.data.Chapter) Verse(biblemulticonverter.data.Verse)

Example 39 with Chapter

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

the class StrippedDiffable method selectVariation.

private void selectVariation(Bible bible, String variation) {
    for (Book book : bible.getBooks()) {
        for (Chapter chapter : book.getChapters()) {
            if (chapter.getProlog() != null) {
                FormattedText newProlog = new FormattedText();
                chapter.getProlog().accept(new SelectVariationVisitor(newProlog.getAppendVisitor(), variation));
                newProlog.finished();
                chapter.setProlog(newProlog);
            }
            List<Verse> verses = chapter.getVerses();
            for (int i = 0; i < verses.size(); i++) {
                Verse v1 = verses.get(i);
                Verse v2 = new Verse(v1.getNumber());
                v1.accept(new SelectVariationVisitor(v2.getAppendVisitor(), variation));
                v2.finished();
                verses.set(i, v2);
            }
        }
    }
}
Also used : Book(biblemulticonverter.data.Book) Chapter(biblemulticonverter.data.Chapter) FormattedText(biblemulticonverter.data.FormattedText) Verse(biblemulticonverter.data.Verse)

Example 40 with Chapter

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

the class StrippedDiffable method doExport.

@Override
public void doExport(Bible bible, String... exportArgs) throws Exception {
    String outputFile = exportArgs[0];
    if (exportArgs.length == 2 && exportArgs[1].equals("ExtractPrologs")) {
        extractPrologs(bible);
        System.out.println("Prologs extracted.");
    } else if (exportArgs.length == 2 && exportArgs[1].equals("MergeIntroductionPrologs")) {
        mergeIntroductionPrologs(bible);
        System.out.println("Introduction prologs merged.");
    } else if (exportArgs.length == 2 && exportArgs[1].equals("ExtractFootnotes")) {
        extractFootnotes(bible);
        System.out.println("Footnotes extracted.");
    } else if (exportArgs.length == 2 && exportArgs[1].equals("RearrangeChapterBoundaries")) {
        rearrangeChapterBoundaries(bible);
        System.out.println("Chapter boundaries rearranged.");
    } else if (exportArgs.length == 3 && exportArgs[1].equals("SelectVariation")) {
        selectVariation(bible, exportArgs[2]);
        System.out.println("Variation " + exportArgs[2] + " kept.");
    } else if (exportArgs.length == 4 && exportArgs[1].equals("RenameBook")) {
        renameBookInXref(bible, exportArgs[2], exportArgs[3], true);
        for (int i = 0; i < bible.getBooks().size(); i++) {
            Book oldBook = bible.getBooks().get(i);
            if (oldBook.getAbbr().equals(exportArgs[2])) {
                Book newBook = new Book(exportArgs[3], oldBook.getId(), oldBook.getShortName(), oldBook.getLongName());
                newBook.getChapters().addAll(oldBook.getChapters());
                bible.getBooks().set(i, newBook);
            }
        }
        System.out.println("Book " + exportArgs[2] + " renamed to " + exportArgs[3]);
    } else if (exportArgs.length >= 2 && exportArgs[1].equals("OptimizeFormatting")) {
        Map<String, String[]> mappings = new HashMap<>();
        for (int i = 2; i < exportArgs.length; i++) {
            int pos = exportArgs[i].indexOf("->");
            if (pos == -1) {
                System.out.println("WARNING: Skipped malformed formatting mapping " + exportArgs[i]);
                continue;
            }
            String key = exportArgs[i].substring(0, pos).trim();
            String[] values = exportArgs[i].substring(pos + 2).split("&");
            mappings.put(key, values);
        }
        optimizeFormatting(bible, mappings);
    } else {
        EnumSet<Feature> chosenFeeatures = EnumSet.noneOf(Feature.class);
        for (int i = 1; i < exportArgs.length; i++) {
            chosenFeeatures.add(Feature.valueOf(exportArgs[i]));
        }
        strip(bible, chosenFeeatures);
    }
    List<Book> booksToRemove = new ArrayList<Book>();
    for (Book book : bible.getBooks()) {
        while (book.getChapters().size() > 0) {
            Chapter lastChapter = book.getChapters().get(book.getChapters().size() - 1);
            if (lastChapter.getProlog() != null || lastChapter.getVerses().size() > 0)
                break;
            book.getChapters().remove(lastChapter);
        }
        if (book.getChapters().size() == 0)
            booksToRemove.add(book);
    }
    bible.getBooks().removeAll(booksToRemove);
    new Diffable().doExport(bible, new String[] { outputFile });
}
Also used : HashMap(java.util.HashMap) Book(biblemulticonverter.data.Book) ArrayList(java.util.ArrayList) Chapter(biblemulticonverter.data.Chapter)

Aggregations

Chapter (biblemulticonverter.data.Chapter)64 Book (biblemulticonverter.data.Book)60 Verse (biblemulticonverter.data.Verse)57 FormattedText (biblemulticonverter.data.FormattedText)28 VirtualVerse (biblemulticonverter.data.VirtualVerse)25 MetadataBook (biblemulticonverter.data.MetadataBook)22 BookID (biblemulticonverter.data.BookID)20 ArrayList (java.util.ArrayList)20 Bible (biblemulticonverter.data.Bible)19 Headline (biblemulticonverter.data.FormattedText.Headline)19 File (java.io.File)18 BufferedWriter (java.io.BufferedWriter)15 IOException (java.io.IOException)14 FileOutputStream (java.io.FileOutputStream)13 OutputStreamWriter (java.io.OutputStreamWriter)12 EnumMap (java.util.EnumMap)9 HashMap (java.util.HashMap)9 Visitor (biblemulticonverter.data.FormattedText.Visitor)8 BufferedReader (java.io.BufferedReader)8 FileInputStream (java.io.FileInputStream)8