Search in sources :

Example 6 with NOTE

use of biblemulticonverter.schema.zef2005.NOTE 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 Note (com.google.containeranalysis.v1alpha1.Note)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 Test (org.junit.Test)2 Book (biblemulticonverter.data.Book)1 Chapter (biblemulticonverter.data.Chapter)1 FormattedText (biblemulticonverter.data.FormattedText)1 ExtraAttributePriority (biblemulticonverter.data.FormattedText.ExtraAttributePriority)1