/[xmltv_parser]/trunk/libxmltv/Core/XMLTVProgramCollection.cs
ViewVC logotype

Contents of /trunk/libxmltv/Core/XMLTVProgramCollection.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (show annotations) (download)
Thu Mar 7 13:46:39 2013 UTC (7 years, 4 months ago) by william
File size: 5858 byte(s)
+ add support to save and read serialized Program/Channel data

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using libxmltv.Interfaces;
6 using System.Xml.Linq;
7 using System.Globalization;
8
9 namespace libxmltv.Core
10 {
11 internal class XMLTVProgramCollection
12 {
13 private Dictionary<int, IXMLTVProgram> entries = new Dictionary<int, IXMLTVProgram>();
14 public XMLTVProgramCollection(object xmltv)
15 {
16 XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("Creating Instance of XMLTVProgramCollection");
17 IXMLTV_PARSER _xmltv;
18 if (!Internals.VerifyInstance<IXMLTV_PARSER>(xmltv, out _xmltv)) { return; }
19 XMLTV_PARSER = _xmltv;
20 Create();
21 }
22
23 #region IXMLTVSource
24 private IXMLTV_PARSER XMLTV_PARSER { get; set; }
25 public Dictionary<int, IXMLTVProgram> Collection
26 {
27 get { return entries; }
28 }
29 #endregion
30
31 //
32 private DateTime ParseDate(string timeStamp)
33 {
34 DateTime dt = new DateTime();
35 try
36 {
37 dt = DateTime.ParseExact(timeStamp, "yyyyMMddHHmmss zzzz", System.Globalization.CultureInfo.CurrentCulture);
38 }
39 catch (Exception ex) { throw ex; }
40 return dt;
41 }
42 private void Create()
43 {
44 var doc = XMLTV_PARSER.XMLTV_LOADER.XmlDoc;
45 int index = 0;
46 foreach (var c in doc.Descendants(XMLTV_CONSTANTS.PROGRAM_ELEMENT))
47 {
48 Program program = new Program();
49
50 program.Id = index;
51 //XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("program_Id: {0}", program.Id);
52 if (c.HasAttributes)
53 {
54 var start = c.Attribute(XMLTV_CONSTANTS.Programs.ProgramStart).Value;
55 program.Start = ParseDate(start);
56 //XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_start: {0}", start);
57 var stop = c.Attribute(XMLTV_CONSTANTS.Programs.ProgramStop).Value;
58 program.Stop = ParseDate(stop);
59 //XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_stop: {0}", stop);
60 var channelid = c.Attribute(XMLTV_CONSTANTS.Programs.ProgramChannelId).Value;
61 //XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_channelid: {0}", channelid);
62 IXMLTVChannel channel = new Channel();
63 try
64 {
65 channel = XMLTV_PARSER.Channels[channelid];
66
67 }
68 catch (KeyNotFoundException)
69 {
70 XMLTV_LOGGER.Log.Verbose.Error.WriteLine(string.Format("Unable to find Channel by id: '{0}' for this program.", channelid));
71 }
72 program.Channel = channel;
73 //XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_channel: {0}", program.Channel.ToString());
74 }
75 try
76 {
77 var title = c.Descendants(XMLTV_CONSTANTS.Programs.ProgramTitle).FirstOrDefault().Value;
78 program.Title = title;
79 }
80 catch (Exception) { program.Title = string.Empty; }
81 //XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_title: {0}", program.Title == string.Empty ? "empty" : program.Title);
82 try
83 {
84 var subtitle = c.Descendants(XMLTV_CONSTANTS.Programs.ProgramSubTitle).FirstOrDefault().Value;
85 program.SubTitle = subtitle;
86 }
87 catch (Exception) { program.SubTitle = string.Empty; }
88 //XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_subtitle: {0}", program.SubTitle == string.Empty ? "empty" : program.SubTitle);
89 try
90 {
91 var description = c.Descendants(XMLTV_CONSTANTS.Programs.ProgramDescription).FirstOrDefault().Value;
92 program.Description = description;
93 }
94 catch (Exception) { program.Description = string.Empty; }
95 //XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_description: {0}", program.Description == string.Empty ? "empty" : program.Description);
96
97 entries.Add(program.Id, program);
98 index++;
99 }
100 }
101 public override string ToString()
102 {
103 //return string.Format("XmlTv Source: '{0}' (Generated by: '{1}') (support: '{2}')", SourceName, GeneratorName, GeneratorUrl);
104 return string.Empty;
105 }
106 }
107 [Serializable]
108 internal class Program : IXMLTVProgram
109 {
110 public Program()
111 {
112 Id = 0;
113 Start = new DateTime();
114 Stop = new DateTime();
115 Channel = new Channel();
116 Title = string.Empty;
117 SubTitle = string.Empty;
118 Description = string.Empty;
119 }
120 #region IXMLTVProgram members
121 public int Id { get; set; }
122 public DateTime Start { get; set; }
123 public DateTime Stop { get; set; }
124 public IXMLTVChannel Channel { get; set; }
125 public string Title { get; set; }
126 public string SubTitle { get; set; }
127 public string Description { get; set; }
128 #endregion
129 public override string ToString()
130 {
131 return string.Format("{0}: {1} - {2} ({3}) ['{4}' <==> '{5}']", Id, Title, SubTitle, Channel.ToString(), Start.ToString("yyyy/MM/dd hh:mm tt"), Stop.ToString("yyyy/MM/dd hh:mm tt"));
132 }
133 }
134 }

  ViewVC Help
Powered by ViewVC 1.1.22