Esempio n. 1
0
        public static WeSayWordsProject InitializeForTests()
        {
            WeSayWordsProject project = new WeSayWordsProject();

            try
            {
                File.Delete(WeSayWordsProject.PathToPretendLiftFile);
            }
            catch (Exception) { }
            DirectoryInfo projectDirectory   = Directory.CreateDirectory(Path.GetDirectoryName(WeSayWordsProject.PathToPretendLiftFile));
            string        pathToLdmlWsFolder = BasilProject.GetPathToLdmlWritingSystemsFolder(projectDirectory.FullName);

            if (File.Exists(WeSayWordsProject.PathToPretendWritingSystemPrefs))
            {
                File.Delete(WeSayWordsProject.PathToPretendWritingSystemPrefs);
            }

            if (Directory.Exists(pathToLdmlWsFolder))
            {
                Directory.Delete(pathToLdmlWsFolder, true);
            }

            Palaso.Lift.Utilities.CreateEmptyLiftFile(WeSayWordsProject.PathToPretendLiftFile, "InitializeForTests()", true);

            //setup writing systems
            Directory.CreateDirectory(pathToLdmlWsFolder);
            IWritingSystemRepository wsc = LdmlInFolderWritingSystemRepository.Initialize(
                pathToLdmlWsFolder,
                OnMigrationHandler,
                OnWritingSystemLoadProblem,
                WritingSystemCompatibility.Flex7V0Compatible
                );

            IWritingSystemDefinition _ws1 = WritingSystemDefinition.Parse(WritingSystemsIdsForTests.VernacularIdForTest);

            _ws1.DefaultFontName = "Arial";
            _ws1.DefaultFontSize = 30;
            wsc.Set(_ws1);
            IWritingSystemDefinition _ws2 = WritingSystemDefinition.Parse(WritingSystemsIdsForTests.AnalysisIdForTest);

            _ws2.DefaultFontName = new Font(FontFamily.GenericSansSerif, 12).Name;
            _ws2.DefaultFontSize = new Font(FontFamily.GenericSansSerif, 12).Size;
            wsc.Set(_ws2);
            IWritingSystemDefinition _ws3 = WritingSystemDefinition.Parse(WritingSystemsIdsForTests.OtherIdForTest);

            _ws3.DefaultFontName = "Arial";
            _ws3.DefaultFontSize = 15;
            wsc.Set(_ws3);


            wsc.Save();

            project.SetupProjectDirForTests(WeSayWordsProject.PathToPretendLiftFile);
            project.BackupMaker = null;            //don't bother. Modern tests which might want to check backup won't be using this old approach anyways.
            return(project);
        }
Esempio n. 2
0
 /// <summary>
 /// Sets the specified writing system.
 /// </summary>
 /// <param name="ws">The writing system.</param>
 public void Set(CoreWritingSystemDefinition ws)
 {
     lock (m_syncRoot)
     {
         m_repo.Set(ws);
         ws.WritingSystemFactory = this;
         ws.Handle = m_nextHandle++;
         m_handleWSs[ws.Handle] = ws;
     }
 }
 public void SetAllPossibleAndRemoveOthers_NewDuplicateWs_SetsToRepo()
 {
     Assert.That(_writingSystemRepository.Count, Is.EqualTo(0));
     //reinitialize the model with a prepopulated repo
     _writingSystemRepository.Set(new WritingSystemDefinition("en"));
     _model = new WritingSystemSetupModel(_writingSystemRepository);
     //add a new writing system definition with identical Id
     _model.AddPredefinedDefinition(new WritingSystemDefinition("en"));
     _model.SetAllPossibleAndRemoveOthers();
     Assert.That(_writingSystemRepository.Count, Is.EqualTo(2));
     Assert.That(_writingSystemRepository.Contains("en"));
     Assert.That(_writingSystemRepository.Contains("en-x-dupl0"));
 }
Esempio n. 4
0
 public void Roundtripping_Works()
 {
     using (var e = new TestEnvironment())
     {
         IWritingSystemRepository wsCollectionToBeWritten = GetWritingSystemRepository(e.PathToWritingSystemsFolder);
         IWritingSystemDefinition ws = TestEnvironment.CreateDetailedWritingSystem("th");
         wsCollectionToBeWritten.Set(ws);
         IWritingSystemDefinition ws2 = TestEnvironment.CreateDetailedWritingSystem("en");
         wsCollectionToBeWritten.Set(ws2);
         wsCollectionToBeWritten.Save();
         IWritingSystemRepository loadedWsCollection = GetWritingSystemRepository(e.PathToWritingSystemsFolder);
         TestEnvironment.AssertWritingSystemCollectionsAreEqual(wsCollectionToBeWritten, loadedWsCollection);
     }
 }
Esempio n. 5
0
 private static ViewTemplate MakeMasterInventory()
 {
     using (var tempFolder = new TemporaryFolder("ProjectFromViewTemplateTests"))
     {
         IWritingSystemRepository w = LdmlInFolderWritingSystemRepository.Initialize(
             tempFolder.Path,
             OnWritingSystemMigration,
             OnWritingSystemLoadProblem,
             WritingSystemCompatibility.Flex7V0Compatible
             );
         w.Set(WritingSystemDefinition.Parse("aaa"));
         w.Set(WritingSystemDefinition.Parse("aab"));
         return(ViewTemplate.MakeMasterTemplate(w));
     }
 }
Esempio n. 6
0
        public void Load_OnlyLdmlWritingSystemFilesExist_WritingSystemsAreLoadedFromThoseFiles()
        {
            using (var e = new TestEnvironment())
            {
                IWritingSystemRepository wsCollectionToBeWritten =
                    GetWritingSystemRepository(e.PathToWritingSystemsFolder);
                IWritingSystemDefinition ws = TestEnvironment.CreateDetailedWritingSystem("en");
                wsCollectionToBeWritten.Set(ws);
                IWritingSystemDefinition ws2 = TestEnvironment.CreateDetailedWritingSystem("de");
                wsCollectionToBeWritten.Set(ws2);
                wsCollectionToBeWritten.Save();

                IWritingSystemRepository loadedWsCollection = GetWritingSystemRepository(e.PathToWritingSystemsFolder);
                TestEnvironment.AssertWritingSystemCollectionsAreEqual(wsCollectionToBeWritten, loadedWsCollection);
            }
        }
Esempio n. 7
0
        protected virtual void OnChangeNotifySharedStore(T ws)
        {
            DateTime lastDateModified;

            if (_writingSystemsToIgnore.TryGetValue(ws.LanguageTag, out lastDateModified) && ws.DateModified > lastDateModified)
            {
                _writingSystemsToIgnore.Remove(ws.LanguageTag);
            }

            if (_globalRepository != null)
            {
                T globalWs;
                if (_globalRepository.TryGet(ws.LanguageTag, out globalWs))
                {
                    if (ws.DateModified > globalWs.DateModified)
                    {
                        T newWs = WritingSystemFactory.Create(ws, cloneId: true);
                        try
                        {
                            _globalRepository.Replace(ws.LanguageTag, newWs);
                        }
                        catch (Exception)
                        {
                            // Live with it if we can't update the global store. In a CS world we might
                            // well not have permission.
                        }
                    }
                }
                else
                {
                    _globalRepository.Set(WritingSystemFactory.Create(ws, cloneId: true));
                }
            }
        }
Esempio n. 8
0
            public void CreateLdmlWritingSystemDefinitionFile()
            {
                IWritingSystemRepository wsCollectionToBeWritten = GetWritingSystemRepository(PathToWritingSystemsFolder);
                IWritingSystemDefinition ws = CreateDetailedWritingSystem("en");

                wsCollectionToBeWritten.Set(ws);
                wsCollectionToBeWritten.Save();
            }
Esempio n. 9
0
        ///<summary>
        /// Constructor.
        ///</summary>
        ///<param name="idsInFile"></param>
        ///<param name="replaceIdsInFile"></param>
        ///<param name="writingSystemRepository"></param>
        public static void FindOrphans(
            IEnumerable <string> idsInFile,
            IdReplacementStrategy replaceIdsInFile,
            IWritingSystemRepository writingSystemRepository)
        {
            List <string> originalIds = idsInFile.ToList();
            List <string> updatedIds  = originalIds.ToList();

            foreach (string wsId in originalIds)
            {
                // Check if it's in the repo
                if (writingSystemRepository.Contains(wsId))
                {
                    continue;
                }
                string newId;
                if (writingSystemRepository.WritingSystemIdHasChanged(wsId))
                {
                    newId = writingSystemRepository.WritingSystemIdHasChangedTo(wsId);
                }
                else
                {
                    // It's an orphan
                    // Clean it
                    var rfcTagCleaner = new IetfLanguageTagCleaner(wsId);
                    rfcTagCleaner.Clean();
                    newId = rfcTagCleaner.GetCompleteTag();
                }

                WritingSystemDefinition conformantWritingSystem;
                writingSystemRepository.WritingSystemFactory.Create(newId, out conformantWritingSystem);
                // If it changed, then change
                if (conformantWritingSystem.LanguageTag != wsId)
                {
                    conformantWritingSystem.LanguageTag = IetfLanguageTag.ToUniqueLanguageTag(
                        conformantWritingSystem.LanguageTag, updatedIds);
                    replaceIdsInFile(wsId, conformantWritingSystem.LanguageTag);
                    updatedIds.Remove(wsId);
                    updatedIds.Add(conformantWritingSystem.LanguageTag);
                }

                // Check if it's in the repo
                if (writingSystemRepository.Contains(conformantWritingSystem.LanguageTag))
                {
                    continue;
                }

                // It's not in the repo so set it
                writingSystemRepository.Set(conformantWritingSystem);
            }
            writingSystemRepository.Save();
        }
Esempio n. 10
0
        public void Write_LoadedWritingSystemIsDeleted_DeletionIsRoundTripped()
        {
            using (var e = new TestEnvironment())
            {
                //Write out two writing systems
                IWritingSystemRepository wsCollectionToBeWritten =
                    GetWritingSystemRepository(e.PathToWritingSystemsFolder);
                IWritingSystemDefinition ws = TestEnvironment.CreateDetailedWritingSystem("en");
                wsCollectionToBeWritten.Set(ws);
                IWritingSystemDefinition ws2 = TestEnvironment.CreateDetailedWritingSystem("th");
                wsCollectionToBeWritten.Set(ws2);
                wsCollectionToBeWritten.Save();

                //load them up again
                IWritingSystemRepository loadedWsCollection = GetWritingSystemRepository(e.PathToWritingSystemsFolder);
                loadedWsCollection.Remove(ws.Id);                 //remove one
                loadedWsCollection.Save();

                //Now check that it hasn't come back!
                IWritingSystemRepository loadedWsCollection2 =
                    GetWritingSystemRepository(e.PathToWritingSystemsFolder);
                Assert.IsFalse(loadedWsCollection2.Contains(ws.Id));
            }
        }
        public void NewerThanNewer_ReturnsNoneNewer()
        {
            var ws1 = new WritingSystemDefinition();

            ws1.Language     = "en";
            ws1.DateModified = new DateTime(2008, 1, 15);
            RepositoryUnderTest.Set(ws1);

            IWritingSystemRepository repository = CreateNewStore();
            var ws2 = RepositoryUnderTest.MakeDuplicate(ws1);

            ws2.DateModified = new DateTime(2008, 1, 16);
            repository.Set(ws2);

            int count = 0;

            foreach (WritingSystemDefinition ws in repository.WritingSystemsNewerIn(RepositoryUnderTest.AllWritingSystems))
            {
                count++;
            }
            Assert.AreEqual(0, count);
        }
Esempio n. 12
0
        ///<summary>
        /// Constructor.
        ///</summary>
        ///<param name="idsInFile"></param>
        ///<param name="replaceIdsInFile"></param>
        ///<param name="writingSystemRepository"></param>
        public static void FindOrphans(
            IEnumerable <string> idsInFile,
            IdReplacementStrategy replaceIdsInFile,
            IWritingSystemRepository writingSystemRepository
            )
        {
            var originalIds = new List <string>(idsInFile);
            var updatedIds  = new List <string>(idsInFile);

            foreach (var wsId in originalIds)
            {
                // Check if it's in the repo
                if (writingSystemRepository.Contains(wsId))
                {
                    continue;
                }
                string newId = wsId;
                if (writingSystemRepository.WritingSystemIdHasChanged(wsId))
                {
                    newId = writingSystemRepository.WritingSystemIdHasChangedTo(wsId);
                }
                else
                {
                    // It's an orphan
                    // Check for the writing system repository compatibility mode
                    if (writingSystemRepository.CompatibilityMode == WritingSystemCompatibility.Flex7V0Compatible)
                    {
                        if (!wsId.StartsWith("x-"))
                        {
                            // Clean it
                            var rfcTagCleaner = new Rfc5646TagCleaner(wsId);
                            rfcTagCleaner.Clean();
                            newId = rfcTagCleaner.GetCompleteTag();
                        }
                    }
                    else
                    {
                        // Clean it
                        var rfcTagCleaner = new Rfc5646TagCleaner(wsId);
                        rfcTagCleaner.Clean();
                        newId = rfcTagCleaner.GetCompleteTag();
                    }
                }
                var conformantWritingSystem = WritingSystemDefinition.Parse(newId);
                // If it changed, then change
                if (conformantWritingSystem.Bcp47Tag != wsId)
                {
                    conformantWritingSystem = WritingSystemDefinition.CreateCopyWithUniqueId(conformantWritingSystem, updatedIds);
                    replaceIdsInFile(wsId, conformantWritingSystem.Bcp47Tag);
                    updatedIds.Remove(wsId);
                    updatedIds.Add(conformantWritingSystem.Bcp47Tag);
                }
                // Check if it's in the repo
                if (writingSystemRepository.Contains(conformantWritingSystem.Bcp47Tag))
                {
                    continue;
                }
                // It's not in the repo so set it
                writingSystemRepository.Set(conformantWritingSystem);
            }
            writingSystemRepository.Save();
        }
Esempio n. 13
0
        public void SetTwoDefinitions_CountEquals2()
        {
            _writingSystem.Language = "one";
            RepositoryUnderTest.Set(_writingSystem);
            var ws2 = new WritingSystemDefinition();

            ws2.Language = "two";
            RepositoryUnderTest.Set(ws2);

            Assert.AreEqual(2, RepositoryUnderTest.Count);
        }
        ///<summary>
        /// Constructor.
        ///</summary>
        ///<param name="idsInFile"></param>
        ///<param name="replaceIdsInFile"></param>
        ///<param name="writingSystemRepository"></param>
        public static void FindOrphans(
			IEnumerable<string> idsInFile,
			IdReplacementStrategy replaceIdsInFile,
			IWritingSystemRepository writingSystemRepository)
        {
            List<string> originalIds = idsInFile.ToList();
            List<string> updatedIds = originalIds.ToList();
            foreach (string wsId in originalIds)
            {
                // Check if it's in the repo
                if (writingSystemRepository.Contains(wsId))
                {
                    continue;
                }
                string newId;
                if (writingSystemRepository.WritingSystemIdHasChanged(wsId))
                {
                    newId = writingSystemRepository.WritingSystemIdHasChangedTo(wsId);
                }
                else
                {
                    // It's an orphan
                    // Clean it
                    var rfcTagCleaner = new IetfLanguageTagCleaner(wsId);
                    rfcTagCleaner.Clean();
                    newId = rfcTagCleaner.GetCompleteTag();
                }

                WritingSystemDefinition conformantWritingSystem;
                writingSystemRepository.WritingSystemFactory.Create(newId, out conformantWritingSystem);
                // If it changed, then change
                if (conformantWritingSystem.LanguageTag != wsId)
                {
                    conformantWritingSystem.LanguageTag = IetfLanguageTag.ToUniqueLanguageTag(
                        conformantWritingSystem.LanguageTag, updatedIds);
                    replaceIdsInFile(wsId, conformantWritingSystem.LanguageTag);
                    updatedIds.Remove(wsId);
                    updatedIds.Add(conformantWritingSystem.LanguageTag);
                }

                // Check if it's in the repo
                if (writingSystemRepository.Contains(conformantWritingSystem.LanguageTag))
                    continue;

                // It's not in the repo so set it
                writingSystemRepository.Set(conformantWritingSystem);
            }
            writingSystemRepository.Save();
        }
		///<summary>
		/// Constructor.
		///</summary>
		///<param name="idsInFile"></param>
		///<param name="replaceIdsInFile"></param>
		///<param name="writingSystemRepository"></param>
		public static void FindOrphans(
			IEnumerable<string> idsInFile,
			IdReplacementStrategy replaceIdsInFile,
			IWritingSystemRepository writingSystemRepository
		) {
			var originalIds = new List<string>(idsInFile);
			var updatedIds = new List<string>(idsInFile);
			foreach (var wsId in originalIds)
			{
				// Check if it's in the repo
				if (writingSystemRepository.Contains(wsId))
				{
					continue;
				}
				string newId = wsId;
				if (writingSystemRepository.WritingSystemIdHasChanged(wsId))
				{
					newId = writingSystemRepository.WritingSystemIdHasChangedTo(wsId);
				}
				else
				{
					// It's an orphan
					// Check for the writing system repository compatibility mode
					if (writingSystemRepository.CompatibilityMode == WritingSystemCompatibility.Flex7V0Compatible)
					{
						if (!wsId.StartsWith("x-"))
						{
							// Clean it
							var rfcTagCleaner = new Rfc5646TagCleaner(wsId);
							rfcTagCleaner.Clean();
							newId = rfcTagCleaner.GetCompleteTag();
						}
					}
					else
					{
						// Clean it
						var rfcTagCleaner = new Rfc5646TagCleaner(wsId);
						rfcTagCleaner.Clean();
						newId = rfcTagCleaner.GetCompleteTag();
					}
				}
				var conformantWritingSystem = WritingSystemDefinition.Parse(newId);
				// If it changed, then change
				if (conformantWritingSystem.Bcp47Tag != wsId)
				{
					conformantWritingSystem = WritingSystemDefinition.CreateCopyWithUniqueId(conformantWritingSystem, updatedIds);
					replaceIdsInFile(wsId, conformantWritingSystem.Bcp47Tag);
					updatedIds.Remove(wsId);
					updatedIds.Add(conformantWritingSystem.Bcp47Tag);
				}
				// Check if it's in the repo
				if (writingSystemRepository.Contains(conformantWritingSystem.Bcp47Tag))
				{
					continue;
				}
				// It's not in the repo so set it
				writingSystemRepository.Set(conformantWritingSystem);
			}
			writingSystemRepository.Save();
		}
Esempio n. 16
0
        internal void SetWritingSystemsForFields()
        {
            var liftDom = new XmlDocument();

            liftDom.Load(_path);             //will throw if the file is ill-formed
            var missingWritingSystems = new StringBuilder();

            foreach (XmlNode node in liftDom.SelectNodes("//@lang"))
            {
                if (node.Value == "x-spec" && !_writingSystems.Contains("x-spec"))
                {
                    _writingSystems.Set(WritingSystemDefinition.Parse("x-spec"));
                }
                if (!_writingSystems.Contains(node.Value))
                {
                    _writingSystems.Set(WritingSystemDefinition.Parse(node.Value));
                    missingWritingSystems.AppendFormat("{0},", node.Value);
                }
            }
            _writingSystems.Save();

            if (missingWritingSystems.Length > 0)
            {
                var list = missingWritingSystems.ToString().Trim(new[] { ',' });
                ErrorReport.NotifyUserOfProblem(
                    "WeSay had a problem locating information on at least one input system used in the LIFT export from FLEx.  One known cause of this is an old version of FLEx. In the folder containing the LIFT file, there should have been '___.ldml' files for the following input systems: {0}.\r\nBecause these input system definitions were not found, WeSay will create blank input systems for each of these, which you will need to set up with the right fonts, keyboards, etc.", list);
            }
            // replace all "v" fields with the first lexical-unit writing system
            //and all "en" with the first translation one...

            var vernacular = GetTopWritingSystem(liftDom, "//lexical-unit/form/@lang");

            if (vernacular != String.Empty)
            {
                _viewTemplate.OnWritingSystemIDChange(WeSayWordsProject.VernacularWritingSystemIdForProjectCreation, vernacular);
                if (_writingSystems.Contains(WeSayWordsProject.VernacularWritingSystemIdForProjectCreation))
                {
                    _writingSystems.Remove(WeSayWordsProject.VernacularWritingSystemIdForProjectCreation);
                }
            }
            var analysis = GetTopWritingSystem(liftDom, "//sense/gloss/@lang");

            if (analysis == String.Empty)
            {
                analysis = GetTopWritingSystem(liftDom, "//sense/definition/@lang");
                //nb: we don't want to remove english, even if they don't use it
            }
            if (analysis != String.Empty)
            {
                _viewTemplate.OnWritingSystemIDChange(WeSayWordsProject.AnalysisWritingSystemIdForProjectCreation, analysis);
            }

            AddWritingSystemsForField(liftDom, "//lexical-unit/form/@lang", LexEntry.WellKnownProperties.LexicalUnit);
            AddWritingSystemsForField(liftDom, "//sense/gloss/@lang", LexSense.WellKnownProperties.Gloss);

            AddWritingSystemsForField(liftDom, "//sense/definition/form/@lang", LexSense.WellKnownProperties.Definition);

            AddAllGlossWritingSystemsToDefinition();

            AddWritingSystemsForField(liftDom, "//example/form/@lang", LexExampleSentence.WellKnownProperties.ExampleSentence);
            AddWritingSystemsForField(liftDom, "//translation/form/@lang", LexExampleSentence.WellKnownProperties.Translation);

            //------------ hack
            var gloss = _viewTemplate.GetField(LexSense.WellKnownProperties.Gloss);
            var def   = _viewTemplate.GetField(LexSense.WellKnownProperties.Definition);

            foreach (var id in def.WritingSystemIds)
            {
                if (!gloss.WritingSystemIds.Contains(id))
                {
                    gloss.WritingSystemIds.Add(id);
                }
            }
            foreach (var id in gloss.WritingSystemIds)
            {
                if (!def.WritingSystemIds.Contains(id))
                {
                    def.WritingSystemIds.Add(id);
                }
            }
        }