/[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 76 - (hide annotations) (download)
Sat Mar 9 11:23:09 2013 UTC (6 years, 6 months ago) by william
File size: 6701 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 william 76 if (field.FieldType == typeof(List<IXMLTVProgram>))
54 william 74 {
55     found_field = true;
56     try
57     {
58     xmltv_logger.Verbose.Debug.WriteLine("Updating instance with program information: {0}", this.ToString());
59     var list = (List<IXMLTVProgram>)field.GetValue(this.GetInstance());
60 william 75 this.Id = list.Count + 1;
61 william 74 list.Add(this);
62     field.SetValue(this.GetInstance(), list);
63     break;
64     }
65     catch (Exception ex)
66     {
67     xmltv_logger.Verbose.Error.WriteLine("Unable to update instance with program information.");
68     xmltv_logger.Verbose.Error.WriteLine(ex.ToString());
69     }
70     }
71     }
72     if (!found_field)
73     {
74     xmltv_logger.Verbose.Error.WriteLine("Unable to update instance with program information.");
75     }
76     }
77 william 75
78     private void Create(XElement node)
79     {
80     if (node.HasAttributes)
81     {
82     var start = node.Attribute(XMLTVConstants.Programs.ProgramStart);
83     this.Start = start == null ? new DateTime() : ParseDate(start.Value);
84     if (!this.Start.Equals(new DateTime())) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_start: {0}", start); }
85    
86     var stop = node.Attribute(XMLTVConstants.Programs.ProgramStop);
87     this.Stop = stop == null ? new DateTime() : ParseDate(stop.Value);
88     if (!this.Stop.Equals(new DateTime())) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_stop: {0}", stop); }
89    
90     var channelid = node.Attribute(XMLTVConstants.Programs.ProgramChannelId);
91     if (!string.IsNullOrEmpty(this.Description)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_channelid: {0}", channelid); }
92     IXMLTVChannel channel = new XMLTVChannel();
93     string _channelid = channelid == null ? string.Empty : channelid.Value;
94     this.Channel = this.GetInstance().Channels.Find(m => m.Id == _channelid);
95     if (this.Channel == null) { this.Channel = new XMLTVChannel(); }
96     if (!string.IsNullOrEmpty(_channelid)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_channel: {0}", this.Channel.ToString()); }
97     }
98     try
99     {
100     var title = node.Descendants(XMLTVConstants.Programs.ProgramTitle).FirstOrDefault();
101     this.Title = title == null ? string.Empty : title.Value;
102     }
103     catch (Exception) { this.Title = string.Empty; }
104     if (!string.IsNullOrEmpty(this.Title)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_title: {0}", this.Title == string.Empty ? "empty" : this.Title); }
105     try
106     {
107     var subtitle = node.Descendants(XMLTVConstants.Programs.ProgramSubTitle).FirstOrDefault();
108     this.SubTitle = subtitle == null ? string.Empty : subtitle.Value;
109     }
110     catch (Exception) { this.SubTitle = string.Empty; }
111     if (!string.IsNullOrEmpty(this.SubTitle)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_subtitle: {0}", this.SubTitle == string.Empty ? "empty" : this.SubTitle); }
112     try
113     {
114     var description = node.Descendants(XMLTVConstants.Programs.ProgramDescription).FirstOrDefault();
115     this.Description = description == null ? string.Empty : description.Value; ;
116     }
117     catch (Exception) { this.Description = string.Empty; }
118     if (!string.IsNullOrEmpty(this.Description)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_description: {0}", this.Description == string.Empty ? "empty" : this.Description); }
119    
120     //entries.Add(program.Id, program);
121     //program_index++;
122     //progress = 100.0 * (program_index / program_count);
123     //xmltv_logger.ReportProgress(this, new Enterprise.Logging.ReportProgressEventArgs((int)progress));
124     //Application.DoEvents();
125     }
126     private DateTime ParseDate(string timeStamp)
127     {
128     DateTime dt = new DateTime();
129     try
130     {
131     dt = DateTime.ParseExact(timeStamp, "yyyyMMddHHmmss zzzz", System.Globalization.CultureInfo.CurrentCulture);
132     }
133     catch (Exception ex) { throw ex; }
134     return dt;
135     }
136 william 73 }
137     }

  ViewVC Help
Powered by ViewVC 1.1.22