Search in sources :

Example 16 with Chapter

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

the class Validate method printHeadlines.

private void printHeadlines(Bible bible) throws IOException {
    HeadlineSummaryVisitor hsv = new HeadlineSummaryVisitor();
    for (Book b : bible.getBooks()) {
        int cnumber = 0;
        for (Chapter c : b.getChapters()) {
            cnumber++;
            if (c.getProlog() != null) {
                hsv.setCurrentVerse(b.getAbbr() + " " + cnumber + " (Prolog)");
                c.getProlog().accept(hsv);
            }
            for (Verse v : c.getVerses()) {
                hsv.setCurrentVerse(b.getAbbr() + " " + cnumber + ":" + v.getNumber());
                v.accept(hsv);
            }
        }
    }
    hsv.printVerseRange();
}
Also used : Book(biblemulticonverter.data.Book) Chapter(biblemulticonverter.data.Chapter) Verse(biblemulticonverter.data.Verse)

Example 17 with Chapter

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

the class Validate method doExport.

@Override
public void doExport(Bible bible, String... exportArgs) throws Exception {
    Map<String, Set<String>> dictionaryEntries = null;
    if (exportArgs.length > 0 && exportArgs[0].equals("IncludeExternalRefs")) {
        dictionaryEntries = new HashMap<String, Set<String>>();
        for (int i = 1; i < exportArgs.length; i++) {
            String ref = exportArgs[i];
            if (ref.startsWith("S"))
                ref = "Lstrongs=" + ref.substring(1);
            if (ref.startsWith("B")) {
                for (Book baseBook : new Diffable().doImport(new File(ref.substring(1))).getBooks()) {
                    if (baseBook.getId().getZefID() < 0)
                        continue;
                    Book book = null;
                    for (Book oldBook : bible.getBooks()) {
                        if (oldBook.getId().equals(baseBook.getId())) {
                            book = oldBook;
                            break;
                        }
                    }
                    if (book == null) {
                        book = new Book(baseBook.getAbbr(), baseBook.getId(), "$BBL$" + baseBook.getAbbr(), "$BBL$" + baseBook.getAbbr());
                        bible.getBooks().add(book);
                    }
                    for (int ch = 0; ch < baseBook.getChapters().size(); ch++) {
                        for (Verse v : baseBook.getChapters().get(ch).getVerses()) {
                            while (book.getChapters().size() <= ch) book.getChapters().add(new Chapter());
                            Chapter c = book.getChapters().get(ch);
                            boolean verseFound = false;
                            for (Verse vv : c.getVerses()) {
                                if (vv.getNumber().equals(v.getNumber())) {
                                    verseFound = true;
                                    break;
                                }
                            }
                            if (!verseFound) {
                                Verse vv = new Verse(v.getNumber());
                                vv.getAppendVisitor().visitText("X");
                                vv.finished();
                                c.getVerses().add(vv);
                            }
                        }
                    }
                }
            } else if (ref.startsWith("L")) {
                if (ref.contains("=")) {
                    int pos = ref.indexOf('=');
                    Set<String> entryList = new HashSet<>();
                    try (BufferedReader br = new BufferedReader(new FileReader(ref.substring(pos + 1)))) {
                        String line;
                        while ((line = br.readLine()) != null) {
                            entryList.add(line);
                        }
                    }
                    dictionaryEntries.put(ref.substring(1, pos), entryList);
                } else {
                    Set<String> entryList = new HashSet<>();
                    for (Book bk : bible.getBooks()) {
                        if (bk.getId().equals(BookID.DICTIONARY_ENTRY)) {
                            entryList.add(bk.getAbbr());
                        }
                    }
                    dictionaryEntries.put(ref.substring(1), entryList);
                }
            } else if (ref.startsWith("X")) {
                try (BufferedWriter bw = new BufferedWriter(new FileWriter(ref.substring(1)))) {
                    for (Book bk : bible.getBooks()) {
                        if (bk.getId().equals(BookID.DICTIONARY_ENTRY)) {
                            bw.write(bk.getAbbr());
                            bw.newLine();
                        }
                    }
                }
            } else {
                System.out.println("WARNING: external reference: " + ref);
            }
        }
        exportArgs = new String[0];
    }
    List<String> danglingReferences = new ArrayList<>();
    bible.validate(danglingReferences, dictionaryEntries);
    if (danglingReferences.size() > 0) {
        System.out.println("Dangling references: ");
        for (String reference : danglingReferences) {
            System.out.println("\t" + reference);
        }
        System.out.println();
    }
    if (exportArgs.length == 1 && exportArgs[0].equals("PrintSpecialVerseSummary")) {
        System.out.println("Special verse numbers:");
        printSpecialVerseNumbers(bible);
    } else if (exportArgs.length == 1 && exportArgs[0].equals("PrintHeadlines")) {
        System.out.println("Headlines:");
        printHeadlines(bible);
    } else if (exportArgs.length > 0) {
        System.out.println("WARNING: Unsupported arguments: " + Arrays.toString(exportArgs));
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) FileWriter(java.io.FileWriter) Diffable(biblemulticonverter.format.Diffable) Chapter(biblemulticonverter.data.Chapter) ArrayList(java.util.ArrayList) BufferedWriter(java.io.BufferedWriter) Book(biblemulticonverter.data.Book) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) Verse(biblemulticonverter.data.Verse)

Example 18 with Chapter

use of biblemulticonverter.data.Chapter 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 19 with Chapter

use of biblemulticonverter.data.Chapter 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 20 with Chapter

use of biblemulticonverter.data.Chapter 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

Chapter (biblemulticonverter.data.Chapter)64 Book (biblemulticonverter.data.Book)60 Verse (biblemulticonverter.data.Verse)57 FormattedText (biblemulticonverter.data.FormattedText)28 VirtualVerse (biblemulticonverter.data.VirtualVerse)25 MetadataBook (biblemulticonverter.data.MetadataBook)22 BookID (biblemulticonverter.data.BookID)20 ArrayList (java.util.ArrayList)20 Bible (biblemulticonverter.data.Bible)19 Headline (biblemulticonverter.data.FormattedText.Headline)19 File (java.io.File)18 BufferedWriter (java.io.BufferedWriter)15 IOException (java.io.IOException)14 FileOutputStream (java.io.FileOutputStream)13 OutputStreamWriter (java.io.OutputStreamWriter)12 EnumMap (java.util.EnumMap)9 HashMap (java.util.HashMap)9 Visitor (biblemulticonverter.data.FormattedText.Visitor)8 BufferedReader (java.io.BufferedReader)8 FileInputStream (java.io.FileInputStream)8