Search in sources :

Example 16 with BookID

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

the class SWORD method doImport.

protected Bible doImport(Book book) throws Exception {
    OSISHelper helper = new OSISHelper();
    Bible result = new Bible(book.getName());
    TransformerHandler th = ((SAXTransformerFactory) SAXTransformerFactory.newInstance()).newTransformerHandler();
    Map<BookID, biblemulticonverter.data.Book> parsedBooks = new EnumMap<>(BookID.class);
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    List<Verse> allVerses = new ArrayList<>();
    Verse nextCandidate = null;
    for (Iterator<?> iter = book.getGlobalKeyList().iterator(); iter.hasNext(); ) {
        Verse v = (Verse) iter.next();
        while (nextCandidate != null && !nextCandidate.equals(v)) {
            if (book.contains(nextCandidate)) {
                System.out.println("WARNING: Verse (after) skipped by iterator: " + nextCandidate);
                allVerses.add(nextCandidate);
            }
            nextCandidate = nextCandidate.getVersification().add(nextCandidate, 1);
        }
        Verse prevCandidate = v.getVersification().subtract(v, 1);
        List<Verse> versesSkippedBefore = new ArrayList<>();
        while (prevCandidate != null && !allVerses.contains(prevCandidate) && !versesSkippedBefore.contains(prevCandidate)) {
            versesSkippedBefore.add(0, prevCandidate);
            prevCandidate = prevCandidate.getVersification().subtract(prevCandidate, 1);
        }
        for (Verse vv : versesSkippedBefore) {
            if (book.contains(vv)) {
                System.out.println("WARNING: Verse (before) skipped by iterator: " + vv);
                allVerses.add(vv);
            }
        }
        allVerses.add(v);
        nextCandidate = v.getVersification().add(v, 1);
    }
    while (nextCandidate != null) {
        if (book.contains(nextCandidate)) {
            System.out.println("WARNING: Verse (at end) skipped by iterator: " + nextCandidate);
            allVerses.add(nextCandidate);
        }
        Verse nextNextCandidate = nextCandidate.getVersification().add(nextCandidate, 1);
        if (nextCandidate.equals(nextNextCandidate))
            break;
        nextCandidate = nextNextCandidate;
    }
    for (Verse v : allVerses) {
        BookID bkid = biblemulticonverter.sword.BookMapping.MAPPING.get(v.getBook());
        biblemulticonverter.data.Book bk = parsedBooks.get(bkid);
        if (!parsedBooks.containsKey(bkid)) {
            bk = new biblemulticonverter.data.Book(bkid.getOsisID().replace("x-Intr", "Intr"), bkid, bkid.getEnglishName(), bkid.getEnglishName());
            parsedBooks.put(bkid, bk);
            bk.getChapters().add(new Chapter());
            result.getBooks().add(bk);
        }
        int chapterNum = v.getChapter(), verseNum = v.getVerse();
        while (bk.getChapters().size() < chapterNum) bk.getChapters().add(new Chapter());
        Chapter chapter = bk.getChapters().get(chapterNum == 0 ? 0 : chapterNum - 1);
        FormattedText verse;
        if (verseNum == 0) {
            verse = new FormattedText();
            if (chapter.getProlog() != null) {
                chapter.getProlog().accept(verse.getAppendVisitor());
            }
            chapter.setProlog(verse);
        } else {
            if (chapterNum == 0)
                throw new IllegalStateException("Verse " + verseNum + " in chapter 0 is invalid");
            verse = new biblemulticonverter.data.Verse("" + verseNum);
            chapter.getVerses().add((biblemulticonverter.data.Verse) verse);
        }
        Element root = doc.createElement("verse");
        th.setResult(new DOMResult(root));
        new BookData(book, v).getSAXEventProvider().provideSAXEvents(th);
        if (root.getChildNodes().getLength() == 1 && root.getFirstChild() instanceof Element && root.getFirstChild().getNodeName().equals("div") && root.getFirstChild().getChildNodes().getLength() >= 1 && root.getFirstChild().getFirstChild().getNodeName().equals("title")) {
            Element div = (Element) root.getFirstChild();
            root.removeChild(div);
            div.removeChild(div.getFirstChild());
            while (div.getFirstChild() != null) {
                Node child = div.getFirstChild();
                div.removeChild(child);
                root.appendChild(child);
            }
        } else {
            throw new RuntimeException("Unexpected OSIS structure!");
        }
        helper.handleVerse(root, verse);
        if (verse.getElementTypes(1).length() == 0) {
            System.out.println("WARNING: Empty verse " + bk.getAbbr() + " " + chapterNum + ":" + verseNum);
            if (verse instanceof biblemulticonverter.data.Verse)
                chapter.getVerses().remove(verse);
            else
                chapter.setProlog(null);
        }
    }
    for (biblemulticonverter.data.Book bk : parsedBooks.values()) {
        while (!bk.getChapters().isEmpty()) {
            Chapter ch = bk.getChapters().get(bk.getChapters().size() - 1);
            if (ch.getProlog() == null && ch.getVerses().isEmpty()) {
                bk.getChapters().remove(ch);
            } else {
                break;
            }
        }
        if (bk.getChapters().isEmpty()) {
            result.getBooks().remove(bk);
        }
    }
    return result;
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) DOMResult(javax.xml.transform.dom.DOMResult) Bible(biblemulticonverter.data.Bible) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) BookID(biblemulticonverter.data.BookID) Book(org.crosswire.jsword.book.Book) BookData(org.crosswire.jsword.book.BookData) EnumMap(java.util.EnumMap) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) Chapter(biblemulticonverter.data.Chapter) FormattedText(biblemulticonverter.data.FormattedText) Verse(org.crosswire.jsword.passage.Verse)

Example 17 with BookID

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

the class SWORDVersification method buildMapping.

private void buildMapping(VersificationSet versifications, String fromName, org.crosswire.jsword.versification.Versification fromV11n, String toName, org.crosswire.jsword.versification.Versification toV11n, Map<BookID, BibleBook> reverseBookMapping) {
    System.out.println("\t" + fromName + " -> " + toName);
    Versification from = versifications.findVersification("SWORD_" + fromName);
    Versification to = versifications.findVersification("SWORD_" + toName);
    Map<Reference, List<Reference>> map = new HashMap<>();
    for (int i = 0; i < from.getVerseCount(); i++) {
        Reference ref = from.getReference(i);
        Verse v = new Verse(fromV11n, reverseBookMapping.get(ref.getBook()), ref.getChapter(), Integer.parseInt(ref.getVerse()));
        VerseKey<?> vk = VersificationsMapper.instance().mapVerse(v, toV11n);
        List<Reference> refs = new ArrayList<>();
        for (String verse : vk.getOsisID().split(" ")) {
            if (verse.isEmpty())
                continue;
            String[] verseParts = verse.split("\\.");
            BookID book = BookID.fromOsisId(verseParts[0]);
            int chapter = Integer.parseInt(verseParts[1]);
            int verseNum = Integer.parseInt(verseParts[2]);
            if (verseNum == 0)
                verseNum = 1;
            refs.add(new Reference(book, chapter, "" + verseNum));
        }
        if (!refs.isEmpty())
            map.put(ref, refs);
    }
    versifications.getMappings().add(VersificationMapping.build(from, to, map));
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Reference(biblemulticonverter.data.Versification.Reference) ArrayList(java.util.ArrayList) Versification(biblemulticonverter.data.Versification) BookID(biblemulticonverter.data.BookID) ArrayList(java.util.ArrayList) List(java.util.List) Verse(org.crosswire.jsword.passage.Verse)

Example 18 with BookID

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

the class SWORDVersification method doImport.

@Override
public void doImport(VersificationSet versifications, String... importArgs) throws Exception {
    org.crosswire.jsword.versification.Versification v11n_kjv = Versifications.instance().getVersification(SystemKJV.V11N_NAME);
    Map<BookID, BibleBook> reverseBookMapping = new EnumMap<>(BookID.class);
    for (Map.Entry<BibleBook, BookID> entry : BookMapping.MAPPING.entrySet()) {
        reverseBookMapping.put(entry.getValue(), entry.getKey());
    }
    for (String versificationName : SWORDVersificationDetector.ALL_V11N_NAMES) {
        System.out.println("--- " + versificationName + " ---");
        org.crosswire.jsword.versification.Versification v11n = Versifications.instance().getVersification(versificationName);
        LinkedHashMap<BookID, int[]> verseCounts = new LinkedHashMap<>();
        for (Iterator<BibleBook> it = v11n.getBookIterator(); it.hasNext(); ) {
            BibleBook bb = (BibleBook) it.next();
            int[] chapters = new int[v11n.getLastChapter(bb)];
            if (chapters.length == 0)
                continue;
            verseCounts.put(BookMapping.MAPPING.get(bb), chapters);
            for (int j = 1; j <= v11n.getLastChapter(bb); j++) {
                chapters[j - 1] = v11n.getLastVerse(bb, j);
            }
        }
        versifications.getVersifications().add(Versification.fromVerseCounts("SWORD_" + versificationName, null, null, verseCounts));
        if (!versificationName.equals(SystemKJV.V11N_NAME) && !missingMappings.contains(versificationName)) {
            buildMapping(versifications, SystemKJV.V11N_NAME, v11n_kjv, versificationName, v11n, reverseBookMapping);
            buildMapping(versifications, versificationName, v11n, SystemKJV.V11N_NAME, v11n_kjv, reverseBookMapping);
        }
    }
}
Also used : BibleBook(org.crosswire.jsword.versification.BibleBook) LinkedHashMap(java.util.LinkedHashMap) BookID(biblemulticonverter.data.BookID) EnumMap(java.util.EnumMap) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 19 with BookID

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

the class OpenScriptures method doImport.

@Override
public void doImport(VersificationSet versifications, String... importArgs) throws Exception {
    File inputFile = new File(importArgs[0]);
    ValidateXML.validateFileBeforeParsing(getSchema(), inputFile);
    JAXBContext ctx = JAXBContext.newInstance(ObjectFactory.class.getPackage().getName());
    Unmarshaller u = ctx.createUnmarshaller();
    BibleVersificationSystem doc = (BibleVersificationSystem) u.unmarshal(inputFile);
    List<Reference> allRefs = new ArrayList<>();
    for (BibleBookVersification bbv : doc.getBibleBookVersification()) {
        BookID bid = REFERENCE_ABBREVIATIONS.get(bbv.getReferenceAbbreviation());
        if (bid == null) {
            System.out.println("WARNING: Skipping " + bbv.getNameEnglish() + " [" + bbv.getReferenceAbbreviation() + "]");
            continue;
        }
        if (bbv.getNumChapters() != bbv.getNumVerses().size())
            System.out.println("WARNING: Chapter count " + bbv.getNumChapters() + " of " + bid + " does not match verse info " + bbv.getNumVerses().size());
        for (NumVerses nv : bbv.getNumVerses()) {
            if (nv.getChapter() == 0 && bbv.getNumVerses().size() == 1) {
                System.out.println("WARNING: Changing chapter " + bid + " 0 to 1");
                nv.setChapter(1);
            }
            if (nv.getCombinedVerses() != null || nv.getReorderedVerses() != null)
                System.out.println("WARNING: Unsupported attribute for " + bid + " " + nv.getChapter());
            Set<Integer> omitted = new HashSet<>();
            if (nv.getOmittedVerses() != null) {
                for (String num : nv.getOmittedVerses().split(",")) {
                    omitted.add(Integer.parseInt(num));
                }
            }
            for (int j = 1; j <= nv.getValue(); j++) {
                if (!omitted.contains(j)) {
                    allRefs.add(new Reference(bid, nv.getChapter(), "" + j));
                }
            }
        }
    }
    versifications.getVersifications().add(Versification.fromReferenceList(importArgs[1], doc.getHeader().getWork().getTitle(), null, allRefs));
}
Also used : Reference(biblemulticonverter.data.Versification.Reference) ArrayList(java.util.ArrayList) JAXBContext(javax.xml.bind.JAXBContext) BibleVersificationSystem(biblemulticonverter.schema.versification.openscriptures.BibleVersificationSystem) BibleBookVersification(biblemulticonverter.schema.versification.openscriptures.BibleBookVersification) BookID(biblemulticonverter.data.BookID) NumVerses(biblemulticonverter.schema.versification.openscriptures.NumVerses) Unmarshaller(javax.xml.bind.Unmarshaller) File(java.io.File) HashSet(java.util.HashSet)

Example 20 with BookID

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

the class OSIS method doImport.

@Override
public Bible doImport(File inputFile) throws Exception {
    ValidateXML.validateFileBeforeParsing(SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(ObjectFactory.class.getResource("/osisCore.2.1.1.xsd")), inputFile);
    printedWarnings.clear();
    DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    XPath xpath = javax.xml.xpath.XPathFactory.newInstance().newXPath();
    Document osisDoc = docBuilder.parse(inputFile);
    String name = xpath.evaluate("/osis/osisText/header/work/title/text()", osisDoc);
    if (name.isEmpty())
        name = "OSIS Bible";
    Bible result = new Bible(name);
    String description = xpath.evaluate("/osis/osisText/header/work/description/text()", osisDoc);
    String rights = xpath.evaluate("/osis/osisText/header/work/rights/text()", osisDoc);
    if (!description.isEmpty() || !rights.isEmpty()) {
        String date = xpath.evaluate("/osis/osisText/header/work/date/text()", osisDoc);
        String titleDesc = xpath.evaluate("/osis/osisText/titlePage/description/text()", osisDoc);
        MetadataBook mb = new MetadataBook();
        if (!description.isEmpty())
            mb.setValue(MetadataBookKey.description, description.replaceAll("[\r\n\t ]+", " ").trim());
        if (!rights.isEmpty())
            mb.setValue(MetadataBookKey.rights, rights.replaceAll("[\r\n\t ]+", " ").trim());
        if (!date.isEmpty())
            mb.setValue(MetadataBookKey.date, date);
        if (!titleDesc.isEmpty())
            mb.setValue("description@titlePage", titleDesc.replaceAll("[\r\n\t ]+", " ").trim());
        mb.finished();
        result.getBooks().add(mb.getBook());
    }
    NodeList osisBooks = (NodeList) xpath.evaluate("/osis/osisText//div[@type='book']", osisDoc, XPathConstants.NODESET);
    for (int bookIndex = 0; bookIndex < osisBooks.getLength(); bookIndex++) {
        Element osisBook = (Element) osisBooks.item(bookIndex);
        if (!osisBook.getAttribute("sID").isEmpty()) {
            Element osisBookEnd = (Element) xpath.evaluate("//div[@eID='" + osisBook.getAttribute("sID") + "']", osisDoc, XPathConstants.NODE);
            if (osisBookEnd == null) {
                throw new IllegalStateException("No milestoned div found with eID " + osisBook.getAttribute("sID"));
            }
            if (!osisBookEnd.getParentNode().isSameNode(osisBook.getParentNode())) {
                List<Node> candidates = new ArrayList<>();
                Node commonParent = osisBookEnd;
                while (commonParent != null) {
                    candidates.add(commonParent);
                    commonParent = commonParent.getParentNode();
                }
                commonParent = osisBook;
                search: while (commonParent != null) {
                    for (Node candidate : candidates) {
                        if (commonParent.isSameNode(candidate)) {
                            break search;
                        }
                    }
                    commonParent = commonParent.getParentNode();
                }
                if (commonParent == null)
                    throw new IllegalStateException("Unable to find common parent of milestoned div start and end tag");
                convertToMilestoned((Element) commonParent);
                if (!osisBookEnd.getParentNode().isSameNode(osisBook.getParentNode())) {
                    throw new IllegalStateException("Unable to normalize XML so that milestoned div start and end tags are siblings");
                }
            }
            while (osisBook.getNextSibling() != null && !osisBook.getNextSibling().isSameNode(osisBookEnd)) {
                osisBook.appendChild(osisBook.getNextSibling());
            }
            osisBookEnd.getParentNode().removeChild(osisBookEnd);
        }
        String bookOsisID = osisBook.getAttribute("osisID");
        BookID bookID = BookID.fromOsisId(bookOsisID);
        String title = bookID.getEnglishName();
        Node titleElem = osisBook.getFirstChild();
        while (titleElem instanceof Text) titleElem = titleElem.getNextSibling();
        if (titleElem instanceof Element && titleElem.getNodeName().equals("title")) {
            Element titleElement = (Element) titleElem;
            if (titleElement.getAttribute("type").equals("main") && titleElement.getChildNodes().getLength() > 0)
                title = titleElement.getTextContent();
        }
        Book bibleBook = new Book(bookOsisID, bookID, title, title);
        result.getBooks().add(bibleBook);
        parseBook(bookOsisID, osisBook, bibleBook);
    }
    return result;
}
Also used : XPath(javax.xml.xpath.XPath) MetadataBook(biblemulticonverter.data.MetadataBook) Bible(biblemulticonverter.data.Bible) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Text(org.w3c.dom.Text) FormattedText(biblemulticonverter.data.FormattedText) Document(org.w3c.dom.Document) BookID(biblemulticonverter.data.BookID) DocumentBuilder(javax.xml.parsers.DocumentBuilder) MetadataBook(biblemulticonverter.data.MetadataBook) Book(biblemulticonverter.data.Book)

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