use of biblemulticonverter.data.Chapter in project BibleMultiConverter by schierlm.
the class StrippedDiffable method optimizeFormatting.
private void optimizeFormatting(Bible bible, Map<String, String[]> mappings) {
CountFormattingVisitor oldCount = new CountFormattingVisitor();
CountFormattingVisitor newCount = new CountFormattingVisitor();
for (Book book : bible.getBooks()) {
for (Chapter chap : book.getChapters()) {
if (chap.getProlog() != null) {
chap.getProlog().accept(oldCount);
FormattedText newProlog = new FormattedText();
chap.getProlog().accept(new OptimizeFormattingVisitor(newProlog.getAppendVisitor(), mappings));
newProlog.finished();
newProlog.accept(newCount);
chap.setProlog(newProlog);
}
for (int j = 0; j < chap.getVerses().size(); j++) {
Verse v = chap.getVerses().get(j);
v.accept(oldCount);
Verse nv = new Verse(v.getNumber());
v.accept(new OptimizeFormattingVisitor(nv.getAppendVisitor(), mappings));
nv.finished();
nv.accept(newCount);
chap.getVerses().set(j, nv);
}
}
}
System.out.println("Formatting instructions before optimizing:");
oldCount.printSummary();
System.out.println();
System.out.println("Formatting instructions after optimizing:");
newCount.printSummary();
}
use of biblemulticonverter.data.Chapter in project BibleMultiConverter by schierlm.
the class TheWord method doImport.
@Override
public Bible doImport(File inputFile) throws Exception {
warningCount = 0;
Bible result = new Bible("Imported From theWord");
boolean hasOT = true, hasNT = true;
if (inputFile.getName().toLowerCase().endsWith(".ot")) {
hasNT = false;
} else if (inputFile.getName().toLowerCase().endsWith(".nt")) {
hasOT = false;
}
try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile), "UTF-8"))) {
for (BookID bid : BOOK_ORDER) {
if ((bid.isNT() && !hasNT) || (!bid.isNT() && !hasOT))
continue;
Book bk = new Book(bid.getOsisID(), bid, bid.getEnglishName(), bid.getEnglishName());
int[] verseCount = StandardVersification.KJV.getVerseCount(bid);
for (int cnumber = 1; cnumber <= verseCount.length; cnumber++) {
Chapter ch = new Chapter();
boolean hasVerses = false;
int maxVerse = verseCount[cnumber - 1];
for (int vnumber = 1; vnumber <= maxVerse; vnumber++) {
String line = br.readLine();
if (line.startsWith("\uFEFF"))
line = line.substring(1);
if (line.equals("- - -") || line.trim().length() == 0)
continue;
line = line.replaceAll(" +", " ").trim();
hasVerses = true;
Verse v = new Verse("" + vnumber);
if (line.contains("<WH") || line.contains("<WG")) {
Matcher m = Pattern.compile("(<FI>[^<>]*<Fi>|<FO>[^<>]*<Fo>|[^<> ]*)((<W[GH][0-9]+>)+)").matcher(line.replaceFirst("^(<W[GH][0-9]+x>)+", ""));
StringBuffer sb = new StringBuffer();
while (m.find()) {
String word = m.group(1);
String tags = m.group(2);
m.appendReplacement(sb, "");
sb.append("<S%" + tags.substring(3, tags.length() - 1).replaceAll("><W[GH]", "%") + ">");
sb.append(word);
sb.append("<s%>");
}
m.appendTail(sb);
line = sb.toString();
}
int pos = parseLine(v.getAppendVisitor(), line, 0, null);
if (pos != line.length())
v.getAppendVisitor().visitText(line.substring(pos));
v.finished();
ch.getVerses().add(v);
}
if (hasVerses) {
while (bk.getChapters().size() < cnumber - 1) {
bk.getChapters().add(new Chapter());
}
if (bk.getChapters().size() != cnumber - 1)
throw new RuntimeException();
bk.getChapters().add(ch);
}
}
if (bk.getChapters().size() > 0)
result.getBooks().add(bk);
}
}
return result;
}
use of biblemulticonverter.data.Chapter in project BibleMultiConverter by schierlm.
the class VersificationMappedDiffable method doExport.
@Override
public void doExport(Bible bible, String... exportArgs) throws Exception {
String outputFile = exportArgs[0];
VersificationSet vs = new VersificationSet(new File(exportArgs[1]));
VersificationMapping vm = vs.findMapping(exportArgs[2]);
boolean dropUnmapped = false, showNumbers = false, addTags = false;
;
for (int i = 3; i < exportArgs.length; i++) {
if (exportArgs[i].equals("DropUnmapped"))
dropUnmapped = true;
else if (exportArgs[i].equals("ShowNumbers"))
showNumbers = true;
else if (exportArgs[i].equals("AddTags"))
addTags = true;
else
throw new IllegalArgumentException("Unsupported option: " + exportArgs[i]);
}
Map<BookID, String> abbrMap = new EnumMap<>(BookID.class);
for (Book book : bible.getBooks()) {
abbrMap.put(book.getId(), book.getAbbr());
}
Bible newBible = new Bible(bible.getName());
Map<BookID, Book> newBooks = new EnumMap<>(BookID.class);
for (Book book : bible.getBooks()) {
if (book.getId().getZefID() < 1) {
// metadata book, introduction or appendix
newBible.getBooks().add(book);
continue;
}
int cnumber = 0;
for (Chapter chap : book.getChapters()) {
cnumber++;
if (chap.getProlog() != null && chap.getVerses().isEmpty()) {
System.out.println("WARNING: Prolog for " + book.getAbbr() + " " + cnumber + " got lost as chapter contains no verses.");
}
for (int j = 0; j < chap.getVerses().size(); j++) {
Verse oldVerse = chap.getVerses().get(j);
Reference ref = new Reference(book.getId(), cnumber, oldVerse.getNumber()), newRef;
List<Reference> newRefs = vm.getMapping(ref);
if ((newRefs == null || newRefs.isEmpty()) && dropUnmapped) {
if (j == 0 && chap.getProlog() != null) {
System.out.println("WARNING: Prolog for " + book.getAbbr() + " " + cnumber + " got lost as first verse of it is unmapped.");
}
continue;
}
if (newRefs == null || newRefs.contains(ref) || newRefs.isEmpty())
newRef = ref;
else
newRef = newRefs.get(0);
if (!newBooks.containsKey(newRef.getBook())) {
Book newBook = null;
for (Book oldBook : bible.getBooks()) {
if (oldBook.getId() == newRef.getBook()) {
newBook = new Book(oldBook.getAbbr(), newRef.getBook(), oldBook.getShortName(), oldBook.getLongName());
break;
}
}
if (newBook == null)
newBook = new Book(newRef.getBook().getOsisID(), newRef.getBook(), newRef.getBook().getEnglishName(), newRef.getBook().getEnglishName());
newBooks.put(newRef.getBook(), newBook);
newBible.getBooks().add(newBook);
}
Book newBook = newBooks.get(newRef.getBook());
while (newBook.getChapters().size() < newRef.getChapter()) newBook.getChapters().add(new Chapter());
Chapter newChapter = newBook.getChapters().get(newRef.getChapter() - 1);
if (j == 0 && chap.getProlog() != null) {
FormattedText newProlog = new FormattedText();
if (newChapter.getProlog() != null) {
newChapter.getProlog().accept(newProlog.getAppendVisitor());
newProlog.getAppendVisitor().visitLineBreak(LineBreakKind.PARAGRAPH);
}
chap.getProlog().accept(new MapXrefVisitor(newProlog.getAppendVisitor(), vm, dropUnmapped, abbrMap));
newProlog.finished();
newChapter.setProlog(newProlog);
}
Verse newVerse = null;
for (Verse v : newChapter.getVerses()) {
if (v.getNumber().equals(newRef.getVerse())) {
newVerse = v;
break;
}
}
boolean needSpace = true;
if (newVerse == null) {
newVerse = new Verse(newRef.getVerse());
newChapter.getVerses().add(newVerse);
needSpace = false;
}
if (needSpace || !ref.equals(newRef)) {
Visitor<RuntimeException> v = newVerse.getAppendVisitor();
if (addTags)
v = v.visitExtraAttribute(ExtraAttributePriority.KEEP_CONTENT, "v11n", "origverse", ref.getBook().getOsisID() + "--" + ref.getChapter() + "--" + ref.getVerse());
if (needSpace)
v.visitText(" ");
if (showNumbers) {
String verseNumber;
if (!ref.getBook().equals(newRef.getBook())) {
verseNumber = ref.getBook().getOsisID() + " " + ref.getChapter() + ":" + ref.getVerse();
} else if (ref.getChapter() != newRef.getChapter()) {
verseNumber = ref.getChapter() + ":" + ref.getVerse();
} else {
verseNumber = ref.getVerse();
}
v.visitFormattingInstruction(FormattingInstructionKind.BOLD).visitText("(" + verseNumber + ")");
v.visitText(" ");
}
}
oldVerse.accept(new MapXrefVisitor(newVerse.getAppendVisitor(), vm, dropUnmapped, abbrMap));
}
}
}
for (Book bk : newBible.getBooks()) {
for (Chapter ch : bk.getChapters()) {
for (Verse v : ch.getVerses()) {
v.finished();
}
}
}
bible = newBible;
new Diffable().doExport(bible, new String[] { outputFile });
}
use of biblemulticonverter.data.Chapter in project BibleMultiConverter by schierlm.
the class YCHPalmBible method doExport.
@Override
public void doExport(Bible bible, String... exportArgs) throws Exception {
String filename = exportArgs[0];
String description = bible.getName();
MetadataBook metadata = bible.getMetadataBook();
if (metadata != null) {
String metaDescription = bible.getMetadataBook().getValue(MetadataBookKey.description);
if (metaDescription != null)
description = metaDescription;
}
try (final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), "windows-1252"))) {
bw.write("<PARSERINFO ENCODE=\"Cp1252\" WORDTYPE=\"SPCSEP\">");
bw.newLine();
bw.write("<BIBLE NAME=\"" + bible.getName() + "\" INFO=\"" + description + "\">");
bw.newLine();
Visitor<IOException> contentVisitor = new FormattedText.VisitorAdapter<IOException>(null) {
@Override
public void visitVerseSeparator() throws IOException {
// strip
}
@Override
public void visitText(String text) throws IOException {
bw.write(text);
}
@Override
public Visitor<IOException> visitFormattingInstruction(FormattingInstructionKind kind) throws IOException {
return this;
}
@Override
public Visitor<IOException> visitCSSFormatting(String css) throws IOException {
return this;
}
@Override
public void visitLineBreak(LineBreakKind kind) throws IOException {
bw.write(" ");
}
};
for (Book bk : bible.getBooks()) {
int zefID = bk.getId().getZefID();
if (zefID < 1 || zefID >= PALM_BOOK_NUMBERS.length || PALM_BOOK_NUMBERS[zefID] == 0) {
System.out.println("WARNING: Skipping unsupported book " + bk.getAbbr() + " (" + bk.getId().getOsisID() + ")");
continue;
}
bw.write("<BOOK NAME=\"" + bk.getShortName() + "\" NUMBER=\"" + PALM_BOOK_NUMBERS[bk.getId().getZefID()] + "\" SHORTCUT=\"" + bk.getAbbr() + "\">");
bw.newLine();
String longtitle = bk.getLongName();
int chapter = 0, verse;
for (Chapter chap : bk.getChapters()) {
chapter++;
if (chap.getProlog() != null)
System.out.println("WARNING: Skipping prolog (prologs not supported)!");
verse = 1;
String chaptext = CHAPTER_NAME + " " + chapter;
bw.write("<CHAPTER>");
bw.newLine();
for (VirtualVerse v : chap.createVirtualVerses()) {
while (v.getNumber() > verse) {
bw.write("<VERSE></VERSE>");
bw.newLine();
verse++;
}
if (v.getNumber() != verse)
throw new RuntimeException("Verse is " + v.getNumber() + ", should be " + verse);
boolean needVersText = false;
bw.write("<VERSE>");
if (longtitle != null) {
bw.write("<BOOKTEXT>" + longtitle);
longtitle = null;
needVersText = true;
}
if (chaptext != null) {
bw.write("<CHAPTEXT>" + chaptext);
chaptext = null;
needVersText = true;
}
for (Headline hl : v.getHeadlines()) {
bw.write("<DESCTEXT>");
hl.accept(contentVisitor);
needVersText = true;
}
if (needVersText)
bw.write("<VERSTEXT>");
for (Verse vv : v.getVerses()) {
if (!vv.getNumber().equals("" + v.getNumber())) {
bw.write("{" + vv.getNumber() + "} ");
}
vv.accept(contentVisitor);
}
bw.write("</VERSE>");
verse++;
bw.newLine();
}
bw.write("</CHAPTER>");
bw.newLine();
}
bw.write("</BOOK>");
bw.newLine();
}
bw.write("</BIBLE>");
bw.newLine();
}
}
use of biblemulticonverter.data.Chapter in project BibleMultiConverter by schierlm.
the class ZefDic method parseBible.
protected Bible parseBible(Dictionary doc) throws Exception {
Bible result = new Bible(doc.getType().toString() + "@" + doc.getRefbible());
MetadataBook metadata = new MetadataBook();
if (doc.getDicversion() != null) {
metadata.setValue(MetadataBookKey.version, doc.getDicversion());
}
if (doc.getRevision() != null) {
metadata.setValue(MetadataBookKey.revision, doc.getRevision());
}
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());
int counter = 0;
for (TItem item : doc.getItem()) {
String id = item.getId();
String internalId = id;
if (!id.matches(Utils.BOOK_ABBR_REGEX))
internalId = "L" + (++counter);
Book bk = new Book(internalId, BookID.DICTIONARY_ENTRY, id, id);
result.getBooks().add(bk);
FormattedText prolog = new FormattedText();
bk.getChapters().add(new Chapter());
bk.getChapters().get(0).setProlog(prolog);
Visitor<RuntimeException> vv = prolog.getAppendVisitor();
String strongId = item.getStrongId();
if (strongId != null) {
Visitor<RuntimeException> vvvv = vv.visitExtraAttribute(ExtraAttributePriority.KEEP_CONTENT, "zefdic", "itemfield", "strongid");
vvvv.visitFormattingInstruction(FormattingInstructionKind.BOLD).visitText("Strong-ID: ");
vvvv.visitText(strongId);
vv.visitLineBreak(LineBreakKind.PARAGRAPH);
}
for (Object s : item.getContent()) {
if (s instanceof String) {
if (((String) s).trim().length() > 0)
throw new RuntimeException((String) s);
} else if (s instanceof JAXBElement) {
Object v = ((JAXBElement<?>) s).getValue();
if (!((JAXBElement<?>) s).getName().getNamespaceURI().equals("")) {
throw new RuntimeException(((JAXBElement<?>) s).getName().getNamespaceURI());
}
String nn = ((JAXBElement<?>) s).getName().getLocalPart();
if (v instanceof TParagraph && nn.equals("description")) {
TParagraph para = (TParagraph) v;
if (para.getId() != null)
throw new RuntimeException(para.getId());
Visitor<RuntimeException> vvv = vv.visitExtraAttribute(ExtraAttributePriority.KEEP_CONTENT, "zefdic", "field", "description");
for (Object oo : para.getContent()) {
if (oo instanceof String) {
vvv.visitText(normalize((String) oo, false));
} else if (oo instanceof JAXBElement) {
Object ovv = ((JAXBElement<?>) oo).getValue();
if (!((JAXBElement<?>) oo).getName().getNamespaceURI().equals("")) {
throw new RuntimeException(((JAXBElement<?>) oo).getName().getNamespaceURI());
}
String nnn = ((JAXBElement<?>) oo).getName().getLocalPart();
if (nnn.equals("br") && ovv instanceof String) {
if (((String) ovv).trim().length() > 0)
throw new RuntimeException((String) ovv);
vvv.visitLineBreak(LineBreakKind.NEWLINE);
} else if (nnn.equals("title") && ovv instanceof String) {
vvv.visitHeadline(2).visitText(((String) ovv).trim().replaceAll(" +", " "));
} else if (nnn.equals("sub") && ovv instanceof String) {
vvv.visitFormattingInstruction(FormattingInstructionKind.SUBSCRIPT).visitText(normalize((String) ovv, false));
} else if (nnn.equals("reflink") && ovv instanceof RefLinkType) {
RefLinkType r = (RefLinkType) ovv;
if (r.getTarget() != null || r.getContent() == null || r.getContent().length() > 0)
System.out.println("WARNING: Unsupported reflink attributes " + r.getTarget() + "|" + r.getContent());
if (r.getMscope() == null)
r.setMscope(r.getContent());
vv.visitDictionaryEntry("reflink", r.getMscope().replace(';', '-'));
} else if (nnn.equals("see") && ovv instanceof SeeType) {
SeeType see = (SeeType) ovv;
if (see.getTarget() != null && !see.getTarget().equals("x-self"))
throw new RuntimeException(see.getTarget());
vv.visitDictionaryEntry("dict", see.getContent());
} else if (nnn.equals("bib_link") && ovv instanceof BibLinkType) {
BibLinkType bl = (BibLinkType) ovv;
Visitor<RuntimeException> fn = vv.visitFootnote();
fn.visitText(FormattedText.XREF_MARKER);
BookID bid = BookID.fromZefId(Integer.parseInt(bl.getBn()));
int chapter = Integer.parseInt(bl.getCn1());
fn.visitCrossReference(bid.getOsisID(), bid, chapter, bl.getVn1(), chapter, bl.getVn1()).visitText(bid.getOsisID() + " " + chapter + ":" + bl.getVn1());
} else if (nnn.equals("greek") && ovv instanceof String) {
vv.visitExtraAttribute(ExtraAttributePriority.KEEP_CONTENT, "zefdic", "tag", "greek").visitText(normalize((String) ovv, false));
} else if (nnn.equals("em") && ovv instanceof String) {
vvv.visitFormattingInstruction(FormattingInstructionKind.ITALIC).visitText(normalize((String) ovv, false));
} else if (nnn.equals("strong") && ovv instanceof String) {
vvv.visitFormattingInstruction(FormattingInstructionKind.BOLD).visitText(normalize((String) ovv, false));
} else if (nnn.equals("q") && ovv instanceof String) {
vv.visitExtraAttribute(ExtraAttributePriority.KEEP_CONTENT, "zefdic", "tag", "q").visitText(normalize((String) ovv, false));
} else {
throw new RuntimeException(nnn + "/" + ovv.getClass().getName());
}
} else {
throw new RuntimeException(oo.getClass().getName());
}
}
vv.visitLineBreak(LineBreakKind.PARAGRAPH);
} else if (v instanceof String || v instanceof MyAnyType) {
Visitor<RuntimeException> vvvv;
boolean addParagraph = false;
if (nn.equals("title")) {
vvvv = vv.visitHeadline(1);
} else if (nn.equals("strong_id")) {
vvvv = vv.visitExtraAttribute(ExtraAttributePriority.KEEP_CONTENT, "zefdic", "field", "strongid");
vvvv.visitFormattingInstruction(FormattingInstructionKind.BOLD).visitText("Strong-ID: ");
addParagraph = true;
} else if (nn.equals("transliteration")) {
vvvv = vv.visitExtraAttribute(ExtraAttributePriority.KEEP_CONTENT, "zefdic", "field", "transliteration");
vvvv.visitFormattingInstruction(FormattingInstructionKind.BOLD).visitText("Transliteration: ");
addParagraph = true;
} else if (nn.equals("pronunciation")) {
vvvv = vv.visitExtraAttribute(ExtraAttributePriority.KEEP_CONTENT, "zefdic", "field", "pronunciation");
vvvv.visitFormattingInstruction(FormattingInstructionKind.BOLD).visitText("Pronunciation: ");
addParagraph = true;
} else {
throw new RuntimeException(nn);
}
if (v instanceof MyAnyType) {
parseElement(vvvv, (MyAnyType) v);
} else if (v instanceof String) {
vvvv.visitText(normalize((String) v, false));
}
if (addParagraph) {
vv.visitLineBreak(LineBreakKind.PARAGRAPH);
}
} else {
throw new RuntimeException(nn + "/" + v.getClass().getName());
}
} else {
throw new RuntimeException("" + s.getClass());
}
}
prolog.trimWhitespace();
prolog.finished();
}
return result;
}
Aggregations