/[xmltv_parser]/branches/linux/xmltv_parser/libxmltv/Core/XMLTVProgram.cs
ViewVC logotype

Annotation of /branches/linux/xmltv_parser/libxmltv/Core/XMLTVProgram.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 90 - (hide annotations) (download)
Sat Mar 9 17:42:34 2013 UTC (6 years, 3 months ago) by william
Original Path: trunk/libxmltv/Core/XMLTVProgram.cs
File size: 15532 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 86 using System.Globalization;
11 william 73
12     namespace libxmltv.Core
13     {
14     [Serializable]
15 william 74 internal class XMLTVProgram : XMLTVBase<XMLTVRuntimeInstance>, IXMLTVProgram
16 william 73 {
17 william 88 public XMLTVProgram()
18     : base(null, XMLTVConstants.Programs.RootElement)
19 william 73 {
20 william 86 InternalDictionaryAddKnownProperties();
21    
22 william 73 }
23 william 74 public XMLTVProgram(XMLTVRuntimeInstance instance, XElement node)
24 william 88 : base(instance, XMLTVConstants.Programs.RootElement)
25 william 74 {
26 william 86 InternalDictionaryAddKnownProperties();
27 william 78 try {
28 william 74 xmltv_logger.Verbose.Debug.WriteLine("Creating Instance of XMLTVProgram");
29 william 75 Create(node);
30 william 74 xmltv_logger.Verbose.Debug.WriteLine("Created Instance of XMLTVProgram");
31 william 75 UpdateInstance();
32 william 78 }
33     catch (IOException ex) { Debug.WriteLine(ex.ToString()); }
34 william 74 }
35 william 73 #region IXMLTVProgram members
36 william 86 //public int Id { get; set; }
37     //public DateTime Start { get; set; }
38     //public DateTime Stop { get; set; }
39     //public IXMLTVChannel Channel { get; set; }
40     //public string Title { get; set; }
41     //public string SubTitle { get; set; }
42     //public string Description { get; set; }
43    
44     private void InternalDictionaryAddKnownProperties()
45     {
46 william 90 Properties = new PropertyDictionary();
47     Properties.AddProperty("Id", 0);
48     Properties.AddProperty(XMLTVConstants.Programs.ProgramStart, new DateTime());
49     Properties.AddProperty(XMLTVConstants.Programs.ProgramStop, new DateTime());
50     Properties.AddProperty(XMLTVConstants.Programs.ProgramChannelId, string.Empty);
51     Properties.AddProperty(XMLTVConstants.Programs.ProgramTitle, string.Empty);
52     Properties.AddProperty(XMLTVConstants.Programs.ProgramSubTitle, string.Empty);
53     Properties.AddProperty(XMLTVConstants.Programs.ProgramDescription, string.Empty);
54 william 86 }
55    
56 william 90 #region Property Dictionary Support
57     public PropertyDictionary Properties { get; private set; }
58     #endregion
59 william 86
60 william 73 #endregion
61     public override string ToString()
62     {
63 william 86 return string.Format("{0}: {1} - {2} ({3}) ['{4}' <==> '{5}']",
64 william 90 Properties["Id"].ToString(),
65     Properties[(XMLTVConstants.Programs.ProgramTitle)].ToString(),
66     Properties[(XMLTVConstants.Programs.ProgramSubTitle)].ToString(),
67     Properties[(XMLTVConstants.Programs.ProgramChannelId)].ToString(),
68     ((DateTime) Properties[XMLTVConstants.Programs.ProgramStart]).ToString("yyyy/MM/dd hh:mm tt"),
69     ((DateTime) Properties[XMLTVConstants.Programs.ProgramStop]).ToString("yyyy/MM/dd hh:mm tt"));
70 william 73 }
71 william 74
72 william 86
73    
74    
75 william 74 private void UpdateInstance()
76     {
77     bool found_field = false;
78     var instance_type = this.GetInstance().GetType();
79     var fields = instance_type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
80     foreach (var field in fields)
81     {
82 william 76 if (field.FieldType == typeof(List<IXMLTVProgram>))
83 william 74 {
84     found_field = true;
85     try
86     {
87 william 77
88 william 74 var list = (List<IXMLTVProgram>)field.GetValue(this.GetInstance());
89 william 90 Properties["Id"] = list.Count + 1;
90 william 74 list.Add(this);
91 william 77 xmltv_logger.Verbose.Debug.WriteLine("Updating instance with program information: {0}", this.ToString());
92 william 74 field.SetValue(this.GetInstance(), list);
93     break;
94     }
95     catch (Exception ex)
96     {
97     xmltv_logger.Verbose.Error.WriteLine("Unable to update instance with program information.");
98     xmltv_logger.Verbose.Error.WriteLine(ex.ToString());
99     }
100     }
101     }
102     if (!found_field)
103     {
104     xmltv_logger.Verbose.Error.WriteLine("Unable to update instance with program information.");
105     }
106     }
107 william 75
108     private void Create(XElement node)
109     {
110 william 86 //if (node.HasAttributes)
111     //{
112     // var start = node.Attribute(XMLTVConstants.Programs.ProgramStart);
113     // this.Start = start == null ? new DateTime() : ParseDate(start.Value);
114     // if (!this.Start.Equals(new DateTime())) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_start: {0}", start); }
115 william 75
116 william 86 // var stop = node.Attribute(XMLTVConstants.Programs.ProgramStop);
117     // this.Stop = stop == null ? new DateTime() : ParseDate(stop.Value);
118     // if (!this.Stop.Equals(new DateTime())) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_stop: {0}", stop); }
119 william 75
120 william 86 // var channelid = node.Attribute(XMLTVConstants.Programs.ProgramChannelId);
121 william 88 // if (!string.IsNullOrEmpty(this.channelid)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_channelid: {0}", channelid); }
122 william 86 // IXMLTVChannel channel = new XMLTVChannel();
123     // string _channelid = channelid == null ? string.Empty : channelid.Value;
124     // this.Channel = this.GetInstance().Channels.Find(m => m.Id == _channelid);
125     // if (this.Channel == null) { this.Channel = new XMLTVChannel(); }
126     // if (!string.IsNullOrEmpty(_channelid)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_channel: {0}", this.Channel.ToString()); } //}
127     //try
128     //{
129     // var title = node.Descendants(XMLTVConstants.Programs.ProgramTitle).FirstOrDefault();
130     // this.Title = title == null ? string.Empty : title.Value;
131     //}
132     //catch (Exception) { this.Title = string.Empty; }
133     //if (!string.IsNullOrEmpty(this.Title)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_title: {0}", this.Title == string.Empty ? "empty" : this.Title); }
134     //try
135     //{
136     // var subtitle = node.Descendants(XMLTVConstants.Programs.ProgramSubTitle).FirstOrDefault();
137     // this.SubTitle = subtitle == null ? string.Empty : subtitle.Value;
138     //}
139     //catch (Exception) { this.SubTitle = string.Empty; }
140     //if (!string.IsNullOrEmpty(this.SubTitle)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_subtitle: {0}", this.SubTitle == string.Empty ? "empty" : this.SubTitle); }
141     //try
142     //{
143     // var description = node.Descendants(XMLTVConstants.Programs.ProgramDescription).FirstOrDefault();
144     // this.Description = description == null ? string.Empty : description.Value; ;
145     //}
146     //catch (Exception) { this.Description = string.Empty; }
147     //if (!string.IsNullOrEmpty(this.Description)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_description: {0}", this.Description == string.Empty ? "empty" : this.Description); }
148    
149     //ParseExtraData(node);
150    
151 william 87 var nodes = node.DescendantsAndSelf().ToList();
152 william 86 foreach (var sub_node in nodes)
153 william 75 {
154 william 86 if (this.GetInstance().IsAborting)
155     {
156     break;
157     }
158     CreateHandlerForProgramMetaDataNode(sub_node);
159 william 75 }
160     }
161 william 87 private static DateTime ParseDate(string timeStamp)
162 william 75 {
163     DateTime dt = new DateTime();
164     try
165     {
166     dt = DateTime.ParseExact(timeStamp, "yyyyMMddHHmmss zzzz", System.Globalization.CultureInfo.CurrentCulture);
167     }
168     catch (Exception ex) { throw ex; }
169     return dt;
170     }
171 william 86
172     private void CreateHandlerForProgramMetaDataNode(XElement node)
173     {
174 william 87
175 william 86 Type t = this.GetType();
176     Assembly asm = t.Assembly;
177     var types = asm.GetTypes();
178     var classes = types.ToList().FindAll(
179     m =>
180     m.DeclaringType == t &&
181     m.IsClass &&
182     !m.IsSealed
183     );
184     classes.TrimExcess();
185    
186     object raw_instance = null;
187     BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
188     CultureInfo culture = CultureInfo.CurrentCulture;
189     Type handler_type = null;
190     foreach (var type in classes)
191     {
192     if (type.BaseType != null && type.BaseType == typeof(XMLTVBase<XMLTVProgram>))
193     {
194     var iface = type.GetInterface("IXMLTVHandler", true);
195     if (iface != null)
196     {
197     var handler_prop = type.GetProperty("Handler");
198     if (handler_prop != null)
199     {
200     var ctors = type.GetConstructors(flags);
201     bool has_default_ctor = false;
202     foreach (var ctor in ctors) { if (ctor.GetParameters().Count() == 0) { has_default_ctor = true; } }
203     if (!has_default_ctor) { continue; }
204     raw_instance = Activator.CreateInstance(type, flags, null, new object[0], culture);
205     if (raw_instance != null)
206     {
207     object handler_value = handler_prop.GetValue(raw_instance, null);
208     if (handler_value != null && handler_value.ToString() == node.Name.ToString())
209     {
210     handler_type = type;
211     break;
212     }
213     }
214     }
215     }
216     }
217     }
218     if (handler_type == null)
219     {
220     StringBuilder node_builder = new StringBuilder();
221     node_builder.AppendFormat("<{0} ", node.Name);
222     if (node.HasAttributes) { foreach (var attribute in node.Attributes()) { node_builder.AppendFormat("{0}=\"{1}\" ", attribute.Name, attribute.Value); } }
223     string node_text = string.Format("{0}>", node_builder.ToString().TrimEnd(new char[] { ' ' }));
224     xmltv_logger.Verbose.Warn.WriteLine("Ignoring unhandled extra meta-data: {0}", node_text);
225     }
226 william 87 else
227     {
228     raw_instance = Activator.CreateInstance(handler_type, flags, null, new object[] { this, node }, culture);
229     }
230 william 86 }
231    
232    
233    
234     #region sub-classes
235 william 88 #region program title
236 william 86 private class title : XMLTVBase<XMLTVProgram>
237     {
238     public title() : base(null, XMLTVConstants.Programs.ProgramTitle) { }
239     public title(XMLTVProgram instance, XElement node)
240     : base(instance, XMLTVConstants.Programs.ProgramTitle)
241 william 87 {
242 william 88 if(node == null){throw new NullReferenceException("The node instance was null");}
243 william 87 if (node.Value != null)
244     {
245 william 90 instance.Properties.AddProperty(XMLTVConstants.Programs.ProgramTitle, node.Value);
246 william 88 xmltv_logger.Verbose.Debug.WriteLine("\tprogram_title: {0}", node.Value);
247     }
248     }
249     }
250     #endregion
251     #region program stop/start/channel (programme)
252     private class programme : XMLTVBase<XMLTVProgram>
253     {
254     public programme() : base(null, XMLTVConstants.Programs.RootElement) { }
255     public programme(XMLTVProgram instance, XElement node)
256     : base(instance, XMLTVConstants.Programs.RootElement)
257     {
258     if (node == null) { throw new NullReferenceException("The node instance was null"); }
259     if (node.HasAttributes)
260     {
261     var start = node.Attribute(XMLTVConstants.Programs.ProgramStart);
262     var t_start = start == null ? new DateTime() : ParseDate(start.Value);
263     if (!t_start.Equals(new DateTime())) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_start: {0}", start); }
264 william 90 instance.Properties.AddProperty(XMLTVConstants.Programs.ProgramStart, t_start);
265 william 88
266     var stop = node.Attribute(XMLTVConstants.Programs.ProgramStop);
267     var t_stop = stop == null ? new DateTime() : ParseDate(stop.Value);
268     if (!t_stop.Equals(new DateTime())) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_stop: {0}", stop); }
269 william 90 instance.Properties.AddProperty(XMLTVConstants.Programs.ProgramStop, t_stop);
270 william 88
271     var channelid = node.Attribute(XMLTVConstants.Programs.ProgramChannelId);
272     if (channelid != null)
273     {
274     if (!string.IsNullOrEmpty(channelid.Value)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_channelid: {0}", channelid.Value); }
275 william 90 instance.Properties.AddProperty(XMLTVConstants.Programs.ProgramChannelId, channelid.Value);
276 william 88 }
277    
278 william 87 }
279 william 86 }
280     }
281     #endregion
282 william 89
283     #region sub title
284     private class subtitle : XMLTVBase<XMLTVProgram>
285     {
286     public subtitle() : base(null, XMLTVConstants.Programs.ProgramSubTitle) { }
287     public subtitle(XMLTVProgram instance, XElement node)
288     : base(instance, XMLTVConstants.Programs.ProgramSubTitle)
289     {
290     if (node == null) { throw new NullReferenceException("The node instance was null"); }
291     if (node.Value != null)
292     {
293 william 90 instance.Properties.AddProperty(XMLTVConstants.Programs.ProgramSubTitle, node.Value);
294 william 89 xmltv_logger.Verbose.Debug.WriteLine("\tprogram_subtitle: {0}", node.Value);
295     }
296     }
297     }
298 william 88 #endregion
299 william 89 #region sub title
300     private class description : XMLTVBase<XMLTVProgram>
301     {
302     public description() : base(null, XMLTVConstants.Programs.ProgramDescription) { }
303     public description(XMLTVProgram instance, XElement node)
304     : base(instance, XMLTVConstants.Programs.ProgramDescription)
305     {
306     if (node == null) { throw new NullReferenceException("The node instance was null"); }
307     if (node.Value != null)
308     {
309 william 90 instance.Properties.AddProperty(XMLTVConstants.Programs.ProgramDescription, node.Value);
310 william 89 xmltv_logger.Verbose.Debug.WriteLine("\tprogram_description: {0}", node.Value);
311     }
312     }
313     }
314     #endregion
315     #endregion
316 william 73 }
317     }

  ViewVC Help
Powered by ViewVC 1.1.22