static public void WriteToFile(string path, SemanticDomainQuestionsCollection questionsCollection)
        {
            XmlWriterSettings settings = new XmlWriterSettings();

            settings.Indent           = true;
            settings.Encoding         = Encoding.UTF8;
            settings.ConformanceLevel = ConformanceLevel.Document;
            XmlWriter fileWriter = XmlWriter.Create(path, settings);

            fileWriter.WriteStartDocument();
            fileWriter.WriteStartElement("semantic-domain-questions");

            string type = "DDP1";

            if (questionsCollection.Version == SemanticDomainQuestionsCollection.SemanticDomainsVersion.DDP4)
            {
                type = "DDP4";
            }
            fileWriter.WriteAttributeString("semantic-domain-type", type);
            fileWriter.WriteAttributeString("lang", questionsCollection.WritingSystemId);

            foreach (SemanticDomainQuestions questions in questionsCollection.DomainKeyToQuestionsMap.Values)
            {
                WriteSemanticDomainQuestions(fileWriter, questions);
            }
            fileWriter.WriteEndElement();
            fileWriter.Close();
        }
Ejemplo n.º 2
0
        static private SemanticDomainQuestionsCollection PopulateSemanticDomainsQuestionsFromXml(XmlReader fileReader)
        {
            if (!(fileReader.Name == "semantic-domain-questions"))
            {
                throw new InvalidOperationException(String.Format("Node is {0} but we expected 'semantic-domain-questions' node.", fileReader.Name));
            }

            SemanticDomainQuestionsCollection allQuestions = new SemanticDomainQuestionsCollection();

            string type = fileReader.GetAttribute("semantic-domain-type");

            if (type == "DDP4")
            {
                allQuestions.Version = SemanticDomainQuestionsCollection.SemanticDomainsVersion.DDP4;
            }
            else if (type == "DDP1")
            {
                allQuestions.Version = SemanticDomainQuestionsCollection.SemanticDomainsVersion.DDP1;
            }
            else
            {
                throw new ArgumentOutOfRangeException(String.Format("There is no type for {0}", type));
            }

            allQuestions.WritingSystemId = fileReader.GetAttribute("lang");

            if (fileReader.IsEmptyElement)
            {
                return(allQuestions);
            }

            while (fileReader.Read())
            {
                bool readerHasReachedEndOfSemanticDomainsQuestions =
                    fileReader.NodeType == XmlNodeType.EndElement && fileReader.Name == "semantic-domain-questions";

                if (readerHasReachedEndOfSemanticDomainsQuestions)
                {
                    break;
                }

                if (fileReader.Name == "semantic-domain")
                {
                    SemanticDomainQuestions questions = PopulateSemanticDomainQuestionsFromXml(fileReader);
                    allQuestions.DomainKeyToQuestionsMap.Add(questions.SemanticDomainKey, questions);
                }
                else
                {
                    throw new InvalidOperationException(
                              String.Format("Malformed 'semantic-domain-questions' node found. Found an unexpected '{0}' element.", fileReader.Name));
                }
            }
            fileReader.Close();
            return(allQuestions);
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            string filePathToDdp1 = @"D:\SoftwareDevelopment\Ddp124Map\DDP4\Ddp1.xml";
            string filePathToDdp4 = @"D:\SoftwareDevelopment\Ddp124Map\DDP4\Ddp4.xml";
            string filePathToThaiDdp1Questions    = @"D:\SoftwareDevelopment\Ddp124Map\DDP4\Ddp4Questions-th.xml";
            string filePathToEnglishDdp4Questions = @"D:\SoftwareDevelopment\Ddp124Map\DDP4\Ddp4Questions-en.xml";
            string filePathToMap = @"D:\SoftwareDevelopment\Ddp124Map\DDP4\Semantic Domains v1 to v4 map.txt";

            string newDdpFile       = @"D:\SoftwareDevelopment\Ddp124Map\DDP4\newDdp.xml";
            string newQuestionsFile = @"D:\SoftwareDevelopment\Ddp124Map\DDP4\newQuestion.xml";

            SemanticDomainCollection semanticDomainsDdp1 = SemanticDomainReader.ReadFromFile(filePathToDdp1);
            SemanticDomainCollection semanticDomainsDdp4 = SemanticDomainReader.ReadFromFile(filePathToDdp4);

            Dictionary <string, string> map = Ddp4to1MapReader.ReadFromFile(filePathToMap);

            foreach (SemanticDomainInfo ddp1SemanticDomain in semanticDomainsDdp1.AllSemanticDomains)
            {
                string correspondingNumberInDdp4 = ddp1SemanticDomain.Number;
                if (map.ContainsKey(ddp1SemanticDomain.Number))
                {
                    correspondingNumberInDdp4 = map[ddp1SemanticDomain.Number];
                }
                SemanticDomainInfo semanticDomainToMergeInto = semanticDomainsDdp4.GetSemanticDomainWithNumber(correspondingNumberInDdp4);
                semanticDomainToMergeInto.MergeInTranslations(ddp1SemanticDomain);
            }

            SemanticDomainWriter.WriteToFile(newDdpFile, semanticDomainsDdp4);

            SemanticDomainQuestionsCollection thaiDdp1Questions =
                SemanticDomainQuestionsReader.ReadFromFile(filePathToThaiDdp1Questions);

            SemanticDomainQuestionsCollection thaiDdp4Questions = new SemanticDomainQuestionsCollection(thaiDdp1Questions.Version, thaiDdp1Questions.WritingSystemId);

            foreach (SemanticDomainQuestions domainQuestions in thaiDdp1Questions.DomainKeyToQuestionsMap.Values)
            {
                string semanticDomainKey = GetMappedSemanticDomainKey(map, semanticDomainsDdp4, domainQuestions);

                bool questionsForDomainAlreadyExist =
                    thaiDdp4Questions.DomainKeyToQuestionsMap.ContainsKey(semanticDomainKey);

                if (!questionsForDomainAlreadyExist)
                {
                    SemanticDomainQuestions newQuestions = new SemanticDomainQuestions(Guid.NewGuid(), semanticDomainKey);
                    thaiDdp4Questions.DomainKeyToQuestionsMap.Add(semanticDomainKey, newQuestions);
                }
                thaiDdp4Questions.DomainKeyToQuestionsMap[semanticDomainKey].Questions.AddRange(domainQuestions.Questions);
            }

            SemanticDomainQuestionsWriter.WriteToFile(newQuestionsFile, thaiDdp4Questions);

            Console.ReadLine();
        }
Ejemplo n.º 4
0
        static public SemanticDomainQuestionsCollection ReadFromFile(string filePath)
        {
            SemanticDomainQuestionsCollection allQuestions = new SemanticDomainQuestionsCollection();
            XmlReaderSettings settings = new XmlReaderSettings();

            settings.IgnoreWhitespace = true;
            XmlReader fileReader = XmlReader.Create(filePath, settings);

            while (fileReader.Read())
            {
                if (fileReader.Name == "semantic-domain-questions")
                {
                    allQuestions = PopulateSemanticDomainsQuestionsFromXml(fileReader);
                }
            }
            fileReader.Close();
            return(allQuestions);
        }