Search in sources :

Example 36 with Book

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

the class SimpleJSON method doExport.

@Override
public void doExport(Bible bible, String... exportArgs) throws Exception {
    try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportArgs[0]), StandardCharsets.UTF_8))) {
        SimpleJSONVisitor sjv = new SimpleJSONVisitor(bw);
        bw.write("\t{\"osis\": [{\"osisText\": [{\"osisRefWork\": {\"_value\": \"" + json(bible.getName()) + "\"}," + "\t\t\"header\": [{\"work\": [{");
        MetadataBook mdbk = bible.getMetadataBook();
        for (MetadataBookKey key : Arrays.asList(MetadataBookKey.title, MetadataBookKey.creator, MetadataBookKey.contributors, MetadataBookKey.identifier, MetadataBookKey.subject, MetadataBookKey.date, MetadataBookKey.description, MetadataBookKey.publisher, MetadataBookKey.type, MetadataBookKey.source, MetadataBookKey.language, MetadataBookKey.coverage, MetadataBookKey.rights)) {
            if (key != MetadataBookKey.title)
                bw.write(',');
            String value = mdbk == null ? null : mdbk.getValue(key);
            bw.write("\"" + key.toString() + "\": [{\"_text\": \"" + json(value == null ? "" : value) + "\"}]");
        }
        bw.write("}]}],\t\"div\": [");
        boolean first = true;
        for (Book bk : bible.getBooks()) {
            if (bk.getChapters().isEmpty() || bk.getChapters().get(0).getVerses().isEmpty()) {
                System.out.println("WARNING: Skipping empty book " + bk.getAbbr());
                continue;
            }
            if (!first)
                bw.write(',');
            bw.write("\t{\"name\": {\"_value\": \"" + json(bk.getLongName()) + "\"},\"osisID\": {\"_value\": \"" + json(bk.getId().getOsisID()) + "\"},\"chapter\": [");
            first = true;
            for (int ch = 1; ch <= bk.getChapters().size(); ch++) {
                if (!first)
                    bw.write(',');
                Chapter cc = bk.getChapters().get(ch - 1);
                bw.write("\t{\"cnumber\":" + ch + ",\n\"verse\": [");
                first = true;
                for (VirtualVerse vv : cc.createVirtualVerses()) {
                    if (!first)
                        bw.write(',');
                    bw.write("\t{\"osisID\": {\"_value\": \"" + json(bk.getId().getOsisID() + "." + ch + "." + vv.getNumber()) + "\"},\"vnumber\":" + vv.getNumber() + ",\"_text\": \"");
                    for (Verse v : vv.getVerses()) {
                        if (!v.getNumber().equals("" + vv.getNumber())) {
                            bw.write(" (" + v.getNumber() + ") ");
                        }
                        v.accept(sjv);
                    }
                    bw.write("\"}");
                    first = false;
                }
                bw.write("]}");
                first = false;
            }
            bw.write("]}");
            first = false;
        }
        bw.write("]}]}]}\n");
    }
}
Also used : MetadataBook(biblemulticonverter.data.MetadataBook) VirtualVerse(biblemulticonverter.data.VirtualVerse) Chapter(biblemulticonverter.data.Chapter) BufferedWriter(java.io.BufferedWriter) MetadataBook(biblemulticonverter.data.MetadataBook) Book(biblemulticonverter.data.Book) FileOutputStream(java.io.FileOutputStream) MetadataBookKey(biblemulticonverter.data.MetadataBook.MetadataBookKey) OutputStreamWriter(java.io.OutputStreamWriter) VirtualVerse(biblemulticonverter.data.VirtualVerse) Verse(biblemulticonverter.data.Verse)

Example 37 with Book

use of biblemulticonverter.data.Book 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 38 with Book

use of biblemulticonverter.data.Book 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 39 with Book

use of biblemulticonverter.data.Book 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 40 with Book

use of biblemulticonverter.data.Book 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)

Aggregations

Book (biblemulticonverter.data.Book)67 Chapter (biblemulticonverter.data.Chapter)60 Verse (biblemulticonverter.data.Verse)57 FormattedText (biblemulticonverter.data.FormattedText)27 MetadataBook (biblemulticonverter.data.MetadataBook)25 VirtualVerse (biblemulticonverter.data.VirtualVerse)24 BookID (biblemulticonverter.data.BookID)22 ArrayList (java.util.ArrayList)22 File (java.io.File)20 Bible (biblemulticonverter.data.Bible)19 Headline (biblemulticonverter.data.FormattedText.Headline)18 BufferedWriter (java.io.BufferedWriter)16 FileOutputStream (java.io.FileOutputStream)15 IOException (java.io.IOException)14 OutputStreamWriter (java.io.OutputStreamWriter)13 HashMap (java.util.HashMap)10 HashSet (java.util.HashSet)10 Visitor (biblemulticonverter.data.FormattedText.Visitor)9 FileInputStream (java.io.FileInputStream)9 EnumMap (java.util.EnumMap)9