--- trunk/libxmltv/Core/XMLTVRuntimeInstance.cs 2013/03/09 10:27:39 73 +++ trunk/libxmltv/Core/XMLTVRuntimeInstance.cs 2013/03/09 11:06:39 74 @@ -275,17 +275,27 @@ 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(); - + foreach(var node in nodes) + { + if (!CreateHandlerForNode(node)) + { + xmltv_logger.Verbose.Debug.WriteLine("Unable to create handler for node: '{0}'", node.Name.ToString()); + } + } } - 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 +315,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 +327,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)) + { + 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; + } } }