/[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 44 - (show annotations) (download)
Fri Mar 8 03:36:44 2013 UTC (7 years, 4 months ago) by william
File size: 6297 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22