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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 69 - (hide annotations) (download)
Sat Mar 9 01:36:49 2013 UTC (8 years, 4 months ago) by william
File size: 7049 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22