/ComicTool/Model/ComicInfo.cs

# · C# · 92 lines · 82 code · 10 blank · 0 comment · 14 complexity · 2ff158b86a2d6cd649f361021724574e MD5 · raw file

  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Xml.Linq;
  5. namespace ComicTool.Model
  6. {
  7. public class ComicInfo : IComparer<Page>
  8. {
  9. public ComicInfo()
  10. {
  11. Writers = new HashSet<string>();
  12. Pages = new List<ComicInfoPage>();
  13. }
  14. public static ComicInfo Read(Stream xml)
  15. {
  16. if (xml == null)
  17. {
  18. return null;
  19. }
  20. var elements = XElement.Load(xml);
  21. ComicInfo info = new ComicInfo();
  22. info.Title = elements.Elements().SingleValue("Title");
  23. info.Writers.AddRange(elements.Elements().MultiValue("Writer"));
  24. var type = elements.Elements().SingleValue("Type");
  25. if (!type.IsNullOrEmpty())
  26. {
  27. info.ComicInfoType = type.ToEnum<ComicInfoType>().Value;
  28. }
  29. elements.Elements().Where(x => x.Name == "Pages").Single()
  30. .Elements().ForEach(page =>
  31. {
  32. info.Pages.Add(ComicInfoPage.CreatePage(page));
  33. });
  34. return info;
  35. }
  36. public string Title
  37. {
  38. get;
  39. private set;
  40. }
  41. public ICollection<string> Writers
  42. {
  43. get;
  44. private set;
  45. }
  46. public ComicInfoType ComicInfoType
  47. {
  48. get;
  49. private set;
  50. }
  51. public IList<ComicInfoPage> Pages
  52. {
  53. get;
  54. private set;
  55. }
  56. public int Compare(Page x, Page y)
  57. {
  58. var xCip = Pages.Where(cip => cip.Uri == x.Name).SingleOrDefault();
  59. var yCip = Pages.Where(cip => cip.Uri == y.Name).SingleOrDefault();
  60. if ((xCip == null) && (yCip == null))
  61. {
  62. return x.CompareTo(y);
  63. }
  64. if (xCip == null)
  65. {
  66. return -1;
  67. }
  68. if (yCip == null)
  69. {
  70. return 1;
  71. }
  72. xCip.LoadPage(x);
  73. yCip.LoadPage(y);
  74. return xCip.Index.CompareTo(yCip.Index);
  75. }
  76. }
  77. public enum ComicInfoType
  78. {
  79. FileComic,
  80. WebComic,
  81. }
  82. }