00001 using System;
00002 using System.Collections.Generic;
00003 using System.IO;
00004 using System.Linq;
00005 using System.Text;
00006 using System.Windows.Forms;
00007 using System.Xml;
00008
00009 namespace Siki.Configure {
00010
00014 public class GlobalConfigure {
00015
00017 public event EventHandler<ConfigureEventArgs> Initialized;
00018
00020 public event EventHandler<ConfigureEventArgs> Loaded;
00021
00023 public event EventHandler<ConfigureEventArgs> Saved;
00024
00025
00027 private string m_fullpath = "";
00028
00032 public string FullPath {
00033 get {
00034 return m_fullpath;
00035 }
00036 protected set {
00037 m_fullpath = value;
00038 }
00039 }
00040
00042 private XmlDocument m_dom = null;
00043
00047 protected XmlDocument Dom {
00048 get {
00049 return m_dom;
00050 }
00051 }
00052
00056 private string RootName {
00057 get;
00058 set;
00059 }
00060
00068 public ConfigureNode this[string name] {
00069 get {
00070 foreach (XmlNode node in Dom.DocumentElement.ChildNodes) {
00071 if (node.Name == name && node is XmlElement) {
00072
00073 return new ConfigureNode(node as XmlElement);
00074 }
00075 }
00076
00077
00078 XmlElement element = Dom.CreateElement(name);
00079
00080 Dom.DocumentElement.AppendChild(element);
00081
00082 return new ConfigureNode(element);
00083 }
00084 }
00085
00086
00091 public GlobalConfigure()
00092 : this("conf.xml") {
00093
00094 }
00095
00096
00097
00103 public GlobalConfigure(string filename)
00104 : this(Application.UserAppDataPath, filename) {
00105
00106 }
00107
00108 public GlobalConfigure(string dir, string filename)
00109 : this(dir, filename, ""){
00110
00111 }
00112
00119 public GlobalConfigure(string dir, string filename, string rootname) {
00120 FullPath = (dir.EndsWith("\\") ? dir : dir + "\\") + filename;
00121 RootName = rootname;
00122 m_dom = new XmlDocument();
00123 Load();
00124 }
00125
00131 public virtual bool Load() {
00132 try {
00133 if (File.Exists(FullPath)) {
00134
00135
00136 Dom.Load(FullPath);
00137 return true;
00138 }
00139 throw new XmlException("ファイルが存在しません。");
00140 } catch (XmlException) {
00141
00142
00143 Dom.AppendChild(Dom.CreateXmlDeclaration("1.0", "UTF-8", "yes"));
00144 Dom.AppendChild(Dom.CreateElement(RootName));
00145 Save();
00146 } catch (Exception) {
00147
00148 return false;
00149 }
00150 return true;
00151 }
00152
00157 public virtual void OnInitialized(ConfigureEventArgs e) {
00158 if (Initialized != null) {
00159 Initialized(this, e);
00160 }
00161 }
00162
00167 public virtual void OnLoaded(ConfigureEventArgs e) {
00168 if (Loaded != null) {
00169 Loaded(this, e);
00170 }
00171 }
00172
00177 public virtual bool Save() {
00178 try {
00179 Dom.Save(FullPath);
00180 OnSaved(new ConfigureEventArgs(FullPath));
00181 } catch (XmlException) {
00182
00183 } catch {
00184
00185 return false;
00186 }
00187 return true;
00188 }
00189
00194 public virtual void OnSaved(ConfigureEventArgs e) {
00195 if(Saved != null){
00196 Saved(this, e);
00197 }
00198 }
00199 }
00200 }