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

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

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

--- trunk/libxmltv/Core/XMLTVRuntimeInstance.cs	2013/03/09 09:29:40	72
+++ trunk/libxmltv/Core/XMLTVRuntimeInstance.cs	2013/03/09 10:27:39	73
@@ -9,6 +9,7 @@
 using System.Reflection;
 using System.Globalization;
 using System.Diagnostics;
+using System.Xml.Linq;
 
 namespace libxmltv.Core
 {
@@ -40,7 +41,6 @@
             }
         }
         
-
         private void OnCreatedInstance(object sender, EventArgs e)
         {
             if (OnInstanceCreated != null) { OnInstanceCreated.Invoke(sender, e); }
@@ -48,9 +48,9 @@
 
         private void init()
         {
-            this.Source = new XMLTVSource();
-            this.Channels = new Dictionary<string, IXMLTVChannel>();
-            this.Programs = new Dictionary<int, IXMLTVProgram>();
+            this.Source = new List<IXMLTVSource>();
+            this.Channels = new List<IXMLTVChannel>();
+            this.Programs = new List<IXMLTVProgram>();
             this.XmlFile_Name = string.Empty;
             this.XmlFile_FullName = string.Empty;
             this.XmlDoc = string.Empty;
@@ -68,13 +68,13 @@
         public string XmlFile_FullName { get { return _XmlFile_FullName; } set { _XmlFile_FullName = value; } }
 
         private string _XmlDoc;
-        public string XmlDoc { get { return _XmlDoc; } set { _XmlDoc = value; } }               
-        private IXMLTVSource _Source;
-        public IXMLTVSource Source { get { return _Source; } set { _Source = value; } }
-        private Dictionary<string, IXMLTVChannel> _Channels;
-        public Dictionary<string, IXMLTVChannel> Channels { get { return _Channels; } set { _Channels = value; } }
-        private Dictionary<int, IXMLTVProgram> _Programs;
-        public Dictionary<int, IXMLTVProgram> Programs { get { return _Programs; } set { _Programs = value; } }
+        public string XmlDoc { get { return _XmlDoc; } set { _XmlDoc = value; } }
+        private List<IXMLTVSource> _Source;
+        public List<IXMLTVSource> Source { get { return _Source; } set { _Source = value; } }
+        private List<IXMLTVChannel> _Channels;
+        public List<IXMLTVChannel> Channels { get { return _Channels; } set { _Channels = value; } }
+        private List<IXMLTVProgram> _Programs;
+        public List<IXMLTVProgram> Programs { get { return _Programs; } set { _Programs = value; } }
         #endregion       
         #region IOnInstanceCreated members
         [NonSerialized]
@@ -122,7 +122,7 @@
             xmltv_logger.Debug.WriteLine("Loading from instance...");
             if (this.Source != null)
             {
-                xmltv_logger.Info.WriteLine("Source Loaded: '{0}' Created by '{1}' - original source file: '{2}'", this.Source.SourceName, this.Source.GeneratorName, this.XmlFile_FullName);
+                xmltv_logger.Info.WriteLine("Source Loaded: '{0}' Created by '{1}' - original source file: '{2}'", this.Source.FirstOrDefault().SourceName, this.Source.FirstOrDefault().GeneratorName, this.XmlFile_FullName);
             }
             else
             {
@@ -131,7 +131,7 @@
             }
             if (this.Channels != null)
             {
-                xmltv_logger.Info.WriteLine("Source Loaded: '{0}' Channels from source '{1}'", this.Channels.Count, this.Source.SourceName);
+                xmltv_logger.Info.WriteLine("Source Loaded: '{0}' Channels from source '{1}'", this.Channels.Count, this.Source.FirstOrDefault().SourceName);
             }
             else
             {
@@ -140,7 +140,7 @@
             }
             if (this.Programs != null)
             {
-                xmltv_logger.Info.WriteLine("Source Loaded: '{0}' Programs from source '{1}'", this.Programs.Count, this.Source.SourceName);
+                xmltv_logger.Info.WriteLine("Source Loaded: '{0}' Programs from source '{1}'", this.Programs.Count, this.Source.FirstOrDefault().SourceName);
             }
             else
             {
@@ -218,6 +218,11 @@
 
     internal class XMLTVInstance : IGetInstance<XMLTVRuntimeInstance>, IDisposable
     {
+        public void Dispose()
+        {
+            //throw new NotImplementedException();
+        }
+
         private XMLTVRuntimeInstance gInstance;
         public XMLTVRuntimeInstance GetInstance() { return gInstance; }
         public XMLTVInstance(string xmlfile) 
@@ -225,6 +230,7 @@
             try
             {
                 CreateLoader(xmlfile);
+                CreateParser();
             }
             catch (Exception ex)
             {
@@ -266,14 +272,52 @@
             if (getter_instance != null) { gInstance = getter_instance.GetInstance(); }
             else { throw new Exception("Found a compatible XMLTV Data Loader, but was unable to obtain the instance holding the data"); }
         }
-        private void CreateParser(XMLTVRuntimeInstance instance)
+        private void CreateParser()
         {
+            var doc = XDocument.Parse(this.GetInstance().XmlDoc);
+            var root_element = doc.Root.Name;
+            if (root_element == null) { throw new NullReferenceException("Root element is null"); }
+            CreateRootHandler(root_element.ToString());
+
+            var nodes = doc.Root.Elements().ToList();
 
         }
 
-        public void Dispose()
+
+        private void CreateRootHandler(string root_element)
         {
-            //throw new NotImplementedException();
+            object raw_instance = null;
+            BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
+            CultureInfo culture = CultureInfo.CurrentCulture;
+            Assembly asm = Assembly.GetExecutingAssembly();
+            var types = asm.GetTypes();
+            Type handler_type = null;
+            foreach (var type in types)
+            {
+                if (type.BaseType != null && type.BaseType == typeof(XMLTVBase<XMLTVRuntimeInstance>))
+                {
+                    try
+                    {
+                        var handler_prop = type.GetProperty("Handler");
+                        if (handler_prop != null)
+                        {
+                            raw_instance = Activator.CreateInstance(type, flags, null, new object[0], culture);
+                            if (raw_instance != null)
+                            {
+                                object handler_value = handler_prop.GetValue(raw_instance, null);
+                                if (handler_value != null && handler_value.ToString() == root_element)
+                                {
+                                    handler_type = type;
+                                    break;
+                                }
+                            }                            
+                        }
+                    }
+                    catch (Exception) { }
+                }
+            }
+            if (handler_type == null) { throw new Exception("Unable to find a compatible handler to parse document root."); }
+            raw_instance = Activator.CreateInstance(handler_type, flags, null, new object[] { gInstance }, culture);
         }
 
         

 

  ViewVC Help
Powered by ViewVC 1.1.22