using System; using System.Collections.Generic; using System.Text; using System.Xml; namespace Nikse.SubtitleEdit.Core.SubtitleFormats { public class OpenDvt : SubtitleFormat { public override string Extension { get { return ".xml"; } } public override string Name { get { return "OpenDVT"; } } public override bool IsTimeBased { get { return true; } } public override bool IsMine(List lines, string fileName) { var sb = new StringBuilder(); lines.ForEach(line => sb.AppendLine(line)); string xmlAsString = sb.ToString().Trim(); if (xmlAsString.Contains("OpenDVT")) { try { var xml = new XmlDocument { XmlResolver = null }; xml.LoadXml(xmlAsString); int numberOfParagraphs = xml.DocumentElement.SelectSingleNode("Lines").ChildNodes.Count; return numberOfParagraphs > 0; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } } return false; } public override string ToText(Subtitle subtitle, string title) { var guid = new Guid().ToString(); string xmlStructure = "" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + " " + Environment.NewLine + " " + guid + " " + Environment.NewLine + " Subtitle Edit " + Environment.NewLine + " 2.9 " + Environment.NewLine + " Nikse.dk " + Environment.NewLine + " " + Environment.NewLine + " www.nikse.dk.com " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + " 1 " + Environment.NewLine + " 3 " + Environment.NewLine + " 25 " + Environment.NewLine + " 1 " + Environment.NewLine + " 06/02/2010 " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + //"C:\Users\Eric\Desktop\Player Folder\Bing\Bing.mpg //"52158464 //"06/02/2009 10:44:37 //"166144 //"OS " " + Environment.NewLine + "" + Environment.NewLine + ""; var xml = new XmlDocument(); xml.LoadXml(xmlStructure); XmlNode lines = xml.DocumentElement.SelectSingleNode("Lines"); int no = 0; foreach (Paragraph p in subtitle.Paragraphs) { XmlNode line = xml.CreateElement("Line"); XmlAttribute id = xml.CreateAttribute("ID"); id.InnerText = no.ToString(); line.Attributes.Append(id); XmlNode stream = xml.CreateElement("Stream"); stream.InnerText = "0"; line.AppendChild(stream); XmlNode timeMS = xml.CreateElement("TimeMs"); timeMS.InnerText = p.StartTime.TotalMilliseconds.ToString(); line.AppendChild(timeMS); XmlNode pageNo = xml.CreateElement("PageNo"); pageNo.InnerText = "1"; line.AppendChild(pageNo); XmlNode lineNo = xml.CreateElement("LineNo"); lineNo.InnerText = "1"; line.AppendChild(lineNo); XmlNode qa = xml.CreateElement("QA"); qa.InnerText = "-"; line.AppendChild(qa); XmlNode text = xml.CreateElement("Text"); text.InnerText = HtmlUtil.RemoveHtmlTags(p.Text, true); line.AppendChild(text); lines.AppendChild(line); no++; } return ToUtf8XmlString(xml); } public override void LoadSubtitle(Subtitle subtitle, List lines, string fileName) { _errorCount = 0; var sb = new StringBuilder(); lines.ForEach(line => sb.AppendLine(line)); var xml = new XmlDocument { XmlResolver = null }; xml.LoadXml(sb.ToString().Trim()); XmlNode div = xml.DocumentElement.SelectSingleNode("Lines"); foreach (XmlNode node in div.ChildNodes) { try { var p = new Paragraph(); XmlNode text = node.SelectSingleNode("Text"); if (text != null) { sb.Clear(); foreach (XmlNode innerNode in text.ChildNodes) { switch (innerNode.Name) { case "br": sb.AppendLine(); break; default: sb.Append(innerNode.InnerText); break; } } p.Text = sb.ToString(); } XmlNode timeMS = node.SelectSingleNode("TimeMs"); if (timeMS != null) { string ms = timeMS.InnerText; long milliseconds; if (long.TryParse(ms, out milliseconds)) p.StartTime = new TimeCode(milliseconds); } p.EndTime = new TimeCode(p.StartTime.TotalMilliseconds + Utilities.GetOptimalDisplayMilliseconds(p.Text)); subtitle.Paragraphs.Add(p); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); _errorCount++; } } subtitle.Renumber(); } } }