SIL.FieldWorks.XWorks.ExportDialog.ExportSemanticDomains C# (CSharp) Method

ExportSemanticDomains() private method

Do the export of the semantic domains list to an HTML document (which is given extension .doc since it is mainly intended to be opened as a Word document, since Word understands the 'page break before' concept). The signature of this method is required by the way it is used as the task of the ProgressDialog. See the first few lines for the required parameters.
private ExportSemanticDomains ( IThreadedProgress progressDlg, object parameters ) : object
progressDlg IThreadedProgress
parameters object
return object
		internal object ExportSemanticDomains(IThreadedProgress progressDlg, object[] parameters)
		{
			string outPath = (string) parameters[0];
			var ft = (FxtType) parameters[1];
			var fxtPath = (string) parameters[2];
			bool allQuestions = (bool) parameters[3];
			m_progressDlg = progressDlg;
			var lists = new List<ICmPossibilityList>();
			lists.Add(m_cache.LangProject.SemanticDomainListOA);
			var exporter = new TranslatedListsExporter(lists, m_translationWritingSystems, progressDlg);
			exporter.ExportLists(outPath);
			FxtType ft1 = ft;
#if DEBUG
			string dirPath = Path.GetTempPath();
#endif
			string xslt = ft1.m_sXsltFiles;
			progressDlg.Position = 0;
			progressDlg.Minimum = 0;
			progressDlg.Maximum = 1;
			progressDlg.Message = xWorksStrings.ProcessingIntoFinalForm;
			int idx = fxtPath.LastIndexOfAny(new[] {'/', '\\'});
			if (idx < 0)
				idx = 0;
			else
				++idx;
			string basePath = fxtPath.Substring(0, idx);
			string sXsltPath = basePath + xslt;
			string sIntermediateFile = ConfiguredExport.RenameOutputToPassN(outPath, 0);

			// The semantic domain xslt uses document('folderStart.xml') to retrieve the list of H1 topics.
			// This is not allowed by default so we must use a settings object to enable it.
			var settings = new XsltSettings(enableDocumentFunction: true, enableScript: false);
			XslCompiledTransform xsl = new XslCompiledTransform();
			xsl.Load(sXsltPath, settings, new XmlUrlResolver());
			var arguments = new XsltArgumentList();
			// If we aren't outputting english we need to ignore it (except possibly for missing items)
			bool ignoreEn = m_translationWritingSystems[0] != m_cache.LanguageWritingSystemFactoryAccessor.GetWsFromStr("en");
			arguments.AddParam(@"ignoreLang", @"", (ignoreEn ? @"en" : @""));
			arguments.AddParam(@"allQuestions", @"", (allQuestions ? @"1" : @"0"));
			using (var writer = FileUtils.OpenFileForWrite(outPath, Encoding.UTF8))
				xsl.Transform(sIntermediateFile, arguments, writer);
			// Deleting them deals with LT-6345,
			// which asked that they be put in the temp folder.
			// But moving them to the temp directory is better for debugging errors.
			FileUtils.MoveFileToTempDirectory(sIntermediateFile, "FieldWorks-Export");
			progressDlg.Step(0);

#if DEBUG
			string s = string.Format("Totally Finished Export Semantic domains at {0}",
				DateTime.Now.ToLongTimeString());
			Debug.WriteLine(s);
#endif
			return null; // method signature is required by ProgressDialog
		}

Usage Example

Example #1
0
        public void ExportSemanticDomains()
        {
            using (var exportDlg = new ExportDialog())
            {
                exportDlg.SetCache(m_cache);
                var tempPath = Path.GetTempFileName();
                var fxt = new ExportDialog.FxtType();
                fxt.m_sXsltFiles = "SemDomQs.xsl";
                var fxtPath = Path.Combine(exportDlg.FxtDirectory, "SemDomQs.xml");
                var wss = new List<int>();
                wss.Add(m_cache.LanguageWritingSystemFactoryAccessor.GetWsFromStr("en"));
                exportDlg.SetTranslationWritingSystems(wss);

                exportDlg.ExportSemanticDomains(new DummyProgressDlg(), new object[] {tempPath, fxt, fxtPath, false});

                string result;
                using (var reader = new StreamReader(tempPath, Encoding.UTF8))
                    result = reader.ReadToEnd();
                File.Delete(tempPath);
                Assert.That(result, Is.StringContaining("What words refer to the sun?"));
                Assert.That(result, Is.Not.StringContaining("1.1.1.11.1.1.1"), "should not output double abbr for en");
                Assert.That(result, Is.Not.StringContaining("class: english"),
                    "English should not give anything the missing translation style");

                wss.Clear();
                wss.Add(m_cache.LanguageWritingSystemFactoryAccessor.GetWsFromStr("fr"));

                exportDlg.ExportSemanticDomains(new DummyProgressDlg(), new object[] {tempPath, fxt, fxtPath, false});

                using (var reader = new StreamReader(tempPath, Encoding.UTF8))
                    result = reader.ReadToEnd();
                File.Delete(tempPath);
                Assert.That(result, Is.Not.StringContaining("What words refer to the sun?"),
                    "french export should not have english questions");
                Assert.That(result, Is.StringContaining("<p class=\"quest1\" lang=\"fr\">(1) Quels mots se"),
                    "French export should have the French question (not english class)");

                exportDlg.ExportSemanticDomains(new DummyProgressDlg(), new object[] {tempPath, fxt, fxtPath, true});
                using (var reader = new StreamReader(tempPath, Encoding.UTF8))
                    result = reader.ReadToEnd();
                File.Delete(tempPath);
                Assert.That(result, Is.StringContaining("<span class=\"english\" lang=\"en\">(1) What words refer to the sun?"),
                    "french export with all questions should have english where french is missing (in red)");
                Assert.That(result, Is.StringContaining("<p class=\"quest1\" lang=\"fr\">(1) Quels mots se"),
                    "French export should have the French question (not red)");
                Assert.That(result, Is.Not.StringContaining("What words refer to everything we can see"),
                    "French export should not have English alternative where French is present");
            }
        }