use of biblemulticonverter.data.Book in project BibleMultiConverter by schierlm.
the class RoundtripHTML method doImport.
@Override
public Bible doImport(File inputDir) throws Exception {
Bible bible;
// metadata
try (BufferedReader br = createReader(inputDir, "metadata.js")) {
String line = br.readLine();
br.readLine();
bible = new Bible(line.substring(13, line.length() - 2).replace("\\\"", "\"").replace("\\\\", "\\"));
Map<String, Object> fieldMap = new HashMap<String, Object>();
while ((line = br.readLine()) != null) {
if (line.startsWith("}")) {
Book bk = new Book((String) fieldMap.get("abbr"), BookID.fromOsisId((String) fieldMap.get("osis")), (String) fieldMap.get("short"), (String) fieldMap.get("long"));
for (int i = 0; i < (Integer) fieldMap.get("chapters"); i++) {
bk.getChapters().add(new Chapter());
}
bible.getBooks().add(bk);
continue;
}
int pos = line.indexOf(":");
String key = line.substring(0, pos);
String value = line.substring(pos + 1);
if (value.endsWith(","))
value = value.substring(0, value.length() - 1);
if (value.startsWith("\"") && value.endsWith("\"")) {
fieldMap.put(key, value.substring(1, value.length() - 1).replace("\\\"", "\"").replace("\\\\", "\\"));
} else if (value.equals("true") || value.equals("false")) {
fieldMap.put(key, Boolean.parseBoolean(value));
} else {
fieldMap.put(key, Integer.parseInt(value));
}
}
}
// chapters
for (Book bk : bible.getBooks()) {
int cnumber = 0;
for (Chapter ch : bk.getChapters()) {
cnumber++;
try (BufferedReader br = createReader(inputDir, getTypeDir(bk.getId()) + "/" + bk.getAbbr() + "_" + cnumber + ".html")) {
String line;
List<FormattedText.Visitor<RuntimeException>> footnotes = new ArrayList<>();
while ((line = br.readLine()) != null) {
if (line.equals("<div class=\"biblehtmlcontent prolog\">")) {
line = br.readLine();
FormattedText prolog = new FormattedText();
int end = parseLine(prolog.getAppendVisitor(), line, 0, footnotes);
ch.setProlog(prolog);
if (end != line.length())
throw new IOException(line.substring(end));
line = br.readLine();
if (!line.equals("</div>"))
throw new IOException(line);
} else if (line.equals("<div class=\"biblehtmlcontent verses\" id=\"verses\">")) {
while ((line = br.readLine()) != null) {
if (line.equals("</div>"))
break;
if (!line.startsWith("<div class=\"v\" id=\"v") || !line.endsWith("</div>"))
throw new IOException(line);
line = line.substring(20, line.length() - 6);
int pos = line.indexOf("\">");
Verse v = new Verse(line.substring(0, pos));
int end = parseLine(v.getAppendVisitor(), line, pos + 2, footnotes);
if (end != line.length())
throw new IOException(line.substring(end));
ch.getVerses().add(v);
}
if (!line.equals("</div>"))
throw new IOException(line);
} else if (line.equals("<div class=\"biblehtmlcontent footnotes\">")) {
for (int i = 0; i < footnotes.size(); i++) {
line = br.readLine();
String prefix = "<div class=\"fn\"><sup class=\"fnt\"><a name=\"fn" + (i + 1) + "\" href=\"#fnm" + (i + 1) + "\">" + (i + 1) + "</a></sup> ";
if (!line.startsWith(prefix) || !line.endsWith("</div>"))
throw new IOException(line);
line = line.substring(prefix.length(), line.length() - 6);
int end = parseLine(footnotes.get(i), line, 0, null);
if (end != line.length())
throw new IOException(line.substring(end));
}
line = br.readLine();
if (!line.equals("</div>"))
throw new IOException(line);
}
}
if (ch.getProlog() != null)
ch.getProlog().finished();
for (Verse v : ch.getVerses()) v.finished();
}
}
}
return bible;
}
use of biblemulticonverter.data.Book in project BibleMultiConverter by schierlm.
the class RoundtripHTML method doExport.
@Override
public void doExport(Bible bible, String... exportArgs) throws Exception {
List<String> filenames = new ArrayList<String>();
File directory = new File(exportArgs[0]);
if (!directory.exists())
directory.mkdirs();
// build xref map
Properties xrefMap = new Properties();
if (exportArgs.length > 1 && new File(exportArgs[1]).exists()) {
try (InputStream in = new FileInputStream(new File(exportArgs[1]))) {
xrefMap.load(in);
}
}
for (Book bk : bible.getBooks()) {
for (int cnumber = 1; cnumber <= bk.getChapters().size(); cnumber++) {
xrefMap.setProperty(bk.getId().getOsisID() + "," + cnumber, new File(directory, getTypeDir(bk.getId()) + "/" + bk.getAbbr() + "_" + cnumber + ".html").getAbsolutePath());
}
}
if (exportArgs.length > 1) {
try (OutputStream out = new FileOutputStream(new File(exportArgs[1]))) {
xrefMap.store(out, "RoundtripHTML Cross Reference Map");
}
}
Path base = new File(directory, "dummydir").getAbsoluteFile().toPath();
for (Object key : xrefMap.keySet()) {
String newValue = base.relativize(new File((String) xrefMap.get(key)).toPath()).toString().replace('\\', '/');
xrefMap.setProperty((String) key, newValue);
}
// metadata
try (BufferedWriter bw = createWriter(directory, filenames, "metadata.js")) {
bw.write("biblename = \"" + bible.getName().replace("\\", "\\\\").replace("\"", "\\\"") + "\";\n");
bw.write("metadata = [{\n");
boolean first = true;
for (Book bk : bible.getBooks()) {
if (!first)
bw.write("},{\n");
first = false;
String[] keys = new String[] { "abbr", "short", "long", "osis", "type" };
String[] values = new String[] { bk.getAbbr(), bk.getShortName(), bk.getLongName(), bk.getId().getOsisID(), getTypeDir(bk.getId()) };
for (int i = 0; i < keys.length; i++) {
bw.write(keys[i] + ":\"" + values[i].replace("\\", "\\\\").replace("\"", "\\\"") + "\",\n");
}
bw.write("nt:" + bk.getId().isNT() + ",\n");
bw.write("chapters:" + bk.getChapters().size() + "\n");
}
bw.write("}];\n");
}
// chapters
for (Book bk : bible.getBooks()) {
int cnumber = 0;
for (Chapter ch : bk.getChapters()) {
cnumber++;
try (BufferedWriter bw = createWriter(directory, filenames, getTypeDir(bk.getId()) + "/" + bk.getAbbr() + "_" + cnumber + ".html")) {
bw.write("<html><head>\n" + "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" />\n" + "<title>" + (bk.getChapters().size() == 1 ? "" : bk.getAbbr() + " " + cnumber + " – ") + bk.getLongName() + " – " + bible.getName() + "</title>\n" + "<script type=\"text/javascript\" src=\"../metadata.js\"></script>\n" + "<script type=\"text/javascript\" src=\"../script.js\"></script>\n" + "<style type=\"text/css\">div.v { display:inline; } /*changed dynamically*/</style>\n" + "<link rel=\"stylesheet\" type=\"text/css\" href=\"../style.css\">\n" + "</head>\n");
bw.write("<body onload=\"showNavbar('" + bk.getAbbr() + "', " + cnumber + ");\">\n");
bw.write("<div id=\"navbar\"><a href=\"../index.html\">" + bible.getName() + "</a> – <b>" + bk.getLongName() + "</b>");
if (bk.getChapters().size() > 1) {
bw.write(" – ");
for (int i = 1; i <= bk.getChapters().size(); i++) {
if (i == cnumber) {
bw.write("<b>" + i + "</b> ");
} else if (i == 2 && cnumber > 4) {
bw.write("... ");
i = cnumber - 3;
} else if (i == cnumber + 3 && i < bk.getChapters().size()) {
bw.write("... ");
i = bk.getChapters().size() - 1;
} else {
bw.write("<a href=\"" + bk.getAbbr() + "_" + i + ".html\">" + i + "</a> ");
}
}
}
bw.write("</div><hr>\n");
bw.write("<h1>" + bk.getAbbr() + (bk.getChapters().size() == 1 ? "" : " " + cnumber) + "</h1>\n");
bw.write("<!-- PARSED BELOW; EDITING MAY BREAK PARSER -->\n");
List<StringWriter> footnotes = new ArrayList<StringWriter>();
if (ch.getProlog() != null) {
bw.write("<div class=\"biblehtmlcontent prolog\">\n");
ch.getProlog().accept(new RoundtripHTMLVisitor(bw, footnotes, "", "", xrefMap));
bw.write("\n");
bw.write("</div>\n");
}
if (ch.getVerses().size() > 0) {
bw.write("<div class=\"biblehtmlcontent verses\" id=\"verses\">\n");
for (Verse v : ch.getVerses()) {
bw.write("<div class=\"v\" id=\"v" + v.getNumber() + "\">");
v.accept(new RoundtripHTMLVisitor(bw, footnotes, "<span class=\"vn\">" + v.getNumber() + "</span> ", "", xrefMap));
bw.write("</div>\n");
}
bw.write("</div>\n");
}
if (footnotes.size() > 0) {
bw.write("<div class=\"biblehtmlcontent footnotes\">\n");
for (StringWriter footnote : footnotes) {
bw.write(footnote.toString() + "\n");
}
bw.write("</div>\n");
}
bw.write("<!-- PARSED ABOVE; EDITING MAY BREAK PARSER -->\n");
bw.write("</body></html>");
}
}
}
// index file
try (BufferedWriter bw = createWriter(directory, filenames, "index.html")) {
bw.write("<html><head>\n" + "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" />\n" + "<title>" + bible.getName() + "</title>\n" + "<script type=\"text/javascript\" src=\"metadata.js\"></script>\n" + "<script type=\"text/javascript\" src=\"script.js\"></script>\n" + "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">\n" + "</head>\n");
bw.write("<body onload=\"showNavbar('', 0);\">\n");
bw.write("<div id=\"navbar\"><b>" + bible.getName() + "</b></div><hr>\n");
bw.write("<h1>" + bible.getName() + "</h1>\n");
for (Book bk : bible.getBooks()) {
bw.write("<a href=\"" + getTypeDir(bk.getId()) + "/" + bk.getAbbr() + "_1.html\">" + bk.getLongName() + "</a><br>\n");
}
bw.write("</body></html>");
}
// static files
for (String staticFile : Arrays.asList("script.js", "style.css", "crossdomain.html")) {
try (BufferedWriter bw = createWriter(directory, filenames, staticFile)) {
Reader r = new InputStreamReader(RoundtripHTML.class.getResourceAsStream("/RoundtripHTML/" + staticFile), StandardCharsets.UTF_8);
char[] buf = new char[4096];
int len;
while ((len = r.read(buf)) != -1) {
bw.write(buf, 0, len);
}
}
}
// filelist.html (for mirroring)
try (BufferedWriter bw = createWriter(directory, filenames, "filelist.html")) {
bw.write("<html><head>\n" + "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" />\n" + "<title>File list – " + bible.getName() + "</title>\n" + "<script type=\"text/javascript\" src=\"metadata.js\"></script>\n" + "<script type=\"text/javascript\" src=\"script.js\"></script>\n" + "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">\n" + "</head>\n");
bw.write("<body onload=\"showNavbar('', 0);\">\n");
bw.write("<div id=\"navbar\"><a href=\"index.html\">" + bible.getName() + "</a></div><hr>\n");
bw.write("<h1>File list</h1>\n");
for (String name : filenames) {
bw.write("<a href=\"" + name + "\">" + name + "</a><br>\n");
}
bw.write("</body></html>");
}
}
use of biblemulticonverter.data.Book in project BibleMultiConverter by schierlm.
the class RoundtripXML method createBible.
protected JAXBElement<BibleType> createBible(Bible sBible) throws Exception {
ObjectFactory of = new ObjectFactory();
JAXBElement<BibleType> dBible = of.createBible(of.createBibleType());
dBible.getValue().setName(sBible.getName());
int dictCounter = 0;
for (Book sBook : sBible.getBooks()) {
BibleType.Book dBook = of.createBibleTypeBook();
dBook.setAbbr(sBook.getAbbr());
if (sBook.getId().equals(BookID.DICTIONARY_ENTRY)) {
dBook.setId(sBook.getId().getOsisID() + "-" + (++dictCounter));
} else {
dBook.setId(sBook.getId().getOsisID());
}
dBook.setShortName(sBook.getShortName());
dBook.setLongName(sBook.getLongName());
dBible.getValue().getBook().add(dBook);
for (Chapter sChapter : sBook.getChapters()) {
BibleType.Book.Chapter dChapter = of.createBibleTypeBookChapter();
dBook.getChapter().add(dChapter);
if (sChapter.getProlog() != null) {
dChapter.setProlog(of.createBibleTypeBookChapterProlog());
sChapter.getProlog().accept(new CreateContentVisitor(of, dChapter.getProlog().getContent()));
}
for (Verse sVerse : sChapter.getVerses()) {
BibleType.Book.Chapter.Verse dVerse = of.createBibleTypeBookChapterVerse();
dChapter.getVerse().add(dVerse);
dVerse.setNumber(sVerse.getNumber());
sVerse.accept(new CreateContentVisitor(of, dVerse.getContent()));
}
}
}
return dBible;
}
use of biblemulticonverter.data.Book in project BibleMultiConverter by schierlm.
the class RoundtripXML method parseBible.
protected Bible parseBible(JAXBElement<BibleType> sBible) throws Exception {
Bible dBible = new Bible(sBible.getValue().getName());
for (BibleType.Book sBook : sBible.getValue().getBook()) {
Book dBook = new Book(sBook.getAbbr(), BookID.fromOsisId(sBook.getId().replaceAll("-[0-9]+$", "")), sBook.getShortName(), sBook.getLongName());
dBible.getBooks().add(dBook);
for (BibleType.Book.Chapter sChapter : sBook.getChapter()) {
Chapter dChapter = new Chapter();
dBook.getChapters().add(dChapter);
if (sChapter.getProlog() != null) {
dChapter.setProlog(new FormattedText());
parseContent(dChapter.getProlog().getAppendVisitor(), sChapter.getProlog().getContent());
dChapter.getProlog().finished();
}
for (BibleType.Book.Chapter.Verse sVerse : sChapter.getVerse()) {
Verse dVerse = new Verse(sVerse.getNumber());
dChapter.getVerses().add(dVerse);
parseContent(dVerse.getAppendVisitor(), sVerse.getContent());
dVerse.finished();
}
}
}
return dBible;
}
use of biblemulticonverter.data.Book in project BibleMultiConverter by schierlm.
the class ScrambledDiffable method doExport.
@Override
public void doExport(Bible bible, String... exportArgs) throws Exception {
String outputFile = exportArgs[0];
Scrambler scrambler = new Scrambler(exportArgs.length == 1 ? null : exportArgs[1]);
for (Book book : bible.getBooks()) {
if (book.getId() == BookID.METADATA)
continue;
for (Chapter chapter : book.getChapters()) {
if (chapter.getProlog() != null) {
FormattedText newProlog = new FormattedText();
scrambleText(scrambler, chapter.getProlog(), newProlog);
chapter.setProlog(newProlog);
}
List<Verse> verses = chapter.getVerses();
for (int i = 0; i < verses.size(); i++) {
Verse oldVerse = verses.get(i);
Verse newVerse = new Verse(oldVerse.getNumber());
scrambleText(scrambler, oldVerse, newVerse);
verses.set(i, newVerse);
}
}
}
new Diffable().doExport(bible, new String[] { outputFile });
}
Aggregations