/[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 51 - (hide annotations) (download)
Fri Mar 8 06:39:56 2013 UTC (7 years, 6 months ago) by william
File size: 4092 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 22
9     namespace libxmltv.Core
10     {
11     /// <summary>
12     /// Main class: Creates the XMLTV Loader
13     /// </summary>
14     public static class XMLTV
15     {
16 william 46 static XMLTV() { xmltv_logger.Initialize(); }
17 william 23
18 william 50 static IXMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance> instance;
19     public static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); }
20 william 49 public static void CreateInstance(string xml_file) { CreateInstance(xml_file, null); }
21 william 50 public static void CreateInstance(string xml_file, EventHandler<CancelEventArgs> t) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(xml_file, t); }
22     //public static IXMLTVSerializer<IXMLTVRuntimeInstance> GetSerializer() {return new XMLTVSerializer<IXMLTVRuntimeInstance>(InternalGetInstance()); }
23    
24     //private static IXMLTVSerializer<T> CreateSerializer<T>() where T : class { return new XMLTVSerializer<T>(InternalGetInstance() as T); }
25    
26     #region IXMLTVSerializer<IXMLTVRuntimeInstance> members
27     public static bool Serialize(string file) { return instance.Serialize(file); }
28     public static bool Serialize(Stream stream) { return instance.Serialize(stream); }
29     public static IXMLTVRuntimeInstance DeSerialize(string file, out bool status) { return instance.DeSerialize(file, out status); }
30     public static IXMLTVRuntimeInstance DeSerialize(Stream stream, out bool status) { return instance.DeSerialize(stream, out status); }
31     #endregion
32 william 22 }
33 william 50
34     internal class XMLTV<INTERFACE, CLASS> : IXMLTV<INTERFACE, CLASS> where CLASS : class,INTERFACE
35     {
36     public XMLTV(string xml_file) : this(xml_file, null) { }
37     public XMLTV(string xml_file, EventHandler<CancelEventArgs> t)
38     {
39     //instance = new CLASS(xml_file, t);
40     instance = null;
41    
42     Type type = typeof(CLASS);
43     var ctors = type.GetConstructors();
44     foreach (var ctor in ctors)
45     {
46     var ctor_params = ctor.GetParameters();
47     if (ctor_params.Count() == 2)
48     {
49     if (ctor_params[0].ParameterType == typeof(string))
50     {
51     if (ctor_params[1].ParameterType == typeof(EventHandler<CancelEventArgs>))
52     {
53     object o = ctor.Invoke(new object[] { xml_file, t });
54     instance = (CLASS)Convert.ChangeType(o, typeof(CLASS));
55     break;
56     }
57     }
58     }
59     else { continue; }
60     }
61     }
62    
63     private CLASS instance;
64     #region IXMLTV<T> members
65 william 51 public IXMLTVSerializer<CLASS> CreateSerializer()
66     {
67     // we must serialize on the CLASS type, using the INTERFACE type is syntatically incorrect
68     ISerializer<CLASS> class_serializer = (instance as ISerializer<CLASS>);
69     if (class_serializer != null)
70     {
71     return class_serializer.Serializer;
72     }
73     else
74     {
75     return new XMLTVSerializer<CLASS>(instance);
76     }
77     }
78 william 50 public INTERFACE GetInstance()
79     {
80     return (INTERFACE)instance;
81     }
82     #endregion
83    
84     #region IXMLTVSerializer<INTERFACE> members
85     public bool Serialize(string file) { return CreateSerializer().Serialize(file); }
86     public bool Serialize(Stream stream) { return CreateSerializer().Serialize(stream); }
87     public INTERFACE DeSerialize(string file, out bool status) { return CreateSerializer().DeSerialize(file, out status); }
88     public INTERFACE DeSerialize(Stream stream, out bool status) { return CreateSerializer().DeSerialize(stream, out status); }
89     #endregion
90     }
91    
92 william 22 }
93 william 46
94 william 49

  ViewVC Help
Powered by ViewVC 1.1.22