/// <summary> /// Remove all slice boundaries in the source script file /// And save it to target script file. /// </summary> /// <param name="sourceScriptFilePath">Source script file.</param> /// <param name="targetScriptFilePath">Target script file.</param> /// <returns>Data error set found.</returns> public static ErrorSet RemoveSliceBoundary(string sourceScriptFilePath, string targetScriptFilePath) { // Parameters validation if (string.IsNullOrEmpty(sourceScriptFilePath)) { throw new ArgumentNullException("sourceScriptFilePath"); } if (string.IsNullOrEmpty(targetScriptFilePath)) { throw new ArgumentNullException("targetScriptFilePath"); } XmlScriptFile script = new XmlScriptFile(); // Keep comments in XmlScript file XmlScriptFile.ContentControler controler = new XmlScriptFile.ContentControler(); controler.LoadComments = true; script.Load(sourceScriptFilePath, controler); foreach (ScriptItem item in script.Items) { foreach (ScriptWord word in item.AllWords) { if (!string.IsNullOrEmpty(word.Pronunciation)) { word.Pronunciation = Pronunciation.RemoveUnitBoundary(word.Pronunciation); } } } // Save comments in XmlScript file script.Save(targetScriptFilePath, Encoding.Unicode); return script.ErrorSet; }
/// <summary> /// Slice the pronunciation of one script file. /// </summary> /// <param name="scriptFilePath">Source file.</param> /// <param name="targetFilePath">Target file.</param> /// <returns>Data error set found.</returns> public ErrorSet Slice(string scriptFilePath, string targetFilePath) { XmlScriptFile script = new XmlScriptFile(); XmlScriptFile.ContentControler controler = new XmlScriptFile.ContentControler(); controler.LoadComments = true; script.Load(scriptFilePath, controler); ErrorSet errorSet = Slice(script); script.Save(targetFilePath); errorSet.Merge(script.ErrorSet); return errorSet; }
/// <summary> /// Clone the script item. /// </summary> /// <returns>Cloned script item.</returns> public ScriptItem Clone() { StringBuilder sb = new StringBuilder(); using (XmlWriter sw = XmlWriter.Create(sb)) { XmlScriptFile.ContentControler contentControler = new XmlScriptFile.ContentControler(); contentControler.SaveComments = true; WriteToXml(sw, contentControler, Language); sw.Flush(); } ScriptItem clonedScriptItem = null; StringReader sr = new StringReader(sb.ToString()); try { using (XmlTextReader xtr = new XmlTextReader(sr)) { sr = null; while (xtr.Read()) { if (xtr.NodeType == XmlNodeType.Element && xtr.Name == "si") { clonedScriptItem = XmlScriptFile.LoadItem(xtr, null, Language); } } } } finally { if (null != sr) { sr.Dispose(); } } return clonedScriptItem; }
/// <summary> /// Write dleeted words to comments. /// </summary> /// <param name="scriptLanguage">Script language.</param> private void WriteDeletedWordsToComments(Language scriptLanguage) { if (TtsXmlComments.TtsXmlStatusDict.ContainsKey(DeletedWordStatusName)) { TtsXmlComments.TtsXmlStatusDict.Remove(DeletedWordStatusName); } foreach (ScriptWord scriptWord in _deletedWordsDict.Keys) { scriptWord.TtsXmlComments.Reset(); // Update deleted words' original content StringBuilder sb = new StringBuilder(); scriptWord.TtsXmlComments.TtsXmlStatusDict.Clear(); using (XmlWriter sw = XmlWriter.Create(sb)) { XmlScriptFile.ContentControler scriptContentController = new XmlScriptFile.ContentControler(); scriptContentController.SaveComments = true; scriptWord.WriteToXml(sw, scriptContentController, scriptLanguage); sw.Flush(); _deletedWordsDict[scriptWord].OriginalValue = sb.ToString(); _deletedWordsDict[scriptWord].Position = XmlScriptCommentHelper.GetDeletedWordPosition(scriptWord); _deletedWordsDict[scriptWord].DelIndex = XmlScriptCommentHelper.GetDeletedWordIndex(scriptWord); // Add deleted words to status list. TtsXmlComments.AppendStatus(_deletedWordsDict[scriptWord], true); } } }
/// <summary> /// Load all the script items from a folder /// Note: Here don't validate the content, But duplicate item ID is not allowed. /// </summary> /// <param name="sourceDir">Script dir.</param> /// <param name="errors">Errors happened.</param> /// <returns>Loaded items collection.</returns> public static Collection<ScriptItem> LoadScriptsWithoutValidation(string sourceDir, ErrorSet errors) { if (string.IsNullOrEmpty(sourceDir)) { throw new ArgumentNullException("sourceDir"); } if (errors == null) { throw new ArgumentNullException("errors"); } Collection<ScriptItem> items = new Collection<ScriptItem>(); Dictionary<string, string> ids = new Dictionary<string, string>(); string pattern = @"*" + XmlScriptFile.Extension; Language language = Language.Neutral; foreach (string file in Directory.GetFiles(sourceDir, pattern, SearchOption.AllDirectories)) { XmlScriptFile script = new XmlScriptFile(); XmlScriptFile.ContentControler controler = new XmlScriptFile.ContentControler(); controler.LoadComments = true; script.Load(file, controler); if (language == Language.Neutral) { language = script.Language; } else if (language != script.Language) { throw new InvalidDataException(Helper.NeutralFormat( "The language name in File [{0}] is different from other files.", file)); } errors.Merge(script.ErrorSet); foreach (ScriptItem item in script.Items) { if (ids.ContainsKey(item.Id)) { errors.Add(ScriptError.DuplicateItemId, item.Id); } else { item.ScriptFile = null; items.Add(item); } } } return items; }
/// <summary> /// Merge scripts in a folder into a script file /// Error items are removed from the output file. /// </summary> /// <param name="scriptDir">Dir conataining script file.</param> /// <param name="targetFile">Merged file.</param> /// <param name="resetId">True means resetting id.</param> /// <param name="validateSetting">Validation setting.</param> /// <returns>ErrorSet.</returns> public static ErrorSet MergeScripts(string scriptDir, string targetFile, bool resetId, XmlScriptValidateSetting validateSetting) { XmlScriptFile.ContentControler controler = new XmlScriptFile.ContentControler(); controler.SaveComments = false; return MergeScripts(scriptDir, targetFile, resetId, validateSetting, controler); }