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 |
|
11 |
namespace libxmltv.Core |
12 |
{ |
13 |
[Serializable] |
14 |
internal class XMLTVChannel : XMLTVBase<XMLTVRuntimeInstance>, IXMLTVChannel |
15 |
{ |
16 |
public XMLTVChannel() : base(null,XMLTVConstants.CHANNEL_ELEMENT) |
17 |
{ |
18 |
Id = string.Empty; |
19 |
Number = 0; |
20 |
CallSign = string.Empty; |
21 |
Name = string.Empty; |
22 |
} |
23 |
public XMLTVChannel(XMLTVRuntimeInstance instance, XElement node) |
24 |
: base(instance, XMLTVConstants.CHANNEL_ELEMENT) |
25 |
{ |
26 |
try |
27 |
{ |
28 |
xmltv_logger.Verbose.Debug.WriteLine("Creating Instance of XMLTVChannel"); |
29 |
Create(node); |
30 |
xmltv_logger.Verbose.Debug.WriteLine("Created Instance of XMLTVChannel"); |
31 |
UpdateInstance(); |
32 |
} |
33 |
catch (IOException ex) { Debug.WriteLine(ex.ToString()); } |
34 |
} |
35 |
#region IXMLTVChannel members |
36 |
public string Id { get; private set; } |
37 |
public int Number { get; private set; } |
38 |
public string CallSign { get; private set; } |
39 |
public string Name { get; private set; } |
40 |
#endregion |
41 |
public override string ToString() |
42 |
{ |
43 |
return string.Format("{0}: {1} {2} ({3})", Id, Number, Name, CallSign); |
44 |
} |
45 |
|
46 |
private void UpdateInstance() |
47 |
{ |
48 |
bool found_field = false; |
49 |
var instance_type = this.GetInstance().GetType(); |
50 |
var fields = instance_type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); |
51 |
foreach (var field in fields) |
52 |
{ |
53 |
if (field.FieldType == typeof(List<IXMLTVChannel>)) |
54 |
{ |
55 |
found_field = true; |
56 |
try |
57 |
{ |
58 |
var list = (List<IXMLTVChannel>)field.GetValue(this.GetInstance()); |
59 |
list.Add(this); |
60 |
xmltv_logger.Verbose.Debug.WriteLine("Updating instance with channel information: {0}", this.ToString()); |
61 |
field.SetValue(this.GetInstance(), list); |
62 |
break; |
63 |
} |
64 |
catch (Exception ex) |
65 |
{ |
66 |
xmltv_logger.Verbose.Error.WriteLine("Unable to update instance with channel information."); |
67 |
xmltv_logger.Verbose.Error.WriteLine(ex.ToString()); |
68 |
} |
69 |
} |
70 |
} |
71 |
if (!found_field) |
72 |
{ |
73 |
xmltv_logger.Verbose.Error.WriteLine("Unable to update instance with channel information."); |
74 |
} |
75 |
} |
76 |
|
77 |
private void Create(XElement node) |
78 |
{ |
79 |
// get the channel id |
80 |
Id = node.Attribute(XMLTVConstants.Channels.ChannelId).Value; |
81 |
xmltv_logger.Verbose.Debug.WriteLine("\tchannel_id: {0}", Id); |
82 |
var names = node.Descendants(XMLTVConstants.Channels.ChannelDisplayName).ToList(); |
83 |
Number = Convert.ToInt32(names[1].Value); |
84 |
xmltv_logger.Verbose.Debug.WriteLine("\tchannel_number: {0}", Number); |
85 |
CallSign = names[2].Value; |
86 |
xmltv_logger.Verbose.Debug.WriteLine("\tchannel_callsign: {0}", CallSign); |
87 |
Name = names[3].Value; |
88 |
xmltv_logger.Verbose.Debug.WriteLine("\tchannel_name: {0}", Name); |
89 |
} |
90 |
} |
91 |
|
92 |
} |