/[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 118 - (hide annotations) (download)
Sun Mar 10 17:41:52 2013 UTC (7 years, 7 months ago) by william
File size: 9951 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 118 using System.Windows.Forms;
11 william 22
12     namespace libxmltv.Core
13     {
14     /// <summary>
15     /// Main class: Creates the XMLTV Loader
16     /// </summary>
17     public static class XMLTV
18     {
19 william 56 static XMLTV()
20     {
21     xmltv_logger.Initialize();
22     if (instance == null)
23     {
24     instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>();
25     }
26     }
27 william 23
28 william 118 #region public members
29     #region save data
30     public static void Save(string file)
31     {
32     if (!Serialize(file))
33     {
34     MessageBox.Show("Failed to save data - check log", "Failed to save data", MessageBoxButtons.OK, MessageBoxIcon.Error);
35     return;
36     }
37     MessageBox.Show("Successfully saved data", "Successfully saved data", MessageBoxButtons.OK, MessageBoxIcon.Information);
38     }
39     public static void Save(Stream stream)
40     {
41     if (!Serialize(stream))
42     {
43     MessageBox.Show("Failed to save data - check log", "Failed to save data", MessageBoxButtons.OK, MessageBoxIcon.Error);
44     return;
45     }
46     MessageBox.Show("Successfully saved data", "Successfully saved data", MessageBoxButtons.OK, MessageBoxIcon.Information);
47     }
48     #endregion
49     #region load data
50     public static void Load(string file) { Load(file, null); }
51     public static void Load(string file, EventHandler<EventArgs> handler)
52     {
53     if (!DeSerialize(file, handler))
54     {
55     MessageBox.Show("Failed to load data - check log", "Failed to load data", MessageBoxButtons.OK, MessageBoxIcon.Error);
56     return;
57     }
58     MessageBox.Show("Successfully loaded data", "Successfully loaded data", MessageBoxButtons.OK, MessageBoxIcon.Information);
59     OnCreatedInstance();
60    
61     }
62     public static void Load(Stream stream) { Load(stream, null); }
63     public static void Load(Stream stream, EventHandler<EventArgs> handler)
64     {
65     if (!DeSerialize(stream, handler))
66     {
67     MessageBox.Show("Failed to load data - check log", "Failed to load data", MessageBoxButtons.OK, MessageBoxIcon.Error);
68     return;
69     }
70     MessageBox.Show("Successfully loaded data", "Successfully loaded data", MessageBoxButtons.OK, MessageBoxIcon.Information);
71     OnCreatedInstance();
72     }
73     #endregion
74     #region Create/Destroy
75     public static void Create(params object[] args) { CreateInstance(args); }
76     public static void Destroy() { DestroyInstance(); }
77     #endregion
78    
79     #region Get Data
80     public static IXMLTVSource GetSource()
81     {
82     var gInstance = GetInstance();
83     var list = gInstance.Source;
84     return list;
85     }
86     public static List<IXMLTVChannel> GetChannels()
87     {
88     var gInstance = GetInstance();
89     var list = gInstance.Channels;
90     return list;
91     }
92     public static List<IXMLTVProgram> GetPrograms()
93     {
94     var gInstance = GetInstance();
95     var list = gInstance.Programs;
96     return list;
97     }
98     #endregion
99     #endregion
100    
101    
102 william 50 static IXMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance> instance;
103 william 117 internal static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); }
104     internal static void CreateInstance(params object[] args) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(args); }
105 william 50
106 william 118 internal static void CreateFromInstance(object raw_instance, EventHandler<EventArgs> handler)
107     {
108     instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(raw_instance, handler);
109     //OnCreatedInstance();
110 william 56 }
111 william 50
112 william 118 internal static void OnCreatedInstance() { if (OnInstanceCreated != null) { OnInstanceCreated.Invoke(null, new EventArgs()); } }
113    
114 william 50 #region IXMLTVSerializer<IXMLTVRuntimeInstance> members
115 william 117 internal static bool Serialize(string file) { return instance.Serialize(file); }
116     internal static bool Serialize(Stream stream) { return instance.Serialize(stream); }
117 william 118 internal static bool DeSerialize(string file, EventHandler<EventArgs> handler)
118 william 117 {
119     bool status;
120 william 118 var gInstance = instance.DeSerialize(file, out status);
121     CreateFromInstance(gInstance, handler);
122 william 117 return status;
123     }
124 william 118 internal static bool DeSerialize(Stream stream, EventHandler<EventArgs> handler)
125 william 117 {
126     bool status;
127     var gInstance = instance.DeSerialize(stream, out status);
128 william 118 CreateFromInstance(gInstance, handler);
129 william 117 return status;
130     }
131 william 50 #endregion
132 william 117 internal static void DestroyInstance() { instance.DestroyInstance(); }
133 william 54
134 william 117 internal static EventHandler<EventArgs> OnInstanceCreated
135 william 54 {
136     get { return instance.OnInstanceCreated; }
137     set { instance.OnInstanceCreated = value; }
138     }
139 william 117
140 william 118
141 william 22 }
142 william 50
143 william 56 internal class XMLTV<INTERFACE, CLASS> : IDestroyInstance, IXMLTV<INTERFACE, CLASS> where CLASS : class,INTERFACE,new()
144 william 50 {
145 william 56 public XMLTV()
146     {
147     instance = new CLASS();
148     }
149     public XMLTV(object raw_instance, EventHandler<EventArgs> handler)
150     {
151     instance = (CLASS)Convert.ChangeType(raw_instance, typeof(CLASS));
152     if (instance != null)
153     {
154     IRuntimeInstanceLoader<CLASS> loader = (instance as IRuntimeInstanceLoader<CLASS>);
155     if (loader != null)
156     {
157     SetOnInstanceCreated(handler);
158     instance = loader.LoadFromInstance(instance);
159     }
160     }
161     }
162 william 52 public XMLTV(params object[] args)
163 william 50 {
164 william 53
165     BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
166 william 52 CultureInfo culture = CultureInfo.CurrentCulture;
167     try
168 william 50 {
169 william 52 object raw_instance = Activator.CreateInstance(typeof(CLASS), flags, null, args, culture);
170     instance = (CLASS)Convert.ChangeType(raw_instance, typeof(CLASS));
171     }
172     catch (Exception ex)
173     {
174 william 55 xmltv_logger.Error.WriteLine(ex.ToString());
175 william 52
176     StringBuilder parameter_builder = new StringBuilder();
177     foreach (object arg in args)
178 william 50 {
179 william 52 Type type = arg.GetType();
180     parameter_builder.AppendFormat("({0}), ", type.FullName);
181 william 50 }
182 william 52
183     string type_parameters = parameter_builder.ToString().TrimEnd(new char[] { ',', ' ' });
184     throw new Exception(string.Format("Unable to create instance of: '{0}' with parameters: '{1}'", typeof(CLASS).Name, type_parameters), ex);
185 william 50 }
186     }
187    
188     private CLASS instance;
189     #region IXMLTV<T> members
190 william 51 public IXMLTVSerializer<CLASS> CreateSerializer()
191     {
192     // we must serialize on the CLASS type, using the INTERFACE type is syntatically incorrect
193     ISerializer<CLASS> class_serializer = (instance as ISerializer<CLASS>);
194     if (class_serializer != null)
195     {
196     return class_serializer.Serializer;
197     }
198     else
199     {
200     return new XMLTVSerializer<CLASS>(instance);
201     }
202     }
203 william 50 public INTERFACE GetInstance()
204     {
205     return (INTERFACE)instance;
206     }
207 william 117 public void SetInstance(INTERFACE gInstance)
208     {
209     this.instance = gInstance as CLASS;
210     }
211 william 50 #endregion
212    
213     #region IXMLTVSerializer<INTERFACE> members
214     public bool Serialize(string file) { return CreateSerializer().Serialize(file); }
215     public bool Serialize(Stream stream) { return CreateSerializer().Serialize(stream); }
216     public INTERFACE DeSerialize(string file, out bool status) { return CreateSerializer().DeSerialize(file, out status); }
217     public INTERFACE DeSerialize(Stream stream, out bool status) { return CreateSerializer().DeSerialize(stream, out status); }
218     #endregion
219 william 54 public void DestroyInstance()
220     {
221     IDestroyInstance destoyer = (instance as IDestroyInstance);
222     if (destoyer != null)
223     {
224     destoyer.DestroyInstance();
225     }
226     else
227     {
228 william 55 xmltv_logger.Error.WriteLine("Unable to call DestroyInstance() on type: '{0}'", instance.GetType().Name);
229 william 54 }
230     }
231     private void SetOnInstanceCreated(EventHandler<EventArgs> event_instance)
232     {
233     ISetCreatedInstanceEvent setter = (instance as ISetCreatedInstanceEvent);
234     if (setter != null) { setter.SetOnInstanceCreated(event_instance); }
235     }
236     private EventHandler<EventArgs> GetOnInstanceCreated()
237     {
238     IGetCreatedInstanceEvent getter = (instance as IGetCreatedInstanceEvent);
239     if (getter != null) { return getter.GetOnInstanceCreated(); }
240     return null;
241     }
242     public EventHandler<EventArgs> OnInstanceCreated
243     {
244     get { return GetOnInstanceCreated(); }
245     set { SetOnInstanceCreated(value); }
246     }
247 william 50 }
248    
249 william 22 }
250 william 46
251 william 49

  ViewVC Help
Powered by ViewVC 1.1.22