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