Search in sources :

Example 1 with CHAPTER

use of biblemulticonverter.schema.haggai.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 2 with CHAPTER

use of biblemulticonverter.schema.haggai.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)

Aggregations

Book (biblemulticonverter.data.Book)2 Chapter (biblemulticonverter.data.Chapter)2 Headline (biblemulticonverter.data.FormattedText.Headline)2 MetadataBook (biblemulticonverter.data.MetadataBook)2 Verse (biblemulticonverter.data.Verse)2 VirtualVerse (biblemulticonverter.data.VirtualVerse)2 BIBLEBOOK (biblemulticonverter.schema.haggai.BIBLEBOOK)2 CAPTION (biblemulticonverter.schema.haggai.CAPTION)2 CHAPTER (biblemulticonverter.schema.haggai.CHAPTER)2 Bible (biblemulticonverter.data.Bible)1 BookID (biblemulticonverter.data.BookID)1 INFORMATION (biblemulticonverter.schema.haggai.INFORMATION)1 ObjectFactory (biblemulticonverter.schema.haggai.ObjectFactory)1 PROLOG (biblemulticonverter.schema.haggai.PROLOG)1 STYLE (biblemulticonverter.schema.haggai.STYLE)1 VERSE (biblemulticonverter.schema.haggai.VERSE)1 XMLBIBLE (biblemulticonverter.schema.haggai.XMLBIBLE)1 IOException (java.io.IOException)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1