New test.
[mono.git] / mcs / class / System.Configuration / System.Configuration / ConfigurationSection.cs
1 //
2 // System.Configuration.ConfigurationSection.cs
3 //
4 // Authors:
5 //      Duncan Mak (duncan@ximian.com)
6 //  Lluis Sanchez Gual (lluis@novell.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.Collections;
32 using System.Xml;
33 using System.IO;
34 #if !TARGET_JVM
35 using System.Security.Cryptography.Xml;
36 #endif
37
38 namespace System.Configuration
39 {
40         public abstract class ConfigurationSection : ConfigurationElement
41         {
42                 SectionInformation sectionInformation;
43                 IConfigurationSectionHandler section_handler;
44
45                 protected ConfigurationSection ()
46                 {
47                 }
48
49                 internal IConfigurationSectionHandler SectionHandler {
50                         get { return section_handler; }
51                         set { section_handler = value; }
52                 }
53
54                 [MonoTODO]
55                 public SectionInformation SectionInformation {
56                         get {
57                                 if (sectionInformation == null)
58                                         sectionInformation = new SectionInformation ();
59                                 return sectionInformation;
60                         }
61                 }
62
63                 [MonoTODO]
64                 protected internal virtual object GetRuntimeObject ()
65                 {
66                         // FIXME: this hack is nasty. We should make some
67                         // refactory on the entire assembly.
68                         if (SectionHandler != null) {
69                                 if (RawXml == null)
70                                         return null;
71                                 XmlDocument doc = new XmlDocument ();
72                                 doc.LoadXml (RawXml);
73                                 return SectionHandler.Create (null, null, doc.DocumentElement);
74                         }
75                         return this;
76                 }
77
78                 [MonoTODO]
79                 protected internal override bool IsModified ()
80                 {
81                         return base.IsModified ();
82                 }
83
84                 [MonoTODO]
85                 protected internal override void ResetModified ()
86                 {
87                         base.ResetModified ();
88                 }
89
90                 ConfigurationElement CreateElement (Type t)
91                 {
92                         ConfigurationElement elem = (ConfigurationElement) Activator.CreateInstance (t);
93                         elem.Init ();
94                         if (IsReadOnly ())
95                                 elem.SetReadOnly ();
96                         return elem;
97                 }
98
99                 [MonoTODO ("find the proper location for the decryption stuff")]
100                 protected internal virtual void DeserializeSection (XmlReader reader)
101                 {
102                         reader.MoveToContent ();
103
104                         /* XXX this stuff shouldn't be here */
105                         {
106                                 string protection_provider = null;
107
108                                 while (reader.MoveToNextAttribute ()) {
109                                         if (reader.LocalName == "configProtectionProvider")
110                                                 protection_provider = reader.Value;
111                                 }
112
113                                 if (protection_provider != null) {
114                                         ProtectedConfigurationProvider prov = ProtectedConfiguration.GetProvider (protection_provider, true);
115                                         XmlDocument doc = new XmlDocument ();
116
117                                         reader.MoveToElement ();
118
119                                         doc.Load (new StringReader (reader.ReadInnerXml ()));
120
121                                         XmlNode n = prov.Decrypt (doc);
122
123                                         reader = new XmlNodeReader (n);
124
125                                         SectionInformation.ProtectSection (protection_provider);
126
127                                         reader.MoveToContent ();
128                                 }
129                         }
130
131                         SectionInformation.SetRawXml (RawXml);
132                         DeserializeElement (reader, false);
133                 }
134
135                 protected internal virtual string SerializeSection (ConfigurationElement parentElement, string name, ConfigurationSaveMode saveMode)
136                 {
137                         ConfigurationElement elem;
138                         if (parentElement != null) {
139                                 elem = (ConfigurationElement) CreateElement (GetType());
140                                 elem.Unmerge (this, parentElement, saveMode);
141                         }
142                         else
143                                 elem = this;
144
145                         StringWriter sw = new StringWriter ();
146                         XmlTextWriter tw = new XmlTextWriter (sw);
147                         tw.Formatting = Formatting.Indented;
148                         elem.SerializeToXmlElement (tw, name);
149                         tw.Close ();
150                         return sw.ToString ();
151                 }
152         }
153 }
154 #endif