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

  ViewVC Help
Powered by ViewVC 1.1.22