using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class IssXml : SubtitleFormat
{
private static readonly Regex RegexTimeCodes = new Regex(@"^\d\d:\d\d:\d\d:\d\d$", RegexOptions.Compiled); //00:02:56:02
public override string Extension => ".ats";
public override string Name => "ATS ISS";
public override string ToText(Subtitle subtitle, string title)
{
string xmlStructure =
"" + Environment.NewLine +
"" + Environment.NewLine +
" " + Environment.NewLine +
" " + Environment.NewLine +
" " + Environment.NewLine +
" " + Environment.NewLine +
" " + Environment.NewLine +
" " + Environment.NewLine +
" " + Environment.NewLine +
" " + Environment.NewLine +
" " + Environment.NewLine +
" " + Environment.NewLine +
" " + Environment.NewLine +
" " + Environment.NewLine +
" " + Environment.NewLine +
"";
string xmlTrackStructure =
"" + Environment.NewLine +
" " + Environment.NewLine +
" " + Environment.NewLine +
" 00:03:57:16" + Environment.NewLine +
"";
var xml = new XmlDocument { XmlResolver = null };
xml.LoadXml(xmlStructure);
// TODO: Set variables...
XmlNode trackNode = xml.DocumentElement.SelectSingleNode("TrackList/Track/StItemList");
int number = 1;
foreach (Paragraph p in subtitle.Paragraphs)
{
// starttime + text
XmlNode stItem = xml.CreateElement("StItem");
stItem.InnerXml = xmlTrackStructure;
XmlAttribute memo = xml.CreateAttribute("Memo");
memo.InnerText = string.Empty;
stItem.Attributes.Append(memo);
XmlAttribute tc = xml.CreateAttribute("TC");
tc.InnerText = ((int)Math.Round(p.StartTime.TotalMilliseconds)).ToString();
stItem.Attributes.Append(tc);
XmlAttribute row = xml.CreateAttribute("Row");
row.InnerText = number.ToString();
stItem.Attributes.Append(row);
XmlNodeList list = stItem.SelectNodes("StTextList/StText");
list[0].InnerText = p.Text;
list[2].InnerText = p.StartTime.ToHHMMSSFF();
trackNode.AppendChild(stItem);
number++;
// endtime
stItem = xml.CreateElement("StItem");
stItem.InnerXml = xmlTrackStructure;
memo = xml.CreateAttribute("Memo");
memo.InnerText = string.Empty;
stItem.Attributes.Append(memo);
tc = xml.CreateAttribute("TC");
tc.InnerText = ((int)Math.Round(p.EndTime.TotalMilliseconds)).ToString();
stItem.Attributes.Append(tc);
row = xml.CreateAttribute("Row");
row.InnerText = number.ToString();
stItem.Attributes.Append(row);
list = stItem.SelectNodes("StTextList/StText");
list[0].InnerText = string.Empty;
list[2].InnerText = p.EndTime.ToString();
trackNode.AppendChild(stItem);
number++;
}
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));
if (!sb.ToString().Contains(" 1)
{
text = (list[0].InnerText + Environment.NewLine + list[1].InnerText).Trim();
}
else if (list.Count == 1)
{
text = list[0].InnerText.Trim();
}
p.Text = text;
subtitle.Paragraphs.Add(p);
}
catch
{
_errorCount++;
}
}
subtitle.Renumber();
}
catch
{
_errorCount = 1;
return;
}
int i = 0;
foreach (Paragraph p in subtitle.Paragraphs)
{
i++;
var next = subtitle.GetParagraphOrDefault(i);
if (next != null)
{
p.EndTime.TotalMilliseconds = next.StartTime.TotalMilliseconds;
}
}
subtitle.RemoveEmptyLines();
}
}
}