Search in sources :

Example 1 with CAPTION

use of biblemulticonverter.schema.zef2005.CAPTION in project BibleMultiConverter by schierlm.

the class ZefaniaXMLMyBible method doExport.

@Override
public void doExport(Bible bible, String... exportArgs) throws Exception {
    new StrippedDiffable().mergeIntroductionPrologs(bible);
    final ObjectFactory f = new ObjectFactory();
    XMLBIBLE doc = f.createXMLBIBLE();
    doc.setBiblename(bible.getName());
    doc.setType(EnumModtyp.X_BIBLE);
    BigInteger revision = null;
    MetadataBook metadata = bible.getMetadataBook();
    if (metadata != null) {
        for (MetadataBookKey key : Arrays.asList(MetadataBookKey.revision, MetadataBookKey.version, MetadataBookKey.date, MetadataBookKey.title)) {
            String digits = metadata.getValue(key);
            if (digits == null)
                continue;
            digits = digits.replaceAll("[^0-9]+", "");
            if (!digits.isEmpty()) {
                revision = new BigInteger(digits);
                break;
            }
        }
    }
    if (revision == null) {
        String digits = bible.getName().replaceAll("[^0-9]+", "");
        if (!digits.isEmpty()) {
            revision = new BigInteger(digits);
        }
    }
    if (revision != null) {
        doc.setRevision(revision);
    }
    doc.setINFORMATION(f.createINFORMATION());
    List<DIV> prologs = new ArrayList<DIV>();
    for (Book bk : bible.getBooks()) {
        if (bk.getId().equals(BookID.METADATA))
            continue;
        int bsnumber = bk.getId().getZefID();
        final BIBLEBOOK book = f.createBIBLEBOOK();
        book.setBnumber(BigInteger.valueOf(bsnumber));
        book.setBname(bk.getShortName());
        book.setBsname(bk.getAbbr());
        doc.getBIBLEBOOK().add(book);
        int cnumber = 0;
        for (Chapter cch : bk.getChapters()) {
            cnumber++;
            if (cch.getProlog() != null) {
                DIV xx = f.createDIV();
                prologs.add(xx);
                NOTE xxx = f.createNOTE();
                xx.setNOTE(xxx);
                xxx.setType("x-studynote");
                NOTE prolog = xxx;
                DIV vers = f.createDIV();
                prolog.getContent().add("<p>");
                prolog.getContent().add(vers);
                prolog.getContent().add("</p>");
                vers.setNOTE(f.createNOTE());
                final List<List<Object>> targetStack = new ArrayList<List<Object>>();
                targetStack.add(vers.getNOTE().getContent());
                cch.getProlog().accept(new Visitor<IOException>() {

                    @Override
                    public Visitor<IOException> visitHeadline(int depth) throws IOException {
                        if (depth > 6)
                            depth = 6;
                        STYLE s = f.createSTYLE();
                        s.setCss("-zef-dummy: true");
                        targetStack.get(0).add("<h" + depth + ">");
                        targetStack.get(0).add(new JAXBElement<STYLE>(new QName("STYLE"), STYLE.class, s));
                        targetStack.get(0).add("</h" + depth + ">");
                        targetStack.add(0, s.getContent());
                        return this;
                    }

                    @Override
                    public void visitVerseSeparator() throws IOException {
                        STYLE x = f.createSTYLE();
                        x.setCss("color:gray");
                        x.getContent().add("/");
                        targetStack.get(0).add(new JAXBElement<STYLE>(new QName("STYLE"), STYLE.class, x));
                    }

                    @Override
                    public void visitText(String text) throws IOException {
                        targetStack.get(0).add(text);
                    }

                    @Override
                    public Visitor<IOException> visitFormattingInstruction(FormattedText.FormattingInstructionKind kind) throws IOException {
                        String startTag, endTag;
                        if (kind.getHtmlTag() != null) {
                            startTag = "<" + kind.getHtmlTag() + ">";
                            endTag = "</" + kind.getHtmlTag() + ">";
                        } else {
                            startTag = "<span style=\"" + kind.getCss() + "\">";
                            endTag = "</span>";
                        }
                        STYLE s = f.createSTYLE();
                        s.setCss("-zef-dummy: true");
                        targetStack.get(0).add(startTag);
                        targetStack.get(0).add(new JAXBElement<STYLE>(new QName("STYLE"), STYLE.class, s));
                        targetStack.get(0).add(endTag);
                        targetStack.add(0, s.getContent());
                        return this;
                    }

                    @Override
                    public Visitor<IOException> visitFootnote() throws IOException {
                        System.out.println("WARNING: Footnotes in prolog are not supported");
                        return null;
                    }

                    @Override
                    public Visitor<IOException> visitCrossReference(String bookAbbr, BookID book, int firstChapter, String firstVerse, int lastChapter, String lastVerse) throws IOException {
                        System.out.println("WARNING: Cross references in prologs are not supported");
                        STYLE s = f.createSTYLE();
                        s.setCss("-zef-dummy: true");
                        targetStack.get(0).add(new JAXBElement<STYLE>(new QName("STYLE"), STYLE.class, s));
                        targetStack.add(0, s.getContent());
                        return this;
                    }

                    @Override
                    public Visitor<IOException> visitVariationText(String[] variations) throws IOException {
                        throw new RuntimeException("Variations not supported");
                    }

                    @Override
                    public void visitLineBreak(LineBreakKind kind) throws IOException {
                        BR br = f.createBR();
                        br.setArt(kind == LineBreakKind.PARAGRAPH ? EnumBreak.X_P : EnumBreak.X_NL);
                        targetStack.get(0).add(" ");
                        targetStack.get(0).add(kind == LineBreakKind.PARAGRAPH ? "<p>" : "<br>");
                        targetStack.get(0).add(br);
                    }

                    @Override
                    public Visitor<IOException> visitGrammarInformation(int[] strongs, String[] rmac, int[] sourceIndices) throws IOException {
                        throw new RuntimeException("Grammar tags in prologs not supported");
                    }

                    @Override
                    public FormattedText.Visitor<IOException> visitDictionaryEntry(String dictionary, String entry) throws IOException {
                        throw new RuntimeException("Dictionary entries in prologs not supported");
                    }

                    @Override
                    public void visitRawHTML(RawHTMLMode mode, String raw) throws IOException {
                        throw new RuntimeException("Raw HTML in prologs not supported");
                    }

                    @Override
                    public Visitor<IOException> visitCSSFormatting(String css) throws IOException {
                        STYLE s = f.createSTYLE();
                        s.setCss("-zef-dummy: true");
                        targetStack.get(0).add("<span style=\"" + css + "\">");
                        targetStack.get(0).add(new JAXBElement<STYLE>(new QName("STYLE"), STYLE.class, s));
                        targetStack.get(0).add("</span>");
                        targetStack.add(0, s.getContent());
                        return this;
                    }

                    @Override
                    public int visitElementTypes(String elementTypes) throws IOException {
                        return 0;
                    }

                    @Override
                    public Visitor<IOException> visitExtraAttribute(ExtraAttributePriority prio, String category, String key, String value) throws IOException {
                        if (prio == ExtraAttributePriority.KEEP_CONTENT)
                            return visitCSSFormatting("-zef-extra-attribute-" + category + "-" + key + ": " + value);
                        else if (prio == ExtraAttributePriority.SKIP)
                            return null;
                        throw new RuntimeException("Extra attributes not supported");
                    }

                    @Override
                    public void visitStart() throws IOException {
                    }

                    @Override
                    public boolean visitEnd() throws IOException {
                        targetStack.remove(0);
                        return false;
                    }
                });
                if (targetStack.size() != 0)
                    throw new RuntimeException();
            }
            if (cch.getVerses().size() == 0)
                continue;
            CHAPTER chapter = f.createCHAPTER();
            chapter.setCnumber(BigInteger.valueOf(cnumber));
            book.getCHAPTER().add(chapter);
            for (VirtualVerse vv : cch.createVirtualVerses()) {
                for (Headline h : vv.getHeadlines()) {
                    CAPTION caption = f.createCAPTION();
                    EnumCaptionType[] types = new EnumCaptionType[] { null, EnumCaptionType.X_H_1, EnumCaptionType.X_H_2, EnumCaptionType.X_H_3, EnumCaptionType.X_H_4, EnumCaptionType.X_H_5, EnumCaptionType.X_H_6, EnumCaptionType.X_H_6, EnumCaptionType.X_H_6, EnumCaptionType.X_H_6 };
                    caption.setType(types[h.getDepth()]);
                    caption.setVref(BigInteger.valueOf(vv.getNumber()));
                    final StringBuilder sb = new StringBuilder();
                    h.accept(new FormattedText.VisitorAdapter<RuntimeException>(null) {

                        @Override
                        protected void beforeVisit() throws RuntimeException {
                            throw new IllegalStateException();
                        }

                        @Override
                        public Visitor<RuntimeException> visitFormattingInstruction(FormattingInstructionKind kind) throws RuntimeException {
                            System.out.println("WARNING: Formatting instructions in captions are not supported (stripped)");
                            return this;
                        }

                        @Override
                        public Visitor<RuntimeException> visitFootnote() throws RuntimeException {
                            System.out.println("WARNING: Footnotes in captions are not supported (stripped)");
                            return null;
                        }

                        @Override
                        public Visitor<RuntimeException> visitCSSFormatting(String css) throws RuntimeException {
                            System.out.println("WARNING: CSS Formatting in captions are not supported (stripped)");
                            return this;
                        }

                        @Override
                        public Visitor<RuntimeException> visitExtraAttribute(ExtraAttributePriority prio, String category, String key, String value) throws RuntimeException {
                            return prio.handleVisitor(category, this);
                        }

                        public void visitText(String text) throws RuntimeException {
                            sb.append(text);
                        }
                    });
                    caption.getContent().add(sb.toString());
                    chapter.getPROLOGOrCAPTIONOrVERS().add(caption);
                }
                VERS vers = f.createVERS();
                vers.setVnumber(BigInteger.valueOf(vv.getNumber()));
                for (DIV prolog : prologs) {
                    vers.getContent().add(prolog);
                }
                prologs.clear();
                chapter.getPROLOGOrCAPTIONOrVERS().add(vers);
                boolean first = true;
                for (Verse v : vv.getVerses()) {
                    if (!first || !v.getNumber().equals("" + vv.getNumber())) {
                        STYLE x = f.createSTYLE();
                        x.setCss("font-weight: bold");
                        x.getContent().add("(" + v.getNumber() + ")");
                        vers.getContent().add(new JAXBElement<STYLE>(new QName("STYLE"), STYLE.class, x));
                        vers.getContent().add(" ");
                    }
                    first = false;
                    final List<List<Object>> targetStack = new ArrayList<List<Object>>();
                    targetStack.add(vers.getContent());
                    v.accept(new FormattedText.Visitor<IOException>() {

                        @Override
                        public void visitVerseSeparator() throws IOException {
                            STYLE x = f.createSTYLE();
                            x.setCss("color:gray");
                            x.getContent().add("/");
                            targetStack.get(0).add(new JAXBElement<STYLE>(new QName("STYLE"), STYLE.class, x));
                        }

                        @Override
                        public void visitText(String text) throws IOException {
                            targetStack.get(0).add(text);
                        }

                        @Override
                        public FormattedText.Visitor<IOException> visitFormattingInstruction(biblemulticonverter.data.FormattedText.FormattingInstructionKind kind) throws IOException {
                            STYLE x = f.createSTYLE();
                            x.setCss(kind.getCss());
                            targetStack.get(0).add(new JAXBElement<STYLE>(new QName("STYLE"), STYLE.class, x));
                            targetStack.add(0, x.getContent());
                            return this;
                        }

                        @Override
                        public Visitor<IOException> visitFootnote() throws IOException {
                            DIV x = f.createDIV();
                            targetStack.get(0).add(x);
                            NOTE n = f.createNOTE();
                            x.setNOTE(n);
                            n.setType("x-studynote");
                            final List<List<Object>> footnoteStack = new ArrayList<List<Object>>();
                            footnoteStack.add(n.getContent());
                            return new Visitor<IOException>() {

                                @Override
                                public void visitStart() throws IOException {
                                }

                                @Override
                                public void visitVerseSeparator() throws IOException {
                                    STYLE x = f.createSTYLE();
                                    x.setCss("color:gray");
                                    x.getContent().add("/");
                                    footnoteStack.get(0).add(new JAXBElement<STYLE>(new QName("STYLE"), STYLE.class, x));
                                }

                                @Override
                                public void visitText(String text) throws IOException {
                                    footnoteStack.get(0).add(text);
                                }

                                @Override
                                public void visitLineBreak(LineBreakKind kind) throws IOException {
                                    BR br = f.createBR();
                                    br.setArt(kind == LineBreakKind.PARAGRAPH ? EnumBreak.X_P : EnumBreak.X_NL);
                                    footnoteStack.get(0).add(" ");
                                    footnoteStack.get(0).add(br);
                                }

                                @Override
                                public Visitor<IOException> visitFormattingInstruction(FormattedText.FormattingInstructionKind kind) throws IOException {
                                    String startTag, endTag;
                                    if (kind.getHtmlTag() != null) {
                                        startTag = "<" + kind.getHtmlTag() + ">";
                                        endTag = "</" + kind.getHtmlTag() + ">";
                                    } else {
                                        startTag = "<span style=\"" + kind.getCss() + "\">";
                                        endTag = "</span>";
                                    }
                                    STYLE s = f.createSTYLE();
                                    s.setCss("-zef-dummy: true");
                                    footnoteStack.get(0).add(startTag);
                                    footnoteStack.get(0).add(new JAXBElement<STYLE>(new QName("STYLE"), STYLE.class, s));
                                    footnoteStack.get(0).add(endTag);
                                    footnoteStack.add(0, s.getContent());
                                    return this;
                                }

                                @Override
                                public Visitor<IOException> visitFootnote() throws IOException {
                                    throw new RuntimeException("Footnotes in footnotes are not supported");
                                }

                                @Override
                                public Visitor<IOException> visitGrammarInformation(int[] strongs, String[] rmac, int[] sourceIndices) throws IOException {
                                    GRAM gram = f.createGRAM();
                                    if (strongs != null) {
                                        StringBuilder entryBuilder = new StringBuilder();
                                        for (int i = 0; i < strongs.length; i++) {
                                            entryBuilder.append((i > 0 ? " " : "") + strongs[i]);
                                        }
                                        String entry = entryBuilder.toString();
                                        gram.setStr(entry);
                                    }
                                    if (rmac != null) {
                                        StringBuilder entryBuilder = new StringBuilder();
                                        for (int i = 0; i < rmac.length; i++) {
                                            if (i > 0)
                                                entryBuilder.append(' ');
                                            entryBuilder.append(rmac[i]);
                                        }
                                        gram.setRmac(entryBuilder.toString());
                                    }
                                    footnoteStack.get(0).add(new JAXBElement<GRAM>(new QName("gr"), GRAM.class, gram));
                                    footnoteStack.add(0, gram.getContent());
                                    return this;
                                }

                                @Override
                                public FormattedText.Visitor<IOException> visitDictionaryEntry(String dictionary, String entry) throws IOException {
                                    GRAM gram = f.createGRAM();
                                    gram.setStr(entry);
                                    footnoteStack.get(0).add(new JAXBElement<GRAM>(new QName("gr"), GRAM.class, gram));
                                    footnoteStack.add(0, gram.getContent());
                                    return this;
                                }

                                @Override
                                public void visitRawHTML(RawHTMLMode mode, String raw) throws IOException {
                                    if (mode != RawHTMLMode.ONLINE)
                                        footnoteStack.get(0).add(raw);
                                }

                                @Override
                                public Visitor<IOException> visitVariationText(String[] variations) throws IOException {
                                    throw new RuntimeException("Variations not supported");
                                }

                                @Override
                                public FormattedText.Visitor<IOException> visitCrossReference(String bookAbbr, BookID book, int firstChapter, String firstVerse, int lastChapter, String lastVerse) throws IOException {
                                    STYLE s = f.createSTYLE();
                                    s.setCss("-zef-dummy: true");
                                    int bookID = book.getZefID();
                                    String mscope, xmscope;
                                    try {
                                        int start = firstVerse.equals("^") ? 1 : Integer.parseInt(firstVerse.replaceAll("[a-zG]|[,/][0-9]*", ""));
                                        int end;
                                        if (firstChapter == lastChapter && !lastVerse.equals("$")) {
                                            end = Integer.parseInt(lastVerse.replaceAll("[a-z]|[,/][0-9]*", ""));
                                        } else {
                                            end = -1;
                                        }
                                        mscope = bookID + "," + firstChapter + "," + start + "," + end;
                                        xmscope = bookID + ";" + firstChapter + ";" + start + "-" + end;
                                    } catch (NumberFormatException ex) {
                                        ex.printStackTrace();
                                        mscope = bookID + ",1,1,999";
                                        xmscope = bookID + ";1;1-999";
                                    }
                                    if (footnoteStack.size() == 1) {
                                        List<Object> outerList = targetStack.get(0);
                                        XREF xref = new XREF();
                                        xref.setMscope(xmscope);
                                        outerList.add(outerList.size() - 1, xref);
                                    }
                                    footnoteStack.get(0).add("<a href=\"mybible:content=location&amp;locations=" + mscope + "\">");
                                    footnoteStack.get(0).add(new JAXBElement<STYLE>(new QName("STYLE"), STYLE.class, s));
                                    footnoteStack.get(0).add("</a>");
                                    footnoteStack.add(0, s.getContent());
                                    return this;
                                }

                                public boolean visitEnd() throws IOException {
                                    footnoteStack.remove(0);
                                    return false;
                                }

                                @Override
                                public int visitElementTypes(String elementTypes) throws IOException {
                                    return 0;
                                }

                                @Override
                                public Visitor<IOException> visitHeadline(int depth) throws IOException {
                                    throw new RuntimeException("Headlines in footnotes not supported");
                                }

                                @Override
                                public Visitor<IOException> visitCSSFormatting(String css) throws IOException {
                                    STYLE s = f.createSTYLE();
                                    s.setCss("-zef-dummy: true");
                                    footnoteStack.get(0).add("<span style=\"" + css + "\">");
                                    footnoteStack.get(0).add(new JAXBElement<STYLE>(new QName("STYLE"), STYLE.class, s));
                                    footnoteStack.get(0).add("</span>");
                                    footnoteStack.add(s.getContent());
                                    return this;
                                }

                                @Override
                                public Visitor<IOException> visitExtraAttribute(ExtraAttributePriority prio, String category, String key, String value) throws IOException {
                                    System.out.println("WARNING: Extra attributes not supported");
                                    Visitor<IOException> result = prio.handleVisitor(category, this);
                                    if (result != null)
                                        footnoteStack.add(0, footnoteStack.get(0));
                                    return result;
                                }
                            };
                        }

                        @Override
                        public FormattedText.Visitor<IOException> visitCrossReference(String bookAbbr, BookID book, int firstChapter, String firstVerse, int lastChapter, String lastVerse) throws IOException {
                            throw new RuntimeException("Xref outside of footnotes not supported!");
                        }

                        @Override
                        public void visitLineBreak(LineBreakKind kind) throws IOException {
                            BR br = f.createBR();
                            br.setArt(kind == LineBreakKind.PARAGRAPH ? EnumBreak.X_P : EnumBreak.X_NL);
                            targetStack.get(0).add(" ");
                            targetStack.get(0).add(br);
                        }

                        @Override
                        public Visitor<IOException> visitGrammarInformation(int[] strongs, String[] rmac, int[] sourceIndices) throws IOException {
                            GRAM gram = f.createGRAM();
                            if (strongs != null) {
                                StringBuilder entryBuilder = new StringBuilder();
                                for (int i = 0; i < strongs.length; i++) {
                                    entryBuilder.append((i > 0 ? " " : "") + strongs[i]);
                                }
                                String entry = entryBuilder.toString();
                                gram.setStr(entry);
                            }
                            if (rmac != null) {
                                StringBuilder entryBuilder = new StringBuilder();
                                for (int i = 0; i < rmac.length; i++) {
                                    if (i > 0)
                                        entryBuilder.append(' ');
                                    entryBuilder.append(rmac[i]);
                                }
                                gram.setRmac(entryBuilder.toString());
                            }
                            targetStack.get(0).add(new JAXBElement<GRAM>(new QName("gr"), GRAM.class, gram));
                            targetStack.add(0, gram.getContent());
                            return this;
                        }

                        @Override
                        public FormattedText.Visitor<IOException> visitDictionaryEntry(String dictionary, String entry) throws IOException {
                            GRAM gram = f.createGRAM();
                            gram.setStr(entry);
                            targetStack.get(0).add(new JAXBElement<GRAM>(new QName("gr"), GRAM.class, gram));
                            targetStack.add(0, gram.getContent());
                            return this;
                        }

                        @Override
                        public void visitRawHTML(RawHTMLMode mode, String raw) throws IOException {
                            throw new RuntimeException("Raw HTML is not supported");
                        }

                        @Override
                        public Visitor<IOException> visitVariationText(String[] variations) throws IOException {
                            throw new RuntimeException("Variations not supported");
                        }

                        @Override
                        public boolean visitEnd() throws IOException {
                            targetStack.remove(0);
                            return false;
                        }

                        @Override
                        public int visitElementTypes(String elementTypes) throws IOException {
                            return 0;
                        }

                        @Override
                        public Visitor<IOException> visitHeadline(int depth) throws IOException {
                            throw new RuntimeException("Headline in virtual verse is impossible");
                        }

                        @Override
                        public void visitStart() throws IOException {
                        }

                        @Override
                        public Visitor<IOException> visitCSSFormatting(String css) throws IOException {
                            STYLE x = f.createSTYLE();
                            x.setCss(css);
                            targetStack.get(0).add(new JAXBElement<STYLE>(new QName("STYLE"), STYLE.class, x));
                            targetStack.add(0, x.getContent());
                            return this;
                        }

                        @Override
                        public Visitor<IOException> visitExtraAttribute(ExtraAttributePriority prio, String category, String key, String value) throws IOException {
                            System.out.println("WARNING: Extra attributes not supported");
                            Visitor<IOException> result = prio.handleVisitor(category, this);
                            if (result != null)
                                targetStack.add(0, targetStack.get(0));
                            return result;
                        }
                    });
                    if (targetStack.size() != 0)
                        throw new RuntimeException();
                }
            }
        }
        if (book.getCHAPTER().size() == 0) {
            doc.getBIBLEBOOK().remove(book);
        }
    }
    final Document docc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    JAXBContext.newInstance(ObjectFactory.class.getPackage().getName()).createMarshaller().marshal(doc, docc);
    docc.getDocumentElement().setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
    docc.getDocumentElement().setAttribute("xsi:noNamespaceSchemaLocation", "zef2005.xsd");
    docc.normalize();
    maskWhitespaceNodes(docc.getDocumentElement());
    try (FileOutputStream fos = new FileOutputStream(exportArgs[0])) {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        transformer.transform(new DOMSource(docc), new StreamResult(fos));
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) VirtualVerse(biblemulticonverter.data.VirtualVerse) ArrayList(java.util.ArrayList) RawHTMLMode(biblemulticonverter.data.FormattedText.RawHTMLMode) Document(org.w3c.dom.Document) ObjectFactory(biblemulticonverter.schema.zef2005.ObjectFactory) BookID(biblemulticonverter.data.BookID) Book(biblemulticonverter.data.Book) MetadataBook(biblemulticonverter.data.MetadataBook) Headline(biblemulticonverter.data.FormattedText.Headline) VERS(biblemulticonverter.schema.zef2005.VERS) List(java.util.List) ArrayList(java.util.ArrayList) MetadataBook(biblemulticonverter.data.MetadataBook) StreamResult(javax.xml.transform.stream.StreamResult) STYLE(biblemulticonverter.schema.zef2005.STYLE) JAXBElement(javax.xml.bind.JAXBElement) FormattedText(biblemulticonverter.data.FormattedText) BIBLEBOOK(biblemulticonverter.schema.zef2005.BIBLEBOOK) LineBreakKind(biblemulticonverter.data.FormattedText.LineBreakKind) XREF(biblemulticonverter.schema.zef2005.XREF) FileOutputStream(java.io.FileOutputStream) BigInteger(java.math.BigInteger) FormattingInstructionKind(biblemulticonverter.data.FormattedText.FormattingInstructionKind) ExtraAttributePriority(biblemulticonverter.data.FormattedText.ExtraAttributePriority) Transformer(javax.xml.transform.Transformer) Visitor(biblemulticonverter.data.FormattedText.Visitor) BR(biblemulticonverter.schema.zef2005.BR) NOTE(biblemulticonverter.schema.zef2005.NOTE) MetadataBookKey(biblemulticonverter.data.MetadataBook.MetadataBookKey) FormattingInstructionKind(biblemulticonverter.data.FormattedText.FormattingInstructionKind) XMLBIBLE(biblemulticonverter.schema.zef2005.XMLBIBLE) QName(javax.xml.namespace.QName) Chapter(biblemulticonverter.data.Chapter) FormattingInstructionKind(biblemulticonverter.data.FormattedText.FormattingInstructionKind) IOException(java.io.IOException) DIV(biblemulticonverter.schema.zef2005.DIV) EnumCaptionType(biblemulticonverter.schema.zef2005.EnumCaptionType) CHAPTER(biblemulticonverter.schema.zef2005.CHAPTER) GRAM(biblemulticonverter.schema.zef2005.GRAM) CAPTION(biblemulticonverter.schema.zef2005.CAPTION) VirtualVerse(biblemulticonverter.data.VirtualVerse) Verse(biblemulticonverter.data.Verse)

Example 2 with CAPTION

use of biblemulticonverter.schema.zef2005.CAPTION in project BibleMultiConverter by schierlm.

the class ZefaniaXMLRoundtrip 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().getTitleOrCreatorOrDescription()) {
        if (elem.getValue() == null)
            continue;
        String value = normalize(elem.getValue().toString(), true).trim();
        if (value.length() == 0)
            value = "-empty-";
        metadata.setValue(elem.getName().getLocalPart(), value);
    }
    metadata.finished();
    if (metadata.getKeys().size() > 0)
        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<BIBLEBOOK> nl = doc.getBIBLEBOOK();
    for (BIBLEBOOK e : nl) {
        String shortname = e.getBsname();
        int number = e.getBnumber().intValue();
        BookID bookID = BookID.fromZefId(number);
        if (shortname == null)
            shortname = "_" + bookID.getOsisID();
        else if (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();
    for (BIBLEBOOK e : nl) {
        String shortname = e.getBsname();
        String longname = e.getBname();
        int number = e.getBnumber().intValue();
        BookID bookID = BookID.fromZefId(number);
        if (shortname == null)
            shortname = "_" + bookID.getOsisID();
        else if (shortname.length() == 0)
            shortname = "_" + bookID.getOsisID() + "[[]]";
        if (longname == null)
            longname = "_" + bookID.getEnglishName();
        else if (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[[Gen]]";
            longname = "Exodus[[Genesis]]";
        }
        if (shortname.equals("1Chr") && longname.equals("2 Chronicles")) {
            System.out.println("WARNING: Book name 2 Chronicles has short name 1Chr");
            shortname = "2Chr[[1Chr]]";
        }
        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 = shortname + i + "[[" + shortname + "]]";
                    break;
                }
            }
        }
        shortnames.add(shortname);
        Book book = new Book(abbr, bookID, shortname, longname);
        int lastvref = -1;
        List<Headline> headlineBuffer = new ArrayList<Headline>();
        for (CHAPTER e2 : e.getCHAPTER()) {
            int chapterNumber = e2.getCnumber().intValue();
            while (book.getChapters().size() < chapterNumber) book.getChapters().add(new Chapter());
            Chapter chapter = book.getChapters().get(chapterNumber - 1);
            for (Object e3 : e2.getPROLOGOrCAPTIONOrVERS()) {
                if (e3 instanceof CAPTION) {
                    CAPTION caption = (CAPTION) e3;
                    if (lastvref != -1 && lastvref != caption.getVref().intValue())
                        throw new IOException();
                    lastvref = caption.getVref().intValue();
                    int level;
                    if (caption.getType() == null) {
                        level = 9;
                    } else {
                        switch(caption.getType()) {
                            case X_H_1:
                                level = 1;
                                break;
                            case X_H_2:
                                level = 2;
                                break;
                            case X_H_3:
                                level = 3;
                                break;
                            case X_H_4:
                                level = 4;
                                break;
                            case X_H_5:
                                level = 5;
                                break;
                            case X_H_6:
                                level = 6;
                                break;
                            default:
                                throw new IOException();
                        }
                    }
                    Headline h = new Headline(level);
                    headlineBuffer.add(h);
                    if (!parseContent(h.getAppendVisitor(), caption.getContent(), abbrMap)) {
                        visitEmptyMarker(h.getAppendVisitor());
                    } else {
                        h.trimWhitespace();
                    }
                    h.finished();
                } else if (e3 instanceof REMARK) {
                    REMARK remark = (REMARK) e3;
                    int vref = remark.getVref().intValue();
                    int idx = chapter.getVerseIndex("" + vref);
                    if (idx == -1)
                        throw new IOException(vref + ":" + remark.getContent());
                    Verse v = chapter.getVerses().get(idx);
                    if (remark.getContent().size() != 1)
                        throw new IOException();
                    String remarkText = normalize((String) remark.getContent().get(0), true).trim();
                    v.getAppendVisitor().visitExtraAttribute(ExtraAttributePriority.KEEP_CONTENT, "zefania", "footnote-source", "remark").visitFootnote().visitText(remarkText);
                } else if (e3 instanceof XREF) {
                    XREF xref = (XREF) e3;
                    int vref = xref.getVref().intValue();
                    int idx = chapter.getVerseIndex("" + vref);
                    if (idx == -1)
                        throw new IOException(vref + ":" + xref.getMscope());
                    Verse v = chapter.getVerses().get(idx);
                    Visitor<RuntimeException> footnoteVisitor = v.getAppendVisitor().visitExtraAttribute(ExtraAttributePriority.KEEP_CONTENT, "zefania", "footnote-source", "outer-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 xrefBookID = BookID.fromZefId(Integer.parseInt(m.group(1)));
                        int xrefChapter = Integer.parseInt(m.group(2)), endChapter = xrefChapter;
                        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 xrefAbbr = abbrMap.get(xrefBookID);
                        if (xrefAbbr == null)
                            xrefAbbr = xrefBookID.getOsisID();
                        if (first)
                            first = false;
                        else
                            footnoteVisitor.visitText(" ");
                        if (xrefChapter == endChapter && !verse.equals("1//G") && !endVerse.equals("1//G") && Integer.parseInt(verse) > Integer.parseInt(endVerse)) {
                            String tmp = verse;
                            verse = endVerse;
                            endVerse = tmp;
                        }
                        footnoteVisitor.visitCrossReference(xrefAbbr, xrefBookID, xrefChapter, verse, endChapter, endVerse).visitText(xrefAbbr + " " + xrefChapter + ":" + verse);
                    }
                } else if (e3 instanceof PROLOG) {
                    PROLOG prolog = (PROLOG) e3;
                    if (prolog.getVref().intValue() != 1)
                        throw new IOException("" + prolog.getVref());
                    if (chapter.getProlog() != null)
                        throw new IOException("More than one prolog found");
                    FormattedText prologText = new FormattedText();
                    if (parseContent(prologText.getAppendVisitor(), prolog.getContent(), abbrMap)) {
                        prologText.trimWhitespace();
                        prologText.finished();
                        chapter.setProlog(prologText);
                    }
                } else if (e3 instanceof VERS) {
                    VERS vers = (VERS) e3;
                    int vnumber = vers.getVnumber().intValue();
                    if (lastvref != -1) {
                        if (lastvref != vnumber)
                            throw new IOException(lastvref + " != " + vnumber);
                        lastvref = -1;
                    }
                    Verse verse = new Verse("" + vnumber);
                    Visitor<RuntimeException> visitor = verse.getAppendVisitor();
                    boolean contentFound = false;
                    if (headlineBuffer.size() > 0) {
                        for (Headline h : headlineBuffer) {
                            h.accept(visitor.visitHeadline(h.getDepth()));
                        }
                        headlineBuffer.clear();
                        contentFound = true;
                    }
                    contentFound |= parseContent(visitor, vers.getContent(), abbrMap);
                    if (!contentFound) {
                        visitEmptyMarker(visitor);
                    }
                    verse.trimWhitespace();
                    chapter.getVerses().add(verse);
                } else {
                    throw new IOException(e3.getClass().toString());
                }
            }
            for (Verse v : chapter.getVerses()) v.finished();
        }
        result.getBooks().add(book);
    }
    return result;
}
Also used : Matcher(java.util.regex.Matcher) 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) VERS(biblemulticonverter.schema.zef2005.VERS) PROLOG(biblemulticonverter.schema.zef2005.PROLOG) EnumMap(java.util.EnumMap) REMARK(biblemulticonverter.schema.zef2005.REMARK) HashSet(java.util.HashSet) MetadataBook(biblemulticonverter.data.MetadataBook) Chapter(biblemulticonverter.data.Chapter) IOException(java.io.IOException) FormattedText(biblemulticonverter.data.FormattedText) BIBLEBOOK(biblemulticonverter.schema.zef2005.BIBLEBOOK) XREF(biblemulticonverter.schema.zef2005.XREF) CHAPTER(biblemulticonverter.schema.zef2005.CHAPTER) CAPTION(biblemulticonverter.schema.zef2005.CAPTION) VirtualVerse(biblemulticonverter.data.VirtualVerse) Verse(biblemulticonverter.data.Verse)

Example 3 with CAPTION

use of biblemulticonverter.schema.zef2005.CAPTION in project BibleMultiConverter by schierlm.

the class ZefaniaXMLRoundtrip method createXMLBible.

protected XMLBIBLE createXMLBible(Bible bible) throws Exception {
    ObjectFactory of = new ObjectFactory();
    XMLBIBLE doc = of.createXMLBIBLE();
    doc.setBiblename(bible.getName());
    doc.setType(EnumModtyp.X_BIBLE);
    doc.setINFORMATION(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(EnumStatus.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 (Arrays.asList(INFORMATION_KEYS).contains(key)) {
                doc.getINFORMATION().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;
        }
        String shortname = removeRoundtripMarker(bk.getShortName());
        String longname = removeRoundtripMarker(bk.getLongName());
        BookID bookID = bk.getId();
        BIBLEBOOK bb = of.createBIBLEBOOK();
        bb.setBnumber(BigInteger.valueOf(bookID.getZefID()));
        if (!shortname.equals("_" + bookID.getOsisID()))
            bb.setBsname(shortname);
        if (!longname.equals("_" + bookID.getEnglishName()))
            bb.setBname(longname);
        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.getCHAPTER().add(cc);
            if (ccc.getProlog() != null) {
                PROLOG prolog = of.createPROLOG();
                prolog.setVref(BigInteger.ONE);
                ccc.getProlog().accept(new CreateContentVisitor(of, prolog.getContent(), null, 0, null));
                cc.getPROLOGOrCAPTIONOrVERS().add(prolog);
            }
            for (VirtualVerse vv : ccc.createVirtualVerses()) {
                for (Headline h : vv.getHeadlines()) {
                    CAPTION caption = of.createCAPTION();
                    caption.setVref(BigInteger.valueOf(vv.getNumber()));
                    h.accept(new CreateContentVisitor(of, caption.getContent(), null, 0, null));
                    EnumCaptionType[] types = new EnumCaptionType[] { null, EnumCaptionType.X_H_1, EnumCaptionType.X_H_2, EnumCaptionType.X_H_3, EnumCaptionType.X_H_4, EnumCaptionType.X_H_5, EnumCaptionType.X_H_6, null, null, null };
                    caption.setType(types[h.getDepth()]);
                    cc.getPROLOGOrCAPTIONOrVERS().add(caption);
                }
                List<Object> remarksAndXrefs = new ArrayList<Object>();
                VERS vers = of.createVERS();
                vers.setVnumber(BigInteger.valueOf(vv.getNumber()));
                for (Verse v : vv.getVerses()) {
                    if (!v.getNumber().equals("" + vv.getNumber())) {
                        STYLE verseNum = of.createSTYLE();
                        verseNum.setCss("font-weight: 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(), remarksAndXrefs, vv.getNumber(), null));
                }
                cc.getPROLOGOrCAPTIONOrVERS().add(vers);
                cc.getPROLOGOrCAPTIONOrVERS().addAll(remarksAndXrefs);
            }
        }
        doc.getBIBLEBOOK().add(bb);
    }
    return doc;
}
Also used : VirtualVerse(biblemulticonverter.data.VirtualVerse) ArrayList(java.util.ArrayList) ObjectFactory(biblemulticonverter.schema.zef2005.ObjectFactory) BookID(biblemulticonverter.data.BookID) Book(biblemulticonverter.data.Book) MetadataBook(biblemulticonverter.data.MetadataBook) Headline(biblemulticonverter.data.FormattedText.Headline) VERS(biblemulticonverter.schema.zef2005.VERS) PROLOG(biblemulticonverter.schema.zef2005.PROLOG) MetadataBook(biblemulticonverter.data.MetadataBook) XMLBIBLE(biblemulticonverter.schema.zef2005.XMLBIBLE) QName(javax.xml.namespace.QName) Chapter(biblemulticonverter.data.Chapter) STYLE(biblemulticonverter.schema.zef2005.STYLE) JAXBElement(javax.xml.bind.JAXBElement) BIBLEBOOK(biblemulticonverter.schema.zef2005.BIBLEBOOK) EnumCaptionType(biblemulticonverter.schema.zef2005.EnumCaptionType) CHAPTER(biblemulticonverter.schema.zef2005.CHAPTER) BigInteger(java.math.BigInteger) CAPTION(biblemulticonverter.schema.zef2005.CAPTION) VirtualVerse(biblemulticonverter.data.VirtualVerse) Verse(biblemulticonverter.data.Verse)

Example 4 with CAPTION

use of biblemulticonverter.schema.zef2005.CAPTION in project BibleMultiConverter by schierlm.

the class ZefaniaXML method createXMLBible.

protected XMLBIBLE createXMLBible(Bible bible) throws Exception {
    ObjectFactory of = new ObjectFactory();
    XMLBIBLE doc = of.createXMLBIBLE();
    doc.setBiblename(bible.getName());
    doc.setType(EnumModtyp.X_BIBLE);
    doc.setINFORMATION(of.createINFORMATION());
    MetadataBook metadata = bible.getMetadataBook();
    if (metadata != null) {
        for (String key : metadata.getKeys()) {
            String value = metadata.getValue(key);
            if (key.equals(MetadataBookKey.status.toString())) {
                doc.setStatus(EnumStatus.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().getTitleOrCreatorOrDescription().add(new JAXBElement<String>(new QName(key), String.class, value));
            }
        }
    }
    doc.getINFORMATION().getTitleOrCreatorOrDescription().add(new JAXBElement<String>(new QName("format"), String.class, "Zefania XML Bible Markup Language"));
    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.getCHAPTER().add(cc);
            if (ccc.getProlog() != null) {
                PROLOG prolog = of.createPROLOG();
                prolog.setVref(BigInteger.ONE);
                ccc.getProlog().accept(new CreateContentVisitor(of, prolog.getContent(), null));
                cc.getPROLOGOrCAPTIONOrVERS().add(prolog);
            }
            for (VirtualVerse vv : ccc.createVirtualVerses()) {
                for (Headline h : vv.getHeadlines()) {
                    CAPTION caption = of.createCAPTION();
                    caption.setVref(BigInteger.valueOf(vv.getNumber()));
                    h.accept(new CreateContentVisitor(of, caption.getContent(), null));
                    EnumCaptionType[] types = new EnumCaptionType[] { null, EnumCaptionType.X_H_1, EnumCaptionType.X_H_2, EnumCaptionType.X_H_3, EnumCaptionType.X_H_4, EnumCaptionType.X_H_5, EnumCaptionType.X_H_6, null, null, null };
                    caption.setType(types[h.getDepth()]);
                    cc.getPROLOGOrCAPTIONOrVERS().add(caption);
                }
                VERS vers = of.createVERS();
                vers.setVnumber(BigInteger.valueOf(vv.getNumber()));
                for (Verse v : vv.getVerses()) {
                    if (!v.getNumber().equals("" + vv.getNumber())) {
                        STYLE verseNum = of.createSTYLE();
                        verseNum.setCss("font-weight: 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.getPROLOGOrCAPTIONOrVERS().add(vers);
            }
        }
        doc.getBIBLEBOOK().add(bb);
    }
    return doc;
}
Also used : MetadataBook(biblemulticonverter.data.MetadataBook) Pattern(java.util.regex.Pattern) VirtualVerse(biblemulticonverter.data.VirtualVerse) XMLBIBLE(biblemulticonverter.schema.zef2005.XMLBIBLE) QName(javax.xml.namespace.QName) Chapter(biblemulticonverter.data.Chapter) STYLE(biblemulticonverter.schema.zef2005.STYLE) BIBLEBOOK(biblemulticonverter.schema.zef2005.BIBLEBOOK) ObjectFactory(biblemulticonverter.schema.zef2005.ObjectFactory) EnumCaptionType(biblemulticonverter.schema.zef2005.EnumCaptionType) CHAPTER(biblemulticonverter.schema.zef2005.CHAPTER) Book(biblemulticonverter.data.Book) MetadataBook(biblemulticonverter.data.MetadataBook) Headline(biblemulticonverter.data.FormattedText.Headline) VERS(biblemulticonverter.schema.zef2005.VERS) BigInteger(java.math.BigInteger) PROLOG(biblemulticonverter.schema.zef2005.PROLOG) CAPTION(biblemulticonverter.schema.zef2005.CAPTION) VirtualVerse(biblemulticonverter.data.VirtualVerse) Verse(biblemulticonverter.data.Verse)

Example 5 with CAPTION

use of biblemulticonverter.schema.zef2005.CAPTION in project BibleMultiConverter by schierlm.

the class ZefaniaXML 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().getTitleOrCreatorOrDescription()) {
        if (elem.getValue() == null)
            continue;
        String value = normalize(elem.getValue().toString(), true).trim();
        if (value.length() != 0)
            metadata.setValue(elem.getName().getLocalPart(), value);
    }
    metadata.finished();
    if (metadata.getKeys().size() > 0)
        result.getBooks().add(metadata.getBook());
    Set<String> abbrs = new HashSet<String>();
    Set<String> shortnames = new HashSet<String>();
    Set<String> longnames = new HashSet<String>();
    Map<BookID, String> abbrMap = new EnumMap<BookID, String>(BookID.class);
    List<BIBLEBOOK> nl = doc.getBIBLEBOOK();
    for (BIBLEBOOK e : nl) {
        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 (BIBLEBOOK e : nl) {
        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);
        if (longnames.contains(longname)) {
            System.out.println("WARNING: Duplicate long name " + shortname);
            for (int i = 2; i < 100; i++) {
                if (!longnames.contains(longname + i)) {
                    longname = longname + i;
                    break;
                }
            }
        }
        longnames.add(longname);
        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 (CHAPTER e2 : e.getCHAPTER()) {
            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.getPROLOGOrCAPTIONOrVERS()) {
                if (e3 instanceof CAPTION) {
                    CAPTION caption = (CAPTION) e3;
                    int depth;
                    if (caption.getType() == null) {
                        depth = 9;
                    } else {
                        switch(caption.getType()) {
                            case X_H_1:
                                depth = 1;
                                break;
                            case X_H_2:
                                depth = 2;
                                break;
                            case X_H_3:
                                depth = 3;
                                break;
                            case X_H_4:
                                depth = 4;
                                break;
                            case X_H_5:
                                depth = 5;
                                break;
                            case X_H_6:
                                depth = 6;
                                break;
                            default:
                                depth = 9;
                                break;
                        }
                    }
                    int lastDepth = headlineBuffer.size() == 0 ? -1 : headlineBuffer.get(headlineBuffer.size() - 1).getDepth();
                    if (depth <= lastDepth)
                        depth = lastDepth == 9 ? 9 : lastDepth + 1;
                    Headline h = new Headline(depth);
                    if (parseContent(h.getAppendVisitor(), caption.getContent(), abbrMap)) {
                        h.trimWhitespace();
                        h.finished();
                        headlineBuffer.add(h);
                    }
                } else if (e3 instanceof REMARK) {
                    REMARK remark = (REMARK) e3;
                    int vref = remark.getVref().intValue();
                    int idx = chapter.getVerseIndex("" + vref);
                    if (idx == -1)
                        continue;
                    Verse v = chapter.getVerses().get(idx);
                    if (remark.getContent().size() != 1)
                        continue;
                    String remarkText = normalize((String) remark.getContent().get(0), true).trim();
                    v.getAppendVisitor().visitFootnote().visitText(remarkText);
                } else if (e3 instanceof XREF) {
                    XREF xref = (XREF) e3;
                    int vref = xref.getVref().intValue();
                    int idx = chapter.getVerseIndex("" + vref);
                    if (idx == -1)
                        continue;
                    Verse v = chapter.getVerses().get(idx);
                    Visitor<RuntimeException> footnoteVisitor = v.getAppendVisitor().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())
                            continue;
                        BookID xrefBookID = BookID.fromZefId(Integer.parseInt(m.group(1)));
                        int xrefChapter = Integer.parseInt(m.group(2)), endChapter = xrefChapter;
                        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 (xrefChapter == endChapter && Integer.parseInt(verse) > Integer.parseInt(endVerse))
                            continue;
                        String xrefAbbr = abbrMap.get(xrefBookID);
                        if (xrefAbbr == null)
                            xrefAbbr = xrefBookID.getOsisID();
                        if (first)
                            first = false;
                        else
                            footnoteVisitor.visitText(" ");
                        footnoteVisitor.visitCrossReference(xrefAbbr, xrefBookID, xrefChapter, verse, endChapter, endVerse).visitText(xrefAbbr + " " + xrefChapter + ":" + verse);
                    }
                    if (first)
                        visitEmptyMarker(footnoteVisitor);
                } else if (e3 instanceof PROLOG) {
                    PROLOG prolog = (PROLOG) e3;
                    if (chapter.getProlog() != null)
                        continue;
                    FormattedText prologText = new FormattedText();
                    if (parseContent(prologText.getAppendVisitor(), prolog.getContent(), abbrMap)) {
                        prologText.trimWhitespace();
                        prologText.finished();
                        chapter.setProlog(prologText);
                    }
                } else if (e3 instanceof VERS) {
                    VERS vers = (VERS) e3;
                    int vnumber = vers.getVnumber() == null ? 0 : vers.getVnumber().intValue();
                    if (vnumber == 0)
                        vnumber = chapter.getVerses().size() + 1;
                    String verseNumber = vnumber + (vers.getAix() == null ? "" : vers.getAix());
                    String infix = "";
                    while (chapter.getVerseIndex(verseNumber) != -1) {
                        for (char ch = 'a'; ch <= 'z'; ch++) {
                            if (chapter.getVerseIndex(vnumber + infix + ch) == -1) {
                                verseNumber = vnumber + infix + ch;
                                break;
                            }
                        }
                        infix += "-";
                    }
                    Verse verse = new Verse(verseNumber);
                    Visitor<RuntimeException> visitor = verse.getAppendVisitor();
                    boolean contentFound = false;
                    if (headlineBuffer.size() > 0) {
                        for (Headline h : headlineBuffer) {
                            h.accept(visitor.visitHeadline(h.getDepth()));
                        }
                        headlineBuffer.clear();
                        contentFound = true;
                    }
                    contentFound |= parseContent(visitor, vers.getContent(), abbrMap);
                    if (contentFound) {
                        verse.trimWhitespace();
                        chapter.getVerses().add(verse);
                    }
                }
            }
            for (Verse v : chapter.getVerses()) {
                if (existingVerses > 0) {
                    existingVerses--;
                    continue;
                }
                v.finished();
            }
        }
    }
    return result;
}
Also used : Matcher(java.util.regex.Matcher) 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) VERS(biblemulticonverter.schema.zef2005.VERS) PROLOG(biblemulticonverter.schema.zef2005.PROLOG) EnumMap(java.util.EnumMap) REMARK(biblemulticonverter.schema.zef2005.REMARK) HashSet(java.util.HashSet) MetadataBook(biblemulticonverter.data.MetadataBook) Chapter(biblemulticonverter.data.Chapter) FormattedText(biblemulticonverter.data.FormattedText) BIBLEBOOK(biblemulticonverter.schema.zef2005.BIBLEBOOK) XREF(biblemulticonverter.schema.zef2005.XREF) CHAPTER(biblemulticonverter.schema.zef2005.CHAPTER) CAPTION(biblemulticonverter.schema.zef2005.CAPTION) VirtualVerse(biblemulticonverter.data.VirtualVerse) Verse(biblemulticonverter.data.Verse)

Aggregations

Book (biblemulticonverter.data.Book)5 Chapter (biblemulticonverter.data.Chapter)5 Headline (biblemulticonverter.data.FormattedText.Headline)5 MetadataBook (biblemulticonverter.data.MetadataBook)5 Verse (biblemulticonverter.data.Verse)5 VirtualVerse (biblemulticonverter.data.VirtualVerse)5 BIBLEBOOK (biblemulticonverter.schema.zef2005.BIBLEBOOK)5 CAPTION (biblemulticonverter.schema.zef2005.CAPTION)5 CHAPTER (biblemulticonverter.schema.zef2005.CHAPTER)5 VERS (biblemulticonverter.schema.zef2005.VERS)5 BookID (biblemulticonverter.data.BookID)4 PROLOG (biblemulticonverter.schema.zef2005.PROLOG)4 ArrayList (java.util.ArrayList)4 FormattedText (biblemulticonverter.data.FormattedText)3 EnumCaptionType (biblemulticonverter.schema.zef2005.EnumCaptionType)3 ObjectFactory (biblemulticonverter.schema.zef2005.ObjectFactory)3 STYLE (biblemulticonverter.schema.zef2005.STYLE)3 XMLBIBLE (biblemulticonverter.schema.zef2005.XMLBIBLE)3 XREF (biblemulticonverter.schema.zef2005.XREF)3 BigInteger (java.math.BigInteger)3