SIL.FieldWorks.Common.Controls.ObtainProjectMethod.RetrieveDefaultWritingSystemIdsFromLift C# (CSharp) Method

RetrieveDefaultWritingSystemIdsFromLift() static private method

Figure out the best default vernacular and analysis writing systems to use for the input, presumed to represent a LIFT file. We get the vernacular WS from the first form element nested in a lexical-unit with a lang attribute, and the analysis WS form the first form element nested in a definition or gloss with a lang attribute.
static private RetrieveDefaultWritingSystemIdsFromLift ( XmlReader reader, string &vernWs, string &analysisWs ) : void
reader XmlReader
vernWs string
analysisWs string
return void
		internal static void RetrieveDefaultWritingSystemIdsFromLift(XmlReader reader, out string vernWs, out string analysisWs)
		{
			vernWs = analysisWs = null;
			bool inLexicalUnit = false;
			bool inDefnOrGloss = false;
			while (reader.Read())
			{
				switch (reader.NodeType)
				{
					case XmlNodeType.Element:
						switch (reader.Name)
						{
							case "lexical-unit":
								inLexicalUnit = true;
								break;
							case "definition":
							case "gloss":
								inDefnOrGloss = true;
								break;
							case "form":
								if (inLexicalUnit && string.IsNullOrWhiteSpace(vernWs))
									vernWs = reader.GetAttribute("lang"); // pathologically may leave it null, if so keep trying.
								if (inDefnOrGloss && string.IsNullOrWhiteSpace(analysisWs))
									analysisWs = reader.GetAttribute("lang"); // pathologically may leave it null, if so keep trying.
								if (!string.IsNullOrWhiteSpace(vernWs) && !string.IsNullOrWhiteSpace(analysisWs))
									return; // got all we need, skip rest of file.
								break;
						}
						break;
					case XmlNodeType.EndElement:
						switch (reader.Name)
						{
							case "lexical-unit":
								inLexicalUnit = false;
								break;
							case "definition":
							case "gloss":
								inDefnOrGloss = false;
								break;
						}
						break;
				}
			}
			if (string.IsNullOrWhiteSpace(vernWs))
				vernWs = "fr"; // Arbitrary default (consistent with default creation of new project) if we don't find an entry
			if (string.IsNullOrWhiteSpace(analysisWs))
				analysisWs = "en"; // Arbitrary default if we don't find a sense
		}

Usage Example

Example #1
0
        public void DefaultWritingSystemsFromLiftWithNoEntries_ReturnsFrenchAndEnglish()
        {
            string input = @"<?xml version='1.0' encoding='utf-8'?>
<lift
	version='0.13'
	producer='SIL.FLEx 7.2.5.41073'>
</lift>";

            using (var stringReader = new StringReader(input))
                using (XmlReader reader = XmlReader.Create(stringReader))
                {
                    string vernWs, analysisWs;
                    ObtainProjectMethod.RetrieveDefaultWritingSystemIdsFromLift(reader, out vernWs, out analysisWs);
                    reader.Close();
                    Assert.That(vernWs, Is.EqualTo("fr"));
                    Assert.That(analysisWs, Is.EqualTo("en"));
                }
        }
All Usage Examples Of SIL.FieldWorks.Common.Controls.ObtainProjectMethod::RetrieveDefaultWritingSystemIdsFromLift