/[xmltv_parser]/trunk/libxmltv/Core/XMLTVSource.cs
ViewVC logotype

Contents of /trunk/libxmltv/Core/XMLTVSource.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 120 - (show annotations) (download)
Sun Mar 10 18:19:12 2013 UTC (7 years, 5 months ago) by william
File size: 4856 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22