1 |
using System; |
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Text; |
5 |
using libxmltv.Interfaces; |
6 |
using System.Diagnostics; |
7 |
using System.Xml.Linq; |
8 |
using System.Reflection; |
9 |
using System.IO; |
10 |
|
11 |
namespace libxmltv.Core |
12 |
{ |
13 |
[Serializable] |
14 |
internal class XMLTVSource : XMLTVBase<XMLTVRuntimeInstance>, IXMLTVSource, IDisposable//, ICloneable |
15 |
{ |
16 |
//static private List<string> known_columns; |
17 |
//static XMLTVSource() |
18 |
//{ |
19 |
// known_columns.Add("Source Name"); |
20 |
// known_columns.Add("Generator Name"); |
21 |
// known_columns.Add("Generator Url"); |
22 |
//} |
23 |
|
24 |
private Dictionary<string, string> entries = new Dictionary<string, string>(); |
25 |
//internal static void CreateInstance(XMLTVRuntimeInstance xmltv) |
26 |
//{ |
27 |
// using (XMLTVSource g = new XMLTVSource(xmltv)) { g.instance.Source = (IXMLTVSource)g.Clone(); } |
28 |
//} |
29 |
|
30 |
//private XMLTVRuntimeInstance instance; |
31 |
//protected XMLTVSource(string sourcename, string generatorname, string generatorurl) |
32 |
//{ |
33 |
// this.SourceName = sourcename; |
34 |
// this.GeneratorName = generatorname; |
35 |
// this.GeneratorUrl = generatorurl; |
36 |
//} |
37 |
|
38 |
|
39 |
public object CreateBindableDataSource() |
40 |
{ |
41 |
object bindable = new object(); |
42 |
|
43 |
//List<PropertyValuePair> list = new List<PropertyValuePair>(); |
44 |
//list.Add(new PropertyValuePair("Source Name", SourceName)); |
45 |
//list.Add(new PropertyValuePair("Generator Name", GeneratorName)); |
46 |
//list.Add(new PropertyValuePair("Generator Url", GeneratorUrl)); |
47 |
bindable = this; |
48 |
return bindable; |
49 |
} |
50 |
|
51 |
public XMLTVSource() |
52 |
: base(null, XMLTVConstants.Root.RootElement) |
53 |
{ |
54 |
this.SourceName = string.Empty; |
55 |
this.GeneratorName = string.Empty; |
56 |
this.GeneratorUrl = string.Empty; |
57 |
} |
58 |
public XMLTVSource(XMLTVRuntimeInstance instance) |
59 |
: base(instance, XMLTVConstants.Root.RootElement) |
60 |
{ |
61 |
try { |
62 |
xmltv_logger.Verbose.Debug.WriteLine("Creating Instance of XMLTVSource"); |
63 |
Create(); |
64 |
xmltv_logger.Verbose.Debug.WriteLine("Created Instance of XMLTVSource '{0}'", SourceName); |
65 |
UpdateInstance(); |
66 |
} |
67 |
catch (IOException ex) { Debug.WriteLine(ex.ToString()); } |
68 |
} |
69 |
|
70 |
private void UpdateInstance() |
71 |
{ |
72 |
bool found_field = false; |
73 |
var instance_type = this.GetInstance().GetType(); |
74 |
var fields = instance_type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); |
75 |
foreach (var field in fields) |
76 |
{ |
77 |
if (field.FieldType == typeof(IXMLTVSource)) |
78 |
{ |
79 |
found_field = true; |
80 |
try |
81 |
{ |
82 |
xmltv_logger.Verbose.Debug.WriteLine("Updating instance with source information: {0}", this.ToString()); |
83 |
field.SetValue(this.GetInstance(), this); |
84 |
break; |
85 |
} |
86 |
catch (Exception ex) |
87 |
{ |
88 |
xmltv_logger.Verbose.Error.WriteLine("Unable to update instance with source information."); |
89 |
xmltv_logger.Verbose.Error.WriteLine(ex.ToString()); |
90 |
} |
91 |
} |
92 |
} |
93 |
if (!found_field) |
94 |
{ |
95 |
xmltv_logger.Verbose.Error.WriteLine("Unable to update instance with source information."); |
96 |
} |
97 |
} |
98 |
|
99 |
#region IXMLTVSource |
100 |
//private IXMLTV_PARSER XMLTV_PARSER { get; set; } |
101 |
public string SourceName { get; private set; } |
102 |
public string GeneratorName { get; private set; } |
103 |
public string GeneratorUrl { get; private set; } |
104 |
#endregion |
105 |
|
106 |
private void Create() |
107 |
{ |
108 |
var doc = XDocument.Parse(this.GetInstance().XmlDoc); |
109 |
Debug.Assert(doc.Root.Name == XMLTVConstants.Root.RootElement, string.Format("Expected Root Element: '{0}' but read: '{1}'", XMLTVConstants.Root.RootElement, doc.Root.Name)); |
110 |
//xmltv_logger.Verbose.Debug.WriteLine("\tRoot: {0}", doc.Root.Name); |
111 |
var attributes = doc.Root.Attributes().ToList(); |
112 |
foreach (var attribute in attributes) |
113 |
{ |
114 |
xmltv_logger.Verbose.Debug.WriteLine("\t{0}: {1}", attribute.Name, attribute.Value); |
115 |
entries.Add(attribute.Name.ToString(), attribute.Value); |
116 |
} |
117 |
|
118 |
SourceName = entries[XMLTVConstants.Source.SourceName]; |
119 |
GeneratorName = entries[XMLTVConstants.Source.GeneratorName]; |
120 |
GeneratorUrl = entries[XMLTVConstants.Source.GeneratorUrl]; |
121 |
|
122 |
} |
123 |
public override string ToString() |
124 |
{ |
125 |
return string.Format("XmlTv Source: '{0}' (Generated by: '{1}') (support: '{2}')", SourceName, GeneratorName, GeneratorUrl); |
126 |
} |
127 |
|
128 |
public void Dispose() |
129 |
{ |
130 |
//throw new NotImplementedException(); |
131 |
} |
132 |
|
133 |
//public object Clone() |
134 |
//{ |
135 |
// return new XMLTVSource(this.SourceName, this.GeneratorName, this.GeneratorUrl); |
136 |
//} |
137 |
} |
138 |
} |