imported everything from my branch (which is slightly harmless).
[mono.git] / mcs / class / System.Configuration / System.Configuration / SectionInfo.cs
1 //
2 // System.Configuration.SectionInfo.cs
3 //
4 // Authors:
5 //      Lluis Sanchez (lluis@novell.com)
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
14 // 
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
17 // 
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 //
26 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
27 //
28 #if NET_2_0
29 using System;
30 using System.Collections;
31 using System.Collections.Specialized;
32 using System.Xml;
33 using System.IO;
34
35 namespace System.Configuration
36 {
37         internal class SectionInfo: ConfigInfo
38         {
39                 public bool AllowLocation = true;
40                 public ConfigurationAllowDefinition AllowDefinition = ConfigurationAllowDefinition.Everywhere;
41
42                 public SectionInfo ()
43                 {
44                 }
45                 
46                 public SectionInfo (string sectionName, string typeName,
47                                     bool allowLocation, ConfigurationAllowDefinition allowDefinition)
48                 {
49                         Name = sectionName;
50                         TypeName = typeName;
51                         AllowLocation = allowLocation;
52                         AllowDefinition = allowDefinition;
53                 }
54                 
55                 public override bool HasDataContent (Configuration config)
56                 {
57                         return config.GetSectionInstance (this, false) != null || config.GetSectionXml (this) != null;
58                 }
59                 
60                 public override bool HasConfigContent (Configuration cfg)
61                 {
62                         return StreamName == cfg.FileName;
63                 }
64
65                 public override void ReadConfig (Configuration cfg, string streamName, XmlTextReader reader)
66                 {
67                         ConfigurationAllowDefinition allowDefinition;
68                         StreamName = streamName;
69
70                         while (reader.MoveToNextAttribute ()) {
71                                 switch (reader.Name)
72                                 {
73                                         case "allowLocation":
74                                                 string allowLoc = reader.Value;
75                                                 AllowLocation = (allowLoc == "true");
76                                                 if (!AllowLocation && allowLoc != "false")
77                                                         ThrowException ("Invalid attribute value", reader);
78                                                 break;
79         
80                                         case "allowDefinition":
81                                                 string allowDef = reader.Value;
82                                                 try {
83                                                         allowDefinition = (ConfigurationAllowDefinition) Enum.Parse (
84                                                                            typeof (ConfigurationAllowDefinition), allowDef);
85                                                 } catch {
86                                                         ThrowException ("Invalid attribute value", reader);
87                                                 }
88                                                 break;
89         
90                                         case "type":
91                                                 TypeName = reader.Value;
92                                                 break;
93                                         
94                                         case "name":
95                                                 Name = reader.Value;
96                                                 if (Name == "location")
97                                                         ThrowException ("location is a reserved section name", reader);
98                                                 break;
99                                                 
100                                         default:
101                                                 ThrowException ("Unrecognized attribute.", reader);
102                                                 break;
103                                 }
104                         }
105
106                         if (Name == null || TypeName == null)
107                                 ThrowException ("Required attribute missing", reader);
108
109                         reader.MoveToElement();
110                         reader.Skip ();
111                 }
112                 
113                 public override void WriteConfig (Configuration cfg, XmlWriter writer, ConfigurationSaveMode mode)
114                 {
115                         writer.WriteStartElement ("section");
116                         writer.WriteAttributeString ("name", Name);
117                         writer.WriteAttributeString ("type", TypeName);
118                         if (!AllowLocation)
119                                 writer.WriteAttributeString ("allowLocation", "false");
120                         if (AllowDefinition != ConfigurationAllowDefinition.Everywhere)
121                                 writer.WriteAttributeString ("allowDefinition", AllowDefinition.ToString ());
122                         writer.WriteEndElement ();
123                 }
124                 
125                 public override void ReadData (Configuration config, XmlTextReader reader)
126                 {
127                         config.SetSectionXml (this, reader.ReadOuterXml ());
128                 }
129                 
130                 public override void WriteData (Configuration config, XmlWriter writer, ConfigurationSaveMode mode)
131                 {
132                         string xml;
133                         
134                         ConfigurationSection section = config.GetSectionInstance (this, false);
135                         if (section != null) {
136                                 ConfigurationSection parentSection = config.Parent != null ? config.Parent.GetSectionInstance (this, false) : null;
137                                 xml = section.SerializeSection (parentSection, Name, mode);
138                         }
139                         else {
140                                 xml = config.GetSectionXml (this);
141                         }
142                         
143                         if (xml != null) {
144                                 XmlTextReader tr = new XmlTextReader (new StringReader (xml));
145                                 writer.WriteNode (tr, true);
146                                 tr.Close ();
147                         }
148                 }
149         }
150 }
151
152 #endif