/[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

--- trunk/libxmltv/Core/XMLTV.cs	2013/03/08 05:20:12	49
+++ trunk/libxmltv/Core/XMLTV.cs	2013/03/08 06:15:44	50
@@ -4,6 +4,7 @@
 using System.Text;
 using libxmltv.Interfaces;
 using System.ComponentModel;
+using System.IO;
 
 namespace libxmltv.Core
 {
@@ -14,13 +15,68 @@
     {
         static XMLTV() { xmltv_logger.Initialize(); }
 
-        private static XMLTVRuntimeInstance instance;
-        public static IXMLTVRuntimeInstance GetInstance() { return InternalGetInstance(); }
-        internal static XMLTVRuntimeInstance InternalGetInstance() { return instance; }
+        static IXMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance> instance;
+        public static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); }
         public static void CreateInstance(string xml_file) { CreateInstance(xml_file, null); }
-        public static void CreateInstance(string xml_file, EventHandler<CancelEventArgs> t) { instance = new XMLTVRuntimeInstance(xml_file, t); }
-        public static IXMLTVSerializer<IXMLTVRuntimeInstance> GetSerializer() {return InternalGetInstance().Serializer; }
+        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()); }
+
+        //private static IXMLTVSerializer<T> CreateSerializer<T>() where T : class { return new XMLTVSerializer<T>(InternalGetInstance() as T); }
+
+        #region IXMLTVSerializer<IXMLTVRuntimeInstance> members
+        public static bool Serialize(string file) { return instance.Serialize(file); }
+        public static bool Serialize(Stream stream) { return instance.Serialize(stream); }
+        public static IXMLTVRuntimeInstance DeSerialize(string file, out bool status) { return instance.DeSerialize(file, out status); }
+        public static IXMLTVRuntimeInstance DeSerialize(Stream stream, out bool status) { return instance.DeSerialize(stream, out status); }
+        #endregion
     }
+
+    internal class XMLTV<INTERFACE, CLASS> : IXMLTV<INTERFACE, CLASS> where CLASS : class,INTERFACE
+    {
+        public XMLTV(string xml_file) : this(xml_file, null) { }
+        public XMLTV(string xml_file, EventHandler<CancelEventArgs> t)
+        {
+            //instance = new CLASS(xml_file, t); 
+            instance = null;
+
+            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; }
+            }
+        }
+
+        private CLASS instance;
+        #region IXMLTV<T> members
+        private IXMLTVSerializer<INTERFACE> CreateSerializer() { return new XMLTVSerializer<INTERFACE>(instance); }
+        public INTERFACE GetInstance()
+        {
+            return (INTERFACE)instance;
+        }
+        #endregion
+
+        #region IXMLTVSerializer<INTERFACE> members
+        public bool Serialize(string file) { return CreateSerializer().Serialize(file); }
+        public bool Serialize(Stream stream) { return CreateSerializer().Serialize(stream); }
+        public INTERFACE DeSerialize(string file, out bool status) { return CreateSerializer().DeSerialize(file, out status); }
+        public INTERFACE DeSerialize(Stream stream, out bool status) { return CreateSerializer().DeSerialize(stream, out status); }
+        #endregion
+    }
+
 }
 
 

 

  ViewVC Help
Powered by ViewVC 1.1.22