Search in sources :

Example 51 with Book

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

the class Compact method doExport.

protected void doExport(Bible bible, Writer w) throws IOException {
    w.write(MAGIC + bible.getName() + "\n");
    for (Book book : bible.getBooks()) {
        w.write("=" + book.getAbbr() + "\t" + book.getId().getOsisID() + "\t" + book.getShortName() + "\t" + book.getLongName() + "\n");
        boolean firstChapter = true;
        int verseNum;
        for (Chapter ch : book.getChapters()) {
            if (firstChapter)
                firstChapter = false;
            else
                w.write('+');
            if (ch.getProlog() != null) {
                w.write("0 ");
                ch.getProlog().accept(new CompactVisitor(w, false));
                w.write('\n');
            }
            verseNum = 1;
            for (Verse v : ch.getVerses()) {
                if (v.getNumber().equals("" + verseNum)) {
                    StringWriter sw = new StringWriter();
                    v.accept(new CompactVisitor(sw, false));
                    String s = sw.toString();
                    if (!s.matches("[A-Za-z].*"))
                        w.write(' ');
                    w.write(s);
                } else {
                    w.write(v.getNumber() + " ");
                    v.accept(new CompactVisitor(w, false));
                    Matcher m = Utils.compilePattern("([0-9]+).*").matcher(v.getNumber());
                    if (!m.matches()) {
                        throw new IllegalStateException();
                    }
                    verseNum = Integer.parseInt(m.group(1));
                }
                verseNum++;
                w.write('\n');
            }
        }
    }
}
Also used : StringWriter(java.io.StringWriter) Matcher(java.util.regex.Matcher) Book(biblemulticonverter.data.Book) Chapter(biblemulticonverter.data.Chapter) Verse(biblemulticonverter.data.Verse)

Example 52 with Book

use of biblemulticonverter.data.Book 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 53 with Book

use of biblemulticonverter.data.Book 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 54 with Book

use of biblemulticonverter.data.Book 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 55 with Book

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

Aggregations

Book (biblemulticonverter.data.Book)67 Chapter (biblemulticonverter.data.Chapter)60 Verse (biblemulticonverter.data.Verse)57 FormattedText (biblemulticonverter.data.FormattedText)27 MetadataBook (biblemulticonverter.data.MetadataBook)25 VirtualVerse (biblemulticonverter.data.VirtualVerse)24 BookID (biblemulticonverter.data.BookID)22 ArrayList (java.util.ArrayList)22 File (java.io.File)20 Bible (biblemulticonverter.data.Bible)19 Headline (biblemulticonverter.data.FormattedText.Headline)18 BufferedWriter (java.io.BufferedWriter)16 FileOutputStream (java.io.FileOutputStream)15 IOException (java.io.IOException)14 OutputStreamWriter (java.io.OutputStreamWriter)13 HashMap (java.util.HashMap)10 HashSet (java.util.HashSet)10 Visitor (biblemulticonverter.data.FormattedText.Visitor)9 FileInputStream (java.io.FileInputStream)9 EnumMap (java.util.EnumMap)9