Minor orbis fix in System.Net
[mono.git] / mcs / nunit24 / ClientUtilities / util / XmlSettingsStorage.cs
1 // ****************************************************************\r
2 // Copyright 2007, Charlie Poole\r
3 // This is free software licensed under the NUnit license. You may\r
4 // obtain a copy of the license at http://nunit.org/?p=license&r=2.4\r
5 // ****************************************************************\r
6 \r
7 using System;\r
8 using System.IO;\r
9 using System.Xml;\r
10 using System.Collections;\r
11 using System.ComponentModel;\r
12 \r
13 namespace NUnit.Util\r
14 {\r
15         /// <summary>\r
16         /// Summary description for XmlSettingsStorage.\r
17         /// </summary>\r
18         public class XmlSettingsStorage : MemorySettingsStorage\r
19         {\r
20                 private string filePath;\r
21 \r
22                 public XmlSettingsStorage( string filePath )\r
23                 {\r
24                         this.filePath = filePath;\r
25                 }\r
26 \r
27                 public override void LoadSettings()\r
28                 {\r
29                         FileInfo info = new FileInfo(filePath);\r
30                         if ( !info.Exists || info.Length == 0 )\r
31                                 return;\r
32 \r
33                         try\r
34                         {\r
35                                 XmlDocument doc = new XmlDocument();\r
36                                 doc.Load( filePath );\r
37 \r
38                                 foreach( XmlElement element in doc.DocumentElement["Settings"].ChildNodes )\r
39                                 {\r
40                                         if ( element.Name != "Setting" )\r
41                                                 throw new ApplicationException( "Unknown element in settings file: " + element.Name );\r
42 \r
43                                         if ( !element.HasAttribute( "name" ) )\r
44                                                 throw new ApplicationException( "Setting must have 'name' attribute" );\r
45 \r
46                                         if ( !element.HasAttribute( "value" ) )\r
47                                                 throw new ApplicationException( "Setting must have 'value' attribute" );\r
48 \r
49                                         settings[ element.GetAttribute( "name" ) ] = element.GetAttribute( "value" );\r
50                                 }\r
51                         }\r
52                         catch( Exception ex )\r
53                         {\r
54                                 throw new ApplicationException( "Error loading settings file", ex );\r
55                         }\r
56                 }\r
57 \r
58                 public override void SaveSettings()\r
59                 {\r
60                         string dirPath = Path.GetDirectoryName( filePath );\r
61                         if ( !Directory.Exists( dirPath ) )\r
62                                 Directory.CreateDirectory( dirPath );\r
63 \r
64                         XmlTextWriter writer = new XmlTextWriter(  filePath, System.Text.Encoding.UTF8 );\r
65                         writer.Formatting = Formatting.Indented;\r
66 \r
67                         writer.WriteProcessingInstruction( "xml", "version=\"1.0\"" );\r
68                         writer.WriteStartElement( "NUnitSettings" );\r
69                         writer.WriteStartElement( "Settings" );\r
70 \r
71                         ArrayList keys = new ArrayList( settings.Keys );\r
72                         keys.Sort();\r
73 \r
74                         foreach( string name in keys )\r
75                         {\r
76                                 object val = settings[name];\r
77                                 if ( val != null )\r
78                                 {\r
79                                         writer.WriteStartElement( "Setting");\r
80                                         writer.WriteAttributeString( "name", name );\r
81                                         writer.WriteAttributeString( "value", val.ToString() );\r
82                                         writer.WriteEndElement();\r
83                                 }\r
84                         }\r
85 \r
86                         writer.WriteEndElement();\r
87                         writer.WriteEndElement();\r
88                         writer.Close();\r
89                 }\r
90         }\r
91 }\r