1 |
using System; |
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Text; |
5 |
using libxmltv.Interfaces; |
6 |
using System.ComponentModel; |
7 |
using System.IO; |
8 |
using System.Reflection; |
9 |
using System.Globalization; |
10 |
|
11 |
namespace libxmltv.Core |
12 |
{ |
13 |
/// <summary> |
14 |
/// Main class: Creates the XMLTV Loader |
15 |
/// </summary> |
16 |
public static class XMLTV |
17 |
{ |
18 |
static XMLTV() |
19 |
{ |
20 |
xmltv_logger.Initialize(); |
21 |
if (instance == null) |
22 |
{ |
23 |
instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(); |
24 |
} |
25 |
} |
26 |
|
27 |
static IXMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance> instance; |
28 |
public static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); } |
29 |
public static void CreateInstance(params object[] args) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(args); } |
30 |
|
31 |
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 |
|
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 |
|
47 |
public static void DestroyInstance() { instance.DestroyInstance(); } |
48 |
|
49 |
public static EventHandler<EventArgs> OnInstanceCreated |
50 |
{ |
51 |
get { return instance.OnInstanceCreated; } |
52 |
set { instance.OnInstanceCreated = value; } |
53 |
} |
54 |
} |
55 |
|
56 |
internal class XMLTV<INTERFACE, CLASS> : IDestroyInstance, IXMLTV<INTERFACE, CLASS> where CLASS : class,INTERFACE,new() |
57 |
{ |
58 |
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 |
public XMLTV(params object[] args) |
76 |
{ |
77 |
|
78 |
BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; |
79 |
CultureInfo culture = CultureInfo.CurrentCulture; |
80 |
try |
81 |
{ |
82 |
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 |
xmltv_logger.Error.WriteLine(ex.ToString()); |
88 |
|
89 |
StringBuilder parameter_builder = new StringBuilder(); |
90 |
foreach (object arg in args) |
91 |
{ |
92 |
Type type = arg.GetType(); |
93 |
parameter_builder.AppendFormat("({0}), ", type.FullName); |
94 |
} |
95 |
|
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 |
} |
99 |
} |
100 |
|
101 |
private CLASS instance; |
102 |
#region IXMLTV<T> members |
103 |
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 |
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 |
public void DestroyInstance() |
129 |
{ |
130 |
IDestroyInstance destoyer = (instance as IDestroyInstance); |
131 |
if (destoyer != null) |
132 |
{ |
133 |
destoyer.DestroyInstance(); |
134 |
} |
135 |
else |
136 |
{ |
137 |
xmltv_logger.Error.WriteLine("Unable to call DestroyInstance() on type: '{0}'", instance.GetType().Name); |
138 |
} |
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 |
} |
157 |
|
158 |
} |
159 |
|
160 |
|