Search in sources :

Example 11 with Visitor

use of biblemulticonverter.data.FormattedText.Visitor in project BibleMultiConverter by schierlm.

the class ZefaniaXML method parseContent.

private boolean parseContent(Visitor<RuntimeException> visitor, List<Object> contentList, Map<BookID, String> abbrMap) throws IOException {
    boolean contentFound = false;
    for (Object n : contentList) {
        if (n instanceof String) {
            String value = normalize((String) n, false);
            visitor.visitText(value);
            contentFound |= value.trim().length() > 0;
        } else if (n instanceof DIV || n instanceof NOTE) {
            NOTE note;
            if (n instanceof DIV) {
                note = ((DIV) n).getNOTE();
            } else {
                note = (NOTE) n;
            }
            if (note.getContent().size() == 0)
                continue;
            Visitor<RuntimeException> v;
            v = visitor.visitFootnote();
            boolean subContentFound = parseContent(v, note.getContent(), abbrMap);
            if (!subContentFound)
                visitEmptyMarker(v);
            contentFound = true;
        } else if (n instanceof BR) {
            BR br = (BR) n;
            Visitor<RuntimeException> v = visitor;
            int count = 1;
            if (br.getCount() != null) {
                count = br.getCount().intValue();
            }
            if (count < 1 || count > 10)
                count = 1;
            for (int ii = 0; ii < count; ii++) {
                if (br.getArt() == EnumBreak.X_P)
                    v.visitLineBreak(LineBreakKind.PARAGRAPH);
                else
                    v.visitLineBreak(LineBreakKind.NEWLINE);
            }
            contentFound = true;
        } else if (n instanceof XREF) {
            XREF xref = (XREF) n;
            Visitor<RuntimeException> footnoteVisitor = visitor.visitFootnote();
            footnoteVisitor.visitText(FormattedText.XREF_MARKER.trim());
            boolean first = true;
            if (xref.getMscope() == null) {
                if (xref.getFscope() == null) {
                    System.out.println("WARNING: Ignoring XREF with neither fscope nor mscope");
                } else {
                    for (String fscope : xref.getFscope().split("; ")) {
                        Matcher m = Utils.compilePattern("([0-9A-Za-z]+) ([0-9]+), ([0-9]+[a-z]?)").matcher(fscope);
                        if (!m.matches()) {
                            System.out.println("WARNING: Unable to parse XREF fscope " + fscope + ", skipping");
                            continue;
                        }
                        String xBook = m.group(1);
                        int xChapter = Integer.parseInt(m.group(2));
                        String xVerse = m.group(3);
                        BookID xID = null;
                        for (Map.Entry<BookID, String> abbrEntry : abbrMap.entrySet()) {
                            if (abbrEntry.getValue().equals(xBook)) {
                                xID = abbrEntry.getKey();
                                break;
                            }
                        }
                        if (xID == null) {
                            System.out.println("WARNING: Book not found for XREF fscope " + fscope + ", skipping");
                            continue;
                        }
                        footnoteVisitor.visitText(" ");
                        footnoteVisitor.visitCrossReference(xBook, xID, xChapter, xVerse, xChapter, xVerse).visitText(xBook + " " + xChapter + ":" + xVerse);
                    }
                }
            } else {
                for (String mscope : xref.getMscope().split(" ")) {
                    Matcher m = Utils.compilePattern("([0-9]+);([0-9]+)(-[0-9]+)?;([0-9]+)(-[0-9]+)?").matcher(mscope);
                    if (!m.matches())
                        continue;
                    BookID bookID = BookID.fromZefId(Integer.parseInt(m.group(1)));
                    int chapter = Integer.parseInt(m.group(2)), endChapter = chapter;
                    if (m.group(3) != null)
                        endChapter = Integer.parseInt(m.group(3).substring(1));
                    String verse = m.group(4);
                    String endVerse = m.group(5);
                    if (endVerse == null)
                        endVerse = verse;
                    else
                        endVerse = endVerse.substring(1);
                    if (verse.equals("0") || endVerse.equals("0"))
                        continue;
                    if (chapter == endChapter && Integer.parseInt(verse) > Integer.parseInt(endVerse))
                        continue;
                    String abbr = abbrMap.get(bookID);
                    if (abbr == null)
                        abbr = bookID.getOsisID();
                    if (first)
                        first = false;
                    else
                        footnoteVisitor.visitText(" ");
                    footnoteVisitor.visitCrossReference(abbr, bookID, chapter, verse, endChapter, endVerse).visitText(abbr + " " + chapter + ":" + verse);
                }
            }
            if (first)
                visitEmptyMarker(footnoteVisitor);
            contentFound = true;
        } else if (n instanceof JAXBElement<?>) {
            String name = ((JAXBElement<?>) n).getName().toString();
            Object nn = ((JAXBElement<?>) n).getValue();
            if (name.equals("STYLE") && nn instanceof STYLE) {
                String css = ((STYLE) nn).getCss();
                String id = ((STYLE) nn).getId();
                FormattingInstructionKind kind = null;
                if (id != null && id.equals("cl:divineName")) {
                    kind = FormattingInstructionKind.DIVINE_NAME;
                } else if (css == null || css.startsWith("display:block;")) {
                    kind = null;
                } else if (css.contains("italic")) {
                    kind = FormattingInstructionKind.ITALIC;
                } else if (css.contains("bold")) {
                    kind = FormattingInstructionKind.BOLD;
                } else if (css.toLowerCase().contains("color:#ff0000")) {
                    kind = FormattingInstructionKind.WORDS_OF_JESUS;
                } else if (css.contains("color:blue")) {
                    kind = FormattingInstructionKind.LINK;
                } else if (css.contains("vertical-align:super") || css.equals("font-size:small")) {
                    kind = FormattingInstructionKind.SUPERSCRIPT;
                }
                Visitor<RuntimeException> contentVisitor = visitor;
                if (kind != null) {
                    contentVisitor = contentVisitor.visitFormattingInstruction(kind);
                } else if (css != null && (kind == null || !kind.getCss().equals(css))) {
                    contentVisitor = contentVisitor.visitCSSFormatting(css);
                }
                List<Object> content = ((STYLE) nn).getContent();
                boolean subContentFound = parseContent(contentVisitor, content, abbrMap);
                if (!subContentFound)
                    visitEmptyMarker(contentVisitor);
            } else if ((name.equals("gr") || name.equals("GRAM")) && nn instanceof GRAM) {
                GRAM gram = (GRAM) nn;
                boolean addSpace = false;
                int lastIndex = gram.getContent().size() - 1;
                if (lastIndex >= 0 && gram.getContent().get(lastIndex) instanceof String) {
                    String lastString = normalize((String) gram.getContent().get(lastIndex), false);
                    if (lastString.endsWith(" ")) {
                        String afterString = "";
                        int pos = contentList.indexOf(n);
                        if (pos < contentList.size() - 1 && contentList.get(pos + 1) instanceof String) {
                            afterString = normalize((String) contentList.get(pos + 1), false);
                        }
                        if (!afterString.startsWith(" ")) {
                            addSpace = true;
                            gram.getContent().set(lastIndex, lastString.substring(0, lastString.length() - 1));
                        }
                    }
                }
                Visitor<RuntimeException> strongVisitor = visitor;
                int[] strongs = null;
                if (gram.getStr() != null) {
                    List<String> strongList = new ArrayList<String>(Arrays.asList(gram.getStr().trim().replaceAll(" ++", " ").replace("G", "").replace("H", "").split(" ")));
                    for (int i = 0; i < strongList.size(); i++) {
                        if (!strongList.get(i).matches("[0-9]+")) {
                            System.out.println("WARNING: Skipping invalid Strong number " + strongList.get(i));
                            strongList.remove(i);
                            i--;
                        }
                    }
                    strongs = new int[strongList.size()];
                    for (int i = 0; i < strongs.length; i++) {
                        strongs[i] = Integer.parseInt(strongList.get(i));
                    }
                }
                String[] rmac = null;
                if (gram.getRmac() != null && gram.getRmac().length() > 0) {
                    List<String> rmacList = new ArrayList<String>(Arrays.asList(gram.getRmac().toUpperCase().split(" ")));
                    for (int i = 0; i < rmacList.size(); i++) {
                        String rmacValue = rmacList.get(i);
                        if (rmacValue.endsWith("-"))
                            rmacValue = rmacValue.substring(0, rmacValue.length() - 1);
                        rmacList.set(i, rmacValue);
                        if (!rmacValue.matches(Utils.RMAC_REGEX)) {
                            System.out.println("WARNING: Skipping invalid RMAC: " + rmacValue);
                            rmacList.remove(i);
                            i--;
                        }
                        rmac = (String[]) rmacList.toArray(new String[rmacList.size()]);
                    }
                }
                if (strongs != null && strongs.length == 0)
                    strongs = null;
                if (rmac != null && rmac.length == 0)
                    rmac = null;
                if (strongs != null)
                    strongVisitor = strongVisitor.visitGrammarInformation(strongs, rmac, null);
                if (!parseContent(strongVisitor, gram.getContent(), abbrMap) && strongVisitor != visitor) {
                    visitEmptyMarker(strongVisitor);
                }
                if (addSpace)
                    visitor.visitText(" ");
            } else {
                continue;
            }
            contentFound = true;
        }
    }
    return contentFound;
}
Also used : Visitor(biblemulticonverter.data.FormattedText.Visitor) Matcher(java.util.regex.Matcher) STYLE(biblemulticonverter.schema.zef2005.STYLE) FormattingInstructionKind(biblemulticonverter.data.FormattedText.FormattingInstructionKind) JAXBElement(javax.xml.bind.JAXBElement) DIV(biblemulticonverter.schema.zef2005.DIV) BR(biblemulticonverter.schema.zef2005.BR) XREF(biblemulticonverter.schema.zef2005.XREF) BookID(biblemulticonverter.data.BookID) NOTE(biblemulticonverter.schema.zef2005.NOTE) GRAM(biblemulticonverter.schema.zef2005.GRAM) List(java.util.List) ArrayList(java.util.ArrayList)

Example 12 with Visitor

use of biblemulticonverter.data.FormattedText.Visitor in project BibleMultiConverter by schierlm.

the class HaggaiXML method parseContent.

private boolean parseContent(Visitor<RuntimeException> visitor, List<? extends Object> contentList, Map<BookID, String> abbrMap) throws IOException {
    boolean contentFound = false;
    for (Object n : contentList) {
        if (n instanceof String) {
            String value = normalize((String) n, false);
            visitor.visitText(value);
            contentFound |= value.trim().length() > 0;
        } else if (n instanceof JAXBElement<?>) {
            String name = ((JAXBElement<?>) n).getName().toString();
            Object nn = ((JAXBElement<?>) n).getValue();
            if (name.equals("STYLE") && nn instanceof STYLE) {
                TStyleFix fs = ((STYLE) nn).getFs();
                FormattingInstructionKind kind;
                switch(fs) {
                    case BOLD:
                        kind = FormattingInstructionKind.BOLD;
                        break;
                    case DIVINE_NAME:
                        kind = FormattingInstructionKind.DIVINE_NAME;
                        break;
                    case EMPHASIS:
                    case ITALIC:
                        kind = FormattingInstructionKind.ITALIC;
                        break;
                    case LINE_THROUGH:
                        kind = FormattingInstructionKind.STRIKE_THROUGH;
                        break;
                    case SUB:
                        kind = FormattingInstructionKind.SUBSCRIPT;
                        break;
                    case SUPER:
                        kind = FormattingInstructionKind.SUPERSCRIPT;
                        break;
                    case UNDERLINE:
                        kind = FormattingInstructionKind.UNDERLINE;
                        break;
                    case UPPERCASE:
                    case ACROSTIC:
                    case ILLUMINATED:
                    case LOWERCASE:
                    case NORMAL:
                    case OVERLINE:
                    case SMALL_CAPS:
                    default:
                        kind = null;
                        break;
                }
                if (kind == null)
                    throw new IOException(fs.toString());
                Visitor<RuntimeException> contentVisitor = visitor;
                if (kind != null) {
                    contentVisitor = contentVisitor.visitFormattingInstruction(kind);
                }
                List<Serializable> content = ((STYLE) nn).getContent();
                boolean subContentFound = parseContent(contentVisitor, content, abbrMap);
                if (!subContentFound)
                    visitEmptyMarker(contentVisitor);
            } else if ((name.equals("gr") || name.equals("GRAM")) && nn instanceof GRAM) {
                GRAM gram = (GRAM) nn;
                Visitor<RuntimeException> strongVisitor = visitor;
                int[] strongs = null;
                if (gram.getStr() != null) {
                    List<String> strongList = new ArrayList<String>(Arrays.asList(gram.getStr().trim().replaceAll(" ++", " ").replace("G", "").replace("H", "").split(" ")));
                    for (int i = 0; i < strongList.size(); i++) {
                        if (!strongList.get(i).matches("[0-9]+")) {
                            System.out.println("WARNING: Skipping invalid Strong number " + strongList.get(i));
                            strongList.remove(i);
                            i--;
                        }
                    }
                    strongs = new int[strongList.size()];
                    for (int i = 0; i < strongs.length; i++) {
                        strongs[i] = Integer.parseInt(strongList.get(i));
                    }
                }
                String[] rmac = null;
                if (gram.getRmac() != null && gram.getRmac().length() > 0) {
                    List<String> rmacList = new ArrayList<String>(Arrays.asList(gram.getRmac().toUpperCase().split(" ")));
                    for (int i = 0; i < rmacList.size(); i++) {
                        String rmacValue = rmacList.get(i);
                        if (rmacValue.endsWith("-"))
                            rmacValue = rmacValue.substring(0, rmacValue.length() - 1);
                        rmacList.set(i, rmacValue);
                        if (!rmacValue.matches(Utils.RMAC_REGEX)) {
                            System.out.println("WARNING: Skipping invalid RMAC: " + rmacValue);
                            rmacList.remove(i);
                            i--;
                        }
                        rmac = (String[]) rmacList.toArray(new String[rmacList.size()]);
                    }
                }
                if (strongs != null && strongs.length == 0)
                    strongs = null;
                if (rmac != null && rmac.length == 0)
                    rmac = null;
                if (strongs != null || rmac != null)
                    strongVisitor = strongVisitor.visitGrammarInformation(strongs, rmac, null);
                if (!parseContent(strongVisitor, gram.getContent(), abbrMap) && strongVisitor != visitor) {
                    visitEmptyMarker(strongVisitor);
                }
            } else if (name.equals("NOTE") && nn instanceof NOTE) {
                NOTE note = (NOTE) nn;
                if (note.getContent().size() == 0)
                    continue;
                Visitor<RuntimeException> v;
                v = visitor.visitFootnote();
                boolean subContentFound = parseContent(v, note.getContent(), abbrMap);
                if (!subContentFound)
                    visitEmptyMarker(v);
                contentFound = true;
            } else if (name.equals("BR")) {
                visitor.visitLineBreak(LineBreakKind.NEWLINE);
                contentFound = true;
            } else {
                throw new IOException(name);
            }
            contentFound = true;
        } else {
            throw new IOException(n.getClass().toString());
        }
    }
    return contentFound;
}
Also used : Visitor(biblemulticonverter.data.FormattedText.Visitor) STYLE(biblemulticonverter.schema.haggai.STYLE) ArrayList(java.util.ArrayList) FormattingInstructionKind(biblemulticonverter.data.FormattedText.FormattingInstructionKind) JAXBElement(javax.xml.bind.JAXBElement) IOException(java.io.IOException) GRAM(biblemulticonverter.schema.haggai.GRAM) NOTE(biblemulticonverter.schema.haggai.NOTE) List(java.util.List) ArrayList(java.util.ArrayList) TStyleFix(biblemulticonverter.schema.haggai.TStyleFix)

Example 13 with Visitor

use of biblemulticonverter.data.FormattedText.Visitor in project BibleMultiConverter by schierlm.

the class BibleAnalyzerDatabase method doExport.

@Override
public void doExport(Bible bible, String... exportArgs) throws Exception {
    File outfile = new File(exportArgs[0]);
    String basename = outfile.getName().replaceAll("\\..*", "");
    boolean hasStrongs = false, hasRMAC = false;
    for (Book bk : bible.getBooks()) {
        for (Chapter ch : bk.getChapters()) {
            for (Verse vv : ch.getVerses()) {
                String elementTypes = vv.getElementTypes(Integer.MAX_VALUE);
                if (elementTypes.contains("g")) {
                    final boolean[] hasGrammar = { hasStrongs, hasRMAC };
                    vv.accept(new VisitorAdapter<RuntimeException>(null) {

                        @Override
                        protected Visitor<RuntimeException> wrapChildVisitor(Visitor<RuntimeException> childVisitor) {
                            return this;
                        }

                        @Override
                        public Visitor<RuntimeException> visitGrammarInformation(int[] strongs, String[] rmac, int[] sourceIndices) {
                            if (strongs != null)
                                hasGrammar[0] = true;
                            if (rmac != null)
                                hasGrammar[1] = true;
                            return super.visitGrammarInformation(strongs, rmac, sourceIndices);
                        }
                    });
                    hasStrongs = hasGrammar[0];
                    hasRMAC = hasGrammar[1];
                }
            }
        }
    }
    outfile.delete();
    SqlJetDb db = SqlJetDb.open(outfile, true);
    db.getOptions().setAutovacuum(true);
    db.beginTransaction(SqlJetTransactionMode.WRITE);
    db.getOptions().setUserVersion(0);
    db.createTable("CREATE TABLE bible (id INTEGER PRIMARY KEY, ref, verse)");
    db.createIndex("CREATE INDEX refsidx ON bible (ref)");
    db.createTable("CREATE TABLE footnote (id INTEGER PRIMARY KEY, ref, note)");
    db.createTable("CREATE TABLE heading (id INTEGER PRIMARY KEY, ref, head)");
    db.createTable("CREATE TABLE images (fName, imageData)");
    db.createTable("CREATE TABLE option (strongs, interlinear, encrypt, apocrypha)");
    db.createTable("CREATE TABLE title (abbr, desc, info)");
    db.createTable("CREATE TABLE wordlist (word, freq)");
    db.createIndex("CREATE INDEX wordlistidx ON wordlist (word)");
    db.getTable("option").insert(hasStrongs ? 1 : 0, hasRMAC ? 1 : 0, 0, 0);
    db.getTable("title").insert(basename, bible.getName(), bible.getName());
    bibleTable = db.getTable("bible");
    footnoteTable = db.getTable("footnote");
    headingTable = db.getTable("heading");
    bibleID = footnoteID = headingID = 1;
    exportBible(basename, null, bible, hasStrongs, hasRMAC);
    db.commit();
    db.close();
}
Also used : Visitor(biblemulticonverter.data.FormattedText.Visitor) SqlJetDb(org.tmatesoft.sqljet.core.table.SqlJetDb) Chapter(biblemulticonverter.data.Chapter) Book(biblemulticonverter.data.Book) File(java.io.File) VirtualVerse(biblemulticonverter.data.VirtualVerse) Verse(biblemulticonverter.data.Verse)

Example 14 with Visitor

use of biblemulticonverter.data.FormattedText.Visitor in project BibleMultiConverter by schierlm.

the class MyBibleZone method doExport.

@Override
public void doExport(Bible bible, String... exportArgs) throws Exception {
    String outfile = exportArgs[0];
    if (!outfile.endsWith(".SQLite3"))
        outfile += ".SQLite3";
    boolean hasFootnotes = false, hasStrongs = false;
    for (Book bk : bible.getBooks()) {
        for (Chapter ch : bk.getChapters()) {
            for (Verse vv : ch.getVerses()) {
                String elementTypes = vv.getElementTypes(Integer.MAX_VALUE);
                if (elementTypes.contains("f")) {
                    hasFootnotes = true;
                }
                if (elementTypes.contains("g")) {
                    hasStrongs = true;
                }
            }
        }
    }
    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 books (book_number NUMERIC, book_color TEXT, short_name TEXT, long_name TEXT)");
    db.createTable("CREATE TABLE introductions (book_number NUMERIC, introduction TEXT)");
    db.createIndex("CREATE UNIQUE INDEX introductions_index on introductions(book_number)");
    db.createTable("CREATE TABLE verses (book_number INTEGER, chapter INTEGER, verse INTEGER, text TEXT)");
    db.createIndex("CREATE UNIQUE INDEX verses_index on verses (book_number, chapter, verse)");
    db.createTable("CREATE TABLE stories (book_number NUMERIC, chapter NUMERIC, verse NUMERIC, order_if_several NUMERIC, title TEXT)");
    db.createIndex("CREATE UNIQUE INDEX stories_index on stories(book_number, chapter, verse, order_if_several)");
    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("chapter_string", "Chapter");
    infoValues.put("introduction_string", "Introduction");
    infoValues.put("strong_numbers", hasStrongs ? "true" : "false");
    infoValues.put("right_to_left", "false");
    infoValues.put("digits0-9", "0123456789");
    infoValues.put("swaps_non_localized_words_in_mixed_language_line", "false");
    infoValues.put("localized_book_abbreviations", "false");
    infoValues.put("font_scale", "1.0");
    infoValues.put("contains_accents", "true");
    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));
        }
    }
    String bibleIntro = null, singleFootnoteMarker = null, singleXrefMarker = null;
    if (exportArgs.length > 1) {
        Properties props = new Properties();
        FileInputStream in = new FileInputStream(exportArgs[1]);
        props.load(in);
        in.close();
        bibleIntro = (String) props.remove("__INTRODUCTION__");
        singleFootnoteMarker = (String) props.remove("__FOOTNOTE_MARKER__");
        singleXrefMarker = (String) props.remove("__XREF_MARKER__");
        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 booksTable = db.getTable("books");
    ISqlJetTable introductionsTable = db.getTable("introductions");
    ISqlJetTable versesTable = db.getTable("verses");
    ISqlJetTable storiesTable = db.getTable("stories");
    for (Map.Entry<String, String> entry : infoValues.entrySet()) {
        infoTable.insert(entry.getKey(), entry.getValue());
    }
    SqlJetDb cdb = null;
    ISqlJetTable footnotesTable = null;
    if (hasFootnotes) {
        String commentaryfile = outfile.replace(".SQLite3", ".commentaries.SQLite3");
        new File(commentaryfile).delete();
        cdb = SqlJetDb.open(new File(commentaryfile), true);
        cdb.getOptions().setAutovacuum(true);
        cdb.beginTransaction(SqlJetTransactionMode.WRITE);
        cdb.getOptions().setUserVersion(0);
        cdb.createTable("CREATE TABLE info (name TEXT, value TEXT)");
        cdb.createTable("CREATE TABLE commentaries (book_number NUMERIC, chapter_number_from NUMERIC, verse_number_from NUMERIC, chapter_number_to NUMERIC, verse_number_to NUMERIC, marker TEXT, text TEXT )");
        cdb.createIndex("CREATE INDEX commentaries_index on commentaries(book_number, chapter_number_from, verse_number_from)");
        ISqlJetTable cInfoTable = cdb.getTable("info");
        for (String key : Arrays.asList("language", "description", "russian_numbering")) {
            cInfoTable.insert(key, infoValues.get(key));
        }
        cInfoTable.insert("is_footnotes", "true");
        footnotesTable = cdb.getTable("commentaries");
    }
    final Set<String> unsupportedFeatures = new HashSet<>();
    FormattedText introProlog = null;
    for (Book bk : bible.getBooks()) {
        if (bk.getId() == BookID.INTRODUCTION || bk.getId() == BookID.INTRODUCTION_OT || bk.getId() == BookID.INTRODUCTION_NT || bk.getId() == BookID.APPENDIX) {
            if (introProlog == null)
                introProlog = new FormattedText();
            introProlog.getAppendVisitor().visitHeadline(1).visitText(bk.getLongName());
            bk.getChapters().get(0).getProlog().accept(introProlog.getAppendVisitor());
            continue;
        }
        MyBibleZoneBook info = null;
        for (MyBibleZoneBook bi : BOOK_INFO) {
            if (bi.bookID == bk.getId())
                info = bi;
        }
        if (info == null) {
            System.out.println("WARNING: Skipping unsupported book " + bk.getId());
            continue;
        }
        booksTable.insert(info.bookNumber, info.bookColor, bk.getAbbr(), bk.getShortName());
        FormattedText prologs = null;
        for (int cn = 1; cn <= bk.getChapters().size(); cn++) {
            Chapter ch = bk.getChapters().get(cn - 1);
            if (ch.getProlog() != null) {
                if (prologs == null)
                    prologs = new FormattedText();
                prologs.getAppendVisitor().visitHeadline(1).visitText(cn == 1 ? bk.getLongName() : "" + cn);
                ch.getProlog().accept(prologs.getAppendVisitor());
            }
            int vn = 0;
            for (VirtualVerse vv : ch.createVirtualVerses()) {
                vn++;
                while (vn < vv.getNumber()) versesTable.insert(info.bookNumber, cn, vn++, "");
                if (vn != vv.getNumber())
                    throw new RuntimeException(vn + " != " + vv.getNumber());
                for (int hl = 0; hl < vv.getHeadlines().size(); hl++) {
                    final StringBuilder sb = new StringBuilder();
                    final Map<StringBuilder, String> xrefTags = new HashMap<>();
                    vv.getHeadlines().get(hl).accept(new VisitorAdapter<RuntimeException>(null) {

                        @Override
                        protected Visitor<RuntimeException> wrapChildVisitor(Visitor<RuntimeException> childVisitor) throws RuntimeException {
                            return this;
                        }

                        @Override
                        protected void beforeVisit() throws RuntimeException {
                            unsupportedFeatures.add("markup in headline");
                        }

                        @Override
                        public void visitText(String text) throws RuntimeException {
                            sb.append(text.replace('<', '〈').replace('>', '〉'));
                        }

                        @Override
                        public Visitor<RuntimeException> visitFootnote() throws RuntimeException {
                            // handle this separately; we do not like
                            // footnote text inside the headline!
                            unsupportedFeatures.add("footnote in headline");
                            return new VisitorAdapter<RuntimeException>(null) {

                                @Override
                                protected Visitor<RuntimeException> wrapChildVisitor(Visitor<RuntimeException> childVisitor) throws RuntimeException {
                                    return this;
                                }

                                @Override
                                public Visitor<RuntimeException> visitCrossReference(String bookAbbr, BookID book, int firstChapter, String firstVerse, int lastChapter, String lastVerse) throws RuntimeException {
                                    if (!BOOK_NUMBERS.containsKey(book))
                                        return null;
                                    final StringBuilder innerBuilder = new StringBuilder();
                                    String endVerse = firstChapter != lastChapter ? "-" + lastChapter + ":" + lastVerse : !firstVerse.equals(lastVerse) ? "-" + lastVerse : "";
                                    xrefTags.put(innerBuilder, "<x>" + BOOK_NUMBERS.get(book) + " " + firstChapter + ":" + firstVerse + endVerse + "</x>");
                                    return new VisitorAdapter<RuntimeException>(null) {

                                        @Override
                                        protected void beforeVisit() throws RuntimeException {
                                            throw new RuntimeException("Unsupported content inside headline xref");
                                        }

                                        @Override
                                        public void visitText(String text) throws RuntimeException {
                                            innerBuilder.append(text.replace('<', '〈').replace('>', '〉'));
                                        }
                                    };
                                }
                            };
                        }

                        @Override
                        public Visitor<RuntimeException> visitExtraAttribute(ExtraAttributePriority prio, String category, String key, String value) throws RuntimeException {
                            unsupportedFeatures.add("extra atrribute in headline");
                            return prio.handleVisitor(category, this);
                        }
                    });
                    String headline = sb.toString();
                    for (Map.Entry<StringBuilder, String> xrefTag : xrefTags.entrySet()) {
                        headline = headline.replace(xrefTag.getKey().toString(), xrefTag.getValue());
                    }
                    storiesTable.insert(info.bookNumber, cn, vn, hl, headline);
                }
                StringBuilder vb = new StringBuilder();
                Map<String, MyBibleHTMLVisitor> footnotes = new HashMap<>();
                MyBibleVerseVisitor mbvv = new MyBibleVerseVisitor(vb, footnotes, unsupportedFeatures);
                for (Verse v : vv.getVerses()) {
                    if (!v.getNumber().equals("" + vv.getNumber())) {
                        vb.append(" <e>(" + v.getNumber() + ")</e> ");
                    }
                    mbvv.reset();
                    v.accept(mbvv);
                }
                if (singleXrefMarker != null || singleFootnoteMarker != null) {
                    String singleXref = null, singleFootnote = null;
                    for (Map.Entry<String, MyBibleHTMLVisitor> fn : footnotes.entrySet()) {
                        if (!fn.getKey().matches("\\[[0-9]+\\]"))
                            continue;
                        if (fn.getValue().getResult().startsWith(FormattedText.XREF_MARKER) && singleXrefMarker != null) {
                            if (singleXref == null) {
                                singleXref = fn.getKey();
                            } else {
                                System.out.println("WARNING: More than one XREF footnote in verse " + info.bookID + " " + cn + ":" + vn);
                                singleXref = "-";
                            }
                        } else if (singleFootnoteMarker != null) {
                            if (singleFootnote == null) {
                                singleFootnote = fn.getKey();
                            } else {
                                System.out.println("WARNING: More than one normal footnote in verse " + info.bookID + " " + cn + ":" + vn);
                                singleFootnote = "-";
                            }
                        }
                    }
                    if (singleXref != null && !singleXref.equals("-")) {
                        MyBibleHTMLVisitor xfn = footnotes.remove(singleXref);
                        if (xfn == null)
                            throw new RuntimeException();
                        footnotes.put(singleXrefMarker, xfn);
                        String verse = vb.toString();
                        vb.setLength(0);
                        vb.append(verse.replace("<f>" + singleXref + "</f>", "<f>" + singleXrefMarker + "</f>"));
                    }
                    if (singleFootnote != null && !singleFootnote.equals("-")) {
                        MyBibleHTMLVisitor sfn = footnotes.remove(singleFootnote);
                        if (sfn == null)
                            throw new RuntimeException();
                        footnotes.put(singleFootnoteMarker, sfn);
                        String verse = vb.toString();
                        vb.setLength(0);
                        vb.append(verse.replace("<f>" + singleFootnote + "</f>", "<f>" + singleFootnoteMarker + "</f>"));
                    }
                }
                for (Map.Entry<String, MyBibleHTMLVisitor> fn : footnotes.entrySet()) {
                    footnotesTable.insert(info.bookNumber, cn, vn, cn, vn, fn.getKey(), fn.getValue().getResult());
                }
                versesTable.insert(info.bookNumber, cn, vn, vb.toString().trim());
            }
        }
        if (prologs != null) {
            MyBibleHTMLVisitor v = new MyBibleHTMLVisitor(unsupportedFeatures, "in introduction");
            prologs.accept(v);
            introductionsTable.insert(info.bookNumber, v.getResult());
        }
    }
    if (bibleIntro != null) {
        introductionsTable.insert(0, bibleIntro);
    } else if (introProlog != null) {
        MyBibleHTMLVisitor v = new MyBibleHTMLVisitor(unsupportedFeatures, "in introduction");
        introProlog.accept(v);
        introductionsTable.insert(0, v.getResult());
    }
    if (!unsupportedFeatures.isEmpty()) {
        System.out.println("WARNING: Skipped unsupported features: " + unsupportedFeatures);
    }
    db.commit();
    db.close();
    if (cdb != null) {
        cdb.commit();
        cdb.close();
    }
}
Also used : ExtraAttributePriority(biblemulticonverter.data.FormattedText.ExtraAttributePriority) VirtualVerse(biblemulticonverter.data.VirtualVerse) AbstractHTMLVisitor(biblemulticonverter.format.AbstractHTMLVisitor) Visitor(biblemulticonverter.data.FormattedText.Visitor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SqlJetDb(org.tmatesoft.sqljet.core.table.SqlJetDb) Properties(java.util.Properties) LinkedHashMap(java.util.LinkedHashMap) BookID(biblemulticonverter.data.BookID) MetadataBook(biblemulticonverter.data.MetadataBook) Book(biblemulticonverter.data.Book) ISqlJetTable(org.tmatesoft.sqljet.core.table.ISqlJetTable) HashSet(java.util.HashSet) MetadataBook(biblemulticonverter.data.MetadataBook) Chapter(biblemulticonverter.data.Chapter) FormattedText(biblemulticonverter.data.FormattedText) FileInputStream(java.io.FileInputStream) File(java.io.File) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) EnumMap(java.util.EnumMap) VirtualVerse(biblemulticonverter.data.VirtualVerse) Verse(biblemulticonverter.data.Verse)

Example 15 with Visitor

use of biblemulticonverter.data.FormattedText.Visitor in project BibleMultiConverter by schierlm.

the class ZefaniaXMLRoundtrip method parseContent.

private boolean parseContent(Visitor<RuntimeException> visitor, List<Object> contentList, Map<BookID, String> abbrMap) throws IOException {
    boolean contentFound = false;
    for (Object n : contentList) {
        if (n instanceof String) {
            String value = normalize((String) n, false);
            visitor.visitText(value);
            contentFound |= value.trim().length() > 0;
        } else if (n instanceof DIV || n instanceof NOTE) {
            NOTE note;
            Visitor<RuntimeException> v;
            if (n instanceof DIV) {
                note = ((DIV) n).getNOTE();
                if (note.getContent().size() == 0)
                    continue;
                v = visitor.visitExtraAttribute(ExtraAttributePriority.KEEP_CONTENT, "zefania", "footnote-source", "div").visitFootnote();
            } else {
                note = (NOTE) n;
                if (note.getContent().size() == 0)
                    continue;
                v = visitor.visitFootnote();
            }
            boolean subContentFound = parseContent(v, note.getContent(), abbrMap);
            if (!subContentFound)
                visitEmptyMarker(v);
            contentFound = true;
        } else if (n instanceof BR) {
            BR br = (BR) n;
            Visitor<RuntimeException> v = visitor;
            int count = 1;
            if (br.getCount() != null) {
                count = br.getCount().intValue();
                v = visitor.visitExtraAttribute(ExtraAttributePriority.KEEP_CONTENT, "zefania", "newline-group", br.getCount() + "--" + br.getArt().value());
            }
            if (count < 1 || count > 10)
                throw new RuntimeException();
            for (int ii = 0; ii < count; ii++) {
                switch(br.getArt()) {
                    case X_NL:
                        v.visitLineBreak(LineBreakKind.NEWLINE);
                        break;
                    case X_P:
                        v.visitLineBreak(LineBreakKind.PARAGRAPH);
                        break;
                    default:
                        throw new RuntimeException(br.getArt().toString());
                }
            }
            contentFound = true;
        } else if (n instanceof XREF) {
            XREF xref = (XREF) n;
            Visitor<RuntimeException> footnoteVisitor = visitor.visitExtraAttribute(ExtraAttributePriority.KEEP_CONTENT, "zefania", "footnote-source", "inner-xref").visitFootnote();
            boolean first = true;
            for (String mscope : xref.getMscope().split(" ")) {
                Matcher m = Utils.compilePattern("([0-9]+);([0-9]+)(-[0-9]+)?;([0-9]+)(-[0-9]+)?").matcher(mscope);
                if (!m.matches())
                    throw new IOException(mscope);
                BookID bookID = BookID.fromZefId(Integer.parseInt(m.group(1)));
                int chapter = Integer.parseInt(m.group(2)), endChapter = chapter;
                if (m.group(3) != null)
                    endChapter = Integer.parseInt(m.group(3).substring(1));
                String verse = m.group(4);
                if (verse.equals("0"))
                    verse = "1//G";
                String endVerse = m.group(5);
                if (endVerse == null)
                    endVerse = verse;
                else
                    endVerse = endVerse.substring(1);
                if (endVerse.equals("0"))
                    endVerse = "1//G";
                String abbr = abbrMap.get(bookID);
                if (abbr == null)
                    abbr = bookID.getOsisID();
                if (first)
                    first = false;
                else
                    footnoteVisitor.visitText(" ");
                if (chapter == endChapter && !verse.equals("1//G") && !endVerse.equals("1//G") && Integer.parseInt(verse) > Integer.parseInt(endVerse)) {
                    String tmp = verse;
                    verse = endVerse;
                    endVerse = tmp;
                }
                footnoteVisitor.visitCrossReference(abbr, bookID, chapter, verse, endChapter, endVerse).visitText(abbr + " " + chapter + ":" + verse);
            }
            contentFound = true;
        } else if (n instanceof JAXBElement<?>) {
            String name = ((JAXBElement<?>) n).getName().toString();
            Object nn = ((JAXBElement<?>) n).getValue();
            if (name.equals("STYLE") && nn instanceof STYLE) {
                String css = ((STYLE) nn).getCss();
                String id = ((STYLE) nn).getId();
                if (id != null && css != null)
                    throw new IOException(id + "/" + css);
                if (css != null && css.startsWith("display:block;")) {
                    // not really a formatting instruction, but more some
                    // clever way of indentation
                    List<Object> content = ((STYLE) nn).getContent();
                    Visitor<RuntimeException> contentVisitor = visitor.visitCSSFormatting(css);
                    boolean subContentFound = parseContent(contentVisitor, content, abbrMap);
                    if (!subContentFound)
                        visitEmptyMarker(contentVisitor);
                } else {
                    FormattingInstructionKind kind;
                    if (id != null && id.equals("cl:divineName")) {
                        kind = FormattingInstructionKind.DIVINE_NAME;
                    } else if (css == null) {
                        throw new IOException(id);
                    } else if (css.contains("italic")) {
                        kind = FormattingInstructionKind.ITALIC;
                    } else if (css.contains("bold")) {
                        kind = FormattingInstructionKind.BOLD;
                    } else if (css.equalsIgnoreCase("color:#FF0000")) {
                        kind = FormattingInstructionKind.WORDS_OF_JESUS;
                    } else if (css.equals("color:blue")) {
                        kind = FormattingInstructionKind.LINK;
                    } else if (css.equals("color:#00CC33;font-size:8pt;vertical-align:super") || css.equals("font-size:small")) {
                        kind = FormattingInstructionKind.SUPERSCRIPT;
                    } else {
                        throw new IOException(css);
                    }
                    List<Object> content = ((STYLE) nn).getContent();
                    Visitor<RuntimeException> contentVisitor = visitor.visitFormattingInstruction(kind);
                    if (css != null && !kind.getCss().equals(css)) {
                        contentVisitor = contentVisitor.visitCSSFormatting(css);
                    }
                    if (content.size() == 0) {
                        visitEmptyMarker(contentVisitor);
                    } else {
                        boolean subContentFound = parseContent(contentVisitor, content, abbrMap);
                        if (!subContentFound)
                            visitEmptyMarker(contentVisitor);
                    }
                }
            } else if ((name.equals("gr") || name.equals("GRAM")) && nn instanceof GRAM) {
                GRAM gram = (GRAM) nn;
                Visitor<RuntimeException> strongVisitor = visitor;
                if (!name.equals("GRAM")) {
                    strongVisitor = strongVisitor.visitExtraAttribute(ExtraAttributePriority.KEEP_CONTENT, "zefania", "gram-tag", name);
                }
                if (gram.getStr() == null && gram.getRmac() == null)
                    throw new IOException();
                int[] strongs = null;
                if (gram.getStr() != null) {
                    String strong = gram.getStr().trim().replaceAll(" ++", " ");
                    if (strong.length() == 0)
                        strong = "0";
                    if (strong.equals("?"))
                        strong = "99111";
                    if (strong.startsWith("G")) {
                        strongVisitor = strongVisitor.visitExtraAttribute(ExtraAttributePriority.KEEP_CONTENT, "zefania", "strong-prefix", "G");
                        strong = strong.replace("G", "");
                    } else if (strong.startsWith("H")) {
                        strongVisitor = strongVisitor.visitExtraAttribute(ExtraAttributePriority.KEEP_CONTENT, "zefania", "strong-prefix", "H");
                        strong = strong.replace("H", "");
                    }
                    if (!strong.matches("[0-9]+( [0-9]+)*"))
                        throw new IOException(strong);
                    String[] tmpStrongs = strong.split(" ");
                    strongs = new int[tmpStrongs.length];
                    for (int i = 0; i < tmpStrongs.length; i++) {
                        strongs[i] = Integer.parseInt(tmpStrongs[i]);
                    }
                }
                String[] rmacs = null;
                if (gram.getRmac() != null) {
                    String rmac = gram.getRmac();
                    rmacs = rmac.split(" ");
                }
                strongVisitor = strongVisitor.visitGrammarInformation(strongs, rmacs, null);
                if (!parseContent(strongVisitor, gram.getContent(), abbrMap)) {
                    visitEmptyMarker(strongVisitor);
                }
            } else {
                throw new IOException(name + "/" + nn.getClass().toString());
            }
            contentFound = true;
        } else {
            throw new IOException(n.getClass().toString());
        }
    }
    return contentFound;
}
Also used : Visitor(biblemulticonverter.data.FormattedText.Visitor) Matcher(java.util.regex.Matcher) STYLE(biblemulticonverter.schema.zef2005.STYLE) FormattingInstructionKind(biblemulticonverter.data.FormattedText.FormattingInstructionKind) IOException(java.io.IOException) JAXBElement(javax.xml.bind.JAXBElement) DIV(biblemulticonverter.schema.zef2005.DIV) BR(biblemulticonverter.schema.zef2005.BR) XREF(biblemulticonverter.schema.zef2005.XREF) BookID(biblemulticonverter.data.BookID) NOTE(biblemulticonverter.schema.zef2005.NOTE) GRAM(biblemulticonverter.schema.zef2005.GRAM)

Aggregations

Visitor (biblemulticonverter.data.FormattedText.Visitor)16 Book (biblemulticonverter.data.Book)8 BookID (biblemulticonverter.data.BookID)8 FormattedText (biblemulticonverter.data.FormattedText)8 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)8 Chapter (biblemulticonverter.data.Chapter)7 FormattingInstructionKind (biblemulticonverter.data.FormattedText.FormattingInstructionKind)6 MetadataBook (biblemulticonverter.data.MetadataBook)6 JAXBElement (javax.xml.bind.JAXBElement)6 Verse (biblemulticonverter.data.Verse)5 List (java.util.List)5 ExtraAttributePriority (biblemulticonverter.data.FormattedText.ExtraAttributePriority)4 Headline (biblemulticonverter.data.FormattedText.Headline)4 LineBreakKind (biblemulticonverter.data.FormattedText.LineBreakKind)4 RawHTMLMode (biblemulticonverter.data.FormattedText.RawHTMLMode)4 VirtualVerse (biblemulticonverter.data.VirtualVerse)4 File (java.io.File)4 Matcher (java.util.regex.Matcher)4 Bible (biblemulticonverter.data.Bible)3