ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/xmltv_parser/trunk/libxmltv/Core/XMLTVProgramCollection.cs
Revision: 45
Committed: Fri Mar 8 03:41:18 2013 UTC (9 years, 3 months ago) by william
File size: 6406 byte(s)
Log Message:

File Contents

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