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

  ViewVC Help
Powered by ViewVC 1.1.22