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