/[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 91 - (show annotations) (download)
Sat Mar 9 17:48:02 2013 UTC (6 years, 4 months ago) by william
File size: 12552 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 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 }
55
56 #region Property Dictionary Support
57 public PropertyDictionary MetaData { 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 MetaData["Id"].ToString(),
65 MetaData[(XMLTVConstants.Programs.ProgramTitle)].ToString(),
66 MetaData[(XMLTVConstants.Programs.ProgramSubTitle)].ToString(),
67 MetaData[(XMLTVConstants.Programs.ProgramChannelId)].ToString(),
68 ((DateTime)MetaData[XMLTVConstants.Programs.ProgramStart]).ToString("yyyy/MM/dd hh:mm tt"),
69 ((DateTime)MetaData[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 MetaData["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 var nodes = node.DescendantsAndSelf().ToList();
111 foreach (var sub_node in nodes)
112 {
113 if (this.GetInstance().IsAborting)
114 {
115 break;
116 }
117 CreateHandlerForProgramMetaDataNode(sub_node);
118 }
119 }
120 private static DateTime ParseDate(string timeStamp)
121 {
122 DateTime dt = new DateTime();
123 try
124 {
125 dt = DateTime.ParseExact(timeStamp, "yyyyMMddHHmmss zzzz", System.Globalization.CultureInfo.CurrentCulture);
126 }
127 catch (Exception ex) { throw ex; }
128 return dt;
129 }
130
131 private void CreateHandlerForProgramMetaDataNode(XElement node)
132 {
133
134 Type t = this.GetType();
135 Assembly asm = t.Assembly;
136 var types = asm.GetTypes();
137 var classes = types.ToList().FindAll(
138 m =>
139 m.DeclaringType == t &&
140 m.IsClass &&
141 !m.IsSealed
142 );
143 classes.TrimExcess();
144
145 object raw_instance = null;
146 BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
147 CultureInfo culture = CultureInfo.CurrentCulture;
148 Type handler_type = null;
149 foreach (var type in classes)
150 {
151 if (type.BaseType != null && type.BaseType == typeof(XMLTVBase<XMLTVProgram>))
152 {
153 var iface = type.GetInterface("IXMLTVHandler", true);
154 if (iface != null)
155 {
156 var handler_prop = type.GetProperty("Handler");
157 if (handler_prop != null)
158 {
159 var ctors = type.GetConstructors(flags);
160 bool has_default_ctor = false;
161 foreach (var ctor in ctors) { if (ctor.GetParameters().Count() == 0) { has_default_ctor = true; } }
162 if (!has_default_ctor) { continue; }
163 raw_instance = Activator.CreateInstance(type, flags, null, new object[0], culture);
164 if (raw_instance != null)
165 {
166 object handler_value = handler_prop.GetValue(raw_instance, null);
167 if (handler_value != null && handler_value.ToString() == node.Name.ToString())
168 {
169 handler_type = type;
170 break;
171 }
172 }
173 }
174 }
175 }
176 }
177 if (handler_type == null)
178 {
179 StringBuilder node_builder = new StringBuilder();
180 node_builder.AppendFormat("<{0} ", node.Name);
181 if (node.HasAttributes) { foreach (var attribute in node.Attributes()) { node_builder.AppendFormat("{0}=\"{1}\" ", attribute.Name, attribute.Value); } }
182 string node_text = string.Format("{0}>", node_builder.ToString().TrimEnd(new char[] { ' ' }));
183 xmltv_logger.Verbose.Warn.WriteLine("Ignoring unhandled extra meta-data: {0}", node_text);
184 }
185 else
186 {
187 raw_instance = Activator.CreateInstance(handler_type, flags, null, new object[] { this, node }, culture);
188 }
189 }
190
191
192
193 #region sub-classes
194 #region program title
195 private class title : XMLTVBase<XMLTVProgram>
196 {
197 public title() : base(null, XMLTVConstants.Programs.ProgramTitle) { }
198 public title(XMLTVProgram instance, XElement node)
199 : base(instance, XMLTVConstants.Programs.ProgramTitle)
200 {
201 if(node == null){throw new NullReferenceException("The node instance was null");}
202 if (node.Value != null)
203 {
204 instance.MetaData.AddProperty(XMLTVConstants.Programs.ProgramTitle, node.Value);
205 xmltv_logger.Verbose.Debug.WriteLine("\tprogram_title: {0}", node.Value);
206 }
207 }
208 }
209 #endregion
210 #region program stop/start/channel (programme)
211 private class programme : XMLTVBase<XMLTVProgram>
212 {
213 public programme() : base(null, XMLTVConstants.Programs.RootElement) { }
214 public programme(XMLTVProgram instance, XElement node)
215 : base(instance, XMLTVConstants.Programs.RootElement)
216 {
217 if (node == null) { throw new NullReferenceException("The node instance was null"); }
218 if (node.HasAttributes)
219 {
220 var start = node.Attribute(XMLTVConstants.Programs.ProgramStart);
221 var t_start = start == null ? new DateTime() : ParseDate(start.Value);
222 if (!t_start.Equals(new DateTime())) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_start: {0}", start); }
223 instance.MetaData.AddProperty(XMLTVConstants.Programs.ProgramStart, t_start);
224
225 var stop = node.Attribute(XMLTVConstants.Programs.ProgramStop);
226 var t_stop = stop == null ? new DateTime() : ParseDate(stop.Value);
227 if (!t_stop.Equals(new DateTime())) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_stop: {0}", stop); }
228 instance.MetaData.AddProperty(XMLTVConstants.Programs.ProgramStop, t_stop);
229
230 var channelid = node.Attribute(XMLTVConstants.Programs.ProgramChannelId);
231 if (channelid != null)
232 {
233 if (!string.IsNullOrEmpty(channelid.Value)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_channelid: {0}", channelid.Value); }
234 instance.MetaData.AddProperty(XMLTVConstants.Programs.ProgramChannelId, channelid.Value);
235 }
236
237 }
238 }
239 }
240 #endregion
241
242 #region sub title
243 private class subtitle : XMLTVBase<XMLTVProgram>
244 {
245 public subtitle() : base(null, XMLTVConstants.Programs.ProgramSubTitle) { }
246 public subtitle(XMLTVProgram instance, XElement node)
247 : base(instance, XMLTVConstants.Programs.ProgramSubTitle)
248 {
249 if (node == null) { throw new NullReferenceException("The node instance was null"); }
250 if (node.Value != null)
251 {
252 instance.MetaData.AddProperty(XMLTVConstants.Programs.ProgramSubTitle, node.Value);
253 xmltv_logger.Verbose.Debug.WriteLine("\tprogram_subtitle: {0}", node.Value);
254 }
255 }
256 }
257 #endregion
258 #region sub title
259 private class description : XMLTVBase<XMLTVProgram>
260 {
261 public description() : base(null, XMLTVConstants.Programs.ProgramDescription) { }
262 public description(XMLTVProgram instance, XElement node)
263 : base(instance, XMLTVConstants.Programs.ProgramDescription)
264 {
265 if (node == null) { throw new NullReferenceException("The node instance was null"); }
266 if (node.Value != null)
267 {
268 instance.MetaData.AddProperty(XMLTVConstants.Programs.ProgramDescription, node.Value);
269 xmltv_logger.Verbose.Debug.WriteLine("\tprogram_description: {0}", node.Value);
270 }
271 }
272 }
273 #endregion
274 #endregion
275 }
276 }

  ViewVC Help
Powered by ViewVC 1.1.22