Search in sources :

Example 11 with BookID

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

the class CCEL method doExport.

@Override
public void doExport(File outputFile, List<Versification> versifications, List<VersificationMapping> mappings) throws Exception {
    if (versifications.size() != 1)
        throw new IllegalArgumentException("CCEL files have to contain exactly one versification");
    Versification versification = versifications.get(0);
    ObjectFactory of = new ObjectFactory();
    RefSys refsys = of.createRefSys();
    kingdoms = false;
    if (versification.getAliases() != null) {
        for (String a : versification.getAliases()) {
            if (a.equals(versification.getName() + "__-_KINGDOMS")) {
                kingdoms = true;
            } else {
                Alias alias = of.createRefSysAlias();
                alias.setCode(a);
                refsys.getAlias().add(alias);
            }
        }
    }
    refsys.setCode(versification.getName());
    if (versification.getDescription() != null)
        refsys.setName(versification.getDescription());
    refsys.setOsisIDs(of.createRefSysOsisIDs());
    for (int i = 0; i < versification.getVerseCount(); i++) {
        Reference r = versification.getReference(i);
        OsisID oi = of.createRefSysOsisIDsOsisID();
        oi.setCode(formatReference(r, kingdoms));
        refsys.getOsisIDs().getOsisID().add(oi);
    }
    for (VersificationMapping vm : mappings) {
        Map<BookID, List<List<Reference>>> destReferencesByBook = new EnumMap<>(BookID.class);
        Map<Reference, List<List<Reference>>> destReferencesByChapter = new HashMap<>();
        for (int i = 0; i < vm.getFrom().getVerseCount(); i++) {
            Reference from = vm.getFrom().getReference(i);
            List<Reference> to = vm.getMapping(from);
            if (to.isEmpty())
                continue;
            if (!destReferencesByBook.containsKey(from.getBook()))
                destReferencesByBook.put(from.getBook(), new ArrayList<List<Reference>>());
            destReferencesByBook.get(from.getBook()).add(to);
            Reference fromChapter = new Reference(from.getBook(), from.getChapter(), "1");
            if (!destReferencesByChapter.containsKey(fromChapter))
                destReferencesByChapter.put(fromChapter, new ArrayList<List<Reference>>());
            destReferencesByChapter.get(fromChapter).add(to);
        }
        Map<BookID, BookID> bookMappings = new EnumMap<>(BookID.class);
        for (Entry<BookID, List<List<Reference>>> byBook : destReferencesByBook.entrySet()) {
            if (byBook.getValue().size() < 2)
                continue;
            BookID sameBook = byBook.getValue().get(0).get(0).getBook();
            for (List<Reference> rr : byBook.getValue()) {
                for (Reference r : rr) {
                    if (sameBook != r.getBook()) {
                        sameBook = null;
                        break;
                    }
                }
                if (sameBook == null)
                    break;
            }
            if (sameBook != null && sameBook != byBook.getKey())
                bookMappings.put(byBook.getKey(), sameBook);
        }
        Map<Reference, Reference> chapterMappings = new HashMap<>();
        for (Entry<Reference, List<List<Reference>>> byChapter : destReferencesByChapter.entrySet()) {
            if (byChapter.getValue().size() < 2)
                continue;
            Reference sameChapter = byChapter.getValue().get(0).get(0);
            sameChapter = new Reference(sameChapter.getBook(), sameChapter.getChapter(), "1");
            for (List<Reference> rr : byChapter.getValue()) {
                for (Reference r : rr) {
                    Reference thisChapter = new Reference(r.getBook(), r.getChapter(), "1");
                    if (sameChapter != null && !sameChapter.equals(thisChapter)) {
                        sameChapter = null;
                        break;
                    }
                }
                if (sameChapter == null)
                    break;
            }
            if (sameChapter == null)
                continue;
            BookID keyBook = byChapter.getKey().getBook();
            if (bookMappings.containsKey(keyBook)) {
                keyBook = bookMappings.get(keyBook);
                ;
            }
            if (sameChapter.getBook() != keyBook || sameChapter.getChapter() != byChapter.getKey().getChapter()) {
                chapterMappings.put(new Reference(byChapter.getKey().getBook(), byChapter.getKey().getChapter(), "1"), sameChapter);
            }
        }
        RefMap refmap = of.createRefSysRefMap();
        refmap.setFrom(vm.getFrom().getName());
        refmap.setTo(vm.getTo().getName());
        boolean kingdomsFrom = isKingdoms(vm.getFrom());
        boolean kingdomsTo = isKingdoms(vm.getTo());
        Set<BookID> bookMappingsDumped = EnumSet.noneOf(BookID.class);
        Set<Reference> chapterMappingsDumped = new HashSet<>();
        for (int i = 0; i < vm.getFrom().getVerseCount(); i++) {
            Reference from = vm.getFrom().getReference(i);
            Reference mappedFrom = from;
            List<Reference> to = vm.getMapping(from);
            if (bookMappings.containsKey(from.getBook())) {
                if (!bookMappingsDumped.contains(from.getBook())) {
                    addMapping(refmap, formatBook(from.getBook(), kingdomsFrom), formatBook(bookMappings.get(from.getBook()), kingdomsTo));
                    bookMappingsDumped.add(from.getBook());
                }
                mappedFrom = new Reference(bookMappings.get(from.getBook()), from.getChapter(), from.getVerse());
            } else if (kingdomsFrom != kingdomsTo && KINGS_IDS.contains(from.getBook().getOsisID())) {
                if (!bookMappingsDumped.contains(from.getBook())) {
                    addMapping(refmap, formatBook(from.getBook(), kingdomsFrom), formatBook(from.getBook(), kingdomsTo));
                    bookMappingsDumped.add(from.getBook());
                }
            }
            Reference chapRef = new Reference(from.getBook(), from.getChapter(), "1");
            Reference mapped = chapterMappings.get(chapRef);
            if (mapped != null) {
                if (!chapterMappingsDumped.contains(chapRef)) {
                    addMapping(refmap, formatBook(chapRef.getBook(), kingdomsFrom) + "." + chapRef.getChapter(), formatBook(mapped.getBook(), kingdomsTo) + "." + mapped.getChapter());
                    chapterMappingsDumped.add(chapRef);
                }
                mappedFrom = new Reference(mapped.getBook(), mapped.getChapter(), from.getVerse());
            }
            String formattedFrom = formatReference(from, kingdomsFrom);
            if (to.size() == 0) {
                addMapping(refmap, formattedFrom, "");
            } else if (to.size() == 1) {
                if (!to.get(0).equals(mappedFrom)) {
                    addMapping(refmap, formattedFrom, formatReference(to.get(0), kingdomsTo));
                }
            } else {
                boolean consecutive = true;
                int base = vm.getTo().getIndexForReference(to.get(0));
                for (int j = 1; j < to.size(); j++) {
                    if (vm.getTo().getIndexForReference(to.get(j)) != base + j) {
                        consecutive = false;
                        break;
                    }
                }
                if (consecutive) {
                    addMapping(refmap, formattedFrom, formatReference(to.get(0), kingdomsTo) + "-" + formatReference(to.get(to.size() - 1), kingdomsTo));
                } else {
                    StringBuilder formattedTo = new StringBuilder();
                    for (Reference r : to) {
                        if (formattedTo.length() > 0)
                            formattedTo.append(" ");
                        formattedTo.append(formatReference(r, kingdomsTo));
                    }
                    addMapping(refmap, formattedFrom, formattedTo.toString());
                }
            }
        }
        refsys.getRefMap().add(refmap);
    }
    JAXBContext ctx = JAXBContext.newInstance(ObjectFactory.class);
    Marshaller m = ctx.createMarshaller();
    m.setSchema(getSchema());
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    m.marshal(refsys, doc);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    transformer.transform(new DOMSource(doc), new StreamResult(outputFile));
}
Also used : VersificationMapping(biblemulticonverter.data.VersificationMapping) DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) HashMap(java.util.HashMap) RefMap(biblemulticonverter.schema.versification.ccel.RefSys.RefMap) ArrayList(java.util.ArrayList) JAXBContext(javax.xml.bind.JAXBContext) Document(org.w3c.dom.Document) ObjectFactory(biblemulticonverter.schema.versification.ccel.ObjectFactory) BookID(biblemulticonverter.data.BookID) ArrayList(java.util.ArrayList) List(java.util.List) OsisID(biblemulticonverter.schema.versification.ccel.RefSys.OsisIDs.OsisID) EnumMap(java.util.EnumMap) HashSet(java.util.HashSet) Marshaller(javax.xml.bind.Marshaller) StreamResult(javax.xml.transform.stream.StreamResult) Reference(biblemulticonverter.data.Versification.Reference) Versification(biblemulticonverter.data.Versification) RefSys(biblemulticonverter.schema.versification.ccel.RefSys) Alias(biblemulticonverter.schema.versification.ccel.RefSys.Alias)

Example 12 with BookID

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

the class MobiPocket method writeVerse.

private void writeVerse(BufferedWriter bw, FormattedText v, final Bible bb, final String versePrefix, final String lexiconName) throws IOException {
    final String lineSeparator = System.getProperty("line.separator");
    v.accept(new AbstractHTMLVisitor(bw, "</p>" + lineSeparator) {

        @Override
        public Visitor<IOException> visitHeadline(int depth) throws IOException {
            int level = depth < 3 ? depth + 3 : 6;
            writer.write("<h" + level + ">");
            pushSuffix("</h" + level + ">" + lineSeparator);
            return this;
        }

        @Override
        public void visitStart() throws IOException {
            if (suffixStack.size() == 1) {
                writer.write("<p>" + versePrefix);
            }
        }

        @Override
        protected String getNextFootnoteTarget() {
            return "#" + chapref + "f" + (footNoteCount + 1);
        }

        @Override
        public Visitor<IOException> visitFootnote() throws IOException {
            final Writer outerWriter = writer;
            return new AbstractHTMLVisitor(new StringWriter(), "") {

                @Override
                public void visitLineBreak(LineBreakKind kind) throws IOException {
                    writer.append("<br>");
                }

                @Override
                public Visitor<IOException> visitFootnote() throws IOException {
                    throw new IllegalStateException("Footnote in footnote");
                }

                @Override
                public Visitor<IOException> visitGrammarInformation(int[] strongs, String[] rmac, int[] sourceIndices) throws IOException {
                    pushSuffix("");
                    return this;
                }

                @Override
                public FormattedText.Visitor<IOException> visitDictionaryEntry(String dictionary, String entry) throws IOException {
                    writer.write("<a href=\"oeb:redirect?title=" + lexiconName + "#" + entry + "\">");
                    pushSuffix("</a>");
                    return this;
                }

                @Override
                public void visitRawHTML(RawHTMLMode mode, String raw) throws IOException {
                    if (mode != RawHTMLMode.ONLINE)
                        writer.write(raw);
                }

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

                @Override
                public Visitor<IOException> visitCrossReference(String bookAbbr, BookID book, int firstChapter, String firstVerse, int lastChapter, String lastVerse) throws IOException {
                    if (checkBXR(bookRef(bookAbbr, book), lastChapter)) {
                        writer.write("<a href=\"oeb:redirect?title=BibleNavigation#b" + bookRef(bookAbbr, book) + "c" + firstChapter + "\">");
                        pushSuffix("</a>");
                    } else {
                        pushSuffix("");
                    }
                    return this;
                }

                @Override
                public boolean visitEnd() throws IOException {
                    super.visitEnd();
                    if (suffixStack.size() == 0) {
                        if (writer.toString().startsWith(FormattedText.XREF_MARKER)) {
                            // this is a cross reference!
                            if (crossRefs.length() > 0)
                                crossRefs.append("<br>");
                            String xref = writer.toString().substring(FormattedText.XREF_MARKER.length()).trim();
                            if (!xref.startsWith("<b>"))
                                xref = versePrefix + xref;
                            crossRefs.append(xref);
                        } else {
                            footNoteCount++;
                            outerWriter.write("<sup><a name=\"" + chapref + "ft" + footNoteCount + "\" href=\"#" + chapref + "f" + footNoteCount + "\">" + footNoteCount + "</a></sup>");
                            if (footNotes.length() > 0)
                                footNotes.append("<br>");
                            footNotes.append("<sup><a name=\"" + chapref + "f" + footNoteCount + "\" href=\"#" + chapref + "ft" + footNoteCount + "\">" + footNoteCount + "</a></sup> ");
                            footNotes.append(writer.toString());
                        }
                    }
                    return false;
                }

                @Override
                public Visitor<IOException> visitHeadline(int depth) throws IOException {
                    throw new IOException("Headline inside footnote");
                }
            };
        }

        @Override
        public FormattedText.Visitor<IOException> visitCrossReference(String bookAbbr, BookID book, int firstChapter, String firstVerse, int lastChapter, String lastVerse) throws IOException {
            if (checkBXR(bookRef(bookAbbr, book), lastChapter)) {
                writer.write("<a href=\"oeb:redirect?title=BibleNavigation#b" + bookRef(bookAbbr, book) + "c" + firstChapter + "\">");
                pushSuffix("</a>");
            } else {
                pushSuffix("");
            }
            return this;
        }

        @Override
        public void visitLineBreak(LineBreakKind kind) throws IOException {
            writer.write("<br>");
        }

        @Override
        public Visitor<IOException> visitGrammarInformation(int[] strongs, String[] rmac, int[] sourceIndices) throws IOException {
            pushSuffix("");
            return this;
        }

        @Override
        public FormattedText.Visitor<IOException> visitDictionaryEntry(String dictionary, String entry) throws IOException {
            writer.write("<a href=\"oeb:redirect?title=" + lexiconName + "#" + entry + "\">");
            pushSuffix("</a>");
            return this;
        }

        @Override
        public Visitor<IOException> visitVariationText(String[] variations) throws IOException {
            throw new UnsupportedOperationException("Variations not supported");
        }
    });
}
Also used : Visitor(biblemulticonverter.data.FormattedText.Visitor) IOException(java.io.IOException) RawHTMLMode(biblemulticonverter.data.FormattedText.RawHTMLMode) LineBreakKind(biblemulticonverter.data.FormattedText.LineBreakKind) StringWriter(java.io.StringWriter) BookID(biblemulticonverter.data.BookID) BufferedWriter(java.io.BufferedWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 13 with BookID

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

the class MobiPocket method doExport.

@Override
public void doExport(Bible bible, String... exportArgs) throws Exception {
    footNotes.setLength(0);
    crossRefs.setLength(0);
    footNoteCount = 0;
    chapref = "";
    File exportFile = new File(exportArgs[0]);
    String title = bible.getName();
    File directory = exportFile.getParentFile();
    String filename = exportFile.getName();
    try (final BufferedWriter opfw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(directory, filename + ".opf")), "UTF-8"))) {
        opfw.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" + "<package unique-identifier=\"uid\">\r\n" + "  <metadata>\r\n" + "    <dc-metadata xmlns:dc=\"http://purl.org/metadata/dublin_core\" xmlns:oebpackage=\"http://openebook.org/namespaces/oeb-package/1.0/\">\r\n" + "      <dc:Title>" + TITLEPREFIX + title + "</dc:Title>\r\n" + "      <dc:Language>" + LANGUAGE + "</dc:Language>\r\n" + "    </dc-metadata>\r\n" + "    <x-metadata>\r\n" + "      <DatabaseName>Bible" + filename + "</DatabaseName>\r\n" + "      <output encoding=\"Windows-1252\"></output>\r\n" + "    </x-metadata>\r\n" + "  </metadata>\r\n" + "  <manifest>\r\n" + "    <item id=\"onlyfile\" media-type=\"text/x-oeb1-document\" href=\"" + filename + ".html\"></item>\r\n" + "  </manifest>\r\n" + "  <spine>\r\n" + "    <itemref idref=\"onlyfile\" />\r\n" + "  </spine>\r\n" + "  <tours></tours>\r\n" + "  <guide></guide>\r\n" + "</package>\r\n" + "");
    }
    boolean isDictionary = false;
    for (Book bk : bible.getBooks()) {
        if (bk.getId() == BookID.DICTIONARY_ENTRY) {
            isDictionary = true;
            break;
        } else if (bk.getId().getZefID() > 0) {
            isDictionary = false;
            break;
        }
    }
    try (final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(directory, filename + ".html")), "UTF-8"))) {
        bw.write("<html><head>");
        bw.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
        bw.write("<style type=\"text/css\">body {font-family: Verdana, Arial, Helvetica, sans-serif}</style>");
        bw.write("<title>" + TITLEPREFIX + title + "</title></head><body>");
        bw.newLine();
        bw.write("<h1>" + title + "</h1>");
        if (isDictionary) {
            bw.write("<a onclick=\"index_search()\">Suchen</a><br>");
        }
        bw.write("<h2>" + TOC + "</h2>");
        bw.newLine();
        List<String> books = new ArrayList<String>();
        Map<String, Integer> maxChapter = new HashMap<String, Integer>();
        Map<String, BookID> bookIDs = new HashMap<String, BookID>();
        for (Book bk : bible.getBooks()) {
            books.add(bk.getAbbr());
            if (isDictionary) {
                bw.write("<p><a href=\"#" + bk.getAbbr() + "\">" + bk.getLongName() + "</a></p>");
            } else {
                bw.write("<p><a href=\"#b" + bookRef(bk) + "\">" + bk.getLongName() + " (" + bk.getAbbr() + ")</a></p>");
            }
            bw.newLine();
            maxChapter.put(bk.getAbbr(), bk.getChapters().size());
            bookIDs.put(bk.getAbbr(), bk.getId());
        }
        try (final BufferedWriter bxw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(directory, filename + ".bxr")), "UTF-8"))) {
            bxw.write(title);
            bxw.newLine();
            for (String bk : books) {
                bxw.write(bk + "|" + bookRef(bk, bookIDs.get(bk)) + "|" + maxChapter.get(bk));
                bxw.newLine();
            }
        }
        bxrs.add(new MobiPocketBXR(filename, new File(directory, filename + ".bxr")));
        final String lexiconName = (isDictionary ? "Bible" : "BibleDict") + filename;
        for (Book bk : bible.getBooks()) {
            if (bk.getId() == BookID.DICTIONARY_ENTRY) {
                bw.write("<mbp:pagebreak><idx:entry>");
                bw.newLine();
                bw.write("<h2><a name=\"" + bk.getAbbr() + "\" external=\"yes\"><idx:orth>" + bk.getLongName() + "</idx:orth></a></h2>");
                bw.newLine();
                bw.write("<p>");
                bw.newLine();
                writeVerse(bw, bk.getChapters().get(0).getProlog(), bible, "", lexiconName);
                bw.write("</p>");
                bw.newLine();
                writeFootNotes(bw);
                bw.write("</idx:entry>");
            } else {
                bw.write("<mbp:pagebreak>");
                bw.newLine();
                chapref = "b" + bookRef(bk);
                bw.write("<h2><a name=\"" + chapref + "\" external=\"yes\">" + bk.getLongName() + " (" + bk.getAbbr() + ")</a> (<a href=\"oeb:redirect?title=BibleNavigation#" + chapref + "\">Navigation</a>)</h2>");
                bw.newLine();
                int chapter = 0;
                for (Chapter ch : bk.getChapters()) {
                    chapter++;
                    chapref = "b" + bookRef(bk) + "c" + chapter;
                    bw.write("<h3><a name=\"" + chapref + "\" external=\"yes\">" + bk.getAbbr() + " " + chapter + "</a> (<a href=\"oeb:redirect?title=BibleNavigation#" + chapref + "\">Navigation</a>)</h3>");
                    bw.newLine();
                    if (ch.getProlog() != null) {
                        bw.write("<small>");
                        bw.newLine();
                        writeVerse(bw, ch.getProlog(), bible, "", lexiconName);
                        bw.write("</small>");
                        bw.newLine();
                    }
                    for (final Verse v : ch.getVerses()) {
                        writeVerse(bw, v, bible, "<b>" + v.getNumber() + "</b> ", lexiconName);
                    }
                    writeFootNotes(bw);
                }
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Chapter(biblemulticonverter.data.Chapter) BufferedWriter(java.io.BufferedWriter) BookID(biblemulticonverter.data.BookID) Book(biblemulticonverter.data.Book) FileOutputStream(java.io.FileOutputStream) MobiPocketBXR(biblemulticonverter.tools.MobiPocketBXR) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) Verse(biblemulticonverter.data.Verse)

Example 14 with BookID

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

the class OSIS method parseStructuredTextElement.

private void parseStructuredTextElement(Visitor<RuntimeException> vv, Element elem) {
    if (elem.getNodeName().equals("lb")) {
        vv.visitLineBreak(LineBreakKind.NEWLINE);
    } else if (elem.getNodeName().equals("brp")) {
        vv.visitLineBreak(LineBreakKind.PARAGRAPH);
    } else if (elem.getNodeName().equals("divineName")) {
        parseStructuredTextChildren(vv.visitFormattingInstruction(FormattingInstructionKind.DIVINE_NAME), elem);
    } else if (elem.getNodeName().equals("woj")) {
        parseStructuredTextChildren(vv.visitFormattingInstruction(FormattingInstructionKind.WORDS_OF_JESUS), elem);
    } else if (elem.getNodeName().equals("catchWord")) {
        parseStructuredTextChildren(vv.visitCSSFormatting("osis-style: catchWord; font-style:italic;"), elem);
    } else if (elem.getNodeName().equals("hi")) {
        FormattingInstructionKind kind;
        if (elem.getAttribute("type").equals("italic")) {
            kind = FormattingInstructionKind.ITALIC;
        } else if (elem.getAttribute("type").equals("bold")) {
            kind = FormattingInstructionKind.BOLD;
        } else {
            kind = null;
            printWarning("WARNING: Invalid hi type: " + elem.getAttribute("type"));
        }
        if (elem.getChildNodes().getLength() != 0) {
            Visitor<RuntimeException> vv1 = kind == null ? vv : vv.visitFormattingInstruction(kind);
            parseStructuredTextChildren(vv1, elem);
        }
    } else if (elem.getNodeName().equals("seg") || elem.getNodeName().equals("transChange") || elem.getNodeName().equals("rdg")) {
        String css;
        if (elem.getNodeName().equals("seg") && elem.getAttribute("type").equals("x-alternative")) {
            css = "osis-style: alternative; color: gray;";
        } else if (elem.getNodeName().equals("transChange") && elem.getAttribute("type").equals("added")) {
            css = "osis-style: added; font-style:italic;";
        } else if (elem.getNodeName().equals("transChange") && elem.getAttribute("type").equals("deleted")) {
            css = "osis-style: deleted; text-decoration: line-through; color: gray;";
        } else if (elem.getNodeName().equals("transChange") && elem.getAttribute("type").equals("amplified")) {
            css = "osis-style: amplified; font-style: italic;";
        } else if (elem.getNodeName().equals("transChange") && elem.getAttribute("type").isEmpty()) {
            css = "osis-style: trans-change;";
        } else if (elem.getNodeName().equals("rdg") && elem.getAttribute("type").equals("alternative")) {
            css = "osis-style: alternative-reading; color: gray;";
        } else if (elem.getNodeName().equals("rdg") && elem.getAttribute("type").equals("x-literal")) {
            css = "osis-style: literal-reading; color: gray;";
        } else if (elem.getNodeName().equals("rdg") && elem.getAttribute("type").equals("x-meaning")) {
            css = "osis-style: meaning-reading; color: gray;";
        } else if (elem.getNodeName().equals("rdg") && elem.getAttribute("type").equals("x-equivalent")) {
            css = "osis-style: equivalent-reading; color: gray;";
        } else if (elem.getNodeName().equals("rdg") && elem.getAttribute("type").equals("x-identity")) {
            css = "osis-style: identity-reading; color: gray;";
        } else if (elem.getNodeName().equals("rdg") && elem.getAttribute("type").isEmpty()) {
            css = "osis-style: reading; color: gray;";
        } else {
            css = null;
            printWarning("WARNING: Invalid " + elem.getNodeName() + " type: " + elem.getAttribute("type"));
        }
        if (elem.getChildNodes().getLength() != 0) {
            Visitor<RuntimeException> vv1 = css == null ? vv : vv.visitCSSFormatting(css);
            parseStructuredTextChildren(vv1, elem);
        }
    } else if (elem.getNodeName().equals("note")) {
        if (elem.getAttribute("type").equals("crossReference")) {
            Visitor<RuntimeException> fn = vv.visitFootnote();
            fn.visitText(FormattedText.XREF_MARKER);
            if (elem.getFirstChild() != null && elem.getFirstChild().getNodeName().equals("reference")) {
                for (Node n = elem.getFirstChild(); n != null; n = n.getNextSibling()) {
                    if (n instanceof Text) {
                        fn.visitText(n.getTextContent());
                        continue;
                    }
                    Element e = (Element) n;
                    String[] ref = e.getAttribute("osisRef").split("\\.");
                    if (ref.length != 3) {
                        printWarning("WARNING: Invalid reference target: " + e.getAttribute("osisRef"));
                        fn.visitText(e.getTextContent());
                        continue;
                    }
                    BookID bookID = BookID.fromOsisId(ref[0]);
                    int ch = Integer.parseInt(ref[1]);
                    String vs = ref[2];
                    fn.visitCrossReference(ref[0], bookID, ch, vs, ch, vs).visitText(e.getTextContent());
                }
            } else if (elem.getTextContent().length() > 0) {
                // OSIS converted from USFM contains a reference back to the verse itself
                for (Node n = elem.getFirstChild(); n != null; n = n.getNextSibling()) {
                    if (n instanceof Element && n.getNodeName().equals("reference")) {
                        if (((Element) n).getAttribute("osisRef").equals(((Element) elem).getAttribute("osisRef"))) {
                            n = n.getPreviousSibling();
                            n.getParentNode().removeChild(n.getNextSibling());
                        }
                    }
                }
                boolean first = true;
                for (String ref : elem.getTextContent().split("\\|")) {
                    Matcher m = XREF_PATTERN.matcher(ref);
                    if (!m.matches()) {
                        ref = ref.trim();
                        if (ref.startsWith("1 ") || ref.startsWith("2 ") || ref.startsWith("3 ")) {
                            ref = ref.substring(0, 1) + ref.substring(2);
                        }
                        m = XREF_PATTERN_2.matcher(ref);
                        if (m.matches()) {
                            try {
                                BookID.fromOsisId(m.group(1));
                            } catch (IllegalArgumentException ex) {
                                BookID bk = null;
                                for (BookID id : BookID.values()) {
                                    if (id.getThreeLetterCode().equalsIgnoreCase(m.group(1)))
                                        bk = id;
                                }
                                if (bk != null) {
                                    m = XREF_PATTERN_2.matcher(bk.getOsisID() + " " + m.group(2) + "." + m.group(3));
                                } else {
                                    m = XREF_PATTERN_2.matcher("");
                                }
                            }
                        }
                    }
                    if (!first)
                        fn.visitText("; ");
                    first = false;
                    if (m.matches()) {
                        String book = m.group(1);
                        BookID bookID = BookID.fromOsisId(book);
                        int ch = Integer.parseInt(m.group(2));
                        String vs = m.group(3);
                        fn.visitCrossReference(book, bookID, ch, vs, ch, vs).visitText(ref);
                    } else {
                        printWarning("WARNING: Malformed cross reference: " + ref);
                        fn.visitText(ref.replaceAll("[\r\n\t ]+", " ").trim());
                    }
                }
            } else {
                printWarning("WARNING: crossReference without content");
                fn.visitText("-");
            }
        } else if (elem.getFirstChild() != null) {
            Visitor<RuntimeException> v = vv.visitFootnote();
            parseStructuredTextChildren(v, elem);
        }
    } else if (elem.getNodeName().equals("w")) {
        if (elem.getFirstChild() == null)
            // skip empty w tags
            return;
        String src = elem.getAttribute("src");
        Visitor<RuntimeException> v = vv;
        int[] strong = null, idx = null;
        List<Integer> strongList = new ArrayList<Integer>();
        for (String lemma : elem.getAttribute("lemma").trim().split(" +")) {
            if (!lemma.startsWith("strong:G") && !lemma.startsWith("strong:H"))
                continue;
            String rawStrong = lemma.substring(8);
            if (!rawStrong.matches("0*[1-9][0-9]*(-0*[1-9][0-9]*)*")) {
                printWarning("WARNING: Invalid strong dictionary entry: " + rawStrong);
                continue;
            }
            String[] strs = rawStrong.split("-");
            for (String str : strs) {
                strongList.add(Integer.parseInt(str));
            }
        }
        if (!strongList.isEmpty())
            strong = strongList.stream().mapToInt(s -> s).toArray();
        List<String> rmac = new ArrayList<>();
        for (String morph : elem.getAttribute("morph").trim().split(" +")) {
            if (morph.startsWith("robinson:")) {
                String rmacCandidate = morph.substring(9);
                if (Utils.compilePattern(Utils.RMAC_REGEX).matcher(rmacCandidate).matches()) {
                    rmac.add(rmacCandidate);
                } else {
                    printWarning("WARNING: Invalid RMAC: " + rmacCandidate);
                }
            }
        }
        if (src.matches("[0-9]{2}( [0-9]{2})*")) {
            String[] strs = src.split(" ");
            idx = new int[strs.length];
            for (int i = 0; i < strs.length; i++) {
                idx[i] = Integer.parseInt(strs[i]);
            }
        }
        if (strong == null && rmac.isEmpty() && idx == null) {
            printWarning("INFO: Skipped <w> tag without any usable information");
        } else {
            v = v.visitGrammarInformation(strong, rmac.isEmpty() ? null : rmac.toArray(new String[rmac.size()]), idx);
        }
        parseStructuredTextChildren(v, elem);
    } else if (elem.getNodeName().equals("reference")) {
        String osisRef = elem.getAttribute("osisRef");
        if (osisRef.contains("\u00A0")) {
            printWarning("WARNING: osisRef contains Non-Breaking spaces: '" + osisRef + "'");
            osisRef = osisRef.replace('\u00A0', ' ');
        }
        if (!osisRef.equals(osisRef.trim())) {
            printWarning("WARNING: Removed whitespace from osisRef '" + osisRef + "' - replaced by '" + osisRef.trim() + "'");
            osisRef = osisRef.trim();
        }
        Matcher fixupMatcher = Utils.compilePattern("([A-Z0-9][A-Z0-9a-z]+\\.[0-9]+\\.)([0-9]+)((?:[+-][0-9]+)+)").matcher(osisRef);
        if (fixupMatcher.matches()) {
            osisRef = fixupMatcher.group(1) + fixupMatcher.group(2);
            for (String suffix : fixupMatcher.group(3).split("(?=[+-])")) {
                if (suffix.isEmpty())
                    continue;
                osisRef += suffix.substring(0, 1).replace('+', ' ') + fixupMatcher.group(1) + suffix.substring(1);
            }
            printWarning("INFO: Replaced osisRef " + elem.getAttribute("osisRef") + " by " + osisRef);
        }
        if (osisRefMap == null) {
            osisRefMap = new Properties();
            String path = System.getProperty("biblemulticonverter.osisrefmap", "");
            if (!path.isEmpty()) {
                try (FileInputStream fis = new FileInputStream(path)) {
                    osisRefMap.load(fis);
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }
        }
        String mappedOsisRef = osisRefMap.getProperty(osisRef);
        if (mappedOsisRef != null) {
            printWarning("INFO: Replaced osisRef " + osisRef + " by " + mappedOsisRef + " (based on osisRef map)");
            osisRef = mappedOsisRef;
        }
        if (osisRef.matches("[^ ]+ [^ ]+") && elem.getFirstChild() instanceof Text && elem.getFirstChild().getNextSibling() == null) {
            String value = elem.getTextContent();
            int lastPos = value.lastIndexOf('.');
            if (lastPos != -1) {
                Element newElem = elem.getOwnerDocument().createElement("reference");
                newElem.setAttribute("osisRef", osisRef.split(" ")[0]);
                newElem.appendChild(elem.getOwnerDocument().createTextNode(value.substring(0, lastPos)));
                parseStructuredTextElement(vv, newElem);
                vv.visitText(".");
                newElem = elem.getOwnerDocument().createElement("reference");
                newElem.setAttribute("osisRef", osisRef.split(" ")[1]);
                newElem.appendChild(elem.getOwnerDocument().createTextNode(value.substring(lastPos + 1)));
                parseStructuredTextElement(vv, newElem);
                return;
            }
        }
        Visitor<RuntimeException> v = vv;
        if (osisRef.matches("[A-Z0-9][A-Z0-9a-z]+\\.[0-9]+\\.[0-9]+")) {
            String[] osisRefParts = osisRef.split("\\.");
            int chapter = Integer.parseInt(osisRefParts[1]);
            try {
                v = v.visitCrossReference(osisRefParts[0], BookID.fromOsisId(osisRefParts[0]), chapter, osisRefParts[2], chapter, osisRefParts[2]);
            } catch (IllegalArgumentException ex) {
                printWarning("WARNING: " + ex.getMessage());
            }
        } else if (osisRef.matches("([A-Z0-9][A-Z0-9a-z]+)\\.[0-9]+\\.[0-9]+-\\1\\.[0-9]+\\.[0-9]+")) {
            String[] osisRefParts = osisRef.split("[.-]");
            int firstChapter = Integer.parseInt(osisRefParts[1]);
            int lastChapter = Integer.parseInt(osisRefParts[4]);
            try {
                v = v.visitCrossReference(osisRefParts[0], BookID.fromOsisId(osisRefParts[0]), firstChapter, osisRefParts[2], lastChapter, osisRefParts[5]);
            } catch (IllegalArgumentException ex) {
                printWarning("WARNING: " + ex.getMessage());
            }
        } else {
            printWarning("WARNING: Unsupported osisRef: " + osisRef);
        }
        parseStructuredTextChildren(v, elem);
    } else if (elem.getNodeName().equals("variation")) {
        parseStructuredTextChildren(vv.visitVariationText(new String[] { elem.getAttribute("name") }), elem);
    } else {
        printWarning("WARNING: invalid structured element level tag: " + elem.getNodeName());
    }
}
Also used : ObjectFactory(biblemulticonverter.schema.roundtripxml.ObjectFactory) DOMSource(javax.xml.transform.dom.DOMSource) Text(org.w3c.dom.Text) LineBreakKind(biblemulticonverter.data.FormattedText.LineBreakKind) Arrays(java.util.Arrays) XPath(javax.xml.xpath.XPath) XPathConstants(javax.xml.xpath.XPathConstants) StreamResult(javax.xml.transform.stream.StreamResult) Bible(biblemulticonverter.data.Bible) MetadataBook(biblemulticonverter.data.MetadataBook) MetadataBookKey(biblemulticonverter.data.MetadataBook.MetadataBookKey) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) FormattedText(biblemulticonverter.data.FormattedText) Matcher(java.util.regex.Matcher) ValidateXML(biblemulticonverter.tools.ValidateXML) Document(org.w3c.dom.Document) Node(org.w3c.dom.Node) Utils(biblemulticonverter.data.Utils) XMLConstants(javax.xml.XMLConstants) Book(biblemulticonverter.data.Book) VirtualVerse(biblemulticonverter.data.VirtualVerse) RawHTMLMode(biblemulticonverter.data.FormattedText.RawHTMLMode) Properties(java.util.Properties) SchemaFactory(javax.xml.validation.SchemaFactory) NodeList(org.w3c.dom.NodeList) FormattingInstructionKind(biblemulticonverter.data.FormattedText.FormattingInstructionKind) Verse(biblemulticonverter.data.Verse) Headline(biblemulticonverter.data.FormattedText.Headline) Set(java.util.Set) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Comment(org.w3c.dom.Comment) File(java.io.File) BookID(biblemulticonverter.data.BookID) List(java.util.List) Element(org.w3c.dom.Element) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Pattern(java.util.regex.Pattern) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) ExtraAttributePriority(biblemulticonverter.data.FormattedText.ExtraAttributePriority) Chapter(biblemulticonverter.data.Chapter) Visitor(biblemulticonverter.data.FormattedText.Visitor) Visitor(biblemulticonverter.data.FormattedText.Visitor) Matcher(java.util.regex.Matcher) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) FormattingInstructionKind(biblemulticonverter.data.FormattedText.FormattingInstructionKind) Text(org.w3c.dom.Text) FormattedText(biblemulticonverter.data.FormattedText) IOException(java.io.IOException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) BookID(biblemulticonverter.data.BookID)

Example 15 with BookID

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

the class OnLineBible method doExport.

@Override
public void doExport(Bible bible, String... exportArgs) throws Exception {
    String outFile = exportArgs[0], namesFile = null;
    ;
    boolean includeStrongs = false;
    if (exportArgs.length > 1) {
        if (exportArgs[1].equals("IncludeStrongs")) {
            includeStrongs = true;
        } else {
            namesFile = exportArgs[1];
        }
        if (exportArgs.length > 2 && exportArgs[2].equals("IncludeStrongs")) {
            includeStrongs = true;
        }
    }
    Set<BookID> supportedBooks = EnumSet.noneOf(BookID.class);
    for (BookMeta bm : BOOK_META) {
        supportedBooks.add(bm.id);
    }
    Map<BookID, Book> bookMap = new EnumMap<>(BookID.class);
    for (Book book : bible.getBooks()) {
        if (supportedBooks.contains(book.getId()))
            bookMap.put(book.getId(), book);
        else
            System.out.println("WARNING: Skipping book " + book.getAbbr());
    }
    if (namesFile != null) {
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(namesFile))) {
            for (BookMeta bm : BOOK_META) {
                Book bk = bookMap.get(bm.id);
                if (bk != null) {
                    bw.write(bk.getShortName() + " " + bm.abbr);
                    bw.newLine();
                }
            }
        }
    }
    try (BufferedWriter bw = new BufferedWriter(new FileWriter(outFile))) {
        for (BookMeta bm : BOOK_META) {
            String prefix = "";
            if (bm.id == BookID.BOOK_Matt && includeStrongs) {
                prefix = "0 ";
            }
            Book bk = bookMap.remove(bm.id);
            int[] verseCount = StandardVersification.KJV.getVerseCount(bm.id);
            for (int i = 0; i < verseCount.length; i++) {
                Chapter ch = bk != null && i < bk.getChapters().size() ? bk.getChapters().get(i) : null;
                int maxVerse = verseCount[i];
                BitSet allowedNumbers = new BitSet(maxVerse + 1);
                allowedNumbers.set(1, maxVerse + 1);
                List<VirtualVerse> vvs = ch == null ? null : ch.createVirtualVerses(null);
                for (int vnum = 1; vnum <= verseCount[i]; vnum++) {
                    bw.write("$$$ " + bm.abbr + " " + (i + 1) + ":" + vnum + " ");
                    bw.newLine();
                    StringBuilder text = new StringBuilder(prefix);
                    if (vvs != null) {
                        for (VirtualVerse vv : vvs) {
                            if (vv.getNumber() == vnum) {
                                for (Headline h : vv.getHeadlines()) {
                                    text.append(" {\\$");
                                    h.accept(new OnLineBibleVisitor(text, includeStrongs));
                                    text.append("\\$} ");
                                }
                                for (Verse v : vv.getVerses()) {
                                    if (!v.getNumber().equals("" + vv.getNumber())) {
                                        text.append("\\\\(" + v.getNumber() + ")\\\\ ");
                                    }
                                    v.accept(new OnLineBibleVisitor(text, includeStrongs));
                                }
                            }
                        }
                    }
                    if (text.length() > 0) {
                        bw.write(text.toString().replaceAll("  +", " "));
                        bw.newLine();
                    }
                    prefix = "";
                }
            }
        }
    }
    if (!bookMap.isEmpty())
        throw new IllegalStateException("Remaining books: " + bookMap.keySet());
}
Also used : VirtualVerse(biblemulticonverter.data.VirtualVerse) FileWriter(java.io.FileWriter) Chapter(biblemulticonverter.data.Chapter) BitSet(java.util.BitSet) BufferedWriter(java.io.BufferedWriter) BookID(biblemulticonverter.data.BookID) Book(biblemulticonverter.data.Book) Headline(biblemulticonverter.data.FormattedText.Headline) EnumMap(java.util.EnumMap) VirtualVerse(biblemulticonverter.data.VirtualVerse) Verse(biblemulticonverter.data.Verse)

Aggregations

BookID (biblemulticonverter.data.BookID)36 Book (biblemulticonverter.data.Book)22 Chapter (biblemulticonverter.data.Chapter)20 Verse (biblemulticonverter.data.Verse)19 ArrayList (java.util.ArrayList)18 EnumMap (java.util.EnumMap)15 MetadataBook (biblemulticonverter.data.MetadataBook)14 Bible (biblemulticonverter.data.Bible)13 FormattedText (biblemulticonverter.data.FormattedText)13 IOException (java.io.IOException)13 VirtualVerse (biblemulticonverter.data.VirtualVerse)12 HashMap (java.util.HashMap)11 List (java.util.List)10 Headline (biblemulticonverter.data.FormattedText.Headline)9 File (java.io.File)9 Visitor (biblemulticonverter.data.FormattedText.Visitor)8 HashSet (java.util.HashSet)8 Matcher (java.util.regex.Matcher)7 JAXBElement (javax.xml.bind.JAXBElement)7 Reference (biblemulticonverter.data.Versification.Reference)6