/[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 90 - (show annotations) (download)
Sat Mar 9 17:42:34 2013 UTC (6 years, 7 months ago) by william
File size: 15532 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 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 }
55
56 #region Property Dictionary Support
57 public PropertyDictionary Properties { get; private set; }
58 #endregion
59
60 #endregion
61 public override string ToString()
62 {
63 return string.Format("{0}: {1} - {2} ({3}) ['{4}' <==> '{5}']",
64 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 }
71
72
73
74
75 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 if (field.FieldType == typeof(List<IXMLTVProgram>))
83 {
84 found_field = true;
85 try
86 {
87
88 var list = (List<IXMLTVProgram>)field.GetValue(this.GetInstance());
89 Properties["Id"] = list.Count + 1;
90 list.Add(this);
91 xmltv_logger.Verbose.Debug.WriteLine("Updating instance with program information: {0}", this.ToString());
92 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
108 private void Create(XElement node)
109 {
110 //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
116 // 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
120 // var channelid = node.Attribute(XMLTVConstants.Programs.ProgramChannelId);
121 // if (!string.IsNullOrEmpty(this.channelid)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_channelid: {0}", channelid); }
122 // 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 var nodes = node.DescendantsAndSelf().ToList();
152 foreach (var sub_node in nodes)
153 {
154 if (this.GetInstance().IsAborting)
155 {
156 break;
157 }
158 CreateHandlerForProgramMetaDataNode(sub_node);
159 }
160 }
161 private static DateTime ParseDate(string timeStamp)
162 {
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
172 private void CreateHandlerForProgramMetaDataNode(XElement node)
173 {
174
175 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 else
227 {
228 raw_instance = Activator.CreateInstance(handler_type, flags, null, new object[] { this, node }, culture);
229 }
230 }
231
232
233
234 #region sub-classes
235 #region program title
236 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 {
242 if(node == null){throw new NullReferenceException("The node instance was null");}
243 if (node.Value != null)
244 {
245 instance.Properties.AddProperty(XMLTVConstants.Programs.ProgramTitle, node.Value);
246 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 instance.Properties.AddProperty(XMLTVConstants.Programs.ProgramStart, t_start);
265
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 instance.Properties.AddProperty(XMLTVConstants.Programs.ProgramStop, t_stop);
270
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 instance.Properties.AddProperty(XMLTVConstants.Programs.ProgramChannelId, channelid.Value);
276 }
277
278 }
279 }
280 }
281 #endregion
282
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 instance.Properties.AddProperty(XMLTVConstants.Programs.ProgramSubTitle, node.Value);
294 xmltv_logger.Verbose.Debug.WriteLine("\tprogram_subtitle: {0}", node.Value);
295 }
296 }
297 }
298 #endregion
299 #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 instance.Properties.AddProperty(XMLTVConstants.Programs.ProgramDescription, node.Value);
310 xmltv_logger.Verbose.Debug.WriteLine("\tprogram_description: {0}", node.Value);
311 }
312 }
313 }
314 #endregion
315 #endregion
316 }
317 }

  ViewVC Help
Powered by ViewVC 1.1.22