/[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 53 - (show annotations) (download)
Fri Mar 8 07:05:53 2013 UTC (7 years, 5 months ago) by william
File size: 4301 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() { xmltv_logger.Initialize(); }
19
20 static IXMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance> instance;
21 public static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); }
22 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 //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 }
35
36 internal class XMLTV<INTERFACE, CLASS> : IXMLTV<INTERFACE, CLASS> where CLASS : class,INTERFACE
37 {
38 public XMLTV(params object[] args)
39 {
40
41 BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
42 CultureInfo culture = CultureInfo.CurrentCulture;
43 try
44 {
45 object raw_instance = Activator.CreateInstance(typeof(CLASS), flags, null, args, culture);
46 instance = (CLASS)Convert.ChangeType(raw_instance, typeof(CLASS));
47 }
48 catch (Exception ex)
49 {
50 xmltv_logger.Log.Error.WriteLine(ex.ToString());
51
52 StringBuilder parameter_builder = new StringBuilder();
53 foreach (object arg in args)
54 {
55 Type type = arg.GetType();
56 parameter_builder.AppendFormat("({0}), ", type.FullName);
57 }
58
59 string type_parameters = parameter_builder.ToString().TrimEnd(new char[] { ',', ' ' });
60 throw new Exception(string.Format("Unable to create instance of: '{0}' with parameters: '{1}'", typeof(CLASS).Name, type_parameters), ex);
61 }
62 }
63
64 private CLASS instance;
65 #region IXMLTV<T> members
66 public IXMLTVSerializer<CLASS> CreateSerializer()
67 {
68 // we must serialize on the CLASS type, using the INTERFACE type is syntatically incorrect
69 ISerializer<CLASS> class_serializer = (instance as ISerializer<CLASS>);
70 if (class_serializer != null)
71 {
72 return class_serializer.Serializer;
73 }
74 else
75 {
76 return new XMLTVSerializer<CLASS>(instance);
77 }
78 }
79 public INTERFACE GetInstance()
80 {
81 return (INTERFACE)instance;
82 }
83 #endregion
84
85 #region IXMLTVSerializer<INTERFACE> members
86 public bool Serialize(string file) { return CreateSerializer().Serialize(file); }
87 public bool Serialize(Stream stream) { return CreateSerializer().Serialize(stream); }
88 public INTERFACE DeSerialize(string file, out bool status) { return CreateSerializer().DeSerialize(file, out status); }
89 public INTERFACE DeSerialize(Stream stream, out bool status) { return CreateSerializer().DeSerialize(stream, out status); }
90 #endregion
91 }
92
93 }
94
95

  ViewVC Help
Powered by ViewVC 1.1.22