Example #1
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Initialize a list of book names available from the import file list.
        /// </summary>
        /// <returns>the number of books available for import</returns>
        /// ------------------------------------------------------------------------------------
        private int InitBookNameList()
        {
            List <int> booksPresent = null;

            try
            {
                booksPresent = m_importSettings.BooksForProject;
            }
            catch
            {
                // TODO: Add a message to tell the user that the paratext project could
                // not be loaded.
            }
            if (booksPresent == null || booksPresent.Count == 0)
            {
                // This can probably only happen in the weird case where a Paratext project
                // that previously had books now has none.
                radImportRange.Enabled = scrPsgFrom.Enabled = scrPsgTo.Enabled = false;
                return(0);
            }
            radImportRange.Enabled = scrPsgFrom.Enabled = scrPsgTo.Enabled = true;

            MultilingScrBooks mlBook =
                new MultilingScrBooks((IScrProjMetaDataProvider)m_cache.LangProject.TranslatedScriptureOA);

            // Get list of books in import files
            BookLabel[] bookNames = new BookLabel[booksPresent.Count];
            int         iName     = 0;

            foreach (int bookOrd in booksPresent)
            {
                bookNames[iName++] = new BookLabel(mlBook.GetBookName(bookOrd), bookOrd);
            }
            scrPsgFrom.BookLabels = bookNames;
            scrPsgTo.BookLabels   = bookNames;

            return(bookNames.Length);
        }
Example #2
0
		/// -----------------------------------------------------------------------------------
		/// <summary>
		/// Get the name of the current book as a string.
		/// </summary>
		/// <param name="selLimitType">Specify Top or Bottom</param>
		/// <returns>The name of the current book, or <c>string.Empty</c> if we don't have a
		/// selection or the selection is in a place we don't know how to handle.</returns>
		/// -----------------------------------------------------------------------------------
		public virtual string CurrentBook(SelectionHelper.SelLimitType selLimitType)
		{
			CheckDisposed();

			// if there is no selection then there can be no book
			if (CurrentSelection == null || m_cache == null)
				return string.Empty;

			int iBook = ((ITeView)Control).LocationTracker.GetBookIndex(
				CurrentSelection, selLimitType);

			if (iBook < 0)
				return string.Empty;

			ScrBook book = BookFilter.GetBook(iBook);
			string sBook = book.Name.UserDefaultWritingSystem;
			if (sBook != null)
				return sBook;
			MultilingScrBooks multiScrBooks = new MultilingScrBooks((IScrProjMetaDataProvider)m_scr);
			multiScrBooks.InitializeWritingSystems(m_cache.LanguageWritingSystemFactoryAccessor);
			return multiScrBooks.GetBookName(book.CanonicalNum);
		}
Example #3
0
        public void DataIntegrity_Headings_HaveConsistentReferences()
        {
            var englishVersification = ScrVers.English;
            var regexHeading         = new Regex("<Section", RegexOptions.Compiled);
            // Although these references are regarded as human-readable, the current state of the data is that in some cases,
            // instead of book names, there is just an abbreviation, either in mixed case or all caps. Probably not ideal, but
            // not really a problem.
            var regexHeadingHumanReadableRef = new Regex(" heading=\"(?<bookName>([1-3] ?)?(([A-Z][A-Za-z]+))( [A-Za-z]+){0,2}) (?<chapterAndVerse>((?<chapter>[1-9][0-9]*):)?(?<startVerse>[1-9][0-9]*)(?<startVerseSegment>[b])?((?<connector>-|(, ?))(((?<endChapter>[1-9][0-9]*)?):)?(?<endVerse>[1-9][0-9]*)(?<endVerseSegment>a)?)?)", RegexOptions.Compiled);
            var regexWellFormedScrRef        = new Regex(" scrref=\"(?<bookId>([1-3][A-Z]{2})|([A-Z]{3})) (?<chapterAndVerseInScrRef>[^\"]*[0-9a-b])\"", RegexOptions.Compiled);
            var regexScrRefAttrib            = new Regex(" scrref=", RegexOptions.Compiled);
            var bbbcccvvvRef  = "((?<zero>0)|(?<bookNum>0?[0-9]{1,2})(?<chapterNum>[0-9]{3})(?<verseNum>[0-9]{3}))";
            var regexStartRef = new Regex($" startref=\"{bbbcccvvvRef}\"", RegexOptions.Compiled);
            var regexEndRef   = new Regex($" endref=\"{bbbcccvvvRef}\"", RegexOptions.Compiled);

            var multilingScrBooks = new MultilingScrBooks();

            string bookId      = null;
            var    bookNum     = -1;
            var    chapter     = -1;
            var    startVerse  = -1;
            var    endVerse    = -1;
            var    endChapter  = -1;
            var    prevBookNum = -1;
            var    prevSectionEndedWithSegment = false;
            var    prevSectionEndCCCVVV        = -1;
            var    inSingleChapterBook         = false;

            foreach (var matchedLine in GetMatchingLines(regexHeading, m_regexQuestion))
            {
                var line     = matchedLine.Line;
                var startPos = matchedLine.MatchEndPosition;
                switch (matchedLine.Level)
                {
                case 0:
                    var matchHeadingHumanReadableRef = regexHeadingHumanReadableRef.Match(line, startPos);
                    Assert.That(matchHeadingHumanReadableRef.Success,
                                "Section head does not contain a valid human-readable reference: " + line);

                    var chapterAndVerse = matchHeadingHumanReadableRef.Groups["chapterAndVerse"].Value.Replace(":", ".");
                    var bookName        = matchHeadingHumanReadableRef.Groups["bookName"].Value;
                    var chapterStr      = matchHeadingHumanReadableRef.Result("${chapter}");
                    chapter    = IsNullOrEmpty(chapterStr) ? 1 : Parse(chapterStr);
                    startVerse = Parse(matchHeadingHumanReadableRef.Groups["startVerse"].Value);
                    var connector   = matchHeadingHumanReadableRef.Result("${connector}");
                    var endVerseStr = matchHeadingHumanReadableRef.Result("${endVerse}");
                    endVerse = IsNullOrEmpty(endVerseStr) ? startVerse : Parse(endVerseStr);
                    var endChapterStr = matchHeadingHumanReadableRef.Result("${endChapter}");
                    endChapter = IsNullOrEmpty(endChapterStr) ? chapter : Parse(endChapterStr);

                    if (connector.StartsWith(","))
                    {
                        Assert.AreEqual(chapter, endChapter,
                                        "If human-readable section reference uses a comma, there must not be an end chapter: " + line);
                        Assert.AreEqual(startVerse + 1, endVerse,
                                        "If human-readable section reference uses a comma, end verse must be the verse following the start verse: " + line);
                        chapterAndVerse = chapterAndVerse.Replace(connector, "-");
                    }

                    var matchScrRef = regexWellFormedScrRef.Match(line, startPos);
                    Assert.That(matchScrRef.Success, "Section head does not contain a valid scrref attribute: " + line);

                    var chapterAndVerseInScrRef = matchScrRef.Groups["chapterAndVerseInScrRef"].Value;
                    if (chapterAndVerse != chapterAndVerseInScrRef)
                    {
                        // Sub-verse letters sometimes occur in the section reference but not in the scrref attribute. These
                        // should probably be consistent, but unfortunately are not. I started to change them, but since this
                        // could At this point
                        Assert.AreEqual(chapterAndVerse.Replace("a", "").Replace("b", ""), chapterAndVerseInScrRef,
                                        "Chapter and verse in scrref attribute do not match human readable chapter and verse: " + line);
                    }

                    bookId  = matchScrRef.Groups["bookId"].Value;
                    bookNum = BCVRef.BookToNumber(bookId);
                    if (bookNum != prevBookNum)
                    {
                        Assert.That(bookNum >= 1 && bookNum <= 66,
                                    $"Book ID ({bookId}) does not correspond to a valid book: " + line);
                        inSingleChapterBook = englishVersification.GetLastChapter(bookNum) == 1;
                    }

                    if (!bookId.Equals(bookName, StringComparison.OrdinalIgnoreCase))
                    {
                        var expectedBookName = multilingScrBooks.GetBookName(bookNum);
                        Assert.That(expectedBookName.StartsWith(bookName),
                                    "Book code in scrref attribute does not correspond to book Name in section heading: " + line);
                    }

                    var matchStartRef = regexStartRef.Match(line, startPos);
                    Assert.That(matchStartRef.Success, "Section head does not contain a valid startref attribute: " + line);
                    Assert.That(matchStartRef.Groups["zero"].Value == Empty,
                                "startref=\"0\" not allowed in section head: " + line);

                    Assert.AreEqual(bookNum, Parse(matchStartRef.Groups["bookNum"].Value),
                                    "Book in startref attribute does not match section book: " + line);
                    Assert.AreEqual(chapter, Parse(matchStartRef.Groups["chapterNum"].Value),
                                    "Chapter in startref attribute does not match section start chapter: " + line);
                    Assert.AreEqual(startVerse, Parse(matchStartRef.Groups["verseNum"].Value),
                                    "Verse in startref attribute does not match section start verse: " + line);

                    var matchEndRef = regexEndRef.Match(line, startPos);
                    Assert.That(matchEndRef.Success, "Section head does not contain a valid endref attribute: " + line);
                    Assert.That(matchEndRef.Groups["zero"].Value == Empty,
                                "endref=\"0\" not allowed in section head: " + line);

                    Assert.AreEqual(bookNum, Parse(matchEndRef.Groups["bookNum"].Value),
                                    "Book in endref attribute does not match section book: " + line);
                    Assert.AreEqual(endChapter, Parse(matchEndRef.Groups["chapterNum"].Value),
                                    "Chapter in endref attribute does not match section end chapter: " + line);
                    Assert.AreEqual(endVerse, Parse(matchEndRef.Groups["verseNum"].Value),
                                    "Verse in endref attribute does not match section end verse: " + line);

                    if (bookNum == prevBookNum)
                    {
                        var currSectionStartsWithSegment = matchHeadingHumanReadableRef.Groups["startVerseSegment"].Value != Empty;
                        var currSectionEndsWithSegment   = matchHeadingHumanReadableRef.Groups["endVerseSegment"].Value != Empty;
                        var currSectionEndCCCVVV         = endChapter * 1000 + endVerse;
                        var currSectionStartCCCVVV       = chapter * 1000 + startVerse;

                        // Ensure references are in ascending order
                        if (prevSectionEndedWithSegment)
                        {
                            Assert.That(currSectionStartsWithSegment && prevSectionEndCCCVVV == currSectionStartCCCVVV,
                                        "Section out of order: " + line);
                        }
                        else
                        {
                            Assert.That(prevSectionEndCCCVVV < currSectionStartCCCVVV,
                                        "Section out of order: " + line);
                        }

                        prevSectionEndCCCVVV        = currSectionEndCCCVVV;
                        prevSectionEndedWithSegment = currSectionEndsWithSegment;
                    }
                    else
                    {
                        prevBookNum          = bookNum;
                        prevSectionEndCCCVVV = -1;
                        Assert.False(prevSectionEndedWithSegment);
                    }
                    break;

                case 1:
                    matchScrRef = regexWellFormedScrRef.Match(line, startPos);
                    if (matchScrRef.Success)
                    {
                        Assert.AreEqual(bookId, matchScrRef.Groups["bookId"].Value,
                                        "Question scrref attribute has incorrect book: " + line);

                        Assert.That(inSingleChapterBook || matchScrRef.Groups["chapterAndVerseInScrRef"].Value.Contains("."),
                                    "Question scrref is missing the chapter or verse: " + line);
                    }
                    else
                    {
                        Assert.IsFalse(regexScrRefAttrib.IsMatch(line, startPos),
                                       "Question contains ill-formed scrref attribute" + line);
                    }

                    matchStartRef = regexStartRef.Match(line, startPos);
                    Assert.That(matchStartRef.Success, "Question does not contain a valid startref attribute: " + line);

                    var questionStartCccVvv = 0;

                    var startRefSpecifiedExplicitly = matchStartRef.Groups["zero"].Value == Empty;
                    if (startRefSpecifiedExplicitly)
                    {
                        Assert.AreEqual(bookNum, Parse(matchStartRef.Groups["bookNum"].Value),
                                        "Unexpected book number in startRef: " + line);
                        var questionStartChapter = Parse(matchStartRef.Groups["chapterNum"].Value);
                        var questionStartVerse   = Parse(matchStartRef.Groups["verseNum"].Value);
                        questionStartCccVvv = questionStartChapter * 1000 + questionStartVerse;
                        if (chapter * 1000 + startVerse > questionStartCccVvv)
                        {
                            Assert.That(line.Contains("multiSectionSummary=\"true\""),
                                        "Question unexpectedly starts outside of containing section: " + line);
                        }
                    }
                    else
                    {
                        // This is important because the logic in MasterQuestionParser only fills in the
                        // start and end references from the section (typical for Overview questions) if
                        // the scrref attribute is not defined. If those values are not set, then sorting
                        // and filtering gets messed up.
                        Assert.IsFalse(matchScrRef.Success, "If scrref is defined, start and end refs should be non-zero: " + line);
                    }

                    matchEndRef = regexEndRef.Match(line, startPos);
                    Assert.That(matchEndRef.Success, "Question does not contain a valid endref attribute: " + line);
                    if (startRefSpecifiedExplicitly)
                    {
                        Assert.That(matchEndRef.Groups["zero"].Value == Empty,
                                    "Start and end ref must both be explicit or both be implicit: " + line);

                        Assert.AreEqual(bookNum, Parse(matchEndRef.Groups["bookNum"].Value));
                        var questionEndChapter = Parse(matchEndRef.Groups["chapterNum"].Value);
                        var questionEndVerse   = Parse(matchEndRef.Groups["verseNum"].Value);

                        var questionEndCccVvv = questionEndChapter * 1000 + questionEndVerse;
                        Assert.That(endChapter * 1000 + endVerse >= questionEndCccVvv,
                                    "Question ends outside of containing section: " + line);

                        Assert.That(questionStartCccVvv <= questionEndCccVvv,
                                    "Question start reference is later than the end reference: " + line);
                    }
                    else
                    {
                        Assert.That(matchEndRef.Groups["zero"].Value != Empty,
                                    "Start and end ref must both be explicit or both be implicit: " + line);
                    }

                    break;
                }
            }
        }
 public void ValidateReferencesWithoutDB()
 {
     Assert.AreEqual("Genesis".ToLower(), m_mlscrBook.GetBookName(1).ToLower(),
                     "Genesis not found");
 }
		public void ValidateReferencesWithDB()
		{
			m_mlscrBook = new DBMultilingScrBooks(m_scr);
			m_mlscrBook.InitializeWritingSystems(
				m_fdoCache.LanguageWritingSystemFactoryAccessor);

			Assert.IsFalse(m_mlscrBook.IsBookAvailableInDb(1), "Genesis found");
			Assert.IsTrue("genesis" != m_mlscrBook.GetBookName(1).ToLower(), "Genesis found");
			Assert.IsFalse(m_mlscrBook.IsReferenceValid("GEN 1:4"),
				"GEN 1:4 said to be a valid Reference");

			Assert.IsTrue(m_mlscrBook.IsBookAvailableInDb(59), "James not found");
			Assert.AreEqual("James".ToLower(), m_mlscrBook.GetBookName(59).ToLower(),
				"James found");
			Assert.IsTrue(m_mlscrBook.IsReferenceValid("JAS 1:12"),
				"JAS 1:12 said to be an invalid Reference");
			// These were removed because setting a reference to out of range values will
			// cause the values to be set to 1 and then the reference is valid.
//			Assert.IsFalse(mlscrBook.IsReferenceValid("JAS 50:1"),
//				"JAS 50:1 is valid Reference... go figure");
//			Assert.IsFalse(mlscrBook.IsReferenceValid("JAS 1:50"),
//				"JAS 1:50 is valid Reference... go figure");
		}
Example #6
0
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Initialize a list of book names available from the import file list.
		/// </summary>
		/// <returns>the number of books available for import</returns>
		/// ------------------------------------------------------------------------------------
		private int InitBookNameList()
		{
			List<int> booksPresent = null;
			try
			{
				booksPresent = m_importSettings.BooksForProject;
			}
			catch
			{
				// TODO: Add a message to tell the user that the paratext project could
				// not be loaded.
			}
			if (booksPresent == null || booksPresent.Count == 0)
			{
				// This can probably only happen in the weird case where a Paratext project
				// that previously had books now has none.
				radImportRange.Enabled = scrPsgFrom.Enabled = scrPsgTo.Enabled = false;
				return 0;
			}
			radImportRange.Enabled = scrPsgFrom.Enabled = scrPsgTo.Enabled = true;

			MultilingScrBooks mlBook =
				new MultilingScrBooks((IScrProjMetaDataProvider)m_cache.LangProject.TranslatedScriptureOA);
			// Get list of books in import files
			BookLabel[] bookNames = new BookLabel[booksPresent.Count];
			int iName = 0;
			foreach (int bookOrd in booksPresent)
				bookNames[iName++] = new BookLabel(mlBook.GetBookName(bookOrd), bookOrd);
			scrPsgFrom.BookLabels = bookNames;
			scrPsgTo.BookLabels = bookNames;

			return bookNames.Length;
		}