Search in sources :

Example 51 with Chapter

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

the class Compact method doImport.

protected Bible doImport(BufferedReader br) throws IOException {
    String line = br.readLine();
    if (!line.startsWith(MAGIC))
        throw new IOException("Invalid header line: " + line);
    Bible result = new Bible(line.substring(MAGIC.length()));
    Book currentBook = null;
    Chapter currentChapter = null;
    int nextVerseNum = -1;
    while ((line = br.readLine()) != null) {
        if (line.length() == 0)
            throw new IOException("Empty line");
        char start = line.charAt(0);
        while (start == '+') {
            if (currentChapter == null)
                currentBook.getChapters().add(new Chapter());
            currentChapter = null;
            nextVerseNum = -1;
            line = line.substring(1);
            if (line.length() == 0)
                throw new IOException("Empty line");
            start = line.charAt(0);
        }
        if (start == '=') {
            String[] fields = line.substring(1).split("\t", -1);
            if (fields.length != 4)
                throw new IOException("Unsupported chapter heading: " + line);
            currentBook = new Book(fields[0], BookID.fromOsisId(fields[1]), fields[2], fields[3]);
            result.getBooks().add(currentBook);
            currentChapter = null;
            nextVerseNum = -1;
        } else if (start == '0' && line.startsWith("0 ")) {
            if (currentChapter == null) {
                currentChapter = new Chapter();
                currentBook.getChapters().add(currentChapter);
                nextVerseNum = 1;
            }
            if (currentChapter.getProlog() != null)
                throw new IOException("More than one prolog for the same chapter");
            FormattedText prolog = new FormattedText();
            parseCompact(prolog.getAppendVisitor(), line.substring(2));
            currentChapter.setProlog(prolog);
            prolog.finished();
        } else if (start >= '1' && start <= '9' && line.contains(" ")) {
            if (currentChapter == null) {
                currentChapter = new Chapter();
                currentBook.getChapters().add(currentChapter);
            }
            int pos = line.indexOf(' ');
            Verse v = new Verse(line.substring(0, pos));
            parseCompact(v.getAppendVisitor(), line.substring(pos + 1));
            v.finished();
            currentChapter.getVerses().add(v);
            Matcher m = Utils.compilePattern("([0-9]+).*").matcher(v.getNumber());
            if (!m.matches()) {
                throw new IllegalStateException();
            }
            nextVerseNum = Integer.parseInt(m.group(1)) + 1;
        } else if (start == ' ' || (start >= 'A' && start <= 'Z') || (start >= 'a' && start <= 'z')) {
            if (currentChapter == null) {
                currentChapter = new Chapter();
                currentBook.getChapters().add(currentChapter);
                nextVerseNum = 1;
            }
            if (start == ' ')
                line = line.substring(1);
            Verse v = new Verse("" + nextVerseNum);
            parseCompact(v.getAppendVisitor(), line);
            v.finished();
            currentChapter.getVerses().add(v);
            nextVerseNum++;
        } else {
            throw new IOException("Unsupported line: " + line);
        }
    }
    return result;
}
Also used : Matcher(java.util.regex.Matcher) Book(biblemulticonverter.data.Book) Bible(biblemulticonverter.data.Bible) Chapter(biblemulticonverter.data.Chapter) IOException(java.io.IOException) FormattedText(biblemulticonverter.data.FormattedText) Verse(biblemulticonverter.data.Verse)

Example 52 with Chapter

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

the class Diffable method doImport.

protected Bible doImport(BufferedReader br) throws IOException {
    String line = br.readLine();
    if (!line.startsWith(MAGIC))
        throw new IOException("Invalid header line: " + line);
    Bible result = new Bible(line.substring(MAGIC.length()));
    Map<String, Book> bookMap = new HashMap<String, Book>();
    while ((line = br.readLine()) != null) {
        line = line.trim();
        if (line.length() == 0 || line.startsWith("#"))
            continue;
        String[] parts = line.split(" ", 3);
        if (parts.length != 3)
            throw new IOException("Not enough fields: " + line);
        try {
            if (parts[1].equals("=")) {
                String[] fields = parts[2].split("\t");
                if (fields.length != 3)
                    throw new IOException("Malformed header line (not 3 fields): " + parts[3]);
                BookID id = BookID.fromOsisId(fields[0]);
                if (id == null)
                    throw new IOException("Unknown book ID: " + fields[0]);
                Book newBook = new Book(parts[0], id, fields[1], fields[2]);
                result.getBooks().add(newBook);
                Book oldBook = bookMap.get(parts[0]);
                if (oldBook != null) {
                    newBook.getChapters().addAll(oldBook.getChapters());
                    result.getBooks().remove(oldBook);
                }
                bookMap.put(parts[0], newBook);
                continue;
            }
            Book book = bookMap.get(parts[0]);
            if (book == null)
                throw new IOException("Unknown book prefix (header line missing?): " + parts[0]);
            if (parts[1].equals("->")) {
                if (!parts[2].equals("-")) {
                    new StrippedDiffable().renameBookInXref(result, parts[0], parts[2], false);
                    Book destBook = bookMap.get(parts[2]);
                    if (destBook == null)
                        throw new IOException("Unknown destination book (header line missing?): " + parts[2]);
                    destBook.getChapters().addAll(book.getChapters());
                }
                result.getBooks().remove(book);
                bookMap.remove(parts[0]);
                continue;
            } else if (parts[1].equals("^^")) {
                Book destBook = bookMap.get(parts[2]);
                if (destBook == null)
                    throw new IOException("Unknown destination book (header line missing?): " + parts[2]);
                result.getBooks().remove(book);
                result.getBooks().add(result.getBooks().indexOf(destBook), book);
                continue;
            }
            int chapterNumber;
            String verse;
            if (parts[1].contains(":")) {
                String[] chapVerse = parts[1].split(":", 2);
                chapterNumber = Integer.parseInt(chapVerse[0]);
                verse = chapVerse[1];
            } else {
                chapterNumber = Integer.parseInt(parts[1]);
                verse = null;
            }
            while (book.getChapters().size() < chapterNumber) {
                book.getChapters().add(new Chapter());
            }
            Chapter chapter = book.getChapters().get(chapterNumber - 1);
            FormattedText target;
            if (verse == null) {
                if (chapter.getProlog() == null)
                    chapter.setProlog(new FormattedText());
                target = chapter.getProlog();
            } else {
                int idx = chapter.getVerseIndex(verse);
                if (idx == -1) {
                    Verse v = new Verse(verse);
                    chapter.getVerses().add(v);
                    target = v;
                } else {
                    target = chapter.getVerses().get(idx);
                }
            }
            parseDiffable(target.getAppendVisitor(), parts[2]);
        } catch (Exception ex) {
            throw new IOException("Error while parsing line: " + line, ex);
        }
    }
    for (Book book : result.getBooks()) {
        for (Chapter chapter : book.getChapters()) {
            if (chapter.getProlog() != null)
                chapter.getProlog().finished();
            for (Verse v : chapter.getVerses()) v.finished();
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) Bible(biblemulticonverter.data.Bible) Chapter(biblemulticonverter.data.Chapter) IOException(java.io.IOException) FormattedText(biblemulticonverter.data.FormattedText) IOException(java.io.IOException) BookID(biblemulticonverter.data.BookID) Book(biblemulticonverter.data.Book) Verse(biblemulticonverter.data.Verse)

Example 53 with Chapter

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

the class HaggaiXML method createXMLBible.

protected XMLBIBLE createXMLBible(Bible bible) throws Exception {
    ObjectFactory of = new ObjectFactory();
    XMLBIBLE doc = of.createXMLBIBLE();
    doc.setBiblename(bible.getName());
    doc.setINFORMATION(new JAXBElement<INFORMATION>(new QName("INFORMATION"), INFORMATION.class, of.createINFORMATION()));
    MetadataBook metadata = bible.getMetadataBook();
    if (metadata != null) {
        for (String key : metadata.getKeys()) {
            String value = metadata.getValue(key);
            if (value.equals("-empty-"))
                value = "";
            if (key.equals(MetadataBookKey.status.toString())) {
                doc.setStatus(ModuleStatus.fromValue(value));
            } else if (key.equals(MetadataBookKey.version.toString())) {
                doc.setVersion(value);
            } else if (key.equals(MetadataBookKey.revision.toString())) {
                doc.setRevision(new BigInteger(value));
            } else if (!key.contains("@")) {
                Pattern regex = INFORMATION_FIELDS.get(MetadataBookKey.valueOf(key));
                if (regex != null && regex.matcher(value).matches())
                    doc.getINFORMATION().getValue().getTitleOrCreatorOrDescription().add(new JAXBElement<String>(new QName(key), String.class, value));
            }
        }
    }
    for (Book bk : bible.getBooks()) {
        if (bk.getId().equals(BookID.METADATA))
            continue;
        if (bk.getId().getZefID() <= 0) {
            System.out.println("WARNING: Unable to export book " + bk.getAbbr());
            continue;
        }
        BIBLEBOOK bb = of.createBIBLEBOOK();
        bb.setBnumber(BigInteger.valueOf(bk.getId().getZefID()));
        bb.setBsname(bk.getShortName());
        bb.setBname(bk.getLongName());
        int cnumber = 0;
        for (Chapter ccc : bk.getChapters()) {
            cnumber++;
            if (ccc.getVerses().size() == 0)
                continue;
            CHAPTER cc = of.createCHAPTER();
            cc.setCnumber(BigInteger.valueOf(cnumber));
            bb.getCAPTIONOrPROLOGOrREMARK().add(new JAXBElement<CHAPTER>(new QName("CHAPTER"), CHAPTER.class, cc));
            if (ccc.getProlog() != null) {
                PROLOG prolog = of.createPROLOG();
                ccc.getProlog().accept(new CreateContentVisitor(of, prolog.getContent(), null));
                cc.getCAPTIONOrPARAGRAPHOrVERSE().add(new JAXBElement<PROLOG>(new QName("PROLOG"), PROLOG.class, prolog));
            }
            for (VirtualVerse vv : ccc.createVirtualVerses()) {
                for (Headline h : vv.getHeadlines()) {
                    CAPTION caption = of.createCAPTION();
                    h.accept(new CreateContentVisitor(of, caption.getContent(), null));
                    cc.getCAPTIONOrPARAGRAPHOrVERSE().add(new JAXBElement<CAPTION>(new QName("CAPTION"), CAPTION.class, caption));
                }
                VERSE vers = of.createVERSE();
                vers.setVnumber(BigInteger.valueOf(vv.getNumber()));
                for (Verse v : vv.getVerses()) {
                    if (!v.getNumber().equals("" + vv.getNumber())) {
                        STYLE verseNum = of.createSTYLE();
                        verseNum.setFs(TStyleFix.BOLD);
                        verseNum.getContent().add("(" + v.getNumber() + ")");
                        vers.getContent().add(new JAXBElement<STYLE>(new QName("STYLE"), STYLE.class, verseNum));
                        vers.getContent().add(" ");
                    }
                    v.accept(new CreateContentVisitor(of, vers.getContent(), vers));
                }
                cc.getCAPTIONOrPARAGRAPHOrVERSE().add(new JAXBElement<VERSE>(new QName("VERSE"), VERSE.class, vers));
            }
        }
        doc.getBIBLEBOOK().add(new JAXBElement<BIBLEBOOK>(new QName("BIBLEBOOK"), BIBLEBOOK.class, bb));
    }
    return doc;
}
Also used : VirtualVerse(biblemulticonverter.data.VirtualVerse) VERSE(biblemulticonverter.schema.haggai.VERSE) ObjectFactory(biblemulticonverter.schema.haggai.ObjectFactory) Book(biblemulticonverter.data.Book) MetadataBook(biblemulticonverter.data.MetadataBook) Headline(biblemulticonverter.data.FormattedText.Headline) PROLOG(biblemulticonverter.schema.haggai.PROLOG) MetadataBook(biblemulticonverter.data.MetadataBook) Pattern(java.util.regex.Pattern) XMLBIBLE(biblemulticonverter.schema.haggai.XMLBIBLE) INFORMATION(biblemulticonverter.schema.haggai.INFORMATION) QName(javax.xml.namespace.QName) Chapter(biblemulticonverter.data.Chapter) STYLE(biblemulticonverter.schema.haggai.STYLE) BIBLEBOOK(biblemulticonverter.schema.haggai.BIBLEBOOK) CHAPTER(biblemulticonverter.schema.haggai.CHAPTER) BigInteger(java.math.BigInteger) CAPTION(biblemulticonverter.schema.haggai.CAPTION) VirtualVerse(biblemulticonverter.data.VirtualVerse) Verse(biblemulticonverter.data.Verse)

Example 54 with Chapter

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

the class HaggaiXML method parseBible.

protected Bible parseBible(XMLBIBLE doc) throws Exception {
    Bible result = new Bible(doc.getBiblename());
    MetadataBook metadata = new MetadataBook();
    if (doc.getStatus() != null) {
        metadata.setValue(MetadataBookKey.status, doc.getStatus().value());
    }
    if (doc.getVersion() != null) {
        metadata.setValue(MetadataBookKey.version, doc.getVersion());
    }
    if (doc.getRevision() != null) {
        metadata.setValue(MetadataBookKey.revision, doc.getRevision().toString());
    }
    for (JAXBElement<?> elem : doc.getINFORMATION().getValue().getTitleOrCreatorOrDescription()) {
        if (elem.getValue() == null)
            continue;
        String value = normalize(elem.getValue().toString(), true).trim();
        if (value.length() != 0)
            metadata.setValue(elem.getName().getLocalPart(), value);
    }
    if (metadata.getKeys().size() > 0) {
        metadata.finished();
        result.getBooks().add(metadata.getBook());
    }
    Set<String> abbrs = new HashSet<String>();
    Set<String> shortnames = new HashSet<String>();
    Map<BookID, String> abbrMap = new EnumMap<BookID, String>(BookID.class);
    List<JAXBElement<BIBLEBOOK>> nl = doc.getBIBLEBOOK();
    for (JAXBElement<BIBLEBOOK> ee : nl) {
        BIBLEBOOK e = ee.getValue();
        String shortname = e.getBsname();
        int number = e.getBnumber().intValue();
        BookID bookID;
        try {
            bookID = BookID.fromZefId(number);
        } catch (IllegalArgumentException ex) {
            continue;
        }
        if (shortname == null || shortname.length() == 0)
            shortname = "_" + bookID.getOsisID();
        String abbr = shortname.replaceAll("[^A-Z0-9a-zäöü]++", "");
        if (abbr.length() == 0 || Character.isLowerCase(abbr.charAt(0)))
            abbr = "X" + abbr;
        if (abbr.length() == 1)
            abbr += "x";
        if (abbrs.contains(abbr)) {
            for (int i = 2; i < 100; i++) {
                if (!abbrs.contains(abbr + i)) {
                    abbr = abbr + i;
                    break;
                }
            }
        }
        abbrs.add(abbr);
        abbrMap.put(bookID, abbr);
    }
    abbrs.clear();
    EnumMap<BookID, Book> existingBooks = new EnumMap<BookID, Book>(BookID.class);
    for (JAXBElement<BIBLEBOOK> ee : nl) {
        BIBLEBOOK e = ee.getValue();
        String shortname = e.getBsname();
        String longname = e.getBname();
        int number = e.getBnumber().intValue();
        BookID bookID;
        try {
            bookID = BookID.fromZefId(number);
        } catch (IllegalArgumentException ex) {
            System.out.println("WARNING: Skipping book with unknown id " + number);
            continue;
        }
        if (shortname == null || shortname.length() == 0)
            shortname = "_" + bookID.getOsisID();
        if (longname == null || longname.length() == 0)
            longname = "_" + bookID.getEnglishName();
        else
            longname = longname.replaceAll("  ++", " ").trim();
        String abbr = shortname.replaceAll("[^A-Z0-9a-zäöü]++", "");
        if (abbr.length() == 0 || Character.isLowerCase(abbr.charAt(0)))
            abbr = "X" + abbr;
        if (abbr.length() == 1)
            abbr += "x";
        if (abbrs.contains(abbr)) {
            for (int i = 2; i < 100; i++) {
                if (!abbrs.contains(abbr + i)) {
                    abbr = abbr + i;
                    break;
                }
            }
        }
        abbrs.add(abbr);
        if (shortname.equals("Gen") && longname.equals("Genesis") && bookID == BookID.BOOK_Exod) {
            System.out.println("WARNING: Book number " + bookID.getZefID() + " has name " + longname);
            shortname = "Exo";
            longname = "Exodus";
        }
        if (shortname.equals("1Chr") && longname.equals("2 Chronicles")) {
            System.out.println("WARNING: Book name 2 Chronicles has short name 1Chr");
            shortname = "2Chr";
        }
        if (shortnames.contains(shortname)) {
            System.out.println("WARNING: Duplicate short name " + shortname);
            for (int i = 2; i < 100; i++) {
                if (!shortnames.contains(shortname + i)) {
                    shortname = shortname + i;
                    break;
                }
            }
        }
        shortnames.add(shortname);
        Book book = existingBooks.get(bookID);
        if (book == null) {
            book = new Book(abbr, bookID, shortname, longname);
            existingBooks.put(bookID, book);
            result.getBooks().add(book);
        }
        List<Headline> headlineBuffer = new ArrayList<Headline>();
        for (JAXBElement<?> cpr : e.getCAPTIONOrPROLOGOrREMARK()) {
            if (cpr.getValue() instanceof CAPTION) {
                CAPTION caption = (CAPTION) cpr.getValue();
                Headline h = new Headline(9);
                if (parseContent(h.getAppendVisitor(), caption.getContent(), abbrMap)) {
                    h.trimWhitespace();
                    h.finished();
                    headlineBuffer.add(h);
                }
            } else if (cpr.getValue() instanceof CHAPTER) {
                CHAPTER e2 = (CHAPTER) cpr.getValue();
                int chapterNumber = e2.getCnumber().intValue();
                while (book.getChapters().size() < chapterNumber) book.getChapters().add(new Chapter());
                Chapter chapter = book.getChapters().get(chapterNumber - 1);
                int existingVerses = chapter.getVerses().size();
                for (Object e3 : e2.getCAPTIONOrPARAGRAPHOrVERSE()) {
                    parseChapterObject(e3, chapter, abbrMap, headlineBuffer);
                }
                for (Verse v : chapter.getVerses()) {
                    if (existingVerses > 0) {
                        existingVerses--;
                        continue;
                    }
                    v.finished();
                }
            } else {
                throw new IOException(cpr.getValue().getClass().toString());
            }
        }
    }
    return result;
}
Also used : Bible(biblemulticonverter.data.Bible) ArrayList(java.util.ArrayList) BookID(biblemulticonverter.data.BookID) Book(biblemulticonverter.data.Book) MetadataBook(biblemulticonverter.data.MetadataBook) Headline(biblemulticonverter.data.FormattedText.Headline) EnumMap(java.util.EnumMap) HashSet(java.util.HashSet) MetadataBook(biblemulticonverter.data.MetadataBook) Chapter(biblemulticonverter.data.Chapter) JAXBElement(javax.xml.bind.JAXBElement) IOException(java.io.IOException) BIBLEBOOK(biblemulticonverter.schema.haggai.BIBLEBOOK) CHAPTER(biblemulticonverter.schema.haggai.CHAPTER) CAPTION(biblemulticonverter.schema.haggai.CAPTION) VirtualVerse(biblemulticonverter.data.VirtualVerse) Verse(biblemulticonverter.data.Verse)

Example 55 with Chapter

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

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