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