private bool TryParseVerse(string verseString, BibleChapter chapter, out int startVerse, out int endVerse, out VerseParseResult result) { startVerse = 0; endVerse = 0; result = VerseParseResult.InvalidSyntax; if (!string.IsNullOrWhiteSpace(verseString)) { var startVerseString = Regex.Match(verseString, "\\d+").Value; if (int.TryParse(startVerseString, out startVerse)) { if (startVerse > chapter.TotalVerses) { // start verse is greater than the number of verses in the chapter result = VerseParseResult.InvalidVerse; } else { var endVerseString = Regex.Match(verseString, "[-,]\\d+").Value.Replace("-", "").Replace(",", ""); if (!string.IsNullOrEmpty(endVerseString)) { if (int.TryParse(endVerseString, out int tempEndVerse)) { if (tempEndVerse > chapter.TotalVerses) { // the end verse is greater than the number of verses in the chapter result = VerseParseResult.InvalidVerse; } else if (tempEndVerse < 1 || (verseString.Contains(",") && tempEndVerse != (startVerse + 1))) { // end verse is 0 or there were two non-sequential verses separated by a comma result = VerseParseResult.InvalidSyntax; } else { endVerse = tempEndVerse; result = VerseParseResult.Success; } } } else { // no end verse specified, so use the start as the end endVerse = startVerse; result = VerseParseResult.Success; } } } } return(result == VerseParseResult.Success); }
public BibleChapter RetrieveChapterDetails(int bookId, int chapterNumber) { var chapter = new BibleChapter { ChapterNumber = chapterNumber, BibleBookId = bookId }; chapter.TotalVerses = _verseCountByChapter[bookId][chapterNumber]; return(chapter); }
private bool TryParseChapter(string chapterVerse, BibleBook book, out BibleChapter chapter, out string verseString, out VerseParseResult result) { chapter = null; verseString = null; result = VerseParseResult.InvalidSyntax; if (book.IsSingleChapter) { chapter = book.Chapters.FirstOrDefault(); verseString = chapterVerse; result = VerseParseResult.Success; } else { var chapterStr = Regex.Match(chapterVerse, @"\d+:").Value; if (!string.IsNullOrEmpty(chapterStr)) { var chapterNum = int.Parse(chapterStr.Replace(":", "").Trim()); if (chapterNum > 0 && chapterNum <= book.Chapters.Count) { chapter = book.SortedChapters[chapterNum - 1]; verseString = chapterVerse.Substring(chapterVerse.IndexOf(":") + 1).Trim(); result = VerseParseResult.Success; } else { result = VerseParseResult.InvalidVerse; } } } return(result == VerseParseResult.Success); }
public Bible LoadContent(string fileName, Bible b) { var xmlDoc = new XmlDocument(); xmlDoc.Load(fileName); var xmlRoot = xmlDoc.DocumentElement; if (xmlRoot.Name == "XMLBIBLE") { b.Books = new List <BibleBook>(); foreach (XmlNode bookNode in xmlRoot.ChildNodes) { if (bookNode.Name.ToLower() == "biblebook") { var bo = new BibleBook { Bible = b, Number = int.Parse(bookNode.Attributes["bnumber"].InnerText) }; bo.Name = bookNode.Attributes["bname"] != null ? bookNode.Attributes["bname"].InnerText : Bible.BookMap[bo.Number - 1]; bo.ShortName = bookNode.Attributes["bsname"] != null ? bookNode.Attributes["bsname"].InnerText : bo.Name; bo.Chapters = new List <BibleChapter>(); foreach (XmlNode chapNode in bookNode.ChildNodes) { if (chapNode.Name.ToLower() == "chapter") { var ch = new BibleChapter { Book = bo, Number = int.Parse(chapNode.Attributes["cnumber"].InnerText), Verses = new List <BibleVerse>() }; foreach (XmlNode verseNode in chapNode.ChildNodes) { if (verseNode.Name.ToLower() == "vers") { string text = string.Empty; foreach (XmlNode textNode in verseNode.ChildNodes) { if (textNode.NodeType == XmlNodeType.Text) { text += textNode.InnerText; } } var v = new BibleVerse { Chapter = ch, Number = int.Parse(verseNode.Attributes["vnumber"].InnerText), Text = text }; ch.Verses.Add(v); } } bo.Chapters.Add(ch); } } b.Books.Add(bo); } } } return(b); }