/[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 227 - (hide annotations) (download)
Sun Mar 17 03:57:57 2013 UTC (6 years, 6 months ago) by william
Original Path: trunk/libxmltv/Core/XMLTVProgram.cs
File size: 15781 byte(s)
+ implement reading of rating from XMLTV data

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 91 MetaData = new PropertyDictionary();
47     MetaData.AddProperty("Id", 0);
48     MetaData.AddProperty(XMLTVConstants.Programs.ProgramStart, new DateTime());
49     MetaData.AddProperty(XMLTVConstants.Programs.ProgramStop, new DateTime());
50     MetaData.AddProperty(XMLTVConstants.Programs.ProgramChannelId, string.Empty);
51     MetaData.AddProperty(XMLTVConstants.Programs.ProgramTitle, string.Empty);
52     MetaData.AddProperty(XMLTVConstants.Programs.ProgramSubTitle, string.Empty);
53     MetaData.AddProperty(XMLTVConstants.Programs.ProgramDescription, string.Empty);
54 william 86 }
55    
56 william 90 #region Property Dictionary Support
57 william 118 private IPropertyDictionary _MetaData;
58     public IPropertyDictionary MetaData { get { return _MetaData; } private set { _MetaData = value; } }
59 william 227 public List<IExtraMetaData> GetExtraMetaData()
60 william 113 {
61 william 227 return (List<IExtraMetaData>)this.MetaData[XMLTVConstants.Programs.ProgramExtraMetaData];
62 william 113 }
63 william 90 #endregion
64 william 86
65 william 73 #endregion
66     public override string ToString()
67     {
68 william 86 return string.Format("{0}: {1} - {2} ({3}) ['{4}' <==> '{5}']",
69 william 91 MetaData["Id"].ToString(),
70 william 103 MetaData[XMLTVConstants.Programs.ProgramTitle].ToString(),
71     MetaData[XMLTVConstants.Programs.ProgramSubTitle].ToString(),
72 william 104 MetaData[XMLTVConstants.Programs.ProgramChannelId].ToString(),
73 william 141 ((DateTime)MetaData[XMLTVConstants.Programs.ProgramStart]).ToDateTimeString(),
74     ((DateTime)MetaData[XMLTVConstants.Programs.ProgramStop]).ToDateTimeString());
75 william 73 }
76 william 74
77 william 86
78    
79    
80 william 74 private void UpdateInstance()
81     {
82     bool found_field = false;
83     var instance_type = this.GetInstance().GetType();
84     var fields = instance_type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
85     foreach (var field in fields)
86     {
87 william 120 if (field.FieldType == typeof(ProgramList))
88 william 74 {
89     found_field = true;
90     try
91     {
92 william 120
93     var list = (ProgramList)field.GetValue(this.GetInstance());
94 william 91 MetaData["Id"] = list.Count + 1;
95 william 74 list.Add(this);
96 william 77 xmltv_logger.Verbose.Debug.WriteLine("Updating instance with program information: {0}", this.ToString());
97 william 74 field.SetValue(this.GetInstance(), list);
98     break;
99     }
100     catch (Exception ex)
101     {
102     xmltv_logger.Verbose.Error.WriteLine("Unable to update instance with program information.");
103     xmltv_logger.Verbose.Error.WriteLine(ex.ToString());
104     }
105     }
106     }
107     if (!found_field)
108     {
109     xmltv_logger.Verbose.Error.WriteLine("Unable to update instance with program information.");
110     }
111     }
112 william 75
113     private void Create(XElement node)
114     {
115 william 96 CreateHandlerForProgramMetaDataNode(node);
116     var nodes = node.Elements().ToList();
117 william 86 foreach (var sub_node in nodes)
118 william 75 {
119 william 86 if (this.GetInstance().IsAborting)
120     {
121     break;
122     }
123     CreateHandlerForProgramMetaDataNode(sub_node);
124 william 75 }
125     }
126 william 87 private static DateTime ParseDate(string timeStamp)
127 william 75 {
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 86
137     private void CreateHandlerForProgramMetaDataNode(XElement node)
138     {
139 william 87
140 william 86 Type t = this.GetType();
141     Assembly asm = t.Assembly;
142     var types = asm.GetTypes();
143     var classes = types.ToList().FindAll(
144     m =>
145     m.DeclaringType == t &&
146     m.IsClass &&
147     !m.IsSealed
148     );
149     classes.TrimExcess();
150    
151     object raw_instance = null;
152     BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
153     CultureInfo culture = CultureInfo.CurrentCulture;
154     Type handler_type = null;
155     foreach (var type in classes)
156     {
157     if (type.BaseType != null && type.BaseType == typeof(XMLTVBase<XMLTVProgram>))
158     {
159     var iface = type.GetInterface("IXMLTVHandler", true);
160     if (iface != null)
161     {
162     var handler_prop = type.GetProperty("Handler");
163     if (handler_prop != null)
164     {
165     var ctors = type.GetConstructors(flags);
166     bool has_default_ctor = false;
167     foreach (var ctor in ctors) { if (ctor.GetParameters().Count() == 0) { has_default_ctor = true; } }
168 william 111 ctors = null;
169 william 86 if (!has_default_ctor) { continue; }
170     raw_instance = Activator.CreateInstance(type, flags, null, new object[0], culture);
171     if (raw_instance != null)
172     {
173     object handler_value = handler_prop.GetValue(raw_instance, null);
174     if (handler_value != null && handler_value.ToString() == node.Name.ToString())
175     {
176     handler_type = type;
177 william 111 handler_value = null;
178     raw_instance = null;
179     handler_prop = null;
180     iface = null;
181 william 86 break;
182     }
183 william 111 handler_value = null;
184 william 86 }
185 william 111 raw_instance = null;
186 william 86 }
187 william 111 handler_prop = null;
188 william 86 }
189 william 111 iface = null;
190 william 86 }
191     }
192 william 111 t = null;
193     asm = null;
194     types = null;
195     classes = null;
196 william 86 if (handler_type == null)
197     {
198 william 105 try
199     {
200 william 107 raw_instance = Activator.CreateInstance(typeof(UnhandledExtraProgramMetaData), flags, null, new object[] { this, node }, culture);
201 william 105 }
202     catch (Exception ex) { throw ex; }
203    
204     if (raw_instance == null)
205     {
206     StringBuilder node_builder = new StringBuilder();
207     node_builder.AppendFormat("<{0} ", node.Name);
208     if (node.HasAttributes) { foreach (var attribute in node.Attributes()) { node_builder.AppendFormat("{0}=\"{1}\" ", attribute.Name, attribute.Value); } }
209     string node_text = string.Format("{0}>", node_builder.ToString().TrimEnd(new char[] { ' ' }));
210     xmltv_logger.Verbose.Warn.WriteLine("Ignoring unhandled extra meta-data: {0}", node_text);
211     }
212 william 86 }
213 william 87 else
214     {
215 william 105 try
216     {
217     raw_instance = Activator.CreateInstance(handler_type, flags, null, new object[] { this, node }, culture);
218     }
219     catch (Exception ex) { throw ex; }
220 william 87 }
221 william 111 raw_instance = null;
222     flags = 0;
223     culture = null;
224     node = null;
225 william 86 }
226    
227    
228    
229 william 105 #region sub-classes
230     #region programme: stop/start/channelid
231 william 88 private class programme : XMLTVBase<XMLTVProgram>
232     {
233     public programme() : base(null, XMLTVConstants.Programs.RootElement) { }
234     public programme(XMLTVProgram instance, XElement node)
235     : base(instance, XMLTVConstants.Programs.RootElement)
236     {
237     if (node == null) { throw new NullReferenceException("The node instance was null"); }
238     if (node.HasAttributes)
239     {
240     var start = node.Attribute(XMLTVConstants.Programs.ProgramStart);
241     var t_start = start == null ? new DateTime() : ParseDate(start.Value);
242 william 140 if (!t_start.Equals(new DateTime()))
243     {
244     xmltv_logger.Verbose.Debug.WriteLine("\tprogram_start: {0}", start);
245     xmltv_logger.Verbose.Debug.WriteLine("\tprogram_start dt: {0}", t_start.ToString());
246     }
247 william 91 instance.MetaData.AddProperty(XMLTVConstants.Programs.ProgramStart, t_start);
248 william 88
249     var stop = node.Attribute(XMLTVConstants.Programs.ProgramStop);
250     var t_stop = stop == null ? new DateTime() : ParseDate(stop.Value);
251 william 140 if (!t_stop.Equals(new DateTime()))
252     {
253     xmltv_logger.Verbose.Debug.WriteLine("\tprogram_stop: {0}", stop);
254     xmltv_logger.Verbose.Debug.WriteLine("\tprogram_stop dt: {0}", t_stop.ToString());
255     }
256 william 91 instance.MetaData.AddProperty(XMLTVConstants.Programs.ProgramStop, t_stop);
257 william 88
258     var channelid = node.Attribute(XMLTVConstants.Programs.ProgramChannelId);
259     if (channelid != null)
260     {
261     if (!string.IsNullOrEmpty(channelid.Value)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_channelid: {0}", channelid.Value); }
262 william 91 instance.MetaData.AddProperty(XMLTVConstants.Programs.ProgramChannelId, channelid.Value);
263 william 88 }
264    
265 william 87 }
266 william 111 node = null;
267 william 86 }
268     }
269     #endregion
270 william 105 #region title
271     private class title : XMLTVBase<XMLTVProgram>
272     {
273     public title() : base(null, XMLTVConstants.Programs.ProgramTitle) { }
274     public title(XMLTVProgram instance, XElement node)
275     : base(instance, XMLTVConstants.Programs.ProgramTitle)
276     {
277     if (node == null) { throw new NullReferenceException("The node instance was null"); }
278     if (node.Value != null)
279     {
280     instance.MetaData.AddProperty(XMLTVConstants.Programs.ProgramTitle, node.Value);
281     xmltv_logger.Verbose.Debug.WriteLine("\tprogram_title: {0}", node.Value);
282     }
283 william 111 node = null;
284 william 105 }
285     }
286     #endregion
287     #region sub-title
288 william 89 private class subtitle : XMLTVBase<XMLTVProgram>
289     {
290     public subtitle() : base(null, XMLTVConstants.Programs.ProgramSubTitle) { }
291     public subtitle(XMLTVProgram instance, XElement node)
292     : base(instance, XMLTVConstants.Programs.ProgramSubTitle)
293     {
294     if (node == null) { throw new NullReferenceException("The node instance was null"); }
295     if (node.Value != null)
296     {
297 william 91 instance.MetaData.AddProperty(XMLTVConstants.Programs.ProgramSubTitle, node.Value);
298 william 89 xmltv_logger.Verbose.Debug.WriteLine("\tprogram_subtitle: {0}", node.Value);
299     }
300 william 111 node = null;
301 william 89 }
302     }
303 william 88 #endregion
304 william 105 #region description
305 william 89 private class description : XMLTVBase<XMLTVProgram>
306     {
307     public description() : base(null, XMLTVConstants.Programs.ProgramDescription) { }
308     public description(XMLTVProgram instance, XElement node)
309     : base(instance, XMLTVConstants.Programs.ProgramDescription)
310     {
311     if (node == null) { throw new NullReferenceException("The node instance was null"); }
312     if (node.Value != null)
313     {
314 william 91 instance.MetaData.AddProperty(XMLTVConstants.Programs.ProgramDescription, node.Value);
315 william 89 xmltv_logger.Verbose.Debug.WriteLine("\tprogram_description: {0}", node.Value);
316     }
317 william 111 node = null;
318 william 89 }
319     }
320     #endregion
321 william 105
322     #region UnhandledExtraMetaData
323 william 107 private class UnhandledExtraProgramMetaData : XMLTVBase<XMLTVProgram>
324 william 105 {
325 william 107 public UnhandledExtraProgramMetaData() : base(null, null) { }
326     public UnhandledExtraProgramMetaData(XMLTVProgram instance, XElement node)
327 william 105 : base(instance, null)
328     {
329 william 106 if (node == null) { throw new NullReferenceException("The node instance was null"); }
330 william 107 xmltv_logger.Verbose.Debug.WriteLine("Parsng unhandled extra program meta-data: {0}", node.Name.ToString());
331 william 105 if (this.GetInstance() != null)
332     {
333 william 110 List<IExtraMetaData> list = new List<IExtraMetaData>();
334 william 106 if (!instance.MetaData.ContainsProperty(XMLTVConstants.Programs.ProgramExtraMetaData)) { instance.MetaData.AddProperty(XMLTVConstants.Programs.ProgramExtraMetaData, new List<ExtraMetaData>()); }
335 william 110 else { list = (List<IExtraMetaData>)instance.MetaData[XMLTVConstants.Programs.ProgramExtraMetaData]; }
336 william 106 ExtraMetaData data = new ExtraMetaData(node);
337     list.Add(data);
338     instance.MetaData[XMLTVConstants.Programs.ProgramExtraMetaData] = list;
339 william 111 data = null;
340 william 105 }
341 william 111 node = null;
342 william 105 }
343 william 107 }
344 william 89 #endregion
345 william 105 #endregion
346 william 73 }
347     }

  ViewVC Help
Powered by ViewVC 1.1.22