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

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

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

revision 51 by william, Fri Mar 8 06:39:56 2013 UTC revision 56 by william, Fri Mar 8 12:05:33 2013 UTC
# Line 5  using System.Text; Line 5  using System.Text;
5  using libxmltv.Interfaces;  using libxmltv.Interfaces;
6  using System.ComponentModel;  using System.ComponentModel;
7  using System.IO;  using System.IO;
8    using System.Reflection;
9    using System.Globalization;
10    
11  namespace libxmltv.Core  namespace libxmltv.Core
12  {  {
# Line 13  namespace libxmltv.Core Line 15  namespace libxmltv.Core
15      /// </summary>      /// </summary>
16      public static class XMLTV      public static class XMLTV
17      {      {
18          static XMLTV() { xmltv_logger.Initialize(); }          static XMLTV()
19            {
20                xmltv_logger.Initialize();
21                if (instance == null)
22                {
23                    instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>();
24                }
25            }
26    
27          static IXMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance> instance;          static IXMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance> instance;
28          public static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); }          public static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); }
29          public static void CreateInstance(string xml_file) { CreateInstance(xml_file, null); }          public static void CreateInstance(params object[] args) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(args); }
         public static void CreateInstance(string xml_file, EventHandler<CancelEventArgs> t) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(xml_file, t); }  
         //public static IXMLTVSerializer<IXMLTVRuntimeInstance> GetSerializer() {return new XMLTVSerializer<IXMLTVRuntimeInstance>(InternalGetInstance()); }  
30    
31          //private static IXMLTVSerializer<T> CreateSerializer<T>() where T : class { return new XMLTVSerializer<T>(InternalGetInstance() as T); }          public static void CreateFromInstance(object raw_instance, EventHandler<EventArgs> handler)
32            {
33                instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(raw_instance,handler);
34                if (OnInstanceCreated != null)
35                {
36                    OnInstanceCreated.Invoke(null, new EventArgs());
37                }
38            }
39    
40          #region IXMLTVSerializer<IXMLTVRuntimeInstance> members          #region IXMLTVSerializer<IXMLTVRuntimeInstance> members
41          public static bool Serialize(string file) { return instance.Serialize(file); }          public static bool Serialize(string file) { return instance.Serialize(file); }
# Line 29  namespace libxmltv.Core Line 43  namespace libxmltv.Core
43          public static IXMLTVRuntimeInstance DeSerialize(string file, out bool status) { return instance.DeSerialize(file, out status); }          public static IXMLTVRuntimeInstance DeSerialize(string file, out bool status) { return instance.DeSerialize(file, out status); }
44          public static IXMLTVRuntimeInstance DeSerialize(Stream stream, out bool status) { return instance.DeSerialize(stream, out status); }          public static IXMLTVRuntimeInstance DeSerialize(Stream stream, out bool status) { return instance.DeSerialize(stream, out status); }
45          #endregion          #endregion
46    
47            public static void DestroyInstance() { instance.DestroyInstance(); }        
48    
49            public static EventHandler<EventArgs> OnInstanceCreated
50            {
51                get { return instance.OnInstanceCreated; }
52                set { instance.OnInstanceCreated = value; }
53            }
54      }      }
55    
56      internal class XMLTV<INTERFACE, CLASS> : IXMLTV<INTERFACE, CLASS> where CLASS : class,INTERFACE      internal class XMLTV<INTERFACE, CLASS> : IDestroyInstance, IXMLTV<INTERFACE, CLASS> where CLASS : class,INTERFACE,new()
57      {      {
58          public XMLTV(string xml_file) : this(xml_file, null) { }          public XMLTV()
         public XMLTV(string xml_file, EventHandler<CancelEventArgs> t)  
59          {          {
60              //instance = new CLASS(xml_file, t);              instance = new CLASS();
61              instance = null;          }
62            public XMLTV(object raw_instance, EventHandler<EventArgs> handler)
63              Type type = typeof(CLASS);          {
64              var ctors = type.GetConstructors();              instance = (CLASS)Convert.ChangeType(raw_instance, typeof(CLASS));
65              foreach (var ctor in ctors)              if (instance != null)
66                {
67                    IRuntimeInstanceLoader<CLASS> loader = (instance as IRuntimeInstanceLoader<CLASS>);
68                    if (loader != null)
69                    {
70                        SetOnInstanceCreated(handler);
71                        instance = loader.LoadFromInstance(instance);
72                    }
73                }
74            }
75            public XMLTV(params object[] args)
76            {
77                
78                BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
79                CultureInfo culture = CultureInfo.CurrentCulture;
80                try
81              {              {
82                  var ctor_params = ctor.GetParameters();                  object raw_instance = Activator.CreateInstance(typeof(CLASS), flags, null, args, culture);
83                  if (ctor_params.Count() == 2)                  instance = (CLASS)Convert.ChangeType(raw_instance, typeof(CLASS));
84                }
85                catch (Exception ex)
86                {
87                    xmltv_logger.Error.WriteLine(ex.ToString());
88    
89                    StringBuilder parameter_builder = new StringBuilder();
90                    foreach (object arg in args)
91                  {                  {
92                      if (ctor_params[0].ParameterType == typeof(string))                      Type type = arg.GetType();
93                      {                      parameter_builder.AppendFormat("({0}), ", type.FullName);
                         if (ctor_params[1].ParameterType == typeof(EventHandler<CancelEventArgs>))  
                         {  
                             object o = ctor.Invoke(new object[] { xml_file, t });  
                             instance = (CLASS)Convert.ChangeType(o, typeof(CLASS));  
                             break;  
                         }  
                     }  
94                  }                  }
95                  else { continue; }  
96                    string type_parameters = parameter_builder.ToString().TrimEnd(new char[] { ',', ' ' });
97                    throw new Exception(string.Format("Unable to create instance of: '{0}' with parameters: '{1}'", typeof(CLASS).Name, type_parameters), ex);
98              }              }
99          }          }
100    
# Line 87  namespace libxmltv.Core Line 125  namespace libxmltv.Core
125          public INTERFACE DeSerialize(string file, out bool status) { return CreateSerializer().DeSerialize(file, out status); }          public INTERFACE DeSerialize(string file, out bool status) { return CreateSerializer().DeSerialize(file, out status); }
126          public INTERFACE DeSerialize(Stream stream, out bool status) { return CreateSerializer().DeSerialize(stream, out status); }          public INTERFACE DeSerialize(Stream stream, out bool status) { return CreateSerializer().DeSerialize(stream, out status); }
127          #endregion          #endregion
128            public void DestroyInstance()
129            {
130                IDestroyInstance destoyer = (instance as IDestroyInstance);
131                if (destoyer != null)
132                {
133                    destoyer.DestroyInstance();
134                }
135                else
136                {
137                    xmltv_logger.Error.WriteLine("Unable to call DestroyInstance() on type: '{0}'", instance.GetType().Name);
138                }            
139            }
140            private void SetOnInstanceCreated(EventHandler<EventArgs> event_instance)
141            {
142                ISetCreatedInstanceEvent setter = (instance as ISetCreatedInstanceEvent);
143                if (setter != null) { setter.SetOnInstanceCreated(event_instance); }
144            }
145            private EventHandler<EventArgs> GetOnInstanceCreated()
146            {
147                IGetCreatedInstanceEvent getter = (instance as IGetCreatedInstanceEvent);
148                if (getter != null) { return getter.GetOnInstanceCreated(); }
149                return null;
150            }
151            public EventHandler<EventArgs> OnInstanceCreated
152            {
153                get { return GetOnInstanceCreated(); }
154                set { SetOnInstanceCreated(value); }
155            }
156      }      }
157    
158  }  }

Legend:
Removed from v.51  
changed lines
  Added in v.56

  ViewVC Help
Powered by ViewVC 1.1.22