Search in sources :

Example 21 with MetadataBook

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

the class MyBibleZoneDictionary method doExport.

@Override
public void doExport(Bible bible, String... exportArgs) throws Exception {
    String outfile = exportArgs[0];
    String dictionaryName = exportArgs[1];
    if (!outfile.endsWith(".dictionary.SQLite3"))
        outfile += ".dictionary.SQLite3";
    new File(outfile).delete();
    SqlJetDb db = SqlJetDb.open(new File(outfile), true);
    db.getOptions().setAutovacuum(true);
    db.beginTransaction(SqlJetTransactionMode.WRITE);
    db.getOptions().setUserVersion(0);
    db.createTable("CREATE TABLE info (name TEXT, value TEXT)");
    db.createTable("CREATE TABLE dictionary (topic TEXT, definition TEXT)");
    db.createIndex("CREATE UNIQUE INDEX dictionary_topic ON dictionary(topic ASC)");
    Map<String, String> infoValues = new LinkedHashMap<>();
    MetadataBook mb = bible.getMetadataBook();
    if (mb == null)
        mb = new MetadataBook();
    infoValues.put("language", "xx");
    infoValues.put("description", bible.getName());
    infoValues.put("detailed_info", "");
    infoValues.put("russian_numbering", "false");
    infoValues.put("is_strong", dictionaryName.equals("strong") ? "true" : "false");
    infoValues.put("is_word_forms", "false");
    infoValues.put("morphology_topic_reference", "");
    for (String mbkey : mb.getKeys()) {
        if (mbkey.startsWith("MyBible.zone@")) {
            infoValues.put(mbkey.substring(13).replace('.', '_'), mb.getValue(mbkey));
        } else {
            infoValues.put("detailed_info", infoValues.get("detailed_info") + "\r\n<br><b>" + mbkey + ":</b>" + mb.getValue(mbkey));
        }
    }
    if (exportArgs.length > 2) {
        Properties props = new Properties();
        FileInputStream in = new FileInputStream(exportArgs[2]);
        props.load(in);
        in.close();
        for (Object key : props.keySet()) {
            String template = props.getProperty(key.toString());
            template = template.replace("${name}", bible.getName());
            for (String mbkey : mb.getKeys()) template = template.replace("${" + mbkey + "}", mb.getValue(mbkey));
            infoValues.put(key.toString(), template);
        }
    }
    ISqlJetTable infoTable = db.getTable("info");
    ISqlJetTable dictionaryTable = db.getTable("dictionary");
    for (Map.Entry<String, String> entry : infoValues.entrySet()) {
        infoTable.insert(entry.getKey(), entry.getValue());
    }
    Set<String> usedTopics = new HashSet<>();
    final Set<String> unsupportedFeatures = new HashSet<>();
    for (Book bk : bible.getBooks()) {
        if (bk.getId() != BookID.DICTIONARY_ENTRY) {
            System.out.println("WARNING: Skipping book " + bk.getAbbr());
            continue;
        }
        String topicName = bk.getShortName();
        if (usedTopics.contains(topicName) && !dictionaryName.equals("strong")) {
            for (int i = 2; ; i++) {
                if (!usedTopics.contains(topicName + " (" + i + ")")) {
                    topicName += " (" + i + ")";
                    break;
                }
            }
        }
        if (usedTopics.contains(topicName)) {
            System.out.println("WARNING: Skipping duplicate topic " + topicName);
            continue;
        }
        if (dictionaryName.equals("strong")) {
            if (!topicName.matches("[GH][1-9][0-9]*")) {
                System.out.println("WARNING: Skipping invalid Strong number " + topicName);
                continue;
            }
        } else if (!dictionaryName.equals("-")) {
            dictionaryTable.insert("[" + dictionaryName + "]" + bk.getAbbr(), "\u2197 <a href=\"S:" + topicName + "\">" + bk.getShortName() + "</a>");
        }
        MyBibleHTMLVisitor v = new MyBibleHTMLVisitor(unsupportedFeatures, "in dictionary entry");
        bk.getChapters().get(0).getProlog().accept(v);
        dictionaryTable.insert(topicName, v.getResult());
    }
    if (!unsupportedFeatures.isEmpty()) {
        System.out.println("WARNING: Skipped unsupported features: " + unsupportedFeatures);
    }
    db.commit();
    db.close();
}
Also used : MetadataBook(biblemulticonverter.data.MetadataBook) SqlJetDb(org.tmatesoft.sqljet.core.table.SqlJetDb) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) LinkedHashMap(java.util.LinkedHashMap) MetadataBook(biblemulticonverter.data.MetadataBook) Book(biblemulticonverter.data.Book) MyBibleHTMLVisitor(biblemulticonverter.sqlite.format.MyBibleZone.MyBibleHTMLVisitor) ISqlJetTable(org.tmatesoft.sqljet.core.table.ISqlJetTable) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 22 with MetadataBook

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

the class LogosHTML method doExport.

@Override
public void doExport(Bible bible, String... exportArgs) throws Exception {
    String versemap = exportArgs.length == 1 ? "Bible" : exportArgs[1];
    Properties bookDatatypeMap = null;
    VersificationScheme scheme;
    if (versemap.startsWith("author:")) {
        bookDatatypeMap = new Properties();
        try (InputStream in = new FileInputStream(versemap.substring(7))) {
            bookDatatypeMap.load(in);
        }
        versemap = null;
        scheme = new LogosVersificationDetector().loadScheme("Bible");
    } else {
        if (!versemap.matches("Bible[A-Z0-9]*")) {
            System.out.println("Invalid versification: " + versemap);
            return;
        }
        scheme = new LogosVersificationDetector().loadScheme(versemap);
        if (scheme == null) {
            System.out.println("Invalid versification: " + versemap);
            return;
        }
    }
    footnoteCounter = 0;
    grammarCounter = 0;
    String title = bible.getName();
    String verseSeparator = "<br />";
    lineSeparator = "<br />";
    boolean noChapterHeadings = false;
    if (exportArgs.length > 3 && exportArgs[3].equals("-inline")) {
        verseSeparator = " ";
        lineSeparator = "<br />&nbsp;&nbsp;&nbsp;&nbsp; ";
    } else if (exportArgs.length > 3 && exportArgs[3].equals("-nochapter")) {
        verseSeparator = " ";
        lineSeparator = "<br />&nbsp;&nbsp;&nbsp;&nbsp; ";
        noChapterHeadings = true;
    }
    try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(exportArgs[0])), StandardCharsets.UTF_8))) {
        bw.write("<html><head>\n" + "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" />\n" + "<style>" + "body, h1, h2, h3, h4, h5, h6 { font-family: \"Times New Roman\";}\n" + "a { color: black; text-decoration: none;}\n" + "a.sdfootnotesym, a.sdendnotesym { font-style: italic;}\n" + "h1 {font-size: 24pt;}\n" + "h2 {font-size: 22pt;}\n" + "h3 {font-size: 20pt;}\n" + "h4 {font-size: 18pt;}\n" + "h5 {font-size: 16pt;}\n" + "h6 {font-size: 14pt;}\n" + "</style>\n" + "</head><body lang=\"de-DE\">\n");
        if (exportArgs.length > 2 && !exportArgs[2].equals("-")) {
            StringWriter sw = new StringWriter();
            try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(exportArgs[2]), StandardCharsets.UTF_8))) {
                char[] buffer = new char[4096];
                int len;
                while ((len = br.read(buffer)) != -1) sw.write(buffer, 0, len);
            }
            String template = sw.toString();
            template = template.replace("${name}", bible.getName());
            MetadataBook mb = bible.getMetadataBook();
            if (mb != null) {
                bible.getBooks().remove(0);
                for (String key : mb.getKeys()) template = template.replace("${" + key + "}", mb.getValue(key));
            }
            bw.write(template);
        } else {
            bw.write("<h1>" + title.replace("&", "&amp").replace("<", "&lt;").replace(">", "&gt;") + "</h1>\n");
        }
        StringWriter footnotes = new StringWriter();
        for (Book book : bible.getBooks()) {
            BitSet[] chapterVerses;
            String milestone;
            if (bookDatatypeMap != null) {
                milestone = bookDatatypeMap.getProperty(book.getId().getOsisID());
                chapterVerses = milestone == null ? null : ALL_CHAPTER_VERSES;
            } else {
                chapterVerses = scheme.getCoveredBooks().get(book.getId());
                String babbr = LOGOS_BOOKS.get(book.getId());
                milestone = babbr == null ? null : (versemap + ":" + babbr + " %c:%v");
            }
            if (milestone == null && book.getId().getZefID() < 0 && book.getChapters().size() == 1) {
                Chapter chapter = book.getChapters().get(0);
                if (chapter.getVerses().size() == 0 && chapter.getProlog() != null) {
                    // prolog only book
                    bw.write("<h2>");
                    if (book.getId() == BookID.DICTIONARY_ENTRY) {
                        if (bible.getName().toUpperCase().contains("STRONG") && book.getShortName().matches("[GH][1-9][0-9]*")) {
                            bw.write("[[@" + (book.getShortName().startsWith("G") ? "Greek" : "Hebrew") + "Strongs:" + book.getShortName() + "]]");
                        } else {
                            bw.write("[[@Headword:" + book.getAbbr() + "]]");
                            if (!book.getAbbr().equals(book.getShortName())) {
                                bw.write("[[@Headword:" + book.getShortName() + "]]");
                            }
                            if (!book.getLongName().equals(book.getShortName())) {
                                bw.write("[[@Headword:" + book.getLongName() + "]]");
                            }
                        }
                    }
                    bw.write(book.getLongName() + "</h2>\n");
                    footnoteNumber = 0;
                    chapter.getProlog().accept(new LogosVisitor(bw, "", footnotes, false, versemap, scheme, null, null, null, 2));
                    bw.write("\n<br/>\n");
                    continue;
                }
            }
            if (milestone == null) {
                System.out.println("WARNING: Skipping book " + book.getId());
                continue;
            }
            bw.write("<h2>[[@" + formatMilestone(milestone, "", "") + "]]" + book.getLongName() + " (" + book.getAbbr() + ")</h2>\n");
            int cnumber = 0;
            for (Chapter chapter : book.getChapters()) {
                cnumber++;
                if (!chapter.getVerses().isEmpty() && chapter.getVerses().get(0).getNumber().endsWith(".p")) {
                    Chapter prologue = new Chapter();
                    BitSet prologueVerses = new BitSet(100);
                    prologueVerses.set(1, 100);
                    while (!chapter.getVerses().isEmpty() && chapter.getVerses().get(0).getNumber().endsWith(".p")) {
                        Verse v = chapter.getVerses().remove(0);
                        Verse vv = new Verse(v.getNumber().replace(".p", ""));
                        v.accept(vv.getAppendVisitor());
                        vv.finished();
                        prologue.getVerses().add(vv);
                    }
                    exportChapter(milestone, "Prologue", prologue, versemap, scheme, verseSeparator, noChapterHeadings, bw, footnotes, book, chapterVerses, prologueVerses);
                }
                BitSet thisChapterVerses = chapterVerses != null && cnumber <= chapterVerses.length ? chapterVerses[cnumber - 1] : null;
                exportChapter(milestone, "" + cnumber, chapter, versemap, scheme, verseSeparator, noChapterHeadings, bw, footnotes, book, chapterVerses, thisChapterVerses);
            }
        }
        bw.write(footnotes.toString());
        bw.write("</body></html>");
    }
}
Also used : MetadataBook(biblemulticonverter.data.MetadataBook) InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BitSet(java.util.BitSet) Chapter(biblemulticonverter.data.Chapter) VersificationScheme(biblemulticonverter.tools.AbstractVersificationDetector.VersificationScheme) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) LogosVersificationDetector(biblemulticonverter.logos.tools.LogosVersificationDetector) BufferedWriter(java.io.BufferedWriter) StringWriter(java.io.StringWriter) MetadataBook(biblemulticonverter.data.MetadataBook) Book(biblemulticonverter.data.Book) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) Verse(biblemulticonverter.data.Verse)

Aggregations

Book (biblemulticonverter.data.Book)22 MetadataBook (biblemulticonverter.data.MetadataBook)22 Chapter (biblemulticonverter.data.Chapter)19 Verse (biblemulticonverter.data.Verse)17 BookID (biblemulticonverter.data.BookID)13 VirtualVerse (biblemulticonverter.data.VirtualVerse)12 Bible (biblemulticonverter.data.Bible)10 FormattedText (biblemulticonverter.data.FormattedText)10 Headline (biblemulticonverter.data.FormattedText.Headline)10 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)9 FileInputStream (java.io.FileInputStream)6 Visitor (biblemulticonverter.data.FormattedText.Visitor)5 BIBLEBOOK (biblemulticonverter.schema.zef2005.BIBLEBOOK)5 CAPTION (biblemulticonverter.schema.zef2005.CAPTION)5 CHAPTER (biblemulticonverter.schema.zef2005.CHAPTER)5 VERS (biblemulticonverter.schema.zef2005.VERS)5 File (java.io.File)5 FileOutputStream (java.io.FileOutputStream)5 EnumMap (java.util.EnumMap)5