/[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 13:51:58	86
+++ branches/linux/xmltv_parser/libxmltv/Core/XMLTVProgram.cs	2018/12/14 19:38:41	298
@@ -14,13 +14,14 @@
     [Serializable]
     internal class XMLTVProgram : XMLTVBase<XMLTVRuntimeInstance>, IXMLTVProgram
     {
-        public XMLTVProgram() : base(null,XMLTVConstants.PROGRAM_ELEMENT)
+        public XMLTVProgram()
+            : base(null, XMLTVConstants.Programs.RootElement)
         {
             InternalDictionaryAddKnownProperties();
          
         }
         public XMLTVProgram(XMLTVRuntimeInstance instance, XElement node)
-            : base(instance, XMLTVConstants.PROGRAM_ELEMENT)
+            : base(instance, XMLTVConstants.Programs.RootElement)
         {
             InternalDictionaryAddKnownProperties();
             try {
@@ -42,48 +43,39 @@
 
         private void InternalDictionaryAddKnownProperties()
         {
-            Properties = new Dictionary<string, object>();
-            Properties.Add("Id", 0);
-            Properties.Add(XMLTVConstants.Programs.ProgramStart, new DateTime());
-            Properties.Add(XMLTVConstants.Programs.ProgramStop, new DateTime());
-            Properties.Add(XMLTVConstants.Programs.ProgramChannelId, string.Empty);
-            Properties.Add(XMLTVConstants.Programs.ProgramTitle, string.Empty);
-            Properties.Add(XMLTVConstants.Programs.ProgramSubTitle, string.Empty);
-            Properties.Add(XMLTVConstants.Programs.ProgramDescription, string.Empty);
+            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 IPropertyDictionary _MetaData;
+        public IPropertyDictionary MetaData { get { return _MetaData; } private set { _MetaData = value; } }
+        public List<IExtraMetaData> GetExtraMetaData()
+        {
+		if (this.MetaData.ContainsProperty (XMLTVConstants.Programs.ProgramExtraMetaData)) {
+			return (List<IExtraMetaData>)this.MetaData [XMLTVConstants.Programs.ProgramExtraMetaData];
+		} else {
+			return new List<IExtraMetaData>();
+		}
         }
+        #endregion
 
-        public Dictionary<string, object> Properties { get; private set; }
-
-        private void InternalDictionaryTestOrThrow()
-        {
-            if (Properties == null) { throw new NullReferenceException("Properties collection has not been initialized."); }
-            if (Properties.Count == 0) { throw new IndexOutOfRangeException("Properties collection has 0 elements."); }
-        }
-        private void InternalProperyExistsOrThrow(string name)
-        {
-            InternalDictionaryTestOrThrow();
-            if (!Properties.ContainsKey(name)) { throw new KeyNotFoundException(string.Format("Property '{0}' does not exist", name)); }
-        }
-        public object GetProperty(string name)
-        {
-            InternalProperyExistsOrThrow(name);
-            return Properties[name];            
-        }
-        public void SetProperty(string name, object value)
-        {
-            InternalProperyExistsOrThrow(name);
-            Properties[name] = value;
-        }
         #endregion
         public override string ToString()
         {
             return string.Format("{0}: {1} - {2} ({3}) ['{4}' <==> '{5}']",
-                GetProperty("Id").ToString(),
-                GetProperty(XMLTVConstants.Programs.ProgramTitle).ToString(),
-                GetProperty(XMLTVConstants.Programs.ProgramSubTitle), ToString(),
-                GetProperty(XMLTVConstants.Programs.ProgramChannelId).ToString(),
-                ((DateTime)GetProperty(XMLTVConstants.Programs.ProgramStart)).ToString("yyyy/MM/dd hh:mm tt"),
-                ((DateTime)GetProperty(XMLTVConstants.Programs.ProgramStop)).ToString("yyyy/MM/dd hh:mm tt"));
+                MetaData["Id"].ToString(),
+                 MetaData[XMLTVConstants.Programs.ProgramTitle].ToString(),
+                 MetaData[XMLTVConstants.Programs.ProgramSubTitle].ToString(),
+                 MetaData[XMLTVConstants.Programs.ProgramChannelId].ToString(),
+                ((DateTime)MetaData[XMLTVConstants.Programs.ProgramStart]).ToDateTimeString(),
+                ((DateTime)MetaData[XMLTVConstants.Programs.ProgramStop]).ToDateTimeString());
         }
 
 
@@ -96,14 +88,14 @@
             var fields = instance_type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
             foreach (var field in fields)
             {
-                if (field.FieldType == typeof(List<IXMLTVProgram>))
+                if (field.FieldType == typeof(ProgramList))
                 {
                     found_field = true;
                     try
                     {
-                        
-                        var list = (List<IXMLTVProgram>)field.GetValue(this.GetInstance());
-                        this.SetProperty("Id", list.Count + 1);
+
+                        var list = (ProgramList)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);
@@ -124,47 +116,7 @@
 
         private void Create(XElement node)
         {
-            //if (node.HasAttributes)
-            //{
-            //    var start = node.Attribute(XMLTVConstants.Programs.ProgramStart);
-            //    this.Start = start == null ? new DateTime() : ParseDate(start.Value);
-            //    if (!this.Start.Equals(new DateTime())) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_start: {0}", start); }
-
-            //    var stop = node.Attribute(XMLTVConstants.Programs.ProgramStop);
-            //    this.Stop = stop == null ? new DateTime() : ParseDate(stop.Value);
-            //    if (!this.Stop.Equals(new DateTime())) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_stop: {0}", stop); }
-
-            //    var channelid = node.Attribute(XMLTVConstants.Programs.ProgramChannelId);
-            //    if (!string.IsNullOrEmpty(this.Description)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_channelid: {0}", channelid); }
-            //    IXMLTVChannel channel = new XMLTVChannel();
-            //    string _channelid = channelid == null ? string.Empty : channelid.Value;
-            //    this.Channel = this.GetInstance().Channels.Find(m => m.Id == _channelid);
-            //    if (this.Channel == null) { this.Channel = new XMLTVChannel(); }
-            //    if (!string.IsNullOrEmpty(_channelid)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_channel: {0}", this.Channel.ToString()); }            //}
-            //try
-            //{
-            //    var title = node.Descendants(XMLTVConstants.Programs.ProgramTitle).FirstOrDefault();
-            //    this.Title = title == null ? string.Empty : title.Value;
-            //}
-            //catch (Exception) { this.Title = string.Empty; }
-            //if (!string.IsNullOrEmpty(this.Title)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_title: {0}", this.Title == string.Empty ? "empty" : this.Title); }
-            //try
-            //{
-            //    var subtitle = node.Descendants(XMLTVConstants.Programs.ProgramSubTitle).FirstOrDefault();
-            //    this.SubTitle = subtitle == null ? string.Empty : subtitle.Value;
-            //}
-            //catch (Exception) { this.SubTitle = string.Empty; }
-            //if (!string.IsNullOrEmpty(this.SubTitle)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_subtitle: {0}", this.SubTitle == string.Empty ? "empty" : this.SubTitle); }
-            //try
-            //{
-            //    var description = node.Descendants(XMLTVConstants.Programs.ProgramDescription).FirstOrDefault();
-            //    this.Description = description == null ? string.Empty : description.Value; ;
-            //}
-            //catch (Exception) { this.Description = string.Empty; }
-            //if (!string.IsNullOrEmpty(this.Description)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_description: {0}", this.Description == string.Empty ? "empty" : this.Description); }
-
-            //ParseExtraData(node);
-
+            CreateHandlerForProgramMetaDataNode(node);
             var nodes = node.Elements().ToList();
             foreach (var sub_node in nodes)
             {
@@ -175,7 +127,7 @@
                 CreateHandlerForProgramMetaDataNode(sub_node);
             }
         }
-        private DateTime ParseDate(string timeStamp)
+        private static DateTime ParseDate(string timeStamp)
         {
             DateTime dt = new DateTime();
             try
@@ -188,6 +140,7 @@
 
         private void CreateHandlerForProgramMetaDataNode(XElement node)
         {
+
             Type t = this.GetType();
             Assembly asm = t.Assembly;
             var types = asm.GetTypes();
@@ -216,6 +169,7 @@
                             var ctors = type.GetConstructors(flags);
                             bool has_default_ctor = false;
                             foreach (var ctor in ctors) { if (ctor.GetParameters().Count() == 0) { has_default_ctor = true; } }
+                            ctors = null;
                             if (!has_default_ctor) { continue; }
                             raw_instance = Activator.CreateInstance(type, flags, null, new object[0], culture);
                             if (raw_instance != null)
@@ -224,35 +178,174 @@
                                 if (handler_value != null && handler_value.ToString() == node.Name.ToString())
                                 {
                                     handler_type = type;
+                                    handler_value = null;
+                                    raw_instance = null;
+                                    handler_prop = null;
+                                    iface = null;
                                     break;
                                 }
+                                handler_value = null;
                             }
+                            raw_instance = null;
                         }
+                        handler_prop = null;
                     }
+                    iface = null;
                 }
             }
+            t = null;
+            asm = null;
+            types = null;
+            classes = null;
             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);
+                try
+                {
+                    raw_instance = Activator.CreateInstance(typeof(UnhandledExtraProgramMetaData), flags, null, new object[] { this, node }, culture);
+                }
+                catch (Exception ex) { throw ex; }
+
+                if (raw_instance == 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
+            {
+                try
+                {
+                    raw_instance = Activator.CreateInstance(handler_type, flags, null, new object[] { this, node }, culture);
+                }
+                catch (Exception ex) { throw ex; }
             }
-            raw_instance = Activator.CreateInstance(handler_type, flags, null, new object[] { this, node }, culture);
+            raw_instance = null;
+            flags = 0;
+            culture = null;
+            node = null;
         }
 
 
 
-        #region sub-classes
+        #region sub-classes       
+        #region programme: stop/start/channelid
+        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);
+                        xmltv_logger.Verbose.Debug.WriteLine("\tprogram_start dt: {0}", t_start.ToString()); 
+                    }
+                    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);
+                        xmltv_logger.Verbose.Debug.WriteLine("\tprogram_stop dt: {0}", t_stop.ToString()); 
+                    }
+                    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);
+                    }
+                    
+                }
+                node = null;
+            }
+        }
+        #endregion
+        #region 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);
+                }
+                node = null;
             }
         }
         #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);
+                }
+                node = null;
+            }
+        }
+        #endregion
+        #region description
+        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);
+                }
+                node = null;
+            }
+        }
+        #endregion
+
+        #region UnhandledExtraMetaData
+        private class UnhandledExtraProgramMetaData : XMLTVBase<XMLTVProgram>
+        {
+            public UnhandledExtraProgramMetaData() : base(null, null) { }
+            public UnhandledExtraProgramMetaData(XMLTVProgram instance, XElement node)
+                : base(instance, null)
+            {
+                if (node == null) { throw new NullReferenceException("The node instance was null"); }      
+                xmltv_logger.Verbose.Debug.WriteLine("Parsng unhandled extra program meta-data: {0}", node.Name.ToString());
+                if (this.GetInstance() != null)
+                {
+                    List<IExtraMetaData> list = new List<IExtraMetaData>();
+                    if (!instance.MetaData.ContainsProperty(XMLTVConstants.Programs.ProgramExtraMetaData)) { instance.MetaData.AddProperty(XMLTVConstants.Programs.ProgramExtraMetaData, new List<ExtraMetaData>()); }
+                    else { list = (List<IExtraMetaData>)instance.MetaData[XMLTVConstants.Programs.ProgramExtraMetaData]; }
+                    ExtraMetaData data = new ExtraMetaData(node);
+                    list.Add(data);
+                    instance.MetaData[XMLTVConstants.Programs.ProgramExtraMetaData] = list;
+                    data = null;
+                }
+                node = null;
+            }
+        }       
+        #endregion
+        #endregion
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22