/[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 117 by william, Sun Mar 10 17:10:47 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(); }          internal static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); }
29          public static void CreateInstance(params object[] args) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(args); }          internal 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); }          internal 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); }          internal static bool Serialize(string file) { return instance.Serialize(file); }
42          public static bool Serialize(Stream stream) { return instance.Serialize(stream); }          internal static bool Serialize(Stream stream) { return instance.Serialize(stream); }
43          public static IXMLTVRuntimeInstance DeSerialize(string file, out bool status) { return instance.DeSerialize(file, out status); }          internal static bool DeSerialize(string file)
44          public static IXMLTVRuntimeInstance DeSerialize(Stream stream, out bool status) { return instance.DeSerialize(stream, out status); }          {
45                bool status;
46                var gInstance = instance.DeSerialize(file, out status);
47                ISetInstance<IXMLTVRuntimeInstance> setter = (instance as ISetInstance<IXMLTVRuntimeInstance>);
48                if (setter != null) { setter.SetInstance(gInstance); }
49                return status;
50            }
51            internal static bool DeSerialize(Stream stream)
52            {
53                bool status;
54                var gInstance = instance.DeSerialize(stream, out status);
55                ISetInstance<IXMLTVRuntimeInstance> setter = (instance as ISetInstance<IXMLTVRuntimeInstance>);
56                if (setter != null) { setter.SetInstance(gInstance); }
57                return status;
58            }
59            #endregion
60            internal static void DestroyInstance() { instance.DestroyInstance(); }
61    
62            internal static EventHandler<EventArgs> OnInstanceCreated
63            {
64                get { return instance.OnInstanceCreated; }
65                set { instance.OnInstanceCreated = value; }
66            }
67    
68            #region public members
69            public static bool Save(string file) { return Serialize(file); }
70            public static bool Load(string file) { return DeSerialize(file); }
71            public static void Create(params object[] args) { CreateInstance(args); }
72            public static void Destroy() { DestroyInstance(); }
73          #endregion          #endregion
74      }      }
75    
76      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()
77      {      {
78            public XMLTV()
79            {
80                instance = new CLASS();
81            }
82            public XMLTV(object raw_instance, EventHandler<EventArgs> handler)
83            {
84                instance = (CLASS)Convert.ChangeType(raw_instance, typeof(CLASS));
85                if (instance != null)
86                {
87                    IRuntimeInstanceLoader<CLASS> loader = (instance as IRuntimeInstanceLoader<CLASS>);
88                    if (loader != null)
89                    {
90                        SetOnInstanceCreated(handler);
91                        instance = loader.LoadFromInstance(instance);
92                    }
93                }
94            }
95          public XMLTV(params object[] args)          public XMLTV(params object[] args)
96          {          {
97              ////instance = new CLASS(xml_file, t);              
98              //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;  
99              CultureInfo culture = CultureInfo.CurrentCulture;              CultureInfo culture = CultureInfo.CurrentCulture;
100              try              try
101              {              {
# Line 68  namespace libxmltv.Core Line 104  namespace libxmltv.Core
104              }              }
105              catch (Exception ex)              catch (Exception ex)
106              {              {
107                  xmltv_logger.Log.Error.WriteLine(ex.ToString());                  xmltv_logger.Error.WriteLine(ex.ToString());
108    
109                  StringBuilder parameter_builder = new StringBuilder();                  StringBuilder parameter_builder = new StringBuilder();
110                  foreach (object arg in args)                  foreach (object arg in args)
# Line 101  namespace libxmltv.Core Line 137  namespace libxmltv.Core
137          {          {
138              return (INTERFACE)instance;              return (INTERFACE)instance;
139          }          }
140            public void SetInstance(INTERFACE gInstance)
141            {
142                this.instance = gInstance as CLASS;
143            }
144          #endregion          #endregion
145    
146          #region IXMLTVSerializer<INTERFACE> members          #region IXMLTVSerializer<INTERFACE> members
# Line 109  namespace libxmltv.Core Line 149  namespace libxmltv.Core
149          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); }
150          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); }
151          #endregion          #endregion
152            public void DestroyInstance()
153            {
154                IDestroyInstance destoyer = (instance as IDestroyInstance);
155                if (destoyer != null)
156                {
157                    destoyer.DestroyInstance();
158                }
159                else
160                {
161                    xmltv_logger.Error.WriteLine("Unable to call DestroyInstance() on type: '{0}'", instance.GetType().Name);
162                }            
163            }
164            private void SetOnInstanceCreated(EventHandler<EventArgs> event_instance)
165            {
166                ISetCreatedInstanceEvent setter = (instance as ISetCreatedInstanceEvent);
167                if (setter != null) { setter.SetOnInstanceCreated(event_instance); }
168            }
169            private EventHandler<EventArgs> GetOnInstanceCreated()
170            {
171                IGetCreatedInstanceEvent getter = (instance as IGetCreatedInstanceEvent);
172                if (getter != null) { return getter.GetOnInstanceCreated(); }
173                return null;
174            }
175            public EventHandler<EventArgs> OnInstanceCreated
176            {
177                get { return GetOnInstanceCreated(); }
178                set { SetOnInstanceCreated(value); }
179            }
180      }      }
181    
182  }  }

Legend:
Removed from v.52  
changed lines
  Added in v.117

  ViewVC Help
Powered by ViewVC 1.1.22