/[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 72 - (hide annotations) (download)
Sat Mar 9 09:29:40 2013 UTC (7 years, 6 months ago) by william
File size: 6463 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 56 static XMLTV()
19     {
20     xmltv_logger.Initialize();
21     if (instance == null)
22     {
23     instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>();
24     }
25     }
26 william 23
27 william 50 static IXMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance> instance;
28     public static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); }
29 william 72 public static void CreateInstance(params object[] args) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(args); }
30 william 50
31 william 56 public static void CreateFromInstance(object raw_instance, EventHandler<EventArgs> handler)
32     {
33     instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(raw_instance,handler);
34     if (OnInstanceCreated != null)
35     {
36     OnInstanceCreated.Invoke(null, new EventArgs());
37     }
38     }
39 william 50
40     #region IXMLTVSerializer<IXMLTVRuntimeInstance> members
41     public static bool Serialize(string file) { return instance.Serialize(file); }
42     public static bool Serialize(Stream stream) { return instance.Serialize(stream); }
43     public static IXMLTVRuntimeInstance DeSerialize(string file, out bool status) { return instance.DeSerialize(file, out status); }
44     public static IXMLTVRuntimeInstance DeSerialize(Stream stream, out bool status) { return instance.DeSerialize(stream, out status); }
45     #endregion
46 william 54
47 william 56 public static void DestroyInstance() { instance.DestroyInstance(); }
48 william 54
49     public static EventHandler<EventArgs> OnInstanceCreated
50     {
51     get { return instance.OnInstanceCreated; }
52     set { instance.OnInstanceCreated = value; }
53     }
54 william 22 }
55 william 50
56 william 56 internal class XMLTV<INTERFACE, CLASS> : IDestroyInstance, IXMLTV<INTERFACE, CLASS> where CLASS : class,INTERFACE,new()
57 william 50 {
58 william 56 public XMLTV()
59     {
60     instance = new CLASS();
61     }
62     public XMLTV(object raw_instance, EventHandler<EventArgs> handler)
63     {
64     instance = (CLASS)Convert.ChangeType(raw_instance, typeof(CLASS));
65     if (instance != null)
66     {
67     IRuntimeInstanceLoader<CLASS> loader = (instance as IRuntimeInstanceLoader<CLASS>);
68     if (loader != null)
69     {
70     SetOnInstanceCreated(handler);
71     instance = loader.LoadFromInstance(instance);
72     }
73     }
74     }
75 william 52 public XMLTV(params object[] args)
76 william 50 {
77 william 53
78     BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
79 william 52 CultureInfo culture = CultureInfo.CurrentCulture;
80     try
81 william 50 {
82 william 52 object raw_instance = Activator.CreateInstance(typeof(CLASS), flags, null, args, culture);
83     instance = (CLASS)Convert.ChangeType(raw_instance, typeof(CLASS));
84     }
85     catch (Exception ex)
86     {
87 william 55 xmltv_logger.Error.WriteLine(ex.ToString());
88 william 52
89     StringBuilder parameter_builder = new StringBuilder();
90     foreach (object arg in args)
91 william 50 {
92 william 52 Type type = arg.GetType();
93     parameter_builder.AppendFormat("({0}), ", type.FullName);
94 william 50 }
95 william 52
96     string type_parameters = parameter_builder.ToString().TrimEnd(new char[] { ',', ' ' });
97     throw new Exception(string.Format("Unable to create instance of: '{0}' with parameters: '{1}'", typeof(CLASS).Name, type_parameters), ex);
98 william 50 }
99     }
100    
101     private CLASS instance;
102     #region IXMLTV<T> members
103 william 51 public IXMLTVSerializer<CLASS> CreateSerializer()
104     {
105     // we must serialize on the CLASS type, using the INTERFACE type is syntatically incorrect
106     ISerializer<CLASS> class_serializer = (instance as ISerializer<CLASS>);
107     if (class_serializer != null)
108     {
109     return class_serializer.Serializer;
110     }
111     else
112     {
113     return new XMLTVSerializer<CLASS>(instance);
114     }
115     }
116 william 50 public INTERFACE GetInstance()
117     {
118     return (INTERFACE)instance;
119     }
120     #endregion
121    
122     #region IXMLTVSerializer<INTERFACE> members
123     public bool Serialize(string file) { return CreateSerializer().Serialize(file); }
124     public bool Serialize(Stream stream) { return CreateSerializer().Serialize(stream); }
125     public INTERFACE DeSerialize(string file, out bool status) { return CreateSerializer().DeSerialize(file, out status); }
126     public INTERFACE DeSerialize(Stream stream, out bool status) { return CreateSerializer().DeSerialize(stream, out status); }
127     #endregion
128 william 54 public void DestroyInstance()
129     {
130     IDestroyInstance destoyer = (instance as IDestroyInstance);
131     if (destoyer != null)
132     {
133     destoyer.DestroyInstance();
134     }
135     else
136     {
137 william 55 xmltv_logger.Error.WriteLine("Unable to call DestroyInstance() on type: '{0}'", instance.GetType().Name);
138 william 54 }
139     }
140     private void SetOnInstanceCreated(EventHandler<EventArgs> event_instance)
141     {
142     ISetCreatedInstanceEvent setter = (instance as ISetCreatedInstanceEvent);
143     if (setter != null) { setter.SetOnInstanceCreated(event_instance); }
144     }
145     private EventHandler<EventArgs> GetOnInstanceCreated()
146     {
147     IGetCreatedInstanceEvent getter = (instance as IGetCreatedInstanceEvent);
148     if (getter != null) { return getter.GetOnInstanceCreated(); }
149     return null;
150     }
151     public EventHandler<EventArgs> OnInstanceCreated
152     {
153     get { return GetOnInstanceCreated(); }
154     set { SetOnInstanceCreated(value); }
155     }
156 william 50 }
157    
158 william 22 }
159 william 46
160 william 49

  ViewVC Help
Powered by ViewVC 1.1.22