1 // ================================================================================================
3 // 言語間の対訳表をあらわすモデルクラスソース</summary>
5 // <copyright file="TranslationTable.cs" company="honeplusのメモ帳">
6 // Copyright (C) 2012 Honeplus. All rights reserved.</copyright>
9 // ================================================================================================
11 namespace Honememo.Wptscs.Models
14 using System.Collections.Generic;
17 using System.Xml.Serialization;
18 using Honememo.Utilities;
21 /// 言語間の対訳表をあらわすモデルクラスです。
23 public class TranslationTable : List<IDictionary<string, string[]>>, IXmlSerializable
30 /// <remarks><see cref="GetWord(string)"/>の呼び出しを簡略化するためのプロパティ。</remarks>
40 /// <remarks><see cref="GetWord(string)"/>の呼び出しを簡略化するためのプロパティ。</remarks>
54 /// <param name="from">翻訳元言語コード。</param>
55 /// <param name="to">翻訳先言語コード。</param>
56 /// <param name="word">翻訳元語句。</param>
57 /// <returns>対訳語句。登録されていない場合 <c>null</c>。</returns>
58 /// <exception cref="ArgumentNullException"><paramref name="from"/>, <paramref name="to"/>, <paramref name="word"/>のいずれかが<c>null</c>の場合。</exception>
59 /// <remarks><paramref name="word"/>の大文字小文字は区別しない。</remarks>
60 public string GetWord(string from, string to, string word)
62 // nullは不可。以降でエラーになるためここでチェック
63 Validate.NotNull(from, "from");
64 Validate.NotNull(to, "to");
65 string w = Validate.NotNull(word, "word").ToLower();
68 foreach (IDictionary<string, string[]> record in this)
70 if (record.ContainsKey(from) && CollectionUtils.ContainsIgnoreCase(record[from], w))
72 // 翻訳元を発見した場合、それに対応する翻訳先の語句を返す
74 if (record.ContainsKey(to))
77 s = record[to].First();
88 /// 指定されている言語の組み合わせで対訳語を取得する。
90 /// <param name="word">翻訳元語。</param>
91 /// <returns>対訳語。登録されていない場合 <c>null</c>。</returns>
92 /// <exception cref="InvalidOperationException"><see cref="From"/>, <see cref="To"/>のいずれかが空の場合。</exception>
93 /// <remarks>大文字小文字は区別しない。</remarks>
94 public string GetWord(string word)
96 if (string.IsNullOrEmpty(this.From) || string.IsNullOrEmpty(this.To))
98 throw new InvalidOperationException("empty from or to");
101 return this.GetWord(this.From, this.To, word);
106 #region XMLシリアライズ用メソッド
109 /// シリアライズするXMLのスキーマ定義を返す。
111 /// <returns>XML表現を記述する<see cref="System.Xml.Schema.XmlSchema"/>。</returns>
112 public System.Xml.Schema.XmlSchema GetSchema()
118 /// XMLからオブジェクトをデシリアライズする。
120 /// <param name="reader">デシリアライズ元の<see cref="XmlReader"/></param>
121 public void ReadXml(XmlReader reader)
123 XmlDocument xml = new XmlDocument();
126 // ※ 以下、基本的に無かったらNGの部分はいちいちチェックしない。例外飛ばす
127 XmlElement tableElement = xml.DocumentElement;
131 foreach (XmlNode recordNode in tableElement.SelectNodes("Group"))
133 IDictionary<string, string[]> record = new Dictionary<string, string[]>();
134 foreach (XmlNode wordNode in recordNode)
136 // 一つの言語に複数の値が登録可能なため、その場合配列に積む
137 XmlElement wordElement = wordNode as XmlElement;
138 string lang = wordElement.GetAttribute("Lang");
139 string word = wordElement.InnerText;
140 List<string> list = new List<string>();
142 if (record.TryGetValue(lang, out words))
144 list.AddRange(words);
147 // 既に登録されている場合、代表であれば先頭に、それ以外なら後ろに追加
149 if (bool.TryParse(wordElement.GetAttribute("Head"), out head) && head)
151 list.Insert(0, word);
158 record[lang] = list.ToArray();
166 /// オブジェクトをXMLにシリアライズする。
168 /// <param name="writer">シリアライズ先の<see cref="XmlWriter"/></param>
169 public void WriteXml(XmlWriter writer)
172 foreach (IDictionary<string, string[]> record in this)
174 writer.WriteStartElement("Group");
175 foreach (KeyValuePair<string, string[]> words in record)
178 foreach (string word in words.Value)
180 writer.WriteStartElement("Word");
181 writer.WriteAttributeString("Lang", words.Key);
182 if (first && words.Value.Length > 1)
184 // 先頭項目は変換先として使用されるため、複数ある場合は代表フラグを出力
185 // ※ 2番目以降は同格のため、先頭以外の並び順は保障しない
186 writer.WriteAttributeString("Head", bool.TrueString);
190 writer.WriteValue(word);
191 writer.WriteEndElement();
195 writer.WriteEndElement();