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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22