/[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 11:08:48	23
+++ trunk/libxmltv/Core/XMLTV.cs	2013/03/08 12:05:33	56
@@ -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,23 +15,146 @@
     /// </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();
+            if (instance == null)
+            {
+                instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>();
+            }
+        }
+
+        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 CreateFromInstance(object raw_instance, EventHandler<EventArgs> handler) 
+        { 
+            instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(raw_instance,handler);
+            if (OnInstanceCreated != null)
+            {
+                OnInstanceCreated.Invoke(null, new EventArgs());
+            }
+        }
+
+        #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
+
+        public static void DestroyInstance() { instance.DestroyInstance(); }        
+
+        public static EventHandler<EventArgs> OnInstanceCreated
+        {
+            get { return instance.OnInstanceCreated; }
+            set { instance.OnInstanceCreated = value; }
+        }
+    }
+
+    internal class XMLTV<INTERFACE, CLASS> : IDestroyInstance, IXMLTV<INTERFACE, CLASS> where CLASS : class,INTERFACE,new()
+    {
+        public XMLTV()
+        {
+            instance = new CLASS();
+        }
+        public XMLTV(object raw_instance, EventHandler<EventArgs> handler) 
+        {
+            instance = (CLASS)Convert.ChangeType(raw_instance, typeof(CLASS));
+            if (instance != null)
+            {
+                IRuntimeInstanceLoader<CLASS> loader = (instance as IRuntimeInstanceLoader<CLASS>);
+                if (loader != null)
+                {
+                    SetOnInstanceCreated(handler);
+                    instance = loader.LoadFromInstance(instance);
+                }
+            }
         }
-        public static object CreateParser(object xmltv)
+        public XMLTV(params object[] args)
         {
-            XMLTV_PARSER parser = new XMLTV_PARSER(xmltv);
-            return parser;
+            
+            BindingFlags flags = BindingFlags.Public | 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.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 INTERFACE GetInstance()
+        {
+            return (INTERFACE)instance;
         }
+        #endregion
 
-        public static IXMLTV_PARSER GetParser(object xmltv)
+        #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
+        public void DestroyInstance()
+        {
+            IDestroyInstance destoyer = (instance as IDestroyInstance);
+            if (destoyer != null)
+            {
+                destoyer.DestroyInstance();
+            }
+            else
+            {
+                xmltv_logger.Error.WriteLine("Unable to call DestroyInstance() on type: '{0}'", instance.GetType().Name);
+            }            
+        }
+        private void SetOnInstanceCreated(EventHandler<EventArgs> event_instance)
+        {
+            ISetCreatedInstanceEvent setter = (instance as ISetCreatedInstanceEvent);
+            if (setter != null) { setter.SetOnInstanceCreated(event_instance); }
+        }
+        private EventHandler<EventArgs> GetOnInstanceCreated()
+        {
+            IGetCreatedInstanceEvent getter = (instance as IGetCreatedInstanceEvent);
+            if (getter != null) { return getter.GetOnInstanceCreated(); }
+            return null;
+        }
+        public EventHandler<EventArgs> OnInstanceCreated
         {
-            IXMLTV_PARSER _xmltv;
-            if (!Internals.VerifyInstance<IXMLTV_PARSER>(xmltv, out _xmltv)) { return null; }
-            return _xmltv;
+            get { return GetOnInstanceCreated(); }
+            set { SetOnInstanceCreated(value); }
         }
     }
+
 }
+
+

 

  ViewVC Help
Powered by ViewVC 1.1.22