/// <summary> /// Clear all existing data and create blank XMLTV workspace /// </summary> public void Clear() { try { localErrors = new List <XMLTVError>(); // New document for new data var newDoc = new XmlTVDocument(_rootGeneratorName, _rootGeneratorUrl, _rootDataSourceName); xmlData = new XmlTVData(newDoc); } catch (Exception ex) { addError(ex); } }
/// <summary> /// Load an XML TV file, and merge contents into existing data (if any) /// </summary> /// <param name="filename"></param> /// <param name="mergeOnly"></param> /// <returns></returns> public bool LoadXmlTV(string filename, bool mergeOnly = false) { try { var xmlTVLoad = new XmlTVDocument(); xmlTVLoad.Load(filename); var localData = new XmlTVData(xmlTVLoad); var localRootTvNode = localData.rootNode; // Check for root TV node if (localRootTvNode == null) { addError(1002, "root TV node was not found", XMLTVError.ErrorSeverity.Error, "", "LoadXmlTV"); return(false); } // Check for dupe channel ids if (validateChannel(xmlData, localData)) { return(false); } if (validateProgramme(xmlData, localData)) { return(false); } if (mergeOnly) { CopyXmlData(ref localData, xmlData, false); } else { CopyXmlData(ref localData, xmlData, true); } return(true); } catch (System.Exception ex) { addError(ex); } return(false); }
private void CopyXmlData(ref XmlTVData fromData, XmlTVData toData, bool translate = true) { try { // Channels foreach (var channelNodeData in fromData.channelData) { var thisChannel = new Channel { ChanelNode = (XmlElement)toData.rootDocument.ImportNode(channelNodeData.Value.ChanelNode, true) }; if (toData.channelData.ContainsKey(channelNodeData.Key)) { toData.channelData[channelNodeData.Key] = thisChannel; } else { toData.channelData.Add(channelNodeData.Key, thisChannel); } var thisChan = toData.channelData[channelNodeData.Key]; // Programmes foreach (var programmeNode in channelNodeData.Value.programmeNodes) { var thisProgrammNode = (XmlElement)toData.rootDocument.ImportNode(programmeNode.Value, true); if (thisChan.programmeNodes.ContainsKey(programmeNode.Key)) { thisChan.programmeNodes[programmeNode.Key] = thisProgrammNode; } else { thisChan.programmeNodes.Add(programmeNode.Key, thisProgrammNode); } } } } catch (Exception ex) { addError(ex); } }
public XmlTV(Config configuration = null, string generatorname = "", string generatorurl = "", string datasourcename = "") { try { localErrors = new List <XMLTVError>(); _rootGeneratorName = generatorname == string.Empty ? "XMLTVSharp 1.0" : generatorname; _rootGeneratorUrl = generatorurl == string.Empty ? "https://github.com/M0OPK/SDJSharp" : generatorurl; _rootDataSourceName = datasourcename != string.Empty ? datasourcename : string.Empty; // New document for new data var newDoc = new XmlTVDocument(_rootGeneratorName, _rootGeneratorUrl, _rootDataSourceName); xmlData = new XmlTVData(newDoc); config = configuration ?? new Config(); } catch (Exception ex) { addError(ex); } }
private bool validateProgramme(XmlTVData thisXmlFile, XmlTVData localXmlFile) { try { // Cache this lookup var programmeNodes = thisXmlFile.programmeNodes(); var localProgrammeNodes = localXmlFile.programmeNodes(); // Validate local list against self first var grouped = from localNode in localProgrammeNodes.Cast <XmlNode>() group localNode by new { channelID = localNode.Attributes["channel"].Value, startTime = localNode.Attributes["start"].Value } into groupNode where groupNode.Count() > 1 select groupNode.Key; if (grouped != null && grouped.Count() > 0) { if (config.RemoveDupeProgrammes) { // Try to repair foreach (var dupe in grouped) { // Get all matches var matches = localProgrammeNodes.Cast <XmlNode>().Where (node => node.Attributes["channel"].Value == dupe.channelID && node.Attributes["start"].Value == dupe.startTime); // Delete all but the first one var first = true; foreach (XmlElement match in matches) { if (!first) { localXmlFile.rootNode.RemoveChild(match); } first = false; } } } else { var errorMessage = "Duplicate programmes found:\r\n"; foreach (var dupe in grouped) { errorMessage += $"{dupe.channelID} at {dupe.startTime}\r\n"; } addError(2001, "Duplicate programmes(s) found", XMLTVError.ErrorSeverity.Error, errorMessage, "LoadXmlTV"); } } // Find exact matches in current data var dupeProgrammes = from localNode in localProgrammeNodes.Cast <XmlNode>() join mainNode in programmeNodes.Cast <XmlNode>() on new { joinChannel = localNode.Attributes["channel"].Value ?? "", joinStart = localNode.Attributes["start"].Value ?? "", joinEnd = localNode.Attributes["stop"].Value ?? "" } equals new { joinChannel = mainNode.Attributes["channel"].Value ?? "", joinStart = mainNode.Attributes["start"].Value ?? "", joinEnd = mainNode.Attributes["stop"].Value ?? "" } where mainNode.Attributes["channel"] != null && localNode.Attributes["channel"] != null select localNode; if (dupeProgrammes.Count() != 0) { if (config.SkipImportedDupeProgrammes) { // Remove the duplicates from the input file foreach (var dupeNode in dupeProgrammes) { // Get all matches var matches = localProgrammeNodes.Cast <XmlNode>().Where (node => node.Attributes["channel"].Value == dupeNode.Attributes["channel"].Value && node.Attributes["start"].Value == dupeNode.Attributes["start"].Value && node.Attributes["stop"].Value == dupeNode.Attributes["stop"].Value); // Delete all foreach (XmlElement match in matches) { localXmlFile.rootNode.RemoveChild(match); } } } else { var errorMessage = "Duplicate programmes found:\r\n"; foreach (var dupeNode in dupeProgrammes) { errorMessage += $"{dupeNode.Attributes["channel"].Value ?? "<NULL>"} between {dupeNode.Attributes["start"].Value ?? "<NULL>"} and {dupeNode.Attributes["stop"].Value ?? "<NULL>"}\r\n"; } addError(2001, "Duplicate programmes(s) found", XMLTVError.ErrorSeverity.Error, errorMessage, "LoadXmlTV"); return(true); } } } catch (System.Exception ex) { addError(ex); } return(false); }
private bool validateChannel(XmlTVData thisXmlFile, XmlTVData localXmlFile) { // Largely irrelevant with keys @ToDo, fix it up try { IEnumerable <XmlNode> channelNodes = thisXmlFile.channelNodes; IEnumerable <XmlNode> localChannelNodes = localXmlFile.channelNodes; // Validate local list against self first var grouped = from localNode in localChannelNodes group localNode by localNode.Attributes["id"].Value into groupNode where groupNode.Count() > 1 select groupNode.Key; if (grouped != null && grouped.Count() > 0) { if (config.RemoveDupeChannels) { // Try to repair foreach (var dupe in grouped) { // Get all matches var matches = localChannelNodes.Cast <XmlNode>().Where(node => node.Attributes["id"].Value == dupe); // Delete all but the first one var first = true; foreach (XmlElement match in matches) { if (!first) { localXmlFile.channelData.Remove(match.Attributes["id"].Value); } first = false; } } } else { var errorMessage = "Duplicate channel(s) found (local):\r\n"; foreach (var dupe in grouped) { errorMessage += $"{dupe}"; } addError(1001, "Duplicate channel(s) found", XMLTVError.ErrorSeverity.Error, errorMessage, "LoadXmlTV"); } } // Find matches in current data var dupeChannels = from localNode in localChannelNodes.Cast <XmlNode>() join mainNode in thisXmlFile.channelNodes.Cast <XmlNode>() on localNode.Attributes["id"].Value ?? "" equals mainNode.Attributes["id"].Value ?? "" where mainNode.Attributes["id"] != null && localNode.Attributes["id"] != null select localNode; if (dupeChannels.Count() != 0) { if (config.SkipImportedDupeChannels) { // Remove the duplicates from the input file foreach (var dupeNode in dupeChannels) { // Get all matches var matches = localChannelNodes.Cast <XmlNode>().Where (node => node.Attributes["id"].Value == dupeNode.Attributes["id"].Value); // Delete all foreach (XmlElement match in matches) { localXmlFile.channelData.Remove(match.Attributes["id"].Value); } } } else { var errorMessage = "Duplicate channels found:\r\n"; foreach (var dupeNode in dupeChannels) { errorMessage += $"{dupeNode.Attributes["id"].Value ?? "<NULL>"}\r\n"; } addError(1001, "Duplicate channel(s) found (merge data)", XMLTVError.ErrorSeverity.Error, errorMessage, "LoadXmlTV"); return(true); } } } catch (System.Exception ex) { addError(ex); } return(false); }