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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 96 - (hide annotations) (download)
Sun Mar 10 09:10:42 2013 UTC (6 years, 3 months ago) by william
Original Path: trunk/libxmltv/Core/XMLTVProgram.cs
File size: 12598 byte(s)
+ only parse containg meta-data elements, don't parse the inner content ... that would be up to a parser class to handle.

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

  ViewVC Help
Powered by ViewVC 1.1.22