/[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 54 - (hide annotations) (download)
Fri Mar 8 08:51:10 2013 UTC (7 years, 7 months ago) by william
File size: 5769 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 54
35     public static void DestroyInstance() { instance.DestroyInstance(); }
36    
37     public static EventHandler<EventArgs> OnInstanceCreated
38     {
39     get { return instance.OnInstanceCreated; }
40     set { instance.OnInstanceCreated = value; }
41     }
42 william 22 }
43 william 50
44 william 54 internal class XMLTV<INTERFACE, CLASS> : IDestroyInstance, IXMLTV<INTERFACE, CLASS> where CLASS : class,INTERFACE
45 william 50 {
46 william 54
47 william 52 public XMLTV(params object[] args)
48 william 50 {
49 william 53
50     BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
51 william 52 CultureInfo culture = CultureInfo.CurrentCulture;
52     try
53 william 50 {
54 william 52 object raw_instance = Activator.CreateInstance(typeof(CLASS), flags, null, args, culture);
55     instance = (CLASS)Convert.ChangeType(raw_instance, typeof(CLASS));
56     }
57     catch (Exception ex)
58     {
59     xmltv_logger.Log.Error.WriteLine(ex.ToString());
60    
61     StringBuilder parameter_builder = new StringBuilder();
62     foreach (object arg in args)
63 william 50 {
64 william 52 Type type = arg.GetType();
65     parameter_builder.AppendFormat("({0}), ", type.FullName);
66 william 50 }
67 william 52
68     string type_parameters = parameter_builder.ToString().TrimEnd(new char[] { ',', ' ' });
69     throw new Exception(string.Format("Unable to create instance of: '{0}' with parameters: '{1}'", typeof(CLASS).Name, type_parameters), ex);
70 william 50 }
71     }
72    
73     private CLASS instance;
74     #region IXMLTV<T> members
75 william 51 public IXMLTVSerializer<CLASS> CreateSerializer()
76     {
77     // we must serialize on the CLASS type, using the INTERFACE type is syntatically incorrect
78     ISerializer<CLASS> class_serializer = (instance as ISerializer<CLASS>);
79     if (class_serializer != null)
80     {
81     return class_serializer.Serializer;
82     }
83     else
84     {
85     return new XMLTVSerializer<CLASS>(instance);
86     }
87     }
88 william 50 public INTERFACE GetInstance()
89     {
90     return (INTERFACE)instance;
91     }
92     #endregion
93    
94     #region IXMLTVSerializer<INTERFACE> members
95     public bool Serialize(string file) { return CreateSerializer().Serialize(file); }
96     public bool Serialize(Stream stream) { return CreateSerializer().Serialize(stream); }
97     public INTERFACE DeSerialize(string file, out bool status) { return CreateSerializer().DeSerialize(file, out status); }
98     public INTERFACE DeSerialize(Stream stream, out bool status) { return CreateSerializer().DeSerialize(stream, out status); }
99     #endregion
100 william 54 public void DestroyInstance()
101     {
102     IDestroyInstance destoyer = (instance as IDestroyInstance);
103     if (destoyer != null)
104     {
105     destoyer.DestroyInstance();
106     }
107     else
108     {
109     xmltv_logger.Log.Error.WriteLine("Unable to call DestroyInstance() on type: '{0}'", instance.GetType().Name);
110     }
111     }
112     private void SetOnInstanceCreated(EventHandler<EventArgs> event_instance)
113     {
114     ISetCreatedInstanceEvent setter = (instance as ISetCreatedInstanceEvent);
115     if (setter != null) { setter.SetOnInstanceCreated(event_instance); }
116     }
117     private EventHandler<EventArgs> GetOnInstanceCreated()
118     {
119     IGetCreatedInstanceEvent getter = (instance as IGetCreatedInstanceEvent);
120     if (getter != null) { return getter.GetOnInstanceCreated(); }
121     return null;
122     }
123     public EventHandler<EventArgs> OnInstanceCreated
124     {
125     get { return GetOnInstanceCreated(); }
126     set { SetOnInstanceCreated(value); }
127     }
128 william 50 }
129    
130 william 22 }
131 william 46
132 william 49

  ViewVC Help
Powered by ViewVC 1.1.22