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

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

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

--- trunk/libxmltv/Core/XMLTVChannel.cs	2013/03/09 10:27:39	73
+++ trunk/libxmltv/Core/XMLTVChannel.cs	2013/03/09 11:28:41	77
@@ -4,32 +4,27 @@
 using System.Text;
 using libxmltv.Interfaces;
 using System.Xml.Linq;
+using System.Reflection;
 
 namespace libxmltv.Core
 {
     [Serializable]
-    internal class XMLTVChannel : IXMLTVChannel
+    internal class XMLTVChannel : XMLTVBase<XMLTVRuntimeInstance>, IXMLTVChannel
     {
-        public XMLTVChannel()
+        public XMLTVChannel() : base(null,XMLTVConstants.CHANNEL_ELEMENT)
         {
             Id = string.Empty;
             Number = 0;
             CallSign = string.Empty;
             Name = string.Empty;
         }
-        public XMLTVChannel(XElement e)
-            : this()
+        public XMLTVChannel(XMLTVRuntimeInstance instance, XElement node)
+            : base(instance, XMLTVConstants.CHANNEL_ELEMENT)
         {
-            // get the channel id
-            Id = e.Attribute(XMLTVConstants.Channels.ChannelId).Value;
-            xmltv_logger.Verbose.Debug.WriteLine("\tchannel_id: {0}", Id);
-            var names = e.Descendants(XMLTVConstants.Channels.ChannelDisplayName).ToList();
-            Number = Convert.ToInt32(names[1].Value);
-            xmltv_logger.Verbose.Debug.WriteLine("\tchannel_number: {0}", Number);
-            CallSign = names[2].Value;
-            xmltv_logger.Verbose.Debug.WriteLine("\tchannel_callsign: {0}", CallSign);
-            Name = names[3].Value;
-            xmltv_logger.Verbose.Debug.WriteLine("\tchannel_name: {0}", Name);
+            xmltv_logger.Verbose.Debug.WriteLine("Creating Instance of XMLTVChannel");
+            Create(node);
+            xmltv_logger.Verbose.Debug.WriteLine("Created Instance of XMLTVChannel");
+            UpdateInstance();            
         }
         #region IXMLTVChannel members
         public string Id { get; private set; }
@@ -41,6 +36,51 @@
         {
             return string.Format("{0}: {1} {2} ({3})", Id, Number, Name, CallSign);
         }
+
+        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(List<IXMLTVChannel>))
+                {
+                    found_field = true;
+                    try
+                    {                                                
+                        var list = (List<IXMLTVChannel>)field.GetValue(this.GetInstance());
+                        list.Add(this);
+                        xmltv_logger.Verbose.Debug.WriteLine("Updating instance with channel information: {0}", this.ToString());
+                        field.SetValue(this.GetInstance(), list);
+                        break;
+                    }
+                    catch (Exception ex)
+                    {
+                        xmltv_logger.Verbose.Error.WriteLine("Unable to update instance with channel information.");
+                        xmltv_logger.Verbose.Error.WriteLine(ex.ToString());
+                    }
+                }
+            }
+            if (!found_field)
+            {
+                xmltv_logger.Verbose.Error.WriteLine("Unable to update instance with channel information.");
+            }
+        }
+
+        private void Create(XElement node)
+        {
+            // get the channel id
+            Id = node.Attribute(XMLTVConstants.Channels.ChannelId).Value;
+            xmltv_logger.Verbose.Debug.WriteLine("\tchannel_id: {0}", Id);
+            var names = node.Descendants(XMLTVConstants.Channels.ChannelDisplayName).ToList();
+            Number = Convert.ToInt32(names[1].Value);
+            xmltv_logger.Verbose.Debug.WriteLine("\tchannel_number: {0}", Number);
+            CallSign = names[2].Value;
+            xmltv_logger.Verbose.Debug.WriteLine("\tchannel_callsign: {0}", CallSign);
+            Name = names[3].Value;
+            xmltv_logger.Verbose.Debug.WriteLine("\tchannel_name: {0}", Name);
+        }
     }
 
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22