/[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 52 by william, Fri Mar 8 07:01:37 2013 UTC revision 56 by william, Fri Mar 8 12:05:33 2013 UTC
# Line 15  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(params object[] args) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(args); }          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 31  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()
59            {
60                instance = new CLASS();
61            }
62            public XMLTV(object raw_instance, EventHandler<EventArgs> handler)
63            {
64                instance = (CLASS)Convert.ChangeType(raw_instance, typeof(CLASS));
65                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)          public XMLTV(params object[] args)
76          {          {
77              ////instance = new CLASS(xml_file, t);              
78              //instance = null;              BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
   
             //Type type = typeof(CLASS);  
             //var ctors = type.GetConstructors();  
             //foreach (var ctor in ctors)  
             //{  
             //    var ctor_params = ctor.GetParameters();  
             //    if (ctor_params.Count() == 2)  
             //    {  
             //        if (ctor_params[0].ParameterType == typeof(string))  
             //        {  
             //            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;  
             //            }  
             //        }  
             //    }  
             //    else { continue; }  
             //}  
             BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance;  
79              CultureInfo culture = CultureInfo.CurrentCulture;              CultureInfo culture = CultureInfo.CurrentCulture;
80              try              try
81              {              {
# Line 68  namespace libxmltv.Core Line 84  namespace libxmltv.Core
84              }              }
85              catch (Exception ex)              catch (Exception ex)
86              {              {
87                  xmltv_logger.Log.Error.WriteLine(ex.ToString());                  xmltv_logger.Error.WriteLine(ex.ToString());
88    
89                  StringBuilder parameter_builder = new StringBuilder();                  StringBuilder parameter_builder = new StringBuilder();
90                  foreach (object arg in args)                  foreach (object arg in args)
# Line 109  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.52  
changed lines
  Added in v.56

  ViewVC Help
Powered by ViewVC 1.1.22