/[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 128 - (show annotations) (download)
Wed Mar 13 13:45:05 2013 UTC (7 years, 7 months ago) by william
File size: 6337 byte(s)
+ current wip

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

  ViewVC Help
Powered by ViewVC 1.1.22