public void Write_WritePrivateUseWsFromFieldWorksLdmlThenNormalLdml_ContainsVersion1()
		{
			using (var badFlexLdml = new TempFile())
			{
				using (var version1Ldml = new TempFile())
				{
					var namespaceManager = new XmlNamespaceManager(new NameTable());
					namespaceManager.AddNamespace("palaso", "urn://palaso.org/ldmlExtensions/v1");
					WriteVersion0Ldml("x-en", "", "", "x-private", badFlexLdml);
					var wsV0 = new WritingSystemDefinition();
					var adaptor = new LdmlDataMapper();
					adaptor.Read(badFlexLdml.Path, wsV0);
					adaptor.Write(badFlexLdml.Path, wsV0, new MemoryStream(File.ReadAllBytes(badFlexLdml.Path)));
					var wsV1 = new WritingSystemDefinition();
					adaptor.Write(version1Ldml.Path, wsV1, null);
					AssertThatVersionIs(2, version1Ldml);
				}
			}
		}
		public void Write_OriginalWasFlexPrivateUseWritingSystemButNowChangedPrivateUse_IdentityElementChangedToPalasoWay()
		{
			using (var file = new TempFile())
			{
				WriteVersion0Ldml("x-en", "", "", "x-private", file);
				var ws = new WritingSystemDefinition();
				var adaptor = new LdmlDataMapper();
				adaptor.Read(file.Path, ws);
				AssertThatRfcTagComponentsOnWritingSystemAreEqualTo(ws, "", "", "", "x-en-private");
				ws.Variant = "x-en-changed";
				adaptor.Write(file.Path, ws, new MemoryStream(File.ReadAllBytes(file.Path)));
				AssertThatLdmlMatches("", "", "", "x-en-changed", file);
			}
		}
		public void Read_ReadPrivateUseWsFromFieldWorksLdmlThenNormalLdmlMissingVersion1Element_Throws()
		{
			using (var badFlexLdml = new TempFile())
			{
				using (var version1Ldml = new TempFile())
				{
					WriteVersion0Ldml("x-en", "", "", "x-private", badFlexLdml);
					WriteVersion0Ldml("en", "", "", "", version1Ldml);
					var wsV1 = new WritingSystemDefinition();
					var wsV0 = new WritingSystemDefinition();
					var adaptor = new LdmlDataMapper();
					adaptor.Read(badFlexLdml.Path, wsV0);
					Assert.Throws<ApplicationException>(()=>adaptor.Read(version1Ldml.Path, wsV1));
				}
			}
		}
		public void WriteRoundTrip_LdmlIsValidLanguageStartingWithX_LdmlIsUnchanged()
		{
			using (var file = new TempFile())
			{
				WriteVersion2Ldml("xh", "", "", "", file);
				var adaptor = new LdmlDataMapper();
				var ws = new WritingSystemDefinition();
				adaptor.Read(file.Path, ws);
				adaptor.Write(file.Path, ws, new MemoryStream(File.ReadAllBytes(file.Path), true), WritingSystemCompatibility.Flex7V0Compatible);
				AssertThatLdmlMatches("xh", "", "", "", file);
				AssertThatVersionIs(2, file);
			}
		}
		public void RoundTripFlexPrivateUseWritingSystem_LanguageAndPrivateUsePopulated()
		{
			using (var file = new TempFile())
			{
				WriteVersion0Ldml("x-en", "", "", "x-private", file);
				var ws = new WritingSystemDefinition();
				string originalLdml = File.ReadAllText(file.Path);
				var adaptor = new LdmlDataMapper();
				adaptor.Read(file.Path, ws);
				AssertThatRfcTagComponentsOnWritingSystemAreEqualTo(ws, "", "", "", "x-en-private");
				adaptor.Write(file.Path, ws, new MemoryStream(File.ReadAllBytes(file.Path)), WritingSystemCompatibility.Flex7V0Compatible);
				Assert.That(File.ReadAllText(file.Path), Is.EqualTo(originalLdml));
			}
		}
 /// <summary>
 /// Exports the current writing system to a file.
 /// </summary>
 /// <param name="filePath"></param>
 public void ExportCurrentWritingSystemAsFile(string filePath)
 {
     if (!HasCurrentSelection) {
         throw new InvalidOperationException ("Unable to export current selection when there is no current selection.");
     }
     LdmlDataMapper adaptor = new LdmlDataMapper ();
     adaptor.Write (filePath, (WritingSystemDefinition)_currentWritingSystem, null);
 }
		public void RoundtripKnownKeyboards()
		{
			var ldmlAdaptor = new LdmlDataMapper();

			Keyboard.Controller = new MyKeyboardController();

			const string sortRules = "(A̍ a̍)";
			var wsWithKnownKeyboards = new WritingSystemDefinition();
			var keyboard1 = new DefaultKeyboardDefinition();
			keyboard1.Locale = "en-US";
			keyboard1.Layout = "MyFavoriteKeyboard";
			keyboard1.OperatingSystem = PlatformID.MacOSX; // pick something that for sure won't be our default
			wsWithKnownKeyboards.AddKnownKeyboard(keyboard1);

			var keyboard2 = new DefaultKeyboardDefinition();
			keyboard2.Locale = "en-GB";
			keyboard2.Layout = "SusannasFavoriteKeyboard";
			keyboard2.OperatingSystem = PlatformID.Unix;
			wsWithKnownKeyboards.AddKnownKeyboard(keyboard2);

			var wsFromLdml = new WritingSystemDefinition();
			using (var tempFile = new TempFile())
			{
				ldmlAdaptor.Write(tempFile.Path, wsWithKnownKeyboards, null);
				ldmlAdaptor.Read(tempFile.Path, wsFromLdml);
			}

			var knownKeyboards = wsFromLdml.KnownKeyboards.ToList();
			Assert.That(knownKeyboards, Has.Count.EqualTo(2), "restored WS should have known keyboards");
			var keyboard1FromLdml = knownKeyboards[0];
			Assert.That(keyboard1FromLdml.Layout, Is.EqualTo("MyFavoriteKeyboard"));
			Assert.That(keyboard1FromLdml.Locale, Is.EqualTo("en-US"));
			Assert.That(keyboard1FromLdml.OperatingSystem, Is.EqualTo(PlatformID.MacOSX));
			Assert.That(keyboard1FromLdml, Is.InstanceOf<MyKeyboardDefn>(), "Reader should have used controller to create keyboard defn");

			var keyboard2FromLdml = knownKeyboards[1];
			Assert.That(keyboard2FromLdml.Layout, Is.EqualTo("SusannasFavoriteKeyboard"));
			Assert.That(keyboard2FromLdml.Locale, Is.EqualTo("en-GB"));
			Assert.That(keyboard2FromLdml.OperatingSystem, Is.EqualTo(PlatformID.Unix));
		}
		public void Write_WritingSystemWasloadedFromLdmlThatContainedLayoutInfo_LayoutInfoIsOnlyWrittenOnce()
		{
			using (var file = new TempFile())
			{
				//create an ldml file to read that contains layout info
				var adaptor = new LdmlDataMapper();
				var ws = WritingSystemDefinition.Parse("en-Zxxx-x-audio");
				ws.RightToLeftScript = true;
				adaptor.Write(file.Path, ws, null);

				//read the file and write it out unchanged
				var ws2 = new WritingSystemDefinition();
				adaptor.Read(file.Path, ws2);
				adaptor.Write(file.Path, ws2, new MemoryStream(File.ReadAllBytes(file.Path)));

				AssertThatXmlIn.File(file.Path).HasNoMatchForXpath("/ldml/layout[2]");
			}
		}
		public void Read_ValidLanguageTagStartingWithXButVersion0_Throws()
		{
			using (var file = new TempFile())
			{
				WriteVersion0Ldml("xh", "", "", "", file);
				var adaptor = new LdmlDataMapper();
				Assert.That(() => adaptor.Read(file.Path, new WritingSystemDefinition()), Throws.Exception.TypeOf<ApplicationException>());
			}
		}
		public void Read_LdmlContainsOnlyPrivateUse_IsoAndprivateUseSetCorrectly()
		{
			const string ldmlWithOnlyPrivateUse = "<ldml><identity><version number=\"\" /><language type=\"\" /><variant type=\"x-private-use\" /></identity><special xmlns:palaso=\"urn://palaso.org/ldmlExtensions/v1\" ><palaso:version value=\"2\" /></special></ldml>";


			string pathToLdmlWithEmptyCollationElement = Path.GetTempFileName();
			try
			{
				File.WriteAllText(pathToLdmlWithEmptyCollationElement, ldmlWithOnlyPrivateUse);

				var adaptor = new LdmlDataMapper();
				var wsFromLdml = new WritingSystemDefinition();
				adaptor.Read(pathToLdmlWithEmptyCollationElement, wsFromLdml);
				var ws = new WritingSystemDefinition();
				adaptor.Read(pathToLdmlWithEmptyCollationElement, ws);
				Assert.That(wsFromLdml.Language, Is.EqualTo(String.Empty));
				Assert.That(wsFromLdml.Variant, Is.EqualTo("x-private-use"));
			}
			finally
			{
				File.Delete(pathToLdmlWithEmptyCollationElement);
			}
		}
		public void Write_LdmlIsNicelyFormatted()
		{
#if MONO
				// mono inserts \r\n\t before xmlns where windows doesn't
			string expectedFileContent =
#region filecontent
@"<?xml version='1.0' encoding='utf-8'?>
<ldml>
	<identity>
		<version
			number='' />
		<generation
			date='0001-01-01T00:00:00' />
		<language
			type='en' />
		<script
			type='Zxxx' />
		<territory
			type='US' />
		<variant
			type='x-audio' />
	</identity>
	<collations />
	<special
		xmlns:palaso='urn://palaso.org/ldmlExtensions/v1'>
		<palaso:abbreviation
			value='en' />
		<palaso:languageName
			value='English' />
		<palaso:version
			value='2' />
	</special>
</ldml>".Replace("'", "\"").Replace("\n", "\r\n");
#endregion

#else
			string expectedFileContent =
#region filecontent
@"<?xml version='1.0' encoding='utf-8'?>
<ldml>
	<identity>
		<version
			number='' />
		<generation
			date='0001-01-01T00:00:00' />
		<language
			type='en' />
		<script
			type='Zxxx' />
		<territory
			type='US' />
		<variant
			type='x-audio' />
	</identity>
	<collations />
	<special xmlns:palaso='urn://palaso.org/ldmlExtensions/v1'>
		<palaso:abbreviation
			value='en' />
		<palaso:languageName
			value='English' />
		<palaso:version
			value='2' />
	</special>
</ldml>".Replace("'", "\"").Replace("\n", "\r\n").Replace("\r\r\n", "\r\n");

#endregion

#endif
			using (var file = new TempFile())
			{
				//Create an ldml fiel to read
				var adaptor = new LdmlDataMapper();
				var ws = WritingSystemDefinition.Parse("en-Zxxx-x-audio");
				adaptor.Write(file.Path, ws, null);

				//change the read writing system and write it out again
				var ws2 = new WritingSystemDefinition();
				adaptor.Read(file.Path, ws2);
				ws2.Region = "US";
				adaptor.Write(file.Path, ws2, new MemoryStream(File.ReadAllBytes(file.Path)));

				Assert.That(File.ReadAllText(file.Path), Is.EqualTo(expectedFileContent));
			}
		}
		//WS-33992
		public void Read_LdmlContainsEmptyCollationElement_SortUsingIsSetToSameAsIfNoCollationElementExisted()
		{
			const string ldmlWithEmptyCollationElement = "<ldml><!--Comment--><identity><version number=\"\" /><generation date=\"0001-01-01T00:00:00\" /><language type=\"qaa\" /></identity><dates /><collations><collation></collation></collations><special xmlns:palaso=\"urn://palaso.org/ldmlExtensions/v1\" ><palaso:version value=\"2\" /></special></ldml>";
			const string ldmlwithNoCollationElement = "<ldml><!--Comment--><identity><version number=\"\" /><generation date=\"0001-01-01T00:00:00\" /><language type=\"qaa\" /></identity><dates /><collations/><special xmlns:palaso=\"urn://palaso.org/ldmlExtensions/v1\" ><palaso:version value=\"2\" /></special></ldml>";

			string pathToLdmlWithEmptyCollationElement = Path.GetTempFileName();
			try
			{
				File.WriteAllText(pathToLdmlWithEmptyCollationElement, ldmlWithEmptyCollationElement);
				string pathToLdmlWithNoCollationElement = Path.GetTempFileName();
				try
				{
					File.WriteAllText(pathToLdmlWithNoCollationElement, ldmlwithNoCollationElement);


					var adaptor = new LdmlDataMapper();
					var wsFromEmptyCollationElement = new WritingSystemDefinition();
					adaptor.Read(pathToLdmlWithEmptyCollationElement, wsFromEmptyCollationElement);
					var wsFromNoCollationElement = new WritingSystemDefinition();
					adaptor.Read(pathToLdmlWithNoCollationElement, wsFromNoCollationElement);

					Assert.AreEqual(wsFromNoCollationElement.SortUsing, wsFromEmptyCollationElement.SortUsing);
				}
				finally
				{
					File.Delete(pathToLdmlWithNoCollationElement);
				}
			}
			finally
			{
				File.Delete(pathToLdmlWithEmptyCollationElement);
			}
		}
		public void SetUp()
		{
			_adaptor = new LdmlDataMapper();
			_ws = new WritingSystemDefinition("en", "Latn", "US", string.Empty, "eng", false);
		}
		public void ReadWindowsLcid()
		{
			var ldmlAdaptor = new LdmlDataMapper();
			var wsFromLdml = new WritingSystemDefinition();
			using (var tempFile = new TempFile())
			{
				using (var writer = new StreamWriter(tempFile.Path, false, Encoding.UTF8))
				{
					writer.Write(
@"<?xml version='1.0' encoding='utf-8'?>
<ldml>
	<identity>
		<version
			number='' />
		<language
			type='qaa' />
		<variant
			type='x-lel' />
	</identity>
	<collations />

	<special xmlns:fw='urn://fieldworks.sil.org/ldmlExtensions/v1'>
		<fw:graphiteEnabled
			value='False' />
		<fw:windowsLCID
			value='1036' />
	</special>
</ldml>".Replace("'", "\""));
				}
				ldmlAdaptor.Read(tempFile.Path, wsFromLdml);
			}
			Assert.That(wsFromLdml.WindowsLcid, Is.EqualTo("1036"));
		}
		public void Read_V0Ldml_ThrowFriendlyException()
		{
			using (var file = new TempFile())
			{
				WriteVersion0Ldml("en", "", "", "", file);
				var ws = new WritingSystemDefinition();
				var dataMapper = new LdmlDataMapper();
				Assert.That(() => dataMapper.Read(file.Path, ws),
								Throws.Exception.TypeOf<ApplicationException>()
										.With.Property("Message")
										.EqualTo(String.Format("The LDML tag 'en' is version 0.  Version {1} was expected.", file.Path,
																	  WritingSystemDefinition.LatestWritingSystemDefinitionVersion)));
			}
		}
		public void WriteNoRoundTrip_LdmlIsFlexPrivateUseFormatlanguageAndScript_LdmlIsChanged()
		{
			using (var file = new TempFile())
			{
				WriteVersion0Ldml("x-en", "Zxxx", "", "", file);
				var adaptor = new LdmlDataMapper();
				var ws = new WritingSystemDefinition();
				adaptor.Read(file.Path, ws);
				adaptor.Write(file.Path, ws, new MemoryStream(File.ReadAllBytes(file.Path), false), WritingSystemCompatibility.Strict);
				AssertThatLdmlMatches("qaa", "Zxxx", "", "x-en", file);
			}
		}
		public void RoundTrippingLdmlDoesNotDuplicateSections()
		{
			using(var roundTripOut2 = new TempFile())
			using(var roundTripOut = new TempFile())
			using(var tempFile = new TempFile())
			{

				using(var writer = new StreamWriter(tempFile.Path, false, Encoding.UTF8))
				{
					writer.Write(
						@"<?xml version='1.0' encoding='utf-8'?>
<ldml>
	<identity>
		<version
			number='' />
		<language
			type='qaa' />
		<variant
			type='x-lel' />
	</identity>
	<collations />

	<special xmlns:fw='urn://fieldworks.sil.org/ldmlExtensions/v1'>
		<fw:graphiteEnabled
			value='False' />
		<fw:windowsLCID
			value='1036' />
	</special>
</ldml>".Replace("'", "\""));
				}
				var ws = new WritingSystemDefinition();
				var dataMapper = new LdmlDataMapper();

				dataMapper.Read(tempFile.Path, ws);
				var keyboard1 = new DefaultKeyboardDefinition();
				keyboard1.Locale = "en-US";
				keyboard1.Layout = "MyFavoriteKeyboard";
				keyboard1.OperatingSystem = PlatformID.MacOSX; // pick something that for sure won't be our default
				ws.AddKnownKeyboard(keyboard1);
				using(var fileStream = new FileStream(tempFile.Path, FileMode.Open))
				{
					dataMapper.Write(roundTripOut.Path, ws, fileStream);
				}
				AssertThatXmlIn.File(roundTripOut.Path).HasSpecifiedNumberOfMatchesForXpath("/ldml/special/*[local-name()='windowsLCID']", 1);
				var secondTripMapper = new LdmlDataMapper();
				var secondTripWs = new WritingSystemDefinition();
				secondTripMapper.Read(roundTripOut.Path, secondTripWs);
				secondTripWs.AddKnownKeyboard(new DefaultKeyboardDefinition()
					{
						Locale = "qaa",
						Layout = "x-tel",
						OperatingSystem = PlatformID.Xbox
					});
				secondTripWs.WindowsLcid = "1037";
				using(var fileStream = new FileStream(roundTripOut.Path, FileMode.Open))
				{
					secondTripMapper.Write(roundTripOut2.Path, secondTripWs, fileStream);
				}
				AssertThatXmlIn.File(roundTripOut2.Path).HasSpecifiedNumberOfMatchesForXpath("/ldml/special/*[local-name()='windowsLCID']", 1); //Element duplicated on round trip
			}
		}
		public void WriteRoundTrip_LdmlIsFlexPrivateUseFormat_LdmlIsUnchanged()
		{
			using (var file = new TempFile())
			{
				WriteVersion0Ldml("x-en", "", "", "", file);
				var adaptor = new LdmlDataMapper();
				var ws = new WritingSystemDefinition();
				adaptor.Read(file.Path, ws);
				adaptor.Write(file.Path, ws, new MemoryStream(File.ReadAllBytes(file.Path), true), WritingSystemCompatibility.Flex7V0Compatible);
				AssertThatLdmlMatches("x-en", "", "", "", file);
			}
		}
 /// <summary>
 /// Imports the given file into the writing system store.
 /// </summary>
 /// <param name="fileName">Full path of file to import</param>
 public void ImportFile(string fileName)
 {
     if (!_usingRepository)
     {
         throw new InvalidOperationException("Unable to import file when not using writing system store.");
     }
     if (fileName == null)
     {
         throw new ArgumentNullException("fileName");
     }
     if (!System.IO.File.Exists(fileName))
     {
         throw new ArgumentException("File does not exist.", "fileName");
     }
     LdmlDataMapper _adaptor = new LdmlDataMapper();
     var ws = _writingSystemRepository.CreateNew();
     _adaptor.Read(fileName, (WritingSystemDefinition)ws);
     WritingSystemDefinitions.Add(ws);
     OnAddOrDelete();
     CurrentDefinition = ws;
 }
		public void RoundtripSimpleCustomSortRules_WS33715()
		{
			var ldmlAdaptor = new LdmlDataMapper();

			const string sortRules = "(A̍ a̍)";
			var wsWithSimpleCustomSortRules = new WritingSystemDefinition();
			wsWithSimpleCustomSortRules.SortUsing = WritingSystemDefinition.SortRulesType.CustomSimple;
			wsWithSimpleCustomSortRules.SortRules = sortRules;

			var wsFromLdml = new WritingSystemDefinition();
			using (var tempFile = new TempFile())
			{
				ldmlAdaptor.Write(tempFile.Path, wsWithSimpleCustomSortRules, null);
				ldmlAdaptor.Read(tempFile.Path, wsFromLdml);
			}

			Assert.AreEqual(sortRules, wsFromLdml.SortRules);
		}