From f2516073911c64c189a5a091557e9654a90d1f4e Mon Sep 17 00:00:00 2001 From: sabdelmalik Date: Sat, 6 Jan 2024 17:46:17 -0500 Subject: [PATCH 1/5] OSIS XML file load support Including support code. To be integrated. --- .../BibleVersions/TargetOsisVersion.cs | 280 ++++++++++++++++++ src/BibleTaggingUtil/OsisXml/OsisTag.cs | 226 ++++++++++++++ src/BibleTaggingUtil/OsisXml/OsisUtils.cs | 89 ++++++ src/BibleTaggingUtil/OsisXml/OsisVerse.cs | 190 ++++++++++++ src/BibleTaggingUtil/OsisXml/VerseTagType.cs | 22 ++ src/BibleTaggingUtil/VerseWord.cs | 8 + 6 files changed, 815 insertions(+) create mode 100644 src/BibleTaggingUtil/BibleVersions/TargetOsisVersion.cs create mode 100644 src/BibleTaggingUtil/OsisXml/OsisTag.cs create mode 100644 src/BibleTaggingUtil/OsisXml/OsisUtils.cs create mode 100644 src/BibleTaggingUtil/OsisXml/OsisVerse.cs create mode 100644 src/BibleTaggingUtil/OsisXml/VerseTagType.cs diff --git a/src/BibleTaggingUtil/BibleVersions/TargetOsisVersion.cs b/src/BibleTaggingUtil/BibleVersions/TargetOsisVersion.cs new file mode 100644 index 0000000..f7b8bbf --- /dev/null +++ b/src/BibleTaggingUtil/BibleVersions/TargetOsisVersion.cs @@ -0,0 +1,280 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using System.Xml; + +using BibleTaggingUtil.OsisXml; + +namespace BibleTaggingUtil.BibleVersions +{ + /// + /// 1. Read all the contents of the OSIS xml as a string + /// 2. Use regex to get offset of each book and create a book/offset map + /// 3. GetVerse() takes a verse reference (e.g. Rev.19.14) and uses regex to find the offset and size + /// of the verse. (regex starts its search from the offset found in the offsets map) + /// 4. ParseVerse() takes the verse text enclsed between sID and eID and loads it into a XmlDocument. + /// This is because the verse content is well formed xml. This makes it easy to extract the verse's + /// words and Strong's tags. + /// 5. Once the tags has been update, the verse is reconstructed and is replaced in the text + /// 6. Finally the text is saved. + /// + internal class TargetOsisVersion : BibleVersion + { + /// + /// The complete OSIS XML file is read into this string + /// + string osisDoc = string.Empty; + + /// + /// Key: Bible book name + /// value: Offset of the bookwithin the osisDoc string + /// The offsets speeds up the regex search for verses + /// + private Dictionary bookOffsets = new Dictionary(); + + /// + /// Key: Verse reference in the format Gen 1:1 + /// value: The parsed OSIS content of the verse + protected Dictionary osisBible = new Dictionary(); + + public TargetOsisVersion(BibleTaggingForm container) : base(container, 31104) { } + + /// + /// + /// + /// + internal void Read(string fileName) + { + // Read the complete OSIS XML file + osisDoc = File.ReadAllText(fileName); + + // Build the book's offsets map + BuildBookOffsets(); + + // Create an OSIS Bible Map + foreach (string book in bookOffsets.Keys) + { + Regex regex = new Regex( + string.Format(@"", book)); + + MatchCollection VerseMatches = regex.Matches(osisDoc, bookOffsets[book]); + if (VerseMatches.Count > 0) + { + foreach (Match VerseMatch in VerseMatches) + { + OsisVerse? osisVerse = GetVersesTags(book, VerseMatch); + if (osisVerse != null) + { + osisBible.Add(osisVerse.VerseRefX, osisVerse); + if (osisVerse.VerseRef == "Gen.9.26") + { + int x = 0; + } + Verse verseWords = osisVerse.GetVerseWords(); + bible.Add(osisVerse.VerseRefX, verseWords); + } + } + } + } + } + + + + private OsisVerse? GetVersesTags(string book, Match VerseMatch) + { + OsisVerse result = null; + + int startIndex = VerseMatch.Index + VerseMatch.Groups[0].Length; + string sID = VerseMatch.Groups[1].Value; + string eID = string.Empty; + string verseRef = string.Format("{0}.{1}.{2}", book, VerseMatch.Groups[2].Value, VerseMatch.Groups[3].Value); + string verseXML = string.Empty; + int endIndex = 0; + + if (verseRef == "Gen.2.7") + { + int x = 0; + } + + try + { + Regex regex = new Regex(string.Format(@"", sID)); + VerseMatch = regex.Match(osisDoc, startIndex); + if (VerseMatch.Success) + { + endIndex = VerseMatch.Index; + eID = VerseMatch.Groups[1].Value; + } + + string verseXml = string.Empty; + if (endIndex > startIndex) + verseXml = osisDoc.Substring(startIndex, endIndex - startIndex); + + result = new OsisVerse(verseRef, startIndex, sID, verseXml, eID); + } + catch (Exception ex) + { + //Trace(string.Format("{0},Error,{1},{2}", sID, ++errorCounter,ex.Message), Color.Red); + //result = string.Format("{0},Error,{1},{2}", sID, ++errorCounter, ex.Message); + } + + return result; + } + + + internal OsisVerse GetVerse(string verseRef) + { + OsisVerse verseXML = null; + + //string verse = @"Then God said, ch. 3:22; 11:7; Isa. 6:8Let us make manThe Hebrew word for man (adam) is the generic term for mankind and becomes the proper name Adam in our image, ch. 5:1; 9:6; 1 Cor. 11:7; Eph. 4:24; Col. 3:10; James 3:9after our likeness. And ch. 9:2; Ps. 8:6-8; James 3:7let them have dominion over the fish of the sea and over the birds of the heavens and over the livestock and over all the earth and over every creeping thing that creeps on the earth."; + verseRef = "Rev.19.14"; + + int bookStart = -1; + Match m = Regex.Match(verseRef, @"([1-9A-Za-z]*)\.(\d{1,3})\.(\d{1,3})"); + if (m != null) + { + string book = m.Groups[1].Value; + string chapter = m.Groups[2].Value; + string verse = m.Groups[3].Value; + + bookStart = bookOffsets[book]; + } + + + string pattern1 = string.Format( + @"", verseRef); + + var regex = new Regex(pattern1); + int startIndex = 0; + int endIndex = 0; + string sID = string.Empty; + string eID = string.Empty; + + Match match = regex.Match(osisDoc, bookStart); + if (match.Success) + { + startIndex = match.Index + match.Groups[0].Length; + sID = match.Groups[1].Value; + } + + string pattern2 = string.Format( + @"", sID); + regex = new Regex(pattern2); + match = regex.Match(osisDoc, startIndex); + if (match.Success) + { + endIndex = match.Index; + eID = match.Groups[1].Value; + } + + string osisVerse = string.Empty; + if (endIndex > startIndex) + { + osisVerse = osisDoc.Substring(startIndex, endIndex - startIndex); + verseXML = new OsisVerse(verseRef, osisVerse.Length, sID, osisVerse, eID); + } + + return verseXML; + //osisDoc = osisDoc.Remove(startIndex, osisVerse.Length).Insert(startIndex, osisVerse); + + } + + private void ParseVerse(string verse) + { + //string verse = @"And the armies of heaven, ch. 3:4; 7:9arrayed in fine linen, white and pure, [ch. 14:20]were following him on white horses."; + string header = @""; + string trailer = @""; + + string verseXML = header + verse + trailer; + + XmlDocument document = new XmlDocument(); + document.LoadXml(verseXML); + //XPathNavigator? nav = document.CreateNavigator(); + + XmlElement root = document.DocumentElement; + foreach (XmlNode node in root.ChildNodes) + { + string name = node.Name; + List strongs = new List(); + if (name == "#text") + { + string word = node.InnerText; + } + else if (name == "w") + { + string word = node.InnerText; + XmlAttributeCollection? attrib = node.Attributes; + if (attrib != null) + { + foreach (XmlAttribute attr in attrib) + { + if (attr.Name == "lemma") + { + strongs = GetStrongs(attr.Value); + } + } + } + + } + + } + } + + private List GetStrongs(string value) + { + List strings = new List(); + MatchCollection matches = Regex.Matches(value, @"strong:([GH]\d\d\d\d)"); + if (matches.Count > 0) + { + foreach (Match match in matches) + { + strings.Add(match.Groups[1].Value); + } + } + + return strings; + } + + internal void Save(string fileName) + { + if (osisDoc != null) + { + foreach (OsisVerse osisVerse in osisBible.Values) + { + if (osisVerse.Dirty) + { + osisDoc = osisDoc.Remove(osisVerse.StartIndex, osisVerse.Length) + .Insert(osisVerse.StartIndex, osisVerse.VerseComplteXml); + } + } + File.WriteAllText(fileName, osisDoc); + } + } + + private void BuildBookOffsets() + { + bookOffsets.Clear(); + while (true) + { + //
+ Regex regex = new Regex(@""); + MatchCollection matches = regex.Matches(osisDoc); + if (matches.Count > 0) + { + foreach (Match match in matches) + { + int offset = match.Index; + string book = match.Groups[1].Value; + bookOffsets[book] = offset; + } + break; + } + } + } + + } +} diff --git a/src/BibleTaggingUtil/OsisXml/OsisTag.cs b/src/BibleTaggingUtil/OsisXml/OsisTag.cs new file mode 100644 index 0000000..2710ecd --- /dev/null +++ b/src/BibleTaggingUtil/OsisXml/OsisTag.cs @@ -0,0 +1,226 @@ +using BibleTaggingUtil; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using System.Xml; +using System.Xml.Linq; + +namespace BibleTaggingUtil.OsisXml +{ + internal class OsisTag + { + private int level = 0; + private int index = 0; + private VerseTagType tagType; + private XmlNode tagXml; + private VerseWord? verseWord = null; + private List osisTags = new List(); + + public OsisTag(int level, int index, VerseTagType tagType, XmlNode tagXML, string verseRef) + { + this.tagType = tagType; + tagXml = tagXML; + this.level = level; + this.index = index; + + switch (tagType) + { + case VerseTagType.text: + CreateUntaggedWord(index, tagXML, verseRef); + break; + case VerseTagType.w: + CreateTaggedWord(index, tagXML, verseRef); + break; + case VerseTagType.note: + case VerseTagType.milestone: + case VerseTagType.b: + case VerseTagType.UNKNOWN: + break; + + default: + { + osisTags = OsisUtils.Instance.GetOsisTags(tagXML, verseRef, ++level); + //foreach (OsisTag tag in osisTags) + //{ + // new OsisTag(++level, index, tag.tagType, tag.tagXml, verseRef); ; + //} + } + break; + } + } + + private void CreateUntaggedWord(int index, XmlNode tagXML, string verseRef) + { + verseWord = new VerseWord(tagXML.InnerText, new string[0], verseRef); + verseWord.OsisTagIndex = index; + verseWord.OsisTagLevel = level; + } + private void CreateTaggedWord(int index, XmlNode tagXML, string verseRef) + { + string word = tagXML.InnerText; + + List strongs = new List(); + + XmlAttributeCollection? attrib = tagXML.Attributes; + if (attrib != null) + { + foreach (XmlAttribute attr in attrib) + { + if (attr.Name == "lemma") + { + strongs = GetStrongs(attr.Value); + } + } + } + + verseWord = new VerseWord(tagXML.InnerText, strongs.ToArray(), verseRef); + verseWord.OsisTagIndex = index; + verseWord.OsisTagLevel = level; + } + + private List GetStrongs(string value) + { + List strings = new List(); + MatchCollection matches = Regex.Matches(value, @"strong:[GH](\d\d\d\d)"); + if (matches.Count > 0) + { + foreach (Match match in matches) + { + strings.Add(match.Groups[1].Value); + } + } + + return strings; + } + + public List GetVerseWords() + { + List verseWords = new List(); + switch (tagType) + { + case VerseTagType.text: + case VerseTagType.w: + verseWords.Add(verseWord); + break; + default: + { + if (osisTags.Count > 0) + { + foreach (OsisTag tag in osisTags) + { + List < VerseWord > verseWords1 = tag.GetVerseWords(); + if(verseWords1.Count > 0) + verseWords.AddRange(verseWords1); + } + } + } + break; + } + return verseWords; + } + public override string ToString() + { + string result = string.Empty; + switch(tagType) + { + case VerseTagType.text: + if (verseWord != null) + result = verseWord.Word; + break; + case VerseTagType.w: + string lemmaValue = string.Empty; + if (verseWord != null) + { + foreach(string s in verseWord.Strong) + { + lemmaValue += string.Format("strong:{0}{1} ", verseWord.Testament== BibleTestament.OT? "H":"G", s); + } + lemmaValue = lemmaValue.Trim(); + XmlNode tagXml1 = tagXml.Clone(); + if (tagXml1 != null) + { + ((XmlElement)tagXml1).SetAttribute("lemma", lemmaValue); // Set to new value. + result = tagXml1.OuterXml; + } + } + break; + case VerseTagType.note: + case VerseTagType.milestone: + case VerseTagType.b: + result = tagXml.OuterXml; + break; + + case VerseTagType.UNKNOWN: + string temp = tagXml.OuterXml; + string marker = tagXml.InnerXml; + int idx = temp.IndexOf(marker); + switch(marker) + { + case "$start$": + result= temp.Substring(idx+marker.Length); + break; + case "$end$": + result= temp.Substring(0,idx); + break; + default: + result = tagXml.OuterXml; + break; + } + break; + + default: + { + string result1 = string.Empty; + if (osisTags.Count > 0) + { + foreach (OsisTag tag in osisTags) + { + result1 += tag.ToString(); + } + XmlNode tagXmlTemp = tagXml.Clone(); + tagXmlTemp.InnerXml = result1; + result = tagXmlTemp.OuterXml; + if (tagType == VerseTagType.l) + { + int i = result.IndexOf("$end$"); + if (i > 0) + result = result.Remove(i, "$end$".Length); + i = result.IndexOf("$start$"); + if (i > 0) + { + int x = result.LastIndexOf(" GetOsisTags(XmlNode? root, string verseRef, int level=0) + { + List osisTags = new List(); + + foreach (XmlNode node in root.ChildNodes) + { + osisTags.Add(GetVerseTag(level,osisTags.Count, node, verseRef)); + } + + return osisTags; + } + private OsisTag GetVerseTag(int level, int index, XmlNode node, string verseRef) + { + VerseTagType tagType = VerseTagType.UNKNOWN; + switch (node.Name) + { + case "#text": + tagType = VerseTagType.text; + break; + case "w": + tagType = VerseTagType.w; + break; + case "hi": + tagType = VerseTagType.hi; + break; + case "q": + tagType = VerseTagType.q; + break; + case "l": + tagType = VerseTagType.l; + break; + case "divineName": + tagType = VerseTagType.divineName; + break; + case "note": + tagType = VerseTagType.note; + break; + case "milestone": + tagType = VerseTagType.milestone; + break; + case "b": + tagType = VerseTagType.b; + break; + } + + return new OsisTag(level, index, tagType, node, verseRef); + + } + + + } +} diff --git a/src/BibleTaggingUtil/OsisXml/OsisVerse.cs b/src/BibleTaggingUtil/OsisXml/OsisVerse.cs new file mode 100644 index 0000000..50cd5b0 --- /dev/null +++ b/src/BibleTaggingUtil/OsisXml/OsisVerse.cs @@ -0,0 +1,190 @@ +using BibleTaggingUtil; +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Reflection.Metadata; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using System.Xml; +using static System.Windows.Forms.AxHost; +using static System.Windows.Forms.VisualStyles.VisualStyleElement; + +namespace BibleTaggingUtil.OsisXml +{ + internal class OsisVerse + { + List osisTags = null; + public OsisVerse(string verseRef, int startIndex, string sid, string verseXml, string eid) + { + if(verseRef == "Ps.42.5") + { + int x = 0; + } + VerseRef = verseRef; + StartIndex = startIndex; + Length = verseXml.Length; + sID = sid; + VerseXml = verseXml; + eID = eid; + + Dirty = false; + + BuildOsisTags(); + } + + private void BuildOsisTags() + { + string header = @""; + string trailer = @""; + + // Gen.3.1 + VerseXml = TreatOddTags(VerseXml, "p"); + // Gen.2.23 + VerseXml = TreatOddTags(VerseXml, "lg"); + // 1Sam.25.1 + VerseXml = TreatOddTags(VerseXml, "div"); + // Ps.42.5 , 6 + VerseXml = TreatOddTags(VerseXml, "l"); + + XmlDocument document = new XmlDocument(); + VerseXml = VerseXml.Replace("> <", ">\uEEFF<"); + document.PreserveWhitespace = true; + try + { + document.LoadXml(header + VerseXml + trailer); + } + catch (Exception e) + { + int x = 0; + } + + + osisTags = OsisUtils.Instance.GetOsisTags(document.DocumentElement, VerseRef); + + } + + public string VerseComplteXml + { + get + { + string verseRebuild = string.Empty; + foreach (OsisTag osisTag in osisTags) + { + verseRebuild += osisTag.ToString(); + } + verseRebuild = verseRebuild.Replace(">\uEEFF<", "> <"); + + return verseRebuild; + } + } + + private string TreatOddTags(string VerseXml, string tag) + { + string s = string.Format("<{0}>", tag); + string e = string.Format("", tag); + string startAdded = string.Format("{0}$start${1}", s,e); + string endAdded = string.Empty; + + int startIndex = 0; + while (true) + { + Regex regex = new Regex(@"(<" + tag + @"[^>]*>)"); + if (tag == "div" || tag == "l") + regex = new Regex(string.Format(@"(<{0}\s[^>]+>)", tag)); + + int startP = -1; + Match match = regex.Match(VerseXml,startIndex); + if (match.Success) + { + startP = match.Index; + s = match.Groups[1].Value; + } + else + startP = VerseXml.IndexOf(s, startIndex); + + endAdded = string.Format("{0}$end${1}", s, e); + + int endP = VerseXml.IndexOf(e, startIndex); + if (startP < 0 && endP < 0) break; + if (startP < 0 && endP >= 0) + { + VerseXml = VerseXml.Remove(endP, e.Length).Insert(endP, startAdded); //Replace(e, startAdded); + startIndex = endP + startAdded.Length; + continue; + } + else if (startP >= 0 && endP < 0) + { + VerseXml = VerseXml.Remove(startP, s.Length).Insert(startP, endAdded); //Replace(s, endAdded); + startIndex = startP + endAdded.Length; + continue; + } + else if(startP < endP) + { + startIndex = endP + e.Length; + continue; + } + else if (startP > endP) + { + VerseXml = VerseXml.Remove(endP, e.Length).Insert(endP, startAdded); //Replace(e, startAdded); + startIndex = endP + startAdded.Length; + continue; + } + } + return VerseXml; + } + + internal Verse GetVerseWords() + { + Verse verse = new Verse(); + + int idx = 0; + foreach(OsisTag tag in osisTags) + { + List words = tag.GetVerseWords(); + foreach(VerseWord word in words) + { + verse[idx++] = word; + } + } + + return verse; + } + + public string VerseRefX + { + get + { + string verseRefX = string.Empty; + + Match m = Regex.Match(VerseRef, @"([1-9A-Za-z]*)\.(\d{1,3})\.(\d{1,3})"); + if (m != null) + { + string book = m.Groups[1].Value; + string chapter = m.Groups[2].Value; + string verse = m.Groups[3].Value; + + verseRefX = string.Format("{0} {1}:{2}", book, chapter, verse); + } + return verseRefX; + } + } + + public void UpdateVerse(Verse verseWords) + { + for(int i = 0; i < verseWords.Count; i++) + { + osisTags[verseWords[i].OsisTagIndex].Update(verseWords[i]); + } + + } + public string VerseRef { get; } + public int StartIndex { get; } + public int Length { get; } + public string sID { get; } + public string VerseXml { get; private set; } + public string eID { get; } + public bool Dirty { get; set; } + } +} diff --git a/src/BibleTaggingUtil/OsisXml/VerseTagType.cs b/src/BibleTaggingUtil/OsisXml/VerseTagType.cs new file mode 100644 index 0000000..8405625 --- /dev/null +++ b/src/BibleTaggingUtil/OsisXml/VerseTagType.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BibleTaggingUtil.OsisXml +{ + internal enum VerseTagType + { + text, + w, + note, + hi, + q, + l, + divineName, + milestone, + b, + UNKNOWN + } +} diff --git a/src/BibleTaggingUtil/VerseWord.cs b/src/BibleTaggingUtil/VerseWord.cs index 06967ad..7a9c13d 100644 --- a/src/BibleTaggingUtil/VerseWord.cs +++ b/src/BibleTaggingUtil/VerseWord.cs @@ -118,5 +118,13 @@ public object Clone() { return MemberwiseClone(); } + + + #region OSIS XML Support + public int OsisTagIndex { get; internal set; } + public int OsisTagLevel { get; internal set; } + + #endregion OSIS XML Support + } } From 698af4def95f184bba04a8a6ea0db6c9f3a3f334 Mon Sep 17 00:00:00 2001 From: sabdelmalik Date: Sun, 7 Jan 2024 18:07:49 -0500 Subject: [PATCH 2/5] Working version before tidu-up --- src/BibleTaggingUtil/App.config | 3 + src/BibleTaggingUtil/BibleTaggingForm.cs | 9 +- src/BibleTaggingUtil/BibleTaggingUtil.csproj | 6 +- .../BibleVersions/BibleVersion.cs | 6 +- .../BibleVersions/TargetOsisVersion.cs | 112 ++- src/BibleTaggingUtil/CofigurationHolder.cs | 5 + src/BibleTaggingUtil/Editor/EditorPanel.cs | 39 +- .../Editor/OsisTargetGridView.cs | 787 ++++++++++++++++++ src/BibleTaggingUtil/Editor/TargetGridView.cs | 31 +- src/BibleTaggingUtil/OsisXml/OsisTag.cs | 100 ++- src/BibleTaggingUtil/OsisXml/OsisVerse.cs | 7 +- .../Properties/Settings.Designer.cs | 14 +- .../Properties/Settings.settings | 3 + src/BibleTaggingUtil/Verse.cs | 6 +- 14 files changed, 1073 insertions(+), 55 deletions(-) create mode 100644 src/BibleTaggingUtil/Editor/OsisTargetGridView.cs diff --git a/src/BibleTaggingUtil/App.config b/src/BibleTaggingUtil/App.config index a52c460..86fe6cd 100644 --- a/src/BibleTaggingUtil/App.config +++ b/src/BibleTaggingUtil/App.config @@ -38,6 +38,9 @@ 0 + + False + diff --git a/src/BibleTaggingUtil/BibleTaggingForm.cs b/src/BibleTaggingUtil/BibleTaggingForm.cs index fc6708f..1d2eaff 100644 --- a/src/BibleTaggingUtil/BibleTaggingForm.cs +++ b/src/BibleTaggingUtil/BibleTaggingForm.cs @@ -35,6 +35,7 @@ public partial class BibleTaggingForm : Form private ProgressForm progressForm; private TargetVersion target; + private TargetOsisVersion osisTarget; private ReferenceVersionKJV referenceKJV; private ReferenceVersionTOTHT referenceTOTHT; private ReferenceVersionTAGNT referenceTAGNT; @@ -78,6 +79,7 @@ public BibleTaggingForm() #endif target = new TargetVersion(this); + osisTarget = new TargetOsisVersion(this); referenceKJV = new ReferenceVersionKJV(this); referenceTOTHT = new ReferenceVersionTOTHT(this); referenceTAGNT = new ReferenceVersionTAGNT(this); @@ -169,6 +171,7 @@ private void BibleTaggingForm_Load(object sender, EventArgs e) tothtPath = Path.Combine(refFolder, "TOTHT"); editorPanel.TargetVersion = target; + editorPanel.TargetOsisVersion = osisTarget; new Thread(() => { LoadBibles(); }).Start(); } @@ -330,7 +333,10 @@ private void LoadBibles() string[] files = Directory.GetFiles(taggedFolder); if (files.Length > 0) { - target.LoadBibleFile(files[0], true, false); + if(Properties.Settings.Default.Osis) + osisTarget.LoadBibleFile(files[0], true, false); + else + target.LoadBibleFile(files[0], true, false); VerseSelectionPanel.SetBookCount(target.BookCount); } else @@ -560,6 +566,7 @@ private DialogResult ShowMessageBox(string text, string caption, MessageBoxButto public EditorPanel EditorPanel { get { return editorPanel; } } public VerseSelectionPanel VerseSelectionPanel { get { return verseSelectionPanel; } } public TargetVersion Target { get { return target; } } + public TargetOsisVersion OsisTarget { get { return osisTarget; } } public ReferenceVersionKJV KJV { get {return referenceKJV; } } public ReferenceVersionTOTHT TOTHT { get { return referenceTOTHT; } } public ReferenceVersionTAGNT TAGNT { get { return referenceTAGNT; } } diff --git a/src/BibleTaggingUtil/BibleTaggingUtil.csproj b/src/BibleTaggingUtil/BibleTaggingUtil.csproj index 7339139..9433a6f 100644 --- a/src/BibleTaggingUtil/BibleTaggingUtil.csproj +++ b/src/BibleTaggingUtil/BibleTaggingUtil.csproj @@ -8,7 +8,7 @@ Copyright © 2023 by Sami Abdel Malik https://github.com/sabdelmalik/BibleTagging git - 4.4.0.0 + 5.0.0.0 Bible Text Tagging with Strong's Numbers @@ -26,6 +26,10 @@ True + + + + diff --git a/src/BibleTaggingUtil/BibleVersions/BibleVersion.cs b/src/BibleTaggingUtil/BibleVersions/BibleVersion.cs index 50f980e..3698472 100644 --- a/src/BibleTaggingUtil/BibleVersions/BibleVersion.cs +++ b/src/BibleTaggingUtil/BibleVersions/BibleVersion.cs @@ -61,7 +61,7 @@ public virtual bool LoadBibleFile(string textFilePath, bool newBible, bool more) return LoadBibleFileInternal(textFilePath, more); } - private bool LoadBibleFileInternal(string textFilePath, bool more) + protected virtual bool LoadBibleFileInternal(string textFilePath, bool more) { Tracing.TraceEntry(MethodBase.GetCurrentMethod().Name, textFilePath, more); bool result = false; @@ -133,9 +133,7 @@ public int BookCount return bookNamesList.Count; } } - public Dictionary Bible - { get { return bible; } } - + public Dictionary Bible { get { return bible; } } public string this[string ubsName] { diff --git a/src/BibleTaggingUtil/BibleVersions/TargetOsisVersion.cs b/src/BibleTaggingUtil/BibleVersions/TargetOsisVersion.cs index f7b8bbf..52f6160 100644 --- a/src/BibleTaggingUtil/BibleVersions/TargetOsisVersion.cs +++ b/src/BibleTaggingUtil/BibleVersions/TargetOsisVersion.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Reflection; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; @@ -22,7 +23,7 @@ namespace BibleTaggingUtil.BibleVersions /// 5. Once the tags has been update, the verse is reconstructed and is replaced in the text /// 6. Finally the text is saved. ///
- internal class TargetOsisVersion : BibleVersion + public class TargetOsisVersion : BibleVersion { /// /// The complete OSIS XML file is read into this string @@ -39,15 +40,51 @@ internal class TargetOsisVersion : BibleVersion /// /// Key: Verse reference in the format Gen 1:1 /// value: The parsed OSIS content of the verse - protected Dictionary osisBible = new Dictionary(); + private Dictionary osisBible = new Dictionary(); public TargetOsisVersion(BibleTaggingForm container) : base(container, 31104) { } + + protected override bool LoadBibleFileInternal(string textFilePath, bool more) + { + bool result = false; + if (File.Exists(textFilePath)) + { + try + { + Load(textFilePath); + + bookNamesList = bookOffsets.Keys.ToList(); + + if (bookNamesList.Count == 66 || bookNamesList.Count == 39) + { + for (int i = 0; i < bookNamesList.Count; i++) + { + bookNames.Add(Constants.ubsNames[i], bookNamesList[i]); + } + } + else if (bookNamesList.Count == 27) + { + for (int i = 0; i < bookNamesList.Count; i++) + { + bookNames.Add(Constants.ubsNames[i + 39], bookNamesList[i]); + } + } + + result = true; + } + catch (Exception ex) + { + + } + } + return result; + } /// /// /// /// - internal void Read(string fileName) + private void Load(string fileName) { // Read the complete OSIS XML file osisDoc = File.ReadAllText(fileName); @@ -82,8 +119,6 @@ internal void Read(string fileName) } } - - private OsisVerse? GetVersesTags(string book, Match VerseMatch) { OsisVerse result = null; @@ -125,7 +160,6 @@ internal void Read(string fileName) return result; } - internal OsisVerse GetVerse(string verseRef) { OsisVerse verseXML = null; @@ -241,18 +275,72 @@ private List GetStrongs(string value) internal void Save(string fileName) { - if (osisDoc != null) + try { - foreach (OsisVerse osisVerse in osisBible.Values) + Tracing.TraceEntry(MethodBase.GetCurrentMethod().Name); + lock (this) { - if (osisVerse.Dirty) + if (!container.EditorPanel.TargetDirty) + return; + + container.WaitCursorControl(true); + container.EditorPanel.TargetDirty = false; + container.EditorPanel.SaveCurrentVerse(); + + if (osisDoc != null) { - osisDoc = osisDoc.Remove(osisVerse.StartIndex, osisVerse.Length) - .Insert(osisVerse.StartIndex, osisVerse.VerseComplteXml); + // 1.Update osisBible from Bible + foreach (string verseRef in Bible.Keys) + { + Verse v = Bible[verseRef]; + if (v.Dirty) + { + //osisBible[verseRef].UpdateVerse(v); + osisBible[verseRef].Dirty = true; + } + } + + // 2. Update osisDoc from osisBible + foreach (OsisVerse osisVerse in osisBible.Values) + { + if (osisVerse.Dirty) + { + osisDoc = osisDoc.Remove(osisVerse.StartIndex, osisVerse.Length) + .Insert(osisVerse.StartIndex, osisVerse.VerseCompleteXml); + } + } + + // construce Updates fileName + string taggedFolder = Path.GetDirectoryName(container.Config.TaggedBible); + string oldTaggedFolder = Path.Combine(taggedFolder, "OldTagged"); + if (!Directory.Exists(oldTaggedFolder)) + Directory.CreateDirectory(oldTaggedFolder); + + // move existing tagged files to the old folder + String[] existingTagged = Directory.GetFiles(taggedFolder, "*.*"); + foreach (String existingTaggedItem in existingTagged) + { + string fName = Path.GetFileName(existingTaggedItem); + string src = Path.Combine(taggedFolder, fName); + string dst = Path.Combine(oldTaggedFolder, fName); + if (System.IO.File.Exists(dst)) + System.IO.File.Delete(src); + else + System.IO.File.Move(src, dst); + } + + string baseName = Path.GetFileNameWithoutExtension(container.Config.TaggedBible); + string updatesFileName = string.Format("{0:s}_{1:s}.txt", baseName, DateTime.Now.ToString("yyyy_MM_dd_HH_mm")); + + File.WriteAllText(Path.Combine(taggedFolder, updatesFileName), osisDoc); } } - File.WriteAllText(fileName, osisDoc); } + catch (Exception ex) + { + Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + } + container.WaitCursorControl(false); } private void BuildBookOffsets() diff --git a/src/BibleTaggingUtil/CofigurationHolder.cs b/src/BibleTaggingUtil/CofigurationHolder.cs index cc22bab..12755b4 100644 --- a/src/BibleTaggingUtil/CofigurationHolder.cs +++ b/src/BibleTaggingUtil/CofigurationHolder.cs @@ -73,6 +73,7 @@ public string ReadBiblesConfig(string biblesFolder) if (state == ParseState.TAGGING) { + bool osis = false; string[] parts = line.Split('='); if (parts.Length != 2) continue; @@ -109,6 +110,10 @@ public string ReadBiblesConfig(string biblesFolder) case "targettextdirection": Properties.Settings.Default.TargetTextDirection = parts[1].Trim(); break; + case "osis": + if(parts[1].Trim().ToLower() == "true") osis = true; + Properties.Settings.Default.Osis = osis; + break; } } diff --git a/src/BibleTaggingUtil/Editor/EditorPanel.cs b/src/BibleTaggingUtil/Editor/EditorPanel.cs index 590c69e..98f704e 100644 --- a/src/BibleTaggingUtil/Editor/EditorPanel.cs +++ b/src/BibleTaggingUtil/Editor/EditorPanel.cs @@ -16,6 +16,7 @@ using BibleTaggingUtil.BibleVersions; using System.Security.Cryptography.Xml; using System.Reflection; +using System.Xml.Linq; namespace BibleTaggingUtil.Editor { @@ -32,6 +33,7 @@ public partial class EditorPanel : DockContent private TestamentEnum testament = TestamentEnum.NEW; private System.Timers.Timer tempTimer = null; + private bool osis = false; public bool TargetDirty { get; set; } @@ -42,6 +44,12 @@ public TargetVersion TargetVersion set { dgvTarget.Bible = value; } } + public TargetOsisVersion TargetOsisVersion + { + get { return dgvTarget.OsisBible; } + set { dgvTarget.OsisBible = value; } + } + class VerseDetails { public VerseDetails() @@ -164,6 +172,8 @@ public void SaveCurrentVerse() } private void Verse_VerseChanged(object sender, VerseChangedEventArgs e) { + osis = Properties.Settings.Default.Osis; + strongsPrefix = e.StrongsPrefix; testament = e.Testament; @@ -243,17 +253,23 @@ private void Verse_VerseChanged(object sender, VerseChangedEventArgs e) // Target view if (!string.IsNullOrEmpty(oldReference) && dgvTarget.Columns.Count > 0) { - dgvTarget.SaveVerse(oldReference); + if(osis) + { + // TODO handle osis save + } + else + dgvTarget.SaveVerse(oldReference); } - actualBookName = container.Target[bookName]; + + actualBookName = osis? container.OsisTarget[bookName] : container.Target[bookName]; if (!string.IsNullOrEmpty(actualBookName)) { string reference = e.VerseReference.Replace(bookName, actualBookName); //targetUpdatedVerse = Utils.GetVerseText(container.Target.Bible[targetRef], true); if (tbTarget.Text.ToLower().Contains("arabic")) DoSepecialHandling(reference); - Verse v = container.Target.Bible[reference]; + Verse v = osis? container.OsisTarget.Bible[reference]: container.Target.Bible[reference]; if (dgvTarget.IsCurrentTextAramaic) { @@ -309,6 +325,8 @@ private void Verse_VerseChanged(object sender, VerseChangedEventArgs e) /// private void DoSepecialHandling(string verseReference) { + if (osis) return; + bool changed = false; Verse v = container.Target.Bible[verseReference]; //List merges = new List(); @@ -630,7 +648,13 @@ private void DgvTarget_KeyUp(object sender, KeyEventArgs e) } else if (e.Control) { - if (e.KeyCode == Keys.S) container.Target.SaveUpdates(); + if (e.KeyCode == Keys.S) + { + if (osis) + container.OsisTarget.Save(""); + else + container.Target.SaveUpdates(); + } if (e.KeyCode == Keys.Y) dgvTarget.Redo(); if (e.KeyCode == Keys.Z) dgvTarget.Undo(); } @@ -659,7 +683,10 @@ private void picUndo_Click(object sender, EventArgs e) private void picSave_Click(object sender, EventArgs e) { - container.Target.SaveUpdates(); + if (osis) + container.OsisTarget.Save(""); + else + container.Target.SaveUpdates(); } private void picDecreaseFont_Click(object sender, EventArgs e) @@ -751,7 +778,7 @@ private void picRefresh_Click(object sender, EventArgs e) string reference = tbCurrentReference.Text; dgvTarget.SaveVerse(reference); - Verse v = container.Target.Bible[reference]; + Verse v = osis? container.OsisTarget.Bible[reference] : container.Target.Bible[reference]; dgvTarget.Update(v); dgvTarget.CurrentCell = dgvTarget[savedColumn, savedRow]; diff --git a/src/BibleTaggingUtil/Editor/OsisTargetGridView.cs b/src/BibleTaggingUtil/Editor/OsisTargetGridView.cs new file mode 100644 index 0000000..2affeb9 --- /dev/null +++ b/src/BibleTaggingUtil/Editor/OsisTargetGridView.cs @@ -0,0 +1,787 @@ +using Microsoft.VisualBasic.Devices; +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +using BibleTaggingUtil.BibleVersions; +using System.Security.Cryptography.X509Certificates; +using System.Threading; +using System.ComponentModel; +using System.Xml.Linq; +using static WeifenLuo.WinFormsUI.Docking.DockPanel; +using System.Reflection; +using System.Text.RegularExpressions; + +namespace BibleTaggingUtil.Editor +{ + public class OsisTargetGridView : DataGridView + { + + public event VerseViewChangedEventHandler VerseViewChanged; + public event RefernceHighlightRequestEventHandler RefernceHighlightRequest; + public event GotoVerseRequestEventHandler GotoVerseRequest; + + private FixedSizeStack undoStack = new FixedSizeStack(); + private FixedSizeStack redoStack = new FixedSizeStack(); + + public OsisTargetGridView() + { + this.ContextMenuStrip = new ContextMenuStrip(); + this.ContextMenuStrip.Opening += ContextMenuStrip_Opening; + this.ContextMenuStrip.ItemClicked += ContextMenuStrip_ItemClicked; + } + + public string CurrentVerseReferece { get; set; } + + public string SearchTag { get; set; } + + public Verse CurrentVerse { get; set; } + + public TargetVersion Bible { get; set; } + + public bool IsLastWord + { + get + { + if (this.SelectedCells.Count == 1 && + this.SelectedCells[0].ColumnIndex == Columns.Count - 1) + return true; + else + return false; + } + } + + public bool IsFirstWord + { + get + { + if (this.SelectedCells.Count == 1 && + this.SelectedCells[0].ColumnIndex == 0) + return true; + else + return false; + } + } + + public bool IsCurrentTextAramaic + { + get + { + bool result = false; + + string referencePattern = @"^([0-9A-Za-z]+)\s([0-9]+):([0-9]+)"; + Match mTx = Regex.Match(CurrentVerseReferece, referencePattern); + if (!mTx.Success) + { + Tracing.TraceError(MethodBase.GetCurrentMethod().Name, "Incorrect reference format: " + CurrentVerseReferece); + return result; + } + + String book = mTx.Groups[1].Value; + string chapter = mTx.Groups[2].Value; + string verse = mTx.Groups[3].Value; + int ch = 0; + int vs = 0; + if (!int.TryParse(chapter, out ch)) + return result; + if (!int.TryParse(verse, out vs)) + return result; + if ((book == "Gen" && ch == 31 && vs == 47) || + (book == "Ezr" && ((ch == 4 && vs >= 8) || (ch == 5) || (ch == 6 && vs <= 18))) || + (book == "Ezr" && (ch >= 7 && vs >= 12 && vs <= 26)) || + (book == "Pro" && ch == 31 && vs == 2) || + (book == "Jer" && ch == 10 && vs == 11) || + (book == "Dan" && ((ch == 2 && vs >= 4) || (ch > 2 && ch < 7) || (ch == 7 && vs <= 28)))) + result = true; + + return result; + } + } + + #region Context Menue + + private const string MERGE_CONTEXT_MENU = "Merge"; + private const string SWAP_CONTEXT_MENU = "Swap Tags"; + private const string SPLIT_CONTEXT_MENU = "Split"; + private const string DELETE_CONTEXT_MENU = "Delete Tag"; + private const string REVERSE_CONTEXT_MENU = "Reverse Tags"; + private const string DELETE_LEFT_CONTEXT_MENU = "Delete Left Tags"; + private const string DELETE_RIGHT_CONTEXT_MENU = "Delete Right Tags"; + + private void ContextMenuStrip_Opening(object sender, System.ComponentModel.CancelEventArgs e) + { + e.Cancel = true; + if (this.SelectedCells.Count == 0) + return; + + if (this.SelectedCells.Count > 1) + { + bool sameRow = true; + foreach (DataGridViewCell cell in this.SelectedCells) + { + // we only merge in the top row + if (cell.RowIndex != 0) + { + sameRow = false; + break; + } + } + + bool mergeOk = true; // we only merge adjacent cells + int colIndex = SelectedCells[0].ColumnIndex; + for (int i = 1; i < this.SelectedCells.Count; i++) + { + if(Math.Abs(SelectedCells[i].ColumnIndex - colIndex) != 1) + { + mergeOk = false; + break; + } + colIndex = SelectedCells[i].ColumnIndex; + } + + + if (sameRow) + { + this.ContextMenuStrip.Items.Clear(); + ToolStripMenuItem mergeMenuItem = new ToolStripMenuItem(MERGE_CONTEXT_MENU); + ToolStripMenuItem swapMenuItem = new ToolStripMenuItem(SWAP_CONTEXT_MENU); + + if(mergeOk) + this.ContextMenuStrip.Items.Add(mergeMenuItem); + if(this.SelectedCells.Count == 2) + this.ContextMenuStrip.Items.Add(swapMenuItem); + e.Cancel = false; + } + } + else + { + if (this.SelectedCells[0].RowIndex == 1) + { + string text = (String)this.SelectedCells[0].Value; + + if (string.IsNullOrEmpty(text)) + return; + + this.ContextMenuStrip.Items.Clear(); + + this.ContextMenuStrip.Items.Clear(); + + string[] strings = text.Trim().Split(' '); + if (strings.Length > 1) + { + ToolStripMenuItem reverseMenuItem = new ToolStripMenuItem(REVERSE_CONTEXT_MENU); + this.ContextMenuStrip.Items.Add(reverseMenuItem); + + ToolStripMenuItem deleteLeftMenuItem = new ToolStripMenuItem(DELETE_LEFT_CONTEXT_MENU); + this.ContextMenuStrip.Items.Add(deleteLeftMenuItem); + + ToolStripMenuItem deleteRightMenuItem = new ToolStripMenuItem(DELETE_RIGHT_CONTEXT_MENU); + this.ContextMenuStrip.Items.Add(deleteRightMenuItem); + } + + ToolStripMenuItem deleteMenuItem = new ToolStripMenuItem(DELETE_CONTEXT_MENU); + this.ContextMenuStrip.Items.Add(deleteMenuItem); + + + e.Cancel = false; + } + else + { + this.ContextMenuStrip.Items.Clear(); + ToolStripMenuItem deleteMenuItem = new ToolStripMenuItem(SPLIT_CONTEXT_MENU); + + this.ContextMenuStrip.Items.Add(deleteMenuItem); + e.Cancel = false; + } + } + } + + private void ContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) + { + if (e.ClickedItem.Text == MERGE_CONTEXT_MENU) + { + int savedColumn = this.CurrentCell.ColumnIndex; + int savedRow = this.CurrentCell.RowIndex; + if (this.CurrentVerse != null) + undoStack.Push(new VerseEx(new Verse(this.CurrentVerse), savedColumn, savedRow)); + + int firstMergeIndex = this.SelectedCells[0].ColumnIndex; + for (int i = 1; i < this.SelectedCells.Count; i++) + { + if (this.SelectedCells[i].ColumnIndex < firstMergeIndex) + firstMergeIndex = this.SelectedCells[i].ColumnIndex; + } + this.CurrentVerse.Merge(firstMergeIndex, this.SelectedCells.Count); + + SaveVerse(CurrentVerseReferece); + this.Update(CurrentVerse); + + this[firstMergeIndex, 1].Selected = true; + this.CurrentCell = this[firstMergeIndex, 1]; + if (!string.IsNullOrEmpty((string)this[firstMergeIndex, 1].Value)) + FireRefernceHighlightRequest((string)this[firstMergeIndex, 1].Value); + FireVerseViewChanged(); + } + else if (e.ClickedItem.Text == SWAP_CONTEXT_MENU) + { + int savedColumn = this.CurrentCell.ColumnIndex; + int savedRow = this.CurrentCell.RowIndex; + if (this.CurrentVerse != null) + undoStack.Push(new VerseEx(new Verse(this.CurrentVerse), savedColumn, savedRow)); + + int firstSwapIndex = this.SelectedCells[0].ColumnIndex; + int secondSwapIndex = this.SelectedCells[1].ColumnIndex; + //for (int i = 1; i < this.SelectedCells.Count; i++) + //{ + // if (this.SelectedCells[i].ColumnIndex < firstSwapIndex) + // firstSwapIndex = this.SelectedCells[i].ColumnIndex; + //} + this.CurrentVerse.SwapTags(firstSwapIndex, secondSwapIndex); + + SaveVerse(CurrentVerseReferece); + this.Update(CurrentVerse); + + this[firstSwapIndex, 1].Selected = true; + this.CurrentCell = this[firstSwapIndex, 1]; + if (!string.IsNullOrEmpty((string)this[firstSwapIndex, 1].Value)) + FireRefernceHighlightRequest((string)this[firstSwapIndex, 1].Value); + FireVerseViewChanged(); + } + else if (e.ClickedItem.Text == SPLIT_CONTEXT_MENU) + { + int savedColumn = this.CurrentCell.ColumnIndex; + int savedRow = this.CurrentCell.RowIndex; + if (this.CurrentVerse != null) + undoStack.Push(new VerseEx(new Verse(this.CurrentVerse), savedColumn, savedRow)); + + int splitIndex = this.SelectedCells[0].ColumnIndex; + for (int i = 1; i < this.SelectedCells.Count; i++) + { + if (this.SelectedCells[i].ColumnIndex < splitIndex) + splitIndex = this.SelectedCells[i].ColumnIndex; + } + this.CurrentVerse.split(splitIndex); + + SaveVerse(CurrentVerseReferece); + this.Update(CurrentVerse); + + this[splitIndex, 1].Selected = true; + this.CurrentCell = this[splitIndex, 1]; + if (!string.IsNullOrEmpty((string)this[splitIndex, 1].Value)) + FireRefernceHighlightRequest((string)this[splitIndex, 1].Value); + FireVerseViewChanged(); + } + else if (e.ClickedItem.Text == DELETE_CONTEXT_MENU) + { + int savedColumn = this.CurrentCell.ColumnIndex; + int savedRow = this.CurrentCell.RowIndex; + if (this.CurrentVerse != null) + undoStack.Push(new VerseEx(new Verse(this.CurrentVerse), savedColumn, savedRow)); + + int col = this.SelectedCells[0].ColumnIndex; + this.CurrentVerse[col].Strong = new string[] { string.Empty }; + SaveVerse(CurrentVerseReferece); + this.Update(CurrentVerse); + + this[col, 1].Selected = true; + this.CurrentCell = this[col, 1]; + FireVerseViewChanged(); + } + else + { + string[] strings = ((string)this.SelectedCells[0].Value).Split(' '); + if (strings.Length < 2) return; + + int savedColumn = this.CurrentCell.ColumnIndex; + int savedRow = this.CurrentCell.RowIndex; + if (this.CurrentVerse != null) + undoStack.Push(new VerseEx(new Verse(this.CurrentVerse), savedColumn, savedRow)); + + string newText = string.Empty; + int col = 0; + if (e.ClickedItem.Text == REVERSE_CONTEXT_MENU) + { + newText = strings[strings.Length - 1]; + for (int i = strings.Length - 2; i >= 0; i--) + { + newText += " " + strings[i]; + } + this.SelectedCells[0].Value = newText; + + col = this.SelectedCells[0].ColumnIndex; + this.CurrentVerse[col].StrongString = newText; + SaveVerse(CurrentVerseReferece); + FireVerseViewChanged(); + } + else if (e.ClickedItem.Text == DELETE_LEFT_CONTEXT_MENU) + { + for (int i = 1; i < strings.Length; i++) + { + newText += string.IsNullOrEmpty(newText) ? strings[i] : " " + strings[i]; + } + this.SelectedCells[0].Value = newText; + + col = this.SelectedCells[0].ColumnIndex; + this.CurrentVerse[col].StrongString = newText; + SaveVerse(CurrentVerseReferece); + FireVerseViewChanged(); + } + else if (e.ClickedItem.Text == DELETE_RIGHT_CONTEXT_MENU) + { + for (int i = 0; i < strings.Length - 1; i++) + { + newText += string.IsNullOrEmpty(newText) ? strings[i] : " " + strings[i]; + } + this.SelectedCells[0].Value = newText; + + col = this.SelectedCells[0].ColumnIndex; + this.CurrentVerse[col].StrongString = newText; + SaveVerse(CurrentVerseReferece); + FireVerseViewChanged(); + } + this[col, 1].Selected = true; + this.CurrentCell = this[col, 1]; + if (!string.IsNullOrEmpty((string)this[col, 1].Value)) + FireRefernceHighlightRequest((string)this[col, 1].Value); + FireVerseViewChanged(); + } + } + + #endregion Context Menue + + #region Save & Update + /// + /// Updates the target verse display when the verse contains tags already + /// + /// tagged verse + public void Update(Verse verse) + { + //« + try + { + if (verse == null) + { + return; + } + + this.CurrentVerse = verse; + bool oldTestament = false; + + string direction = Properties.Settings.Default.TargetTextDirection; + this.Rows.Clear(); + + string[] verseWords = new string[verse.Count]; + string[] verseTags = new string[verse.Count]; + oldTestament = (verse[0].Testament == BibleTestament.OT); + for (int i = 0; i < verse.Count; i++) + { + verseWords[i] = verse[i].Word; + for (int j = 0; j < verse[i].Strong.Length; j++) + verseTags[i] += "<" + verse[i].Strong[j] + "> "; + verseTags[i] = verseTags[i].Trim(); + } + + int col = -1; + for (int i = 0; i < verseTags.Length; i++) + { + // if (verseTags[i].Contains("3068")) //verseTags[i].Contains("???") || verseTags[i].Contains("0000")) + //{ + // col = i; + // break; + // } + if (verseTags[i] == "<>") + verseTags[i] = string.Empty; + } + + + this.ColumnCount = verseWords.Length; + this.Rows.Add(verseWords); + this.Rows.Add(verseTags); + + string tagToHighlight = SearchTag; + if (string.IsNullOrEmpty(tagToHighlight) || tagToHighlight.ToLower() == "") + tagToHighlight = "<>"; + for (int i = 0; i < verseWords.Length; i++) + { + string word = (string)this.Rows[0].Cells[i].Value; + string tag = (string)this.Rows[1].Cells[i].Value; + string nextTag = string.Empty; + if(i < verseWords.Length - 1) + { + nextTag = (string)this.Rows[1].Cells[i+1].Value; + } + if (tag == null) + continue; + + if ((tag.Contains("3068") || tag.Contains("3069")) && oldTestament) + { + this.Rows[1].Cells[i].Style.ForeColor = Color.Red; + } + if (tag.Contains("0430>") && oldTestament) + { + //this.Rows[1].Cells[i].Style.BackColor = Color.Green; + this.Rows[1].Cells[i].Style.ForeColor = Color.Green; + } + + if (tag.Contains("0410>") && oldTestament) + this.Rows[1].Cells[i].Style.ForeColor = Color.Blue; + + if (tag.Contains(tagToHighlight) || tag.Contains("0000") || (tag == string.Empty && tagToHighlight == "<>")) + this.Rows[1].Cells[i].Style.BackColor = Color.LightGray; + else if (!string.IsNullOrEmpty(tag) && tag == nextTag) + { + this.Rows[1].Cells[i].Style.BackColor = Color.Yellow; + this.Rows[1].Cells[i+1].Style.BackColor = Color.Yellow; + } + + if (direction.ToLower() == "rtl") + this.Columns[i].DisplayIndex = verseWords.Length - i - 1; + } + + if (col >= 0) + this.CurrentCell = this.Rows[1].Cells[col]; + + this.ClearSelection(); + + this.Rows[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; + this.Rows[0].ReadOnly = true; + this[0,1].Selected = true; + this.CurrentCell = this[0, 1]; + if(!string.IsNullOrEmpty((string)this[0, 1].Value)) + FireRefernceHighlightRequest((string)this[0, 1].Value); + //this.Rows[1].ReadOnly = true; + + } + catch (Exception ex) + { + Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + } + } + + + /// + /// + /// + /// + public void SaveVerse(Verse verse) + { + Bible.Bible[verse[0].Reference] = verse; + if (!Utils.AreReferencesEqual(CurrentVerseReferece, verse[0].Reference)) + { + FireGotoVerseRequest(verse[0].Reference); + } + } + + + /// + /// + /// + /// + public void SaveVerse(string reference) + { + Verse verse = new Verse(); + + for (int i = 0; i < this.Columns.Count; i++) + { + string[] tags; + string tag = ((string)this[i, 1].Value); + if (string.IsNullOrEmpty(tag)) + tags = new string[] { "<>" }; + else + tags = tag.Split(' '); + + // remove <> from tags + for (int j = 0; j < tags.Length; j++) + tags[j] = tags[j].Replace("<", "").Replace(">", ""); + + verse[i] = new VerseWord((string)this[i, 0].Value, tags, reference); + } + + reference = Bible.GetCorrectReference(reference); + + if (Bible.Bible.ContainsKey(reference)) + { + Bible.Bible[reference] = verse; + } + else + { + Bible.Bible.Add(reference, verse); + } + } + #endregion Save & Update + + + #region overrides + /// + /// + /// + /// + protected override void OnCellValueChanged(DataGridViewCellEventArgs e) + { + if (e.RowIndex == 0) + { + string newWord = (string)this[e.ColumnIndex, e.RowIndex].Value; + CurrentVerse.UpdateWord(e.ColumnIndex, newWord); +// FireVerseViewChanged(); + } + base.OnCellValueChanged(e); + } + + /// + /// + /// + /// + protected override void OnCellEnter(DataGridViewCellEventArgs e) + { + if (this.Rows.Count > 1) + { + // during initialsation, we may come here + // when the grid rows are not fully initialised + //if (this.SelectedRows.Count > 1) + //{ + //} + if(e.RowIndex == 1) + { + this.ClearSelection(); + this[e.ColumnIndex, e.RowIndex].Selected = true; + } + FireRefernceHighlightRequest((string)this.Rows[1].Cells[e.ColumnIndex].Value); + + } + //base.OnCellEnter(e); + } + + /// + /// + /// + /// + //protected override void OnColumnRemoved(DataGridViewColumnEventArgs e) + //{ + // FireVerseViewChanged(); + // base.OnColumnRemoved(e); + //} + + protected override void OnKeyUp(KeyEventArgs e) + { + if (e.KeyCode == Keys.Delete) + { + if(this.SelectedCells.Count == 1) + { + int savedColumn = this.CurrentCell.ColumnIndex; + int savedRow = this.CurrentCell.RowIndex; + if (this.CurrentVerse != null) + undoStack.Push(new VerseEx(new Verse(this.CurrentVerse), savedColumn, savedRow)); + + int col = this.SelectedCells[0].ColumnIndex; + this[col, 1].Value = string.Empty; + this.CurrentVerse[col].Strong = new string[] { string.Empty }; + SaveVerse(CurrentVerseReferece); + this.Update(CurrentVerse); + + } + } + + base.OnKeyUp(e); + } + + /// + /// + /// + /// + protected override void OnDragEnter(DragEventArgs drgevent) + { + drgevent.Effect = DragDropEffects.Copy; + base.OnDragEnter(drgevent); + } + + + protected override void OnDragDrop(DragEventArgs drgevent) + { + DragData data = drgevent.Data.GetData(typeof(DragData)) as DragData; + string droppedValue = data.Text; + Point cursorLocation = this.PointToClient(new Point(drgevent.X, drgevent.Y)); + + System.Windows.Forms.DataGridView.HitTestInfo hittest = this.HitTest(cursorLocation.X, cursorLocation.Y); + if (hittest.ColumnIndex != -1 + && hittest.RowIndex == 1) + { //CHANGE + if (data.Source.Equals(this)) + { + if (data.ColumnIndex == hittest.ColumnIndex) + return; + } + + droppedValue = droppedValue.Replace("+G", "> <"); + string[] droppedValueParts = droppedValue.Split(' '); + String newValue = string.Empty; + + string currentValue = (string)this[hittest.ColumnIndex, 1].Value; + + if (!string.IsNullOrEmpty(currentValue) && !currentValue.Contains("???") && Control.ModifierKeys == Keys.Control) + newValue = currentValue; + + // special Handling for Aramaic + if (droppedValueParts.Length == 2 && IsCurrentTextAramaic && data.Source is Editor.TOHTHGridView) + { + droppedValueParts = new string[1] { droppedValueParts[1] }; + } + + for (int i = 0; i < droppedValueParts.Length; i++) + { + if (droppedValueParts[i].Contains('(')) + continue; //skip morphology + + string tmp = droppedValueParts[i].Trim().Replace("<", "").Replace(">", ""); + tmp = ("0000" + tmp).Substring(tmp.Length); + int val = Convert.ToInt32(tmp); + if (val > 0) + { + tmp = "<" + tmp + ">"; + // if (!droppedValue.Contains(tmp) || Control.ModifierKeys == Keys.Alt) + newValue += string.IsNullOrEmpty(droppedValue) ? tmp : (" " + tmp); + } + } + int savedColumn = this.CurrentCell.ColumnIndex; + int savedRow = this.CurrentCell.RowIndex; + if (this.CurrentVerse != null) + undoStack.Push(new VerseEx(new Verse(this.CurrentVerse), savedColumn, savedRow)); + + this[hittest.ColumnIndex, 1].Value = newValue.Trim(); + this.CurrentVerse[hittest.ColumnIndex].StrongString = newValue.Trim(); + + FireRefernceHighlightRequest(newValue); + + if (data.Source.Equals(this)) + { + this[data.ColumnIndex, 1].Value = string.Empty; + this.CurrentVerse[data.ColumnIndex].StrongString = string.Empty; + } + SaveVerse(CurrentVerseReferece); + FireVerseViewChanged(); + + this.ClearSelection(); + this[hittest.ColumnIndex, 1].Selected = true; + this.Rows[0].ReadOnly = true; + + this[hittest.ColumnIndex, 1].Selected = true; + this.CurrentCell = this[hittest.ColumnIndex, 1]; + if (!string.IsNullOrEmpty((string)this[hittest.ColumnIndex, 1].Value)) + FireRefernceHighlightRequest((string)this[hittest.ColumnIndex, 1].Value); + + this.Focus(); + } + + base.OnDragDrop(drgevent); + } + + /// + /// + /// + /// + protected override void OnMouseDown(MouseEventArgs e) + { + if (e.Button == MouseButtons.Left && e.Clicks == 1) + { + DataGridView.HitTestInfo info = this.HitTest(e.X, e.Y); + if (info.RowIndex > 0) + { + if (info.RowIndex >= 0 && info.ColumnIndex >= 0) + { + string text = (String)this.Rows[1].Cells[info.ColumnIndex].Value; + DragData data = new DragData(1, info.ColumnIndex, text, this); + if (text != null) + { + //Need to put braces here CHANGE + this.DoDragDrop(data, DragDropEffects.Copy); + } + } + } + } + base.OnMouseDown(e); + } + + #endregion overrides + + #region Undo / Redo + public void Undo() + { + if (undoStack.Count > 0) + { + int savedColumn = this.CurrentCell.ColumnIndex; + int savedRow = this.CurrentCell.RowIndex; + redoStack.Push(new VerseEx(new Verse(this.CurrentVerse), savedColumn, savedRow)); + + VerseEx verseEx = undoStack.Pop(); + Verse verse = verseEx.SavedVerse; + + if (Utils.AreReferencesEqual(CurrentVerseReferece, verse[0].Reference)) + { + Update(verse); + } + SaveVerse(verse); + this.CurrentCell = this[verseEx.Colum, verseEx.Row]; + } + } + + public void Redo() + { + if (redoStack.Count > 0) + { + VerseEx verseEx = redoStack.Pop(); + Verse verse = verseEx.SavedVerse; + if (Utils.AreReferencesEqual(CurrentVerseReferece, verse[0].Reference)) + { + Update(verse); + } + SaveVerse(verse); + this.CurrentCell = this[verseEx.Colum, verseEx.Row]; + } + } + + #endregion Undo / Redo + + #region Firing events + public void FireVerseViewChanged() + { + if (this.VerseViewChanged != null) + { + this.VerseViewChanged(this, EventArgs.Empty); + } + + } + + public void FireRefernceHighlightRequest(string tag) + { + if (this.RefernceHighlightRequest != null) + { + bool firstHalf = true; + if(this.SelectedCells.Count == 1) + { + if (this.SelectedCells[0].ColumnIndex > (this.ColumnCount/2)) + firstHalf = false; + } + this.RefernceHighlightRequest(this, tag, firstHalf); + } + + } + + public void FireGotoVerseRequest(string tag) + { + if (this.GotoVerseRequest != null) + { + this.GotoVerseRequest(this, tag); + } + + } + #endregion Firing events + + } + public delegate void VerseViewChangedEventHandler(object sender, EventArgs e); + public delegate void RefernceHighlightRequestEventHandler(object sender, string tag, bool firstHalf); + public delegate void GotoVerseRequestEventHandler(object sender, string reference); + +} diff --git a/src/BibleTaggingUtil/Editor/TargetGridView.cs b/src/BibleTaggingUtil/Editor/TargetGridView.cs index 6e9ab8b..2b746f8 100644 --- a/src/BibleTaggingUtil/Editor/TargetGridView.cs +++ b/src/BibleTaggingUtil/Editor/TargetGridView.cs @@ -42,6 +42,7 @@ public TargetGridView() public Verse CurrentVerse { get; set; } public TargetVersion Bible { get; set; } + public TargetOsisVersion OsisBible { get; internal set; } public bool IsLastWord { @@ -102,6 +103,7 @@ public bool IsCurrentTextAramaic } } + #region Context Menue private const string MERGE_CONTEXT_MENU = "Merge"; @@ -382,7 +384,11 @@ public void Update(Verse verse) verseWords[i] = verse[i].Word; for (int j = 0; j < verse[i].Strong.Length; j++) verseTags[i] += "<" + verse[i].Strong[j] + "> "; - verseTags[i] = verseTags[i].Trim(); + if (verseTags[i] == null) + verseTags[i] = string.Empty; + else + verseTags[i] = verseTags[i].Trim(); + } int col = -1; @@ -469,7 +475,11 @@ public void Update(Verse verse) /// public void SaveVerse(Verse verse) { - Bible.Bible[verse[0].Reference] = verse; + if (Properties.Settings.Default.Osis) + OsisBible.Bible[verse[0].Reference] = verse; + else + Bible.Bible[verse[0].Reference] = verse; + if (!Utils.AreReferencesEqual(CurrentVerseReferece, verse[0].Reference)) { FireGotoVerseRequest(verse[0].Reference); @@ -483,6 +493,8 @@ public void SaveVerse(Verse verse) /// public void SaveVerse(string reference) { + bool osis = Properties.Settings.Default.Osis; + Verse verse = new Verse(); for (int i = 0; i < this.Columns.Count; i++) @@ -499,17 +511,24 @@ public void SaveVerse(string reference) tags[j] = tags[j].Replace("<", "").Replace(">", ""); verse[i] = new VerseWord((string)this[i, 0].Value, tags, reference); + if (osis) + { + verse[i].OsisTagIndex = CurrentVerse[i].OsisTagIndex; + verse[i].OsisTagLevel = CurrentVerse[i].OsisTagLevel; + verse.Dirty = true; + } } - reference = Bible.GetCorrectReference(reference); + reference = osis? OsisBible.GetCorrectReference(reference) : Bible.GetCorrectReference(reference); - if (Bible.Bible.ContainsKey(reference)) + Dictionary bible = osis ? OsisBible.Bible : Bible.Bible; + if (bible.ContainsKey(reference)) { - Bible.Bible[reference] = verse; + bible[reference] = verse; } else { - Bible.Bible.Add(reference, verse); + bible.Add(reference, verse); } } #endregion Save & Update diff --git a/src/BibleTaggingUtil/OsisXml/OsisTag.cs b/src/BibleTaggingUtil/OsisXml/OsisTag.cs index 2710ecd..708671b 100644 --- a/src/BibleTaggingUtil/OsisXml/OsisTag.cs +++ b/src/BibleTaggingUtil/OsisXml/OsisTag.cs @@ -25,14 +25,14 @@ public OsisTag(int level, int index, VerseTagType tagType, XmlNode tagXML, strin tagXml = tagXML; this.level = level; this.index = index; - + string verseRefX = GetVerseRefX(verseRef); switch (tagType) { case VerseTagType.text: - CreateUntaggedWord(index, tagXML, verseRef); + CreateUntaggedWord(index, tagXML, verseRefX); break; case VerseTagType.w: - CreateTaggedWord(index, tagXML, verseRef); + CreateTaggedWord(index, tagXML, verseRefX); break; case VerseTagType.note: case VerseTagType.milestone: @@ -52,6 +52,22 @@ public OsisTag(int level, int index, VerseTagType tagType, XmlNode tagXML, strin } } + private string GetVerseRefX(string VerseRef) + { + string verseRefX = string.Empty; + + Match m = Regex.Match(VerseRef, @"([1-9A-Za-z]*)\.(\d{1,3})\.(\d{1,3})"); + if (m != null) + { + string book = m.Groups[1].Value; + string chapter = m.Groups[2].Value; + string verse = m.Groups[3].Value; + + verseRefX = string.Format("{0} {1}:{2}", book, chapter, verse); + } + return verseRefX; + } + private void CreateUntaggedWord(int index, XmlNode tagXML, string verseRef) { verseWord = new VerseWord(tagXML.InnerText, new string[0], verseRef); @@ -124,26 +140,50 @@ public List GetVerseWords() public override string ToString() { string result = string.Empty; - switch(tagType) + switch (tagType) { case VerseTagType.text: - if (verseWord != null) - result = verseWord.Word; - break; - case VerseTagType.w: - string lemmaValue = string.Empty; + if (verseWord != null) { - foreach(string s in verseWord.Strong) + if (verseWord.Strong.Length > 1 || + (verseWord.Strong.Length == 1 && !string.IsNullOrEmpty(verseWord.Strong[0]))) { - lemmaValue += string.Format("strong:{0}{1} ", verseWord.Testament== BibleTestament.OT? "H":"G", s); + string lemmaValue = string.Empty; + foreach (string s in verseWord.Strong) + { + lemmaValue += string.Format("strong:{0}{1} ", verseWord.Testament == BibleTestament.OT ? "H" : "G", s); + } + lemmaValue = lemmaValue.Trim(); + result = string.Format("{1}", lemmaValue, verseWord.Word); } - lemmaValue = lemmaValue.Trim(); - XmlNode tagXml1 = tagXml.Clone(); - if (tagXml1 != null) + else + result = verseWord.Word; + } + + break; + case VerseTagType.w: + if (verseWord.Strong.Length == 0 || + (verseWord.Strong.Length == 1 && string.IsNullOrEmpty(verseWord.Strong[0]))) + { + result = verseWord.Word; + } + else + { + string lemmaValue = string.Empty; + if (verseWord != null) { - ((XmlElement)tagXml1).SetAttribute("lemma", lemmaValue); // Set to new value. - result = tagXml1.OuterXml; + foreach (string s in verseWord.Strong) + { + lemmaValue += string.Format("strong:{0}{1} ", verseWord.Testament == BibleTestament.OT ? "H" : "G", s); + } + lemmaValue = lemmaValue.Trim(); + XmlNode tagXml1 = tagXml.Clone(); + if (tagXml1 != null) + { + ((XmlElement)tagXml1).SetAttribute("lemma", lemmaValue); // Set to new value. + result = tagXml1.OuterXml; + } } } break; @@ -157,13 +197,13 @@ public override string ToString() string temp = tagXml.OuterXml; string marker = tagXml.InnerXml; int idx = temp.IndexOf(marker); - switch(marker) + switch (marker) { case "$start$": - result= temp.Substring(idx+marker.Length); + result = temp.Substring(idx + marker.Length); break; case "$end$": - result= temp.Substring(0,idx); + result = temp.Substring(0, idx); break; default: result = tagXml.OuterXml; @@ -207,9 +247,29 @@ public override string ToString() internal void Update(VerseWord verseWord) { + // Currently we only update Strong's numbers + if(level == verseWord.OsisTagLevel) { - + VerseWord w; + if (verseWord != null) w = verseWord; + else + { + w = osisTags[verseWord.OsisTagIndex].verseWord; + } + w.Strong = verseWord.Strong; + if(tagType == VerseTagType.w) + { + if(w.Strong.Length == 0 || + (w.Strong.Length == 1 && string.IsNullOrEmpty(w.Strong[0]))) + tagType = VerseTagType.text; + } + else if(tagType == VerseTagType.text) + { + if (w.Strong.Length > 1 || + (w.Strong.Length == 1 && !string.IsNullOrEmpty(w.Strong[0]))) + tagType = VerseTagType.w; + } } } diff --git a/src/BibleTaggingUtil/OsisXml/OsisVerse.cs b/src/BibleTaggingUtil/OsisXml/OsisVerse.cs index 50cd5b0..159d663 100644 --- a/src/BibleTaggingUtil/OsisXml/OsisVerse.cs +++ b/src/BibleTaggingUtil/OsisXml/OsisVerse.cs @@ -15,6 +15,7 @@ namespace BibleTaggingUtil.OsisXml { internal class OsisVerse { + private const string zeroWidthSpace = "\u200B"; List osisTags = null; public OsisVerse(string verseRef, int startIndex, string sid, string verseXml, string eid) { @@ -49,7 +50,7 @@ private void BuildOsisTags() VerseXml = TreatOddTags(VerseXml, "l"); XmlDocument document = new XmlDocument(); - VerseXml = VerseXml.Replace("> <", ">\uEEFF<"); + VerseXml = VerseXml.Replace("> <", ">" + zeroWidthSpace + "<"); document.PreserveWhitespace = true; try { @@ -65,7 +66,7 @@ private void BuildOsisTags() } - public string VerseComplteXml + public string VerseCompleteXml { get { @@ -74,7 +75,7 @@ public string VerseComplteXml { verseRebuild += osisTag.ToString(); } - verseRebuild = verseRebuild.Replace(">\uEEFF<", "> <"); + verseRebuild = verseRebuild.Replace(">" + zeroWidthSpace + "<", "> <"); return verseRebuild; } diff --git a/src/BibleTaggingUtil/Properties/Settings.Designer.cs b/src/BibleTaggingUtil/Properties/Settings.Designer.cs index 6559c6d..a4579d9 100644 --- a/src/BibleTaggingUtil/Properties/Settings.Designer.cs +++ b/src/BibleTaggingUtil/Properties/Settings.Designer.cs @@ -12,7 +12,7 @@ namespace BibleTaggingUtil.Properties { [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.4.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.8.0.0")] internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); @@ -142,5 +142,17 @@ public int PeriodicSaveTime { this["PeriodicSaveTime"] = value; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("False")] + public bool Osis { + get { + return ((bool)(this["Osis"])); + } + set { + this["Osis"] = value; + } + } } } diff --git a/src/BibleTaggingUtil/Properties/Settings.settings b/src/BibleTaggingUtil/Properties/Settings.settings index 252aa18..95e5a75 100644 --- a/src/BibleTaggingUtil/Properties/Settings.settings +++ b/src/BibleTaggingUtil/Properties/Settings.settings @@ -32,5 +32,8 @@ 0 + + False + \ No newline at end of file diff --git a/src/BibleTaggingUtil/Verse.cs b/src/BibleTaggingUtil/Verse.cs index cea5b34..ccd1bb0 100644 --- a/src/BibleTaggingUtil/Verse.cs +++ b/src/BibleTaggingUtil/Verse.cs @@ -14,7 +14,7 @@ public class Verse { private Dictionary verse = new Dictionary(); - public Verse() { } + public Verse() { Dirty = false; } /// /// Creates a deep copy of itself @@ -152,6 +152,7 @@ public void split(int index) verse = temp; } + public bool Dirty { get; set; } } @@ -166,11 +167,14 @@ public VerseEx(Verse verse, int col, int row) SavedVerse = verse; Colum = col; Row = row; + Dirty = false; } public Verse SavedVerse { get; private set; } public int Colum { get; private set; } public int Row { get; private set; } + public bool Dirty { get; set; } + } } From 6b9a0ebdde7fbbc8d2048caa049deae4312ec953 Mon Sep 17 00:00:00 2001 From: sabdelmalik Date: Wed, 10 Jan 2024 14:44:18 -0500 Subject: [PATCH 3/5] Tidy up for OSIS XML Support --- src/BibleTaggingUtil/AboutForm.cs | 4 +- src/BibleTaggingUtil/BibleTaggingForm.cs | 76 +++++- .../BibleVersions/BibleVersion.cs | 4 +- .../BibleVersions/ReferenceVersionKJV.cs | 4 +- .../BibleVersions/ReferenceVersionTAGNT.cs | 4 +- .../BibleVersions/ReferenceVersionTOTHT.cs | 16 +- .../BibleVersions/TargetOsisVersion.cs | 134 ++++++++- .../BibleVersions/TargetVersion.cs | 4 +- src/BibleTaggingUtil/Editor/EditorPanel.cs | 24 +- .../Editor/HebrewGreekGrid.cs | 8 +- src/BibleTaggingUtil/Editor/KJVGridView.cs | 4 +- src/BibleTaggingUtil/Editor/TOHTHGridView.cs | 8 +- src/BibleTaggingUtil/Editor/TargetGridView.cs | 7 +- src/BibleTaggingUtil/OSIS_Generator.cs | 24 +- src/BibleTaggingUtil/OsisXml/OsisTag.cs | 257 ++++++++++-------- src/BibleTaggingUtil/OsisXml/OsisVerse.cs | 97 ++++--- .../USFM2OSIS _BookData.cs | 2 +- .../{OSIS => USFM2OSIS}/USFM2OSIS _Convert.cs | 2 +- .../{OSIS => USFM2OSIS}/USFM2OSIS _Sort.cs | 2 +- .../{OSIS => USFM2OSIS}/USFM2OSIS _Util.cs | 2 +- .../{OSIS => USFM2OSIS}/USFM2OSIS.cs | 11 +- src/BibleTaggingUtil/VerseSelectionPanel.cs | 8 +- 22 files changed, 480 insertions(+), 222 deletions(-) rename src/BibleTaggingUtil/{OSIS => USFM2OSIS}/USFM2OSIS _BookData.cs (99%) rename src/BibleTaggingUtil/{OSIS => USFM2OSIS}/USFM2OSIS _Convert.cs (99%) rename src/BibleTaggingUtil/{OSIS => USFM2OSIS}/USFM2OSIS _Sort.cs (98%) rename src/BibleTaggingUtil/{OSIS => USFM2OSIS}/USFM2OSIS _Util.cs (94%) rename src/BibleTaggingUtil/{OSIS => USFM2OSIS}/USFM2OSIS.cs (95%) diff --git a/src/BibleTaggingUtil/AboutForm.cs b/src/BibleTaggingUtil/AboutForm.cs index 24b7f5e..87dd055 100644 --- a/src/BibleTaggingUtil/AboutForm.cs +++ b/src/BibleTaggingUtil/AboutForm.cs @@ -74,7 +74,9 @@ private void OpenBrowser(string link) } catch (Exception ex) { - Console.WriteLine(ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } } diff --git a/src/BibleTaggingUtil/BibleTaggingForm.cs b/src/BibleTaggingUtil/BibleTaggingForm.cs index 1d2eaff..f260347 100644 --- a/src/BibleTaggingUtil/BibleTaggingForm.cs +++ b/src/BibleTaggingUtil/BibleTaggingForm.cs @@ -13,7 +13,7 @@ using BibleTaggingUtil.Editor; using SM.Bible.Formats.USFM; -using SM.Bible.Formats.OSIS; +using SM.Bible.Formats.USFM2OSIS; using System.Reflection; using BibleTaggingUtil.BibleVersions; @@ -225,7 +225,9 @@ private void BibleTaggingForm_Closing(object sender, System.ComponentModel.Cance } catch(Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } } @@ -274,6 +276,7 @@ private void LoadBibles() editorPanel.TargetBibleName(Path.GetFileName(biblesFolder)); target.BibleName = Path.GetFileName(biblesFolder); + osisTarget.BibleName = Path.GetFileName(biblesFolder); referenceKJV.BibleName = "KJV"; referenceTAGNT.BibleName = "TAGNT"; referenceTOTHT.BibleName = "TOTHT"; @@ -325,7 +328,9 @@ private void LoadBibles() } catch(Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } WaitCursorControl(true); @@ -333,8 +338,17 @@ private void LoadBibles() string[] files = Directory.GetFiles(taggedFolder); if (files.Length > 0) { - if(Properties.Settings.Default.Osis) + if (Properties.Settings.Default.Osis) + { + generateOSISToolStripMenuItem.Visible = false; + usfmToolStripMenuItem.Visible = false; + oSISToolStripMenuItem.Visible = true; + generateSWORDFilesToolStripMenuItem.Visible = false; + saveHebrewToolStripMenuItem.Visible = false; + saveKJVPlainToolStripMenuItem.Visible = false; + osisTarget.LoadBibleFile(files[0], true, false); + } else target.LoadBibleFile(files[0], true, false); VerseSelectionPanel.SetBookCount(target.BookCount); @@ -796,7 +810,9 @@ private void saveKJVPlainToolStripMenuItem_Click(object sender, EventArgs e) } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } } } @@ -804,7 +820,9 @@ private void saveKJVPlainToolStripMenuItem_Click(object sender, EventArgs e) } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } } @@ -894,7 +912,9 @@ private string[] GetHebrewLine(Verse verseWords) } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } return lines; } @@ -950,7 +970,9 @@ private void saveHebrewToolStripMenuItem_Click(object sender, EventArgs e) } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } } } @@ -960,7 +982,9 @@ private void saveHebrewToolStripMenuItem_Click(object sender, EventArgs e) } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } } @@ -988,7 +1012,9 @@ public void FindVerse (string tag) } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } if ((string.IsNullOrEmpty(tag) || tag.ToLower() == "")) @@ -1009,7 +1035,9 @@ public void FindVerse (string tag) } catch(Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } } @@ -1050,13 +1078,17 @@ public void FindRepetitive() } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } } } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } } @@ -1091,6 +1123,16 @@ private void generateOSISToolStripMenuItem_Click(object sender, EventArgs e) private void generateSWORDFilesOsisToolStripMenuItem_Click(object sender, EventArgs e) { target.SaveUpdates(); + + if (Properties.Settings.Default.Osis) + { + // copy last changes to the output xml + string taggedFolder = Path.Combine(Properties.Settings.Default.BiblesFolder, "tagged"); + string LastChanged = Directory.GetFiles(taggedFolder)[0]; + string outputXml = Path.Combine(Properties.Settings.Default.BiblesFolder, config.OSIS[OsisConstants.output_file]); + System.IO.File.Copy(LastChanged, outputXml, true); + } + new Thread( () => { @@ -1197,7 +1239,9 @@ private void RunOsis2mod(string sourceFileName, string targetFolderName) } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } @@ -1263,7 +1307,9 @@ private void RunOsis2mod(string sourceFileName, string targetFolderName) } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name + ": Executing osis2mod", ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } finally { diff --git a/src/BibleTaggingUtil/BibleVersions/BibleVersion.cs b/src/BibleTaggingUtil/BibleVersions/BibleVersion.cs index 3698472..3888886 100644 --- a/src/BibleTaggingUtil/BibleVersions/BibleVersion.cs +++ b/src/BibleTaggingUtil/BibleVersions/BibleVersion.cs @@ -147,7 +147,9 @@ public string this[string ubsName] } catch(Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } return bookName; diff --git a/src/BibleTaggingUtil/BibleVersions/ReferenceVersionKJV.cs b/src/BibleTaggingUtil/BibleVersions/ReferenceVersionKJV.cs index dcd4a83..8cbb895 100644 --- a/src/BibleTaggingUtil/BibleVersions/ReferenceVersionKJV.cs +++ b/src/BibleTaggingUtil/BibleVersions/ReferenceVersionKJV.cs @@ -34,7 +34,9 @@ public void Load() } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } } diff --git a/src/BibleTaggingUtil/BibleVersions/ReferenceVersionTAGNT.cs b/src/BibleTaggingUtil/BibleVersions/ReferenceVersionTAGNT.cs index 0259aea..e04714f 100644 --- a/src/BibleTaggingUtil/BibleVersions/ReferenceVersionTAGNT.cs +++ b/src/BibleTaggingUtil/BibleVersions/ReferenceVersionTAGNT.cs @@ -194,7 +194,9 @@ override protected void ParseLine(string line) } catch(Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } } } diff --git a/src/BibleTaggingUtil/BibleVersions/ReferenceVersionTOTHT.cs b/src/BibleTaggingUtil/BibleVersions/ReferenceVersionTOTHT.cs index 539f6d5..b556f11 100644 --- a/src/BibleTaggingUtil/BibleVersions/ReferenceVersionTOTHT.cs +++ b/src/BibleTaggingUtil/BibleVersions/ReferenceVersionTOTHT.cs @@ -76,7 +76,9 @@ private void SaveUpdates() } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } @@ -263,7 +265,9 @@ override protected void ParseLine(string wordLine) } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } } @@ -381,7 +385,9 @@ protected void ParseLine2(string wordLine) } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } } @@ -509,7 +515,9 @@ protected void ParseLine3(string wordLine) } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } } diff --git a/src/BibleTaggingUtil/BibleVersions/TargetOsisVersion.cs b/src/BibleTaggingUtil/BibleVersions/TargetOsisVersion.cs index 52f6160..2667c27 100644 --- a/src/BibleTaggingUtil/BibleVersions/TargetOsisVersion.cs +++ b/src/BibleTaggingUtil/BibleVersions/TargetOsisVersion.cs @@ -1,10 +1,12 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Text.RegularExpressions; +using System.Threading; using System.Threading.Tasks; using System.Xml; @@ -36,6 +38,7 @@ public class TargetOsisVersion : BibleVersion /// The offsets speeds up the regex search for verses /// private Dictionary bookOffsets = new Dictionary(); + private Dictionary> bookTemp = new Dictionary>(); /// /// Key: Verse reference in the format Gen 1:1 @@ -52,7 +55,20 @@ protected override bool LoadBibleFileInternal(string textFilePath, bool more) { try { - Load(textFilePath); + //ThreadPool.SetMinThreads(8, 8); + + var stopwatch = new Stopwatch(); + stopwatch.Start(); + LoadT(textFilePath); + stopwatch.Stop(); + long elapsed_time = stopwatch.ElapsedMilliseconds; + + foreach (OsisVerse osisVerse in osisBible.Values) + { + Verse verseWords = osisVerse.GetVerseWords(); + bible.Add(osisVerse.VerseRefX, verseWords); + } + bookNamesList = bookOffsets.Keys.ToList(); @@ -75,7 +91,9 @@ protected override bool LoadBibleFileInternal(string textFilePath, bool more) } catch (Exception ex) { - + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } } return result; @@ -87,7 +105,11 @@ protected override bool LoadBibleFileInternal(string textFilePath, bool more) private void Load(string fileName) { // Read the complete OSIS XML file - osisDoc = File.ReadAllText(fileName); + //osisDoc = File.ReadAllText(fileName); + using(StreamReader sr = new StreamReader(fileName)) + { + osisDoc = sr.ReadToEnd(); + } // Build the book's offsets map BuildBookOffsets(); @@ -107,16 +129,98 @@ private void Load(string fileName) if (osisVerse != null) { osisBible.Add(osisVerse.VerseRefX, osisVerse); - if (osisVerse.VerseRef == "Gen.9.26") + //Verse verseWords = osisVerse.GetVerseWords(); + //bible.Add(osisVerse.VerseRefX, verseWords); + } + } + } + } + } + + private void LoadT(string fileName) + { + try + { + // Read the complete OSIS XML file + //osisDoc = File.ReadAllText(fileName); + using (StreamReader sr = new StreamReader(fileName)) + { + osisDoc = sr.ReadToEnd(); + } + + // Build the book's offsets map + BuildBookOffsets(); + + bookCount = 0; + container.UpdateProgress("Loading " + bibleName, (100 * bookCount) / 66); + // Create an OSIS Bible Map + foreach (string book in bookOffsets.Keys) + { + //new Thread(() => { LoadBook(book); }).Start(); + ThreadPool.QueueUserWorkItem(LoadBook, book); + } + while (bookCount < bookOffsets.Keys.Count) ; + + foreach(string book in bookTemp.Keys) + { + osisBible = osisBible.Concat(bookTemp[book]).ToDictionary(kvp => kvp.Key, kvp => kvp.Value); + } + } + catch(Exception ex) + { + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); + } + } + + object lockingObject = new object(); + int bookCount = 0; + private void BookLoaded(string book, Dictionary localBible) + { + lock (lockingObject) + { + bookCount++; + container.UpdateProgress("Loading " + bibleName, (100 * bookCount) / 66); + bookTemp[book] = localBible; + } + } + private void LoadBook(Object threadContext) //string book) + { + string book = (string)threadContext; + + Dictionary localBible = new Dictionary(); + try + { + Regex regex = new Regex( + string.Format(@"", book)); + + MatchCollection VerseMatches = regex.Matches(osisDoc, bookOffsets[book]); + if (VerseMatches.Count > 0) + { + foreach (Match VerseMatch in VerseMatches) + { + OsisVerse? osisVerse = GetVersesTags(book, VerseMatch); + if (osisVerse != null) + { + //lock (this) { - int x = 0; + localBible.Add(osisVerse.VerseRefX, osisVerse); } - Verse verseWords = osisVerse.GetVerseWords(); - bible.Add(osisVerse.VerseRefX, verseWords); + // Verse verseWords = osisVerse.GetVerseWords(); + // bible.Add(osisVerse.VerseRefX, verseWords); } } } } + catch (Exception ex) + { + int x = 0; + } + finally + { + BookLoaded(book, localBible); + } } private OsisVerse? GetVersesTags(string book, Match VerseMatch) @@ -153,8 +257,9 @@ private void Load(string fileName) } catch (Exception ex) { - //Trace(string.Format("{0},Error,{1},{2}", sID, ++errorCounter,ex.Message), Color.Red); - //result = string.Format("{0},Error,{1},{2}", sID, ++errorCounter, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } return result; @@ -213,8 +318,6 @@ internal OsisVerse GetVerse(string verseRef) } return verseXML; - //osisDoc = osisDoc.Remove(startIndex, osisVerse.Length).Insert(startIndex, osisVerse); - } private void ParseVerse(string verse) @@ -275,9 +378,11 @@ private List GetStrongs(string value) internal void Save(string fileName) { + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; try { - Tracing.TraceEntry(MethodBase.GetCurrentMethod().Name); + Tracing.TraceEntry(name); lock (this) { if (!container.EditorPanel.TargetDirty) @@ -330,7 +435,7 @@ internal void Save(string fileName) } string baseName = Path.GetFileNameWithoutExtension(container.Config.TaggedBible); - string updatesFileName = string.Format("{0:s}_{1:s}.txt", baseName, DateTime.Now.ToString("yyyy_MM_dd_HH_mm")); + string updatesFileName = string.Format("{0:s}_{1:s}.xml", baseName, DateTime.Now.ToString("yyyy_MM_dd_HH_mm")); File.WriteAllText(Path.Combine(taggedFolder, updatesFileName), osisDoc); } @@ -338,7 +443,7 @@ internal void Save(string fileName) } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + Tracing.TraceException(name, ex.Message); } container.WaitCursorControl(false); } @@ -358,6 +463,7 @@ private void BuildBookOffsets() int offset = match.Index; string book = match.Groups[1].Value; bookOffsets[book] = offset; + bookTemp[book] = null; } break; } diff --git a/src/BibleTaggingUtil/BibleVersions/TargetVersion.cs b/src/BibleTaggingUtil/BibleVersions/TargetVersion.cs index 84fea9a..e96253c 100644 --- a/src/BibleTaggingUtil/BibleVersions/TargetVersion.cs +++ b/src/BibleTaggingUtil/BibleVersions/TargetVersion.cs @@ -82,7 +82,9 @@ public void SaveUpdates() } catch(Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } container.WaitCursorControl(false); diff --git a/src/BibleTaggingUtil/Editor/EditorPanel.cs b/src/BibleTaggingUtil/Editor/EditorPanel.cs index 98f704e..f1f0a32 100644 --- a/src/BibleTaggingUtil/Editor/EditorPanel.cs +++ b/src/BibleTaggingUtil/Editor/EditorPanel.cs @@ -170,8 +170,16 @@ public void SaveCurrentVerse() dgvTarget.SaveVerse(tbCurrentReference.Text); } } + + private bool firstEvent = true; private void Verse_VerseChanged(object sender, VerseChangedEventArgs e) { + if (firstEvent) + { + firstEvent = false; + picEnableEdit.Hide(); + + } osis = Properties.Settings.Default.Osis; strongsPrefix = e.StrongsPrefix; @@ -198,7 +206,9 @@ private void Verse_VerseChanged(object sender, VerseChangedEventArgs e) } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name + "-KJV", ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } Verse verseWords = null; @@ -245,7 +255,9 @@ private void Verse_VerseChanged(object sender, VerseChangedEventArgs e) } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name + "-TOTHT", ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } try @@ -308,7 +320,9 @@ private void Verse_VerseChanged(object sender, VerseChangedEventArgs e) } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name + "-Target", ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } } @@ -555,7 +569,9 @@ private void SelectReferenceTags(string tag, bool firstHalf) } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } } } diff --git a/src/BibleTaggingUtil/Editor/HebrewGreekGrid.cs b/src/BibleTaggingUtil/Editor/HebrewGreekGrid.cs index 62cd474..dd068e4 100644 --- a/src/BibleTaggingUtil/Editor/HebrewGreekGrid.cs +++ b/src/BibleTaggingUtil/Editor/HebrewGreekGrid.cs @@ -170,7 +170,9 @@ private void UpdateOT(Verse verseWords) } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } this.ClearSelection(); @@ -219,7 +221,9 @@ private void UpdateNT(Verse verseWords) } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } } diff --git a/src/BibleTaggingUtil/Editor/KJVGridView.cs b/src/BibleTaggingUtil/Editor/KJVGridView.cs index 91d5efe..a9c2152 100644 --- a/src/BibleTaggingUtil/Editor/KJVGridView.cs +++ b/src/BibleTaggingUtil/Editor/KJVGridView.cs @@ -91,7 +91,9 @@ public void Update(Verse verse) } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } } diff --git a/src/BibleTaggingUtil/Editor/TOHTHGridView.cs b/src/BibleTaggingUtil/Editor/TOHTHGridView.cs index 8873980..6207e16 100644 --- a/src/BibleTaggingUtil/Editor/TOHTHGridView.cs +++ b/src/BibleTaggingUtil/Editor/TOHTHGridView.cs @@ -171,7 +171,9 @@ private void UpdateOT(Verse verseWords) } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } this.ClearSelection(); @@ -224,7 +226,9 @@ private void UpdateNT(Verse verseWords) } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } } } diff --git a/src/BibleTaggingUtil/Editor/TargetGridView.cs b/src/BibleTaggingUtil/Editor/TargetGridView.cs index 2b746f8..ff8dd9d 100644 --- a/src/BibleTaggingUtil/Editor/TargetGridView.cs +++ b/src/BibleTaggingUtil/Editor/TargetGridView.cs @@ -120,6 +120,9 @@ private void ContextMenuStrip_Opening(object sender, System.ComponentModel.Cance if (this.SelectedCells.Count == 0) return; + if (Properties.Settings.Default.Osis) + return; + if (this.SelectedCells.Count > 1) { bool sameRow = true; @@ -464,7 +467,9 @@ public void Update(Verse verse) } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } } diff --git a/src/BibleTaggingUtil/OSIS_Generator.cs b/src/BibleTaggingUtil/OSIS_Generator.cs index 32e492c..638a6c6 100644 --- a/src/BibleTaggingUtil/OSIS_Generator.cs +++ b/src/BibleTaggingUtil/OSIS_Generator.cs @@ -164,7 +164,9 @@ public void Generate(TargetVersion targetVersion) } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); throw; } } @@ -372,7 +374,9 @@ private void WriteBibleBooks(StreamReader sr, StreamWriter sw, string strongPref } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); throw; } @@ -419,7 +423,9 @@ private string GetTaggedVerse(string verseText, string strongPrefix) } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } @@ -477,7 +483,9 @@ private string GetTaggedVerse(string verseText, string strongPrefix) } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); throw; } } @@ -505,7 +513,9 @@ private void WriteOsisTextStartTag(StreamWriter sw) } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); throw; } } @@ -577,7 +587,9 @@ private void WriteHeader(StreamWriter sw) } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); throw; } } diff --git a/src/BibleTaggingUtil/OsisXml/OsisTag.cs b/src/BibleTaggingUtil/OsisXml/OsisTag.cs index 708671b..6138db7 100644 --- a/src/BibleTaggingUtil/OsisXml/OsisTag.cs +++ b/src/BibleTaggingUtil/OsisXml/OsisTag.cs @@ -115,132 +115,151 @@ private List GetStrongs(string value) public List GetVerseWords() { List verseWords = new List(); - switch (tagType) + try { - case VerseTagType.text: - case VerseTagType.w: - verseWords.Add(verseWord); - break; - default: - { - if (osisTags.Count > 0) + switch (tagType) + { + case VerseTagType.text: + case VerseTagType.w: + verseWords.Add(verseWord); + break; + default: { - foreach (OsisTag tag in osisTags) + if (osisTags.Count > 0) { - List < VerseWord > verseWords1 = tag.GetVerseWords(); - if(verseWords1.Count > 0) - verseWords.AddRange(verseWords1); + foreach (OsisTag tag in osisTags) + { + List verseWords1 = tag.GetVerseWords(); + if (verseWords1.Count > 0) + verseWords.AddRange(verseWords1); + } } } - } - break; + break; + } + } + catch (Exception ex) + { + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } + return verseWords; } public override string ToString() { string result = string.Empty; - switch (tagType) + try { - case VerseTagType.text: + switch (tagType) + { + case VerseTagType.text: - if (verseWord != null) - { - if (verseWord.Strong.Length > 1 || - (verseWord.Strong.Length == 1 && !string.IsNullOrEmpty(verseWord.Strong[0]))) + if (verseWord != null) { - string lemmaValue = string.Empty; - foreach (string s in verseWord.Strong) + if (verseWord.Strong.Length > 1 || + (verseWord.Strong.Length == 1 && !string.IsNullOrEmpty(verseWord.Strong[0]))) { - lemmaValue += string.Format("strong:{0}{1} ", verseWord.Testament == BibleTestament.OT ? "H" : "G", s); + string lemmaValue = string.Empty; + foreach (string s in verseWord.Strong) + { + lemmaValue += string.Format("strong:{0}{1} ", verseWord.Testament == BibleTestament.OT ? "H" : "G", s); + } + lemmaValue = lemmaValue.Trim(); + result = string.Format("{1}", lemmaValue, verseWord.Word); } - lemmaValue = lemmaValue.Trim(); - result = string.Format("{1}", lemmaValue, verseWord.Word); + else + result = verseWord.Word; } - else - result = verseWord.Word; - } - break; - case VerseTagType.w: - if (verseWord.Strong.Length == 0 || - (verseWord.Strong.Length == 1 && string.IsNullOrEmpty(verseWord.Strong[0]))) - { - result = verseWord.Word; - } - else - { - string lemmaValue = string.Empty; - if (verseWord != null) + break; + case VerseTagType.w: + if (verseWord.Strong.Length == 0 || + (verseWord.Strong.Length == 1 && string.IsNullOrEmpty(verseWord.Strong[0]))) { - foreach (string s in verseWord.Strong) - { - lemmaValue += string.Format("strong:{0}{1} ", verseWord.Testament == BibleTestament.OT ? "H" : "G", s); - } - lemmaValue = lemmaValue.Trim(); - XmlNode tagXml1 = tagXml.Clone(); - if (tagXml1 != null) + result = verseWord.Word; + } + else + { + string lemmaValue = string.Empty; + if (verseWord != null) { - ((XmlElement)tagXml1).SetAttribute("lemma", lemmaValue); // Set to new value. - result = tagXml1.OuterXml; + foreach (string s in verseWord.Strong) + { + lemmaValue += string.Format("strong:{0}{1} ", verseWord.Testament == BibleTestament.OT ? "H" : "G", s); + } + lemmaValue = lemmaValue.Trim(); + XmlNode tagXml1 = tagXml.Clone(); + if (tagXml1 != null) + { + ((XmlElement)tagXml1).SetAttribute("lemma", lemmaValue); // Set to new value. + result = tagXml1.OuterXml; + } } } - } - break; - case VerseTagType.note: - case VerseTagType.milestone: - case VerseTagType.b: - result = tagXml.OuterXml; - break; + break; + case VerseTagType.note: + case VerseTagType.milestone: + case VerseTagType.b: + result = tagXml.OuterXml; + break; - case VerseTagType.UNKNOWN: - string temp = tagXml.OuterXml; - string marker = tagXml.InnerXml; - int idx = temp.IndexOf(marker); - switch (marker) - { - case "$start$": - result = temp.Substring(idx + marker.Length); - break; - case "$end$": - result = temp.Substring(0, idx); - break; - default: - result = tagXml.OuterXml; - break; - } - break; + case VerseTagType.UNKNOWN: + string temp = tagXml.OuterXml; + string marker = tagXml.InnerXml; + int idx = temp.IndexOf(marker); + switch (marker) + { + case "$start$": + result = temp.Substring(idx + marker.Length); + break; + case "$end$": + result = temp.Substring(0, idx); + break; + default: + result = tagXml.OuterXml; + break; + } + break; - default: - { - string result1 = string.Empty; - if (osisTags.Count > 0) + default: { - foreach (OsisTag tag in osisTags) + string result1 = string.Empty; + if (osisTags.Count > 0) { - result1 += tag.ToString(); - } - XmlNode tagXmlTemp = tagXml.Clone(); - tagXmlTemp.InnerXml = result1; - result = tagXmlTemp.OuterXml; - if (tagType == VerseTagType.l) - { - int i = result.IndexOf("$end$"); - if (i > 0) - result = result.Remove(i, "$end$".Length); - i = result.IndexOf("$start$"); - if (i > 0) + foreach (OsisTag tag in osisTags) { - int x = result.LastIndexOf(" 0) + result = result.Remove(i, "$end$".Length); + i = result.IndexOf("$start$"); + if (i > 0) + { + int x = result.LastIndexOf(" 1 || - (w.Strong.Length == 1 && !string.IsNullOrEmpty(w.Strong[0]))) - tagType = VerseTagType.w; + VerseWord w; + if (verseWord != null) w = verseWord; + else + { + w = osisTags[verseWord.OsisTagIndex].verseWord; + } + w.Strong = verseWord.Strong; + if (tagType == VerseTagType.w) + { + if (w.Strong.Length == 0 || + (w.Strong.Length == 1 && string.IsNullOrEmpty(w.Strong[0]))) + tagType = VerseTagType.text; + } + else if (tagType == VerseTagType.text) + { + if (w.Strong.Length > 1 || + (w.Strong.Length == 1 && !string.IsNullOrEmpty(w.Strong[0]))) + tagType = VerseTagType.w; + } } } + catch (Exception ex) + { + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); + } } public XmlNode TagNode diff --git a/src/BibleTaggingUtil/OsisXml/OsisVerse.cs b/src/BibleTaggingUtil/OsisXml/OsisVerse.cs index 159d663..e13ea8f 100644 --- a/src/BibleTaggingUtil/OsisXml/OsisVerse.cs +++ b/src/BibleTaggingUtil/OsisXml/OsisVerse.cs @@ -56,9 +56,11 @@ private void BuildOsisTags() { document.LoadXml(header + VerseXml + trailer); } - catch (Exception e) + catch (Exception ex) { - int x = 0; + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } @@ -85,54 +87,63 @@ private string TreatOddTags(string VerseXml, string tag) { string s = string.Format("<{0}>", tag); string e = string.Format("", tag); - string startAdded = string.Format("{0}$start${1}", s,e); + string startAdded = string.Format("{0}$start${1}", s, e); string endAdded = string.Empty; int startIndex = 0; - while (true) + try { - Regex regex = new Regex(@"(<" + tag + @"[^>]*>)"); - if (tag == "div" || tag == "l") - regex = new Regex(string.Format(@"(<{0}\s[^>]+>)", tag)); - - int startP = -1; - Match match = regex.Match(VerseXml,startIndex); - if (match.Success) - { - startP = match.Index; - s = match.Groups[1].Value; - } - else - startP = VerseXml.IndexOf(s, startIndex); - - endAdded = string.Format("{0}$end${1}", s, e); - - int endP = VerseXml.IndexOf(e, startIndex); - if (startP < 0 && endP < 0) break; - if (startP < 0 && endP >= 0) + while (true) { - VerseXml = VerseXml.Remove(endP, e.Length).Insert(endP, startAdded); //Replace(e, startAdded); - startIndex = endP + startAdded.Length; - continue; - } - else if (startP >= 0 && endP < 0) - { - VerseXml = VerseXml.Remove(startP, s.Length).Insert(startP, endAdded); //Replace(s, endAdded); - startIndex = startP + endAdded.Length; - continue; - } - else if(startP < endP) - { - startIndex = endP + e.Length; - continue; - } - else if (startP > endP) - { - VerseXml = VerseXml.Remove(endP, e.Length).Insert(endP, startAdded); //Replace(e, startAdded); - startIndex = endP + startAdded.Length; - continue; + Regex regex = new Regex(@"(<" + tag + @"[^>]*>)"); + if (tag == "div" || tag == "l") + regex = new Regex(string.Format(@"(<{0}\s[^>]+>)", tag)); + + int startP = -1; + Match match = regex.Match(VerseXml, startIndex); + if (match.Success) + { + startP = match.Index; + s = match.Groups[1].Value; + } + else + startP = VerseXml.IndexOf(s, startIndex); + + endAdded = string.Format("{0}$end${1}", s, e); + + int endP = VerseXml.IndexOf(e, startIndex); + if (startP < 0 && endP < 0) break; + if (startP < 0 && endP >= 0) + { + VerseXml = VerseXml.Remove(endP, e.Length).Insert(endP, startAdded); //Replace(e, startAdded); + startIndex = endP + startAdded.Length; + continue; + } + else if (startP >= 0 && endP < 0) + { + VerseXml = VerseXml.Remove(startP, s.Length).Insert(startP, endAdded); //Replace(s, endAdded); + startIndex = startP + endAdded.Length; + continue; + } + else if (startP < endP) + { + startIndex = endP + e.Length; + continue; + } + else if (startP > endP) + { + VerseXml = VerseXml.Remove(endP, e.Length).Insert(endP, startAdded); //Replace(e, startAdded); + startIndex = endP + startAdded.Length; + continue; + } } } + catch (Exception ex) + { + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); + } return VerseXml; } diff --git a/src/BibleTaggingUtil/OSIS/USFM2OSIS _BookData.cs b/src/BibleTaggingUtil/USFM2OSIS/USFM2OSIS _BookData.cs similarity index 99% rename from src/BibleTaggingUtil/OSIS/USFM2OSIS _BookData.cs rename to src/BibleTaggingUtil/USFM2OSIS/USFM2OSIS _BookData.cs index 1a64de7..357e629 100644 --- a/src/BibleTaggingUtil/OSIS/USFM2OSIS _BookData.cs +++ b/src/BibleTaggingUtil/USFM2OSIS/USFM2OSIS _BookData.cs @@ -5,7 +5,7 @@ using System.Text; using System.Threading.Tasks; -namespace SM.Bible.Formats.OSIS +namespace SM.Bible.Formats.USFM2OSIS { /// /// This code is based on the Python code by Chris Little https://github.com/chrislit/usfm2osis diff --git a/src/BibleTaggingUtil/OSIS/USFM2OSIS _Convert.cs b/src/BibleTaggingUtil/USFM2OSIS/USFM2OSIS _Convert.cs similarity index 99% rename from src/BibleTaggingUtil/OSIS/USFM2OSIS _Convert.cs rename to src/BibleTaggingUtil/USFM2OSIS/USFM2OSIS _Convert.cs index 66510e7..f6059b1 100644 --- a/src/BibleTaggingUtil/OSIS/USFM2OSIS _Convert.cs +++ b/src/BibleTaggingUtil/USFM2OSIS/USFM2OSIS _Convert.cs @@ -9,7 +9,7 @@ using System.Threading.Tasks; using static System.Net.Mime.MediaTypeNames; -namespace SM.Bible.Formats.OSIS +namespace SM.Bible.Formats.USFM2OSIS { /// /// This code is based on the Python code by Chris Little https://github.com/chrislit/usfm2osis diff --git a/src/BibleTaggingUtil/OSIS/USFM2OSIS _Sort.cs b/src/BibleTaggingUtil/USFM2OSIS/USFM2OSIS _Sort.cs similarity index 98% rename from src/BibleTaggingUtil/OSIS/USFM2OSIS _Sort.cs rename to src/BibleTaggingUtil/USFM2OSIS/USFM2OSIS _Sort.cs index 08658d6..a86aa63 100644 --- a/src/BibleTaggingUtil/OSIS/USFM2OSIS _Sort.cs +++ b/src/BibleTaggingUtil/USFM2OSIS/USFM2OSIS _Sort.cs @@ -4,7 +4,7 @@ using System.Text; using System.Threading.Tasks; -namespace SM.Bible.Formats.OSIS +namespace SM.Bible.Formats.USFM2OSIS { /// /// This code is based on the Python code by Chris Little https://github.com/chrislit/usfm2osis diff --git a/src/BibleTaggingUtil/OSIS/USFM2OSIS _Util.cs b/src/BibleTaggingUtil/USFM2OSIS/USFM2OSIS _Util.cs similarity index 94% rename from src/BibleTaggingUtil/OSIS/USFM2OSIS _Util.cs rename to src/BibleTaggingUtil/USFM2OSIS/USFM2OSIS _Util.cs index bad9fd6..d69e306 100644 --- a/src/BibleTaggingUtil/OSIS/USFM2OSIS _Util.cs +++ b/src/BibleTaggingUtil/USFM2OSIS/USFM2OSIS _Util.cs @@ -5,7 +5,7 @@ using System.Threading.Tasks; using static System.Net.Mime.MediaTypeNames; -namespace SM.Bible.Formats.OSIS +namespace SM.Bible.Formats.USFM2OSIS { /// /// This code is based on the Python code by Chris Little https://github.com/chrislit/usfm2osis diff --git a/src/BibleTaggingUtil/OSIS/USFM2OSIS.cs b/src/BibleTaggingUtil/USFM2OSIS/USFM2OSIS.cs similarity index 95% rename from src/BibleTaggingUtil/OSIS/USFM2OSIS.cs rename to src/BibleTaggingUtil/USFM2OSIS/USFM2OSIS.cs index 5267c6c..e021127 100644 --- a/src/BibleTaggingUtil/OSIS/USFM2OSIS.cs +++ b/src/BibleTaggingUtil/USFM2OSIS/USFM2OSIS.cs @@ -10,7 +10,7 @@ using System.Text.RegularExpressions; using System.Threading.Tasks; -namespace SM.Bible.Formats.OSIS +namespace SM.Bible.Formats.USFM2OSIS { /// /// This code is based on the Python code by Chris Little https://github.com/chrislit/usfm2osis @@ -146,7 +146,9 @@ private void read_identifiers_from_osis(string filename) } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } using (StreamReader sr = new StreamReader(filename, encoding)) @@ -177,10 +179,11 @@ private void read_identifiers_from_osis(string filename) } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } - using (StreamReader sr = new StreamReader(filename, encoding)) { osis = sr.ReadToEnd().Trim() + "\n"; diff --git a/src/BibleTaggingUtil/VerseSelectionPanel.cs b/src/BibleTaggingUtil/VerseSelectionPanel.cs index 1507073..3f4df5e 100644 --- a/src/BibleTaggingUtil/VerseSelectionPanel.cs +++ b/src/BibleTaggingUtil/VerseSelectionPanel.cs @@ -336,7 +336,9 @@ public string GetNextRef(string currentReference) } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } return result; @@ -404,7 +406,9 @@ private void SetSelectedIndex(int currentBook, int currentChapter, int currentVe } catch (Exception ex) { - Tracing.TraceException(MethodBase.GetCurrentMethod().Name, ex.Message); + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); } } From cb035921458f46e9348d552e3849738ae3a8e7df Mon Sep 17 00:00:00 2001 From: sabdelmalik Date: Fri, 12 Jan 2024 17:00:28 -0500 Subject: [PATCH 4/5] 5.1 Update Changed approach to handling osis xml Added more tracing Some exceptions now re-throw --- src/BibleTaggingUtil/BibleTaggingForm.cs | 5 + src/BibleTaggingUtil/BibleTaggingUtil.csproj | 2 +- .../BibleVersions/TargetOsisVersion.cs | 313 ++++++++++++++---- src/BibleTaggingUtil/Editor/EditorPanel.cs | 13 + src/BibleTaggingUtil/Editor/TargetGridView.cs | 62 ++-- src/BibleTaggingUtil/OsisXml/OsisTag.cs | 3 + src/BibleTaggingUtil/OsisXml/OsisUtils.cs | 16 +- src/BibleTaggingUtil/OsisXml/OsisVerse.cs | 34 +- src/BibleTaggingUtil/Verse.cs | 6 + 9 files changed, 357 insertions(+), 97 deletions(-) diff --git a/src/BibleTaggingUtil/BibleTaggingForm.cs b/src/BibleTaggingUtil/BibleTaggingForm.cs index f260347..1f728be 100644 --- a/src/BibleTaggingUtil/BibleTaggingForm.cs +++ b/src/BibleTaggingUtil/BibleTaggingForm.cs @@ -1350,5 +1350,10 @@ private void reloadTargetToolStripMenuItem_Click(object sender, EventArgs e) } } + + internal void EnableSaveButton(bool v) + { + editorPanel.EnableSaveButton(v); + } } } diff --git a/src/BibleTaggingUtil/BibleTaggingUtil.csproj b/src/BibleTaggingUtil/BibleTaggingUtil.csproj index 9433a6f..7e5de8d 100644 --- a/src/BibleTaggingUtil/BibleTaggingUtil.csproj +++ b/src/BibleTaggingUtil/BibleTaggingUtil.csproj @@ -8,7 +8,7 @@ Copyright © 2023 by Sami Abdel Malik https://github.com/sabdelmalik/BibleTagging git - 5.0.0.0 + 5.1.0.0 Bible Text Tagging with Strong's Numbers diff --git a/src/BibleTaggingUtil/BibleVersions/TargetOsisVersion.cs b/src/BibleTaggingUtil/BibleVersions/TargetOsisVersion.cs index 2667c27..165498f 100644 --- a/src/BibleTaggingUtil/BibleVersions/TargetOsisVersion.cs +++ b/src/BibleTaggingUtil/BibleVersions/TargetOsisVersion.cs @@ -1,8 +1,10 @@ using System; using System.Collections.Generic; +using System.Data; using System.Diagnostics; using System.IO; using System.Linq; +using System.Net.Http; using System.Reflection; using System.Text; using System.Text.RegularExpressions; @@ -30,14 +32,16 @@ public class TargetOsisVersion : BibleVersion /// /// The complete OSIS XML file is read into this string /// - string osisDoc = string.Empty; + private string osisDoc = string.Empty; + private Dictionary osisDocSegments = new Dictionary(); + /// /// Key: Bible book name /// value: Offset of the bookwithin the osisDoc string /// The offsets speeds up the regex search for verses /// - private Dictionary bookOffsets = new Dictionary(); +// private Dictionary bookOffsets = new Dictionary(); private Dictionary> bookTemp = new Dictionary>(); /// @@ -50,6 +54,10 @@ public TargetOsisVersion(BibleTaggingForm container) : base(container, 31104) { protected override bool LoadBibleFileInternal(string textFilePath, bool more) { + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceEntry(name, textFilePath, more); + bool result = false; if (File.Exists(textFilePath)) { @@ -70,7 +78,9 @@ protected override bool LoadBibleFileInternal(string textFilePath, bool more) } - bookNamesList = bookOffsets.Keys.ToList(); + //bookNamesList = bookOffsets.Keys.ToList(); + bookNamesList = new List(); + bookNamesList.AddRange(osisDocSegments.Keys.Skip(1)); if (bookNamesList.Count == 66 || bookNamesList.Count == 39) { @@ -91,9 +101,8 @@ protected override bool LoadBibleFileInternal(string textFilePath, bool more) } catch (Exception ex) { - var cm = System.Reflection.MethodBase.GetCurrentMethod(); - var name = cm.DeclaringType.FullName + "." + cm.Name; Tracing.TraceException(name, ex.Message); + throw; } } return result; @@ -112,15 +121,18 @@ private void Load(string fileName) } // Build the book's offsets map - BuildBookOffsets(); + // BuildBookOffsets(); + BuildOsisSegments(); // Create an OSIS Bible Map - foreach (string book in bookOffsets.Keys) + //foreach (string book in bookOffsets.Keys) + foreach (string book in osisDocSegments.Keys) { Regex regex = new Regex( string.Format(@"", book)); - MatchCollection VerseMatches = regex.Matches(osisDoc, bookOffsets[book]); +// MatchCollection VerseMatches = regex.Matches(osisDoc, bookOffsets[book]); + MatchCollection VerseMatches = regex.Matches(osisDocSegments[book]); if (VerseMatches.Count > 0) { foreach (Match VerseMatch in VerseMatches) @@ -149,19 +161,27 @@ private void LoadT(string fileName) } // Build the book's offsets map - BuildBookOffsets(); + //BuildBookOffsets(); + BuildOsisSegments(); bookCount = 0; container.UpdateProgress("Loading " + bibleName, (100 * bookCount) / 66); // Create an OSIS Bible Map - foreach (string book in bookOffsets.Keys) + //foreach (string book in bookOffsets.Keys) + //{ + // //new Thread(() => { LoadBook(book); }).Start(); + // ThreadPool.QueueUserWorkItem(LoadBook, book); + //} + //while (bookCount < bookOffsets.Keys.Count) ; + + foreach (string book in osisDocSegments.Keys.Skip(1)) { //new Thread(() => { LoadBook(book); }).Start(); - ThreadPool.QueueUserWorkItem(LoadBook, book); + ThreadPool.QueueUserWorkItem(LoadBookFromSegments, book); } - while (bookCount < bookOffsets.Keys.Count) ; + while (bookCount < osisDocSegments.Keys.Count-1) ; - foreach(string book in bookTemp.Keys) + foreach(string book in osisDocSegments.Keys.Skip(1)) { osisBible = osisBible.Concat(bookTemp[book]).ToDictionary(kvp => kvp.Key, kvp => kvp.Value); } @@ -171,6 +191,7 @@ private void LoadT(string fileName) var cm = System.Reflection.MethodBase.GetCurrentMethod(); var name = cm.DeclaringType.FullName + "." + cm.Name; Tracing.TraceException(name, ex.Message); + throw; } } @@ -185,7 +206,46 @@ private void BookLoaded(string book, Dictionary localBible) bookTemp[book] = localBible; } } - private void LoadBook(Object threadContext) //string book) + //private void LoadBook(Object threadContext) //string book) + //{ + // string book = (string)threadContext; + + // Dictionary localBible = new Dictionary(); + // try + // { + // Regex regex = new Regex( + // string.Format(@"", book)); + + // MatchCollection VerseMatches = regex.Matches(osisDoc, bookOffsets[book]); + // if (VerseMatches.Count > 0) + // { + // foreach (Match VerseMatch in VerseMatches) + // { + // OsisVerse? osisVerse = GetVersesTags(book, VerseMatch); + // if (osisVerse != null) + // { + // //lock (this) + // { + // localBible.Add(osisVerse.VerseRefX, osisVerse); + // } + // // Verse verseWords = osisVerse.GetVerseWords(); + // // bible.Add(osisVerse.VerseRefX, verseWords); + // } + // } + // } + // } + // catch (Exception ex) + // { + // var cm = System.Reflection.MethodBase.GetCurrentMethod(); + // var name = cm.DeclaringType.FullName + "." + cm.Name; + // Tracing.TraceException(name, ex.Message); + // } + // finally + // { + // BookLoaded(book, localBible); + // } + //} + private void LoadBookFromSegments(Object threadContext) //string book) { string book = (string)threadContext; @@ -195,7 +255,7 @@ private void LoadBook(Object threadContext) //string book) Regex regex = new Regex( string.Format(@"", book)); - MatchCollection VerseMatches = regex.Matches(osisDoc, bookOffsets[book]); + MatchCollection VerseMatches = regex.Matches(osisDocSegments[book]); if (VerseMatches.Count > 0) { foreach (Match VerseMatch in VerseMatches) @@ -215,7 +275,10 @@ private void LoadBook(Object threadContext) //string book) } catch (Exception ex) { - int x = 0; + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); + throw; } finally { @@ -242,7 +305,8 @@ private void LoadBook(Object threadContext) //string book) try { Regex regex = new Regex(string.Format(@"", sID)); - VerseMatch = regex.Match(osisDoc, startIndex); +// VerseMatch = regex.Match(osisDoc, startIndex); + VerseMatch = regex.Match(osisDocSegments[book]); if (VerseMatch.Success) { endIndex = VerseMatch.Index; @@ -251,7 +315,8 @@ private void LoadBook(Object threadContext) //string book) string verseXml = string.Empty; if (endIndex > startIndex) - verseXml = osisDoc.Substring(startIndex, endIndex - startIndex); + verseXml = osisDocSegments[book].Substring(startIndex, endIndex - startIndex); + // verseXml = osisDoc.Substring(startIndex, endIndex - startIndex); result = new OsisVerse(verseRef, startIndex, sID, verseXml, eID); } @@ -260,6 +325,7 @@ private void LoadBook(Object threadContext) //string book) var cm = System.Reflection.MethodBase.GetCurrentMethod(); var name = cm.DeclaringType.FullName + "." + cm.Name; Tracing.TraceException(name, ex.Message); + throw; } return result; @@ -273,14 +339,15 @@ internal OsisVerse GetVerse(string verseRef) verseRef = "Rev.19.14"; int bookStart = -1; + string book = string.Empty; Match m = Regex.Match(verseRef, @"([1-9A-Za-z]*)\.(\d{1,3})\.(\d{1,3})"); if (m != null) { - string book = m.Groups[1].Value; + book = m.Groups[1].Value; string chapter = m.Groups[2].Value; string verse = m.Groups[3].Value; - bookStart = bookOffsets[book]; + //bookStart = bookOffsets[book]; } @@ -293,7 +360,8 @@ internal OsisVerse GetVerse(string verseRef) string sID = string.Empty; string eID = string.Empty; - Match match = regex.Match(osisDoc, bookStart); + //Match match = regex.Match(osisDoc, bookStart); + Match match = regex.Match(osisDocSegments[book]); if (match.Success) { startIndex = match.Index + match.Groups[0].Length; @@ -303,7 +371,8 @@ internal OsisVerse GetVerse(string verseRef) string pattern2 = string.Format( @"", sID); regex = new Regex(pattern2); - match = regex.Match(osisDoc, startIndex); + //match = regex.Match(osisDoc, startIndex); + match = regex.Match(osisDocSegments[book]); if (match.Success) { endIndex = match.Index; @@ -313,7 +382,8 @@ internal OsisVerse GetVerse(string verseRef) string osisVerse = string.Empty; if (endIndex > startIndex) { - osisVerse = osisDoc.Substring(startIndex, endIndex - startIndex); + //osisVerse = osisDoc.Substring(startIndex, endIndex - startIndex); + osisVerse = osisDocSegments[book].Substring(startIndex, endIndex - startIndex); verseXML = new OsisVerse(verseRef, osisVerse.Length, sID, osisVerse, eID); } @@ -392,65 +462,163 @@ internal void Save(string fileName) container.EditorPanel.TargetDirty = false; container.EditorPanel.SaveCurrentVerse(); - if (osisDoc != null) + // 1.Update osisBible' dirty fkag from Bible + foreach (string verseRef in Bible.Keys) { - // 1.Update osisBible from Bible - foreach (string verseRef in Bible.Keys) + Verse v = Bible[verseRef]; + if (v.Dirty) { - Verse v = Bible[verseRef]; - if (v.Dirty) - { - //osisBible[verseRef].UpdateVerse(v); - osisBible[verseRef].Dirty = true; - } + //osisBible[verseRef].UpdateVerse(v); + osisBible[verseRef].Dirty = true; } + } - // 2. Update osisDoc from osisBible - foreach (OsisVerse osisVerse in osisBible.Values) + // 2. Update osisDoc from osisBible + foreach (OsisVerse osisVerse in osisBible.Values) + { + if (osisVerse.Dirty) { - if (osisVerse.Dirty) + osisDocSegments[osisVerse.Book] = osisDocSegments[osisVerse.Book] + .Remove(osisVerse.StartIndex, osisVerse.Length) + .Insert(osisVerse.StartIndex, osisVerse.VerseCompleteXml); + + int diff = osisVerse.VerseCompleteXml.Length - osisVerse.Length; + if (diff != 0) { - osisDoc = osisDoc.Remove(osisVerse.StartIndex, osisVerse.Length) - .Insert(osisVerse.StartIndex, osisVerse.VerseCompleteXml); + osisVerse.UpdateVerseXml(osisVerse.VerseCompleteXml); + UpdateIndices(osisVerse.Book); } } + } - // construce Updates fileName - string taggedFolder = Path.GetDirectoryName(container.Config.TaggedBible); - string oldTaggedFolder = Path.Combine(taggedFolder, "OldTagged"); - if (!Directory.Exists(oldTaggedFolder)) - Directory.CreateDirectory(oldTaggedFolder); - - // move existing tagged files to the old folder - String[] existingTagged = Directory.GetFiles(taggedFolder, "*.*"); - foreach (String existingTaggedItem in existingTagged) - { - string fName = Path.GetFileName(existingTaggedItem); - string src = Path.Combine(taggedFolder, fName); - string dst = Path.Combine(oldTaggedFolder, fName); - if (System.IO.File.Exists(dst)) - System.IO.File.Delete(src); - else - System.IO.File.Move(src, dst); - } + // rebuild Document + osisDoc = string.Empty; + foreach(string seg in osisDocSegments.Values) + { + osisDoc += seg; + } - string baseName = Path.GetFileNameWithoutExtension(container.Config.TaggedBible); - string updatesFileName = string.Format("{0:s}_{1:s}.xml", baseName, DateTime.Now.ToString("yyyy_MM_dd_HH_mm")); + // construce Updates fileName + string taggedFolder = Path.GetDirectoryName(container.Config.TaggedBible); + string oldTaggedFolder = Path.Combine(taggedFolder, "OldTagged"); + if (!Directory.Exists(oldTaggedFolder)) + Directory.CreateDirectory(oldTaggedFolder); - File.WriteAllText(Path.Combine(taggedFolder, updatesFileName), osisDoc); + // move existing tagged files to the old folder + String[] existingTagged = Directory.GetFiles(taggedFolder, "*.*"); + foreach (String existingTaggedItem in existingTagged) + { + string fName = Path.GetFileName(existingTaggedItem); + string src = Path.Combine(taggedFolder, fName); + string dst = Path.Combine(oldTaggedFolder, fName); + if (System.IO.File.Exists(dst)) + System.IO.File.Delete(src); + else + System.IO.File.Move(src, dst); } + + string baseName = Path.GetFileNameWithoutExtension(container.Config.TaggedBible); + string updatesFileName = string.Format("{0:s}_{1:s}.xml", baseName, DateTime.Now.ToString("yyyy_MM_dd_HH_mm")); + + File.WriteAllText(Path.Combine(taggedFolder, updatesFileName), osisDoc); } } catch (Exception ex) { Tracing.TraceException(name, ex.Message); + throw; } container.WaitCursorControl(false); } - private void BuildBookOffsets() + private void UpdateIndices(string book) { - bookOffsets.Clear(); + try + { + Regex regex = new Regex( + string.Format(@"", book)); + + MatchCollection VerseMatches = regex.Matches(osisDocSegments[book]); + if (VerseMatches.Count > 0) + { + foreach (Match VerseMatch in VerseMatches) + { + int startIndex = VerseMatch.Index + VerseMatch.Groups[0].Length; + string verseRefX = string.Format("{0} {1}:{2}", book, VerseMatch.Groups[2].Value, VerseMatch.Groups[3].Value); + + OsisVerse? osisVerse = osisBible[verseRefX]; + osisVerse.UpdateVerseStartIndex(startIndex); + } + } + } + catch (Exception ex) + { + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); + throw; + } + } + + //private void UpdateBook(Object threadContext) //string book) + //{ + // string book = (string)threadContext; + + // try + // { + // Regex regex = new Regex( + // string.Format(@"", book)); + + // MatchCollection VerseMatches = regex.Matches(osisDocSegments[book]); + // if (VerseMatches.Count > 0) + // { + // foreach (Match VerseMatch in VerseMatches) + // { + // int startIndex = VerseMatch.Index + VerseMatch.Groups[0].Length; + // string verseRefX = string.Format("{0} {1}:{2}", book, VerseMatch.Groups[2].Value, VerseMatch.Groups[3].Value); + + // OsisVerse? osisVerse = osisBible[verseRefX]; + // osisVerse.UpdateVerseStartIndex(startIndex); + // } + // } + // } + // catch (Exception ex) + // { + // var cm = System.Reflection.MethodBase.GetCurrentMethod(); + // var name = cm.DeclaringType.FullName + "." + cm.Name; + // Tracing.TraceException(name, ex.Message); + // } + // finally + // { + // BookUpdated(); + // } + //} + + + //private void BuildBookOffsets() + //{ + // bookOffsets.Clear(); + // while (true) + // { + // //
+ // Regex regex = new Regex(@""); + // MatchCollection matches = regex.Matches(osisDoc); + // if (matches.Count > 0) + // { + // foreach (Match match in matches) + // { + // int offset = match.Index; + // string book = match.Groups[1].Value; + // bookOffsets[book] = offset; + // bookTemp[book] = null; + // } + // break; + // } + // } + //} + private void BuildOsisSegments() + { + osisDocSegments.Clear(); while (true) { //
@@ -458,15 +626,34 @@ private void BuildBookOffsets() MatchCollection matches = regex.Matches(osisDoc); if (matches.Count > 0) { + string book = "header"; + int start = 0; + foreach (Match match in matches) { int offset = match.Index; - string book = match.Groups[1].Value; - bookOffsets[book] = offset; - bookTemp[book] = null; + try + { + osisDocSegments.Add(book, osisDoc.Substring(start, offset-start)); + } + catch (Exception ex) + { + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); + throw; + } + + start = offset; + book = match.Groups[1].Value; } + osisDocSegments.Add(book, osisDoc.Substring(start)); break; } + else + { + throw new Exception("Could not identify any book in the document"); + } } } diff --git a/src/BibleTaggingUtil/Editor/EditorPanel.cs b/src/BibleTaggingUtil/Editor/EditorPanel.cs index f1f0a32..53deb3f 100644 --- a/src/BibleTaggingUtil/Editor/EditorPanel.cs +++ b/src/BibleTaggingUtil/Editor/EditorPanel.cs @@ -799,5 +799,18 @@ private void picRefresh_Click(object sender, EventArgs e) dgvTarget.CurrentCell = dgvTarget[savedColumn, savedRow]; } + + internal void EnableSaveButton(bool v) + { + if (InvokeRequired) + { + Invoke(new Action(() => { EnableSaveButton(v); })); + } + else + { + picSave.Visible = v; + } + + } } } diff --git a/src/BibleTaggingUtil/Editor/TargetGridView.cs b/src/BibleTaggingUtil/Editor/TargetGridView.cs index ff8dd9d..48b87de 100644 --- a/src/BibleTaggingUtil/Editor/TargetGridView.cs +++ b/src/BibleTaggingUtil/Editor/TargetGridView.cs @@ -498,42 +498,56 @@ public void SaveVerse(Verse verse) /// public void SaveVerse(string reference) { + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + bool osis = Properties.Settings.Default.Osis; Verse verse = new Verse(); - for (int i = 0; i < this.Columns.Count; i++) + try { - string[] tags; - string tag = ((string)this[i, 1].Value); - if (string.IsNullOrEmpty(tag)) - tags = new string[] { "<>" }; - else - tags = tag.Split(' '); + for (int i = 0; i < this.Columns.Count; i++) + { + string[] tags; + string tag = ((string)this[i, 1].Value); + if (string.IsNullOrEmpty(tag)) + tags = new string[] { "<>" }; + else + tags = tag.Split(' '); - // remove <> from tags - for (int j = 0; j < tags.Length; j++) - tags[j] = tags[j].Replace("<", "").Replace(">", ""); + // remove <> from tags + for (int j = 0; j < tags.Length; j++) + tags[j] = tags[j].Replace("<", "").Replace(">", ""); - verse[i] = new VerseWord((string)this[i, 0].Value, tags, reference); - if (osis) - { - verse[i].OsisTagIndex = CurrentVerse[i].OsisTagIndex; - verse[i].OsisTagLevel = CurrentVerse[i].OsisTagLevel; - verse.Dirty = true; + verse[i] = new VerseWord((string)this[i, 0].Value, tags, reference); + if (osis) + { + verse[i].OsisTagIndex = CurrentVerse[i].OsisTagIndex; + verse[i].OsisTagLevel = CurrentVerse[i].OsisTagLevel; + verse.Dirty = true; + } } - } - reference = osis? OsisBible.GetCorrectReference(reference) : Bible.GetCorrectReference(reference); + reference = osis ? OsisBible.GetCorrectReference(reference) : Bible.GetCorrectReference(reference); - Dictionary bible = osis ? OsisBible.Bible : Bible.Bible; - if (bible.ContainsKey(reference)) - { - bible[reference] = verse; + Dictionary bible = osis ? OsisBible.Bible : Bible.Bible; + if (bible.ContainsKey(reference)) + { + string oldVerse = bible[reference].ToString(); + bible[reference] = verse; + Tracing.TraceInfo(name, "OLD: " + oldVerse, "NEW: " + verse.ToString()); + + } + else + { + bible.Add(reference, verse); + } } - else + catch (Exception ex) { - bible.Add(reference, verse); + Tracing.TraceException(name, ex); + throw; } } #endregion Save & Update diff --git a/src/BibleTaggingUtil/OsisXml/OsisTag.cs b/src/BibleTaggingUtil/OsisXml/OsisTag.cs index 6138db7..b835b34 100644 --- a/src/BibleTaggingUtil/OsisXml/OsisTag.cs +++ b/src/BibleTaggingUtil/OsisXml/OsisTag.cs @@ -143,6 +143,7 @@ public List GetVerseWords() var cm = System.Reflection.MethodBase.GetCurrentMethod(); var name = cm.DeclaringType.FullName + "." + cm.Name; Tracing.TraceException(name, ex.Message); + throw; } return verseWords; @@ -260,6 +261,7 @@ public override string ToString() var cm = System.Reflection.MethodBase.GetCurrentMethod(); var name = cm.DeclaringType.FullName + "." + cm.Name; Tracing.TraceException(name, ex.Message); + throw; } return result; } @@ -297,6 +299,7 @@ internal void Update(VerseWord verseWord) var cm = System.Reflection.MethodBase.GetCurrentMethod(); var name = cm.DeclaringType.FullName + "." + cm.Name; Tracing.TraceException(name, ex.Message); + throw; } } diff --git a/src/BibleTaggingUtil/OsisXml/OsisUtils.cs b/src/BibleTaggingUtil/OsisXml/OsisUtils.cs index e253b6e..dff867f 100644 --- a/src/BibleTaggingUtil/OsisXml/OsisUtils.cs +++ b/src/BibleTaggingUtil/OsisXml/OsisUtils.cs @@ -38,11 +38,19 @@ private OsisUtils() public List GetOsisTags(XmlNode? root, string verseRef, int level=0) { List osisTags = new List(); - - foreach (XmlNode node in root.ChildNodes) + try + { + foreach (XmlNode node in root.ChildNodes) + { + osisTags.Add(GetVerseTag(level, osisTags.Count, node, verseRef)); + } + } + catch (Exception ex) { - osisTags.Add(GetVerseTag(level,osisTags.Count, node, verseRef)); - } + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); + } return osisTags; } diff --git a/src/BibleTaggingUtil/OsisXml/OsisVerse.cs b/src/BibleTaggingUtil/OsisXml/OsisVerse.cs index e13ea8f..f8a9a93 100644 --- a/src/BibleTaggingUtil/OsisXml/OsisVerse.cs +++ b/src/BibleTaggingUtil/OsisXml/OsisVerse.cs @@ -7,6 +7,7 @@ using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; +using System.Windows.Forms; using System.Xml; using static System.Windows.Forms.AxHost; using static System.Windows.Forms.VisualStyles.VisualStyleElement; @@ -24,6 +25,8 @@ public OsisVerse(string verseRef, int startIndex, string sid, string verseXml, s int x = 0; } VerseRef = verseRef; + int idx = verseRef.IndexOf("."); + Book = verseRef.Substring(0, idx); StartIndex = startIndex; Length = verseXml.Length; sID = sid; @@ -40,8 +43,14 @@ private void BuildOsisTags() string header = @""; string trailer = @""; - // Gen.3.1 - VerseXml = TreatOddTags(VerseXml, "p"); + if (VerseRef == "John.1.9") + { + //   + int x = 0; + } + + // Gen.3.1 + VerseXml = TreatOddTags(VerseXml, "p"); // Gen.2.23 VerseXml = TreatOddTags(VerseXml, "lg"); // 1Sam.25.1 @@ -52,6 +61,7 @@ private void BuildOsisTags() XmlDocument document = new XmlDocument(); VerseXml = VerseXml.Replace("> <", ">" + zeroWidthSpace + "<"); document.PreserveWhitespace = true; + try { document.LoadXml(header + VerseXml + trailer); @@ -61,9 +71,10 @@ private void BuildOsisTags() var cm = System.Reflection.MethodBase.GetCurrentMethod(); var name = cm.DeclaringType.FullName + "." + cm.Name; Tracing.TraceException(name, ex.Message); + MessageBox.Show("Exception Encountered\r\n" + ex.Message, "Fatal Error!"); + throw; } - osisTags = OsisUtils.Instance.GetOsisTags(document.DocumentElement, VerseRef); } @@ -143,6 +154,7 @@ private string TreatOddTags(string VerseXml, string tag) var cm = System.Reflection.MethodBase.GetCurrentMethod(); var name = cm.DeclaringType.FullName + "." + cm.Name; Tracing.TraceException(name, ex.Message); + throw; } return VerseXml; } @@ -191,12 +203,24 @@ public void UpdateVerse(Verse verseWords) } } + internal void UpdateVerseXml(string newCompleteXml) + { + VerseXml = newCompleteXml; + Length = VerseXml.Length; + } + + internal void UpdateVerseStartIndex(int newStartIndex) + { + StartIndex = newStartIndex; + } + public string VerseRef { get; } - public int StartIndex { get; } - public int Length { get; } + public int StartIndex { get; private set; } + public int Length { get; private set; } public string sID { get; } public string VerseXml { get; private set; } public string eID { get; } public bool Dirty { get; set; } + public string Book { get; internal set; } } } diff --git a/src/BibleTaggingUtil/Verse.cs b/src/BibleTaggingUtil/Verse.cs index ccd1bb0..c58212a 100644 --- a/src/BibleTaggingUtil/Verse.cs +++ b/src/BibleTaggingUtil/Verse.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; @@ -96,6 +97,11 @@ public void Merge(int start, int count) verse = temp; } + public override string ToString() + { + return Utils.GetVerseText(this, true); + } + /// /// Splits the word at index into two /// From c2192189fa11bc9fabe85e8a5e693bba13731b7a Mon Sep 17 00:00:00 2001 From: sabdelmalik Date: Tue, 23 Jan 2024 09:21:16 -0500 Subject: [PATCH 5/5] Added support for osis reference Bible to replace KJV if required --- .gitignore | 2 + src/BibleTaggingUtil/BibleTaggingForm.cs | 71 ++-- src/BibleTaggingUtil/BibleTaggingUtil.csproj | 2 +- .../BibleTaggingUtil.csproj.user | 2 +- .../BibleVersions/BibleVersion.cs | 348 +++++++++++++++++- ...ceVersionKJV.cs => ReferenceTopVersion.cs} | 4 +- .../BibleVersions/TargetOsisVersion.cs | 43 +-- src/BibleTaggingUtil/CofigurationHolder.cs | 9 +- .../Editor/EditorPanel.Designer.cs | 88 ++--- src/BibleTaggingUtil/Editor/EditorPanel.cs | 53 ++- .../{KJVGridView.cs => TopVersionGridView.cs} | 2 +- src/BibleTaggingUtil/OSIS_Generator.cs | 14 +- src/BibleTaggingUtil/OsisXml/OsisUtils.cs | 78 ++++ 13 files changed, 590 insertions(+), 126 deletions(-) rename src/BibleTaggingUtil/BibleVersions/{ReferenceVersionKJV.cs => ReferenceTopVersion.cs} (92%) rename src/BibleTaggingUtil/Editor/{KJVGridView.cs => TopVersionGridView.cs} (98%) diff --git a/.gitignore b/.gitignore index fce85ed..ad03036 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ Bibles/ BibleModuleDev/ !.gitignore src/BibleTaggingUtil/ReferenceBibles/NIV/01.NIV11-NT-Strong.txt +src/BibleTaggingUtil/ReferenceBibles/NASB2020/1.osis.xml +src/BibleTaggingUtil/ReferenceBibles/NIV/1.NIV11-NT-Strong.txt diff --git a/src/BibleTaggingUtil/BibleTaggingForm.cs b/src/BibleTaggingUtil/BibleTaggingForm.cs index 1f728be..c6ad5c7 100644 --- a/src/BibleTaggingUtil/BibleTaggingForm.cs +++ b/src/BibleTaggingUtil/BibleTaggingForm.cs @@ -36,12 +36,13 @@ public partial class BibleTaggingForm : Form private TargetVersion target; private TargetOsisVersion osisTarget; - private ReferenceVersionKJV referenceKJV; + private ReferenceTopVersion referenceTopVersion; private ReferenceVersionTOTHT referenceTOTHT; private ReferenceVersionTAGNT referenceTAGNT; private string execFolder = string.Empty; - private string kjvPath = string.Empty; + private string refFolder = string.Empty; + private string topReferencePath = string.Empty; private string tagntPath = string.Empty; private string tothtPath = string.Empty; private string crosswirePath = string.Empty; @@ -80,7 +81,7 @@ public BibleTaggingForm() target = new TargetVersion(this); osisTarget = new TargetOsisVersion(this); - referenceKJV = new ReferenceVersionKJV(this); + referenceTopVersion = new ReferenceTopVersion(this); referenceTOTHT = new ReferenceVersionTOTHT(this); referenceTAGNT = new ReferenceVersionTAGNT(this); } @@ -162,11 +163,8 @@ private void BibleTaggingForm_Load(object sender, EventArgs e) Tracing.InitialiseTrace(execFolder); crosswirePath = Path.Combine(execFolder, "Crosswire"); - string refFolder = Path.Combine(execFolder, "ReferenceBibles"); - if(workingOnNIV) - kjvPath = Path.Combine(refFolder, "NIV"); - else - kjvPath = Path.Combine(refFolder, "KJV"); + refFolder = Path.Combine(execFolder, "ReferenceBibles"); + tagntPath = Path.Combine(refFolder, "TAGNT"); tothtPath = Path.Combine(refFolder, "TOTHT"); @@ -274,13 +272,7 @@ private void LoadBibles() } } - editorPanel.TargetBibleName(Path.GetFileName(biblesFolder)); - target.BibleName = Path.GetFileName(biblesFolder); - osisTarget.BibleName = Path.GetFileName(biblesFolder); - referenceKJV.BibleName = "KJV"; - referenceTAGNT.BibleName = "TAGNT"; - referenceTOTHT.BibleName = "TOTHT"; - + // Load configuration config = new ConfigurationHolder(); string confResult = config.ReadBiblesConfig(biblesFolder); if (!string.IsNullOrEmpty(confResult)) @@ -325,8 +317,34 @@ private void LoadBibles() break; } } + if (string.IsNullOrEmpty(config.TopReferenceVersion)) + { + topReferencePath = Path.Combine(refFolder, "KJV"); + } + else + { + topReferencePath = Path.Combine(refFolder, config.TopReferenceVersion); + if (!Directory.Exists(topReferencePath)) + { + topReferencePath = Path.Combine(refFolder, "KJV"); + } + } + + + + editorPanel.TargetBibleName(Path.GetFileName(biblesFolder)); + target.BibleName = Path.GetFileName(biblesFolder); + osisTarget.BibleName = Path.GetFileName(biblesFolder); + string refBibleName = Path.GetFileName(topReferencePath); + referenceTopVersion.BibleName = refBibleName; + editorPanel.TopReferenceBibleName(refBibleName); + + referenceTAGNT.BibleName = "TAGNT"; + referenceTOTHT.BibleName = "TOTHT"; + + } - catch(Exception ex) + catch (Exception ex) { var cm = System.Reflection.MethodBase.GetCurrentMethod(); var name = cm.DeclaringType.FullName + "." + cm.Name; @@ -338,8 +356,13 @@ private void LoadBibles() string[] files = Directory.GetFiles(taggedFolder); if (files.Length > 0) { - if (Properties.Settings.Default.Osis) + string ext = Path.GetExtension(files[0]); + Properties.Settings.Default.Osis = false; + // if (Properties.Settings.Default.Osis) + if (ext.ToLower() == ".xml") { + Properties.Settings.Default.Osis = true; + generateOSISToolStripMenuItem.Visible = false; usfmToolStripMenuItem.Visible = false; oSISToolStripMenuItem.Visible = true; @@ -380,19 +403,19 @@ private void LoadBibles() this.Closing -= BibleTaggingForm_Closing; this.Closing += BibleTaggingForm_Closing; - if (!LoadReferenceFiles(kjvPath, referenceKJV)) { CloseForm(); return; } + if (!LoadReferenceFiles(topReferencePath, referenceTopVersion)) { CloseForm(); return; } if (!LoadReferenceFiles(tothtPath, referenceTOTHT)) { CloseForm(); return; } if (!LoadReferenceFiles(tagntPath, referenceTAGNT)) { CloseForm(); return; } /* bool result = false; - files = Directory.GetFiles(kjvPath); + files = Directory.GetFiles(topReferencePath); for (int i = 0; i < files.Length; i++) { - result = referenceKJV.LoadBibleFile(files[i], i == 0, i != (files.Length - 1)); + result = referenceTopVersion.LoadBibleFile(files[i], i == 0, i != (files.Length - 1)); } -// referenceKJV.LoadBibleFile(config.KJV, true, false); +// referenceTopVersion.LoadBibleFile(config.KJV, true, false); StartGui(); @@ -581,7 +604,7 @@ private DialogResult ShowMessageBox(string text, string caption, MessageBoxButto public VerseSelectionPanel VerseSelectionPanel { get { return verseSelectionPanel; } } public TargetVersion Target { get { return target; } } public TargetOsisVersion OsisTarget { get { return osisTarget; } } - public ReferenceVersionKJV KJV { get {return referenceKJV; } } + public ReferenceTopVersion TopVersion { get {return referenceTopVersion; } } public ReferenceVersionTOTHT TOTHT { get { return referenceTOTHT; } } public ReferenceVersionTAGNT TAGNT { get { return referenceTAGNT; } } @@ -801,9 +824,9 @@ private void saveKJVPlainToolStripMenuItem_Click(object sender, EventArgs e) for (int verse = 1; verse <= lastVerse; verse++) { string verseRef = string.Format("{0:s} {1:d}:{2:d}", bookName, chapter + 1, verse); - if (referenceKJV.Bible.ContainsKey(verseRef)) + if (referenceTopVersion.Bible.ContainsKey(verseRef)) { - string line = string.Format("{0:s} {1:s}", verseRef, Utils.GetVerseText(referenceKJV.Bible[verseRef], false)); + string line = string.Format("{0:s} {1:s}", verseRef, Utils.GetVerseText(referenceTopVersion.Bible[verseRef], false)); outputFile.WriteLine(line); } } diff --git a/src/BibleTaggingUtil/BibleTaggingUtil.csproj b/src/BibleTaggingUtil/BibleTaggingUtil.csproj index 7e5de8d..2da49fd 100644 --- a/src/BibleTaggingUtil/BibleTaggingUtil.csproj +++ b/src/BibleTaggingUtil/BibleTaggingUtil.csproj @@ -8,7 +8,7 @@ Copyright © 2023 by Sami Abdel Malik https://github.com/sabdelmalik/BibleTagging git - 5.1.0.0 + 5.2.0.0 Bible Text Tagging with Strong's Numbers diff --git a/src/BibleTaggingUtil/BibleTaggingUtil.csproj.user b/src/BibleTaggingUtil/BibleTaggingUtil.csproj.user index a6dcf6d..01dd2fe 100644 --- a/src/BibleTaggingUtil/BibleTaggingUtil.csproj.user +++ b/src/BibleTaggingUtil/BibleTaggingUtil.csproj.user @@ -20,7 +20,7 @@ Component - + Component diff --git a/src/BibleTaggingUtil/BibleVersions/BibleVersion.cs b/src/BibleTaggingUtil/BibleVersions/BibleVersion.cs index 3888886..ed0e05e 100644 --- a/src/BibleTaggingUtil/BibleVersions/BibleVersion.cs +++ b/src/BibleTaggingUtil/BibleVersions/BibleVersion.cs @@ -1,4 +1,5 @@ -using System; +using BibleTaggingUtil.OsisXml; +using System; using System.CodeDom; using System.Collections.Generic; using System.IO; @@ -6,8 +7,11 @@ using System.Reflection; using System.Text; using System.Text.RegularExpressions; +using System.Threading; using System.Threading.Tasks; using System.Windows.Forms.Design; +using System.Xml; +using System.Xml.Linq; using System.Xml.Schema; namespace BibleTaggingUtil.BibleVersions @@ -47,7 +51,7 @@ public BibleVersion(BibleTaggingForm container, int totalVerses) this.totalVerses = totalVerses; } - public string BibleName { set { bibleName = value; } } + public string BibleName { set { bibleName = value;} } public virtual bool LoadBibleFile(string textFilePath, bool newBible, bool more) { @@ -58,7 +62,11 @@ public virtual bool LoadBibleFile(string textFilePath, bool newBible, bool more) bookNamesList.Clear(); currentVerseCount = 0; } - return LoadBibleFileInternal(textFilePath, more); + string ext = Path.GetExtension(textFilePath); + if (ext == ".xml" && this is ReferenceTopVersion) + return LoadOsisBibleFileInternal(textFilePath, more); + else + return LoadBibleFileInternal(textFilePath, more); } protected virtual bool LoadBibleFileInternal(string textFilePath, bool more) @@ -377,5 +385,339 @@ public string GetCorrectReference(string reference) return correctReference; } + #region OSIS Top Version + /// + /// key: book name + /// value: osis segment representing this book + /// the first segment is the osis header + /// + private Dictionary osisDocSegments = new Dictionary(); + + /// + /// Key: book name + /// Value: Dictionary with: + /// Key: verse reference + /// value: Verse + /// + private Dictionary> bookTemp = new Dictionary>(); + + private int bookCount = 0; + private object lockingObject = new object(); + + /// + /// 1. Read all the contents of the OSIS xml as a string + /// 2. Use regex to get offset of each book and create a book/offset map + /// 3. GetVerse() takes a verse reference (e.g. Rev.19.14) and uses regex to find the offset and size + /// of the verse. (regex starts its search from the offset found in the offsets map) + /// 4. ParseVerse() takes the verse text enclosed between sID and eID and loads it into a XmlDocument. + /// This is because the verse content is well formed xml. This makes it easy to extract the verse's + /// words and Strong's tags. + /// + /// + /// + /// + /// + private bool LoadOsisBibleFileInternal(string filePath, bool more) + { + bool result = true; + string currentReference = string.Empty; + string currentSID = string.Empty; + Verse verse = null; + + try + { + string osisDoc = string.Empty; + using (StreamReader sr = new StreamReader(filePath)) + { + osisDoc = sr.ReadToEnd(); + } + osisDoc = osisDoc.Replace("\r\n", "").Replace("\n", "").Replace("

","").Replace("

", ""); + + osisDoc = RemoveOddTags(osisDoc, "p"); + osisDoc = RemoveOddTags(osisDoc, "lg"); + //osisDoc = RemoveOddTags(osisDoc, "div"); + osisDoc = RemoveOddTags(osisDoc, "l"); + + XmlDocument doc = new XmlDocument(); + doc.PreserveWhitespace = false; + doc.LoadXml(osisDoc); + XmlElement root = doc.DocumentElement; + + var verses = root.SelectNodes("//verse[@sID]"); + int totalVerses = verses.Count; + int verseCounter = 0; + + foreach (XmlNode node in verses) + { + container.UpdateProgress("Loading " + bibleName, (100 * verseCounter) / totalVerses); + + XmlNode xmlNode = node; + int wordIndex = 0; + while (true) + { + XmlNode temp1; + + if (xmlNode.Name == "verse") + { + if (xmlNode.Attributes["sID"] != null) + { + verse = new Verse(); + wordIndex = 0; + currentSID = xmlNode.Attributes["sID"].Value; + currentReference = OsisUtils.Instance.ChangeReferenceToLocalFormat(xmlNode.Attributes["osisID"].Value); + } + else if (xmlNode.Attributes["eID"] != null) + { + string eID = currentSID = xmlNode.Attributes["eID"].Value; + if (eID == currentSID) + { + bible[currentReference] = verse; + int idx = currentReference.IndexOf(" "); + if (idx != -1) + { + string book = currentReference.Substring(0, idx); + if (!bookNamesList.Contains(book)) + bookNamesList.Add(book); + } + verseCounter++; + break; + } + else + { + // error + result = false; + } + } + temp1 = xmlNode.NextSibling; + if (temp1 == null) + { + result = false; + } + else xmlNode = temp1; + + continue; + } + + if(xmlNode.Name == "l") + { + temp1 = xmlNode.NextSibling; + if (temp1 == null) + { + result = false; + } + else xmlNode = temp1; + + continue; + } + else if (xmlNode.Name == "#text") + { + verse[wordIndex++] = new VerseWord(xmlNode.InnerText, "", currentReference); + temp1 = xmlNode.NextSibling; + if (temp1 == null) + { + result = false; + } + else xmlNode = temp1; + + continue; + } + else if (xmlNode.Name == "w") + { + verse[wordIndex++] = new VerseWord(xmlNode.InnerText, + OsisUtils.Instance.GetStrongsFromLemma(xmlNode.Attributes["lemma"].Value).ToArray(), + currentReference); + + temp1 = xmlNode.NextSibling; + if (temp1 == null) + { + result = false; + } + else xmlNode = temp1; + + continue; + } + + temp1 = xmlNode.NextSibling; + if (temp1 == null) + { + result = false; + } + else xmlNode = temp1; + + continue; + } + } + + } + catch (Exception ex) + { + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); + throw; + } + + if (!more && !(new int[] { 66, 39, 27 }).Contains(bookNamesList.Count)) + { + Tracing.TraceError(MethodBase.GetCurrentMethod().Name, string.Format("{0}:Book Names Count = {1}. Was expecting 66, 39 or 27", + Path.GetFileName(filePath), bookNamesList.Count)); + return false; + } + + + if (bookNamesList.Count == 66 || bookNamesList.Count == 39) + { + for (int i = 0; i < bookNamesList.Count; i++) + { + bookNames.Add(Constants.ubsNames[i], bookNamesList[i]); + } + } + else if (bookNamesList.Count == 27) + { + for (int i = 0; i < bookNamesList.Count; i++) + { + bookNames.Add(Constants.ubsNames[i + 39], bookNamesList[i]); + } + } + + return result; + } + + private string RemoveOddTags(string VerseXml, string tag) + { + string sPattern = @"(<" + tag + @"[^>]*>)"; + if (tag == "div" || tag == "l") + sPattern = string.Format(@"(<{0}\s[^>]+>)", tag); + + string ePattern = @"(]*>)"; + string result = string.Empty; + + int startIndex = 0; + try + { + result = Regex.Replace(VerseXml, sPattern, ""); + result = Regex.Replace(result, ePattern, ""); + } + catch (Exception ex) + { + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); + throw; + } + return result; + } + + private void ProcessNodeL() + { + + } + + private void BookLoaded(string book, Dictionary localBible) + { + lock (lockingObject) + { + bookCount++; + container.UpdateProgress("Loading " + bibleName, (100 * bookCount) / 66); + bookTemp[book] = localBible; + } + } + + private void LoadBookFromSegments(Object threadContext) //string book) + { + string book = (string)threadContext; + + Dictionary localBible = new Dictionary(); + try + { + Regex regex = new Regex( + string.Format(@"", book)); + + MatchCollection VerseMatches = regex.Matches(osisDocSegments[book]); + if (VerseMatches.Count > 0) + { + foreach (Match VerseMatch in VerseMatches) + { + OsisVerse? osisVerse = OsisUtils.Instance.GetVersesTags(book, osisDocSegments[book], VerseMatch); + if (osisVerse != null) + { + //lock (this) + { + localBible.Add(osisVerse.VerseRefX, osisVerse); + } + // Verse verseWords = osisVerse.GetVerseWords(); + // bible.Add(osisVerse.VerseRefX, verseWords); + } + } + } + } + catch (Exception ex) + { + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); + throw; + } + finally + { + BookLoaded(book, localBible); + } + } + + private void BuildOsisSegments(string osisDoc) + { + osisDocSegments.Clear(); + try + { + while (true) + { + //
+ Regex regex = new Regex(@""); + MatchCollection matches = regex.Matches(osisDoc); + if (matches.Count > 0) + { + string book = "header"; + int start = 0; + + foreach (Match match in matches) + { + int offset = match.Index; + try + { + osisDocSegments.Add(book, osisDoc.Substring(start, offset - start)); + } + catch (Exception ex) + { + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); + throw; + } + + start = offset; + book = match.Groups[1].Value; + } + osisDocSegments.Add(book, osisDoc.Substring(start)); + break; + } + else + { + throw new Exception("Could not identify any book in the document"); + } + } + } + catch (Exception ex) + { + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); + throw; + } + } + + #endregion OSIS Top Version + + + } } diff --git a/src/BibleTaggingUtil/BibleVersions/ReferenceVersionKJV.cs b/src/BibleTaggingUtil/BibleVersions/ReferenceTopVersion.cs similarity index 92% rename from src/BibleTaggingUtil/BibleVersions/ReferenceVersionKJV.cs rename to src/BibleTaggingUtil/BibleVersions/ReferenceTopVersion.cs index 8cbb895..cf76ad9 100644 --- a/src/BibleTaggingUtil/BibleVersions/ReferenceVersionKJV.cs +++ b/src/BibleTaggingUtil/BibleVersions/ReferenceTopVersion.cs @@ -8,9 +8,9 @@ namespace BibleTaggingUtil.BibleVersions { - public class ReferenceVersionKJV : BibleVersion + public class ReferenceTopVersion : BibleVersion { - public ReferenceVersionKJV(BibleTaggingForm container) : base(container, 23145 + 7957) { } + public ReferenceTopVersion(BibleTaggingForm container) : base(container, 23145 + 7957) { } public void Load() { diff --git a/src/BibleTaggingUtil/BibleVersions/TargetOsisVersion.cs b/src/BibleTaggingUtil/BibleVersions/TargetOsisVersion.cs index 165498f..0a2941d 100644 --- a/src/BibleTaggingUtil/BibleVersions/TargetOsisVersion.cs +++ b/src/BibleTaggingUtil/BibleVersions/TargetOsisVersion.cs @@ -21,7 +21,7 @@ namespace BibleTaggingUtil.BibleVersions /// 2. Use regex to get offset of each book and create a book/offset map /// 3. GetVerse() takes a verse reference (e.g. Rev.19.14) and uses regex to find the offset and size /// of the verse. (regex starts its search from the offset found in the offsets map) - /// 4. ParseVerse() takes the verse text enclsed between sID and eID and loads it into a XmlDocument. + /// 4. ParseVerse() takes the verse text enclosed between sID and eID and loads it into a XmlDocument. /// This is because the verse content is well formed xml. This makes it easy to extract the verse's /// words and Strong's tags. /// 5. Once the tags has been update, the verse is reconstructed and is replaced in the text @@ -34,14 +34,19 @@ public class TargetOsisVersion : BibleVersion ///
private string osisDoc = string.Empty; + /// + /// key: book name + /// value: osis segment representing this book + /// the first segment is the osis header + /// private Dictionary osisDocSegments = new Dictionary(); /// - /// Key: Bible book name - /// value: Offset of the bookwithin the osisDoc string - /// The offsets speeds up the regex search for verses + /// Key: book name + /// Value: Dictionary with: + /// Key: verse reference + /// value: Verse /// -// private Dictionary bookOffsets = new Dictionary(); private Dictionary> bookTemp = new Dictionary>(); /// @@ -137,7 +142,7 @@ private void Load(string fileName) { foreach (Match VerseMatch in VerseMatches) { - OsisVerse? osisVerse = GetVersesTags(book, VerseMatch); + OsisVerse? osisVerse = OsisUtils.Instance.GetVersesTags(book, osisDocSegments[book], VerseMatch); if (osisVerse != null) { osisBible.Add(osisVerse.VerseRefX, osisVerse); @@ -260,7 +265,7 @@ private void LoadBookFromSegments(Object threadContext) //string book) { foreach (Match VerseMatch in VerseMatches) { - OsisVerse? osisVerse = GetVersesTags(book, VerseMatch); + OsisVerse? osisVerse = OsisUtils.Instance.GetVersesTags(book, osisDocSegments[book], VerseMatch); if (osisVerse != null) { //lock (this) @@ -286,7 +291,8 @@ private void LoadBookFromSegments(Object threadContext) //string book) } } - private OsisVerse? GetVersesTags(string book, Match VerseMatch) + + private OsisVerse? GetVersesTagsObsolete(string book, string osisSegment, Match VerseMatch) { OsisVerse result = null; @@ -306,7 +312,7 @@ private void LoadBookFromSegments(Object threadContext) //string book) { Regex regex = new Regex(string.Format(@"", sID)); // VerseMatch = regex.Match(osisDoc, startIndex); - VerseMatch = regex.Match(osisDocSegments[book]); + VerseMatch = regex.Match(osisSegment, startIndex); if (VerseMatch.Success) { endIndex = VerseMatch.Index; @@ -315,7 +321,7 @@ private void LoadBookFromSegments(Object threadContext) //string book) string verseXml = string.Empty; if (endIndex > startIndex) - verseXml = osisDocSegments[book].Substring(startIndex, endIndex - startIndex); + verseXml = osisSegment.Substring(startIndex, endIndex - startIndex); // verseXml = osisDoc.Substring(startIndex, endIndex - startIndex); result = new OsisVerse(verseRef, startIndex, sID, verseXml, eID); @@ -421,7 +427,7 @@ private void ParseVerse(string verse) { if (attr.Name == "lemma") { - strongs = GetStrongs(attr.Value); + strongs = OsisUtils.Instance.GetStrongsFromLemma(attr.Value); } } } @@ -431,21 +437,6 @@ private void ParseVerse(string verse) } } - private List GetStrongs(string value) - { - List strings = new List(); - MatchCollection matches = Regex.Matches(value, @"strong:([GH]\d\d\d\d)"); - if (matches.Count > 0) - { - foreach (Match match in matches) - { - strings.Add(match.Groups[1].Value); - } - } - - return strings; - } - internal void Save(string fileName) { var cm = System.Reflection.MethodBase.GetCurrentMethod(); diff --git a/src/BibleTaggingUtil/CofigurationHolder.cs b/src/BibleTaggingUtil/CofigurationHolder.cs index 12755b4..8518216 100644 --- a/src/BibleTaggingUtil/CofigurationHolder.cs +++ b/src/BibleTaggingUtil/CofigurationHolder.cs @@ -45,6 +45,7 @@ public string ReadBiblesConfig(string biblesFolder) ParseState state = ParseState.NONE; using (StreamReader sr = new StreamReader(configFilePath)) { + bool osis = false; while (sr.Peek() >= 0) { string line = sr.ReadLine().Trim(); @@ -73,7 +74,6 @@ public string ReadBiblesConfig(string biblesFolder) if (state == ParseState.TAGGING) { - bool osis = false; string[] parts = line.Split('='); if (parts.Length != 2) continue; @@ -89,6 +89,9 @@ public string ReadBiblesConfig(string biblesFolder) case "kjv": KJV = Path.Combine(biblesFolder, parts[1].Trim()); break; + case "topreferenceversion": + TopReferenceVersion = parts[1].Trim(); + break; case "hebrewreferences": { string[] hebParts = parts[1].Split(','); @@ -112,7 +115,6 @@ public string ReadBiblesConfig(string biblesFolder) break; case "osis": if(parts[1].Trim().ToLower() == "true") osis = true; - Properties.Settings.Default.Osis = osis; break; } } @@ -136,12 +138,15 @@ public string ReadBiblesConfig(string biblesFolder) } } + + Properties.Settings.Default.Osis = osis; } return string.Empty; } public string UnTaggedBible { get; private set; } public string TaggedBible { get; private set; } + public string TopReferenceVersion { get; private set; } public string KJV { get; private set; } public List HebrewReferences { get; private set; } diff --git a/src/BibleTaggingUtil/Editor/EditorPanel.Designer.cs b/src/BibleTaggingUtil/Editor/EditorPanel.Designer.cs index ed044a8..184a284 100644 --- a/src/BibleTaggingUtil/Editor/EditorPanel.Designer.cs +++ b/src/BibleTaggingUtil/Editor/EditorPanel.Designer.cs @@ -48,8 +48,8 @@ private void InitializeComponent() cbTagToFind = new System.Windows.Forms.ComboBox(); tbCurrentReference = new System.Windows.Forms.TextBox(); splitContainerMainEditor = new System.Windows.Forms.SplitContainer(); - dgvKJV = new KJVGridView(); - tbKJV = new System.Windows.Forms.TextBox(); + dgvTopVersion = new TopVersionGridView(); + tbTopVersion = new System.Windows.Forms.TextBox(); splitContainer1 = new System.Windows.Forms.SplitContainer(); dgvTOTHT = new TOHTHGridView(); tbTH = new System.Windows.Forms.TextBox(); @@ -72,7 +72,7 @@ private void InitializeComponent() splitContainerMainEditor.Panel1.SuspendLayout(); splitContainerMainEditor.Panel2.SuspendLayout(); splitContainerMainEditor.SuspendLayout(); - ((System.ComponentModel.ISupportInitialize)dgvKJV).BeginInit(); + ((System.ComponentModel.ISupportInitialize)dgvTopVersion).BeginInit(); ((System.ComponentModel.ISupportInitialize)splitContainer1).BeginInit(); splitContainer1.Panel1.SuspendLayout(); splitContainer1.Panel2.SuspendLayout(); @@ -255,8 +255,8 @@ private void InitializeComponent() // // splitContainerMainEditor.Panel1 // - splitContainerMainEditor.Panel1.Controls.Add(dgvKJV); - splitContainerMainEditor.Panel1.Controls.Add(tbKJV); + splitContainerMainEditor.Panel1.Controls.Add(dgvTopVersion); + splitContainerMainEditor.Panel1.Controls.Add(tbTopVersion); // // splitContainerMainEditor.Panel2 // @@ -265,16 +265,16 @@ private void InitializeComponent() splitContainerMainEditor.SplitterDistance = 266; splitContainerMainEditor.TabIndex = 4; // - // dgvKJV + // dgvTopVersion // - dgvKJV.AllowDrop = true; - dgvKJV.AllowUserToAddRows = false; - dgvKJV.AllowUserToDeleteRows = false; - dgvKJV.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells; - dgvKJV.BackgroundColor = System.Drawing.SystemColors.ControlLight; - dgvKJV.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; - dgvKJV.ColumnHeadersVisible = false; - dgvKJV.Cursor = System.Windows.Forms.Cursors.Hand; + dgvTopVersion.AllowDrop = true; + dgvTopVersion.AllowUserToAddRows = false; + dgvTopVersion.AllowUserToDeleteRows = false; + dgvTopVersion.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells; + dgvTopVersion.BackgroundColor = System.Drawing.SystemColors.ControlLight; + dgvTopVersion.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dgvTopVersion.ColumnHeadersVisible = false; + dgvTopVersion.Cursor = System.Windows.Forms.Cursors.Hand; dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft; dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Window; dataGridViewCellStyle1.Font = new System.Drawing.Font("Calibri", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); @@ -282,33 +282,33 @@ private void InitializeComponent() dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight; dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText; dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.False; - dgvKJV.DefaultCellStyle = dataGridViewCellStyle1; - dgvKJV.Dock = System.Windows.Forms.DockStyle.Fill; - dgvKJV.GridColor = System.Drawing.SystemColors.ControlText; - dgvKJV.Location = new System.Drawing.Point(0, 31); - dgvKJV.Name = "dgvKJV"; - dgvKJV.ReadOnly = true; - dgvKJV.RowHeadersVisible = false; - dgvKJV.RowHeadersWidth = 51; - dgvKJV.RowTemplate.Height = 29; - dgvKJV.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.CellSelect; - dgvKJV.ShowCellToolTips = false; - dgvKJV.Size = new System.Drawing.Size(1299, 235); - dgvKJV.TabIndex = 4; - // - // tbKJV - // - tbKJV.BackColor = System.Drawing.SystemColors.MenuHighlight; - tbKJV.Dock = System.Windows.Forms.DockStyle.Top; - tbKJV.Font = new System.Drawing.Font("Segoe UI", 10.8F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point); - tbKJV.ForeColor = System.Drawing.SystemColors.HighlightText; - tbKJV.Location = new System.Drawing.Point(0, 0); - tbKJV.Name = "tbKJV"; - tbKJV.ReadOnly = true; - tbKJV.Size = new System.Drawing.Size(1299, 31); - tbKJV.TabIndex = 3; - tbKJV.Text = "KJV"; - tbKJV.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; + dgvTopVersion.DefaultCellStyle = dataGridViewCellStyle1; + dgvTopVersion.Dock = System.Windows.Forms.DockStyle.Fill; + dgvTopVersion.GridColor = System.Drawing.SystemColors.ControlText; + dgvTopVersion.Location = new System.Drawing.Point(0, 31); + dgvTopVersion.Name = "dgvTopVersion"; + dgvTopVersion.ReadOnly = true; + dgvTopVersion.RowHeadersVisible = false; + dgvTopVersion.RowHeadersWidth = 51; + dgvTopVersion.RowTemplate.Height = 29; + dgvTopVersion.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.CellSelect; + dgvTopVersion.ShowCellToolTips = false; + dgvTopVersion.Size = new System.Drawing.Size(1299, 235); + dgvTopVersion.TabIndex = 4; + // + // tbTopVersion + // + tbTopVersion.BackColor = System.Drawing.SystemColors.MenuHighlight; + tbTopVersion.Dock = System.Windows.Forms.DockStyle.Top; + tbTopVersion.Font = new System.Drawing.Font("Segoe UI", 10.8F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point); + tbTopVersion.ForeColor = System.Drawing.SystemColors.HighlightText; + tbTopVersion.Location = new System.Drawing.Point(0, 0); + tbTopVersion.Name = "tbTopVersion"; + tbTopVersion.ReadOnly = true; + tbTopVersion.Size = new System.Drawing.Size(1299, 31); + tbTopVersion.TabIndex = 3; + //tbTopVersion.Text = "KJV"; + tbTopVersion.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; // // splitContainer1 // @@ -451,7 +451,7 @@ private void InitializeComponent() splitContainerMainEditor.Panel2.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)splitContainerMainEditor).EndInit(); splitContainerMainEditor.ResumeLayout(false); - ((System.ComponentModel.ISupportInitialize)dgvKJV).EndInit(); + ((System.ComponentModel.ISupportInitialize)dgvTopVersion).EndInit(); splitContainer1.Panel1.ResumeLayout(false); splitContainer1.Panel1.PerformLayout(); splitContainer1.Panel2.ResumeLayout(false); @@ -471,7 +471,7 @@ private void InitializeComponent() private System.Windows.Forms.SplitContainer splitContainerMainEditor; private System.Windows.Forms.SplitContainer splitContainer1; private TargetGridView dgvTarget; - private KJVGridView dgvKJV; + private TopVersionGridView dgvTopVersion; private TOHTHGridView dgvTOTHT; private System.Windows.Forms.ComboBox cbTagToFind; private System.Windows.Forms.PictureBox picNextVerse; @@ -483,7 +483,7 @@ private void InitializeComponent() private System.Windows.Forms.PictureBox picFindTagForward; private System.Windows.Forms.ToolTip toolTip1; private System.Windows.Forms.PictureBox picRedo; - private System.Windows.Forms.TextBox tbKJV; + private System.Windows.Forms.TextBox tbTopVersion; private System.Windows.Forms.TextBox tbTH; private System.Windows.Forms.TextBox tbTarget; private System.Windows.Forms.PictureBox picRefresh; diff --git a/src/BibleTaggingUtil/Editor/EditorPanel.cs b/src/BibleTaggingUtil/Editor/EditorPanel.cs index 53deb3f..02415a9 100644 --- a/src/BibleTaggingUtil/Editor/EditorPanel.cs +++ b/src/BibleTaggingUtil/Editor/EditorPanel.cs @@ -93,8 +93,8 @@ public EditorPanel(BibleTaggingForm container, BrowserPanel browser, VerseSelect private void EditorPanel_Load(object sender, EventArgs e) { - int tbKJVHeight = tbKJV.Height; - int dgvKJVHeight = dgvKJV.Height; + int tbTopVersionHeight = tbTopVersion.Height; + int dgvTopVersionHeight = dgvTopVersion.Height; int tbTHHeight = tbTH.Height; int dgvTOTHTHeight = dgvTOTHT.Height; @@ -115,7 +115,7 @@ private void EditorPanel_Load(object sender, EventArgs e) } - dgvKJV.CellContentDoubleClick += Dgv_CellContentDoubleClick; + dgvTopVersion.CellContentDoubleClick += Dgv_CellContentDoubleClick; dgvTarget.CellContentDoubleClick += Dgv_CellContentDoubleClick; dgvTOTHT.CellContentDoubleClick += Dgv_CellContentDoubleClick; //dgvTOTHT.CellContentDoubleClick += DgvTOTHTView_CellContentDoubleClick; @@ -126,7 +126,7 @@ private void EditorPanel_Load(object sender, EventArgs e) dgvTarget.KeyDown += DgvTarget_KeyDown; dgvTarget.KeyUp += DgvTarget_KeyUp; - dgvKJV.KeyUp += DgvTarget_KeyUp; + dgvTopVersion.KeyUp += DgvTarget_KeyUp; dgvTOTHT.KeyUp += DgvTarget_KeyUp; dgvTarget.SearchTag = cbTagToFind.Text; @@ -156,8 +156,8 @@ public void ClearCurrentVerse() tbCurrentReference.Text = string.Empty; dgvTarget.Rows.Clear(); dgvTarget.ColumnCount = 0; - dgvKJV.Rows.Clear(); - dgvKJV.ColumnCount = 0; + dgvTopVersion.Rows.Clear(); + dgvTopVersion.ColumnCount = 0; dgvTOTHT.Rows.Clear(); dgvTOTHT.ColumnCount = 0; } @@ -193,15 +193,15 @@ private void Verse_VerseChanged(object sender, VerseChangedEventArgs e) string actualBookName = string.Empty; try { - // KJV view - actualBookName = container.KJV[bookName]; + // Top Version view + actualBookName = container.TopVersion[bookName]; if (!string.IsNullOrEmpty(actualBookName)) { string reference = e.VerseReference.Replace(bookName, actualBookName); - if (container.KJV.Bible.ContainsKey(reference)) - dgvKJV.Update(container.KJV.Bible[reference]); + if (container.TopVersion.Bible.ContainsKey(reference)) + dgvTopVersion.Update(container.TopVersion.Bible[reference]); else - dgvKJV.Update(null); + dgvTopVersion.Update(null); } } catch (Exception ex) @@ -542,13 +542,13 @@ private void SelectReferenceTags(string tag, bool firstHalf) { try { - dgvKJV.ClearSelection(); + dgvTopVersion.ClearSelection(); dgvTOTHT.ClearSelection(); if (string.IsNullOrEmpty(tag)) { SetHighlightedCell(dgvTOTHT, null, null, firstHalf); - SetHighlightedCell(dgvKJV, null, null, firstHalf); + SetHighlightedCell(dgvTopVersion, null, null, firstHalf); return; } @@ -564,7 +564,7 @@ private void SelectReferenceTags(string tag, bool firstHalf) } SetHighlightedCell(dgvTOTHT, tags1, tags2, firstHalf); - SetHighlightedCell(dgvKJV, tags1, tags2, firstHalf); + SetHighlightedCell(dgvTopVersion, tags1, tags2, firstHalf); } catch (Exception ex) @@ -707,9 +707,9 @@ private void picSave_Click(object sender, EventArgs e) private void picDecreaseFont_Click(object sender, EventArgs e) { - Font font = dgvKJV.DefaultCellStyle.Font; - dgvKJV.DefaultCellStyle.Font = new Font(font.Name, font.Size - 1); - font = dgvKJV.DefaultCellStyle.Font; + Font font = dgvTopVersion.DefaultCellStyle.Font; + dgvTopVersion.DefaultCellStyle.Font = new Font(font.Name, font.Size - 1); + font = dgvTopVersion.DefaultCellStyle.Font; dgvTarget.DefaultCellStyle.Font = new Font(font.Name, font.Size - 1); font = dgvTOTHT.DefaultCellStyle.Font; dgvTOTHT.DefaultCellStyle.Font = new Font(font.Name, font.Size - 1); @@ -717,9 +717,9 @@ private void picDecreaseFont_Click(object sender, EventArgs e) } private void picIncreaseFont_Click(object sender, EventArgs e) { - Font font = dgvKJV.DefaultCellStyle.Font; - dgvKJV.DefaultCellStyle.Font = new Font(font.Name, font.Size + 1); - font = dgvKJV.DefaultCellStyle.Font; + Font font = dgvTopVersion.DefaultCellStyle.Font; + dgvTopVersion.DefaultCellStyle.Font = new Font(font.Name, font.Size + 1); + font = dgvTopVersion.DefaultCellStyle.Font; dgvTarget.DefaultCellStyle.Font = new Font(font.Name, font.Size + 1); font = dgvTOTHT.DefaultCellStyle.Font; dgvTOTHT.DefaultCellStyle.Font = new Font(font.Name, font.Size + 1); @@ -782,6 +782,19 @@ public void TargetBibleName(string name) tbTarget.Text = name; } } + + public void TopReferenceBibleName(string name) + { + if (InvokeRequired) + { + Action safeWrite = delegate { TopReferenceBibleName(name); }; + Invoke(safeWrite); + } + else + { + tbTopVersion.Text = name; + } + } private void cbTagToFind_SelectedIndexChanged(object sender, EventArgs e) { dgvTarget.SearchTag = cbTagToFind.Text; diff --git a/src/BibleTaggingUtil/Editor/KJVGridView.cs b/src/BibleTaggingUtil/Editor/TopVersionGridView.cs similarity index 98% rename from src/BibleTaggingUtil/Editor/KJVGridView.cs rename to src/BibleTaggingUtil/Editor/TopVersionGridView.cs index a9c2152..3841732 100644 --- a/src/BibleTaggingUtil/Editor/KJVGridView.cs +++ b/src/BibleTaggingUtil/Editor/TopVersionGridView.cs @@ -9,7 +9,7 @@ namespace BibleTaggingUtil.Editor { - internal class KJVGridView : DataGridView + internal class TopVersionGridView : DataGridView { protected override void OnCellMouseDown(DataGridViewCellMouseEventArgs e) { diff --git a/src/BibleTaggingUtil/OSIS_Generator.cs b/src/BibleTaggingUtil/OSIS_Generator.cs index 638a6c6..4c4ecf5 100644 --- a/src/BibleTaggingUtil/OSIS_Generator.cs +++ b/src/BibleTaggingUtil/OSIS_Generator.cs @@ -450,7 +450,13 @@ private string GetTaggedVerse(string verseText, string strongPrefix) if (string.IsNullOrEmpty(strongStr)) words.Add(string.Format("{0}", word)); else - words.Add(string.Format("{1}", strongStr, word)); + { + //if((strongStr.Contains("H3068") || strongStr.Contains("H3069")) && + // osisConf[OsisConstants.osisIDWork].ToLower().Contains("ara")) + // words.Add(string.Format("{1}", strongStr, word)); + //else + words.Add(string.Format("{1}", strongStr, word)); + } } word = string.Empty; @@ -470,7 +476,11 @@ private string GetTaggedVerse(string verseText, string strongPrefix) { strongStr += string.Format(" strong:{0}{1}", strongPrefix, tags[j]); } - words.Add(string.Format("{1}", strongStr, word)); + //if ((strongStr.Contains("H3068") || strongStr.Contains("H3069")) && + // osisConf[OsisConstants.osisIDWork].ToLower().Contains("ara")) + // words.Add(string.Format("{1}", strongStr, word)); + //else + words.Add(string.Format("{1}", strongStr, word)); } } diff --git a/src/BibleTaggingUtil/OsisXml/OsisUtils.cs b/src/BibleTaggingUtil/OsisXml/OsisUtils.cs index dff867f..1f24d64 100644 --- a/src/BibleTaggingUtil/OsisXml/OsisUtils.cs +++ b/src/BibleTaggingUtil/OsisXml/OsisUtils.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Xml; using System.Xml.Linq; @@ -50,10 +51,87 @@ public List GetOsisTags(XmlNode? root, string verseRef, int level=0) var cm = System.Reflection.MethodBase.GetCurrentMethod(); var name = cm.DeclaringType.FullName + "." + cm.Name; Tracing.TraceException(name, ex.Message); + } return osisTags; } + + public List GetStrongsFromLemma(string value) + { + List strings = new List(); + MatchCollection matches = Regex.Matches(value, @"strong:([GH]\d\d\d\d)"); + if (matches.Count > 0) + { + foreach (Match match in matches) + { + strings.Add(match.Groups[1].Value); + } + } + + return strings; + } + + public OsisVerse? GetVersesTags(string book, string osisSegment, Match VerseMatch) + { + OsisVerse result = null; + + int startIndex = VerseMatch.Index + VerseMatch.Groups[0].Length; + string sID = VerseMatch.Groups[1].Value; + string eID = string.Empty; + string verseRef = string.Format("{0}.{1}.{2}", book, VerseMatch.Groups[2].Value, VerseMatch.Groups[3].Value); + string verseXML = string.Empty; + int endIndex = 0; + + if (verseRef == "Gen.2.7") + { + int x = 0; + } + + try + { + Regex regex = new Regex(string.Format(@"", sID)); + // VerseMatch = regex.Match(osisDoc, startIndex); + VerseMatch = regex.Match(osisSegment, startIndex); + if (VerseMatch.Success) + { + endIndex = VerseMatch.Index; + eID = VerseMatch.Groups[1].Value; + } + + string verseXml = string.Empty; + if (endIndex > startIndex) + verseXml = osisSegment.Substring(startIndex, endIndex - startIndex); + // verseXml = osisDoc.Substring(startIndex, endIndex - startIndex); + + result = new OsisVerse(verseRef, startIndex, sID, verseXml, eID); + } + catch (Exception ex) + { + var cm = System.Reflection.MethodBase.GetCurrentMethod(); + var name = cm.DeclaringType.FullName + "." + cm.Name; + Tracing.TraceException(name, ex.Message); + throw; + } + + return result; + } + + public string ChangeReferenceToLocalFormat(string VerseRef) + { + string verseRefX = string.Empty; + + Match m = Regex.Match(VerseRef, @"([1-9A-Za-z]*)\.(\d{1,3})\.(\d{1,3})"); + if (m != null) + { + string book = m.Groups[1].Value; + string chapter = m.Groups[2].Value; + string verse = m.Groups[3].Value; + + verseRefX = string.Format("{0} {1}:{2}", book, chapter, verse); + } + return verseRefX; + } private OsisTag GetVerseTag(int level, int index, XmlNode node, string verseRef) { VerseTagType tagType = VerseTagType.UNKNOWN;