Universal Bible API 1.0.2011.1274

Scriptureparser.cs

Go to the documentation of this file.
00001 using System;
00002 using System.Collections.Generic;
00003 using System.Linq;
00004 using System.IO;
00005 using System.Xml;
00006 using System.Xml.Linq;
00007 using System.Text;
00008 using System.Text.RegularExpressions;
00009 
00010 namespace UniversalBibleAPI
00011 {
00012 
00016     public class ScriptureEventArgs : EventArgs
00017     {
00022         public string UnparsedReference { get; set; }
00027         public string BookNumber { get; set; }
00032         public string ChapterNumber { get; set; }
00037         public string VerseNumber { get; set; }
00042         public bool IsValid { get; set; }
00043     }
00044 
00048     public class Scriptureparser
00049     {
00050         List<string> ChapterNumbersList = new List<string>();
00051         List<string> VerseNumbersList = new List<string>();
00052         private string LastBookNumber;
00053         private string LastBook;
00054         private string tmpbook;
00055         XDocument BibleBookNames;
00059         public event EventHandler<ScriptureEventArgs> OnSingleScriptureParsed;
00063         public event EventHandler OnScriptureParsed;
00064 
00065         public Scriptureparser()
00066         {
00067             if (File.Exists("bnames.xml"))
00068             {
00069                 BibleBookNames = XDocument.Load("bnames.xml");
00070             }
00071             else {
00072 
00073                 BibleBookNames = XDocument.Parse(Properties.Resources.bnames);
00074             
00075             }
00076         }
00081         public void ParseScripture(string reference)
00082         {
00083             char[] delimiters = new char[] { ';' };
00084             char[] delimiters2 = new char[] { ',', ':' };
00085             try
00086             {
00087                 string[] REFS = reference.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
00088                 foreach (string refer in REFS)
00089                 {
00090 
00091                     if (OnSingleScriptureParsed != null)
00092                     {
00093                         string WorkingRef = refer.Trim().Replace(" ", ""); 
00094 
00095                         ScriptureEventArgs SEARGS = new ScriptureEventArgs();
00096                         SEARGS.IsValid = true;
00097                         SEARGS.UnparsedReference = WorkingRef;
00098 
00099                         Match RegMatchBook = Regex.Match(WorkingRef, "^[1-9]*[\\s]*[\\.]*[\\p{Lu}]+", RegexOptions.IgnoreCase);
00100                         if (SEARGS.IsValid = RegMatchBook.Success)
00101                         {
00102 
00103                             SEARGS.BookNumber = GetBookNumber(RegMatchBook.Value);
00104                             
00105                             if (SEARGS.BookNumber == "0") {
00106                                 // Couldn't parse bible book name  
00107                                 SEARGS.IsValid = false;
00108                             
00109                             }
00110                             WorkingRef = WorkingRef.Replace(RegMatchBook.Value, "");
00111                         }
00112 
00113                         string[] CVP = WorkingRef.Trim().Split(delimiters2, StringSplitOptions.RemoveEmptyEntries);
00114 
00115                         if (CVP.Length == 1)
00116                         {
00117 
00118                             SEARGS.IsValid = ExpandChapter(CVP[0]);
00119 
00120                             SEARGS.IsValid = ExpandChapterVerse("0");
00121 
00122                         }
00123 
00124                         if (CVP.Length == 0)
00125                         {
00126 
00127                             SEARGS.IsValid = ExpandChapter("0");
00128 
00129                             SEARGS.IsValid = ExpandChapterVerse("0");
00130 
00131                         }
00132 
00133                         if (CVP.Length == 2)
00134                         {
00135 
00136                             SEARGS.IsValid = ExpandChapter(CVP[0]);
00137 
00138                             SEARGS.IsValid = ExpandChapterVerse(CVP[1]);
00139 
00140                         }
00141 
00142                         foreach (string ChapterNumber in ChapterNumbersList)
00143                         {
00144 
00145                             SEARGS.ChapterNumber = ChapterNumber;
00146                             foreach (string VersNumber in VerseNumbersList)
00147                             {
00148 
00149                                 SEARGS.VerseNumber = VersNumber;
00150 
00151                                 OnSingleScriptureParsed(this, SEARGS);
00152                             }
00153 
00154                         }
00155 
00156 
00157 
00158                     }
00159                 }
00160 
00161                 if (OnScriptureParsed != null) {
00162 
00163                     OnScriptureParsed(this, new EventArgs());
00164 
00165                 }
00166 
00167             }
00168             catch (Exception)
00169             {
00170 
00171                 throw;
00172             }
00173         }
00174 
00182         public string GetOsisID(string BookNumber) {
00183              
00184             var descr = from n in BibleBookNames.Descendants("ID") where (n.Attribute("descr").Value == "english") select n;
00185             if (descr.Count() > 0)
00186             {
00187                 var OsisID = from n in descr.ElementAt(0).Descendants("BOOK") where (n.Attribute("bnumber").Value == BookNumber) select n;
00188                 if (OsisID.Count() > 0)
00189                 {
00190                     return OsisID.ElementAt(0).Attribute("bshort").Value;
00191                 }
00192                 else
00193                 {
00194 
00195                     return "0";
00196                 }
00197             }
00198             else
00199             {
00200 
00201                 return "0";
00202             }
00203         
00204         }
00205         public CurrentContentKey GetKeyFromLine(string Inputline) {
00206            
00207             CurrentContentKey tmp = new CurrentContentKey();
00208             tmp.Book = "0";
00209             tmp.Chapter = "0";
00210             tmp.Verse = "0";
00211             tmp.ContentText = "Nothing";
00212             tmp.ContentType = CurrentContentKey.TypeOfContentKey.Nothing;
00213             try
00214             {
00215                
00216                 Match RegMatchBook = Regex.Match(Inputline, "(^[0-9]*[\\s]*[\\.]*[\\p{Lu}]+)[\\s]*([0-9]+)[,:]([0-9]+)[\\s]*(.+)", RegexOptions.IgnoreCase);
00217                
00218                 if (RegMatchBook.Success)
00219                 {
00220                    
00221                     tmp.Book = GetBookNumber(RegMatchBook.Groups[1].Value);                  
00222                     tmp.Chapter = RegMatchBook.Groups[2].Value;
00223                     tmp.Verse = RegMatchBook.Groups[3].Value;
00224                     tmp.ContentText = RegMatchBook.Groups[4].Value;
00225                     tmp.ContentType = CurrentContentKey.TypeOfContentKey.Verse;
00226                     return tmp;
00227                 };
00228 
00229                 RegMatchBook = Regex.Match(Inputline, "(^[0-9]+)[\\s]+([0-9]+)[\\s]*[,:][\\s]*([0-9]+)[\\s]+(.+)");
00230 
00231                 if (RegMatchBook.Success)
00232                 {
00233 
00234                     tmp.Book = GetBookNumber(RegMatchBook.Groups[1].Value);
00235                     tmp.Chapter = RegMatchBook.Groups[2].Value;
00236                     tmp.Verse = RegMatchBook.Groups[3].Value;
00237                     tmp.ContentText = RegMatchBook.Groups[4].Value;
00238                     tmp.ContentType = CurrentContentKey.TypeOfContentKey.Verse;
00239                     return tmp;
00240                 };
00241 
00242                 RegMatchBook = Regex.Match(Inputline, "^##(.+)");
00243                 if (RegMatchBook.Success)
00244                 {
00245                     tmp.ContentText = RegMatchBook.Groups[1].Value;
00246                     tmp.ContentType = CurrentContentKey.TypeOfContentKey.Remark;
00247                     return tmp;
00248                 };
00249 
00250                 RegMatchBook = Regex.Match(Inputline, "^#(.+)");
00251                 if (RegMatchBook.Success)
00252                 {
00253                     tmp.ContentText = RegMatchBook.Groups[1].Value;
00254                     tmp.ContentType = CurrentContentKey.TypeOfContentKey.Caption;
00255                     return tmp;
00256                 };
00257                 return tmp;
00258             }
00259             catch (Exception e)
00260             {
00261 
00262                 tmp.ContentText = "Exception: " + e.Message;
00263                 tmp.ContentType = CurrentContentKey.TypeOfContentKey.Nothing;
00264                 return tmp;
00265             }
00266         
00267         }
00273         public string GetBookNumber(string Book)
00274         {
00275             if (Book == "") {
00276 
00277                 LastBook = Book;
00278               
00279             };
00280             if (Book == LastBook)
00281             {
00282                 return LastBookNumber;
00283             }
00284             else
00285             {
00286                 tmpbook = Book.Replace(".", "").Replace(" ", "").ToLower().Trim();
00287 
00288                 var BookNumber = from n in BibleBookNames.Descendants("BOOK") where (n.Attribute("bshort").Value.Replace(".", "").Replace(" ", "").ToLower().Trim() == tmpbook) select n.Attribute("bnumber").Value;
00289                 
00290                 if (BookNumber.Count() > 0)
00291                 {
00292                     LastBook = Book;
00293                     LastBookNumber = BookNumber.ElementAt(0);
00294                     return BookNumber.ElementAt(0);
00295                 }
00296                 else
00297                 {
00298                     BookNumber = from n in BibleBookNames.Descendants("BOOK") where (n.Value.Replace(".", "").Replace(" ", "").ToLower().Trim() == tmpbook) select n.Attribute("bnumber").Value;
00299                     if (BookNumber.Count() > 0)
00300                     {
00301                         LastBook = Book;
00302                         LastBookNumber = BookNumber.ElementAt(0);
00303                         return BookNumber.ElementAt(0);
00304                     }
00305                     else
00306                     {
00307                         return Book;
00308                     }
00309                 };
00310             }
00311         }
00319         public string GetBookName(string BookNumber, string Descr, bool ShortBookName) {
00320             try
00321             {
00322                 var BookName = from n in BibleBookNames.Descendants("BOOK") where (n.Parent.Attribute("descr").Value.ToLower() == Descr & n.Attribute("bnumber").Value == BookNumber) select n;
00323                 if (BookName.Count() > 0)
00324                 {
00325                     if (ShortBookName)
00326                     {
00327                         return BookName.ElementAt(0).Attribute("bshort").Value;
00328                     }
00329                     else {
00330 
00331                         return BookName.ElementAt(0).Value;
00332                     }
00333                 }
00334                 else {
00335 
00336                     return BookNumber;
00337                 }
00338             }
00339             catch (Exception e)
00340             {
00341                 
00342                 throw e;
00343             }
00344         
00345         }
00346         private bool ExpandChapterVerse(string p)
00347         {
00348             char[] delimiters = new char[] { '.' };
00349             char[] delimiters2 = new char[] { '-' };
00350             bool IsValid = true;
00351             try
00352             {
00353                 VerseNumbersList.Clear();
00354                 string[] VerseParts = p.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
00355 
00356                 foreach (string VP in VerseParts)
00357                 {
00358 
00359                     if (!VP.Trim().Contains("-"))
00360                     {
00361 
00362                         VerseNumbersList.Add(VP);
00363                     }
00364                     else
00365                     {
00366 
00367                         if (IsValid = Regex.Match(VP.Trim(), "^[1-9][0-9]*[-][1-9][0-9]*$").Success)
00368                         {
00369 
00370                             string[] RangeParts = VP.Split(delimiters2, StringSplitOptions.RemoveEmptyEntries);
00371                             int FromV = Convert.ToInt16(RangeParts[0]);
00372                             int TillV = Convert.ToInt16(RangeParts[1]);
00373                             if (FromV <= TillV)
00374                             {
00375                                 for (int i = FromV; i <= TillV; i++)
00376                                 {
00377                                     VerseNumbersList.Add(i.ToString());
00378                                 }
00379                             }
00380                             else
00381                             {
00382                                 for (int i = FromV; i >= TillV; i--)
00383                                 {
00384 
00385                                     VerseNumbersList.Add(i.ToString());
00386                                 }
00387 
00388                             };
00389 
00390                         }
00391 
00392                     }
00393                 }
00394 
00395 
00396                 return IsValid;
00397 
00398             }
00399             catch (Exception)
00400             {
00401 
00402                 throw;
00403             }
00404         }
00405         private bool ExpandChapter(string p)
00406         {
00407             char[] delimiters = new char[] { '.' };
00408             char[] delimiters2 = new char[] { '-' };
00409             bool IsValid = true;
00410             try
00411             {
00412                 ChapterNumbersList.Clear();
00413                 string[] ChapterParts = p.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
00414 
00415                 foreach (string CP in ChapterParts)
00416                 {
00417 
00418                     if (!CP.Trim().Contains("-"))
00419                     {
00420 
00421                         ChapterNumbersList.Add(CP);
00422                     }
00423                     else
00424                     {
00425 
00426                         if (IsValid = Regex.Match(CP, "^[1-9]+[-][1-9]+$").Success)
00427                         {
00428 
00429                             string[] RangeParts = CP.Trim().Split(delimiters2, StringSplitOptions.RemoveEmptyEntries);
00430                             int FromV = Convert.ToInt16(RangeParts[0]);
00431                             int TillV = Convert.ToInt16(RangeParts[1]);
00432                             if (FromV <= TillV)
00433                             {
00434                                 for (int i = FromV; i <= TillV; i++)
00435                                 {
00436                                     ChapterNumbersList.Add(i.ToString());
00437                                 }
00438                             }
00439                             else
00440                             {
00441                                 for (int i = FromV; i >= TillV; i--)
00442                                 {
00443 
00444                                     ChapterNumbersList.Add(i.ToString());
00445                                 }
00446 
00447                             };
00448 
00449                         }
00450 
00451                     }
00452                 }
00453 
00454 
00455                 return IsValid;
00456 
00457             }
00458             catch (Exception)
00459             {
00460 
00461                 throw;
00462             }
00463         }
00464     }
00465 }
 All Classes Namespaces Files Functions Enumerations Properties Events