Пример #1
0
        // Uses deep copy
        public Analogy Copy()
        {
            Analogy analogy;

            analogy           = new Analogy();
            analogy.question  = question;
            analogy.type      = type;
            analogy.tip       = tip;
            analogy.rationale = rationale;
            analogy.right     = right;

            if (answers != null)
            {
                analogy.answers = new string [answers.Length];
                for (int i = 0; i < answers.Length; i++)
                {
                    analogy.answers [i] = answers[i];
                }
            }

            return(analogy);
        }
Пример #2
0
        static public void Read(string file)
        {
            Analogy       analogy;
            string        name;
            List <string> answers;

            try
            {
                StreamReader  stream = new StreamReader(file);
                XmlTextReader reader = new XmlTextReader(stream);
                answers = new List <string> ();

                for (int i = 0; i < (int)Analogy.Type.Last; i++)
                {
                    analogies_arrays[i].Clear();
                }

                analogy = new Analogy();
                while (reader.Read())
                {
                    name = reader.Name.ToLower();
                    switch (name)
                    {
                    case "analogy":
                        if (reader.NodeType == XmlNodeType.Element)
                        {
                            analogy = new Analogy();
                            answers.Clear();
                        }
                        else
                        {
                            if (reader.NodeType == XmlNodeType.EndElement &&
                                // Ignores verbal analogies disabled for a specific locale
                                ServiceLocator.Instance.GetService <ITranslations> ().GetString(analogy.question) != IgnoreAnalogy)
                            {
                                analogy.answers = answers.ToArray();
                                analogies_arrays [(int)analogy.type].Add(analogies_arrays [(int)analogy.type].Count, analogy);
                            }
                        }
                        break;

                    case "_question":
                        if (reader.NodeType != XmlNodeType.Element)
                        {
                            return;
                        }

                        string type;

                        type = reader.GetAttribute("type");

                        if (String.IsNullOrEmpty(type) == false)
                        {
                            switch (type.ToLower())
                            {
                            case "multipleoptions":
                                analogy.type = Analogy.Type.MultipleOptions;
                                break;

                            case "pairofwordsoptions":
                                analogy.type = Analogy.Type.PairOfWordsOptions;
                                break;

                            case "pairofwordscompare":
                                analogy.type = Analogy.Type.PairOfWordsCompare;
                                break;

                            default:
                                analogy.type = Analogy.Type.QuestionAnswer;
                                break;
                            }
                        }

                        analogy.question = reader.ReadElementString();
                        break;

                    case "_tip":
                        if (reader.NodeType == XmlNodeType.Element)
                        {
                            analogy.tip = reader.ReadElementString();
                        }

                        break;

                    case "_rationale":
                        if (reader.NodeType == XmlNodeType.Element)
                        {
                            analogy.rationale = reader.ReadElementString();
                        }

                        break;

                    case "_answer":
                        if (reader.NodeType != XmlNodeType.Element)
                        {
                            break;
                        }

                        string right;

                        right = reader.GetAttribute("correct");

                        if (String.IsNullOrEmpty(right) == false)
                        {
                            if (right.ToLower() == "yes")
                            {
                                analogy.right = answers.Count;
                            }
                        }

                        answers.Add(reader.ReadElementString());
                        break;
                    }
                }

                reader.Close();
                stream.Dispose();
            }

            catch (Exception e)
            {
                Console.WriteLine("AnalogiesFactory. Error loading file: {0}", e.Message);
            }
        }