/[xmltv_parser]/trunk/libxmltv/Core/XMLTVRuntimeInstance.cs
ViewVC logotype

Diff of /trunk/libxmltv/Core/XMLTVRuntimeInstance.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 72 by william, Sat Mar 9 09:29:40 2013 UTC revision 78 by william, Sat Mar 9 11:36:24 2013 UTC
# Line 9  using System.Threading; Line 9  using System.Threading;
9  using System.Reflection;  using System.Reflection;
10  using System.Globalization;  using System.Globalization;
11  using System.Diagnostics;  using System.Diagnostics;
12    using System.Xml.Linq;
13    
14  namespace libxmltv.Core  namespace libxmltv.Core
15  {  {
# Line 40  namespace libxmltv.Core Line 41  namespace libxmltv.Core
41              }              }
42          }          }
43                    
   
44          private void OnCreatedInstance(object sender, EventArgs e)          private void OnCreatedInstance(object sender, EventArgs e)
45          {          {
46              if (OnInstanceCreated != null) { OnInstanceCreated.Invoke(sender, e); }              if (OnInstanceCreated != null) { OnInstanceCreated.Invoke(sender, e); }
# Line 49  namespace libxmltv.Core Line 49  namespace libxmltv.Core
49          private void init()          private void init()
50          {          {
51              this.Source = new XMLTVSource();              this.Source = new XMLTVSource();
52              this.Channels = new Dictionary<string, IXMLTVChannel>();              this.Channels = new List<IXMLTVChannel>();
53              this.Programs = new Dictionary<int, IXMLTVProgram>();              this.Programs = new List<IXMLTVProgram>();
54              this.XmlFile_Name = string.Empty;              this.XmlFile_Name = string.Empty;
55              this.XmlFile_FullName = string.Empty;              this.XmlFile_FullName = string.Empty;
56              this.XmlDoc = string.Empty;              this.XmlDoc = string.Empty;
# Line 68  namespace libxmltv.Core Line 68  namespace libxmltv.Core
68          public string XmlFile_FullName { get { return _XmlFile_FullName; } set { _XmlFile_FullName = value; } }          public string XmlFile_FullName { get { return _XmlFile_FullName; } set { _XmlFile_FullName = value; } }
69    
70          private string _XmlDoc;          private string _XmlDoc;
71          public string XmlDoc { get { return _XmlDoc; } set { _XmlDoc = value; } }                        public string XmlDoc { get { return _XmlDoc; } set { _XmlDoc = value; } }
72          private IXMLTVSource _Source;          private IXMLTVSource _Source;
73          public IXMLTVSource Source { get { return _Source; } set { _Source = value; } }          public IXMLTVSource Source { get { return _Source; } set { _Source = value; } }
74          private Dictionary<string, IXMLTVChannel> _Channels;          private List<IXMLTVChannel> _Channels;
75          public Dictionary<string, IXMLTVChannel> Channels { get { return _Channels; } set { _Channels = value; } }          public List<IXMLTVChannel> Channels { get { return _Channels; } set { _Channels = value; } }
76          private Dictionary<int, IXMLTVProgram> _Programs;          private List<IXMLTVProgram> _Programs;
77          public Dictionary<int, IXMLTVProgram> Programs { get { return _Programs; } set { _Programs = value; } }          public List<IXMLTVProgram> Programs { get { return _Programs; } set { _Programs = value; } }
78          #endregion                #endregion      
79          #region IOnInstanceCreated members          #region IOnInstanceCreated members
80          [NonSerialized]          [NonSerialized]
# Line 118  namespace libxmltv.Core Line 118  namespace libxmltv.Core
118              {              {
119                  throw new NullReferenceException("Failed to load from instance because the instance is null.");                  throw new NullReferenceException("Failed to load from instance because the instance is null.");
120              }              }
             CloneFromInstance(ref instance);  
121              xmltv_logger.Debug.WriteLine("Loading from instance...");              xmltv_logger.Debug.WriteLine("Loading from instance...");
122                CloneFromInstance(ref instance);            
123                xmltv_logger.Debug.WriteLine("Loaded from instance...");
124              if (this.Source != null)              if (this.Source != null)
125              {              {
126                  xmltv_logger.Info.WriteLine("Source Loaded: '{0}' Created by '{1}' - original source file: '{2}'", this.Source.SourceName, this.Source.GeneratorName, this.XmlFile_FullName);                  xmltv_logger.Info.WriteLine("Source Loaded: '{0}' Created by '{1}' - original source file: '{2}'", this.Source.SourceName, this.Source.GeneratorName, this.XmlFile_FullName);
# Line 146  namespace libxmltv.Core Line 147  namespace libxmltv.Core
147              {              {
148                  xmltv_logger.Error.WriteLine("Programs Property is null.");                  xmltv_logger.Error.WriteLine("Programs Property is null.");
149                  throw new NullReferenceException("Programs Property is null.");                  throw new NullReferenceException("Programs Property is null.");
150              }              }            
             xmltv_logger.Debug.WriteLine("Loaded from instance...");  
151              return instance;              return instance;
152          }          }
153          #endregion          #endregion
# Line 218  namespace libxmltv.Core Line 218  namespace libxmltv.Core
218    
219      internal class XMLTVInstance : IGetInstance<XMLTVRuntimeInstance>, IDisposable      internal class XMLTVInstance : IGetInstance<XMLTVRuntimeInstance>, IDisposable
220      {      {
221            public void Dispose()
222            {
223                //throw new NotImplementedException();
224            }
225    
226          private XMLTVRuntimeInstance gInstance;          private XMLTVRuntimeInstance gInstance;
227          public XMLTVRuntimeInstance GetInstance() { return gInstance; }          public XMLTVRuntimeInstance GetInstance() { return gInstance; }
228          public XMLTVInstance(string xmlfile)          public XMLTVInstance(string xmlfile)
# Line 225  namespace libxmltv.Core Line 230  namespace libxmltv.Core
230              try              try
231              {              {
232                  CreateLoader(xmlfile);                  CreateLoader(xmlfile);
233                    CreateParser();
234              }              }
235              catch (Exception ex)              catch (Exception ex)
236              {              {
# Line 266  namespace libxmltv.Core Line 272  namespace libxmltv.Core
272              if (getter_instance != null) { gInstance = getter_instance.GetInstance(); }              if (getter_instance != null) { gInstance = getter_instance.GetInstance(); }
273              else { throw new Exception("Found a compatible XMLTV Data Loader, but was unable to obtain the instance holding the data"); }              else { throw new Exception("Found a compatible XMLTV Data Loader, but was unable to obtain the instance holding the data"); }
274          }          }
275          private void CreateParser(XMLTVRuntimeInstance instance)          private void CreateParser()
276          {          {
277                var doc = XDocument.Parse(this.GetInstance().XmlDoc);
278                var root_element = doc.Root;            
279                CreateHandlerForRootNode(root_element);
280                var nodes = doc.Root.Elements().ToList();
281    
282                double total_nodes = nodes.Count;
283                double node_index = 0;
284                double progress = 0;
285                foreach(var node in nodes)
286                {
287                    if (this.GetInstance().IsAborting)
288                    {
289                        break;
290                    }
291                    if (!CreateHandlerForNode(node)) { xmltv_logger.Verbose.Debug.WriteLine("Unable to create handler for node: '{0}'", node.Name.ToString()); }
292                    node_index++;
293                    progress = 100.0 * (node_index / total_nodes);
294                    xmltv_logger.ReportProgress(this, new Enterprise.Logging.ReportProgressEventArgs((int)progress));
295                    Application.DoEvents();
296                }
297            }
298    
299    
300            private void CreateHandlerForRootNode(XElement root)
301            {                        
302                if (root == null) { throw new NullReferenceException("Root element is null"); }
303                if (root.Name == null) { throw new NullReferenceException("Root element's Name is null"); }
304                var root_name =  root.Name.ToString();
305                xmltv_logger.Verbose.Debug.WriteLine("Creating handler for root: '{0}'", root_name.ToString());
306                object raw_instance = null;
307                BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
308                CultureInfo culture = CultureInfo.CurrentCulture;
309                Assembly asm = Assembly.GetExecutingAssembly();
310                var types = asm.GetTypes();
311                Type handler_type = null;
312                foreach (var type in types)
313                {
314                    if (type.BaseType != null && type.BaseType == typeof(XMLTVBase<XMLTVRuntimeInstance>))
315                    {
316                        try
317                        {
318                            var handler_prop = type.GetProperty("Handler");
319                            if (handler_prop != null)
320                            {
321                                raw_instance = Activator.CreateInstance(type, flags, null, new object[0], culture);
322                                if (raw_instance != null)
323                                {
324                                    object handler_value = handler_prop.GetValue(raw_instance, null);
325                                    if (handler_value != null && handler_value.ToString() == root_name)
326                                    {
327                                        handler_type = type;
328                                        break;
329                                    }
330                                }                            
331                            }
332                        }
333                        catch (Exception) { }
334                    }
335                }
336                if (handler_type == null) { throw new Exception("Unable to find a compatible handler to parse document root."); }
337                xmltv_logger.Verbose.Debug.WriteLine("Created handler for root: '{0}'", root_name.ToString());
338                raw_instance = Activator.CreateInstance(handler_type, flags, null, new object[] { gInstance }, culture);
339          }          }
340    
341          public void Dispose()          private bool CreateHandlerForNode(XElement node)
342          {          {
343              //throw new NotImplementedException();              if (node == null) { throw new NullReferenceException("Node element is null"); }
344          }              if (node.Name == null) { throw new NullReferenceException("Node element's Name is null"); }
345                var node_name = node.Name.ToString();
346    
347                xmltv_logger.Verbose.Debug.WriteLine("Creating handler for node: '{0}'", node_name.ToString());
348                object raw_instance = null;
349                BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
350                CultureInfo culture = CultureInfo.CurrentCulture;
351                Assembly asm = Assembly.GetExecutingAssembly();
352                var types = asm.GetTypes();
353                Type handler_type = null;
354                foreach (var type in types)
355                {
356                    if (type.BaseType != null && type.BaseType == typeof(XMLTVBase<XMLTVRuntimeInstance>))
357                    {
358                        try
359                        {
360                            var handler_prop = type.GetProperty("Handler");
361                            if (handler_prop != null)
362                            {
363                                raw_instance = Activator.CreateInstance(type, flags, null, new object[0], culture);
364                                if (raw_instance != null)
365                                {
366                                    object handler_value = handler_prop.GetValue(raw_instance, null);
367                                    if (handler_value != null && handler_value.ToString() == node_name)
368                                    {
369                                        handler_type = type;
370                                        break;
371                                    }
372                                }
373                            }
374                        }
375                        catch (Exception) { }
376                    }
377                }
378                if (handler_type == null) { throw new Exception("Unable to find a compatible handler to parse document root."); }
379                xmltv_logger.Verbose.Debug.WriteLine("Created handler for node: '{0}'", node_name.ToString());
380                raw_instance = Activator.CreateInstance(handler_type, flags, null, new object[] { gInstance, node }, culture);
381                return true;
382            }
383                    
384      }      }
385  }  }

Legend:
Removed from v.72  
changed lines
  Added in v.78

  ViewVC Help
Powered by ViewVC 1.1.22