/[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
+++ trunk/libxmltv/Core/XMLTVProgram.cs	2013/03/09 17:42:34	90
@@ -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,30 @@
 
         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);
+            Properties = new PropertyDictionary();
+            Properties.AddProperty("Id", 0);
+            Properties.AddProperty(XMLTVConstants.Programs.ProgramStart, new DateTime());
+            Properties.AddProperty(XMLTVConstants.Programs.ProgramStop, new DateTime());
+            Properties.AddProperty(XMLTVConstants.Programs.ProgramChannelId, string.Empty);
+            Properties.AddProperty(XMLTVConstants.Programs.ProgramTitle, string.Empty);
+            Properties.AddProperty(XMLTVConstants.Programs.ProgramSubTitle, string.Empty);
+            Properties.AddProperty(XMLTVConstants.Programs.ProgramDescription, string.Empty);
         }
 
-        public Dictionary<string, object> Properties { get; private set; }
+        #region Property Dictionary Support
+        public PropertyDictionary Properties { get; private set; }
+        #endregion
 
-        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"));
+                Properties["Id"].ToString(),
+                 Properties[(XMLTVConstants.Programs.ProgramTitle)].ToString(),
+                 Properties[(XMLTVConstants.Programs.ProgramSubTitle)].ToString(),
+                 Properties[(XMLTVConstants.Programs.ProgramChannelId)].ToString(),
+                ((DateTime) Properties[XMLTVConstants.Programs.ProgramStart]).ToString("yyyy/MM/dd hh:mm tt"),
+                ((DateTime) Properties[XMLTVConstants.Programs.ProgramStop]).ToString("yyyy/MM/dd hh:mm tt"));
         }
 
 
@@ -103,7 +86,7 @@
                     {
                         
                         var list = (List<IXMLTVProgram>)field.GetValue(this.GetInstance());
-                        this.SetProperty("Id", list.Count + 1);
+                        Properties["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);
@@ -135,7 +118,7 @@
             //    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); }
+            //    if (!string.IsNullOrEmpty(this.channelid)) { 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);
@@ -165,7 +148,7 @@
 
             //ParseExtraData(node);
 
-            var nodes = node.Elements().ToList();
+            var nodes = node.DescendantsAndSelf().ToList();
             foreach (var sub_node in nodes)
             {
                 if (this.GetInstance().IsAborting)
@@ -175,7 +158,7 @@
                 CreateHandlerForProgramMetaDataNode(sub_node);
             }
         }
-        private DateTime ParseDate(string timeStamp)
+        private static DateTime ParseDate(string timeStamp)
         {
             DateTime dt = new DateTime();
             try
@@ -188,6 +171,7 @@
 
         private void CreateHandlerForProgramMetaDataNode(XElement node)
         {
+
             Type t = this.GetType();
             Assembly asm = t.Assembly;
             var types = asm.GetTypes();
@@ -239,20 +223,95 @@
                 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);
             }
-            raw_instance = Activator.CreateInstance(handler_type, flags, null, new object[] { this, node }, culture);
+            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.Properties.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.Properties.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.Properties.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.Properties.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.Properties.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.Properties.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