Search in sources :

Example 6 with Reference

use of biblemulticonverter.data.Versification.Reference 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 7 with Reference

use of biblemulticonverter.data.Versification.Reference in project BibleMultiConverter by schierlm.

the class VersificationMappedDiffable method doExport.

@Override
public void doExport(Bible bible, String... exportArgs) throws Exception {
    String outputFile = exportArgs[0];
    VersificationSet vs = new VersificationSet(new File(exportArgs[1]));
    VersificationMapping vm = vs.findMapping(exportArgs[2]);
    boolean dropUnmapped = false, showNumbers = false, addTags = false;
    ;
    for (int i = 3; i < exportArgs.length; i++) {
        if (exportArgs[i].equals("DropUnmapped"))
            dropUnmapped = true;
        else if (exportArgs[i].equals("ShowNumbers"))
            showNumbers = true;
        else if (exportArgs[i].equals("AddTags"))
            addTags = true;
        else
            throw new IllegalArgumentException("Unsupported option: " + exportArgs[i]);
    }
    Map<BookID, String> abbrMap = new EnumMap<>(BookID.class);
    for (Book book : bible.getBooks()) {
        abbrMap.put(book.getId(), book.getAbbr());
    }
    Bible newBible = new Bible(bible.getName());
    Map<BookID, Book> newBooks = new EnumMap<>(BookID.class);
    for (Book book : bible.getBooks()) {
        if (book.getId().getZefID() < 1) {
            // metadata book, introduction or appendix
            newBible.getBooks().add(book);
            continue;
        }
        int cnumber = 0;
        for (Chapter chap : book.getChapters()) {
            cnumber++;
            if (chap.getProlog() != null && chap.getVerses().isEmpty()) {
                System.out.println("WARNING: Prolog for " + book.getAbbr() + " " + cnumber + " got lost as chapter contains no verses.");
            }
            for (int j = 0; j < chap.getVerses().size(); j++) {
                Verse oldVerse = chap.getVerses().get(j);
                Reference ref = new Reference(book.getId(), cnumber, oldVerse.getNumber()), newRef;
                List<Reference> newRefs = vm.getMapping(ref);
                if ((newRefs == null || newRefs.isEmpty()) && dropUnmapped) {
                    if (j == 0 && chap.getProlog() != null) {
                        System.out.println("WARNING: Prolog for " + book.getAbbr() + " " + cnumber + " got lost as first verse of it is unmapped.");
                    }
                    continue;
                }
                if (newRefs == null || newRefs.contains(ref) || newRefs.isEmpty())
                    newRef = ref;
                else
                    newRef = newRefs.get(0);
                if (!newBooks.containsKey(newRef.getBook())) {
                    Book newBook = null;
                    for (Book oldBook : bible.getBooks()) {
                        if (oldBook.getId() == newRef.getBook()) {
                            newBook = new Book(oldBook.getAbbr(), newRef.getBook(), oldBook.getShortName(), oldBook.getLongName());
                            break;
                        }
                    }
                    if (newBook == null)
                        newBook = new Book(newRef.getBook().getOsisID(), newRef.getBook(), newRef.getBook().getEnglishName(), newRef.getBook().getEnglishName());
                    newBooks.put(newRef.getBook(), newBook);
                    newBible.getBooks().add(newBook);
                }
                Book newBook = newBooks.get(newRef.getBook());
                while (newBook.getChapters().size() < newRef.getChapter()) newBook.getChapters().add(new Chapter());
                Chapter newChapter = newBook.getChapters().get(newRef.getChapter() - 1);
                if (j == 0 && chap.getProlog() != null) {
                    FormattedText newProlog = new FormattedText();
                    if (newChapter.getProlog() != null) {
                        newChapter.getProlog().accept(newProlog.getAppendVisitor());
                        newProlog.getAppendVisitor().visitLineBreak(LineBreakKind.PARAGRAPH);
                    }
                    chap.getProlog().accept(new MapXrefVisitor(newProlog.getAppendVisitor(), vm, dropUnmapped, abbrMap));
                    newProlog.finished();
                    newChapter.setProlog(newProlog);
                }
                Verse newVerse = null;
                for (Verse v : newChapter.getVerses()) {
                    if (v.getNumber().equals(newRef.getVerse())) {
                        newVerse = v;
                        break;
                    }
                }
                boolean needSpace = true;
                if (newVerse == null) {
                    newVerse = new Verse(newRef.getVerse());
                    newChapter.getVerses().add(newVerse);
                    needSpace = false;
                }
                if (needSpace || !ref.equals(newRef)) {
                    Visitor<RuntimeException> v = newVerse.getAppendVisitor();
                    if (addTags)
                        v = v.visitExtraAttribute(ExtraAttributePriority.KEEP_CONTENT, "v11n", "origverse", ref.getBook().getOsisID() + "--" + ref.getChapter() + "--" + ref.getVerse());
                    if (needSpace)
                        v.visitText(" ");
                    if (showNumbers) {
                        String verseNumber;
                        if (!ref.getBook().equals(newRef.getBook())) {
                            verseNumber = ref.getBook().getOsisID() + " " + ref.getChapter() + ":" + ref.getVerse();
                        } else if (ref.getChapter() != newRef.getChapter()) {
                            verseNumber = ref.getChapter() + ":" + ref.getVerse();
                        } else {
                            verseNumber = ref.getVerse();
                        }
                        v.visitFormattingInstruction(FormattingInstructionKind.BOLD).visitText("(" + verseNumber + ")");
                        v.visitText(" ");
                    }
                }
                oldVerse.accept(new MapXrefVisitor(newVerse.getAppendVisitor(), vm, dropUnmapped, abbrMap));
            }
        }
    }
    for (Book bk : newBible.getBooks()) {
        for (Chapter ch : bk.getChapters()) {
            for (Verse v : ch.getVerses()) {
                v.finished();
            }
        }
    }
    bible = newBible;
    new Diffable().doExport(bible, new String[] { outputFile });
}
Also used : VersificationMapping(biblemulticonverter.data.VersificationMapping) Reference(biblemulticonverter.data.Versification.Reference) Bible(biblemulticonverter.data.Bible) Chapter(biblemulticonverter.data.Chapter) FormattedText(biblemulticonverter.data.FormattedText) VersificationSet(biblemulticonverter.data.VersificationSet) BookID(biblemulticonverter.data.BookID) Book(biblemulticonverter.data.Book) File(java.io.File) EnumMap(java.util.EnumMap) Verse(biblemulticonverter.data.Verse)

Example 8 with Reference

use of biblemulticonverter.data.Versification.Reference in project BibleMultiConverter by schierlm.

the class VersificationTool method printInfo.

private void printInfo(String label, VersificationMapping m) {
    System.out.println(label + ": " + m.getRuleCount() + " rules");
    Map<Reference, Integer> occurrenceFrom = new HashMap<>();
    Map<Reference, Integer> occurrenceTo = new HashMap<>();
    for (int i = 0; i < m.getFrom().getVerseCount(); i++) {
        Reference r = m.getFrom().getReference(i);
        for (Reference rr : m.getMapping(r)) {
            countOccurrence(occurrenceFrom, r);
            countOccurrence(occurrenceTo, rr);
        }
    }
    System.out.println("\t" + m.getFrom().getName() + ": " + mappedVerseInfo(occurrenceFrom) + " of " + m.getFrom().getVerseCount() + " verses");
    System.out.println("\t" + m.getTo().getName() + ": " + mappedVerseInfo(occurrenceTo) + " of " + m.getTo().getVerseCount() + " verses");
}
Also used : HashMap(java.util.HashMap) Reference(biblemulticonverter.data.Versification.Reference)

Example 9 with Reference

use of biblemulticonverter.data.Versification.Reference in project BibleMultiConverter by schierlm.

the class CCEL method parseReference.

private Reference parseReference(String ref) throws IOException {
    String[] parts = ref.split("\\.");
    if (parts.length != 3)
        throw new IOException("Invalid OSIS ID: " + ref);
    String book = parts[0];
    BookID bk = parseBook(book);
    if (bk == null)
        return null;
    if (bk == BookID.BOOK_Esth && Arrays.asList("A", "B", "C", "D", "E", "F").contains(parts[1])) {
        parts[1] = String.valueOf(101 + parts[1].charAt(0) - 'A');
    }
    int chapter = Integer.parseInt(parts[1]);
    String verse = parts[2];
    if (verse.equals("SKIP")) {
        System.out.println("WARNING: Invalid verse number: SKIP");
        return null;
    }
    if (verse.endsWith("I") || verse.endsWith("L")) {
        System.out.println("WARNING: Invalid verse number: " + verse + ", replaced by " + verse.toLowerCase());
        verse = verse.toLowerCase();
    }
    return new Reference(bk, chapter, verse);
}
Also used : BookID(biblemulticonverter.data.BookID) Reference(biblemulticonverter.data.Versification.Reference) IOException(java.io.IOException)

Aggregations

Reference (biblemulticonverter.data.Versification.Reference)9 BookID (biblemulticonverter.data.BookID)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 List (java.util.List)5 Versification (biblemulticonverter.data.Versification)4 File (java.io.File)4 EnumMap (java.util.EnumMap)4 HashSet (java.util.HashSet)4 VersificationMapping (biblemulticonverter.data.VersificationMapping)3 Map (java.util.Map)3 JAXBContext (javax.xml.bind.JAXBContext)3 VersificationSet (biblemulticonverter.data.VersificationSet)2 RefSys (biblemulticonverter.schema.versification.ccel.RefSys)2 Alias (biblemulticonverter.schema.versification.ccel.RefSys.Alias)2 OsisID (biblemulticonverter.schema.versification.ccel.RefSys.OsisIDs.OsisID)2 RefMap (biblemulticonverter.schema.versification.ccel.RefSys.RefMap)2 Unmarshaller (javax.xml.bind.Unmarshaller)2 Bible (biblemulticonverter.data.Bible)1 Book (biblemulticonverter.data.Book)1