use of biblemulticonverter.data.BookID 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.data.BookID in project BibleMultiConverter by schierlm.
the class MobiPocket method writeVerse.
private void writeVerse(BufferedWriter bw, FormattedText v, final Bible bb, final String versePrefix, final String lexiconName) throws IOException {
final String lineSeparator = System.getProperty("line.separator");
v.accept(new AbstractHTMLVisitor(bw, "</p>" + lineSeparator) {
@Override
public Visitor<IOException> visitHeadline(int depth) throws IOException {
int level = depth < 3 ? depth + 3 : 6;
writer.write("<h" + level + ">");
pushSuffix("</h" + level + ">" + lineSeparator);
return this;
}
@Override
public void visitStart() throws IOException {
if (suffixStack.size() == 1) {
writer.write("<p>" + versePrefix);
}
}
@Override
protected String getNextFootnoteTarget() {
return "#" + chapref + "f" + (footNoteCount + 1);
}
@Override
public Visitor<IOException> visitFootnote() throws IOException {
final Writer outerWriter = writer;
return new AbstractHTMLVisitor(new StringWriter(), "") {
@Override
public void visitLineBreak(LineBreakKind kind) throws IOException {
writer.append("<br>");
}
@Override
public Visitor<IOException> visitFootnote() throws IOException {
throw new IllegalStateException("Footnote in footnote");
}
@Override
public Visitor<IOException> visitGrammarInformation(int[] strongs, String[] rmac, int[] sourceIndices) throws IOException {
pushSuffix("");
return this;
}
@Override
public FormattedText.Visitor<IOException> visitDictionaryEntry(String dictionary, String entry) throws IOException {
writer.write("<a href=\"oeb:redirect?title=" + lexiconName + "#" + entry + "\">");
pushSuffix("</a>");
return this;
}
@Override
public void visitRawHTML(RawHTMLMode mode, String raw) throws IOException {
if (mode != RawHTMLMode.ONLINE)
writer.write(raw);
}
@Override
public Visitor<IOException> visitVariationText(String[] variations) throws IOException {
throw new UnsupportedOperationException("Variations not supported");
}
@Override
public Visitor<IOException> visitCrossReference(String bookAbbr, BookID book, int firstChapter, String firstVerse, int lastChapter, String lastVerse) throws IOException {
if (checkBXR(bookRef(bookAbbr, book), lastChapter)) {
writer.write("<a href=\"oeb:redirect?title=BibleNavigation#b" + bookRef(bookAbbr, book) + "c" + firstChapter + "\">");
pushSuffix("</a>");
} else {
pushSuffix("");
}
return this;
}
@Override
public boolean visitEnd() throws IOException {
super.visitEnd();
if (suffixStack.size() == 0) {
if (writer.toString().startsWith(FormattedText.XREF_MARKER)) {
// this is a cross reference!
if (crossRefs.length() > 0)
crossRefs.append("<br>");
String xref = writer.toString().substring(FormattedText.XREF_MARKER.length()).trim();
if (!xref.startsWith("<b>"))
xref = versePrefix + xref;
crossRefs.append(xref);
} else {
footNoteCount++;
outerWriter.write("<sup><a name=\"" + chapref + "ft" + footNoteCount + "\" href=\"#" + chapref + "f" + footNoteCount + "\">" + footNoteCount + "</a></sup>");
if (footNotes.length() > 0)
footNotes.append("<br>");
footNotes.append("<sup><a name=\"" + chapref + "f" + footNoteCount + "\" href=\"#" + chapref + "ft" + footNoteCount + "\">" + footNoteCount + "</a></sup> ");
footNotes.append(writer.toString());
}
}
return false;
}
@Override
public Visitor<IOException> visitHeadline(int depth) throws IOException {
throw new IOException("Headline inside footnote");
}
};
}
@Override
public FormattedText.Visitor<IOException> visitCrossReference(String bookAbbr, BookID book, int firstChapter, String firstVerse, int lastChapter, String lastVerse) throws IOException {
if (checkBXR(bookRef(bookAbbr, book), lastChapter)) {
writer.write("<a href=\"oeb:redirect?title=BibleNavigation#b" + bookRef(bookAbbr, book) + "c" + firstChapter + "\">");
pushSuffix("</a>");
} else {
pushSuffix("");
}
return this;
}
@Override
public void visitLineBreak(LineBreakKind kind) throws IOException {
writer.write("<br>");
}
@Override
public Visitor<IOException> visitGrammarInformation(int[] strongs, String[] rmac, int[] sourceIndices) throws IOException {
pushSuffix("");
return this;
}
@Override
public FormattedText.Visitor<IOException> visitDictionaryEntry(String dictionary, String entry) throws IOException {
writer.write("<a href=\"oeb:redirect?title=" + lexiconName + "#" + entry + "\">");
pushSuffix("</a>");
return this;
}
@Override
public Visitor<IOException> visitVariationText(String[] variations) throws IOException {
throw new UnsupportedOperationException("Variations not supported");
}
});
}
use of biblemulticonverter.data.BookID in project BibleMultiConverter by schierlm.
the class MobiPocket method doExport.
@Override
public void doExport(Bible bible, String... exportArgs) throws Exception {
footNotes.setLength(0);
crossRefs.setLength(0);
footNoteCount = 0;
chapref = "";
File exportFile = new File(exportArgs[0]);
String title = bible.getName();
File directory = exportFile.getParentFile();
String filename = exportFile.getName();
try (final BufferedWriter opfw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(directory, filename + ".opf")), "UTF-8"))) {
opfw.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" + "<package unique-identifier=\"uid\">\r\n" + " <metadata>\r\n" + " <dc-metadata xmlns:dc=\"http://purl.org/metadata/dublin_core\" xmlns:oebpackage=\"http://openebook.org/namespaces/oeb-package/1.0/\">\r\n" + " <dc:Title>" + TITLEPREFIX + title + "</dc:Title>\r\n" + " <dc:Language>" + LANGUAGE + "</dc:Language>\r\n" + " </dc-metadata>\r\n" + " <x-metadata>\r\n" + " <DatabaseName>Bible" + filename + "</DatabaseName>\r\n" + " <output encoding=\"Windows-1252\"></output>\r\n" + " </x-metadata>\r\n" + " </metadata>\r\n" + " <manifest>\r\n" + " <item id=\"onlyfile\" media-type=\"text/x-oeb1-document\" href=\"" + filename + ".html\"></item>\r\n" + " </manifest>\r\n" + " <spine>\r\n" + " <itemref idref=\"onlyfile\" />\r\n" + " </spine>\r\n" + " <tours></tours>\r\n" + " <guide></guide>\r\n" + "</package>\r\n" + "");
}
boolean isDictionary = false;
for (Book bk : bible.getBooks()) {
if (bk.getId() == BookID.DICTIONARY_ENTRY) {
isDictionary = true;
break;
} else if (bk.getId().getZefID() > 0) {
isDictionary = false;
break;
}
}
try (final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(directory, filename + ".html")), "UTF-8"))) {
bw.write("<html><head>");
bw.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
bw.write("<style type=\"text/css\">body {font-family: Verdana, Arial, Helvetica, sans-serif}</style>");
bw.write("<title>" + TITLEPREFIX + title + "</title></head><body>");
bw.newLine();
bw.write("<h1>" + title + "</h1>");
if (isDictionary) {
bw.write("<a onclick=\"index_search()\">Suchen</a><br>");
}
bw.write("<h2>" + TOC + "</h2>");
bw.newLine();
List<String> books = new ArrayList<String>();
Map<String, Integer> maxChapter = new HashMap<String, Integer>();
Map<String, BookID> bookIDs = new HashMap<String, BookID>();
for (Book bk : bible.getBooks()) {
books.add(bk.getAbbr());
if (isDictionary) {
bw.write("<p><a href=\"#" + bk.getAbbr() + "\">" + bk.getLongName() + "</a></p>");
} else {
bw.write("<p><a href=\"#b" + bookRef(bk) + "\">" + bk.getLongName() + " (" + bk.getAbbr() + ")</a></p>");
}
bw.newLine();
maxChapter.put(bk.getAbbr(), bk.getChapters().size());
bookIDs.put(bk.getAbbr(), bk.getId());
}
try (final BufferedWriter bxw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(directory, filename + ".bxr")), "UTF-8"))) {
bxw.write(title);
bxw.newLine();
for (String bk : books) {
bxw.write(bk + "|" + bookRef(bk, bookIDs.get(bk)) + "|" + maxChapter.get(bk));
bxw.newLine();
}
}
bxrs.add(new MobiPocketBXR(filename, new File(directory, filename + ".bxr")));
final String lexiconName = (isDictionary ? "Bible" : "BibleDict") + filename;
for (Book bk : bible.getBooks()) {
if (bk.getId() == BookID.DICTIONARY_ENTRY) {
bw.write("<mbp:pagebreak><idx:entry>");
bw.newLine();
bw.write("<h2><a name=\"" + bk.getAbbr() + "\" external=\"yes\"><idx:orth>" + bk.getLongName() + "</idx:orth></a></h2>");
bw.newLine();
bw.write("<p>");
bw.newLine();
writeVerse(bw, bk.getChapters().get(0).getProlog(), bible, "", lexiconName);
bw.write("</p>");
bw.newLine();
writeFootNotes(bw);
bw.write("</idx:entry>");
} else {
bw.write("<mbp:pagebreak>");
bw.newLine();
chapref = "b" + bookRef(bk);
bw.write("<h2><a name=\"" + chapref + "\" external=\"yes\">" + bk.getLongName() + " (" + bk.getAbbr() + ")</a> (<a href=\"oeb:redirect?title=BibleNavigation#" + chapref + "\">Navigation</a>)</h2>");
bw.newLine();
int chapter = 0;
for (Chapter ch : bk.getChapters()) {
chapter++;
chapref = "b" + bookRef(bk) + "c" + chapter;
bw.write("<h3><a name=\"" + chapref + "\" external=\"yes\">" + bk.getAbbr() + " " + chapter + "</a> (<a href=\"oeb:redirect?title=BibleNavigation#" + chapref + "\">Navigation</a>)</h3>");
bw.newLine();
if (ch.getProlog() != null) {
bw.write("<small>");
bw.newLine();
writeVerse(bw, ch.getProlog(), bible, "", lexiconName);
bw.write("</small>");
bw.newLine();
}
for (final Verse v : ch.getVerses()) {
writeVerse(bw, v, bible, "<b>" + v.getNumber() + "</b> ", lexiconName);
}
writeFootNotes(bw);
}
}
}
}
}
use of biblemulticonverter.data.BookID in project BibleMultiConverter by schierlm.
the class OSIS method parseStructuredTextElement.
private void parseStructuredTextElement(Visitor<RuntimeException> vv, Element elem) {
if (elem.getNodeName().equals("lb")) {
vv.visitLineBreak(LineBreakKind.NEWLINE);
} else if (elem.getNodeName().equals("brp")) {
vv.visitLineBreak(LineBreakKind.PARAGRAPH);
} else if (elem.getNodeName().equals("divineName")) {
parseStructuredTextChildren(vv.visitFormattingInstruction(FormattingInstructionKind.DIVINE_NAME), elem);
} else if (elem.getNodeName().equals("woj")) {
parseStructuredTextChildren(vv.visitFormattingInstruction(FormattingInstructionKind.WORDS_OF_JESUS), elem);
} else if (elem.getNodeName().equals("catchWord")) {
parseStructuredTextChildren(vv.visitCSSFormatting("osis-style: catchWord; font-style:italic;"), elem);
} else if (elem.getNodeName().equals("hi")) {
FormattingInstructionKind kind;
if (elem.getAttribute("type").equals("italic")) {
kind = FormattingInstructionKind.ITALIC;
} else if (elem.getAttribute("type").equals("bold")) {
kind = FormattingInstructionKind.BOLD;
} else {
kind = null;
printWarning("WARNING: Invalid hi type: " + elem.getAttribute("type"));
}
if (elem.getChildNodes().getLength() != 0) {
Visitor<RuntimeException> vv1 = kind == null ? vv : vv.visitFormattingInstruction(kind);
parseStructuredTextChildren(vv1, elem);
}
} else if (elem.getNodeName().equals("seg") || elem.getNodeName().equals("transChange") || elem.getNodeName().equals("rdg")) {
String css;
if (elem.getNodeName().equals("seg") && elem.getAttribute("type").equals("x-alternative")) {
css = "osis-style: alternative; color: gray;";
} else if (elem.getNodeName().equals("transChange") && elem.getAttribute("type").equals("added")) {
css = "osis-style: added; font-style:italic;";
} else if (elem.getNodeName().equals("transChange") && elem.getAttribute("type").equals("deleted")) {
css = "osis-style: deleted; text-decoration: line-through; color: gray;";
} else if (elem.getNodeName().equals("transChange") && elem.getAttribute("type").equals("amplified")) {
css = "osis-style: amplified; font-style: italic;";
} else if (elem.getNodeName().equals("transChange") && elem.getAttribute("type").isEmpty()) {
css = "osis-style: trans-change;";
} else if (elem.getNodeName().equals("rdg") && elem.getAttribute("type").equals("alternative")) {
css = "osis-style: alternative-reading; color: gray;";
} else if (elem.getNodeName().equals("rdg") && elem.getAttribute("type").equals("x-literal")) {
css = "osis-style: literal-reading; color: gray;";
} else if (elem.getNodeName().equals("rdg") && elem.getAttribute("type").equals("x-meaning")) {
css = "osis-style: meaning-reading; color: gray;";
} else if (elem.getNodeName().equals("rdg") && elem.getAttribute("type").equals("x-equivalent")) {
css = "osis-style: equivalent-reading; color: gray;";
} else if (elem.getNodeName().equals("rdg") && elem.getAttribute("type").equals("x-identity")) {
css = "osis-style: identity-reading; color: gray;";
} else if (elem.getNodeName().equals("rdg") && elem.getAttribute("type").isEmpty()) {
css = "osis-style: reading; color: gray;";
} else {
css = null;
printWarning("WARNING: Invalid " + elem.getNodeName() + " type: " + elem.getAttribute("type"));
}
if (elem.getChildNodes().getLength() != 0) {
Visitor<RuntimeException> vv1 = css == null ? vv : vv.visitCSSFormatting(css);
parseStructuredTextChildren(vv1, elem);
}
} else if (elem.getNodeName().equals("note")) {
if (elem.getAttribute("type").equals("crossReference")) {
Visitor<RuntimeException> fn = vv.visitFootnote();
fn.visitText(FormattedText.XREF_MARKER);
if (elem.getFirstChild() != null && elem.getFirstChild().getNodeName().equals("reference")) {
for (Node n = elem.getFirstChild(); n != null; n = n.getNextSibling()) {
if (n instanceof Text) {
fn.visitText(n.getTextContent());
continue;
}
Element e = (Element) n;
String[] ref = e.getAttribute("osisRef").split("\\.");
if (ref.length != 3) {
printWarning("WARNING: Invalid reference target: " + e.getAttribute("osisRef"));
fn.visitText(e.getTextContent());
continue;
}
BookID bookID = BookID.fromOsisId(ref[0]);
int ch = Integer.parseInt(ref[1]);
String vs = ref[2];
fn.visitCrossReference(ref[0], bookID, ch, vs, ch, vs).visitText(e.getTextContent());
}
} else if (elem.getTextContent().length() > 0) {
// OSIS converted from USFM contains a reference back to the verse itself
for (Node n = elem.getFirstChild(); n != null; n = n.getNextSibling()) {
if (n instanceof Element && n.getNodeName().equals("reference")) {
if (((Element) n).getAttribute("osisRef").equals(((Element) elem).getAttribute("osisRef"))) {
n = n.getPreviousSibling();
n.getParentNode().removeChild(n.getNextSibling());
}
}
}
boolean first = true;
for (String ref : elem.getTextContent().split("\\|")) {
Matcher m = XREF_PATTERN.matcher(ref);
if (!m.matches()) {
ref = ref.trim();
if (ref.startsWith("1 ") || ref.startsWith("2 ") || ref.startsWith("3 ")) {
ref = ref.substring(0, 1) + ref.substring(2);
}
m = XREF_PATTERN_2.matcher(ref);
if (m.matches()) {
try {
BookID.fromOsisId(m.group(1));
} catch (IllegalArgumentException ex) {
BookID bk = null;
for (BookID id : BookID.values()) {
if (id.getThreeLetterCode().equalsIgnoreCase(m.group(1)))
bk = id;
}
if (bk != null) {
m = XREF_PATTERN_2.matcher(bk.getOsisID() + " " + m.group(2) + "." + m.group(3));
} else {
m = XREF_PATTERN_2.matcher("");
}
}
}
}
if (!first)
fn.visitText("; ");
first = false;
if (m.matches()) {
String book = m.group(1);
BookID bookID = BookID.fromOsisId(book);
int ch = Integer.parseInt(m.group(2));
String vs = m.group(3);
fn.visitCrossReference(book, bookID, ch, vs, ch, vs).visitText(ref);
} else {
printWarning("WARNING: Malformed cross reference: " + ref);
fn.visitText(ref.replaceAll("[\r\n\t ]+", " ").trim());
}
}
} else {
printWarning("WARNING: crossReference without content");
fn.visitText("-");
}
} else if (elem.getFirstChild() != null) {
Visitor<RuntimeException> v = vv.visitFootnote();
parseStructuredTextChildren(v, elem);
}
} else if (elem.getNodeName().equals("w")) {
if (elem.getFirstChild() == null)
// skip empty w tags
return;
String src = elem.getAttribute("src");
Visitor<RuntimeException> v = vv;
int[] strong = null, idx = null;
List<Integer> strongList = new ArrayList<Integer>();
for (String lemma : elem.getAttribute("lemma").trim().split(" +")) {
if (!lemma.startsWith("strong:G") && !lemma.startsWith("strong:H"))
continue;
String rawStrong = lemma.substring(8);
if (!rawStrong.matches("0*[1-9][0-9]*(-0*[1-9][0-9]*)*")) {
printWarning("WARNING: Invalid strong dictionary entry: " + rawStrong);
continue;
}
String[] strs = rawStrong.split("-");
for (String str : strs) {
strongList.add(Integer.parseInt(str));
}
}
if (!strongList.isEmpty())
strong = strongList.stream().mapToInt(s -> s).toArray();
List<String> rmac = new ArrayList<>();
for (String morph : elem.getAttribute("morph").trim().split(" +")) {
if (morph.startsWith("robinson:")) {
String rmacCandidate = morph.substring(9);
if (Utils.compilePattern(Utils.RMAC_REGEX).matcher(rmacCandidate).matches()) {
rmac.add(rmacCandidate);
} else {
printWarning("WARNING: Invalid RMAC: " + rmacCandidate);
}
}
}
if (src.matches("[0-9]{2}( [0-9]{2})*")) {
String[] strs = src.split(" ");
idx = new int[strs.length];
for (int i = 0; i < strs.length; i++) {
idx[i] = Integer.parseInt(strs[i]);
}
}
if (strong == null && rmac.isEmpty() && idx == null) {
printWarning("INFO: Skipped <w> tag without any usable information");
} else {
v = v.visitGrammarInformation(strong, rmac.isEmpty() ? null : rmac.toArray(new String[rmac.size()]), idx);
}
parseStructuredTextChildren(v, elem);
} else if (elem.getNodeName().equals("reference")) {
String osisRef = elem.getAttribute("osisRef");
if (osisRef.contains("\u00A0")) {
printWarning("WARNING: osisRef contains Non-Breaking spaces: '" + osisRef + "'");
osisRef = osisRef.replace('\u00A0', ' ');
}
if (!osisRef.equals(osisRef.trim())) {
printWarning("WARNING: Removed whitespace from osisRef '" + osisRef + "' - replaced by '" + osisRef.trim() + "'");
osisRef = osisRef.trim();
}
Matcher fixupMatcher = Utils.compilePattern("([A-Z0-9][A-Z0-9a-z]+\\.[0-9]+\\.)([0-9]+)((?:[+-][0-9]+)+)").matcher(osisRef);
if (fixupMatcher.matches()) {
osisRef = fixupMatcher.group(1) + fixupMatcher.group(2);
for (String suffix : fixupMatcher.group(3).split("(?=[+-])")) {
if (suffix.isEmpty())
continue;
osisRef += suffix.substring(0, 1).replace('+', ' ') + fixupMatcher.group(1) + suffix.substring(1);
}
printWarning("INFO: Replaced osisRef " + elem.getAttribute("osisRef") + " by " + osisRef);
}
if (osisRefMap == null) {
osisRefMap = new Properties();
String path = System.getProperty("biblemulticonverter.osisrefmap", "");
if (!path.isEmpty()) {
try (FileInputStream fis = new FileInputStream(path)) {
osisRefMap.load(fis);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}
String mappedOsisRef = osisRefMap.getProperty(osisRef);
if (mappedOsisRef != null) {
printWarning("INFO: Replaced osisRef " + osisRef + " by " + mappedOsisRef + " (based on osisRef map)");
osisRef = mappedOsisRef;
}
if (osisRef.matches("[^ ]+ [^ ]+") && elem.getFirstChild() instanceof Text && elem.getFirstChild().getNextSibling() == null) {
String value = elem.getTextContent();
int lastPos = value.lastIndexOf('.');
if (lastPos != -1) {
Element newElem = elem.getOwnerDocument().createElement("reference");
newElem.setAttribute("osisRef", osisRef.split(" ")[0]);
newElem.appendChild(elem.getOwnerDocument().createTextNode(value.substring(0, lastPos)));
parseStructuredTextElement(vv, newElem);
vv.visitText(".");
newElem = elem.getOwnerDocument().createElement("reference");
newElem.setAttribute("osisRef", osisRef.split(" ")[1]);
newElem.appendChild(elem.getOwnerDocument().createTextNode(value.substring(lastPos + 1)));
parseStructuredTextElement(vv, newElem);
return;
}
}
Visitor<RuntimeException> v = vv;
if (osisRef.matches("[A-Z0-9][A-Z0-9a-z]+\\.[0-9]+\\.[0-9]+")) {
String[] osisRefParts = osisRef.split("\\.");
int chapter = Integer.parseInt(osisRefParts[1]);
try {
v = v.visitCrossReference(osisRefParts[0], BookID.fromOsisId(osisRefParts[0]), chapter, osisRefParts[2], chapter, osisRefParts[2]);
} catch (IllegalArgumentException ex) {
printWarning("WARNING: " + ex.getMessage());
}
} else if (osisRef.matches("([A-Z0-9][A-Z0-9a-z]+)\\.[0-9]+\\.[0-9]+-\\1\\.[0-9]+\\.[0-9]+")) {
String[] osisRefParts = osisRef.split("[.-]");
int firstChapter = Integer.parseInt(osisRefParts[1]);
int lastChapter = Integer.parseInt(osisRefParts[4]);
try {
v = v.visitCrossReference(osisRefParts[0], BookID.fromOsisId(osisRefParts[0]), firstChapter, osisRefParts[2], lastChapter, osisRefParts[5]);
} catch (IllegalArgumentException ex) {
printWarning("WARNING: " + ex.getMessage());
}
} else {
printWarning("WARNING: Unsupported osisRef: " + osisRef);
}
parseStructuredTextChildren(v, elem);
} else if (elem.getNodeName().equals("variation")) {
parseStructuredTextChildren(vv.visitVariationText(new String[] { elem.getAttribute("name") }), elem);
} else {
printWarning("WARNING: invalid structured element level tag: " + elem.getNodeName());
}
}
use of biblemulticonverter.data.BookID in project BibleMultiConverter by schierlm.
the class OnLineBible method doExport.
@Override
public void doExport(Bible bible, String... exportArgs) throws Exception {
String outFile = exportArgs[0], namesFile = null;
;
boolean includeStrongs = false;
if (exportArgs.length > 1) {
if (exportArgs[1].equals("IncludeStrongs")) {
includeStrongs = true;
} else {
namesFile = exportArgs[1];
}
if (exportArgs.length > 2 && exportArgs[2].equals("IncludeStrongs")) {
includeStrongs = true;
}
}
Set<BookID> supportedBooks = EnumSet.noneOf(BookID.class);
for (BookMeta bm : BOOK_META) {
supportedBooks.add(bm.id);
}
Map<BookID, Book> bookMap = new EnumMap<>(BookID.class);
for (Book book : bible.getBooks()) {
if (supportedBooks.contains(book.getId()))
bookMap.put(book.getId(), book);
else
System.out.println("WARNING: Skipping book " + book.getAbbr());
}
if (namesFile != null) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(namesFile))) {
for (BookMeta bm : BOOK_META) {
Book bk = bookMap.get(bm.id);
if (bk != null) {
bw.write(bk.getShortName() + " " + bm.abbr);
bw.newLine();
}
}
}
}
try (BufferedWriter bw = new BufferedWriter(new FileWriter(outFile))) {
for (BookMeta bm : BOOK_META) {
String prefix = "";
if (bm.id == BookID.BOOK_Matt && includeStrongs) {
prefix = "0 ";
}
Book bk = bookMap.remove(bm.id);
int[] verseCount = StandardVersification.KJV.getVerseCount(bm.id);
for (int i = 0; i < verseCount.length; i++) {
Chapter ch = bk != null && i < bk.getChapters().size() ? bk.getChapters().get(i) : null;
int maxVerse = verseCount[i];
BitSet allowedNumbers = new BitSet(maxVerse + 1);
allowedNumbers.set(1, maxVerse + 1);
List<VirtualVerse> vvs = ch == null ? null : ch.createVirtualVerses(null);
for (int vnum = 1; vnum <= verseCount[i]; vnum++) {
bw.write("$$$ " + bm.abbr + " " + (i + 1) + ":" + vnum + " ");
bw.newLine();
StringBuilder text = new StringBuilder(prefix);
if (vvs != null) {
for (VirtualVerse vv : vvs) {
if (vv.getNumber() == vnum) {
for (Headline h : vv.getHeadlines()) {
text.append(" {\\$");
h.accept(new OnLineBibleVisitor(text, includeStrongs));
text.append("\\$} ");
}
for (Verse v : vv.getVerses()) {
if (!v.getNumber().equals("" + vv.getNumber())) {
text.append("\\\\(" + v.getNumber() + ")\\\\ ");
}
v.accept(new OnLineBibleVisitor(text, includeStrongs));
}
}
}
}
if (text.length() > 0) {
bw.write(text.toString().replaceAll(" +", " "));
bw.newLine();
}
prefix = "";
}
}
}
}
if (!bookMap.isEmpty())
throw new IllegalStateException("Remaining books: " + bookMap.keySet());
}
Aggregations