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

  ViewVC Help
Powered by ViewVC 1.1.22