ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/xmltv_parser/trunk/libxmltv/Core/XMLTV.cs
Revision: 54
Committed: Fri Mar 8 08:51:10 2013 UTC (10 years, 6 months ago) by william
File size: 5769 byte(s)
Log Message:

File Contents

# Content
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 public static void DestroyInstance() { instance.DestroyInstance(); }
36
37 public static EventHandler<EventArgs> OnInstanceCreated
38 {
39 get { return instance.OnInstanceCreated; }
40 set { instance.OnInstanceCreated = value; }
41 }
42 }
43
44 internal class XMLTV<INTERFACE, CLASS> : IDestroyInstance, IXMLTV<INTERFACE, CLASS> where CLASS : class,INTERFACE
45 {
46
47 public XMLTV(params object[] args)
48 {
49
50 BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
51 CultureInfo culture = CultureInfo.CurrentCulture;
52 try
53 {
54 object raw_instance = Activator.CreateInstance(typeof(CLASS), flags, null, args, culture);
55 instance = (CLASS)Convert.ChangeType(raw_instance, typeof(CLASS));
56 }
57 catch (Exception ex)
58 {
59 xmltv_logger.Log.Error.WriteLine(ex.ToString());
60
61 StringBuilder parameter_builder = new StringBuilder();
62 foreach (object arg in args)
63 {
64 Type type = arg.GetType();
65 parameter_builder.AppendFormat("({0}), ", type.FullName);
66 }
67
68 string type_parameters = parameter_builder.ToString().TrimEnd(new char[] { ',', ' ' });
69 throw new Exception(string.Format("Unable to create instance of: '{0}' with parameters: '{1}'", typeof(CLASS).Name, type_parameters), ex);
70 }
71 }
72
73 private CLASS instance;
74 #region IXMLTV<T> members
75 public IXMLTVSerializer<CLASS> CreateSerializer()
76 {
77 // we must serialize on the CLASS type, using the INTERFACE type is syntatically incorrect
78 ISerializer<CLASS> class_serializer = (instance as ISerializer<CLASS>);
79 if (class_serializer != null)
80 {
81 return class_serializer.Serializer;
82 }
83 else
84 {
85 return new XMLTVSerializer<CLASS>(instance);
86 }
87 }
88 public INTERFACE GetInstance()
89 {
90 return (INTERFACE)instance;
91 }
92 #endregion
93
94 #region IXMLTVSerializer<INTERFACE> members
95 public bool Serialize(string file) { return CreateSerializer().Serialize(file); }
96 public bool Serialize(Stream stream) { return CreateSerializer().Serialize(stream); }
97 public INTERFACE DeSerialize(string file, out bool status) { return CreateSerializer().DeSerialize(file, out status); }
98 public INTERFACE DeSerialize(Stream stream, out bool status) { return CreateSerializer().DeSerialize(stream, out status); }
99 #endregion
100 public void DestroyInstance()
101 {
102 IDestroyInstance destoyer = (instance as IDestroyInstance);
103 if (destoyer != null)
104 {
105 destoyer.DestroyInstance();
106 }
107 else
108 {
109 xmltv_logger.Log.Error.WriteLine("Unable to call DestroyInstance() on type: '{0}'", instance.GetType().Name);
110 }
111 }
112 private void SetOnInstanceCreated(EventHandler<EventArgs> event_instance)
113 {
114 ISetCreatedInstanceEvent setter = (instance as ISetCreatedInstanceEvent);
115 if (setter != null) { setter.SetOnInstanceCreated(event_instance); }
116 }
117 private EventHandler<EventArgs> GetOnInstanceCreated()
118 {
119 IGetCreatedInstanceEvent getter = (instance as IGetCreatedInstanceEvent);
120 if (getter != null) { return getter.GetOnInstanceCreated(); }
121 return null;
122 }
123 public EventHandler<EventArgs> OnInstanceCreated
124 {
125 get { return GetOnInstanceCreated(); }
126 set { SetOnInstanceCreated(value); }
127 }
128 }
129
130 }
131
132