using System; using System.Collections.Generic; using System.Linq; using System.Text; using libxmltv.Interfaces; using System.Xml.Linq; using System.Reflection; using System.IO; using System.Diagnostics; namespace libxmltv.Core { [Serializable] internal class XMLTVChannel : XMLTVBase, IXMLTVChannel { public XMLTVChannel() : base(null, XMLTVConstants.Channels.RootElement) { InternalDictionaryAddKnownProperties(); Id = string.Empty; //Number = 0; //CallSign = string.Empty; //Name = string.Empty; } public XMLTVChannel(XMLTVRuntimeInstance instance, XElement node) : base(instance, XMLTVConstants.Channels.RootElement) { InternalDictionaryAddKnownProperties(); try { xmltv_logger.Verbose.Debug.WriteLine("Creating Instance of XMLTVChannel"); Create(node); xmltv_logger.Verbose.Debug.WriteLine("Created Instance of XMLTVChannel"); UpdateInstance(); } catch (IOException ex) { Debug.WriteLine(ex.ToString()); } } private void InternalDictionaryAddKnownProperties() { MetaData = new PropertyList(); } #region IXMLTVChannel members public string Id { get; private set; } //public int Number { get; private set; } //public string CallSign { get; private set; } //public string Name { get; private set; } private PropertyList _MetaData; public PropertyList MetaData { get { return _MetaData; } private set { _MetaData = value; } } #endregion public override string ToString() { return string.Format("{0}", Id); } 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)) { found_field = true; try { var list = (List)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) { //throw new NotImplementedException("Channel.Create(node) is not currently implemented."); // get the channel id Id = node.Attribute(XMLTVConstants.Channels.ChannelId).Value; xmltv_logger.Verbose.Debug.WriteLine("\tchannel_id: {0}", Id); var names = node.Elements().ToList(); foreach (var name in names) { PropertyValuePair p = new PropertyValuePair(name.Name.ToString(), name.Value); MetaData.AddProperty(p.Name,p.Value); xmltv_logger.Verbose.Debug.WriteLine("\t{0}: {1}", p.Name, p.Value); } } } }