/[xmltv_parser]/branches/linux/xmltv_parser/libxmltv/Core/XMLTVProgram.cs
ViewVC logotype

Diff of /branches/linux/xmltv_parser/libxmltv/Core/XMLTVProgram.cs

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

--- trunk/libxmltv/Core/XMLTVProgram.cs	2013/03/09 10:27:39	73
+++ trunk/libxmltv/Core/XMLTVProgram.cs	2013/03/10 11:10:30	104
@@ -3,34 +3,276 @@
 using System.Linq;
 using System.Text;
 using libxmltv.Interfaces;
+using System.Xml.Linq;
+using System.Reflection;
+using System.IO;
+using System.Diagnostics;
+using System.Globalization;
 
 namespace libxmltv.Core
 {
     [Serializable]
-    internal class XMLTVProgram : IXMLTVProgram
+    internal class XMLTVProgram : XMLTVBase<XMLTVRuntimeInstance>, IXMLTVProgram
     {
         public XMLTVProgram()
+            : base(null, XMLTVConstants.Programs.RootElement)
         {
-            Id = 0;
-            Start = new DateTime();
-            Stop = new DateTime();
-            Channel = new XMLTVChannel();
-            Title = string.Empty;
-            SubTitle = string.Empty;
-            Description = string.Empty;
+            InternalDictionaryAddKnownProperties();
+         
+        }
+        public XMLTVProgram(XMLTVRuntimeInstance instance, XElement node)
+            : base(instance, XMLTVConstants.Programs.RootElement)
+        {
+            InternalDictionaryAddKnownProperties();
+            try {
+            xmltv_logger.Verbose.Debug.WriteLine("Creating Instance of XMLTVProgram");
+            Create(node);
+            xmltv_logger.Verbose.Debug.WriteLine("Created Instance of XMLTVProgram");
+            UpdateInstance();
+            }
+            catch (IOException ex) { Debug.WriteLine(ex.ToString()); }
         }
         #region IXMLTVProgram members
-        public int Id { get; set; }
-        public DateTime Start { get; set; }
-        public DateTime Stop { get; set; }
-        public IXMLTVChannel Channel { get; set; }
-        public string Title { get; set; }
-        public string SubTitle { get; set; }
-        public string Description { get; set; }
+        //public int Id { get; set; }
+        //public DateTime Start { get; set; }
+        //public DateTime Stop { get; set; }
+        //public IXMLTVChannel Channel { get; set; }
+        //public string Title { get; set; }
+        //public string SubTitle { get; set; }
+        //public string Description { get; set; }
+
+        private void InternalDictionaryAddKnownProperties()
+        {
+            MetaData = new PropertyDictionary();
+            MetaData.AddProperty("Id", 0);
+            MetaData.AddProperty(XMLTVConstants.Programs.ProgramStart, new DateTime());
+            MetaData.AddProperty(XMLTVConstants.Programs.ProgramStop, new DateTime());
+            MetaData.AddProperty(XMLTVConstants.Programs.ProgramChannelId, string.Empty);
+            MetaData.AddProperty(XMLTVConstants.Programs.ProgramTitle, string.Empty);
+            MetaData.AddProperty(XMLTVConstants.Programs.ProgramSubTitle, string.Empty);
+            MetaData.AddProperty(XMLTVConstants.Programs.ProgramDescription, string.Empty);
+        }
+
+        #region Property Dictionary Support
+        private PropertyDictionary _MetaData;
+        public PropertyDictionary MetaData { get { return _MetaData; } private set { _MetaData = value; } }
+        #endregion
+
         #endregion
         public override string ToString()
         {
-            return string.Format("{0}: {1} - {2} ({3}) ['{4}' <==> '{5}']", Id, Title, SubTitle, Channel.ToString(), Start.ToString("yyyy/MM/dd hh:mm tt"), Stop.ToString("yyyy/MM/dd hh:mm tt"));
+            return string.Format("{0}: {1} - {2} ({3}) ['{4}' <==> '{5}']",
+                MetaData["Id"].ToString(),
+                 MetaData[XMLTVConstants.Programs.ProgramTitle].ToString(),
+                 MetaData[XMLTVConstants.Programs.ProgramSubTitle].ToString(),
+                 MetaData[XMLTVConstants.Programs.ProgramChannelId].ToString(),
+                ((DateTime)MetaData[XMLTVConstants.Programs.ProgramStart]).ToString("yyyy/MM/dd hh:mm tt"),
+                ((DateTime)MetaData[XMLTVConstants.Programs.ProgramStop]).ToString("yyyy/MM/dd hh:mm tt"));
+        }
+
+
+        
+
+        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<IXMLTVProgram>))
+                {
+                    found_field = true;
+                    try
+                    {
+                        
+                        var list = (List<IXMLTVProgram>)field.GetValue(this.GetInstance());
+                        MetaData["Id"] = list.Count + 1;
+                        list.Add(this);
+                        xmltv_logger.Verbose.Debug.WriteLine("Updating instance with program information: {0}", this.ToString());
+                        field.SetValue(this.GetInstance(), list);
+                        break;
+                    }
+                    catch (Exception ex)
+                    {
+                        xmltv_logger.Verbose.Error.WriteLine("Unable to update instance with program information.");
+                        xmltv_logger.Verbose.Error.WriteLine(ex.ToString());
+                    }
+                }
+            }
+            if (!found_field)
+            {
+                xmltv_logger.Verbose.Error.WriteLine("Unable to update instance with program information.");
+            }
+        }
+
+        private void Create(XElement node)
+        {
+            CreateHandlerForProgramMetaDataNode(node);
+            var nodes = node.Elements().ToList();
+            foreach (var sub_node in nodes)
+            {
+                if (this.GetInstance().IsAborting)
+                {
+                    break;
+                }
+                CreateHandlerForProgramMetaDataNode(sub_node);
+            }
+        }
+        private static DateTime ParseDate(string timeStamp)
+        {
+            DateTime dt = new DateTime();
+            try
+            {
+                dt = DateTime.ParseExact(timeStamp, "yyyyMMddHHmmss zzzz", System.Globalization.CultureInfo.CurrentCulture);
+            }
+            catch (Exception ex) { throw ex; }
+            return dt;
+        }
+
+        private void CreateHandlerForProgramMetaDataNode(XElement node)
+        {
+
+            Type t = this.GetType();
+            Assembly asm = t.Assembly;
+            var types = asm.GetTypes();
+            var classes = types.ToList().FindAll(
+                m => 
+                    m.DeclaringType == t &&
+                    m.IsClass &&
+                    !m.IsSealed
+                );
+            classes.TrimExcess();
+
+            object raw_instance = null;
+            BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
+            CultureInfo culture = CultureInfo.CurrentCulture;
+            Type handler_type = null;
+            foreach (var type in classes)
+            {
+                if (type.BaseType != null && type.BaseType == typeof(XMLTVBase<XMLTVProgram>))
+                {
+                    var iface = type.GetInterface("IXMLTVHandler", true);
+                    if (iface != null)
+                    {
+                        var handler_prop = type.GetProperty("Handler");
+                        if (handler_prop != null)
+                        {
+                            var ctors = type.GetConstructors(flags);
+                            bool has_default_ctor = false;
+                            foreach (var ctor in ctors) { if (ctor.GetParameters().Count() == 0) { has_default_ctor = true; } }
+                            if (!has_default_ctor) { continue; }
+                            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.ToString())
+                                {
+                                    handler_type = type;
+                                    break;
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+            if (handler_type == null)
+            {
+                StringBuilder node_builder = new StringBuilder();
+                node_builder.AppendFormat("<{0} ", node.Name);
+                if (node.HasAttributes) { foreach (var attribute in node.Attributes()) { node_builder.AppendFormat("{0}=\"{1}\" ", attribute.Name, attribute.Value); } }
+                string node_text = string.Format("{0}>", node_builder.ToString().TrimEnd(new char[] { ' ' }));
+                xmltv_logger.Verbose.Warn.WriteLine("Ignoring unhandled extra meta-data: {0}", node_text);
+            }
+            else
+            {
+                raw_instance = Activator.CreateInstance(handler_type, flags, null, new object[] { this, node }, culture);
+            }
         }
+
+
+
+        #region sub-classes
+        #region program title
+        private class title : XMLTVBase<XMLTVProgram>
+        {
+            public title() : base(null, XMLTVConstants.Programs.ProgramTitle) { }
+            public title(XMLTVProgram instance, XElement node)
+                : base(instance, XMLTVConstants.Programs.ProgramTitle)
+            {                
+                if(node == null){throw new NullReferenceException("The node instance was null");}                
+                if (node.Value != null)
+                {
+                    instance.MetaData.AddProperty(XMLTVConstants.Programs.ProgramTitle, node.Value);
+                    xmltv_logger.Verbose.Debug.WriteLine("\tprogram_title: {0}", node.Value);
+                }                
+            }
+        }
+        #endregion
+        #region program stop/start/channel (programme)
+        private class programme : XMLTVBase<XMLTVProgram>
+        {
+            public programme() : base(null, XMLTVConstants.Programs.RootElement) { }
+            public programme(XMLTVProgram instance, XElement node)
+                : base(instance, XMLTVConstants.Programs.RootElement)
+            {
+                if (node == null) { throw new NullReferenceException("The node instance was null"); }
+                if (node.HasAttributes)
+                {                    
+                    var start = node.Attribute(XMLTVConstants.Programs.ProgramStart);
+                    var t_start = start == null ? new DateTime() : ParseDate(start.Value);
+                    if (!t_start.Equals(new DateTime())) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_start: {0}", start); }
+                    instance.MetaData.AddProperty(XMLTVConstants.Programs.ProgramStart, t_start);
+
+                    var stop = node.Attribute(XMLTVConstants.Programs.ProgramStop);
+                    var t_stop = stop == null ? new DateTime() : ParseDate(stop.Value);
+                    if (!t_stop.Equals(new DateTime())) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_stop: {0}", stop); }
+                    instance.MetaData.AddProperty(XMLTVConstants.Programs.ProgramStop, t_stop);
+
+                    var channelid = node.Attribute(XMLTVConstants.Programs.ProgramChannelId);
+                    if (channelid != null)
+                    {
+                        if (!string.IsNullOrEmpty(channelid.Value)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_channelid: {0}", channelid.Value); }
+                        instance.MetaData.AddProperty(XMLTVConstants.Programs.ProgramChannelId, channelid.Value);
+                    }
+                    
+                }
+            }
+        }
+        #endregion
+
+        #region sub title
+        private class subtitle : XMLTVBase<XMLTVProgram>
+        {
+            public subtitle() : base(null, XMLTVConstants.Programs.ProgramSubTitle) { }
+            public subtitle(XMLTVProgram instance, XElement node)
+                : base(instance, XMLTVConstants.Programs.ProgramSubTitle)
+            {
+                if (node == null) { throw new NullReferenceException("The node instance was null"); }
+                if (node.Value != null)
+                {
+                    instance.MetaData.AddProperty(XMLTVConstants.Programs.ProgramSubTitle, node.Value);
+                    xmltv_logger.Verbose.Debug.WriteLine("\tprogram_subtitle: {0}", node.Value);
+                }
+            }
+        }
+        #endregion
+        #region sub title
+        private class description : XMLTVBase<XMLTVProgram>
+        {
+            public description() : base(null, XMLTVConstants.Programs.ProgramDescription) { }
+            public description(XMLTVProgram instance, XElement node)
+                : base(instance, XMLTVConstants.Programs.ProgramDescription)
+            {
+                if (node == null) { throw new NullReferenceException("The node instance was null"); }
+                if (node.Value != null)
+                {
+                    instance.MetaData.AddProperty(XMLTVConstants.Programs.ProgramDescription, node.Value);
+                    xmltv_logger.Verbose.Debug.WriteLine("\tprogram_description: {0}", node.Value);
+                }
+            }
+        }
+        #endregion
+        #endregion
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22