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

Contents of /trunk/libxmltv/Core/XMLTVProgramCollection.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 65 - (show annotations) (download)
Sat Mar 9 01:09:45 2013 UTC (7 years, 5 months ago) by william
File size: 6715 byte(s)
+ speed-up program parsing by doing pre-checks for NullReference(s) and the like

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using libxmltv.Interfaces;
6 using System.Xml.Linq;
7 using System.Globalization;
8 using System.Windows.Forms;
9
10 namespace libxmltv.Core
11 {
12 internal class XMLTVProgramCollection : IDisposable
13 {
14 private Dictionary<int, IXMLTVProgram> entries = new Dictionary<int, IXMLTVProgram>();
15 internal static void CreateInstance(XMLTVRuntimeInstance xmltv)
16 {
17 using (XMLTVProgramCollection g = new XMLTVProgramCollection(xmltv))
18 {
19 g.instance.Programs = g.Collection;
20 xmltv_logger.Info.WriteLine(g.ToString());
21 }
22 }
23 private XMLTVRuntimeInstance instance;
24 protected XMLTVProgramCollection(XMLTVRuntimeInstance xmltv)
25 {
26 xmltv_logger.Info.WriteLine("Creating Instance of XMLTVProgramCollection");
27 instance = xmltv;
28 Create();
29 }
30
31 #region IXMLTVSource
32 public Dictionary<int, IXMLTVProgram> Collection
33 {
34 get { return entries; }
35 }
36 #endregion
37
38 //
39 private DateTime ParseDate(string timeStamp)
40 {
41 DateTime dt = new DateTime();
42 try
43 {
44 dt = DateTime.ParseExact(timeStamp, "yyyyMMddHHmmss zzzz", System.Globalization.CultureInfo.CurrentCulture);
45 }
46 catch (Exception ex) { throw ex; }
47 return dt;
48 }
49 private void Create()
50 {
51 var doc = XDocument.Parse(instance.XmlDoc);
52 double program_index = 0;
53 double program_count = doc.Descendants(XMLTVConstants.PROGRAM_ELEMENT).Count();
54 double progress = 0;
55 foreach (var c in doc.Descendants(XMLTVConstants.PROGRAM_ELEMENT))
56 {
57 if (instance.IsAborting)
58 {
59 xmltv_logger.Verbose.Debug.WriteLine("Detected Instance abort event...");
60 break;
61 }
62 Program program = new Program();
63
64 program.Id = (int)program_index;
65 xmltv_logger.Verbose.Debug.WriteLine("program_Id: {0}", program.Id);
66 if (c.HasAttributes)
67 {
68 var start = c.Attribute(XMLTVConstants.Programs.ProgramStart);
69 program.Start = start == null ? new DateTime() : ParseDate(start.Value);
70 xmltv_logger.Verbose.Debug.WriteLine("\tprogram_start: {0}", start);
71
72 var stop = c.Attribute(XMLTVConstants.Programs.ProgramStop);
73 program.Stop = stop == null ? new DateTime() : ParseDate(stop.Value);
74 xmltv_logger.Verbose.Debug.WriteLine("\tprogram_stop: {0}", stop);
75
76 var channelid = c.Attribute(XMLTVConstants.Programs.ProgramChannelId);
77 xmltv_logger.Verbose.Debug.WriteLine("\tprogram_channelid: {0}", channelid);
78 IXMLTVChannel channel = new Channel();
79 string _channelid = channelid == null ? string.Empty : channelid.Value;
80 if (instance.Channels.ContainsKey(_channelid)) { program.Channel = instance.Channels[_channelid]; }
81 else { program.Channel = new Channel(); }
82 xmltv_logger.Verbose.Debug.WriteLine("\tprogram_channel: {0}", program.Channel.ToString());
83 }
84 try
85 {
86 var title = c.Descendants(XMLTVConstants.Programs.ProgramTitle).FirstOrDefault();
87 program.Title = title == null ? string.Empty : title.Value;
88 }
89 catch (Exception) { program.Title = string.Empty; }
90 xmltv_logger.Verbose.Debug.WriteLine("\tprogram_title: {0}", program.Title == string.Empty ? "empty" : program.Title);
91 try
92 {
93 var subtitle = c.Descendants(XMLTVConstants.Programs.ProgramSubTitle).FirstOrDefault();
94 program.SubTitle = subtitle == null ? string.Empty : subtitle.Value;
95 }
96 catch (Exception) { program.SubTitle = string.Empty; }
97 xmltv_logger.Verbose.Debug.WriteLine("\tprogram_subtitle: {0}", program.SubTitle == string.Empty ? "empty" : program.SubTitle);
98 try
99 {
100 var description = c.Descendants(XMLTVConstants.Programs.ProgramDescription).FirstOrDefault();
101 program.Description = description == null ? string.Empty : description.Value; ;
102 }
103 catch (Exception) { program.Description = string.Empty; }
104 xmltv_logger.Verbose.Debug.WriteLine("\tprogram_description: {0}", program.Description == string.Empty ? "empty" : program.Description);
105
106 entries.Add(program.Id, program);
107
108 program_index++;
109 progress = 100.0 * (program_index / program_count);
110 xmltv_logger.ReportProgress(this, new Enterprise.Logging.ReportProgressEventArgs((int)progress));
111 Application.DoEvents();
112 }
113 //instance.Programs = Collection;
114 }
115 public override string ToString()
116 {
117 return string.Format("Total Programs Parsed: {0}", Collection == null ? 0 : Collection.Count);
118 }
119
120 public void Dispose()
121 {
122 //throw new NotImplementedException();
123 }
124 }
125 [Serializable]
126 internal class Program : IXMLTVProgram
127 {
128 public Program()
129 {
130 Id = 0;
131 Start = new DateTime();
132 Stop = new DateTime();
133 Channel = new Channel();
134 Title = string.Empty;
135 SubTitle = string.Empty;
136 Description = string.Empty;
137 }
138 #region IXMLTVProgram members
139 public int Id { get; set; }
140 public DateTime Start { get; set; }
141 public DateTime Stop { get; set; }
142 public IXMLTVChannel Channel { get; set; }
143 public string Title { get; set; }
144 public string SubTitle { get; set; }
145 public string Description { get; set; }
146 #endregion
147 public override string ToString()
148 {
149 return string.Format("{0}: {1} - {2} ({3}) ['{4}' <==> '{5}']", Id, Title, SubTitle, Channel.ToString(), Start.ToString("yyyy/MM/dd hh:mm tt"), Stop.ToString("yyyy/MM/dd hh:mm tt"));
150 }
151 }
152 }

  ViewVC Help
Powered by ViewVC 1.1.22