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 |
|
|
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 |
william |
87 |
//if (Properties.Count == 0) { throw new IndexOutOfRangeException("Properties collection has 0 elements."); } |
62 |
william |
86 |
} |
63 |
william |
87 |
private void InternalProperyExistsOrThrow(string propertyname) |
64 |
william |
86 |
{ |
65 |
|
|
InternalDictionaryTestOrThrow(); |
66 |
william |
87 |
//if (!ContainsProperty(propertyname)) { throw new KeyNotFoundException(string.Format("Property '{0}' does not exist.", propertyname)); } |
67 |
william |
86 |
} |
68 |
william |
87 |
|
69 |
|
|
public void RemoveProperty(string propertyname) |
70 |
william |
86 |
{ |
71 |
william |
87 |
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 |
william |
86 |
} |
80 |
william |
87 |
|
81 |
|
|
public void AddProperty(string propertyname) { AddProperty(propertyname, new object()); } |
82 |
|
|
public void AddProperty(string propertyname, object propertyvalue) |
83 |
william |
86 |
{ |
84 |
william |
87 |
if (!ContainsProperty(propertyname)) |
85 |
|
|
{ |
86 |
|
|
Properties.Add(propertyname, propertyvalue); |
87 |
|
|
} |
88 |
|
|
else |
89 |
|
|
{ |
90 |
|
|
SetProperty(propertyname, propertyvalue); |
91 |
|
|
} |
92 |
william |
86 |
} |
93 |
william |
87 |
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 |
william |
73 |
#endregion |
110 |
|
|
public override string ToString() |
111 |
|
|
{ |
112 |
william |
86 |
return string.Format("{0}: {1} - {2} ({3}) ['{4}' <==> '{5}']", |
113 |
|
|
GetProperty("Id").ToString(), |
114 |
|
|
GetProperty(XMLTVConstants.Programs.ProgramTitle).ToString(), |
115 |
william |
87 |
GetProperty(XMLTVConstants.Programs.ProgramSubTitle).ToString(), |
116 |
william |
86 |
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 |
william |
73 |
} |
120 |
william |
74 |
|
121 |
william |
86 |
|
122 |
|
|
|
123 |
|
|
|
124 |
william |
74 |
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 |
william |
76 |
if (field.FieldType == typeof(List<IXMLTVProgram>)) |
132 |
william |
74 |
{ |
133 |
|
|
found_field = true; |
134 |
|
|
try |
135 |
|
|
{ |
136 |
william |
77 |
|
137 |
william |
74 |
var list = (List<IXMLTVProgram>)field.GetValue(this.GetInstance()); |
138 |
william |
86 |
this.SetProperty("Id", list.Count + 1); |
139 |
william |
74 |
list.Add(this); |
140 |
william |
77 |
xmltv_logger.Verbose.Debug.WriteLine("Updating instance with program information: {0}", this.ToString()); |
141 |
william |
74 |
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 |
william |
75 |
|
157 |
|
|
private void Create(XElement node) |
158 |
|
|
{ |
159 |
william |
86 |
//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 |
william |
75 |
|
165 |
william |
86 |
// 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 |
william |
75 |
|
169 |
william |
86 |
// var channelid = node.Attribute(XMLTVConstants.Programs.ProgramChannelId); |
170 |
william |
88 |
// if (!string.IsNullOrEmpty(this.channelid)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_channelid: {0}", channelid); } |
171 |
william |
86 |
// 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 |
william |
87 |
var nodes = node.DescendantsAndSelf().ToList(); |
201 |
william |
86 |
foreach (var sub_node in nodes) |
202 |
william |
75 |
{ |
203 |
william |
86 |
if (this.GetInstance().IsAborting) |
204 |
|
|
{ |
205 |
|
|
break; |
206 |
|
|
} |
207 |
|
|
CreateHandlerForProgramMetaDataNode(sub_node); |
208 |
william |
75 |
} |
209 |
|
|
} |
210 |
william |
87 |
private static DateTime ParseDate(string timeStamp) |
211 |
william |
75 |
{ |
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 |
william |
86 |
|
221 |
|
|
private void CreateHandlerForProgramMetaDataNode(XElement node) |
222 |
|
|
{ |
223 |
william |
87 |
|
224 |
william |
86 |
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 |
william |
87 |
else |
276 |
|
|
{ |
277 |
|
|
raw_instance = Activator.CreateInstance(handler_type, flags, null, new object[] { this, node }, culture); |
278 |
|
|
} |
279 |
william |
86 |
} |
280 |
|
|
|
281 |
|
|
|
282 |
|
|
|
283 |
|
|
#region sub-classes |
284 |
william |
88 |
#region program title |
285 |
william |
86 |
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 |
william |
87 |
{ |
291 |
william |
88 |
if(node == null){throw new NullReferenceException("The node instance was null");} |
292 |
william |
87 |
if (node.Value != null) |
293 |
|
|
{ |
294 |
|
|
instance.AddProperty(XMLTVConstants.Programs.ProgramTitle, node.Value); |
295 |
william |
88 |
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 |
william |
87 |
} |
328 |
william |
86 |
} |
329 |
|
|
} |
330 |
|
|
#endregion |
331 |
william |
88 |
#endregion |
332 |
william |
73 |
} |
333 |
|
|
} |