/TestLibTracker/LibTracker/XMLDataSet.cs

http://posttracker.codeplex.com · C# · 174 lines · 129 code · 40 blank · 5 comment · 26 complexity · 078baec04a9595cea0ffcf5477e2a245 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading;
  8. using System.Xml.Linq;
  9. namespace LibTracker
  10. {
  11. class XMLDataSet : IPostDataSet
  12. {
  13. public string XMLFile { get; protected set; }
  14. // ToDo ???????? ????? ?????????? ?????????? ?????? get ? set
  15. public string TrackNumber { get; protected set; }
  16. public string CountryDestanationID { get; protected set; }
  17. public XMLDataSet(string aTrackNumber, string aCountryDestanationID, string PathXml)
  18. {
  19. TrackNumber = aTrackNumber;
  20. CountryDestanationID = aCountryDestanationID;
  21. XMLFile = LibTracker.Properties.Resources.services;//PathXml;
  22. }
  23. public IEnumerable<PostServiceInfo> GetPostServices()
  24. {
  25. XDocument xDoc = XDocument.Parse(XMLFile);
  26. IEnumerable<XElement> services = xDoc.Element("trackchecker.services").Element("servlst").Elements();
  27. // ??????? ?????? ?? ????????? track number
  28. var activeServices = services
  29. .Where(e =>
  30. {
  31. string strReg = e.Attribute("mask") != null ? e.Attribute("mask").Value : String.Empty;
  32. try
  33. {
  34. MatchCollection tmpMatch = Regex.Matches(TrackNumber, strReg);
  35. return tmpMatch.Count > 0;
  36. }
  37. catch (Exception err)
  38. {
  39. Debug.WriteLine(String.Format("????? {0}: ????? GetPostServices: {1}",
  40. Thread.CurrentThread.GetHashCode(), err.Message));
  41. }
  42. return false;
  43. });
  44. // ???????? ?????? ?????? ??????? ? ???????????? ????
  45. var tmp1 = SericesInfoInList(activeServices);
  46. // ??????? ?????? ?? ????????? track number
  47. activeServices = services
  48. .Where(e =>
  49. {
  50. string countryID = e.Attribute("countryid") != null ? e.Attribute("countryid").Value : String.Empty;
  51. return countryID.Equals(CountryDestanationID);
  52. });
  53. // ???????? ?????? ?????? ??????? ? ???????????? ????
  54. var tmp2 = SericesInfoInList(activeServices);
  55. return tmp1.Concat(tmp2);
  56. }
  57. private IList<PostServiceInfo> SericesInfoInList(IEnumerable<XElement> aActiveServices)
  58. {
  59. IList<PostServiceInfo> result = new List<PostServiceInfo>();
  60. foreach (var activeService in aActiveServices)
  61. {
  62. PostServiceInfo info = new PostServiceInfo();
  63. info.Id = activeService.Attribute("id") != null ? int.Parse(activeService.Attribute("id").Value) : -1;
  64. info.CountryID = activeService.Attribute("countryid") != null ? int.Parse(activeService.Attribute("countryid").Value) : -1;
  65. info.Sid = activeService.Attribute("sid") != null ? activeService.Attribute("sid").Value : String.Empty;
  66. info.Name = activeService.Attribute("name") != null
  67. ? activeService.Attribute("name").Value
  68. : String.Empty;
  69. info.Auto = activeService.Attribute("auto") != null
  70. ? int.Parse(activeService.Attribute("auto").Value)
  71. : -1;
  72. info.Lang = activeService.Attribute("lang") != null
  73. ? activeService.Attribute("lang").Value
  74. : String.Empty;
  75. info.Mask = activeService.Attribute("mask") != null
  76. ? activeService.Attribute("mask").Value
  77. : String.Empty;
  78. info.Preparse = activeService.Attribute("preparse") != null
  79. ? activeService.Attribute("preparse").Value
  80. : String.Empty;
  81. info.Url = activeService.Attribute("url") != null
  82. ? activeService.Attribute("url").Value
  83. : String.Empty;
  84. info.ViewUrl = activeService.Attribute("viewurl") != null
  85. ? activeService.Attribute("viewurl").Value
  86. : String.Empty;
  87. info.Post = activeService.Attribute("post") != null
  88. ? int.Parse(activeService.Attribute("post").Value)
  89. : -1;
  90. info.Postflds = activeService.Attribute("postflds") != null
  91. ? activeService.Attribute("postflds").Value
  92. : String.Empty;
  93. info.Fail = activeService.Attribute("fail") != null
  94. ? activeService.Attribute("fail").Value
  95. : String.Empty;
  96. info.Done = activeService.Attribute("done") != null
  97. ? activeService.Attribute("done").Value
  98. : String.Empty;
  99. info.InfoPre = activeService.Attribute("infopre") != null
  100. ? activeService.Attribute("infopre").Value
  101. : String.Empty;
  102. info.Info = activeService.Attribute("info") != null
  103. ? activeService.Attribute("info").Value
  104. : String.Empty;
  105. info.InfoSes = activeService.Attribute("info_ses") != null
  106. ? activeService.Attribute("info_ses").Value
  107. : String.Empty;
  108. info.DateSe = activeService.Attribute("date_se") != null
  109. ? activeService.Attribute("date_se").Value
  110. : String.Empty;
  111. info.DateFmt = activeService.Attribute("date_fmt") != null
  112. ? activeService.Attribute("date_fmt").Value
  113. : String.Empty;
  114. info.DateDlm = activeService.Attribute("date_dlm") != null
  115. ? activeService.Attribute("date_dlm").Value
  116. : String.Empty;
  117. info.TimeSe = activeService.Attribute("time_se") != null
  118. ? activeService.Attribute("time_se").Value
  119. : String.Empty;
  120. info.TimeFmt = activeService.Attribute("time_fmt") != null
  121. ? activeService.Attribute("time_fmt").Value
  122. : String.Empty;
  123. info.TimeDdlm = activeService.Attribute("time_dlm") != null
  124. ? activeService.Attribute("time_dlm").Value
  125. : String.Empty;
  126. info.HDRS = activeService.Attribute("hdrs") != null
  127. ? activeService.Attribute("hdrs").Value
  128. : String.Empty;
  129. result.Add(info);
  130. }
  131. return result;
  132. }
  133. }
  134. }