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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 74 - (hide annotations) (download)
Sat Mar 9 11:06:39 2013 UTC (7 years, 7 months ago) by william
File size: 4607 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22