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 |
128 |
internal class XMLTVSource : XMLTVBase<XMLTVRuntimeInstance>, IXMLTVSource, IDisposable //,IDataConverter<IXMLTVSource>, 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 |
128 |
//public object ConvertObjectData(object source) { return this.ConvertData(source); } |
38 |
|
|
//public IXMLTVSource ConvertData(object source) |
39 |
|
|
//{ |
40 |
|
|
// if (source.GetType() != typeof(XMLTVSource)) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, typeof(XMLTVSource).Name)); } |
41 |
|
|
// //if (type != typeof(IXMLTVSource)) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", type.Name, typeof(IXMLTVSource).Name)); } |
42 |
|
|
// IXMLTVSource t = (IXMLTVSource)source; |
43 |
|
|
// return t; |
44 |
|
|
//} |
45 |
|
|
/// <summary> |
46 |
|
|
/// |
47 |
|
|
/// </summary> |
48 |
|
|
/// <param name="type"></param> |
49 |
|
|
/// <returns></returns> |
50 |
|
|
public object CreateBindableDataSource(out Type type) |
51 |
william |
120 |
{ |
52 |
william |
128 |
type = typeof(IXMLTVSource); |
53 |
william |
121 |
object bindable = new object(); |
54 |
|
|
|
55 |
william |
122 |
//List<PropertyValuePair> list = new List<PropertyValuePair>(); |
56 |
|
|
//list.Add(new PropertyValuePair("Source Name", SourceName)); |
57 |
|
|
//list.Add(new PropertyValuePair("Generator Name", GeneratorName)); |
58 |
|
|
//list.Add(new PropertyValuePair("Generator Url", GeneratorUrl)); |
59 |
|
|
bindable = this; |
60 |
william |
121 |
return bindable; |
61 |
william |
120 |
} |
62 |
|
|
|
63 |
william |
73 |
public XMLTVSource() |
64 |
william |
88 |
: base(null, XMLTVConstants.Root.RootElement) |
65 |
william |
25 |
{ |
66 |
william |
72 |
this.SourceName = string.Empty; |
67 |
|
|
this.GeneratorName = string.Empty; |
68 |
|
|
this.GeneratorUrl = string.Empty; |
69 |
william |
44 |
} |
70 |
william |
88 |
public XMLTVSource(XMLTVRuntimeInstance instance) |
71 |
|
|
: base(instance, XMLTVConstants.Root.RootElement) |
72 |
william |
44 |
{ |
73 |
william |
78 |
try { |
74 |
william |
74 |
xmltv_logger.Verbose.Debug.WriteLine("Creating Instance of XMLTVSource"); |
75 |
william |
25 |
Create(); |
76 |
william |
74 |
xmltv_logger.Verbose.Debug.WriteLine("Created Instance of XMLTVSource '{0}'", SourceName); |
77 |
william |
73 |
UpdateInstance(); |
78 |
william |
78 |
} |
79 |
|
|
catch (IOException ex) { Debug.WriteLine(ex.ToString()); } |
80 |
william |
25 |
} |
81 |
william |
73 |
|
82 |
|
|
private void UpdateInstance() |
83 |
|
|
{ |
84 |
|
|
bool found_field = false; |
85 |
|
|
var instance_type = this.GetInstance().GetType(); |
86 |
|
|
var fields = instance_type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); |
87 |
|
|
foreach (var field in fields) |
88 |
|
|
{ |
89 |
william |
77 |
if (field.FieldType == typeof(IXMLTVSource)) |
90 |
william |
73 |
{ |
91 |
|
|
found_field = true; |
92 |
|
|
try |
93 |
|
|
{ |
94 |
william |
74 |
xmltv_logger.Verbose.Debug.WriteLine("Updating instance with source information: {0}", this.ToString()); |
95 |
william |
77 |
field.SetValue(this.GetInstance(), this); |
96 |
william |
73 |
break; |
97 |
|
|
} |
98 |
|
|
catch (Exception ex) |
99 |
|
|
{ |
100 |
william |
74 |
xmltv_logger.Verbose.Error.WriteLine("Unable to update instance with source information."); |
101 |
|
|
xmltv_logger.Verbose.Error.WriteLine(ex.ToString()); |
102 |
william |
73 |
} |
103 |
|
|
} |
104 |
|
|
} |
105 |
|
|
if (!found_field) |
106 |
|
|
{ |
107 |
william |
74 |
xmltv_logger.Verbose.Error.WriteLine("Unable to update instance with source information."); |
108 |
william |
73 |
} |
109 |
|
|
} |
110 |
|
|
|
111 |
william |
25 |
#region IXMLTVSource |
112 |
william |
36 |
//private IXMLTV_PARSER XMLTV_PARSER { get; set; } |
113 |
william |
44 |
public string SourceName { get; private set; } |
114 |
|
|
public string GeneratorName { get; private set; } |
115 |
|
|
public string GeneratorUrl { get; private set; } |
116 |
william |
25 |
#endregion |
117 |
|
|
|
118 |
|
|
private void Create() |
119 |
|
|
{ |
120 |
william |
72 |
var doc = XDocument.Parse(this.GetInstance().XmlDoc); |
121 |
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)); |
122 |
william |
55 |
//xmltv_logger.Verbose.Debug.WriteLine("\tRoot: {0}", doc.Root.Name); |
123 |
william |
25 |
var attributes = doc.Root.Attributes().ToList(); |
124 |
|
|
foreach (var attribute in attributes) |
125 |
|
|
{ |
126 |
william |
59 |
xmltv_logger.Verbose.Debug.WriteLine("\t{0}: {1}", attribute.Name, attribute.Value); |
127 |
william |
25 |
entries.Add(attribute.Name.ToString(), attribute.Value); |
128 |
|
|
} |
129 |
william |
44 |
|
130 |
|
|
SourceName = entries[XMLTVConstants.Source.SourceName]; |
131 |
|
|
GeneratorName = entries[XMLTVConstants.Source.GeneratorName]; |
132 |
|
|
GeneratorUrl = entries[XMLTVConstants.Source.GeneratorUrl]; |
133 |
|
|
|
134 |
william |
25 |
} |
135 |
|
|
public override string ToString() |
136 |
|
|
{ |
137 |
|
|
return string.Format("XmlTv Source: '{0}' (Generated by: '{1}') (support: '{2}')", SourceName, GeneratorName, GeneratorUrl); |
138 |
|
|
} |
139 |
william |
44 |
|
140 |
|
|
public void Dispose() |
141 |
|
|
{ |
142 |
|
|
//throw new NotImplementedException(); |
143 |
|
|
} |
144 |
|
|
|
145 |
william |
72 |
//public object Clone() |
146 |
|
|
//{ |
147 |
|
|
// return new XMLTVSource(this.SourceName, this.GeneratorName, this.GeneratorUrl); |
148 |
william |
120 |
//} |
149 |
william |
25 |
} |
150 |
|
|
} |