/** * Get the Key object of the current key's chapter given a specific verse * * @param key * @param verse * @return */ public static Verse getVerseKey(Key key, int verse) { try { if (Utils.isVerse((key))) { // Returns current chapter Verse currentVerse = (Verse) key; return new Verse(currentVerse.getBook(), currentVerse.getChapter(), verse); } else if (Utils.isVerseRange((key))) { Verse currentVerse = ((VerseRange) key).getStart(); return new Verse(currentVerse.getBook(), currentVerse.getChapter(), verse); } else { } } catch (NoSuchVerseException e) { // TODO Auto-generated catch block e.printStackTrace(); } throw new RuntimeException("Unsupported Type for next chapter key"); }
public static Key prevChapter(Key key) { Verse verse; // Get verse to work with if (Utils.isVerse(key)) { verse = (Verse) key; } else if (Utils.isVerseRange(key)) { verse = ((VerseRange) key).getStart(); } else { throw new RuntimeException("Unsupported Type for next chapter key"); } // Check if start of Bible if ((verse.getChapter() == 1 && verse.getBook().equals(BibleBook.GEN))) { return key; } else { return new VerseRange( verse.getFirstVerseInChapter().subtract(1).getFirstVerseInChapter(), verse.getFirstVerseInChapter().subtract(1)); } // if(TextUtility.isVerse((key))){ // //Check if it is the first book of Genesis // if (((Verse) key).getChapter() == 1 && ((Verse) key).getBook().equals(BibleBook.GEN)){ // return key; // } // return new VerseRange(((Verse) // key).getFirstVerseInChapter().subtract(1).getFirstVerseInChapter(), ((Verse) // key).getFirstVerseInChapter().subtract(1)); // } else if (TextUtility.isVerseRange((key))) { // //TODO: what if the book is not a bible book? // // //Check if it is the first book of Genesis // if (((VerseRange) key).getStart().getChapter() == 1 && ((VerseRange) // key).getStart().getBook().equals(BibleBook.GEN)){ // return key; // } // //Else just the chapter reference // return new VerseRange(((VerseRange) // key).getStart().getFirstVerseInChapter().subtract(1).getFirstVerseInChapter(),((VerseRange) // key).getStart().getFirstVerseInChapter().subtract(1)); // } else { // throw new RuntimeException("Unsupported Type for next chapter key"); // } }
public void dump(String name, String range) throws NoSuchKeyException, IOException, BookException { Books books = Books.installed(); Book bible = books.getBook(name); BookMetaData bmd = bible.getBookMetaData(); String lastBookName = ""; int lastChapter = -1; StringBuffer buf = new StringBuffer(); boolean inPreVerse = false; Key keys = bible.getKey(range); openOutputFile(bmd.getInitials(), !BY_BOOK); buildDocumentOpen(buf, bmd, range, !BY_BOOK); if (!BY_BOOK) { writeDocument(buf); } // Get a verse iterator for (Key key : keys) { Verse verse = (Verse) key; String raw = bible.getRawText(verse); String osisID = verse.getOsisID(); String currentBookName = verse.getBook().getOSIS(); int currentChapter = verse.getChapter(); boolean newBookFound = !lastBookName.equals(currentBookName); if (newBookFound) { if (lastBookName.length() > 0) { if (currentChapter == 1) { if (inPreVerse) { buildPreVerseClose(buf); inPreVerse = false; } buildChapterClose(buf); } buildBookClose(buf); buildDocumentClose(buf, BY_BOOK); openOutputFile(lastBookName, BY_BOOK); writeDocument(buf); closeOutputFile(BY_BOOK); } buf = new StringBuffer(); buildDocumentOpen(buf, bmd, currentBookName, BY_BOOK); buildBookOpen(buf, currentBookName); } if (newBookFound || lastChapter != currentChapter) { if (currentChapter != 1) { if (inPreVerse) { buildPreVerseClose(buf); inPreVerse = false; } buildChapterClose(buf); } buildChapterOpen(buf, currentBookName, currentChapter); } /* Output the verse */ boolean foundPreVerse = false; String preVerseText = ""; if (raw.indexOf(preVerseStart) != -1) { Matcher matcher = preVersePattern.matcher(raw); StringBuffer rawbuf = new StringBuffer(); if (matcher.find()) { foundPreVerse = true; preVerseText = matcher.group(1); matcher.appendReplacement(rawbuf, ""); } matcher.appendTail(rawbuf); raw = rawbuf.toString(); } boolean foundPsalmTitle = false; String psalmTitleText = ""; if (raw.indexOf(psalmTitleStart) != -1) { Matcher matcher = psalmTitlePattern.matcher(raw); StringBuffer rawbuf = new StringBuffer(); if (matcher.find()) { foundPsalmTitle = true; psalmTitleText = matcher.group(1); matcher.appendReplacement(rawbuf, ""); } matcher.appendTail(rawbuf); raw = rawbuf.toString(); } if (foundPsalmTitle) { buildPsalmTitle(buf, psalmTitleText); } if (foundPreVerse && !preVerseText.equals(psalmTitleText)) { if (inPreVerse) { buildPreVerseClose(buf); } buildPreVerseOpen(buf, preVerseText); inPreVerse = true; } buildVerseOpen(buf, osisID); buf.append(raw); buildVerseClose(buf, osisID); lastChapter = currentChapter; lastBookName = currentBookName; } // Close everything that is open if (inPreVerse) { buildPreVerseClose(buf); inPreVerse = false; } buildChapterClose(buf); buildBookClose(buf); buildDocumentClose(buf, true); openOutputFile(lastBookName, BY_BOOK); writeDocument(buf); closeOutputFile(true); }