/[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 49 by william, Fri Mar 8 05:20:12 2013 UTC revision 52 by william, Fri Mar 8 07:01:37 2013 UTC
# Line 4  using System.Linq; Line 4  using System.Linq;
4  using System.Text;  using System.Text;
5  using libxmltv.Interfaces;  using libxmltv.Interfaces;
6  using System.ComponentModel;  using System.ComponentModel;
7    using System.IO;
8    using System.Reflection;
9    using System.Globalization;
10    
11  namespace libxmltv.Core  namespace libxmltv.Core
12  {  {
# Line 14  namespace libxmltv.Core Line 17  namespace libxmltv.Core
17      {      {
18          static XMLTV() { xmltv_logger.Initialize(); }          static XMLTV() { xmltv_logger.Initialize(); }
19    
20          private static XMLTVRuntimeInstance instance;          static IXMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance> instance;
21          public static IXMLTVRuntimeInstance GetInstance() { return InternalGetInstance(); }          public static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); }
22          internal static XMLTVRuntimeInstance InternalGetInstance() { return instance; }          public static void CreateInstance(params object[] args) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(args); }
23          public static void CreateInstance(string xml_file) { CreateInstance(xml_file, null); }          //public static void CreateInstance(string xml_file, EventHandler<CancelEventArgs> t) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(xml_file, t); }
24          public static void CreateInstance(string xml_file, EventHandler<CancelEventArgs> t) { instance = new XMLTVRuntimeInstance(xml_file, t); }          //public static IXMLTVSerializer<IXMLTVRuntimeInstance> GetSerializer() {return new XMLTVSerializer<IXMLTVRuntimeInstance>(InternalGetInstance()); }
25          public static IXMLTVSerializer<IXMLTVRuntimeInstance> GetSerializer() {return InternalGetInstance().Serializer; }  
26            //private static IXMLTVSerializer<T> CreateSerializer<T>() where T : class { return new XMLTVSerializer<T>(InternalGetInstance() as T); }
27    
28            #region IXMLTVSerializer<IXMLTVRuntimeInstance> members
29            public static bool Serialize(string file) { return instance.Serialize(file); }
30            public static bool Serialize(Stream stream) { return instance.Serialize(stream); }
31            public static IXMLTVRuntimeInstance DeSerialize(string file, out bool status) { return instance.DeSerialize(file, out status); }
32            public static IXMLTVRuntimeInstance DeSerialize(Stream stream, out bool status) { return instance.DeSerialize(stream, out status); }
33            #endregion
34      }      }
35    
36        internal class XMLTV<INTERFACE, CLASS> : IXMLTV<INTERFACE, CLASS> where CLASS : class,INTERFACE
37        {
38            public XMLTV(params object[] args)
39            {
40                ////instance = new CLASS(xml_file, t);
41                //instance = null;
42    
43                //Type type = typeof(CLASS);
44                //var ctors = type.GetConstructors();
45                //foreach (var ctor in ctors)
46                //{
47                //    var ctor_params = ctor.GetParameters();
48                //    if (ctor_params.Count() == 2)
49                //    {
50                //        if (ctor_params[0].ParameterType == typeof(string))
51                //        {
52                //            if (ctor_params[1].ParameterType == typeof(EventHandler<CancelEventArgs>))
53                //            {
54                //                object o = ctor.Invoke(new object[] { xml_file, t });
55                //                instance = (CLASS)Convert.ChangeType(o, typeof(CLASS));
56                //                break;
57                //            }
58                //        }
59                //    }
60                //    else { continue; }
61                //}
62                BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance;
63                CultureInfo culture = CultureInfo.CurrentCulture;
64                try
65                {
66                    object raw_instance = Activator.CreateInstance(typeof(CLASS), flags, null, args, culture);
67                    instance = (CLASS)Convert.ChangeType(raw_instance, typeof(CLASS));
68                }
69                catch (Exception ex)
70                {
71                    xmltv_logger.Log.Error.WriteLine(ex.ToString());
72    
73                    StringBuilder parameter_builder = new StringBuilder();
74                    foreach (object arg in args)
75                    {
76                        Type type = arg.GetType();
77                        parameter_builder.AppendFormat("({0}), ", type.FullName);
78                    }
79    
80                    string type_parameters = parameter_builder.ToString().TrimEnd(new char[] { ',', ' ' });
81                    throw new Exception(string.Format("Unable to create instance of: '{0}' with parameters: '{1}'", typeof(CLASS).Name, type_parameters), ex);
82                }
83            }
84    
85            private CLASS instance;
86            #region IXMLTV<T> members
87            public IXMLTVSerializer<CLASS> CreateSerializer()
88            {
89                // we must serialize on the CLASS type, using the INTERFACE type is syntatically incorrect
90                ISerializer<CLASS> class_serializer = (instance as ISerializer<CLASS>);
91                if (class_serializer != null)
92                {
93                    return class_serializer.Serializer;
94                }
95                else
96                {
97                    return new XMLTVSerializer<CLASS>(instance);
98                }
99            }
100            public INTERFACE GetInstance()
101            {
102                return (INTERFACE)instance;
103            }
104            #endregion
105    
106            #region IXMLTVSerializer<INTERFACE> members
107            public bool Serialize(string file) { return CreateSerializer().Serialize(file); }
108            public bool Serialize(Stream stream) { return CreateSerializer().Serialize(stream); }
109            public INTERFACE DeSerialize(string file, out bool status) { return CreateSerializer().DeSerialize(file, out status); }
110            public INTERFACE DeSerialize(Stream stream, out bool status) { return CreateSerializer().DeSerialize(stream, out status); }
111            #endregion
112        }
113    
114  }  }
115    
116    

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

  ViewVC Help
Powered by ViewVC 1.1.22