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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/libxmltv/Core/XMLTVSource.cs	2013/03/07 13:46:39	31
+++ trunk/libxmltv/Core/XMLTVSource.cs	2013/03/09 14:52:10	88
@@ -4,44 +4,114 @@
 using System.Text;
 using libxmltv.Interfaces;
 using System.Diagnostics;
+using System.Xml.Linq;
+using System.Reflection;
+using System.IO;
 
 namespace libxmltv.Core
 {
     [Serializable]
-    internal class XMLTVSource : IXMLTVSource
+    internal class XMLTVSource : XMLTVBase<XMLTVRuntimeInstance>, IXMLTVSource, IDisposable//, ICloneable
     {
+       
         private Dictionary<string, string> entries = new Dictionary<string, string>();
-        public XMLTVSource(object xmltv)
+        //internal static void CreateInstance(XMLTVRuntimeInstance xmltv)
+        //{
+        //    using (XMLTVSource g = new XMLTVSource(xmltv)) { g.instance.Source = (IXMLTVSource)g.Clone(); }
+        //}
+
+        //private XMLTVRuntimeInstance instance;
+        //protected XMLTVSource(string sourcename, string generatorname, string generatorurl)
+        //{
+        //    this.SourceName = sourcename;
+        //    this.GeneratorName = generatorname;
+        //    this.GeneratorUrl = generatorurl;
+        //}
+        public XMLTVSource()
+            : base(null, XMLTVConstants.Root.RootElement) 
         {
-            XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("Creating Instance of XMLTVSource");
-            IXMLTV_PARSER _xmltv;
-            if (!Internals.VerifyInstance<IXMLTV_PARSER>(xmltv, out _xmltv)) { return; }
-            XMLTV_PARSER = _xmltv;
+            this.SourceName = string.Empty;
+            this.GeneratorName = string.Empty;
+            this.GeneratorUrl = string.Empty;
+        }
+        public XMLTVSource(XMLTVRuntimeInstance instance)
+            : base(instance, XMLTVConstants.Root.RootElement)
+        {
+            try {
+            xmltv_logger.Verbose.Debug.WriteLine("Creating Instance of XMLTVSource");
             Create();
+            xmltv_logger.Verbose.Debug.WriteLine("Created Instance of XMLTVSource '{0}'", SourceName);
+            UpdateInstance();
+            }
+            catch (IOException ex) { Debug.WriteLine(ex.ToString()); }
         }
-       
+
+        private void UpdateInstance()
+        {
+            bool found_field = false;
+            var instance_type = this.GetInstance().GetType();
+            var fields = instance_type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
+            foreach (var field in fields)
+            {
+                if (field.FieldType == typeof(IXMLTVSource))
+                {
+                    found_field = true;
+                    try
+                    {
+                        xmltv_logger.Verbose.Debug.WriteLine("Updating instance with source information: {0}", this.ToString());
+                        field.SetValue(this.GetInstance(), this);                        
+                        break;
+                    }
+                    catch (Exception ex)
+                    {
+                        xmltv_logger.Verbose.Error.WriteLine("Unable to update instance with source information.");
+                        xmltv_logger.Verbose.Error.WriteLine(ex.ToString());
+                    }
+                }
+            }
+            if (!found_field)
+            {
+                xmltv_logger.Verbose.Error.WriteLine("Unable to update instance with source information.");
+            }
+        }
+
         #region IXMLTVSource
-        private IXMLTV_PARSER XMLTV_PARSER { get; set; }
-        public string SourceName { get { return entries[XMLTV_CONSTANTS.Source.SourceName]; } }
-        public string GeneratorName { get { return entries[XMLTV_CONSTANTS.Source.GeneratorName]; } }
-        public string GeneratorUrl { get { return entries[XMLTV_CONSTANTS.Source.GeneratorUrl]; } }
+        //private IXMLTV_PARSER XMLTV_PARSER { get; set; }
+        public string SourceName { get; private set; }
+        public string GeneratorName { get; private set; }
+        public string GeneratorUrl { get; private set; }
         #endregion
 
         private void Create()
         {
-            var doc = XMLTV_PARSER.XMLTV_LOADER.XmlDoc;
-            Debug.Assert(doc.Root.Name == XMLTV_CONSTANTS.ROOT_ELEMENT, string.Format("Expected Root Element: '{0}' but read: '{1}'", XMLTV_CONSTANTS.ROOT_ELEMENT, doc.Root.Name));
-            XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tRoot: {0}", doc.Root.Name);
+            var doc = XDocument.Parse(this.GetInstance().XmlDoc);
+            Debug.Assert(doc.Root.Name == XMLTVConstants.Root.RootElement, string.Format("Expected Root Element: '{0}' but read: '{1}'", XMLTVConstants.Root.RootElement, doc.Root.Name));
+            //xmltv_logger.Verbose.Debug.WriteLine("\tRoot: {0}", doc.Root.Name);
             var attributes = doc.Root.Attributes().ToList();
             foreach (var attribute in attributes)
             {
-                //XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\t{0}: {1}", attribute.Name, attribute.Value);
+                xmltv_logger.Verbose.Debug.WriteLine("\t{0}: {1}", attribute.Name, attribute.Value);
                 entries.Add(attribute.Name.ToString(), attribute.Value);
             }
+
+            SourceName = entries[XMLTVConstants.Source.SourceName];
+            GeneratorName = entries[XMLTVConstants.Source.GeneratorName];
+            GeneratorUrl = entries[XMLTVConstants.Source.GeneratorUrl];
+
         }
         public override string ToString()
         {
             return string.Format("XmlTv Source: '{0}' (Generated by: '{1}') (support: '{2}')", SourceName, GeneratorName, GeneratorUrl);
         }
+
+        public void Dispose()
+        {
+            //throw new NotImplementedException();
+        }
+
+        //public object Clone()
+        //{
+        //    return new XMLTVSource(this.SourceName, this.GeneratorName, this.GeneratorUrl);
+        //}
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22