/[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 126 - (hide annotations) (download)
Sun Mar 10 20:19:33 2013 UTC (7 years, 7 months ago) by william
File size: 11849 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 william 120
99 william 126 public static object CreateDataSourceFromObject(object data)
100 william 120 {
101     if (data == null) { throw new ArgumentNullException("data", "cannot be null"); }
102 william 126 //BindingSource source = new BindingSource();
103 william 120
104     IDataSourceBindable binder = (data as IDataSourceBindable);
105 william 125 if (binder == null) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", data.GetType().Name, typeof(IDataSourceBindable).Name)); }
106 william 120 object bindable = binder.CreateBindableDataSource();
107 william 126 //source.DataSource = bindable;
108     //return source;
109     return bindable;
110 william 120 }
111    
112 william 126 public static void CreateFilterFromDataSource(ref object source, params string[] args)
113 william 125 {
114     if (source == null) { throw new ArgumentNullException("source", "cannot be null"); }
115     if (args == null) { throw new ArgumentNullException("args", "cannot be null"); }
116     IDataSourceFilterable filter = (source as IDataSourceFilterable);
117     if (filter == null) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, typeof(IDataSourceFilterable).Name)); }
118 william 126 filter.Filter(ref source,args);
119 william 125 }
120 william 126 public static void CreateSorterFromDataSource(ref object source, params string[] args)
121 william 125 {
122     if (source == null) { throw new ArgumentNullException("source", "cannot be null"); }
123     if (args == null) { throw new ArgumentNullException("args", "cannot be null"); }
124     IDataSourceSortable sorter = (source as IDataSourceSortable);
125     if (sorter == null) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, typeof(IDataSourceSortable).Name)); }
126 william 126 sorter.Sort(ref source, args);
127 william 125 }
128 william 118 #endregion
129     #endregion
130    
131    
132 william 50 static IXMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance> instance;
133 william 117 internal static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); }
134     internal static void CreateInstance(params object[] args) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(args); }
135 william 50
136 william 118 internal static void CreateFromInstance(object raw_instance, EventHandler<EventArgs> handler)
137     {
138     instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(raw_instance, handler);
139     //OnCreatedInstance();
140 william 56 }
141 william 50
142 william 118 internal static void OnCreatedInstance() { if (OnInstanceCreated != null) { OnInstanceCreated.Invoke(null, new EventArgs()); } }
143    
144 william 50 #region IXMLTVSerializer<IXMLTVRuntimeInstance> members
145 william 117 internal static bool Serialize(string file) { return instance.Serialize(file); }
146     internal static bool Serialize(Stream stream) { return instance.Serialize(stream); }
147 william 118 internal static bool DeSerialize(string file, EventHandler<EventArgs> handler)
148 william 117 {
149     bool status;
150 william 118 var gInstance = instance.DeSerialize(file, out status);
151     CreateFromInstance(gInstance, handler);
152 william 117 return status;
153     }
154 william 118 internal static bool DeSerialize(Stream stream, EventHandler<EventArgs> handler)
155 william 117 {
156     bool status;
157     var gInstance = instance.DeSerialize(stream, out status);
158 william 118 CreateFromInstance(gInstance, handler);
159 william 117 return status;
160     }
161 william 50 #endregion
162 william 117 internal static void DestroyInstance() { instance.DestroyInstance(); }
163 william 54
164 william 117 internal static EventHandler<EventArgs> OnInstanceCreated
165 william 54 {
166     get { return instance.OnInstanceCreated; }
167     set { instance.OnInstanceCreated = value; }
168     }
169 william 117
170 william 118
171 william 22 }
172 william 50
173 william 56 internal class XMLTV<INTERFACE, CLASS> : IDestroyInstance, IXMLTV<INTERFACE, CLASS> where CLASS : class,INTERFACE,new()
174 william 50 {
175 william 56 public XMLTV()
176     {
177     instance = new CLASS();
178     }
179     public XMLTV(object raw_instance, EventHandler<EventArgs> handler)
180     {
181     instance = (CLASS)Convert.ChangeType(raw_instance, typeof(CLASS));
182     if (instance != null)
183     {
184     IRuntimeInstanceLoader<CLASS> loader = (instance as IRuntimeInstanceLoader<CLASS>);
185     if (loader != null)
186     {
187     SetOnInstanceCreated(handler);
188     instance = loader.LoadFromInstance(instance);
189     }
190     }
191     }
192 william 52 public XMLTV(params object[] args)
193 william 50 {
194 william 53
195     BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
196 william 52 CultureInfo culture = CultureInfo.CurrentCulture;
197     try
198 william 50 {
199 william 52 object raw_instance = Activator.CreateInstance(typeof(CLASS), flags, null, args, culture);
200     instance = (CLASS)Convert.ChangeType(raw_instance, typeof(CLASS));
201     }
202     catch (Exception ex)
203     {
204 william 55 xmltv_logger.Error.WriteLine(ex.ToString());
205 william 52
206     StringBuilder parameter_builder = new StringBuilder();
207     foreach (object arg in args)
208 william 50 {
209 william 52 Type type = arg.GetType();
210     parameter_builder.AppendFormat("({0}), ", type.FullName);
211 william 50 }
212 william 52
213     string type_parameters = parameter_builder.ToString().TrimEnd(new char[] { ',', ' ' });
214     throw new Exception(string.Format("Unable to create instance of: '{0}' with parameters: '{1}'", typeof(CLASS).Name, type_parameters), ex);
215 william 50 }
216     }
217    
218     private CLASS instance;
219     #region IXMLTV<T> members
220 william 51 public IXMLTVSerializer<CLASS> CreateSerializer()
221     {
222     // we must serialize on the CLASS type, using the INTERFACE type is syntatically incorrect
223     ISerializer<CLASS> class_serializer = (instance as ISerializer<CLASS>);
224     if (class_serializer != null)
225     {
226     return class_serializer.Serializer;
227     }
228     else
229     {
230     return new XMLTVSerializer<CLASS>(instance);
231     }
232     }
233 william 50 public INTERFACE GetInstance()
234     {
235     return (INTERFACE)instance;
236     }
237 william 117 public void SetInstance(INTERFACE gInstance)
238     {
239     this.instance = gInstance as CLASS;
240     }
241 william 50 #endregion
242    
243     #region IXMLTVSerializer<INTERFACE> members
244     public bool Serialize(string file) { return CreateSerializer().Serialize(file); }
245     public bool Serialize(Stream stream) { return CreateSerializer().Serialize(stream); }
246     public INTERFACE DeSerialize(string file, out bool status) { return CreateSerializer().DeSerialize(file, out status); }
247     public INTERFACE DeSerialize(Stream stream, out bool status) { return CreateSerializer().DeSerialize(stream, out status); }
248     #endregion
249 william 54 public void DestroyInstance()
250     {
251     IDestroyInstance destoyer = (instance as IDestroyInstance);
252     if (destoyer != null)
253     {
254     destoyer.DestroyInstance();
255     }
256     else
257     {
258 william 55 xmltv_logger.Error.WriteLine("Unable to call DestroyInstance() on type: '{0}'", instance.GetType().Name);
259 william 54 }
260     }
261     private void SetOnInstanceCreated(EventHandler<EventArgs> event_instance)
262     {
263     ISetCreatedInstanceEvent setter = (instance as ISetCreatedInstanceEvent);
264     if (setter != null) { setter.SetOnInstanceCreated(event_instance); }
265     }
266     private EventHandler<EventArgs> GetOnInstanceCreated()
267     {
268     IGetCreatedInstanceEvent getter = (instance as IGetCreatedInstanceEvent);
269     if (getter != null) { return getter.GetOnInstanceCreated(); }
270     return null;
271     }
272     public EventHandler<EventArgs> OnInstanceCreated
273     {
274     get { return GetOnInstanceCreated(); }
275     set { SetOnInstanceCreated(value); }
276     }
277 william 50 }
278    
279 william 22 }
280 william 46
281 william 49

  ViewVC Help
Powered by ViewVC 1.1.22