/[xmltv_parser]/trunk/libxmltv/Core/XMLTV.cs
ViewVC logotype

Annotation of /trunk/libxmltv/Core/XMLTV.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 52 - (hide annotations) (download)
Fri Mar 8 07:01:37 2013 UTC (7 years, 6 months ago) by william
File size: 5205 byte(s)

1 william 22 using System;
2     using System.Collections.Generic;
3     using System.Linq;
4     using System.Text;
5     using libxmltv.Interfaces;
6 william 46 using System.ComponentModel;
7 william 50 using System.IO;
8 william 52 using System.Reflection;
9     using System.Globalization;
10 william 22
11     namespace libxmltv.Core
12     {
13     /// <summary>
14     /// Main class: Creates the XMLTV Loader
15     /// </summary>
16     public static class XMLTV
17     {
18 william 46 static XMLTV() { xmltv_logger.Initialize(); }
19 william 23
20 william 50 static IXMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance> instance;
21     public static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); }
22 william 52 public static void CreateInstance(params object[] args) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(args); }
23     //public static void CreateInstance(string xml_file, EventHandler<CancelEventArgs> t) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(xml_file, t); }
24 william 50 //public static IXMLTVSerializer<IXMLTVRuntimeInstance> GetSerializer() {return new XMLTVSerializer<IXMLTVRuntimeInstance>(InternalGetInstance()); }
25    
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 william 22 }
35 william 50
36     internal class XMLTV<INTERFACE, CLASS> : IXMLTV<INTERFACE, CLASS> where CLASS : class,INTERFACE
37     {
38 william 52 public XMLTV(params object[] args)
39 william 50 {
40 william 52 ////instance = new CLASS(xml_file, t);
41     //instance = null;
42 william 50
43 william 52 //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 william 50 {
66 william 52 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 william 50 {
76 william 52 Type type = arg.GetType();
77     parameter_builder.AppendFormat("({0}), ", type.FullName);
78 william 50 }
79 william 52
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 william 50 }
83     }
84    
85     private CLASS instance;
86     #region IXMLTV<T> members
87 william 51 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 william 50 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 william 22 }
115 william 46
116 william 49

  ViewVC Help
Powered by ViewVC 1.1.22