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

  ViewVC Help
Powered by ViewVC 1.1.22