/[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/07 10:20:50	22
+++ trunk/libxmltv/Core/XMLTV.cs	2013/03/08 07:01:37	52
@@ -3,6 +3,10 @@
 using System.Linq;
 using System.Text;
 using libxmltv.Interfaces;
+using System.ComponentModel;
+using System.IO;
+using System.Reflection;
+using System.Globalization;
 
 namespace libxmltv.Core
 {
@@ -11,16 +15,102 @@
     /// </summary>
     public static class XMLTV
     {
-        static XMLTV() { XMLTV_LOGGER.Initialize(); }
-        public static object CreateLoader(string xml_file)
-        {          
-            XMLTV_LOADER loader = new XMLTV_LOADER(xml_file);
-            return loader;
+        static XMLTV() { xmltv_logger.Initialize(); }
+
+        static IXMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance> instance;
+        public static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); }
+        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()); }
+
+        //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(params object[] args)
+        {
+            ////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; }
+            //}
+            BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance;
+            CultureInfo culture = CultureInfo.CurrentCulture;
+            try
+            {
+                object raw_instance = Activator.CreateInstance(typeof(CLASS), flags, null, args, culture);
+                instance = (CLASS)Convert.ChangeType(raw_instance, typeof(CLASS));
+            }
+            catch (Exception ex)
+            {
+                xmltv_logger.Log.Error.WriteLine(ex.ToString());
+
+                StringBuilder parameter_builder = new StringBuilder();
+                foreach (object arg in args)
+                {
+                    Type type = arg.GetType();
+                    parameter_builder.AppendFormat("({0}), ", type.FullName);
+                }
+
+                string type_parameters = parameter_builder.ToString().TrimEnd(new char[] { ',', ' ' });
+                throw new Exception(string.Format("Unable to create instance of: '{0}' with parameters: '{1}'", typeof(CLASS).Name, type_parameters), ex);
+            }
+        }
+
+        private CLASS instance;
+        #region IXMLTV<T> members
+        public IXMLTVSerializer<CLASS> CreateSerializer() 
+        {
+            // we must serialize on the CLASS type, using the INTERFACE type is syntatically incorrect
+            ISerializer<CLASS> class_serializer = (instance as ISerializer<CLASS>);
+            if (class_serializer != null)
+            {
+                return class_serializer.Serializer;
+            }
+            else
+            {
+                return new XMLTVSerializer<CLASS>(instance);
+            }
         }
-        public static object CreateParser(object xmltv)
+        public INTERFACE GetInstance()
         {
-            XMLTV_PARSER parser = new XMLTV_PARSER(xmltv);
-            return parser;
+            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