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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 75 - (hide annotations) (download)
Sat Mar 9 11:17:13 2013 UTC (6 years, 4 months ago) by william
File size: 6802 byte(s)

1 william 73 using System;
2     using System.Collections.Generic;
3     using System.Linq;
4     using System.Text;
5     using libxmltv.Interfaces;
6 william 74 using System.Xml.Linq;
7     using System.Reflection;
8 william 73
9     namespace libxmltv.Core
10     {
11     [Serializable]
12 william 74 internal class XMLTVProgram : XMLTVBase<XMLTVRuntimeInstance>, IXMLTVProgram
13 william 73 {
14 william 74 public XMLTVProgram() : base(null,XMLTVConstants.PROGRAM_ELEMENT)
15 william 73 {
16     Id = 0;
17     Start = new DateTime();
18     Stop = new DateTime();
19     Channel = new XMLTVChannel();
20     Title = string.Empty;
21     SubTitle = string.Empty;
22     Description = string.Empty;
23     }
24 william 74 public XMLTVProgram(XMLTVRuntimeInstance instance, XElement node)
25     : base(instance, XMLTVConstants.PROGRAM_ELEMENT)
26     {
27     xmltv_logger.Verbose.Debug.WriteLine("Creating Instance of XMLTVProgram");
28 william 75 Create(node);
29 william 74 xmltv_logger.Verbose.Debug.WriteLine("Created Instance of XMLTVProgram");
30 william 75 UpdateInstance();
31 william 74 }
32 william 73 #region IXMLTVProgram members
33     public int Id { get; set; }
34     public DateTime Start { get; set; }
35     public DateTime Stop { get; set; }
36     public IXMLTVChannel Channel { get; set; }
37     public string Title { get; set; }
38     public string SubTitle { get; set; }
39     public string Description { get; set; }
40     #endregion
41     public override string ToString()
42     {
43     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"));
44     }
45 william 74
46     private void UpdateInstance()
47     {
48     bool found_field = false;
49     var instance_type = this.GetInstance().GetType();
50     var fields = instance_type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
51     foreach (var field in fields)
52     {
53     if (field.FieldType == typeof(List<IXMLTVChannel>))
54     {
55     found_field = true;
56     try
57     {
58     xmltv_logger.Verbose.Debug.WriteLine("Updating instance with program information: {0}", this.ToString());
59     //field.SetValue(this.GetInstance(), new List<IXMLTVChannel>() { this });
60    
61     var list = (List<IXMLTVProgram>)field.GetValue(this.GetInstance());
62 william 75 this.Id = list.Count + 1;
63 william 74 list.Add(this);
64     field.SetValue(this.GetInstance(), list);
65     break;
66     }
67     catch (Exception ex)
68     {
69     xmltv_logger.Verbose.Error.WriteLine("Unable to update instance with program information.");
70     xmltv_logger.Verbose.Error.WriteLine(ex.ToString());
71     }
72     }
73     }
74     if (!found_field)
75     {
76     xmltv_logger.Verbose.Error.WriteLine("Unable to update instance with program information.");
77     }
78     }
79 william 75
80     private void Create(XElement node)
81     {
82     if (node.HasAttributes)
83     {
84     var start = node.Attribute(XMLTVConstants.Programs.ProgramStart);
85     this.Start = start == null ? new DateTime() : ParseDate(start.Value);
86     if (!this.Start.Equals(new DateTime())) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_start: {0}", start); }
87    
88     var stop = node.Attribute(XMLTVConstants.Programs.ProgramStop);
89     this.Stop = stop == null ? new DateTime() : ParseDate(stop.Value);
90     if (!this.Stop.Equals(new DateTime())) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_stop: {0}", stop); }
91    
92     var channelid = node.Attribute(XMLTVConstants.Programs.ProgramChannelId);
93     if (!string.IsNullOrEmpty(this.Description)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_channelid: {0}", channelid); }
94     IXMLTVChannel channel = new XMLTVChannel();
95     string _channelid = channelid == null ? string.Empty : channelid.Value;
96     this.Channel = this.GetInstance().Channels.Find(m => m.Id == _channelid);
97     if (this.Channel == null) { this.Channel = new XMLTVChannel(); }
98     if (!string.IsNullOrEmpty(_channelid)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_channel: {0}", this.Channel.ToString()); }
99     }
100     try
101     {
102     var title = node.Descendants(XMLTVConstants.Programs.ProgramTitle).FirstOrDefault();
103     this.Title = title == null ? string.Empty : title.Value;
104     }
105     catch (Exception) { this.Title = string.Empty; }
106     if (!string.IsNullOrEmpty(this.Title)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_title: {0}", this.Title == string.Empty ? "empty" : this.Title); }
107     try
108     {
109     var subtitle = node.Descendants(XMLTVConstants.Programs.ProgramSubTitle).FirstOrDefault();
110     this.SubTitle = subtitle == null ? string.Empty : subtitle.Value;
111     }
112     catch (Exception) { this.SubTitle = string.Empty; }
113     if (!string.IsNullOrEmpty(this.SubTitle)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_subtitle: {0}", this.SubTitle == string.Empty ? "empty" : this.SubTitle); }
114     try
115     {
116     var description = node.Descendants(XMLTVConstants.Programs.ProgramDescription).FirstOrDefault();
117     this.Description = description == null ? string.Empty : description.Value; ;
118     }
119     catch (Exception) { this.Description = string.Empty; }
120     if (!string.IsNullOrEmpty(this.Description)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_description: {0}", this.Description == string.Empty ? "empty" : this.Description); }
121    
122     //entries.Add(program.Id, program);
123     //program_index++;
124     //progress = 100.0 * (program_index / program_count);
125     //xmltv_logger.ReportProgress(this, new Enterprise.Logging.ReportProgressEventArgs((int)progress));
126     //Application.DoEvents();
127     }
128     private DateTime ParseDate(string timeStamp)
129     {
130     DateTime dt = new DateTime();
131     try
132     {
133     dt = DateTime.ParseExact(timeStamp, "yyyyMMddHHmmss zzzz", System.Globalization.CultureInfo.CurrentCulture);
134     }
135     catch (Exception ex) { throw ex; }
136     return dt;
137     }
138 william 73 }
139     }

  ViewVC Help
Powered by ViewVC 1.1.22