/[xmltv_parser]/trunk/libxmltv/Core/XMLTVLoader.cs
ViewVC logotype

Diff of /trunk/libxmltv/Core/XMLTVLoader.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/libxmltv/Core/XMLTV_LOADER.cs	2013/03/07 05:48:51	11
+++ trunk/libxmltv/Core/XMLTVLoader.cs	2013/03/09 10:27:39	73
@@ -4,50 +4,60 @@
 using System.Text;
 using libxmltv.Interfaces;
 using System.IO;
+using Enterprise.Logging;
+using System.Xml.Linq;
+using System.Xml;
 
 namespace libxmltv.Core
 {
-    /// <summary>
-    /// Main class: Creates the XMLTV Loader
-    /// </summary>
-    public static class XMLTV
+    internal class XMLTVLoader : XMLTVBase<XMLTVRuntimeInstance>, IDisposable
     {
-        public static object CreateLoader(string xml_file)
-        {
-            XMLTV_LOADER loader = new XMLTV_LOADER(xml_file);
-            return loader;
-        }
-        public static void Test(object xmltv)
+        private string xmlfile = string.Empty;
+        //internal static void CreateInstance(string xml_file, XMLTVRuntimeInstance xmltv)
+        //{
+        //    using (XMLTVLoader g = new XMLTVLoader(xml_file, xmltv)) { }            
+        //}
+        protected XMLTVLoader(string xml_file, XMLTVRuntimeInstance instance)
+            : base(instance, xml_file)
         {
-            if (!VerifyInstance<XMLTV_LOADER>(xmltv)) { return; }
+            xmltv_logger.Debug.WriteLine("Creating Instance of XMLTVLoader");
+           var fi = new FileInfo(xml_file);
+           this.GetInstance().XmlFile_FullName = fi.FullName;
+           this.GetInstance().XmlFile_Name = fi.Name;
+            LoadXml();
+            xmltv_logger.Debug.WriteLine("Created Instance of XMLTVLoader");
         }
 
-
-        private static bool VerifyInstance<T>(object xmltv) where T : class
+        private void LoadXml()
         {
+            xmltv_logger.Info.WriteLine("Loading XMLTV File: {0}", this.GetInstance().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(this.GetInstance().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);
+                    this.GetInstance().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}", this.GetInstance().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;
+            //throw new NotImplementedException();
         }
-        #region IXMLTV_LOADER
-        public FileInfo XmlFile { get { return new FileInfo(xmlfile); } }
-        #endregion
+
+        
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22