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

  ViewVC Help
Powered by ViewVC 1.1.22