/// <summary> /// Find how many verses are in a certain book chapter of the Bible. /// </summary> /// <param name="reference">The book and chapter to look up (verse number ignored).</param> /// <returns>The number of verses in the chapter.</returns> public int GetVerseCount(BibleReference reference) { XElement book = XDocument.Descendants("BIBLEBOOK").ElementAt((int)reference.Book); XElement chapter = book.Descendants("CHAPTER").ElementAt(reference.Chapter - 1); return(chapter.Descendants("VERS").Count()); }
private void Worker(BibleReference reference, bool isNewTab) { if (reference == null) { throw new ArgumentNullException("A new browser tab without a reference should be created with the overloaded constructor."); } else { History.Add(reference); } Guid = Guid.NewGuid(); IsNewTab = IsNewTab; }
/// <summary> /// Overridden string method. /// </summary> /// <returns>A bible reference</returns> public override string ToString() { BibleReference reference = History.LastOrDefault(); // TODO if (reference == null) { return(Guid.ToString()); } else { return("[" + reference.Version + "] " + reference.SimplifiedReference); } }
/// <summary> /// Get every verse in a chapter by <code>BibleReference</code>. /// </summary> /// <param name="reference">The chapter to look up</param> /// <returns>A list of strings that are the contents of each verse in the Bible's chapter.</returns> public List <string> GetChapterVerses(BibleReference reference) { List <string> verseContents = new List <string>(); if (reference == null) { return(verseContents); // Blank page } XElement book = XDocument.Descendants("BIBLEBOOK").ElementAt((int)reference.Book); XElement chapter = book.Descendants("CHAPTER").ElementAt(reference.Chapter - 1); foreach (XElement element in chapter.Elements()) { verseContents.Add(element.Value.FormatTypographically()); } return(verseContents); }
/// <summary> /// Add a reference to history as necessary. /// </summary> /// <param name="reference">The reference to visit; do not pass in <c>null</c>. /// This <c>ref</c> parameter returns the navigation result (so it can be navigated to).</param> /// <param name="mode">When the <c>NavigationMode</c> is <c>Previous</c> or <c>Next</c>, the reference is moved to but not added in history.</param> public void AddToHistory(ref BibleReference reference, NavigationMode mode = NavigationMode.Add) { if (reference == null) { throw new Exception("Do not assign null to a Bible reference in history"); } else { switch (mode) { case NavigationMode.Add: // Delete history that's past the current item for (int i = m_HistoryIndex; i < History.Count; i++) { if (i > m_HistoryIndex) { History.RemoveAt(i); } } History.Add(reference); m_HistoryIndex = History.Count - 1; NotifyPropertyChanged(); return; case NavigationMode.Previous: m_HistoryIndex = Math.Clamp(m_HistoryIndex - 1, 0, History.Count - 1); reference = History[m_HistoryIndex]; NotifyPropertyChanged(); return; case NavigationMode.Next: m_HistoryIndex = Math.Clamp(m_HistoryIndex + 1, 0, History.Count - 1); reference = History[m_HistoryIndex]; NotifyPropertyChanged(); return; } foreach (var item in History) { Debug.WriteLine("History item: " + item.FullReference + " " + item.IsSearch); } } }
/// <summary> /// Constructor. /// </summary> public SearchResult(BibleReference reference, string verse, string highlight) { if (reference == null) { throw new ArgumentNullException("The reference passed was null"); } else { Reference = reference; } if (verse == null) { throw new ArgumentNullException("The verse text passed was null"); } else { Text = verse; } HighlightText = highlight; }
/// <summary> /// Find how many chapters are in a certain book of the Bible. /// </summary> /// <param name="reference">The book to look up (chapter number ignored).</param> /// <returns>The number of chapters in the book.</returns> public int GetChapterCount(BibleReference reference) { XElement book = XDocument.Descendants("BIBLEBOOK").ElementAt((int)reference.Book); return(book.Descendants("CHAPTER").Count()); }
/// <summary> /// Initialize the tabs to when the browser was previously open. /// Assign the tabs to the loaded data. /// </summary> public static async Task LoadSavedTabs() { // There may not be a saved document try { // Read the saved XML tabs StorageFile readFile = await m_localFolder.GetFileAsync("SavedTabs.xml"); string text = await FileIO.ReadTextAsync(readFile); // Debug file contents Debug.WriteLine("The saved tabs xml file contents :"); Debug.WriteLine(text); XDocument XMLTabs = XDocument.Parse(text); // Debug the file contents Debug.WriteLine("Tabs loaded :"); Debug.WriteLine(XMLTabs); // Create the tab list from XML IEnumerable <XElement> tabs = XMLTabs.Descendants("Reference"); TrulyObservableCollection <BrowserTab> savedTabs = new TrulyObservableCollection <BrowserTab>(); foreach (XElement node in tabs) { // Get the information from XML BibleVersion bibleVersion = new BibleVersion(node.Element("FileName").Value); BibleVersion comparisonVersion; if (node.Element("ComparisonFileName").Value == "Null") { comparisonVersion = null; } else { comparisonVersion = new BibleVersion(node.Element("ComparisonFileName").Value); } string bookName = node.Element("BookName").Value; BibleBook book = BibleReference.StringToBook(bookName, bibleVersion); int chapter = int.Parse(node.Element("Chapter").Value); int verse = int.Parse(node.Element("Verse").Value); // Create the reference that goes in the tab BibleReference reference = new BibleReference(bibleVersion, comparisonVersion, book, chapter, verse); savedTabs.Add(new BrowserTab(reference)); } // Add the tabs to the browser foreach (BrowserTab tab in savedTabs) { Tabs.Add(tab); } RequireNewTab(); } catch (System.IO.FileNotFoundException fileNotFoundE) { Debug.WriteLine("A resource was not loaded correctly; this may be a missing bible version :"); Debug.WriteLine(fileNotFoundE.Message); RequireNewTab(); } catch (System.Xml.XmlException xmlE) // Parse error { Debug.WriteLine("Reading the saved tabs xml file choked :"); Debug.WriteLine(xmlE.Message); RequireNewTab(); } catch (Exception e) { Debug.WriteLine("Loading saved tabs was interrupted :"); Debug.WriteLine(e.Message); RequireNewTab(); } }
/// <summary> /// Create a pre-existing browser tab. /// </summary> public BrowserTab(BibleReference reference) { Worker(reference, false); }
/// <summary> /// Create a custom browser tab. /// </summary> public BrowserTab(BibleReference reference, bool isNewTab) { Worker(reference, IsNewTab); }
/// <summary> /// Asynchronously search the Bible for every verse that contains a certain text as a substring. /// </summary> /// <param name="query">The string to be matched in the Bible reference for the verse to be returned.</param> public static async Task <SearchProgressInfo> SearchAsync(BibleVersion version, string query, IProgress <SearchProgressInfo> progress, CancellationTokenSource cancellation) { SearchProgressInfo progressInfo = new SearchProgressInfo(query); query = query.ToLower(CultureInfo.InvariantCulture).RemoveDiacritics().RemovePunctuation(); progressInfo = await Task.Run <SearchProgressInfo>(() => { // Go through each book of the Bible for (int book = 0; book < version.BookNumbers.Count; book++) { // Throw an exception if cancellation was requested try { BibleReference reference = new BibleReference(version, null, (BibleBook)book); progressInfo.Completion = DGL.Math.Percent(book + 1, version.BookNumbers.Count); progressInfo.Status = version.BookNames[book]; // Go through each chapter of the book of the Bible Parallel.For(1, version.GetChapterCount(reference) + 1, chapter => { // See if the search has hit too many results for the computer's good if (progressInfo.ResultCount > TOOMANYRESULTS) { cancellation.Cancel(); Debug.WriteLine("******************* TOO MANY RESULTS"); progress.Report(progressInfo); throw new OperationCanceledException(); } else if (cancellation.IsCancellationRequested) { cancellation.Cancel(); Debug.WriteLine("******************* CANCELLATION REQUESTED"); progress.Report(progressInfo); throw new OperationCanceledException(); } else { BibleReference chapterReference = new BibleReference(version, null, (BibleBook)book, chapter); // Go through each verse of the chapter int verseNumber = 1; foreach (string verse in version.GetChapterVerses(chapterReference)) { if (verse.ToLower().RemoveDiacritics().RemovePunctuation().Contains(query)) { BibleReference hit = new BibleReference(version, null, (BibleBook)book, chapter, verseNumber); Debug.WriteLine(hit + ":" + verseNumber + " -- " + verse); progressInfo.AddResult(new SearchResult(hit, verse, query)); } verseNumber++; } progress.Report(progressInfo); } }); } catch (OperationCanceledException) { progressInfo.IsCanceled = true; return(progressInfo); } catch (Exception) { progressInfo.IsCanceled = true; return(progressInfo); } } return(progressInfo); }); return(progressInfo); }