Search in sources :

Example 1 with ObjectFactory

use of biblemulticonverter.schema.versification.ccel.ObjectFactory in project BibleMultiConverter by schierlm.

the class CCEL method doExport.

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

Example 2 with ObjectFactory

use of biblemulticonverter.schema.versification.ccel.ObjectFactory in project BibleMultiConverter by schierlm.

the class CCEL method addMapping.

private void addMapping(RefMap refmap, String osisFrom, String osisTo) {
    RefMap.Map map = new ObjectFactory().createRefSysRefMapMap();
    map.setFrom(osisFrom);
    map.setTo(osisTo);
    refmap.getMap().add(map);
}
Also used : ObjectFactory(biblemulticonverter.schema.versification.ccel.ObjectFactory) RefMap(biblemulticonverter.schema.versification.ccel.RefSys.RefMap)

Aggregations

ObjectFactory (biblemulticonverter.schema.versification.ccel.ObjectFactory)2 RefMap (biblemulticonverter.schema.versification.ccel.RefSys.RefMap)2 BookID (biblemulticonverter.data.BookID)1 Versification (biblemulticonverter.data.Versification)1 Reference (biblemulticonverter.data.Versification.Reference)1 VersificationMapping (biblemulticonverter.data.VersificationMapping)1 RefSys (biblemulticonverter.schema.versification.ccel.RefSys)1 Alias (biblemulticonverter.schema.versification.ccel.RefSys.Alias)1 OsisID (biblemulticonverter.schema.versification.ccel.RefSys.OsisIDs.OsisID)1 ArrayList (java.util.ArrayList)1 EnumMap (java.util.EnumMap)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 JAXBContext (javax.xml.bind.JAXBContext)1 Marshaller (javax.xml.bind.Marshaller)1 Transformer (javax.xml.transform.Transformer)1 DOMSource (javax.xml.transform.dom.DOMSource)1 StreamResult (javax.xml.transform.stream.StreamResult)1 Document (org.w3c.dom.Document)1