Example #1
0
        /// <summary>
        /// Creates and returns a keyboard definition object based on the layout and locale.
        /// </summary>
        /// <remarks>The keyboard controller implementing this method will have to check the
        /// availability of the keyboard and what engine provides it.</remarks>
        public virtual IKeyboardDefinition CreateKeyboard(string id, KeyboardFormat format, IEnumerable <string> urls)
        {
            DefaultKeyboardDefinition keyboard;

            if (!_keyboards.TryGetValue(id, out keyboard))
            {
                string[] parts = id.Split('_');
                string   layout, locale = null;
                if (parts.Length == 1)
                {
                    layout = parts[0];
                }
                else
                {
                    locale = parts[0];
                    layout = parts[1];
                }
                keyboard       = new DefaultKeyboardDefinition(id, layout, layout, locale, true);
                _keyboards[id] = keyboard;
            }

            keyboard.Format = format;

            // Clear any exisiting URL list
            keyboard.Urls.Clear();
            foreach (string url in urls)
            {
                keyboard.Urls.Add(url);
            }
            return(keyboard);
        }
		public void Write_KeyboardInfoIsOnlyWrittenOnce()
		{
			using (var environment = new TestEnvironment())
			{
				var ws = new WritingSystemDefinition("en-Zxxx-x-audio");
				var keyboard1 = new DefaultKeyboardDefinition("en-US_MyFavoriteKeyboard", "MyFavoriteKeyboard", "MyFavoriteKeyboard", "en-US", true);
				keyboard1.Format = KeyboardFormat.Msklc;
				ws.KnownKeyboards.Add(keyboard1);
				var keyboard2 = new DefaultKeyboardDefinition("en-GB_SusannasFavoriteKeyboard", "SusannasFavoriteKeyboard", "SusannasFavoriteKeyboard", "en-GB", true);
				keyboard2.Format = KeyboardFormat.Msklc;
				ws.KnownKeyboards.Add(keyboard2);

				var ldmlAdaptor = new LdmlDataMapper(new TestWritingSystemFactory());


				ldmlAdaptor.Write(environment.FilePath("test.ldml"), ws, null);

				//read the file and write it out unchanged
				var ws2 = new WritingSystemDefinition();
				ldmlAdaptor.Read(environment.FilePath("test.ldml"), ws2);
				ldmlAdaptor.Write(environment.FilePath("test.ldml"), ws2, new MemoryStream(File.ReadAllBytes(environment.FilePath("test.ldml"))));

				AssertThatXmlIn.File(environment.FilePath("test.ldml")).HasSpecifiedNumberOfMatchesForXpath("/ldml/special/sil:external-resources/sil:kbd[@id='en-US_MyFavoriteKeyboard' and @type='msklc']", 1, environment.NamespaceManager);
				AssertThatXmlIn.File(environment.FilePath("test.ldml")).HasSpecifiedNumberOfMatchesForXpath("/ldml/special/sil:external-resources/sil:kbd[@id='en-GB_SusannasFavoriteKeyboard' and @type='msklc']", 1, environment.NamespaceManager);
			}
		}
		public void RoundtripKnownKeyboards()
		{
			var ldmlAdaptor = new LdmlDataMapper(new TestWritingSystemFactory());

			//Keyboard.Controller = new MyKeyboardController();

			var wsWithKnownKeyboards = new WritingSystemDefinition();
			// ID name layout local available
			var keyboard1 = new DefaultKeyboardDefinition("en-US_MyFavoriteKeyboard", "MyFavoriteKeyboard", "MyFavoriteKeyboard", "en-US", true);
			keyboard1.Format = KeyboardFormat.Msklc;
			wsWithKnownKeyboards.KnownKeyboards.Add(keyboard1);

			var keyboard2 = new DefaultKeyboardDefinition("en-GB_SusannasFavoriteKeyboard", "SusannasFavoriteKeyboard", "SusannasFavoriteKeyboard", "en-GB", true);
			keyboard2.Format = KeyboardFormat.Msklc;
			wsWithKnownKeyboards.KnownKeyboards.Add(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"));

			var keyboard2FromLdml = knownKeyboards[1];
			Assert.That(keyboard2FromLdml.Layout, Is.EqualTo("SusannasFavoriteKeyboard"));
			Assert.That(keyboard2FromLdml.Locale, Is.EqualTo("en-GB"));
		}
		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(LdmlContentForTests.CurrentVersion("qaa", "", "", "x-lel"));
				}
				var ws = new WritingSystemDefinition();
				var dataMapper = new LdmlDataMapper(new TestWritingSystemFactory());

				dataMapper.Read(tempFile.Path, ws);
				var keyboard1 = new DefaultKeyboardDefinition("MyFavoriteKeyboard", string.Empty);
				ws.KnownKeyboards.Add(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()='external-resources']", 1);
				var secondTripMapper = new LdmlDataMapper(new TestWritingSystemFactory());
				var secondTripWs = new WritingSystemDefinition();
				secondTripMapper.Read(roundTripOut.Path, secondTripWs);
				secondTripWs.KnownKeyboards.Add(new DefaultKeyboardDefinition("x-tel", string.Empty));
				using (var fileStream = new FileStream(roundTripOut.Path, FileMode.Open))
				{
					secondTripMapper.Write(roundTripOut2.Path, secondTripWs, fileStream);
				}
				AssertThatXmlIn.File(roundTripOut2.Path).HasSpecifiedNumberOfMatchesForXpath("/ldml/special/*[local-name()='external-resources']", 1);
			}
		}
		/// <summary>
		/// Creates and returns a keyboard definition object based on the layout and locale.
		/// </summary>
		/// <remarks>The keyboard controller implementing this method will have to check the
		/// availability of the keyboard and what engine provides it.</remarks>
		public virtual IKeyboardDefinition CreateKeyboard(string id, KeyboardFormat format, IEnumerable<string> urls)
		{
			DefaultKeyboardDefinition keyboard;
			if (!_keyboards.TryGetValue(id, out keyboard))
			{
				string[] parts = id.Split('_');
				string layout, locale = null;
				if (parts.Length == 1)
				{
					layout = parts[0];
				}
				else
				{
					locale = parts[0];
					layout = parts[1];
				}
				keyboard = new DefaultKeyboardDefinition(id, layout, layout, locale, true);
				_keyboards[id] = keyboard;
			}

			keyboard.Format = format;
			
			// Clear any exisiting URL list
			keyboard.Urls.Clear();
			foreach (string url in urls)
				keyboard.Urls.Add(url);
			return keyboard;
		}