Search in sources :

Example 1 with DIV

use of biblemulticonverter.schema.zef2005.DIV 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 DIV

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

the class ZefaniaXML method parseContent.

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

Example 3 with DIV

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

the class ZefaniaXMLRoundtrip method parseContent.

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

Aggregations

BookID (biblemulticonverter.data.BookID)3 FormattingInstructionKind (biblemulticonverter.data.FormattedText.FormattingInstructionKind)3 Visitor (biblemulticonverter.data.FormattedText.Visitor)3 BR (biblemulticonverter.schema.zef2005.BR)3 DIV (biblemulticonverter.schema.zef2005.DIV)3 GRAM (biblemulticonverter.schema.zef2005.GRAM)3 NOTE (biblemulticonverter.schema.zef2005.NOTE)3 STYLE (biblemulticonverter.schema.zef2005.STYLE)3 XREF (biblemulticonverter.schema.zef2005.XREF)3 JAXBElement (javax.xml.bind.JAXBElement)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Matcher (java.util.regex.Matcher)2 Book (biblemulticonverter.data.Book)1 Chapter (biblemulticonverter.data.Chapter)1 FormattedText (biblemulticonverter.data.FormattedText)1 ExtraAttributePriority (biblemulticonverter.data.FormattedText.ExtraAttributePriority)1 Headline (biblemulticonverter.data.FormattedText.Headline)1 LineBreakKind (biblemulticonverter.data.FormattedText.LineBreakKind)1