Universal Bible API 1.0.2011.1274

VplBible.cs

Go to the documentation of this file.
00001 using System;
00002 using System.IO;
00003 using System.Collections;
00004 using System.Collections.Generic;
00005 using System.ComponentModel;
00006 using System.Linq;
00007 using System.Text;
00008 using System.Text.RegularExpressions;
00009 
00010 namespace UniversalBibleAPI
00011 {   
00012 
00013 
00014     public class VplBible : IBibleModul
00015     {
00016         private string FModuleName = "No Name";
00017         private string FModulePath = "";
00018         private List<CurrentContentKey> FVplBible;
00019         public List<CurrentContentKey> SetCurrentContentKeys
00020         {
00021             get { return FVplBible; }
00022             set { FVplBible = value; }
00023         }
00024         private BackgroundWorker BackGroundWorkerLoadModul;
00025         private Scriptureparser BibleParser = new Scriptureparser();
00026         private List<CurrentContentKey> ResultVersKeyList;
00027         #region IBibleModul Members
00028 
00029         public event EventHandler<ModulConnectedEventArgs> OnConnected;
00030 
00031         public event EventHandler<ParseEventArgs> OnQueryFinished;
00032 
00033         public void Connect()
00034         {
00035 
00036             try
00037             {
00038                 FVplBible = new List<CurrentContentKey>();
00039                 ResultVersKeyList = new List<CurrentContentKey>();
00040                 BibleParser.OnSingleScriptureParsed += new EventHandler<ScriptureEventArgs>(BibleParser_OnSingleScriptureParsed);
00041                 BibleParser.OnScriptureParsed += new EventHandler(BibleParser_OnScriptureParsed);
00042                 ModulConnectedEventArgs args = new ModulConnectedEventArgs();
00043                 args.ModulName = FModuleName;
00044             }
00045             catch (Exception e)
00046             {
00047 
00048                 throw e;
00049             }
00050 
00051         }
00052 
00053         public void Connect(string PathToBibleModul)
00054         {
00055             BackGroundWorkerLoadModul = new BackgroundWorker();
00056             BackGroundWorkerLoadModul.DoWork += new DoWorkEventHandler(BackGroundWorkerLoadModul_DoWork);
00057             BackGroundWorkerLoadModul.RunWorkerAsync(PathToBibleModul);
00058         }
00059 
00060         void BackGroundWorkerLoadModul_DoWork(object sender, DoWorkEventArgs e)
00061         {
00062 
00063             try
00064             {
00065                 FVplBible = new List<CurrentContentKey>();
00066                 
00067                 if (File.Exists((e.Argument as string)))
00068                 {
00069                     FModuleName = Path.GetFileNameWithoutExtension(e.Argument as string);
00070                     FModulePath = (e.Argument as string);
00071                     var VersLines = from Versline in File.ReadAllLines((e.Argument as string)) where (Versline != "" & Versline != " ") select Versline.Trim();
00072 
00073                     if (VersLines.ElementAt(0).StartsWith("~1"))
00074                     {
00075 
00076                         foreach (var Line in VersLines)
00077                         {
00078                             if (Line.StartsWith("~1")) { continue; }
00079                             FVplBible.Add(BibleParser.GetKeyFromLine(Line));
00080                         }
00081                     }
00082 
00083                     if (VersLines.ElementAt(0).StartsWith("~2"))
00084                     {
00085                         string Book = ""; string Parts = "";
00086                         foreach (var Line in VersLines)
00087                         {
00088                             if (Line.StartsWith("~2")) { continue; }
00089 
00090                             if (Line.Length > 30)
00091                             {
00092                                 FVplBible.Add(BibleParser.GetKeyFromLine(Book + Parts + ":" + Line));
00093 
00094                             }
00095                             else
00096                             {
00097 
00098                                 if (BibleParser.GetBookNumber(Line) != Line)
00099                                 {
00100                                     Book = Line;
00101 
00102                                 }
00103                                 else
00104                                 {
00105 
00106                                     Parts = " " + Line;
00107 
00108                                 }
00109 
00110                             }
00111                         }
00112                     }
00113                    
00114                     int x = FVplBible.Count();
00115                     string bn = "0"; string cn = "0"; string vn = "0";
00116                     for (int i = x - 1; i >= 0; i--)
00117                     {
00118                         if (FVplBible.ElementAt(i).ContentType == CurrentContentKey.TypeOfContentKey.Verse)
00119                         {
00120 
00121                             bn = FVplBible.ElementAt(i).Book;
00122                             cn = FVplBible.ElementAt(i).Chapter;
00123                             vn = FVplBible.ElementAt(i).Verse;
00124                             continue;
00125 
00126                         }
00127                         if (FVplBible.ElementAt(i).ContentType == CurrentContentKey.TypeOfContentKey.Caption)
00128                         {
00129 
00130                             FVplBible.ElementAt(i).Book = bn;
00131                             FVplBible.ElementAt(i).Chapter = cn;
00132                             FVplBible.ElementAt(i).Verse = vn;
00133                             continue;
00134                         }
00135 
00136                         if (FVplBible.ElementAt(i).ContentType == CurrentContentKey.TypeOfContentKey.Remark)
00137                         {
00138 
00139                             FVplBible.ElementAt(i).Book = bn;
00140                             FVplBible.ElementAt(i).Chapter = cn;
00141                             FVplBible.ElementAt(i).Verse = vn;
00142                             continue;
00143                         }
00144 
00145                     }
00146 
00147                     if (OnConnected != null)
00148                     {
00149 
00150                         ResultVersKeyList = new List<CurrentContentKey>();
00151                         BibleParser.OnSingleScriptureParsed += new EventHandler<ScriptureEventArgs>(BibleParser_OnSingleScriptureParsed);
00152                         BibleParser.OnScriptureParsed += new EventHandler(BibleParser_OnScriptureParsed);
00153                         ModulConnectedEventArgs args = new ModulConnectedEventArgs();
00154                         args.ModulName = FModuleName;
00155                         OnConnected(this, args);
00156 
00157                     }
00158                 }
00159             }
00160             catch (Exception xe)
00161             {
00162 
00163                 throw xe;
00164             }
00165         }
00166         
00167 
00168         void BibleParser_OnScriptureParsed(object sender, EventArgs e)
00169         {
00170             if (OnQueryFinished != null)
00171             {
00172                 ParseEventArgs PEV = new ParseEventArgs();
00173                 PEV.QueryResultList = ResultVersKeyList;
00174                 OnQueryFinished(this, PEV);
00175 
00176             }
00177         }
00178 
00179         void BibleParser_OnSingleScriptureParsed(object sender, ScriptureEventArgs e)
00180         {
00181 
00182 
00183             if (e.VerseNumber != "0")
00184             {
00185                 // ein einzelner Vers wurde angefordert
00186                 var Verse = from b in FVplBible where (b.Book == e.BookNumber & b.Chapter == e.ChapterNumber & b.Verse == e.VerseNumber) select b;
00187                 if (Verse.Count() > 0)
00188                 {
00189                     ResultVersKeyList.Add(DoFilter(Verse.ElementAt(0)));
00190                 }
00191 
00192             }
00193             else
00194             {
00195                 // ein Kapitel wurde angefordert
00196                 var Verse = from b in FVplBible where (b.Book == e.BookNumber & b.Chapter == e.ChapterNumber) select b;
00197                 foreach (var vers in Verse)
00198                 {
00199 
00200                     ResultVersKeyList.Add(DoFilter(vers));
00201 
00202                 }
00203             }
00204         }
00205 
00206         public bool HasVersRef(string SingleVersRef)
00207         {
00208             throw new NotImplementedException();
00209         }
00210 
00211         public void ParseQuery(string BibleQuery)
00212         {
00213             try
00214             {
00215                 ResultVersKeyList.Clear();
00216                 BibleParser.ParseScripture(BibleQuery);
00217             }
00218             catch (Exception)
00219             {
00220 
00221                 throw;
00222             }
00223         }
00224 
00225         public void NextVerse()
00226         {
00227             try
00228             {
00229                 CurrentContentKey tmp = new CurrentContentKey();
00230                 CurrentContentKey currentContentKey = ResultVersKeyList.Last();
00231                 int KeyIndex = FVplBible.IndexOf(currentContentKey);
00232                 var vl = FVplBible.Where((v, i) => i > KeyIndex & v.ContentType == CurrentContentKey.TypeOfContentKey.Verse);
00233 
00234                 if (vl.Count() > 0)
00235                 {
00236 
00237                     ResultVersKeyList.Clear();
00238                     ResultVersKeyList.Add(vl.First());
00239 
00240                 }
00241 
00242                 if (OnQueryFinished != null & ResultVersKeyList.Count() > 0)
00243                 {
00244 
00245                     ParseEventArgs PEV = new ParseEventArgs();
00246                     PEV.QueryResultList = ResultVersKeyList;
00247                     OnQueryFinished(this, PEV);
00248 
00249                 }
00250 
00251             }
00252             catch (Exception e)
00253             {
00254 
00255                 throw e;
00256             }
00257         }
00258 
00259         public void PreviousVerse()
00260         {
00261             try
00262             {
00263                 CurrentContentKey tmp = new CurrentContentKey();
00264                 CurrentContentKey currentContentKey = ResultVersKeyList.Last();
00265                 int KeyIndex = FVplBible.IndexOf(currentContentKey);
00266                 var vl = FVplBible.Where((v, i) => i < KeyIndex & v.ContentType == CurrentContentKey.TypeOfContentKey.Verse);
00267 
00268                 if (vl.Count() > 0)
00269                 {
00270 
00271                     ResultVersKeyList.Clear();
00272                     ResultVersKeyList.Add(vl.Last());
00273 
00274                 }
00275 
00276                 if (OnQueryFinished != null & ResultVersKeyList.Count() > 0)
00277                 {
00278 
00279                     ParseEventArgs PEV = new ParseEventArgs();
00280                     PEV.QueryResultList = ResultVersKeyList;
00281                     OnQueryFinished(this, PEV);
00282 
00283                 }
00284 
00285             }
00286             catch (Exception e)
00287             {
00288 
00289                 throw e;
00290             }
00291         }
00292 
00293         public void NextChapter()
00294         {
00295             try
00296             {
00297                 CurrentContentKey tmp = new CurrentContentKey();
00298                 CurrentContentKey currentContentKey = ResultVersKeyList.Last();
00299                 string NextBook = (int.Parse(currentContentKey.Book) + 1).ToString();
00300                 string NextChapter = (int.Parse(currentContentKey.Chapter) + 1).ToString();
00301                 var vl = from v in FVplBible where (currentContentKey.Book == v.Book & v.Chapter == NextChapter & v.ContentType == CurrentContentKey.TypeOfContentKey.Verse) select v;
00302 
00303                 if (vl.Count() > 0)
00304                 {
00305 
00306                     ResultVersKeyList.Clear();
00307                     foreach (var v in vl)
00308                     {
00309 
00310                         ResultVersKeyList.Add(v);
00311 
00312                     }
00313 
00314 
00315                 }
00316                 else
00317                 {
00318 
00319                     vl = from v in FVplBible where (v.Book == NextBook & v.ContentType == CurrentContentKey.TypeOfContentKey.Verse) select v;
00320                     var fvl = from v in vl group v by v.Chapter;
00321                     if (fvl.Count() > 0)
00322                     {
00323                         vl = fvl.First();
00324                         if (vl.Count() > 0)
00325                         {
00326 
00327                             ResultVersKeyList.Clear();
00328                             foreach (var v in vl)
00329                             {
00330 
00331                                 ResultVersKeyList.Add(v);
00332 
00333                             }
00334                         }
00335                     }
00336 
00337                 }
00338 
00339 
00340                 if (OnQueryFinished != null & ResultVersKeyList.Count() > 0)
00341                 {
00342 
00343                     ParseEventArgs PEV = new ParseEventArgs();
00344                     PEV.QueryResultList = ResultVersKeyList;
00345                     OnQueryFinished(this, PEV);
00346 
00347                 }
00348 
00349 
00350             }
00351             catch (Exception e)
00352             {
00353 
00354                 throw e;
00355             }
00356         }
00357 
00358         public void PreviousChapter()
00359         {
00360             try
00361             {
00362                 CurrentContentKey tmp = new CurrentContentKey();
00363                 CurrentContentKey currentContentKey = ResultVersKeyList.Last();
00364                 string PrevBook = (int.Parse(currentContentKey.Book) - 1).ToString();
00365                 string PrevChapter = (int.Parse(currentContentKey.Chapter) - 1).ToString();
00366                 var vl = from v in FVplBible where (currentContentKey.Book == v.Book & v.Chapter == PrevChapter & v.ContentType == CurrentContentKey.TypeOfContentKey.Verse) select v;
00367 
00368                 if (vl.Count() > 0)
00369                 {
00370 
00371                     ResultVersKeyList.Clear();
00372                     foreach (var v in vl)
00373                     {
00374 
00375                         ResultVersKeyList.Add(v);
00376 
00377                     }
00378 
00379 
00380                 }
00381                 else
00382                 {
00383 
00384                     vl = from v in FVplBible where (v.Book == PrevBook & v.ContentType == CurrentContentKey.TypeOfContentKey.Verse) select v;
00385                     var fvl = from v in vl group v by v.Chapter;
00386                     if (fvl.Count() > 0)
00387                     {
00388                         vl = fvl.Last();
00389                         if (vl.Count() > 0)
00390                         {
00391 
00392                             ResultVersKeyList.Clear();
00393                             foreach (var v in vl)
00394                             {
00395 
00396                                 ResultVersKeyList.Add(v);
00397 
00398                             }
00399                         }
00400                     }
00401 
00402                 }
00403 
00404 
00405                 if (OnQueryFinished != null & ResultVersKeyList.Count() > 0)
00406                 {
00407 
00408                     ParseEventArgs PEV = new ParseEventArgs();
00409                     PEV.QueryResultList = ResultVersKeyList;
00410                     OnQueryFinished(this, PEV);
00411 
00412                 }
00413 
00414 
00415             }
00416             catch (Exception)
00417             {
00418 
00419                 throw;
00420             }
00421         }
00422 
00423         public void NextBook()
00424         {
00425             CurrentContentKey tmp = new CurrentContentKey();
00426             CurrentContentKey currentContentKey = ResultVersKeyList.Last();
00427             string NextBook = (int.Parse(currentContentKey.Book) + 1).ToString();
00428 
00429             var vl = from v in FVplBible where (v.Book == NextBook & v.ContentType == CurrentContentKey.TypeOfContentKey.Verse) select v;
00430 
00431             var fvl = from v in vl group v by v.Chapter;
00432             if (fvl.Count() > 0)
00433             {
00434                 vl = fvl.First();
00435                 if (vl.Count() > 0)
00436                 {
00437 
00438                     ResultVersKeyList.Clear();
00439                     foreach (var v in vl)
00440                     {
00441 
00442                         ResultVersKeyList.Add(v);
00443 
00444                     }
00445                 }
00446                 if (OnQueryFinished != null)
00447                 {
00448                     ParseEventArgs PEV = new ParseEventArgs();
00449                     PEV.QueryResultList = ResultVersKeyList;
00450                     OnQueryFinished(this, PEV);
00451 
00452                 }
00453             }
00454         }
00455 
00456         public void PreviousBook()
00457         {
00458             CurrentContentKey tmp = new CurrentContentKey();
00459             CurrentContentKey currentContentKey = ResultVersKeyList.Last();
00460             string PrevBook = (int.Parse(currentContentKey.Book) - 1).ToString();
00461 
00462             var vl = from v in FVplBible where (v.Book == PrevBook & v.ContentType == CurrentContentKey.TypeOfContentKey.Verse) select v;
00463 
00464             var fvl = from v in vl group v by v.Chapter;
00465             if (fvl.Count() > 0)
00466             {
00467                 vl = fvl.First();
00468                 if (vl.Count() > 0)
00469                 {
00470 
00471                     ResultVersKeyList.Clear();
00472                     foreach (var v in vl)
00473                     {
00474 
00475                         ResultVersKeyList.Add(v);
00476 
00477                     }
00478                 }
00479                 if (OnQueryFinished != null)
00480                 {
00481                     ParseEventArgs PEV = new ParseEventArgs();
00482                     PEV.QueryResultList = ResultVersKeyList;
00483                     OnQueryFinished(this, PEV);
00484 
00485                 }
00486             }
00487         }
00488 
00489         private CurrentContentKey DoFilter(CurrentContentKey tmp)
00490         {
00491             try
00492             {
00493                 if (this.MyFilter != null & tmp.ContentText != null)
00494                 {
00495                     tmp.ContentText = this.MyFilter.PerformFilter(tmp.ContentText);
00496                 }
00497                 return tmp;
00498             }
00499             catch (Exception e)
00500             {
00501 
00502                 throw e;
00503             }
00504         }
00505         public IFilter MyFilter { get; set; }
00506         public bool IsOnEnd()
00507         {
00508             CurrentContentKey tmp = new CurrentContentKey();
00509             CurrentContentKey currentContentKey = ResultVersKeyList.Last();
00510             if (FVplBible.Last() == currentContentKey)
00511             {
00512 
00513                 return true;
00514             }
00515             else
00516             {
00517 
00518                 return false;
00519             }
00520 
00521         }
00522         public bool IsOnStart()
00523         {
00524             CurrentContentKey tmp = new CurrentContentKey();
00525             CurrentContentKey currentContentKey = ResultVersKeyList.FirstOrDefault();
00526             if (FVplBible.First() == currentContentKey)
00527             {
00528 
00529                 return true;
00530             }
00531             else
00532             {
00533 
00534                 return false;
00535             }
00536         }
00537         public string ModuleName
00538         {
00539             get
00540             {
00541                 return FModuleName;
00542             }
00543             set
00544             {
00545                 FModuleName = value;
00546             }
00547         }
00548         public string ModulePath
00549         {
00550             get { return FModulePath; }
00551         }
00552         public List<CurrentContentKey> GetAllVerses()
00553         {
00554             return FVplBible;
00555         }
00556 
00557         public void ConvertTo(IBibleModul ModulToConvert)
00558         {
00559             throw new NotImplementedException();
00560         }
00561 
00562         #endregion
00563     }
00564 }
 All Classes Namespaces Files Functions Enumerations Properties Events