New test.
[mono.git] / mcs / class / System.Configuration / System.Configuration / AppSettingsSection.cs
1 //
2 // System.Configuration.AppSettingsSection.cs
3 //
4 // Authors:
5 //      Duncan Mak (duncan@ximian.com)
6 //      Chris Toshok (toshok@ximian.com)
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
15 // 
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
18 // 
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 //
27 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
28 //
29
30 #if NET_2_0
31 using System;
32 using System.ComponentModel;
33 using System.Collections.Specialized;
34 using System.Xml;
35 using System.IO;
36
37 namespace System.Configuration {
38
39         public sealed class AppSettingsSection : ConfigurationSection
40         {
41                 private static ConfigurationPropertyCollection _properties;
42                 private static readonly ConfigurationProperty _propFile;
43                 private static readonly ConfigurationProperty _propSettings;
44
45                 static AppSettingsSection ()
46                 {
47                         _propFile = new ConfigurationProperty ("file", typeof(string), "",
48                                                                new StringConverter(), null, ConfigurationPropertyOptions.None);
49                         _propSettings = new ConfigurationProperty ("", typeof(KeyValueConfigurationCollection), null, 
50                                                                    null, null, ConfigurationPropertyOptions.IsDefaultCollection);
51
52                         _properties     = new ConfigurationPropertyCollection ();
53
54                         _properties.Add (_propFile);
55                         _properties.Add (_propSettings);
56                 }
57
58                 public AppSettingsSection ()
59                 {
60                 }
61
62                 protected internal override  bool IsModified ()
63                 {
64                         return Settings.IsModified ();
65                 }
66
67                 [MonoTODO ("file path?  do we use a System.Configuration api for opening it?  do we keep it open?  do we open it writable?")]
68                 protected internal override void DeserializeElement (XmlReader reader, bool serializeCollectionKey)
69                 {
70                         /* need to do this so we pick up the File attribute */
71                         base.DeserializeElement (reader, serializeCollectionKey);
72
73                         if (File != "") {
74                                 try {
75                                         Stream s = System.IO.File.OpenRead (File);
76                                         XmlReader subreader = new XmlTextReader (s);
77                                         base.DeserializeElement (subreader, serializeCollectionKey);
78                                         s.Close ();
79                                 }
80                                 catch {
81                                         // nada, we just ignore a missing/unreadble file
82                                 }
83                         }
84                 }
85
86                 protected internal override void Reset (ConfigurationElement parentSection)
87                 {
88                         AppSettingsSection psec = parentSection as AppSettingsSection;
89                         if (psec != null)
90                                 Settings.Reset (psec.Settings);
91                 }
92
93                 [MonoTODO]
94                 protected internal override string SerializeSection (
95                         ConfigurationElement parent, string name, ConfigurationSaveMode mode)
96                 {
97                         if (File == "") {
98                                 return base.SerializeSection (parent, name, mode);
99                         }
100                         else {
101                                 throw new NotImplementedException ();
102                         }
103                 }
104
105                 [ConfigurationProperty ("file", DefaultValue = "")]
106                 public string File {
107                         get { return (string)base [_propFile]; }
108                         set { base [_propFile] = value; }
109                 }
110
111                 [ConfigurationProperty ("", Options = ConfigurationPropertyOptions.IsDefaultCollection)]
112                 public KeyValueConfigurationCollection Settings {
113                         get { return (KeyValueConfigurationCollection) base [_propSettings]; }
114                 }
115
116                 protected internal override ConfigurationPropertyCollection Properties {
117                         get {
118                                 return _properties;
119                         }
120                 }
121
122                 protected internal override object GetRuntimeObject ()
123                 {
124                         KeyValueInternalCollection col = new KeyValueInternalCollection ();
125                                 
126                         foreach (string key in Settings.AllKeys) {
127                                 KeyValueConfigurationElement ele = Settings[key];
128                                 col.Add (ele.Key, ele.Value);
129                         }
130                                 
131                         if (!ConfigurationManager.ConfigurationSystem.SupportsUserConfig)
132                                 col.SetReadOnly ();
133
134                         return col;
135                 }
136         }
137 }
138 #endif