/[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 117 - (hide annotations) (download)
Sun Mar 10 17:10:47 2013 UTC (7 years, 7 months ago) by william
File size: 7411 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 56 static XMLTV()
19     {
20     xmltv_logger.Initialize();
21     if (instance == null)
22     {
23     instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>();
24     }
25     }
26 william 23
27 william 50 static IXMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance> instance;
28 william 117 internal static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); }
29     internal static void CreateInstance(params object[] args) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(args); }
30 william 50
31 william 117 internal static void CreateFromInstance(object raw_instance, EventHandler<EventArgs> handler)
32 william 56 {
33     instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(raw_instance,handler);
34     if (OnInstanceCreated != null)
35     {
36     OnInstanceCreated.Invoke(null, new EventArgs());
37     }
38     }
39 william 50
40     #region IXMLTVSerializer<IXMLTVRuntimeInstance> members
41 william 117 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 william 50 #endregion
60 william 117 internal static void DestroyInstance() { instance.DestroyInstance(); }
61 william 54
62 william 117 internal static EventHandler<EventArgs> OnInstanceCreated
63 william 54 {
64     get { return instance.OnInstanceCreated; }
65     set { instance.OnInstanceCreated = value; }
66     }
67 william 117
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 william 22 }
75 william 50
76 william 56 internal class XMLTV<INTERFACE, CLASS> : IDestroyInstance, IXMLTV<INTERFACE, CLASS> where CLASS : class,INTERFACE,new()
77 william 50 {
78 william 56 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 william 52 public XMLTV(params object[] args)
96 william 50 {
97 william 53
98     BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
99 william 52 CultureInfo culture = CultureInfo.CurrentCulture;
100     try
101 william 50 {
102 william 52 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 william 55 xmltv_logger.Error.WriteLine(ex.ToString());
108 william 52
109     StringBuilder parameter_builder = new StringBuilder();
110     foreach (object arg in args)
111 william 50 {
112 william 52 Type type = arg.GetType();
113     parameter_builder.AppendFormat("({0}), ", type.FullName);
114 william 50 }
115 william 52
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 william 50 }
119     }
120    
121     private CLASS instance;
122     #region IXMLTV<T> members
123 william 51 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 william 50 public INTERFACE GetInstance()
137     {
138     return (INTERFACE)instance;
139     }
140 william 117 public void SetInstance(INTERFACE gInstance)
141     {
142     this.instance = gInstance as CLASS;
143     }
144 william 50 #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 william 54 public void DestroyInstance()
153     {
154     IDestroyInstance destoyer = (instance as IDestroyInstance);
155     if (destoyer != null)
156     {
157     destoyer.DestroyInstance();
158     }
159     else
160     {
161 william 55 xmltv_logger.Error.WriteLine("Unable to call DestroyInstance() on type: '{0}'", instance.GetType().Name);
162 william 54 }
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 william 50 }
181    
182 william 22 }
183 william 46
184 william 49

  ViewVC Help
Powered by ViewVC 1.1.22