Search in sources :

Example 61 with EnumMap

use of java.util.EnumMap in project BibleMultiConverter by schierlm.

the class UnboundBible method doExport.

@Override
public void doExport(Bible bible, String... exportArgs) throws Exception {
    MetadataBook mb = bible.getMetadataBook();
    if (mb == null)
        mb = new MetadataBook();
    String fileTypeName = mb.getValue("filetype@unbound");
    UnboundBibleFileType fileType = fileTypeName != null ? UnboundBibleFileType.valueOf(fileTypeName) : UnboundBibleFileType.Unmapped_BCVS;
    Map<String, List<String[]>> mapping = new HashMap<>();
    Map<BookID, List<String>> extraEmptyVerses = new EnumMap<>(BookID.class);
    List<Book> allBooks = new ArrayList<>(bible.getBooks());
    if (exportArgs.length == 2) {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(exportArgs[1]), StandardCharsets.UTF_8))) {
            String line;
            List<Book> nonexistingExtraEmptyVersesBooks = new ArrayList<>();
            while ((line = br.readLine()) != null) {
                if (line.isEmpty() || line.startsWith("#"))
                    continue;
                String[] fields = line.split("\t", -1);
                if (fields.length != 8)
                    throw new IOException(line);
                int isNull = Integer.parseInt(fields[7]);
                if (isNull == 0) {
                    String key = fields[3] + " " + fields[4] + ":" + fields[5] + " " + fields[6];
                    String[] value = Arrays.copyOf(fields, 3);
                    if (!mapping.containsKey(key))
                        mapping.put(key, new ArrayList<String[]>());
                    mapping.get(key).add(value);
                } else if (isNull == 1) {
                    BookID id = BOOK_INFO_BY_CODE.get(fields[3]).id;
                    if (!extraEmptyVerses.containsKey(id))
                        extraEmptyVerses.put(id, new ArrayList<String>());
                    extraEmptyVerses.get(id).add(fields[0] + "\t" + fields[1] + "\t" + fields[2] + "\t" + fields[3] + "\t" + fields[4] + "\t" + fields[5] + "\t" + fields[6] + "\t0\t");
                    Book existingBook = null;
                    for (Book bk : allBooks) {
                        if (bk.getId() == id)
                            existingBook = bk;
                    }
                    if (existingBook == null) {
                        if (nonexistingExtraEmptyVersesBooks.isEmpty() || nonexistingExtraEmptyVersesBooks.get(nonexistingExtraEmptyVersesBooks.size() - 1).getId() != id)
                            nonexistingExtraEmptyVersesBooks.add(new Book(id.getOsisID(), id, id.getOsisID(), id.getOsisID()));
                    } else if (!nonexistingExtraEmptyVersesBooks.isEmpty()) {
                        int pos = allBooks.indexOf(existingBook);
                        allBooks.addAll(pos, nonexistingExtraEmptyVersesBooks);
                        nonexistingExtraEmptyVersesBooks.clear();
                    }
                } else {
                    throw new IOException(line);
                }
            }
        }
        fileType = UnboundBibleFileType.Mapped_BCVS;
    }
    boolean useRoundtrip = Boolean.getBoolean("unbound.roundtrip");
    boolean useParsedFormat = Boolean.getBoolean("unbound.parsed");
    try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportArgs[0]), StandardCharsets.UTF_8))) {
        if (exportArgs.length == 2 && useRoundtrip)
            bw.write("\r\n");
        bw.write("#THE UNBOUND BIBLE (www.unboundbible.org)\r\n");
        bw.write("#name\t" + bible.getName() + "\r\n");
        bw.write("#filetype\t" + fileType.name().replace('_', '-') + "\r\n");
        writeMetadata(bw, "copyright", mb.getValue(MetadataBookKey.rights));
        writeMetadata(bw, "abbreviation", mb.getValue("abbreviation@unbound"));
        writeMetadata(bw, "language", mb.getValue(MetadataBookKey.language));
        writeMetadata(bw, "note", mb.getValue(MetadataBookKey.description));
        bw.write("#columns\t" + fileType.getColumnHeader() + "\r\n");
        int[] sorting = { 0 };
        for (Book bk : allBooks) {
            if (bk.getId() == BookID.METADATA)
                continue;
            UnboundBibleBookInfo bi = BOOK_INFO_BY_ID.get(bk.getId());
            if (bi == null) {
                System.out.println("WARNING: Skipping unsupported book: " + bk.getAbbr());
                continue;
            }
            if (extraEmptyVerses.containsKey(bk.getId())) {
                for (String emptyVerse : extraEmptyVerses.get(bk.getId())) {
                    bw.write(emptyVerse + "\r\n");
                }
            }
            for (int cc = 0; cc < bk.getChapters().size(); cc++) {
                Chapter ch = bk.getChapters().get(cc);
                int chapter = cc + 1;
                for (Verse vv : ch.getVerses()) {
                    String vn = vv.getNumber(), svn = "";
                    int c = chapter;
                    if (vn.matches("[0-9]+,.*")) {
                        int pos = vn.indexOf(',');
                        c = Integer.parseInt(vn.substring(0, pos));
                        vn = vn.substring(pos + 1);
                    }
                    if (vn.equals("1-/") || vn.equals("1-//")) {
                        vn = "0";
                    } else if (c == 1 && vn.endsWith("//")) {
                        c = 0;
                        vn = vn.substring(0, vn.length() - 2);
                    } else if (vn.endsWith("/a")) {
                        vn = vn.substring(0, vn.length() - 2);
                        svn = "EndA";
                    } else if (vn.endsWith("/b")) {
                        vn = vn.substring(0, vn.length() - 2);
                        svn = "EndB";
                    } else if (vn.endsWith("/")) {
                        vn = vn.substring(0, vn.length() - 1);
                    } else if (vn.matches("[0-9]+[.][a-z]")) {
                        svn = vn.substring(vn.length() - 1) + vn.substring(vn.length() - 1);
                        vn = vn.substring(0, vn.length() - 2);
                    } else if (!vn.matches("[0-9]+")) {
                        Matcher m = Pattern.compile("([0-9]+)([-,/.a-zG][-0-9,/.a-zG]*)").matcher(vn);
                        if (!m.matches())
                            throw new IOException(vn);
                        vn = m.group(1);
                        svn = m.group(2);
                    }
                    int v = Integer.parseInt(vn);
                    sorting[0] += 10;
                    StringBuilder sb = new StringBuilder();
                    vv.accept(new UnboundBibleVisitor(sb, sorting, useParsedFormat));
                    String text = sb.toString();
                    if (useRoundtrip && text.contains("\uFEFF")) {
                        if (text.equals("\uFEFF-\uFEFF"))
                            text = "";
                        text = text.replace("\uFEFF ", " ").replace(" \uFEFF", " ");
                    }
                    for (String[] nrsva_fields : lookup(mapping, bi.code, c, v, svn)) {
                        String[] fields = new String[] { nrsva_fields[0], nrsva_fields[1], nrsva_fields[2], bi.code, "" + c, "" + v, svn, "" + sorting[0], text };
                        fileType.writeFields(bw, fields, "nrsva_book_index", "nrsva_chapter", "nrsva_verse", "orig_book_index", "orig_chapter", "orig_verse", "orig_subverse", "order_by", "text");
                    }
                }
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) BufferedWriter(java.io.BufferedWriter) BookID(biblemulticonverter.data.BookID) MetadataBook(biblemulticonverter.data.MetadataBook) Book(biblemulticonverter.data.Book) ArrayList(java.util.ArrayList) List(java.util.List) EnumMap(java.util.EnumMap) MetadataBook(biblemulticonverter.data.MetadataBook) InputStreamReader(java.io.InputStreamReader) Chapter(biblemulticonverter.data.Chapter) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) Verse(biblemulticonverter.data.Verse)

Example 62 with EnumMap

use of java.util.EnumMap in project BibleMultiConverter by schierlm.

the class ZefaniaXMLRoundtrip method parseBible.

protected Bible parseBible(XMLBIBLE doc) throws Exception {
    Bible result = new Bible(doc.getBiblename());
    MetadataBook metadata = new MetadataBook();
    if (doc.getStatus() != null) {
        metadata.setValue(MetadataBookKey.status, doc.getStatus().value());
    }
    if (doc.getVersion() != null) {
        metadata.setValue(MetadataBookKey.version, doc.getVersion());
    }
    if (doc.getRevision() != null) {
        metadata.setValue(MetadataBookKey.revision, doc.getRevision().toString());
    }
    for (JAXBElement<?> elem : doc.getINFORMATION().getTitleOrCreatorOrDescription()) {
        if (elem.getValue() == null)
            continue;
        String value = normalize(elem.getValue().toString(), true).trim();
        if (value.length() == 0)
            value = "-empty-";
        metadata.setValue(elem.getName().getLocalPart(), value);
    }
    metadata.finished();
    if (metadata.getKeys().size() > 0)
        result.getBooks().add(metadata.getBook());
    Set<String> abbrs = new HashSet<String>();
    Set<String> shortnames = new HashSet<String>();
    Map<BookID, String> abbrMap = new EnumMap<BookID, String>(BookID.class);
    List<BIBLEBOOK> nl = doc.getBIBLEBOOK();
    for (BIBLEBOOK e : nl) {
        String shortname = e.getBsname();
        int number = e.getBnumber().intValue();
        BookID bookID = BookID.fromZefId(number);
        if (shortname == null)
            shortname = "_" + bookID.getOsisID();
        else if (shortname.length() == 0)
            shortname = "_" + bookID.getOsisID() + "[[]]";
        String abbr = shortname.replaceAll("[^A-Z0-9a-zäöü]++", "");
        if (abbr.length() == 0 || Character.isLowerCase(abbr.charAt(0)))
            abbr = "X" + abbr;
        if (abbr.length() == 1)
            abbr += "x";
        if (abbrs.contains(abbr)) {
            for (int i = 2; i < 100; i++) {
                if (!abbrs.contains(abbr + i)) {
                    abbr = abbr + i;
                    break;
                }
            }
        }
        abbrs.add(abbr);
        abbrMap.put(bookID, abbr);
    }
    abbrs.clear();
    for (BIBLEBOOK e : nl) {
        String shortname = e.getBsname();
        String longname = e.getBname();
        int number = e.getBnumber().intValue();
        BookID bookID = BookID.fromZefId(number);
        if (shortname == null)
            shortname = "_" + bookID.getOsisID();
        else if (shortname.length() == 0)
            shortname = "_" + bookID.getOsisID() + "[[]]";
        if (longname == null)
            longname = "_" + bookID.getEnglishName();
        else if (longname.length() == 0)
            longname = "_" + bookID.getEnglishName() + "[[]]";
        else
            longname = longname.replaceAll("  ++", " ").trim();
        String abbr = shortname.replaceAll("[^A-Z0-9a-zäöü]++", "");
        if (abbr.length() == 0 || Character.isLowerCase(abbr.charAt(0)))
            abbr = "X" + abbr;
        if (abbr.length() == 1)
            abbr += "x";
        if (abbrs.contains(abbr)) {
            for (int i = 2; i < 100; i++) {
                if (!abbrs.contains(abbr + i)) {
                    abbr = abbr + i;
                    break;
                }
            }
        }
        abbrs.add(abbr);
        if (shortname.equals("Gen") && longname.equals("Genesis") && bookID == BookID.BOOK_Exod) {
            System.out.println("WARNING: Book number " + bookID.getZefID() + " has name " + longname);
            shortname = "Exo[[Gen]]";
            longname = "Exodus[[Genesis]]";
        }
        if (shortname.equals("1Chr") && longname.equals("2 Chronicles")) {
            System.out.println("WARNING: Book name 2 Chronicles has short name 1Chr");
            shortname = "2Chr[[1Chr]]";
        }
        if (shortnames.contains(shortname)) {
            System.out.println("WARNING: Duplicate short name " + shortname);
            for (int i = 2; i < 100; i++) {
                if (!shortnames.contains(shortname + i + "[[" + shortname + "]]")) {
                    shortname = shortname + i + "[[" + shortname + "]]";
                    break;
                }
            }
        }
        shortnames.add(shortname);
        Book book = new Book(abbr, bookID, shortname, longname);
        int lastvref = -1;
        List<Headline> headlineBuffer = new ArrayList<Headline>();
        for (CHAPTER e2 : e.getCHAPTER()) {
            int chapterNumber = e2.getCnumber().intValue();
            while (book.getChapters().size() < chapterNumber) book.getChapters().add(new Chapter());
            Chapter chapter = book.getChapters().get(chapterNumber - 1);
            for (Object e3 : e2.getPROLOGOrCAPTIONOrVERS()) {
                if (e3 instanceof CAPTION) {
                    CAPTION caption = (CAPTION) e3;
                    if (lastvref != -1 && lastvref != caption.getVref().intValue())
                        throw new IOException();
                    lastvref = caption.getVref().intValue();
                    int level;
                    if (caption.getType() == null) {
                        level = 9;
                    } else {
                        switch(caption.getType()) {
                            case X_H_1:
                                level = 1;
                                break;
                            case X_H_2:
                                level = 2;
                                break;
                            case X_H_3:
                                level = 3;
                                break;
                            case X_H_4:
                                level = 4;
                                break;
                            case X_H_5:
                                level = 5;
                                break;
                            case X_H_6:
                                level = 6;
                                break;
                            default:
                                throw new IOException();
                        }
                    }
                    Headline h = new Headline(level);
                    headlineBuffer.add(h);
                    if (!parseContent(h.getAppendVisitor(), caption.getContent(), abbrMap)) {
                        visitEmptyMarker(h.getAppendVisitor());
                    } else {
                        h.trimWhitespace();
                    }
                    h.finished();
                } else if (e3 instanceof REMARK) {
                    REMARK remark = (REMARK) e3;
                    int vref = remark.getVref().intValue();
                    int idx = chapter.getVerseIndex("" + vref);
                    if (idx == -1)
                        throw new IOException(vref + ":" + remark.getContent());
                    Verse v = chapter.getVerses().get(idx);
                    if (remark.getContent().size() != 1)
                        throw new IOException();
                    String remarkText = normalize((String) remark.getContent().get(0), true).trim();
                    v.getAppendVisitor().visitExtraAttribute(ExtraAttributePriority.KEEP_CONTENT, "zefania", "footnote-source", "remark").visitFootnote().visitText(remarkText);
                } else if (e3 instanceof XREF) {
                    XREF xref = (XREF) e3;
                    int vref = xref.getVref().intValue();
                    int idx = chapter.getVerseIndex("" + vref);
                    if (idx == -1)
                        throw new IOException(vref + ":" + xref.getMscope());
                    Verse v = chapter.getVerses().get(idx);
                    Visitor<RuntimeException> footnoteVisitor = v.getAppendVisitor().visitExtraAttribute(ExtraAttributePriority.KEEP_CONTENT, "zefania", "footnote-source", "outer-xref").visitFootnote();
                    boolean first = true;
                    for (String mscope : xref.getMscope().split(" ")) {
                        Matcher m = Utils.compilePattern("([0-9]+);([0-9]+)(-[0-9]+)?;([0-9]+)(-[0-9]+)?").matcher(mscope);
                        if (!m.matches())
                            throw new IOException(mscope);
                        BookID xrefBookID = BookID.fromZefId(Integer.parseInt(m.group(1)));
                        int xrefChapter = Integer.parseInt(m.group(2)), endChapter = xrefChapter;
                        if (m.group(3) != null)
                            endChapter = Integer.parseInt(m.group(3).substring(1));
                        String verse = m.group(4);
                        if (verse.equals("0"))
                            verse = "1//G";
                        String endVerse = m.group(5);
                        if (endVerse == null)
                            endVerse = verse;
                        else
                            endVerse = endVerse.substring(1);
                        if (endVerse.equals("0"))
                            endVerse = "1//G";
                        String xrefAbbr = abbrMap.get(xrefBookID);
                        if (xrefAbbr == null)
                            xrefAbbr = xrefBookID.getOsisID();
                        if (first)
                            first = false;
                        else
                            footnoteVisitor.visitText(" ");
                        if (xrefChapter == endChapter && !verse.equals("1//G") && !endVerse.equals("1//G") && Integer.parseInt(verse) > Integer.parseInt(endVerse)) {
                            String tmp = verse;
                            verse = endVerse;
                            endVerse = tmp;
                        }
                        footnoteVisitor.visitCrossReference(xrefAbbr, xrefBookID, xrefChapter, verse, endChapter, endVerse).visitText(xrefAbbr + " " + xrefChapter + ":" + verse);
                    }
                } else if (e3 instanceof PROLOG) {
                    PROLOG prolog = (PROLOG) e3;
                    if (prolog.getVref().intValue() != 1)
                        throw new IOException("" + prolog.getVref());
                    if (chapter.getProlog() != null)
                        throw new IOException("More than one prolog found");
                    FormattedText prologText = new FormattedText();
                    if (parseContent(prologText.getAppendVisitor(), prolog.getContent(), abbrMap)) {
                        prologText.trimWhitespace();
                        prologText.finished();
                        chapter.setProlog(prologText);
                    }
                } else if (e3 instanceof VERS) {
                    VERS vers = (VERS) e3;
                    int vnumber = vers.getVnumber().intValue();
                    if (lastvref != -1) {
                        if (lastvref != vnumber)
                            throw new IOException(lastvref + " != " + vnumber);
                        lastvref = -1;
                    }
                    Verse verse = new Verse("" + vnumber);
                    Visitor<RuntimeException> visitor = verse.getAppendVisitor();
                    boolean contentFound = false;
                    if (headlineBuffer.size() > 0) {
                        for (Headline h : headlineBuffer) {
                            h.accept(visitor.visitHeadline(h.getDepth()));
                        }
                        headlineBuffer.clear();
                        contentFound = true;
                    }
                    contentFound |= parseContent(visitor, vers.getContent(), abbrMap);
                    if (!contentFound) {
                        visitEmptyMarker(visitor);
                    }
                    verse.trimWhitespace();
                    chapter.getVerses().add(verse);
                } else {
                    throw new IOException(e3.getClass().toString());
                }
            }
            for (Verse v : chapter.getVerses()) v.finished();
        }
        result.getBooks().add(book);
    }
    return result;
}
Also used : Matcher(java.util.regex.Matcher) Bible(biblemulticonverter.data.Bible) ArrayList(java.util.ArrayList) BookID(biblemulticonverter.data.BookID) Book(biblemulticonverter.data.Book) MetadataBook(biblemulticonverter.data.MetadataBook) Headline(biblemulticonverter.data.FormattedText.Headline) VERS(biblemulticonverter.schema.zef2005.VERS) PROLOG(biblemulticonverter.schema.zef2005.PROLOG) EnumMap(java.util.EnumMap) REMARK(biblemulticonverter.schema.zef2005.REMARK) HashSet(java.util.HashSet) MetadataBook(biblemulticonverter.data.MetadataBook) Chapter(biblemulticonverter.data.Chapter) IOException(java.io.IOException) FormattedText(biblemulticonverter.data.FormattedText) BIBLEBOOK(biblemulticonverter.schema.zef2005.BIBLEBOOK) XREF(biblemulticonverter.schema.zef2005.XREF) CHAPTER(biblemulticonverter.schema.zef2005.CHAPTER) CAPTION(biblemulticonverter.schema.zef2005.CAPTION) VirtualVerse(biblemulticonverter.data.VirtualVerse) Verse(biblemulticonverter.data.Verse)

Example 63 with EnumMap

use of java.util.EnumMap in project BibleMultiConverter by schierlm.

the class VersificationTool method run.

@Override
public void run(String... args) throws Exception {
    File dbFile = new File(args[0]);
    VersificationSet vs = dbFile.exists() ? new VersificationSet(dbFile) : new VersificationSet();
    boolean save = false;
    switch(args[1]) {
        case "list":
            if (args.length == 2) {
                for (Versification v : vs.getVersifications()) printInfo(v);
                Map<String, Integer> mappingCounts = new HashMap<>();
                for (VersificationMapping m : vs.getMappings()) {
                    String namePrefix = m.getFrom().getName() + "/" + m.getTo().getName() + "/";
                    printInfo(namePrefix + countOccurrence(mappingCounts, namePrefix), m);
                }
            } else {
                for (int i = 2; i < args.length; i++) {
                    if (args[i].contains("/"))
                        printInfo(args[i], vs.findMapping(args[i]));
                    else
                        printInfo(vs.findVersification(args[i]));
                }
            }
            break;
        case "import":
            Module<VersificationFormat> importModule = Main.versificationFormats.get(args[2]);
            importModule.getImplementationClass().newInstance().doImport(vs, Arrays.copyOfRange(args, 3, args.length));
            save = true;
            break;
        case "export":
            Module<VersificationFormat> exportModule = Main.versificationFormats.get(args[2]);
            List<Versification> vv = args.length == 4 ? vs.getVersifications() : new ArrayList<Versification>();
            List<VersificationMapping> mm = args.length == 4 ? vs.getMappings() : new ArrayList<VersificationMapping>();
            for (int i = 4; i < args.length; i++) {
                if (args[i].contains("/"))
                    mm.add(vs.findMapping(args[i]));
                else
                    vv.add(vs.findVersification(args[i]));
            }
            exportModule.getImplementationClass().newInstance().doExport(new File(args[3]), vv, mm);
            break;
        case "rename":
            vs.findVersification(args[2]).setName(args[3]);
            save = true;
            break;
        case "remove":
            List<Versification> vvv = new ArrayList<>();
            List<VersificationMapping> mmm = new ArrayList<>();
            for (int i = 2; i < args.length; i++) {
                if (args[i].contains("/"))
                    mmm.add(vs.findMapping(args[i]));
                else
                    vvv.add(vs.findVersification(args[i]));
            }
            for (Versification v : vvv) {
                vs.getVersifications().remove(v);
            }
            for (VersificationMapping m : mmm) vs.getMappings().remove(m);
            save = true;
            break;
        case "join":
            VersificationMapping m1 = vs.findMapping(args[2]);
            for (int i = 3; i < args.length; i++) {
                VersificationMapping m2 = vs.findMapping(args[i]);
                if (m1.getTo() != m2.getFrom())
                    throw new IllegalStateException("Cannot join, versification mismatch: " + m1.getTo().getName() + " != " + m2.getFrom().getName());
                Map<Reference, List<Reference>> map = new HashMap<>();
                for (int j = 0; j < m1.getFrom().getVerseCount(); j++) {
                    Reference r1 = m1.getFrom().getReference(j);
                    List<Reference> r3 = new ArrayList<>();
                    for (Reference r2 : m1.getMapping(r1)) {
                        r3.addAll(m2.getMapping(r2));
                    }
                    for (int k = 0; k < r3.size() - 1; k++) {
                        if (r3.get(k).equals(r3.get(k + 1))) {
                            r3.remove(k);
                            k--;
                        }
                    }
                    if (!r3.isEmpty())
                        map.put(r1, r3);
                }
                m1 = VersificationMapping.build(m1.getFrom(), m2.getTo(), map);
            }
            vs.getMappings().add(m1);
            save = true;
            break;
        case "compare":
            if (args[2].contains("/")) {
                VersificationMapping vm1 = vs.findMapping(args[2]);
                VersificationMapping vm2 = vs.findMapping(args[3]);
                if (vm1.getFrom() != vm2.getFrom() || vm1.getTo() != vm2.getTo())
                    throw new IllegalArgumentException("Versification mappings need to have same source/destination versification to be compared");
                EnumMap<MappingVerseCompareVariant, Integer> variants = new EnumMap<>(MappingVerseCompareVariant.class);
                for (int i = 0; i < vm1.getFrom().getVerseCount(); i++) {
                    Reference r = vm1.getFrom().getReference(i);
                    MappingVerseCompareVariant cv = MappingVerseCompareVariant.get(vm1.getMapping(r), vm2.getMapping(r));
                    variants.put(cv, variants.containsKey(cv) ? variants.get(cv) + 1 : 1);
                }
                for (Map.Entry<MappingVerseCompareVariant, Integer> entry : variants.entrySet()) {
                    System.out.println(entry.getKey().toString() + ": " + entry.getValue());
                }
            } else {
                Versification v1 = vs.findVersification(args[2]);
                Set<Reference> v1r = new HashSet<>();
                for (int i = 0; i < v1.getVerseCount(); i++) v1r.add(v1.getReference(i));
                Versification v2 = vs.findVersification(args[3]);
                Set<Reference> v2r = new HashSet<>();
                for (int i = 0; i < v2.getVerseCount(); i++) v2r.add(v2.getReference(i));
                if (v1.getVerseCount() == v2.getVerseCount() && v1r.equals(v2r)) {
                    boolean sameOrder = true;
                    for (int i = 0; i < v1.getVerseCount(); i++) {
                        if (!v1.getReference(i).equals(v2.getReference(i))) {
                            sameOrder = false;
                            break;
                        }
                    }
                    System.out.println("Versifications contain same verses" + (sameOrder ? " in same order" : ""));
                } else if (v1r.containsAll(v2r)) {
                    System.out.println("Right versification is a subset of left versification");
                } else if (v2r.containsAll(v1r)) {
                    System.out.println("Left versification is a subset of right versification");
                } else {
                    Set<Reference> intersect = new HashSet<Reference>();
                    intersect.addAll(v1r);
                    intersect.retainAll(v2r);
                    if (intersect.isEmpty())
                        System.out.println("Versifications are disjoint");
                    else
                        System.out.println("Versifications have " + intersect.size() + " verses in common");
                }
            }
            break;
        default:
            System.out.println("Unsupported command: " + args[1]);
    }
    if (save) {
        try (Writer w = new OutputStreamWriter(new FileOutputStream(dbFile), StandardCharsets.UTF_8)) {
            vs.saveTo(w);
        }
    }
}
Also used : VersificationMapping(biblemulticonverter.data.VersificationMapping) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) VersificationSet(biblemulticonverter.data.VersificationSet) ArrayList(java.util.ArrayList) List(java.util.List) EnumMap(java.util.EnumMap) HashSet(java.util.HashSet) Reference(biblemulticonverter.data.Versification.Reference) Versification(biblemulticonverter.data.Versification) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) VersificationFormat(biblemulticonverter.versification.VersificationFormat) File(java.io.File) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap) Map(java.util.Map) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 64 with EnumMap

use of java.util.EnumMap in project BibleMultiConverter by schierlm.

the class CCEL method doImport.

@Override
public void doImport(VersificationSet vset, String... importArgs) throws Exception {
    JAXBContext ctx = JAXBContext.newInstance(ObjectFactory.class);
    Unmarshaller u = ctx.createUnmarshaller();
    for (String filename : importArgs) {
        File inputFile = new File(filename);
        ValidateXML.validateFileBeforeParsing(getSchema(), inputFile);
        RefSys doc = (RefSys) u.unmarshal(inputFile);
        List<String> aliases = new ArrayList<>();
        for (Alias alias : doc.getAlias()) {
            aliases.add(alias.getCode());
        }
        List<Versification.Reference> refs = new ArrayList<>();
        Set<Versification.Reference> refsSeen = new HashSet<>();
        kingdoms = null;
        for (OsisID osisid : doc.getOsisIDs().getOsisID()) {
            Reference r = parseReference(osisid.getCode());
            if (r != null) {
                if (!refsSeen.add(r))
                    System.out.println("WARNING: Same verse referenced twice in versification: " + r);
                else
                    refs.add(r);
            }
        }
        if (kingdoms == null)
            kingdoms = false;
        if (kingdoms) {
            aliases.add(doc.getCode() + "__-_KINGDOMS");
        }
        vset.getVersifications().add(Versification.fromReferenceList(doc.getCode(), doc.getName(), aliases.isEmpty() ? null : (String[]) aliases.toArray(new String[aliases.size()]), refs));
        for (RefMap refmap : doc.getRefMap()) {
            Versification from = vset.findVersification(refmap.getFrom());
            Versification to = vset.findVersification(refmap.getTo());
            System.out.println("-- " + from.getName() + " -> " + to.getName() + " --");
            Map<BookID, BookID> bookMap = new EnumMap<>(BookID.class);
            Map<Reference, Reference> chapterMap = new HashMap<>();
            Map<Reference, List<Reference>> mapMap = new HashMap<>();
            for (RefMap.Map map : refmap.getMap()) {
                int partsCount = map.getFrom().split("\\.").length;
                if (partsCount == 1) {
                    kingdoms = isKingdoms(from);
                    BookID ff = parseBook(map.getFrom());
                    kingdoms = isKingdoms(to);
                    ;
                    BookID tt = map.getTo().isEmpty() ? BookID.DICTIONARY_ENTRY : parseBook(map.getTo());
                    if (ff == null || tt == null)
                        continue;
                    bookMap.put(ff, tt);
                } else if (partsCount == 2) {
                    kingdoms = isKingdoms(from);
                    Reference ff = parseReference(map.getFrom() + ".1");
                    kingdoms = isKingdoms(to);
                    Reference tt = parseReference(map.getTo() + ".1");
                    if (ff == null || tt == null)
                        continue;
                    chapterMap.put(ff, tt);
                } else {
                    kingdoms = isKingdoms(from);
                    Reference ff = parseReference(map.getFrom());
                    if (ff == null)
                        continue;
                    if (!from.containsReference(ff)) {
                        System.out.println("WARNING: Versification " + from.getName() + " does not contain verse " + ff + "; skipping");
                        continue;
                    }
                    if (map.getTo().isEmpty()) {
                        mapMap.put(ff, new ArrayList<Reference>());
                        continue;
                    }
                    kingdoms = isKingdoms(to);
                    List<Reference> list = new ArrayList<>();
                    if (map.getTo().contains(" ")) {
                        for (String rrr : map.getTo().split(" ")) {
                            Reference r1 = parseReference(rrr);
                            if (r1 == null)
                                continue;
                            if (!to.containsReference(r1)) {
                                System.out.println("WARNING: Verse " + r1 + " does not exist in " + to.getName() + "; skipping");
                                continue;
                            }
                            list.add(r1);
                        }
                    } else {
                        String[] parts = map.getTo().split("-", 2);
                        Reference r1 = parseReference(parts[0]);
                        if (r1 == null)
                            continue;
                        if (!to.containsReference(r1)) {
                            System.out.println("WARNING: Mapping maps to verse " + r1 + " not contained in destination mapping " + to.getName());
                            continue;
                        }
                        if (parts.length == 1) {
                            list.add(r1);
                        } else {
                            Reference r2 = parseReference(parts[1]);
                            int i1 = to.getIndexForReference(r1);
                            int i2 = to.getIndexForReference(r2);
                            for (int i = i1; i <= i2; i++) {
                                list.add(to.getReference(i));
                            }
                        }
                    }
                    mapMap.put(ff, list);
                }
            }
            for (int i = 0; i < from.getVerseCount(); i++) {
                final Reference ff = from.getReference(i), tt;
                if (mapMap.containsKey(ff))
                    continue;
                Reference chapterMapped = chapterMap.get(new Reference(ff.getBook(), ff.getChapter(), "1"));
                if (chapterMapped != null) {
                    tt = new Reference(chapterMapped.getBook(), chapterMapped.getChapter(), ff.getVerse());
                } else if (bookMap.containsKey(ff.getBook())) {
                    tt = new Reference(bookMap.get(ff.getBook()), ff.getChapter(), ff.getVerse());
                } else {
                    tt = ff;
                }
                if (to.containsReference(tt)) {
                    List<Reference> list = new ArrayList<>();
                    list.add(tt);
                    mapMap.put(ff, list);
                }
            }
            for (Iterator<Map.Entry<Reference, List<Reference>>> it = mapMap.entrySet().iterator(); it.hasNext(); ) {
                Map.Entry<Reference, List<Reference>> reference = it.next();
                if (reference.getValue().isEmpty())
                    it.remove();
            }
            vset.getMappings().add(VersificationMapping.build(from, to, mapMap));
        }
    }
}
Also used : RefMap(biblemulticonverter.schema.versification.ccel.RefSys.RefMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JAXBContext(javax.xml.bind.JAXBContext) Entry(java.util.Map.Entry) BookID(biblemulticonverter.data.BookID) ArrayList(java.util.ArrayList) List(java.util.List) Unmarshaller(javax.xml.bind.Unmarshaller) OsisID(biblemulticonverter.schema.versification.ccel.RefSys.OsisIDs.OsisID) EnumMap(java.util.EnumMap) HashSet(java.util.HashSet) Reference(biblemulticonverter.data.Versification.Reference) Versification(biblemulticonverter.data.Versification) RefSys(biblemulticonverter.schema.versification.ccel.RefSys) Alias(biblemulticonverter.schema.versification.ccel.RefSys.Alias) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) EnumMap(java.util.EnumMap) RefMap(biblemulticonverter.schema.versification.ccel.RefSys.RefMap)

Example 65 with EnumMap

use of java.util.EnumMap 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)

Aggregations

EnumMap (java.util.EnumMap)389 Map (java.util.Map)73 ArrayList (java.util.ArrayList)70 List (java.util.List)61 HashMap (java.util.HashMap)60 Test (org.junit.Test)46 IOException (java.io.IOException)38 Collection (java.util.Collection)35 DecodeHintType (com.google.zxing.DecodeHintType)30 HashSet (java.util.HashSet)26 Set (java.util.Set)26 EncodeHintType (com.google.zxing.EncodeHintType)17 File (java.io.File)17 BitMatrix (com.google.zxing.common.BitMatrix)15 BookID (biblemulticonverter.data.BookID)14 Iterator (java.util.Iterator)14 URL (java.net.URL)12 TreeMap (java.util.TreeMap)12 Header (com.jsql.model.bean.util.Header)10 Request (com.jsql.model.bean.util.Request)10