/[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 78 - (show annotations) (download)
Sat Mar 9 11:36:24 2013 UTC (6 years, 4 months ago) by william
File size: 6878 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 using System.IO;
9 using System.Diagnostics;
10
11 namespace libxmltv.Core
12 {
13 [Serializable]
14 internal class XMLTVProgram : XMLTVBase<XMLTVRuntimeInstance>, IXMLTVProgram
15 {
16 public XMLTVProgram() : base(null,XMLTVConstants.PROGRAM_ELEMENT)
17 {
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 public XMLTVProgram(XMLTVRuntimeInstance instance, XElement node)
27 : base(instance, XMLTVConstants.PROGRAM_ELEMENT)
28 {
29 try {
30 xmltv_logger.Verbose.Debug.WriteLine("Creating Instance of XMLTVProgram");
31 Create(node);
32 xmltv_logger.Verbose.Debug.WriteLine("Created Instance of XMLTVProgram");
33 UpdateInstance();
34 }
35 catch (IOException ex) { Debug.WriteLine(ex.ToString()); }
36 }
37 #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
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 if (field.FieldType == typeof(List<IXMLTVProgram>))
59 {
60 found_field = true;
61 try
62 {
63
64 var list = (List<IXMLTVProgram>)field.GetValue(this.GetInstance());
65 this.Id = list.Count + 1;
66 list.Add(this);
67 xmltv_logger.Verbose.Debug.WriteLine("Updating instance with program information: {0}", this.ToString());
68 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
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 }
143 }

  ViewVC Help
Powered by ViewVC 1.1.22