/[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 53 - (hide annotations) (download)
Fri Mar 8 07:05:53 2013 UTC (7 years, 7 months ago) by william
File size: 4301 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 46 static XMLTV() { xmltv_logger.Initialize(); }
19 william 23
20 william 50 static IXMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance> instance;
21     public static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); }
22 william 52 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 william 50 //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 william 22 }
35 william 50
36     internal class XMLTV<INTERFACE, CLASS> : IXMLTV<INTERFACE, CLASS> where CLASS : class,INTERFACE
37     {
38 william 52 public XMLTV(params object[] args)
39 william 50 {
40 william 53
41     BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
42 william 52 CultureInfo culture = CultureInfo.CurrentCulture;
43     try
44 william 50 {
45 william 52 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 william 50 {
55 william 52 Type type = arg.GetType();
56     parameter_builder.AppendFormat("({0}), ", type.FullName);
57 william 50 }
58 william 52
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 william 50 }
62     }
63    
64     private CLASS instance;
65     #region IXMLTV<T> members
66 william 51 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 william 50 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 william 22 }
94 william 46
95 william 49

  ViewVC Help
Powered by ViewVC 1.1.22