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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 117 - (show annotations) (download)
Sun Mar 10 17:10:47 2013 UTC (7 years, 5 months ago) by william
File size: 7411 byte(s)

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 internal static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); }
29 internal static void CreateInstance(params object[] args) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(args); }
30
31 internal 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 internal static bool Serialize(string file) { return instance.Serialize(file); }
42 internal static bool Serialize(Stream stream) { return instance.Serialize(stream); }
43 internal static bool DeSerialize(string file)
44 {
45 bool status;
46 var gInstance = instance.DeSerialize(file, out status);
47 ISetInstance<IXMLTVRuntimeInstance> setter = (instance as ISetInstance<IXMLTVRuntimeInstance>);
48 if (setter != null) { setter.SetInstance(gInstance); }
49 return status;
50 }
51 internal static bool DeSerialize(Stream stream)
52 {
53 bool status;
54 var gInstance = instance.DeSerialize(stream, out status);
55 ISetInstance<IXMLTVRuntimeInstance> setter = (instance as ISetInstance<IXMLTVRuntimeInstance>);
56 if (setter != null) { setter.SetInstance(gInstance); }
57 return status;
58 }
59 #endregion
60 internal static void DestroyInstance() { instance.DestroyInstance(); }
61
62 internal static EventHandler<EventArgs> OnInstanceCreated
63 {
64 get { return instance.OnInstanceCreated; }
65 set { instance.OnInstanceCreated = value; }
66 }
67
68 #region public members
69 public static bool Save(string file) { return Serialize(file); }
70 public static bool Load(string file) { return DeSerialize(file); }
71 public static void Create(params object[] args) { CreateInstance(args); }
72 public static void Destroy() { DestroyInstance(); }
73 #endregion
74 }
75
76 internal class XMLTV<INTERFACE, CLASS> : IDestroyInstance, IXMLTV<INTERFACE, CLASS> where CLASS : class,INTERFACE,new()
77 {
78 public XMLTV()
79 {
80 instance = new CLASS();
81 }
82 public XMLTV(object raw_instance, EventHandler<EventArgs> handler)
83 {
84 instance = (CLASS)Convert.ChangeType(raw_instance, typeof(CLASS));
85 if (instance != null)
86 {
87 IRuntimeInstanceLoader<CLASS> loader = (instance as IRuntimeInstanceLoader<CLASS>);
88 if (loader != null)
89 {
90 SetOnInstanceCreated(handler);
91 instance = loader.LoadFromInstance(instance);
92 }
93 }
94 }
95 public XMLTV(params object[] args)
96 {
97
98 BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
99 CultureInfo culture = CultureInfo.CurrentCulture;
100 try
101 {
102 object raw_instance = Activator.CreateInstance(typeof(CLASS), flags, null, args, culture);
103 instance = (CLASS)Convert.ChangeType(raw_instance, typeof(CLASS));
104 }
105 catch (Exception ex)
106 {
107 xmltv_logger.Error.WriteLine(ex.ToString());
108
109 StringBuilder parameter_builder = new StringBuilder();
110 foreach (object arg in args)
111 {
112 Type type = arg.GetType();
113 parameter_builder.AppendFormat("({0}), ", type.FullName);
114 }
115
116 string type_parameters = parameter_builder.ToString().TrimEnd(new char[] { ',', ' ' });
117 throw new Exception(string.Format("Unable to create instance of: '{0}' with parameters: '{1}'", typeof(CLASS).Name, type_parameters), ex);
118 }
119 }
120
121 private CLASS instance;
122 #region IXMLTV<T> members
123 public IXMLTVSerializer<CLASS> CreateSerializer()
124 {
125 // we must serialize on the CLASS type, using the INTERFACE type is syntatically incorrect
126 ISerializer<CLASS> class_serializer = (instance as ISerializer<CLASS>);
127 if (class_serializer != null)
128 {
129 return class_serializer.Serializer;
130 }
131 else
132 {
133 return new XMLTVSerializer<CLASS>(instance);
134 }
135 }
136 public INTERFACE GetInstance()
137 {
138 return (INTERFACE)instance;
139 }
140 public void SetInstance(INTERFACE gInstance)
141 {
142 this.instance = gInstance as CLASS;
143 }
144 #endregion
145
146 #region IXMLTVSerializer<INTERFACE> members
147 public bool Serialize(string file) { return CreateSerializer().Serialize(file); }
148 public bool Serialize(Stream stream) { return CreateSerializer().Serialize(stream); }
149 public INTERFACE DeSerialize(string file, out bool status) { return CreateSerializer().DeSerialize(file, out status); }
150 public INTERFACE DeSerialize(Stream stream, out bool status) { return CreateSerializer().DeSerialize(stream, out status); }
151 #endregion
152 public void DestroyInstance()
153 {
154 IDestroyInstance destoyer = (instance as IDestroyInstance);
155 if (destoyer != null)
156 {
157 destoyer.DestroyInstance();
158 }
159 else
160 {
161 xmltv_logger.Error.WriteLine("Unable to call DestroyInstance() on type: '{0}'", instance.GetType().Name);
162 }
163 }
164 private void SetOnInstanceCreated(EventHandler<EventArgs> event_instance)
165 {
166 ISetCreatedInstanceEvent setter = (instance as ISetCreatedInstanceEvent);
167 if (setter != null) { setter.SetOnInstanceCreated(event_instance); }
168 }
169 private EventHandler<EventArgs> GetOnInstanceCreated()
170 {
171 IGetCreatedInstanceEvent getter = (instance as IGetCreatedInstanceEvent);
172 if (getter != null) { return getter.GetOnInstanceCreated(); }
173 return null;
174 }
175 public EventHandler<EventArgs> OnInstanceCreated
176 {
177 get { return GetOnInstanceCreated(); }
178 set { SetOnInstanceCreated(value); }
179 }
180 }
181
182 }
183
184

  ViewVC Help
Powered by ViewVC 1.1.22