/[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 130 - (show annotations) (download)
Wed Mar 13 14:39:08 2013 UTC (6 years, 11 months ago) by william
File size: 14355 byte(s)
+ current wip

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 using System.Windows.Forms;
11 using Enterprise.Logging;
12
13 namespace libxmltv.Core
14 {
15 /// <summary>
16 /// Main class: Creates the XMLTV Loader
17 /// </summary>
18 public static class XMLTV
19 {
20 static XMLTV()
21 {
22 xmltv_logger.Initialize();
23 if (instance == null)
24 {
25 instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>();
26 }
27 }
28
29 #region public members
30 #region save data
31 public static void Save(string file)
32 {
33 if (!Serialize(file))
34 {
35 MessageBox.Show("Failed to save data - check log", "Failed to save data", MessageBoxButtons.OK, MessageBoxIcon.Error);
36 return;
37 }
38 MessageBox.Show("Successfully saved data", "Successfully saved data", MessageBoxButtons.OK, MessageBoxIcon.Information);
39 }
40 public static void Save(Stream stream)
41 {
42 if (!Serialize(stream))
43 {
44 MessageBox.Show("Failed to save data - check log", "Failed to save data", MessageBoxButtons.OK, MessageBoxIcon.Error);
45 return;
46 }
47 MessageBox.Show("Successfully saved data", "Successfully saved data", MessageBoxButtons.OK, MessageBoxIcon.Information);
48 }
49 #endregion
50 #region load data
51 public static void Load(string file) { Load(file, null); }
52 public static void Load(string file, EventHandler<EventArgs> handler)
53 {
54 if (!DeSerialize(file, handler))
55 {
56 MessageBox.Show("Failed to load data - check log", "Failed to load data", MessageBoxButtons.OK, MessageBoxIcon.Error);
57 return;
58 }
59 MessageBox.Show("Successfully loaded data", "Successfully loaded data", MessageBoxButtons.OK, MessageBoxIcon.Information);
60 OnCreatedInstance();
61
62 }
63 public static void Load(Stream stream) { Load(stream, null); }
64 public static void Load(Stream stream, EventHandler<EventArgs> handler)
65 {
66 if (!DeSerialize(stream, handler))
67 {
68 MessageBox.Show("Failed to load data - check log", "Failed to load data", MessageBoxButtons.OK, MessageBoxIcon.Error);
69 return;
70 }
71 MessageBox.Show("Successfully loaded data", "Successfully loaded data", MessageBoxButtons.OK, MessageBoxIcon.Information);
72 OnCreatedInstance();
73 }
74 #endregion
75 #region Create/Destroy
76 public static void Create(params object[] args) { CreateInstance(args); }
77 public static void Destroy() { DestroyInstance(); }
78 #endregion
79
80 #region Get Data
81 /// <summary>
82 /// Gets the source of the XMLTV Data : <see cref="libxmltv.Interfaces.IXMLTVSource"/>
83 /// </summary>
84 /// <returns>returns an instance of the xmltv data source : <see cref="libxmltv.Interfaces.IXMLTVSource"/></returns>
85 public static IXMLTVSource GetSource()
86 {
87 var gInstance = GetInstance();
88 var list = gInstance.Source;
89 return list;
90 }
91 /// <summary>
92 /// Gets the collection of XMLTV Channels : List of <see cref="libxmltv.Interfaces.IXMLTVChannel"/>
93 /// </summary>
94 /// <returns>returns an instance of the xmltv channels : as a list of: <see cref="libxmltv.Interfaces.IXMLTVChannel"/></returns>
95 public static List<IXMLTVChannel> GetChannels()
96 {
97 var gInstance = GetInstance();
98 var list = gInstance.Channels;
99 return list;
100 }
101 /// <summary>
102 /// Gets the collection of XMLTV Programs : List of <see cref="libxmltv.Interfaces.IXMLTVProgram"/>
103 /// </summary>
104 /// <returns>returns an instance of the xmltv channels : as a list of: <see cref="libxmltv.Interfaces.IXMLTVProgram"/></returns>
105 public static List<IXMLTVProgram> GetPrograms()
106 {
107 var gInstance = GetInstance();
108 var list = gInstance.Programs;
109 return list;
110 }
111 /// <summary>
112 /// Creates a binding source for a bindable control
113 /// </summary>
114 /// <param name="data">The object data to create a binding source from</param>
115 /// <param name="type">Represents the underlying type of the created binding source data</param>
116 /// <returns>Returns an object representing the created binding source</returns>
117 public static object CreateBindingSourceFromData(object data, out Type type)
118 {
119 if (data == null) { throw new ArgumentNullException("data", "cannot be null"); }
120 //BindingSource source = new BindingSource();
121
122 IDataSourceBindable binder = (data as IDataSourceBindable);
123 if (binder == null) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", data.GetType().Name, typeof(IDataSourceBindable).Name)); }
124 object bindable = binder.CreateBindableDataSource(out type);
125 //source.DataSource = bindable;
126 //return source;
127 return bindable;
128 }
129
130 private static object ConvertSourceType(Type source_type, object source)
131 {
132 //try
133 //{
134 // IDataConverter converter = (source as IDataConverter);
135 // if (converter == null) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, typeof(IDataConverter).Name)); }
136 // object t = converter.ConvertObjectData(source);
137 // if (t != null) { source = t; }
138 // return t;
139 //}
140 //catch (Exception ex) { gLog.Verbose.Error.WriteLine(ex.ToString()); return null; }
141 return source;
142 }
143
144 public static void CreateFilterFromDataSource(ref object source, params string[] args)
145 {
146 if (source == null) { throw new ArgumentNullException("source", "cannot be null"); }
147 object t = ConvertSourceType(source.GetType(), source);
148 if (t == null) { throw new ArgumentException("source_type", string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, source.GetType().Name)); }
149 if (args == null) { throw new ArgumentNullException("args", "cannot be null"); }
150 source = t;
151 IDataSourceFilterable filter = (source as IDataSourceFilterable);
152 if (filter == null) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, typeof(IDataSourceFilterable).Name)); }
153 filter.Filter(ref source, args);
154 }
155 public static void CreateSorterFromDataSource(ref object source, params string[] args)
156 {
157 if (source == null) { throw new ArgumentNullException("source", "cannot be null"); }
158 object t = ConvertSourceType(source.GetType(), source);
159 if (t == null) { throw new ArgumentException("source_type", string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, source.GetType().Name)); }
160 if (args == null) { throw new ArgumentNullException("args", "cannot be null"); }
161 source = t;
162 IDataSourceSortable sorter = (source as IDataSourceSortable);
163 if (sorter == null) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, typeof(IDataSourceSortable).Name)); }
164 sorter.Sort(ref source, args);
165 }
166 #endregion
167 #endregion
168
169
170 static IXMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance> instance;
171 internal static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); }
172 internal static void CreateInstance(params object[] args) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(args); }
173
174 internal static void CreateFromInstance(object raw_instance, EventHandler<EventArgs> handler)
175 {
176 instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(raw_instance, handler);
177 //OnCreatedInstance();
178 }
179
180 internal static void OnCreatedInstance() { if (OnInstanceCreated != null) { OnInstanceCreated.Invoke(null, new EventArgs()); } }
181
182 #region IXMLTVSerializer<IXMLTVRuntimeInstance> members
183 internal static bool Serialize(string file) { return instance.Serialize(file); }
184 internal static bool Serialize(Stream stream) { return instance.Serialize(stream); }
185 internal static bool DeSerialize(string file, EventHandler<EventArgs> handler)
186 {
187 bool status;
188 var gInstance = instance.DeSerialize(file, out status);
189 CreateFromInstance(gInstance, handler);
190 return status;
191 }
192 internal static bool DeSerialize(Stream stream, EventHandler<EventArgs> handler)
193 {
194 bool status;
195 var gInstance = instance.DeSerialize(stream, out status);
196 CreateFromInstance(gInstance, handler);
197 return status;
198 }
199 #endregion
200 internal static void DestroyInstance() { instance.DestroyInstance(); }
201
202 internal static EventHandler<EventArgs> OnInstanceCreated
203 {
204 get { return instance.OnInstanceCreated; }
205 set { instance.OnInstanceCreated = value; }
206 }
207
208
209 }
210
211 internal class XMLTV<INTERFACE, CLASS> : IDestroyInstance, IXMLTV<INTERFACE, CLASS> where CLASS : class,INTERFACE,new()
212 {
213 public XMLTV()
214 {
215 instance = new CLASS();
216 }
217 public XMLTV(object raw_instance, EventHandler<EventArgs> handler)
218 {
219 instance = (CLASS)Convert.ChangeType(raw_instance, typeof(CLASS));
220 if (instance != null)
221 {
222 IRuntimeInstanceLoader<CLASS> loader = (instance as IRuntimeInstanceLoader<CLASS>);
223 if (loader != null)
224 {
225 SetOnInstanceCreated(handler);
226 instance = loader.LoadFromInstance(instance);
227 }
228 }
229 }
230 public XMLTV(params object[] args)
231 {
232
233 BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
234 CultureInfo culture = CultureInfo.CurrentCulture;
235 try
236 {
237 object raw_instance = Activator.CreateInstance(typeof(CLASS), flags, null, args, culture);
238 instance = (CLASS)Convert.ChangeType(raw_instance, typeof(CLASS));
239 }
240 catch (Exception ex)
241 {
242 xmltv_logger.Error.WriteLine(ex.ToString());
243
244 StringBuilder parameter_builder = new StringBuilder();
245 foreach (object arg in args)
246 {
247 Type type = arg.GetType();
248 parameter_builder.AppendFormat("({0}), ", type.FullName);
249 }
250
251 string type_parameters = parameter_builder.ToString().TrimEnd(new char[] { ',', ' ' });
252 throw new Exception(string.Format("Unable to create instance of: '{0}' with parameters: '{1}'", typeof(CLASS).Name, type_parameters), ex);
253 }
254 }
255
256 private CLASS instance;
257 #region IXMLTV<T> members
258 public IXMLTVSerializer<CLASS> CreateSerializer()
259 {
260 // we must serialize on the CLASS type, using the INTERFACE type is syntatically incorrect
261 ISerializer<CLASS> class_serializer = (instance as ISerializer<CLASS>);
262 if (class_serializer != null)
263 {
264 return class_serializer.Serializer;
265 }
266 else
267 {
268 return new XMLTVSerializer<CLASS>(instance);
269 }
270 }
271 public INTERFACE GetInstance()
272 {
273 return (INTERFACE)instance;
274 }
275 public void SetInstance(INTERFACE gInstance)
276 {
277 this.instance = gInstance as CLASS;
278 }
279 #endregion
280
281 #region IXMLTVSerializer<INTERFACE> members
282 public bool Serialize(string file) { return CreateSerializer().Serialize(file); }
283 public bool Serialize(Stream stream) { return CreateSerializer().Serialize(stream); }
284 public INTERFACE DeSerialize(string file, out bool status) { return CreateSerializer().DeSerialize(file, out status); }
285 public INTERFACE DeSerialize(Stream stream, out bool status) { return CreateSerializer().DeSerialize(stream, out status); }
286 #endregion
287 public void DestroyInstance()
288 {
289 IDestroyInstance destoyer = (instance as IDestroyInstance);
290 if (destoyer != null)
291 {
292 destoyer.DestroyInstance();
293 }
294 else
295 {
296 xmltv_logger.Error.WriteLine("Unable to call DestroyInstance() on type: '{0}'", instance.GetType().Name);
297 }
298 }
299 private void SetOnInstanceCreated(EventHandler<EventArgs> event_instance)
300 {
301 ISetCreatedInstanceEvent setter = (instance as ISetCreatedInstanceEvent);
302 if (setter != null) { setter.SetOnInstanceCreated(event_instance); }
303 }
304 private EventHandler<EventArgs> GetOnInstanceCreated()
305 {
306 IGetCreatedInstanceEvent getter = (instance as IGetCreatedInstanceEvent);
307 if (getter != null) { return getter.GetOnInstanceCreated(); }
308 return null;
309 }
310 public EventHandler<EventArgs> OnInstanceCreated
311 {
312 get { return GetOnInstanceCreated(); }
313 set { SetOnInstanceCreated(value); }
314 }
315 }
316
317 }
318
319

  ViewVC Help
Powered by ViewVC 1.1.22