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() |
17 |
: base(null, XMLTVConstants.Channels.RootElement) |
18 |
{ |
19 |
InternalDictionaryAddKnownProperties(); |
20 |
Id = string.Empty; |
21 |
//Number = 0; |
22 |
//CallSign = string.Empty; |
23 |
//Name = string.Empty; |
24 |
} |
25 |
public XMLTVChannel(XMLTVRuntimeInstance instance, XElement node) |
26 |
: base(instance, XMLTVConstants.Channels.RootElement) |
27 |
{ |
28 |
InternalDictionaryAddKnownProperties(); |
29 |
try |
30 |
{ |
31 |
xmltv_logger.Verbose.Debug.WriteLine("Creating Instance of XMLTVChannel"); |
32 |
Create(node); |
33 |
xmltv_logger.Verbose.Debug.WriteLine("Created Instance of XMLTVChannel"); |
34 |
UpdateInstance(); |
35 |
} |
36 |
catch (IOException ex) { Debug.WriteLine(ex.ToString()); } |
37 |
} |
38 |
private void InternalDictionaryAddKnownProperties() |
39 |
{ |
40 |
MetaData = new PropertyList(); |
41 |
//MetaData.AddProperty("Id", string.Empty); |
42 |
} |
43 |
#region IXMLTVChannel members |
44 |
public string Id { get; private set; } |
45 |
//public int Number { get; private set; } |
46 |
//public string CallSign { get; private set; } |
47 |
//public string Name { get; private set; } |
48 |
private PropertyList _MetaData; |
49 |
public PropertyList MetaData { get { return _MetaData; } private set { _MetaData = value; } } |
50 |
#endregion |
51 |
public override string ToString() |
52 |
{ |
53 |
return string.Format("{0}", Id); |
54 |
} |
55 |
|
56 |
private void UpdateInstance() |
57 |
{ |
58 |
bool found_field = false; |
59 |
var instance_type = this.GetInstance().GetType(); |
60 |
var fields = instance_type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); |
61 |
foreach (var field in fields) |
62 |
{ |
63 |
if (field.FieldType == typeof(List<IXMLTVChannel>)) |
64 |
{ |
65 |
found_field = true; |
66 |
try |
67 |
{ |
68 |
var list = (List<IXMLTVChannel>)field.GetValue(this.GetInstance()); |
69 |
list.Add(this); |
70 |
xmltv_logger.Verbose.Debug.WriteLine("Updating instance with channel information: {0}", this.ToString()); |
71 |
field.SetValue(this.GetInstance(), list); |
72 |
break; |
73 |
} |
74 |
catch (Exception ex) |
75 |
{ |
76 |
xmltv_logger.Verbose.Error.WriteLine("Unable to update instance with channel information."); |
77 |
xmltv_logger.Verbose.Error.WriteLine(ex.ToString()); |
78 |
} |
79 |
} |
80 |
} |
81 |
if (!found_field) |
82 |
{ |
83 |
xmltv_logger.Verbose.Error.WriteLine("Unable to update instance with channel information."); |
84 |
} |
85 |
} |
86 |
|
87 |
private void Create(XElement node) |
88 |
{ |
89 |
//throw new NotImplementedException("Channel.Create(node) is not currently implemented."); |
90 |
// get the channel id |
91 |
Id = node.Attribute(XMLTVConstants.Channels.ChannelId).Value; |
92 |
xmltv_logger.Verbose.Debug.WriteLine("\tchannel_id: {0}", Id); |
93 |
var names = node.Descendants(XMLTVConstants.Channels.ChannelDisplayName).ToList(); |
94 |
//Number = Convert.ToInt32(names[1].Value); |
95 |
//xmltv_logger.Verbose.Debug.WriteLine("\tchannel_number: {0}", Number); |
96 |
//CallSign = names[2].Value; |
97 |
//xmltv_logger.Verbose.Debug.WriteLine("\tchannel_callsign: {0}", CallSign); |
98 |
//Name = names[3].Value; |
99 |
//xmltv_logger.Verbose.Debug.WriteLine("\tchannel_name: {0}", Name); |
100 |
|
101 |
foreach (var name in names) |
102 |
{ |
103 |
PropertyValuePair p = new PropertyValuePair(name.Name.ToString(), name.Value); |
104 |
MetaData.AddProperty(p.Name,p.Value); |
105 |
xmltv_logger.Verbose.Debug.WriteLine("\t{0}: {1}", p.Name, p.Value); |
106 |
} |
107 |
|
108 |
} |
109 |
} |
110 |
|
111 |
} |