9 |
using System.Reflection; |
using System.Reflection; |
10 |
using System.Globalization; |
using System.Globalization; |
11 |
using System.Diagnostics; |
using System.Diagnostics; |
12 |
|
using System.Xml.Linq; |
13 |
|
|
14 |
namespace libxmltv.Core |
namespace libxmltv.Core |
15 |
{ |
{ |
41 |
} |
} |
42 |
} |
} |
43 |
|
|
|
|
|
44 |
private void OnCreatedInstance(object sender, EventArgs e) |
private void OnCreatedInstance(object sender, EventArgs e) |
45 |
{ |
{ |
46 |
if (OnInstanceCreated != null) { OnInstanceCreated.Invoke(sender, e); } |
if (OnInstanceCreated != null) { OnInstanceCreated.Invoke(sender, e); } |
48 |
|
|
49 |
private void init() |
private void init() |
50 |
{ |
{ |
51 |
this.Source = new XMLTVSource(); |
this.Source = new List<IXMLTVSource>(); |
52 |
this.Channels = new Dictionary<string, IXMLTVChannel>(); |
this.Channels = new List<IXMLTVChannel>(); |
53 |
this.Programs = new Dictionary<int, IXMLTVProgram>(); |
this.Programs = new List<IXMLTVProgram>(); |
54 |
this.XmlFile_Name = string.Empty; |
this.XmlFile_Name = string.Empty; |
55 |
this.XmlFile_FullName = string.Empty; |
this.XmlFile_FullName = string.Empty; |
56 |
this.XmlDoc = string.Empty; |
this.XmlDoc = string.Empty; |
68 |
public string XmlFile_FullName { get { return _XmlFile_FullName; } set { _XmlFile_FullName = value; } } |
public string XmlFile_FullName { get { return _XmlFile_FullName; } set { _XmlFile_FullName = value; } } |
69 |
|
|
70 |
private string _XmlDoc; |
private string _XmlDoc; |
71 |
public string XmlDoc { get { return _XmlDoc; } set { _XmlDoc = value; } } |
public string XmlDoc { get { return _XmlDoc; } set { _XmlDoc = value; } } |
72 |
private IXMLTVSource _Source; |
private List<IXMLTVSource> _Source; |
73 |
public IXMLTVSource Source { get { return _Source; } set { _Source = value; } } |
public List<IXMLTVSource> Source { get { return _Source; } set { _Source = value; } } |
74 |
private Dictionary<string, IXMLTVChannel> _Channels; |
private List<IXMLTVChannel> _Channels; |
75 |
public Dictionary<string, IXMLTVChannel> Channels { get { return _Channels; } set { _Channels = value; } } |
public List<IXMLTVChannel> Channels { get { return _Channels; } set { _Channels = value; } } |
76 |
private Dictionary<int, IXMLTVProgram> _Programs; |
private List<IXMLTVProgram> _Programs; |
77 |
public Dictionary<int, IXMLTVProgram> Programs { get { return _Programs; } set { _Programs = value; } } |
public List<IXMLTVProgram> Programs { get { return _Programs; } set { _Programs = value; } } |
78 |
#endregion |
#endregion |
79 |
#region IOnInstanceCreated members |
#region IOnInstanceCreated members |
80 |
[NonSerialized] |
[NonSerialized] |
122 |
xmltv_logger.Debug.WriteLine("Loading from instance..."); |
xmltv_logger.Debug.WriteLine("Loading from instance..."); |
123 |
if (this.Source != null) |
if (this.Source != null) |
124 |
{ |
{ |
125 |
xmltv_logger.Info.WriteLine("Source Loaded: '{0}' Created by '{1}' - original source file: '{2}'", this.Source.SourceName, this.Source.GeneratorName, this.XmlFile_FullName); |
xmltv_logger.Info.WriteLine("Source Loaded: '{0}' Created by '{1}' - original source file: '{2}'", this.Source.FirstOrDefault().SourceName, this.Source.FirstOrDefault().GeneratorName, this.XmlFile_FullName); |
126 |
} |
} |
127 |
else |
else |
128 |
{ |
{ |
131 |
} |
} |
132 |
if (this.Channels != null) |
if (this.Channels != null) |
133 |
{ |
{ |
134 |
xmltv_logger.Info.WriteLine("Source Loaded: '{0}' Channels from source '{1}'", this.Channels.Count, this.Source.SourceName); |
xmltv_logger.Info.WriteLine("Source Loaded: '{0}' Channels from source '{1}'", this.Channels.Count, this.Source.FirstOrDefault().SourceName); |
135 |
} |
} |
136 |
else |
else |
137 |
{ |
{ |
140 |
} |
} |
141 |
if (this.Programs != null) |
if (this.Programs != null) |
142 |
{ |
{ |
143 |
xmltv_logger.Info.WriteLine("Source Loaded: '{0}' Programs from source '{1}'", this.Programs.Count, this.Source.SourceName); |
xmltv_logger.Info.WriteLine("Source Loaded: '{0}' Programs from source '{1}'", this.Programs.Count, this.Source.FirstOrDefault().SourceName); |
144 |
} |
} |
145 |
else |
else |
146 |
{ |
{ |
218 |
|
|
219 |
internal class XMLTVInstance : IGetInstance<XMLTVRuntimeInstance>, IDisposable |
internal class XMLTVInstance : IGetInstance<XMLTVRuntimeInstance>, IDisposable |
220 |
{ |
{ |
221 |
|
public void Dispose() |
222 |
|
{ |
223 |
|
//throw new NotImplementedException(); |
224 |
|
} |
225 |
|
|
226 |
private XMLTVRuntimeInstance gInstance; |
private XMLTVRuntimeInstance gInstance; |
227 |
public XMLTVRuntimeInstance GetInstance() { return gInstance; } |
public XMLTVRuntimeInstance GetInstance() { return gInstance; } |
228 |
public XMLTVInstance(string xmlfile) |
public XMLTVInstance(string xmlfile) |
230 |
try |
try |
231 |
{ |
{ |
232 |
CreateLoader(xmlfile); |
CreateLoader(xmlfile); |
233 |
|
CreateParser(); |
234 |
} |
} |
235 |
catch (Exception ex) |
catch (Exception ex) |
236 |
{ |
{ |
272 |
if (getter_instance != null) { gInstance = getter_instance.GetInstance(); } |
if (getter_instance != null) { gInstance = getter_instance.GetInstance(); } |
273 |
else { throw new Exception("Found a compatible XMLTV Data Loader, but was unable to obtain the instance holding the data"); } |
else { throw new Exception("Found a compatible XMLTV Data Loader, but was unable to obtain the instance holding the data"); } |
274 |
} |
} |
275 |
private void CreateParser(XMLTVRuntimeInstance instance) |
private void CreateParser() |
276 |
{ |
{ |
277 |
|
var doc = XDocument.Parse(this.GetInstance().XmlDoc); |
278 |
|
var root_element = doc.Root.Name; |
279 |
|
if (root_element == null) { throw new NullReferenceException("Root element is null"); } |
280 |
|
CreateRootHandler(root_element.ToString()); |
281 |
|
|
282 |
|
var nodes = doc.Root.Elements().ToList(); |
283 |
|
|
284 |
} |
} |
285 |
|
|
286 |
public void Dispose() |
|
287 |
|
private void CreateRootHandler(string root_element) |
288 |
{ |
{ |
289 |
//throw new NotImplementedException(); |
object raw_instance = null; |
290 |
|
BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; |
291 |
|
CultureInfo culture = CultureInfo.CurrentCulture; |
292 |
|
Assembly asm = Assembly.GetExecutingAssembly(); |
293 |
|
var types = asm.GetTypes(); |
294 |
|
Type handler_type = null; |
295 |
|
foreach (var type in types) |
296 |
|
{ |
297 |
|
if (type.BaseType != null && type.BaseType == typeof(XMLTVBase<XMLTVRuntimeInstance>)) |
298 |
|
{ |
299 |
|
try |
300 |
|
{ |
301 |
|
var handler_prop = type.GetProperty("Handler"); |
302 |
|
if (handler_prop != null) |
303 |
|
{ |
304 |
|
raw_instance = Activator.CreateInstance(type, flags, null, new object[0], culture); |
305 |
|
if (raw_instance != null) |
306 |
|
{ |
307 |
|
object handler_value = handler_prop.GetValue(raw_instance, null); |
308 |
|
if (handler_value != null && handler_value.ToString() == root_element) |
309 |
|
{ |
310 |
|
handler_type = type; |
311 |
|
break; |
312 |
|
} |
313 |
|
} |
314 |
|
} |
315 |
|
} |
316 |
|
catch (Exception) { } |
317 |
|
} |
318 |
|
} |
319 |
|
if (handler_type == null) { throw new Exception("Unable to find a compatible handler to parse document root."); } |
320 |
|
raw_instance = Activator.CreateInstance(handler_type, flags, null, new object[] { gInstance }, culture); |
321 |
} |
} |
322 |
|
|
323 |
|
|