/[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 revision 11 by william, Thu Mar 7 05:48:51 2013 UTC trunk/libxmltv/Core/XMLTVLoader.cs revision 73 by william, Sat Mar 9 10:27:39 2013 UTC
# Line 4  using System.Linq; Line 4  using System.Linq;
4  using System.Text;  using System.Text;
5  using libxmltv.Interfaces;  using libxmltv.Interfaces;
6  using System.IO;  using System.IO;
7    using Enterprise.Logging;
8    using System.Xml.Linq;
9    using System.Xml;
10    
11  namespace libxmltv.Core  namespace libxmltv.Core
12  {  {
13      /// <summary>      internal class XMLTVLoader : XMLTVBase<XMLTVRuntimeInstance>, IDisposable
     /// Main class: Creates the XMLTV Loader  
     /// </summary>  
     public static class XMLTV  
14      {      {
15          public static object CreateLoader(string xml_file)          private string xmlfile = string.Empty;
16          {          //internal static void CreateInstance(string xml_file, XMLTVRuntimeInstance xmltv)
17              XMLTV_LOADER loader = new XMLTV_LOADER(xml_file);          //{
18              return loader;          //    using (XMLTVLoader g = new XMLTVLoader(xml_file, xmltv)) { }            
19          }          //}
20          public static void Test(object xmltv)          protected XMLTVLoader(string xml_file, XMLTVRuntimeInstance instance)
21                : base(instance, xml_file)
22          {          {
23              if (!VerifyInstance<XMLTV_LOADER>(xmltv)) { return; }              xmltv_logger.Debug.WriteLine("Creating Instance of XMLTVLoader");
24               var fi = new FileInfo(xml_file);
25               this.GetInstance().XmlFile_FullName = fi.FullName;
26               this.GetInstance().XmlFile_Name = fi.Name;
27                LoadXml();
28                xmltv_logger.Debug.WriteLine("Created Instance of XMLTVLoader");
29          }          }
30    
31            private void LoadXml()
         private static bool VerifyInstance<T>(object xmltv) where T : class  
32          {          {
33                xmltv_logger.Info.WriteLine("Loading XMLTV File: {0}", this.GetInstance().XmlFile_Name);
34                //xmltv_logger.Warn.WriteLine("XML File Loading has not been implemented yet!");
35              try              try
36              {              {
37                  if (xmltv == null) { return false; }                  var document = XDocument.Load(this.GetInstance().XmlFile_FullName);
38                  T t = (xmltv as T);                  using (var ms = new MemoryStream())
39                  if (t == null) { throw new InvalidCastException(string.Format("Unable to cast type: {0} to {1}", xmltv.GetType().Name, typeof(T).Name)); }                  using (var xw = new XmlTextWriter(ms, Encoding.UTF8))
40                  else { return true; }                  {
41                        document.Save(xw);
42                        xw.Flush();
43                        StreamReader sr = new StreamReader(ms);
44                        ms.Seek(0, SeekOrigin.Begin);
45                        this.GetInstance().XmlDoc = sr.ReadToEnd();
46                    }
47                    document = null;
48              }              }
49              catch (Exception ex)              catch (Exception ex)
50              {              {
51                  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);
52                    xmltv_logger.Error.WriteLine(ex.GetBaseException().ToString());
53              }              }
54          }          }
     }  
55    
56      internal class XMLTV_LOADER : IXMLTV_LOADER          public void Dispose()
     {  
         private string xmlfile = string.Empty;  
         public XMLTV_LOADER(string xml_file)  
57          {          {
58              xmlfile = xml_file;              //throw new NotImplementedException();
59          }          }
60          #region IXMLTV_LOADER  
61          public FileInfo XmlFile { get { return new FileInfo(xmlfile); } }          
         #endregion  
62      }      }
63  }  }

Legend:
Removed from v.11  
changed lines
  Added in v.73

  ViewVC Help
Powered by ViewVC 1.1.22