Siki.Configure #0
今書いてるソースを上げてみる。
連想配列(のようなもの)<->XML を簡単に行うためのクラスです。詳細はいずれ。
作りかけなので、全く整理してないです。
GlobalConfigure.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Windows.Forms; using System.Xml; namespace Siki.Configure { /// <summary> /// アプリケーションの設定を管理するクラス /// </summary> public class GlobalConfigure { #region フィールド /// <summary>ファイルのパス</summary> private string m_fullpath = ""; /// <summary>現在のアプリケーション設定を表すDOM</summary> private XmlDocument m_dom = null; #endregion フィールド #region イベント /// <summary>DOMが初期化されたときに発生するイベント</summary> public event EventHandler<ConfigureEventArgs> Initialized; /// <summary>ファイルから読み込まれたときに発生するイベント</summary> public event EventHandler<ConfigureEventArgs> Loaded; /// <summary>ファイルに保存されたときに発生するイベント</summary> public event EventHandler<ConfigureEventArgs> Saved; #endregion イベント #region プロパティ /// <summary> /// 設定ファイルのフルパスを取得する. /// </summary> public string FullPath { get { return m_fullpath; } protected set { m_fullpath = value; } } /// <summary> /// XmlDocumentを取得する /// </summary> protected XmlDocument Dom { get { return m_dom; } } public ConfigureNode this[string name] { get { foreach (XmlNode node in Dom.DocumentElement.ChildNodes) { if (node.Name == name && node is XmlElement) { // 見つかった場合 return new ConfigureNode(node as XmlElement); } } // 見つからなかった場合 // 要素を作成 XmlElement element = Dom.CreateElement(name); // 要素を追加 Dom.DocumentElement.AppendChild(element); // return new ConfigureNode(element); } } #endregion プロパティ #region コンストラクタ /// <summary> /// デフォルトコンストラクタ. /// 設定ファイルをデフォルトのパスに指定する. /// </summary> public GlobalConfigure() : this("conf.xml") { } #endregion コンストラクタ /// <summary> /// コンストラクタ. /// 設定ファイルをデフォルトのフォルダの @a filename に指定する. /// </summary> /// <param name="filename">設定ファイル名</param> public GlobalConfigure(string filename) : this(Application.UserAppDataPath, filename) { } /// <summary> /// コンストラクタ. /// 設定ファイルを @a dir フォルダの @a filename に指定する. /// </summary> /// <param name="dir">設定ファイルのフォルダ</param> /// <param name="filename">設定ファイル名</param> public GlobalConfigure(string dir, string filename) { FullPath = (dir.EndsWith("\\") ? dir : dir + "\\") + filename; m_dom = new XmlDocument(); Load(); } /// <summary> /// 設定ファイルを読み込む. /// ファイルが存在しない場合は新規に作成する. /// </summary> /// <returns>XMLの読み込みor作成に成功したか.</returns> public virtual bool Load() { try { if (File.Exists(FullPath)) { // 設定ファイルが存在するとき // ファイルを読み込む Dom.Load(FullPath); } else { // 設定ファイルが存在しないとき Dom.AppendChild(Dom.CreateXmlDeclaration("1.0", "UTF-8", "yes")); Dom.AppendChild(Dom.CreateElement("alert")); Save(); } } catch (XmlException) { // XMLに関する例外 throw; } catch (Exception) { // その他の例外 return false; } return true; } /// <summary> /// Initializedイベントを発行する. /// </summary> /// <param name="e"></param> public virtual void OnInitialized(ConfigureEventArgs e) { if (Initialized != null) { Initialized(this, e); } } /// <summary> /// Loadedイベントを発行する. /// </summary> /// <param name="e"></param> public virtual void OnLoaded(ConfigureEventArgs e) { if (Loaded != null) { Loaded(this, e); } } /// <summary> /// 現在のDOMの状態を設定ファイルに書き込む. /// </summary> /// <returns></returns> public virtual bool Save() { try { Dom.Save(FullPath); OnSaved(new ConfigureEventArgs(FullPath)); } catch (XmlException) { // XMLに関する例外 throw; } catch { // その他の例外 return false; } return true; } /// <summary> /// Savedイベントを発行する. /// </summary> /// <param name="e"></param> public virtual void OnSaved(ConfigureEventArgs e) { if(Saved != null){ Saved(this, e); } } /// <summary> /// @a groupname で指定された名前のグループを取得する. /// </summary> /// <param name="groupname"></param> /// <returns></returns> public ConfigureNode GetGroup(string groupname){ foreach (XmlNode node in Dom.DocumentElement.ChildNodes) { if (node.Name == groupname && node is XmlElement) { return new ConfigureNode(node as XmlElement); } } XmlElement element = Dom.CreateElement(groupname); Dom.DocumentElement.AppendChild(element); return new ConfigureNode(element); } } } |
ConfigureNode.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; namespace Siki.Configure { /// <summary> /// 設定ノード. /// </summary> public class ConfigureNode { /// <summary> /// 対応するXmlElementを取得する. /// </summary> public XmlElement Element { get; private set; } /// <summary> /// 最初のXmlTextを取得する. /// </summary> private XmlText Text { get { foreach (XmlNode node in Element.ChildNodes) { if (node is XmlText) { return node as XmlText; } } return null; } } /// <summary> /// 子ノードを名前で取得するインデクサ. /// </summary> /// <param name="name"></param> /// <returns></returns> public ConfigureNode this[string name] { get { foreach (XmlNode node in Element.ChildNodes) { if(node.Name == name && node is XmlElement){ // 見つかった場合 return new ConfigureNode(node as XmlElement); } } // 見つからなかった場合 XmlDocument xmldoc = Element.OwnerDocument; // 要素を作成 XmlElement element = xmldoc.CreateElement(name); // 要素を追加 Element.AppendChild(element); // return new ConfigureNode(element); } } /// <summary> /// このノードの名前を取得する. /// </summary> public string Name { get { return Element.Name; } } /// <summary> /// このノードの値を取得、設定する /// </summary> public string Value { get { XmlText text = Text; return text != null ? text.Value : ""; } set { XmlDocument doc = Element.OwnerDocument; XmlText text = Text; if (text != null) { text.Value = value; } else { text = doc.CreateTextNode(value); Element.AppendChild(text); } } } /// <summary> /// コンストラクタ. /// </summary> /// <param name="element">対応するXmlElement</param> public ConfigureNode(XmlElement element) { Element = element; } /// <summary> /// ノードの値を取得する. /// </summary> /// <param name="node"></param> /// <returns></returns> public static implicit operator string(ConfigureNode node) { return node.Element.Value; } /// <summary> /// このノードが値を持っているか調べる. /// </summary> /// <returns>値を持っていれば真.</returns> public bool HasValue() { return Text != null; } } } |
ConfigureEventArgs.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Siki.Configure { /// <summary> /// /// </summary> public class ConfigureEventArgs : EventArgs{ private string m_path; public string Path { get { return m_path; } } public ConfigureEventArgs(string path){ m_path = path; } } } |

rei@sikios.com
コメントはまだありません。