Search in sources :

Example 1 with ISqlJetCursor

use of org.tmatesoft.sqljet.core.table.ISqlJetCursor in project BibleMultiConverter by schierlm.

the class MyBibleZone method convertFromVerse.

private String convertFromVerse(String text, Visitor<RuntimeException> vv, SqlJetDb footnoteDB, int[] vnums) {
    int pos = text.indexOf("<");
    while (pos != -1) {
        String strongsWord = "";
        if (text.startsWith("<S>", pos)) {
            strongsWord = text.substring(0, pos);
            int spacePos = strongsWord.lastIndexOf(' ');
            if (spacePos != -1) {
                vv.visitText(cleanText(strongsWord.substring(0, spacePos + 1)));
                strongsWord = strongsWord.substring(spacePos + 1);
            }
        } else {
            vv.visitText(cleanText(text.substring(0, pos)));
        }
        text = text.substring(pos);
        if (text.startsWith("<p>"))
            // AT.SQLite3
            text = "<pb/>" + text.substring(3);
        if (text.startsWith("<S>")) {
            pos = text.indexOf("</S>");
            String[] txt = cleanText(text.substring(3, pos)).split(",");
            int[] snum = new int[txt.length];
            for (int i = 0; i < txt.length; i++) {
                try {
                    snum[i] = Integer.parseInt(txt[i].trim().replaceAll("^[GH]", ""));
                    if (snum[i] == 0) {
                        System.out.println("WARNING: Strong number may not be zero");
                        snum[i] = 99999;
                    }
                } catch (NumberFormatException ex) {
                    System.out.println("WARNING: Invalid Strong number: " + txt[i]);
                    snum[i] = 99999;
                }
            }
            String rmac = null;
            text = text.substring(pos + 4).replaceFirst("^ +<m>", "<m>");
            if (text.startsWith("<m>") && !rawMorphology) {
                pos = text.indexOf("</m>");
                rmac = cleanText(text.substring(3, pos));
                text = text.substring(pos + 4);
                if (!Utils.compilePattern(Utils.RMAC_REGEX).matcher(rmac).matches()) {
                    System.out.println("WARNING: Skipping malformed RMAC morphology code: " + rmac);
                    rmac = null;
                }
            }
            if (snum.length == 0 && rmac == null)
                vv.visitText(strongsWord);
            else
                vv.visitGrammarInformation(snum.length == 0 ? null : snum, rmac == null ? null : new String[] { rmac }, null).visitText(strongsWord);
        } else if (text.startsWith("<n>")) {
            text = convertFromVerse(text.substring(3), vv.visitCSSFormatting("font-style: italic; myBibleType=note"), footnoteDB, vnums);
            if (!text.startsWith("</n>"))
                System.out.println("WARNING: Unclosed <n> tag at: " + text);
            else {
                text = text.substring(4);
            }
        } else if (text.startsWith("<t>")) {
            vv.visitLineBreak(LineBreakKind.NEWLINE_WITH_INDENT);
            text = text.substring(3);
        } else if (text.startsWith("</t>")) {
            vv.visitLineBreak(LineBreakKind.NEWLINE);
            text = text.substring(4);
        } else if (text.startsWith("</")) {
            return text;
        } else if (text.startsWith("<pb/>")) {
            vv.visitLineBreak(LineBreakKind.PARAGRAPH);
            text = text.substring(5);
        } else if (text.startsWith("<br/>")) {
            vv.visitLineBreak(LineBreakKind.NEWLINE);
            text = text.substring(5);
        } else if ((text.startsWith("<m>") && rawMorphology) || (text.startsWith("<f>") && rawFootnotes)) {
            String tag = text.substring(1, 2);
            text = convertFromVerse(text.substring(3), vv.visitExtraAttribute(ExtraAttributePriority.SKIP, "mybiblezone", "rawtag", tag), footnoteDB, vnums);
            if (!text.startsWith("</" + tag + ">"))
                System.out.println("WARNING: Unclosed <" + tag + "> tag at: " + text);
            else {
                text = text.substring(4);
            }
        } else if (text.startsWith("<m>")) {
            System.out.println("WARNING: Morph code without Strongs not supported");
            vv.visitText("<");
            text = text.substring(1);
        } else if (text.startsWith("<i>")) {
            text = convertFromVerse(text.substring(3), vv.visitFormattingInstruction(FormattingInstructionKind.ITALIC), footnoteDB, vnums);
            if (!text.startsWith("</i>"))
                System.out.println("WARNING: Unclosed <i> tag at: " + text);
            else {
                text = text.substring(4);
            }
        } else if (text.startsWith("<J>")) {
            text = convertFromVerse(text.substring(3), vv.visitFormattingInstruction(FormattingInstructionKind.WORDS_OF_JESUS), footnoteDB, vnums);
            if (!text.startsWith("</J>"))
                System.out.println("WARNING: Unclosed <J> tag at: " + text);
            else {
                text = text.substring(4);
            }
        } else if (text.startsWith("<e>")) {
            text = convertFromVerse(text.substring(3), vv.visitFormattingInstruction(FormattingInstructionKind.BOLD), footnoteDB, vnums);
            if (!text.startsWith("</e>"))
                System.out.println("WARNING: Unclosed <e> tag at: " + text);
            else {
                text = text.substring(4);
            }
        } else if (text.startsWith("<f>")) {
            pos = text.indexOf("</f>");
            String fn = cleanText(text.substring(3, pos));
            text = text.substring(pos + 4);
            if (!fn.matches("\\[\\*?[0-9]+\\]"))
                System.out.println("WARNING: Unusual footnote mark: " + fn);
            try {
                ISqlJetCursor cursor = footnoteDB.getTable("commentaries").lookup("commentaries_index", vnums[0], vnums[1], vnums[2]);
                String html = null;
                while (!cursor.eof()) {
                    String marker = cursor.getString("marker");
                    if (marker.equals(fn)) {
                        html = cursor.getString("text");
                    }
                    cursor.next();
                }
                cursor.close();
                if (html == null)
                    System.out.println("WARNING: Footnote text " + fn + " not found in " + vnums[0] + " " + vnums[1] + ":" + vnums[2]);
                else
                    convertFromHTML(html, vv.visitFootnote());
            } catch (SqlJetException ex) {
                throw new RuntimeException(text, ex);
            }
        } else {
            System.out.println("WARNING: Unknown tag, treated as plain text: " + text);
            vv.visitText("<");
            text = text.substring(1);
        }
        pos = text.indexOf("<");
    }
    vv.visitText(cleanText(text));
    return "";
}
Also used : SqlJetException(org.tmatesoft.sqljet.core.SqlJetException) ISqlJetCursor(org.tmatesoft.sqljet.core.table.ISqlJetCursor)

Example 2 with ISqlJetCursor

use of org.tmatesoft.sqljet.core.table.ISqlJetCursor in project BibleMultiConverter by schierlm.

the class SQLiteDump method run.

@Override
public void run(String... args) throws Exception {
    SqlJetDb db = SqlJetDb.open(new File(args[0]), false);
    db.beginTransaction(SqlJetTransactionMode.READ_ONLY);
    try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(args[1]), StandardCharsets.UTF_8))) {
        ISqlJetOptions options = db.getOptions();
        bw.write("FLAG\tAutovacuum\t" + options.isAutovacuum());
        bw.newLine();
        bw.write("FLAG\tLegacyFileFormat\t" + options.isLegacyFileFormat());
        bw.newLine();
        bw.write("FLAG\tIncrementalVacuum\t" + options.isIncrementalVacuum());
        bw.newLine();
        bw.write("OPTION\tCacheSize\t" + options.getCacheSize());
        bw.newLine();
        bw.write("OPTION\tEncoding\t" + options.getEncoding());
        bw.newLine();
        bw.write("OPTION\tFileFormat\t" + options.getFileFormat());
        bw.newLine();
        bw.write("OPTION\tSchemaVersion\t" + options.getSchemaVersion());
        bw.newLine();
        bw.write("OPTION\tUserVersion\t" + options.getUserVersion());
        bw.newLine();
        ISqlJetSchema schema = db.getSchema();
        List<String> triggers = new ArrayList<>(schema.getTriggerNames());
        Collections.sort(triggers);
        for (String trigger : triggers) {
            bw.write("TRG\t" + trigger + "\t" + schema.getTrigger(trigger).toSQL());
            bw.newLine();
        }
        List<String> indexes = new ArrayList<>(schema.getIndexNames());
        Collections.sort(indexes);
        for (String index : indexes) {
            bw.write("IDX\t" + index + "\t" + schema.getIndex(index).toSQL());
            bw.newLine();
        }
        List<String> views = new ArrayList<>(schema.getViewNames());
        Collections.sort(views);
        for (String view : views) {
            bw.write("VIEW\t" + view + "\t" + schema.getView(view).toSQL());
            bw.newLine();
        }
        List<String> vTables = new ArrayList<>(schema.getVirtualTableNames());
        Collections.sort(vTables);
        for (String vtbl : vTables) {
            bw.write("VTBL\t" + vtbl + "\t" + schema.getVirtualTable(vtbl).toSQL());
            bw.newLine();
        }
        List<String> tables = new ArrayList<>(schema.getTableNames());
        Collections.sort(tables);
        for (String table : tables) {
            bw.write("TABLE\t" + table + "\t" + schema.getTable(table).toSQL());
            bw.newLine();
            ISqlJetTable tbl = db.getTable(table);
            String primaryIndex = tbl.getPrimaryKeyIndexName();
            ISqlJetCursor cursor = primaryIndex == null ? tbl.open() : tbl.order(primaryIndex);
            int count = cursor.getFieldsCount();
            while (!cursor.eof()) {
                for (int i = 0; i < count; i++) {
                    bw.write("  " + cursor.getFieldType(i).name().substring(0, 1) + "\t" + escape("" + cursor.getValue(i)));
                    bw.newLine();
                }
                bw.write("  -----");
                bw.newLine();
                cursor.next();
            }
            cursor.close();
        }
    }
    db.commit();
    db.close();
}
Also used : SqlJetDb(org.tmatesoft.sqljet.core.table.SqlJetDb) ArrayList(java.util.ArrayList) ISqlJetOptions(org.tmatesoft.sqljet.core.table.ISqlJetOptions) ISqlJetSchema(org.tmatesoft.sqljet.core.schema.ISqlJetSchema) BufferedWriter(java.io.BufferedWriter) FileOutputStream(java.io.FileOutputStream) ISqlJetTable(org.tmatesoft.sqljet.core.table.ISqlJetTable) OutputStreamWriter(java.io.OutputStreamWriter) ISqlJetCursor(org.tmatesoft.sqljet.core.table.ISqlJetCursor) File(java.io.File)

Example 3 with ISqlJetCursor

use of org.tmatesoft.sqljet.core.table.ISqlJetCursor 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)

Aggregations

ISqlJetCursor (org.tmatesoft.sqljet.core.table.ISqlJetCursor)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 SqlJetDb (org.tmatesoft.sqljet.core.table.SqlJetDb)2 Bible (biblemulticonverter.data.Bible)1 Book (biblemulticonverter.data.Book)1 BookID (biblemulticonverter.data.BookID)1 Chapter (biblemulticonverter.data.Chapter)1 FormattedText (biblemulticonverter.data.FormattedText)1 Headline (biblemulticonverter.data.FormattedText.Headline)1 MetadataBook (biblemulticonverter.data.MetadataBook)1 Verse (biblemulticonverter.data.Verse)1 VirtualVerse (biblemulticonverter.data.VirtualVerse)1 BufferedWriter (java.io.BufferedWriter)1 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 SqlJetException (org.tmatesoft.sqljet.core.SqlJetException)1