--- trunk/libxmltv/Core/XMLTV_LOADER.cs 2013/03/07 10:00:40 19
+++ trunk/libxmltv/Core/XMLTVLoader.cs 2013/03/08 13:40:32 59
@@ -6,59 +6,64 @@
using System.IO;
using Enterprise.Logging;
using System.Xml.Linq;
+using System.Xml;
namespace libxmltv.Core
-{
- ///
- /// Main class: Creates the XMLTV Loader
- ///
- public static class XMLTV
+{
+ internal class XMLTVLoader : IDisposable
{
- public static object CreateLoader(string xml_file)
+ private string xmlfile = string.Empty;
+ internal static void CreateInstance(string xml_file, XMLTVRuntimeInstance xmltv)
{
- XMLTV_LOGGER.Initialize();
- XMLTV_LOADER loader = new XMLTV_LOADER(xml_file);
- return loader;
+ using (XMLTVLoader g = new XMLTVLoader(xml_file, xmltv)) { }
}
- public static void Test(object xmltv)
+ private XMLTVRuntimeInstance instance;
+ protected XMLTVLoader(string xml_file, XMLTVRuntimeInstance xmltv)
{
- if (!VerifyInstance(xmltv)) { return; }
+ xmltv_logger.Info.WriteLine("Creating Instance of XMLTVLoader");
+ //string _xmltv;
+ //if (!Internals.VerifyInstance(xmltv, out _xmltv)) { return; }
+ //xmlfile = _xmltv;
+ //LoadXml();
+ instance = xmltv;
+ instance.XmlFile = new FileInfo(xml_file);
+ LoadXml();
}
+ //#region IXMLTV_LOADER
+ //public FileInfo XmlFile { get { return new FileInfo(xmlfile); } }
+ //public XDocument XmlDoc { get; private set; }
+ //#endregion
-
- private static bool VerifyInstance(object xmltv) where T : class
+ private void LoadXml()
{
+ xmltv_logger.Info.WriteLine("Loading XMLTV File: {0}", instance.XmlFile.Name);
+ //xmltv_logger.Warn.WriteLine("XML File Loading has not been implemented yet!");
try
{
- if (xmltv == null) { return false; }
- T t = (xmltv as T);
- if (t == null) { throw new InvalidCastException(string.Format("Unable to cast type: {0} to {1}", xmltv.GetType().Name, typeof(T).Name)); }
- else { return true; }
+ var document = XDocument.Load(instance.XmlFile.FullName);
+ using (var ms = new MemoryStream())
+ using (var xw = new XmlTextWriter(ms, Encoding.UTF8))
+ {
+ document.Save(xw);
+ xw.Flush();
+ StreamReader sr = new StreamReader(ms);
+ ms.Seek(0, SeekOrigin.Begin);
+ instance.XmlDoc = sr.ReadToEnd();
+ }
+ document = null;
}
catch (Exception ex)
{
- throw new InvalidCastException(string.Format("Unable to cast type: {0} to {1}", xmltv.GetType().Name, typeof(T).Name), ex);
+ xmltv_logger.Error.WriteLine("Failed to load XMLTV File: {0}", instance.XmlFile.Name);
+ xmltv_logger.Error.WriteLine(ex.GetBaseException().ToString());
}
}
- }
- internal class XMLTV_LOADER : IXMLTV_LOADER
- {
- private string xmlfile = string.Empty;
- public XMLTV_LOADER(string xml_file)
+ public void Dispose()
{
- xmlfile = xml_file;
- LoadXml();
+ //throw new NotImplementedException();
}
- #region IXMLTV_LOADER
- public FileInfo XmlFile { get { return new FileInfo(xmlfile); } }
- #endregion
- private void LoadXml()
- {
- XMLTV_LOGGER.Log.Info.WriteLine("Loading XMLTV File: {0}", XmlFile.Name);
- //XMLTV_LOGGER.Log.Warn.WriteLine("XML File Loading has not been implemented yet!");
- XDocument doc = XDocument.Load(XmlFile.FullName);
- }
+
}
}