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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 76 - (show annotations) (download)
Sat Mar 9 11:23:09 2013 UTC (6 years, 7 months ago) by william
File size: 6701 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.Reflection;
8
9 namespace libxmltv.Core
10 {
11 [Serializable]
12 internal class XMLTVProgram : XMLTVBase<XMLTVRuntimeInstance>, IXMLTVProgram
13 {
14 public XMLTVProgram() : base(null,XMLTVConstants.PROGRAM_ELEMENT)
15 {
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 public XMLTVProgram(XMLTVRuntimeInstance instance, XElement node)
25 : base(instance, XMLTVConstants.PROGRAM_ELEMENT)
26 {
27 xmltv_logger.Verbose.Debug.WriteLine("Creating Instance of XMLTVProgram");
28 Create(node);
29 xmltv_logger.Verbose.Debug.WriteLine("Created Instance of XMLTVProgram");
30 UpdateInstance();
31 }
32 #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
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<IXMLTVProgram>))
54 {
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 this.Id = list.Count + 1;
61 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
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 }
137 }

  ViewVC Help
Powered by ViewVC 1.1.22