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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 86 - (show annotations) (download)
Sat Mar 9 13:51:58 2013 UTC (6 years, 3 months ago) by william
Original Path: trunk/libxmltv/Core/XMLTVProgram.cs
File size: 12308 byte(s)
+ use a Properties dictionary to hold the Program metadata

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using libxmltv.Interfaces;
6 using System.Xml.Linq;
7 using System.Reflection;
8 using System.IO;
9 using System.Diagnostics;
10 using System.Globalization;
11
12 namespace libxmltv.Core
13 {
14 [Serializable]
15 internal class XMLTVProgram : XMLTVBase<XMLTVRuntimeInstance>, IXMLTVProgram
16 {
17 public XMLTVProgram() : base(null,XMLTVConstants.PROGRAM_ELEMENT)
18 {
19 InternalDictionaryAddKnownProperties();
20
21 }
22 public XMLTVProgram(XMLTVRuntimeInstance instance, XElement node)
23 : base(instance, XMLTVConstants.PROGRAM_ELEMENT)
24 {
25 InternalDictionaryAddKnownProperties();
26 try {
27 xmltv_logger.Verbose.Debug.WriteLine("Creating Instance of XMLTVProgram");
28 Create(node);
29 xmltv_logger.Verbose.Debug.WriteLine("Created Instance of XMLTVProgram");
30 UpdateInstance();
31 }
32 catch (IOException ex) { Debug.WriteLine(ex.ToString()); }
33 }
34 #region IXMLTVProgram members
35 //public int Id { get; set; }
36 //public DateTime Start { get; set; }
37 //public DateTime Stop { get; set; }
38 //public IXMLTVChannel Channel { get; set; }
39 //public string Title { get; set; }
40 //public string SubTitle { get; set; }
41 //public string Description { get; set; }
42
43 private void InternalDictionaryAddKnownProperties()
44 {
45 Properties = new Dictionary<string, object>();
46 Properties.Add("Id", 0);
47 Properties.Add(XMLTVConstants.Programs.ProgramStart, new DateTime());
48 Properties.Add(XMLTVConstants.Programs.ProgramStop, new DateTime());
49 Properties.Add(XMLTVConstants.Programs.ProgramChannelId, string.Empty);
50 Properties.Add(XMLTVConstants.Programs.ProgramTitle, string.Empty);
51 Properties.Add(XMLTVConstants.Programs.ProgramSubTitle, string.Empty);
52 Properties.Add(XMLTVConstants.Programs.ProgramDescription, string.Empty);
53 }
54
55 public Dictionary<string, object> Properties { get; private set; }
56
57 private void InternalDictionaryTestOrThrow()
58 {
59 if (Properties == null) { throw new NullReferenceException("Properties collection has not been initialized."); }
60 if (Properties.Count == 0) { throw new IndexOutOfRangeException("Properties collection has 0 elements."); }
61 }
62 private void InternalProperyExistsOrThrow(string name)
63 {
64 InternalDictionaryTestOrThrow();
65 if (!Properties.ContainsKey(name)) { throw new KeyNotFoundException(string.Format("Property '{0}' does not exist", name)); }
66 }
67 public object GetProperty(string name)
68 {
69 InternalProperyExistsOrThrow(name);
70 return Properties[name];
71 }
72 public void SetProperty(string name, object value)
73 {
74 InternalProperyExistsOrThrow(name);
75 Properties[name] = value;
76 }
77 #endregion
78 public override string ToString()
79 {
80 return string.Format("{0}: {1} - {2} ({3}) ['{4}' <==> '{5}']",
81 GetProperty("Id").ToString(),
82 GetProperty(XMLTVConstants.Programs.ProgramTitle).ToString(),
83 GetProperty(XMLTVConstants.Programs.ProgramSubTitle), ToString(),
84 GetProperty(XMLTVConstants.Programs.ProgramChannelId).ToString(),
85 ((DateTime)GetProperty(XMLTVConstants.Programs.ProgramStart)).ToString("yyyy/MM/dd hh:mm tt"),
86 ((DateTime)GetProperty(XMLTVConstants.Programs.ProgramStop)).ToString("yyyy/MM/dd hh:mm tt"));
87 }
88
89
90
91
92 private void UpdateInstance()
93 {
94 bool found_field = false;
95 var instance_type = this.GetInstance().GetType();
96 var fields = instance_type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
97 foreach (var field in fields)
98 {
99 if (field.FieldType == typeof(List<IXMLTVProgram>))
100 {
101 found_field = true;
102 try
103 {
104
105 var list = (List<IXMLTVProgram>)field.GetValue(this.GetInstance());
106 this.SetProperty("Id", list.Count + 1);
107 list.Add(this);
108 xmltv_logger.Verbose.Debug.WriteLine("Updating instance with program information: {0}", this.ToString());
109 field.SetValue(this.GetInstance(), list);
110 break;
111 }
112 catch (Exception ex)
113 {
114 xmltv_logger.Verbose.Error.WriteLine("Unable to update instance with program information.");
115 xmltv_logger.Verbose.Error.WriteLine(ex.ToString());
116 }
117 }
118 }
119 if (!found_field)
120 {
121 xmltv_logger.Verbose.Error.WriteLine("Unable to update instance with program information.");
122 }
123 }
124
125 private void Create(XElement node)
126 {
127 //if (node.HasAttributes)
128 //{
129 // var start = node.Attribute(XMLTVConstants.Programs.ProgramStart);
130 // this.Start = start == null ? new DateTime() : ParseDate(start.Value);
131 // if (!this.Start.Equals(new DateTime())) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_start: {0}", start); }
132
133 // var stop = node.Attribute(XMLTVConstants.Programs.ProgramStop);
134 // this.Stop = stop == null ? new DateTime() : ParseDate(stop.Value);
135 // if (!this.Stop.Equals(new DateTime())) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_stop: {0}", stop); }
136
137 // var channelid = node.Attribute(XMLTVConstants.Programs.ProgramChannelId);
138 // if (!string.IsNullOrEmpty(this.Description)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_channelid: {0}", channelid); }
139 // IXMLTVChannel channel = new XMLTVChannel();
140 // string _channelid = channelid == null ? string.Empty : channelid.Value;
141 // this.Channel = this.GetInstance().Channels.Find(m => m.Id == _channelid);
142 // if (this.Channel == null) { this.Channel = new XMLTVChannel(); }
143 // if (!string.IsNullOrEmpty(_channelid)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_channel: {0}", this.Channel.ToString()); } //}
144 //try
145 //{
146 // var title = node.Descendants(XMLTVConstants.Programs.ProgramTitle).FirstOrDefault();
147 // this.Title = title == null ? string.Empty : title.Value;
148 //}
149 //catch (Exception) { this.Title = string.Empty; }
150 //if (!string.IsNullOrEmpty(this.Title)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_title: {0}", this.Title == string.Empty ? "empty" : this.Title); }
151 //try
152 //{
153 // var subtitle = node.Descendants(XMLTVConstants.Programs.ProgramSubTitle).FirstOrDefault();
154 // this.SubTitle = subtitle == null ? string.Empty : subtitle.Value;
155 //}
156 //catch (Exception) { this.SubTitle = string.Empty; }
157 //if (!string.IsNullOrEmpty(this.SubTitle)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_subtitle: {0}", this.SubTitle == string.Empty ? "empty" : this.SubTitle); }
158 //try
159 //{
160 // var description = node.Descendants(XMLTVConstants.Programs.ProgramDescription).FirstOrDefault();
161 // this.Description = description == null ? string.Empty : description.Value; ;
162 //}
163 //catch (Exception) { this.Description = string.Empty; }
164 //if (!string.IsNullOrEmpty(this.Description)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_description: {0}", this.Description == string.Empty ? "empty" : this.Description); }
165
166 //ParseExtraData(node);
167
168 var nodes = node.Elements().ToList();
169 foreach (var sub_node in nodes)
170 {
171 if (this.GetInstance().IsAborting)
172 {
173 break;
174 }
175 CreateHandlerForProgramMetaDataNode(sub_node);
176 }
177 }
178 private DateTime ParseDate(string timeStamp)
179 {
180 DateTime dt = new DateTime();
181 try
182 {
183 dt = DateTime.ParseExact(timeStamp, "yyyyMMddHHmmss zzzz", System.Globalization.CultureInfo.CurrentCulture);
184 }
185 catch (Exception ex) { throw ex; }
186 return dt;
187 }
188
189 private void CreateHandlerForProgramMetaDataNode(XElement node)
190 {
191 Type t = this.GetType();
192 Assembly asm = t.Assembly;
193 var types = asm.GetTypes();
194 var classes = types.ToList().FindAll(
195 m =>
196 m.DeclaringType == t &&
197 m.IsClass &&
198 !m.IsSealed
199 );
200 classes.TrimExcess();
201
202 object raw_instance = null;
203 BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
204 CultureInfo culture = CultureInfo.CurrentCulture;
205 Type handler_type = null;
206 foreach (var type in classes)
207 {
208 if (type.BaseType != null && type.BaseType == typeof(XMLTVBase<XMLTVProgram>))
209 {
210 var iface = type.GetInterface("IXMLTVHandler", true);
211 if (iface != null)
212 {
213 var handler_prop = type.GetProperty("Handler");
214 if (handler_prop != null)
215 {
216 var ctors = type.GetConstructors(flags);
217 bool has_default_ctor = false;
218 foreach (var ctor in ctors) { if (ctor.GetParameters().Count() == 0) { has_default_ctor = true; } }
219 if (!has_default_ctor) { continue; }
220 raw_instance = Activator.CreateInstance(type, flags, null, new object[0], culture);
221 if (raw_instance != null)
222 {
223 object handler_value = handler_prop.GetValue(raw_instance, null);
224 if (handler_value != null && handler_value.ToString() == node.Name.ToString())
225 {
226 handler_type = type;
227 break;
228 }
229 }
230 }
231 }
232 }
233 }
234 if (handler_type == null)
235 {
236 StringBuilder node_builder = new StringBuilder();
237 node_builder.AppendFormat("<{0} ", node.Name);
238 if (node.HasAttributes) { foreach (var attribute in node.Attributes()) { node_builder.AppendFormat("{0}=\"{1}\" ", attribute.Name, attribute.Value); } }
239 string node_text = string.Format("{0}>", node_builder.ToString().TrimEnd(new char[] { ' ' }));
240 xmltv_logger.Verbose.Warn.WriteLine("Ignoring unhandled extra meta-data: {0}", node_text);
241 }
242 raw_instance = Activator.CreateInstance(handler_type, flags, null, new object[] { this, node }, culture);
243 }
244
245
246
247 #region sub-classes
248 private class title : XMLTVBase<XMLTVProgram>
249 {
250 public title() : base(null, XMLTVConstants.Programs.ProgramTitle) { }
251 public title(XMLTVProgram instance, XElement node)
252 : base(instance, XMLTVConstants.Programs.ProgramTitle)
253 {
254 }
255 }
256 #endregion
257 }
258 }

  ViewVC Help
Powered by ViewVC 1.1.22