Search in sources :

Example 31 with Chapter

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

the class RoundtripHTML method doExport.

@Override
public void doExport(Bible bible, String... exportArgs) throws Exception {
    List<String> filenames = new ArrayList<String>();
    File directory = new File(exportArgs[0]);
    if (!directory.exists())
        directory.mkdirs();
    // build xref map
    Properties xrefMap = new Properties();
    if (exportArgs.length > 1 && new File(exportArgs[1]).exists()) {
        try (InputStream in = new FileInputStream(new File(exportArgs[1]))) {
            xrefMap.load(in);
        }
    }
    for (Book bk : bible.getBooks()) {
        for (int cnumber = 1; cnumber <= bk.getChapters().size(); cnumber++) {
            xrefMap.setProperty(bk.getId().getOsisID() + "," + cnumber, new File(directory, getTypeDir(bk.getId()) + "/" + bk.getAbbr() + "_" + cnumber + ".html").getAbsolutePath());
        }
    }
    if (exportArgs.length > 1) {
        try (OutputStream out = new FileOutputStream(new File(exportArgs[1]))) {
            xrefMap.store(out, "RoundtripHTML Cross Reference Map");
        }
    }
    Path base = new File(directory, "dummydir").getAbsoluteFile().toPath();
    for (Object key : xrefMap.keySet()) {
        String newValue = base.relativize(new File((String) xrefMap.get(key)).toPath()).toString().replace('\\', '/');
        xrefMap.setProperty((String) key, newValue);
    }
    // metadata
    try (BufferedWriter bw = createWriter(directory, filenames, "metadata.js")) {
        bw.write("biblename = \"" + bible.getName().replace("\\", "\\\\").replace("\"", "\\\"") + "\";\n");
        bw.write("metadata = [{\n");
        boolean first = true;
        for (Book bk : bible.getBooks()) {
            if (!first)
                bw.write("},{\n");
            first = false;
            String[] keys = new String[] { "abbr", "short", "long", "osis", "type" };
            String[] values = new String[] { bk.getAbbr(), bk.getShortName(), bk.getLongName(), bk.getId().getOsisID(), getTypeDir(bk.getId()) };
            for (int i = 0; i < keys.length; i++) {
                bw.write(keys[i] + ":\"" + values[i].replace("\\", "\\\\").replace("\"", "\\\"") + "\",\n");
            }
            bw.write("nt:" + bk.getId().isNT() + ",\n");
            bw.write("chapters:" + bk.getChapters().size() + "\n");
        }
        bw.write("}];\n");
    }
    // chapters
    for (Book bk : bible.getBooks()) {
        int cnumber = 0;
        for (Chapter ch : bk.getChapters()) {
            cnumber++;
            try (BufferedWriter bw = createWriter(directory, filenames, getTypeDir(bk.getId()) + "/" + bk.getAbbr() + "_" + cnumber + ".html")) {
                bw.write("<html><head>\n" + "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" />\n" + "<title>" + (bk.getChapters().size() == 1 ? "" : bk.getAbbr() + " " + cnumber + " &ndash; ") + bk.getLongName() + " &ndash; " + bible.getName() + "</title>\n" + "<script type=\"text/javascript\" src=\"../metadata.js\"></script>\n" + "<script type=\"text/javascript\" src=\"../script.js\"></script>\n" + "<style type=\"text/css\">div.v { display:inline; } /*changed dynamically*/</style>\n" + "<link rel=\"stylesheet\" type=\"text/css\" href=\"../style.css\">\n" + "</head>\n");
                bw.write("<body onload=\"showNavbar('" + bk.getAbbr() + "', " + cnumber + ");\">\n");
                bw.write("<div id=\"navbar\"><a href=\"../index.html\">" + bible.getName() + "</a> &ndash; <b>" + bk.getLongName() + "</b>");
                if (bk.getChapters().size() > 1) {
                    bw.write(" &ndash; ");
                    for (int i = 1; i <= bk.getChapters().size(); i++) {
                        if (i == cnumber) {
                            bw.write("<b>" + i + "</b> ");
                        } else if (i == 2 && cnumber > 4) {
                            bw.write("... ");
                            i = cnumber - 3;
                        } else if (i == cnumber + 3 && i < bk.getChapters().size()) {
                            bw.write("... ");
                            i = bk.getChapters().size() - 1;
                        } else {
                            bw.write("<a href=\"" + bk.getAbbr() + "_" + i + ".html\">" + i + "</a> ");
                        }
                    }
                }
                bw.write("</div><hr>\n");
                bw.write("<h1>" + bk.getAbbr() + (bk.getChapters().size() == 1 ? "" : " " + cnumber) + "</h1>\n");
                bw.write("<!-- PARSED BELOW; EDITING MAY BREAK PARSER -->\n");
                List<StringWriter> footnotes = new ArrayList<StringWriter>();
                if (ch.getProlog() != null) {
                    bw.write("<div class=\"biblehtmlcontent prolog\">\n");
                    ch.getProlog().accept(new RoundtripHTMLVisitor(bw, footnotes, "", "", xrefMap));
                    bw.write("\n");
                    bw.write("</div>\n");
                }
                if (ch.getVerses().size() > 0) {
                    bw.write("<div class=\"biblehtmlcontent verses\" id=\"verses\">\n");
                    for (Verse v : ch.getVerses()) {
                        bw.write("<div class=\"v\" id=\"v" + v.getNumber() + "\">");
                        v.accept(new RoundtripHTMLVisitor(bw, footnotes, "<span class=\"vn\">" + v.getNumber() + "</span> ", "", xrefMap));
                        bw.write("</div>\n");
                    }
                    bw.write("</div>\n");
                }
                if (footnotes.size() > 0) {
                    bw.write("<div class=\"biblehtmlcontent footnotes\">\n");
                    for (StringWriter footnote : footnotes) {
                        bw.write(footnote.toString() + "\n");
                    }
                    bw.write("</div>\n");
                }
                bw.write("<!-- PARSED ABOVE; EDITING MAY BREAK PARSER -->\n");
                bw.write("</body></html>");
            }
        }
    }
    // index file
    try (BufferedWriter bw = createWriter(directory, filenames, "index.html")) {
        bw.write("<html><head>\n" + "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" />\n" + "<title>" + bible.getName() + "</title>\n" + "<script type=\"text/javascript\" src=\"metadata.js\"></script>\n" + "<script type=\"text/javascript\" src=\"script.js\"></script>\n" + "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">\n" + "</head>\n");
        bw.write("<body onload=\"showNavbar('', 0);\">\n");
        bw.write("<div id=\"navbar\"><b>" + bible.getName() + "</b></div><hr>\n");
        bw.write("<h1>" + bible.getName() + "</h1>\n");
        for (Book bk : bible.getBooks()) {
            bw.write("<a href=\"" + getTypeDir(bk.getId()) + "/" + bk.getAbbr() + "_1.html\">" + bk.getLongName() + "</a><br>\n");
        }
        bw.write("</body></html>");
    }
    // static files
    for (String staticFile : Arrays.asList("script.js", "style.css", "crossdomain.html")) {
        try (BufferedWriter bw = createWriter(directory, filenames, staticFile)) {
            Reader r = new InputStreamReader(RoundtripHTML.class.getResourceAsStream("/RoundtripHTML/" + staticFile), StandardCharsets.UTF_8);
            char[] buf = new char[4096];
            int len;
            while ((len = r.read(buf)) != -1) {
                bw.write(buf, 0, len);
            }
        }
    }
    // filelist.html (for mirroring)
    try (BufferedWriter bw = createWriter(directory, filenames, "filelist.html")) {
        bw.write("<html><head>\n" + "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" />\n" + "<title>File list &ndash; " + bible.getName() + "</title>\n" + "<script type=\"text/javascript\" src=\"metadata.js\"></script>\n" + "<script type=\"text/javascript\" src=\"script.js\"></script>\n" + "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">\n" + "</head>\n");
        bw.write("<body onload=\"showNavbar('', 0);\">\n");
        bw.write("<div id=\"navbar\"><a href=\"index.html\">" + bible.getName() + "</a></div><hr>\n");
        bw.write("<h1>File list</h1>\n");
        for (String name : filenames) {
            bw.write("<a href=\"" + name + "\">" + name + "</a><br>\n");
        }
        bw.write("</body></html>");
    }
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) Properties(java.util.Properties) BufferedWriter(java.io.BufferedWriter) StringWriter(java.io.StringWriter) Book(biblemulticonverter.data.Book) Path(java.nio.file.Path) InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Chapter(biblemulticonverter.data.Chapter) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Verse(biblemulticonverter.data.Verse)

Example 32 with Chapter

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

the class RoundtripXML method createBible.

protected JAXBElement<BibleType> createBible(Bible sBible) throws Exception {
    ObjectFactory of = new ObjectFactory();
    JAXBElement<BibleType> dBible = of.createBible(of.createBibleType());
    dBible.getValue().setName(sBible.getName());
    int dictCounter = 0;
    for (Book sBook : sBible.getBooks()) {
        BibleType.Book dBook = of.createBibleTypeBook();
        dBook.setAbbr(sBook.getAbbr());
        if (sBook.getId().equals(BookID.DICTIONARY_ENTRY)) {
            dBook.setId(sBook.getId().getOsisID() + "-" + (++dictCounter));
        } else {
            dBook.setId(sBook.getId().getOsisID());
        }
        dBook.setShortName(sBook.getShortName());
        dBook.setLongName(sBook.getLongName());
        dBible.getValue().getBook().add(dBook);
        for (Chapter sChapter : sBook.getChapters()) {
            BibleType.Book.Chapter dChapter = of.createBibleTypeBookChapter();
            dBook.getChapter().add(dChapter);
            if (sChapter.getProlog() != null) {
                dChapter.setProlog(of.createBibleTypeBookChapterProlog());
                sChapter.getProlog().accept(new CreateContentVisitor(of, dChapter.getProlog().getContent()));
            }
            for (Verse sVerse : sChapter.getVerses()) {
                BibleType.Book.Chapter.Verse dVerse = of.createBibleTypeBookChapterVerse();
                dChapter.getVerse().add(dVerse);
                dVerse.setNumber(sVerse.getNumber());
                sVerse.accept(new CreateContentVisitor(of, dVerse.getContent()));
            }
        }
    }
    return dBible;
}
Also used : ObjectFactory(biblemulticonverter.schema.roundtripxml.ObjectFactory) Book(biblemulticonverter.data.Book) Chapter(biblemulticonverter.data.Chapter) BibleType(biblemulticonverter.schema.roundtripxml.BibleType) Verse(biblemulticonverter.data.Verse)

Example 33 with Chapter

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

the class RoundtripXML method parseBible.

protected Bible parseBible(JAXBElement<BibleType> sBible) throws Exception {
    Bible dBible = new Bible(sBible.getValue().getName());
    for (BibleType.Book sBook : sBible.getValue().getBook()) {
        Book dBook = new Book(sBook.getAbbr(), BookID.fromOsisId(sBook.getId().replaceAll("-[0-9]+$", "")), sBook.getShortName(), sBook.getLongName());
        dBible.getBooks().add(dBook);
        for (BibleType.Book.Chapter sChapter : sBook.getChapter()) {
            Chapter dChapter = new Chapter();
            dBook.getChapters().add(dChapter);
            if (sChapter.getProlog() != null) {
                dChapter.setProlog(new FormattedText());
                parseContent(dChapter.getProlog().getAppendVisitor(), sChapter.getProlog().getContent());
                dChapter.getProlog().finished();
            }
            for (BibleType.Book.Chapter.Verse sVerse : sChapter.getVerse()) {
                Verse dVerse = new Verse(sVerse.getNumber());
                dChapter.getVerses().add(dVerse);
                parseContent(dVerse.getAppendVisitor(), sVerse.getContent());
                dVerse.finished();
            }
        }
    }
    return dBible;
}
Also used : Book(biblemulticonverter.data.Book) Bible(biblemulticonverter.data.Bible) Chapter(biblemulticonverter.data.Chapter) BibleType(biblemulticonverter.schema.roundtripxml.BibleType) FormattedText(biblemulticonverter.data.FormattedText) Verse(biblemulticonverter.data.Verse)

Example 34 with Chapter

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

the class ScrambledDiffable method doExport.

@Override
public void doExport(Bible bible, String... exportArgs) throws Exception {
    String outputFile = exportArgs[0];
    Scrambler scrambler = new Scrambler(exportArgs.length == 1 ? null : exportArgs[1]);
    for (Book book : bible.getBooks()) {
        if (book.getId() == BookID.METADATA)
            continue;
        for (Chapter chapter : book.getChapters()) {
            if (chapter.getProlog() != null) {
                FormattedText newProlog = new FormattedText();
                scrambleText(scrambler, chapter.getProlog(), newProlog);
                chapter.setProlog(newProlog);
            }
            List<Verse> verses = chapter.getVerses();
            for (int i = 0; i < verses.size(); i++) {
                Verse oldVerse = verses.get(i);
                Verse newVerse = new Verse(oldVerse.getNumber());
                scrambleText(scrambler, oldVerse, newVerse);
                verses.set(i, newVerse);
            }
        }
    }
    new Diffable().doExport(bible, new String[] { outputFile });
}
Also used : Book(biblemulticonverter.data.Book) Chapter(biblemulticonverter.data.Chapter) FormattedText(biblemulticonverter.data.FormattedText) Verse(biblemulticonverter.data.Verse)

Example 35 with Chapter

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

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