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));
}
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);
}
Aggregations