/[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 10:27:39	73
+++ trunk/libxmltv/Core/XMLTVRuntimeInstance.cs	2013/03/09 11:28:41	77
@@ -48,7 +48,7 @@
 
         private void init()
         {
-            this.Source = new List<IXMLTVSource>();
+            this.Source = new XMLTVSource();
             this.Channels = new List<IXMLTVChannel>();
             this.Programs = new List<IXMLTVProgram>();
             this.XmlFile_Name = string.Empty;
@@ -69,8 +69,8 @@
 
         private string _XmlDoc;
         public string XmlDoc { get { return _XmlDoc; } set { _XmlDoc = value; } }
-        private List<IXMLTVSource> _Source;
-        public List<IXMLTVSource> Source { get { return _Source; } set { _Source = value; } }
+        private IXMLTVSource _Source;
+        public 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;
@@ -118,11 +118,12 @@
             {
                 throw new NullReferenceException("Failed to load from instance because the instance is null.");
             }
-            CloneFromInstance(ref instance);
             xmltv_logger.Debug.WriteLine("Loading from instance...");
+            CloneFromInstance(ref instance);            
+            xmltv_logger.Debug.WriteLine("Loaded from instance...");
             if (this.Source != null)
             {
-                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);
+                xmltv_logger.Info.WriteLine("Source Loaded: '{0}' Created by '{1}' - original source file: '{2}'", this.Source.SourceName, this.Source.GeneratorName, this.XmlFile_FullName);
             }
             else
             {
@@ -131,7 +132,7 @@
             }
             if (this.Channels != null)
             {
-                xmltv_logger.Info.WriteLine("Source Loaded: '{0}' Channels from source '{1}'", this.Channels.Count, this.Source.FirstOrDefault().SourceName);
+                xmltv_logger.Info.WriteLine("Source Loaded: '{0}' Channels from source '{1}'", this.Channels.Count, this.Source.SourceName);
             }
             else
             {
@@ -140,14 +141,13 @@
             }
             if (this.Programs != null)
             {
-                xmltv_logger.Info.WriteLine("Source Loaded: '{0}' Programs from source '{1}'", this.Programs.Count, this.Source.FirstOrDefault().SourceName);
+                xmltv_logger.Info.WriteLine("Source Loaded: '{0}' Programs from source '{1}'", this.Programs.Count, this.Source.SourceName);
             }
             else
             {
                 xmltv_logger.Error.WriteLine("Programs Property is null.");
                 throw new NullReferenceException("Programs Property is null.");
-            }
-            xmltv_logger.Debug.WriteLine("Loaded from instance...");
+            }            
             return instance;
         }
         #endregion
@@ -275,17 +275,30 @@
         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 root_element = doc.Root;            
+            CreateHandlerForRootNode(root_element);
             var nodes = doc.Root.Elements().ToList();
 
+            double total_nodes = nodes.Count;
+            double node_index = 0;
+            double progress = 0;
+            foreach(var node in nodes)
+            {
+                if (!CreateHandlerForNode(node)) { xmltv_logger.Verbose.Debug.WriteLine("Unable to create handler for node: '{0}'", node.Name.ToString()); }
+                node_index++;
+                progress = 100.0 * (node_index / total_nodes);
+                xmltv_logger.ReportProgress(this, new Enterprise.Logging.ReportProgressEventArgs((int)progress));
+                Application.DoEvents();
+            }
         }
 
 
-        private void CreateRootHandler(string root_element)
-        {
+        private void CreateHandlerForRootNode(XElement root)
+        {                        
+            if (root == null) { throw new NullReferenceException("Root element is null"); }
+            if (root.Name == null) { throw new NullReferenceException("Root element's Name is null"); }
+            var root_name =  root.Name.ToString();
+            xmltv_logger.Verbose.Debug.WriteLine("Creating handler for root: '{0}'", root_name.ToString());
             object raw_instance = null;
             BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
             CultureInfo culture = CultureInfo.CurrentCulture;
@@ -305,7 +318,7 @@
                             if (raw_instance != null)
                             {
                                 object handler_value = handler_prop.GetValue(raw_instance, null);
-                                if (handler_value != null && handler_value.ToString() == root_element)
+                                if (handler_value != null && handler_value.ToString() == root_name)
                                 {
                                     handler_type = type;
                                     break;
@@ -317,9 +330,52 @@
                 }
             }
             if (handler_type == null) { throw new Exception("Unable to find a compatible handler to parse document root."); }
+            xmltv_logger.Verbose.Debug.WriteLine("Created handler for root: '{0}'", root_name.ToString());
             raw_instance = Activator.CreateInstance(handler_type, flags, null, new object[] { gInstance }, culture);
         }
 
+        private bool CreateHandlerForNode(XElement node)
+        {
+            if (node == null) { throw new NullReferenceException("Node element is null"); }
+            if (node.Name == null) { throw new NullReferenceException("Node element's Name is null"); }
+            var node_name = node.Name.ToString();
+
+            xmltv_logger.Verbose.Debug.WriteLine("Creating handler for node: '{0}'", node_name.ToString());
+            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() == node_name)
+                                {
+                                    handler_type = type;
+                                    break;
+                                }
+                            }
+                        }
+                    }
+                    catch (Exception) { }
+                }
+            }
+            if (handler_type == null) { throw new Exception("Unable to find a compatible handler to parse document root."); }
+            xmltv_logger.Verbose.Debug.WriteLine("Created handler for node: '{0}'", node_name.ToString());
+            raw_instance = Activator.CreateInstance(handler_type, flags, null, new object[] { gInstance, node }, culture);
+            return true;
+        }
         
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22