public static EmpElement Create(MondaiDocument mondaiDocument) { var result = new EmpElement(""); var documentElement = new EmpElement("document"); documentElement.Items.Add(new EmpElement("id", mondaiDocument.Id)); documentElement.Items.Add(new EmpElement("title", mondaiDocument.Name)); result.Items.Add(documentElement); documentElement.Items.AddRange ( mondaiDocument.Items.Select(daimon => { var daimonElement = new EmpElement("daimon"); daimonElement.Items.Add(new EmpElement("id", daimon.Id)); daimonElement.Items.Add(new EmpElement("title", daimon.Name)); daimonElement.Items.AddRange ( daimon.Items.Select(chumon => { var chumonElement = new EmpElement("chumon"); chumonElement.Items.Add(new EmpElement("id", chumon.Id)); chumonElement.Items.Add(new EmpElement("title", chumon.Name)); if (chumon.IsShuffled) { chumonElement.Items.Add(new EmpElement("shuffle", "true")); } if (chumon.IsOptional) { chumonElement.Items.Add(new EmpElement("optional", "true")); } chumonElement.Items.AddRange(chumon.HelpIds.Select(helpId => new EmpElement("helpId", helpId))); chumonElement.Items.AddRange ( chumon.Items.Select(mondai => { var shomon = mondai as Shomon; if (null != shomon) { return Create(shomon); } var ankimon = mondai as Ankimon; if (null != ankimon) { return Create(ankimon); } throw new ApplicationException(); }) ); return chumonElement; }) ); return daimonElement; }) ); return result; }
// Static members private static EmpElement Parse(string name, StringReader stringReader) { var element = new EmpElement(name); var buffer = new StringBuilder(); int c = -1; while (-1 != (c = stringReader.Read())) { switch (c) { case '{' : { element.Items.Add(Parse(buffer.ToString().Trim(), stringReader)); buffer = new StringBuilder(); break; } case '}' : { element.Value = buffer.ToString().Trim(); return element; } case '=' : { var line = stringReader.ReadLine(); // line = 先頭と末尾のダブルクォーテーションを取り除く(line); element.Items.Add(new EmpElement(buffer.ToString(), line)); buffer = new StringBuilder(); break; } default : { buffer.Append((char)c); break; } } } // throw new ApplicationException(); return element; }
public static Shomon CreateShomon(EmpElement shomonElement) { var shomon = new Shomon() { Id = shomonElement.GetValue("id"), Mondaibun = shomonElement.GetValue("mon"), }; var seikaiTexts = shomonElement.GetValues("sei").Select(x => 余分な空白を取り除く(x)); if (seikaiTexts.Any(x => string.IsNullOrWhiteSpace(x))) throw new EmpException("seiが指定されていません。: id :" + shomon.Id); var display = shomonElement.GetValue("display"); if (!string.IsNullOrWhiteSpace(display)) { shomon.LettersThatAreDisplayedOnCard = (LettersThatAreDisplayedOnCard)Enum.Parse(typeof(LettersThatAreDisplayedOnCard), display, true); } // 正解項目の設定 foreach (var seikaiText in seikaiTexts.Select(text => MondaiWord.分割指示記号を取り除く(text))) { shomon.CorrectAnswers.Add(new CorrectAnswer(seikaiText)); } var wordTexts = shomonElement.GetValues("word").Select(x => 余分な空白を取り除く(x)); shomon.MondaiWords = wordTexts.Select(s => new MondaiWord(s)).ToArray(); // word項目で正解が組み立てられない場合は、正解項目を分割処理してword項目を作成する。 var result = shomon.正解探索(); if (result.Any(array => null == array)) { var otoriTexts = shomonElement.GetValues("otori").Concat(shomonElement.GetValues("otr")).Select(x => 余分な空白を取り除く(x)); var texts = seikaiTexts.Concat(otoriTexts); shomon.MondaiWords = MondaiWord.Split2(texts).Concat(shomon.MondaiWords).ToArray(); } /* #if DEBUG if (Environment.StackTrace.Contains("括弧とクォーテーションに対応させる")) { foreach (var mondaiWord in shomon.MondaiWords) { Console.WriteLine(mondaiWord); } } #endif // DEBUG */ if (shomon.正解探索().Any(array => null == array)) { throw new ApplicationException(string.Format("正解の組み合わせが見つかりません。: id: {0}", shomon.Id)); } return shomon; }
public static Ankimon CreateAnkimon(EmpElement ankimonElement) { if ("ankimon" != ankimonElement.Name) throw new ArgumentException(); var ankimonItems = ankimonElement.GetElements("item").Select(itemElement => { var ankimonItem = new AnkimonItem() { Name = itemElement.GetElement("name").Value, }; var words = itemElement.GetElements(new [] { "word", "split", }).SelectMany ( wordElement => { switch (wordElement.Name) { case "word" : { return new [] { new MondaiWord(wordElement.Value) }; } case "split" : { return MondaiWord.Split2(wordElement.Value); } default : { // ここに到達することはないはず throw new ApplicationException(); } } } ); ankimonItem.MondaiWords = words.ToArray(); return ankimonItem; }); return new Ankimon ( ankimonElement.GetElement("id").Value, ankimonItems.ToArray() ); }
public static EmpElement Create(Shomon shomon) { if (null == shomon) throw new ArgumentNullException(); var shomonElement = new EmpElement("shomon"); shomonElement.Items.Add(new EmpElement("id", shomon.Id)); shomonElement.Items.Add(new EmpElement("mon", shomon.Mondaibun)); var list = new List<MondaiWord>(shomon.MondaiWords); shomonElement.Items.AddRange(shomon.CorrectAnswers.Select(seikai => { var mondaiWords = Shomon.正解探索(seikai, shomon.MondaiWords); foreach (var w in mondaiWords) { var index = list.IndexOf(w); if (-1 == index) { continue; } list.RemoveAt(index); } var result = MondaiWord.相互変換可能なテキスト形式を取得する(mondaiWords); return new EmpElement("sei", result); })); shomonElement.Items.AddRange(list.Select(mondaiWord => new EmpElement("word", mondaiWord.Text.Replace(" ", "+")))); return shomonElement; }
public static EmpElement Create(Ankimon ankimon) { var ankimonElement = new EmpElement("ankimon"); ankimonElement.Items.Add(new EmpElement("id", ankimon.Id)); ankimonElement.Items.AddRange(ankimon.AnkimonItems.Select(ankimonItem => { var ankimonItemElement = new EmpElement("item"); ankimonItemElement.Items.Add(new EmpElement("name", ankimonItem.Name)); ankimonItemElement.Items.Add(new EmpElement("split", MondaiWord.相互変換可能なテキスト形式を取得する(ankimonItem.MondaiWords))); // ankimonItemElement.Items.AddRange(ankimonItem.MondaiWords.Select(mondaiWord => new EmpElement("word", mondaiWord.Text))); return ankimonItemElement; })); return ankimonElement; }