2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System / 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 && XML_DEP
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 FileName == cfg.FileName;
63                 }
64
65 #if (XML_DEP)
66                 public override void ReadConfig (Configuration cfg, XmlTextReader reader)
67                 {
68                         string attName;
69                         ConfigurationAllowDefinition allowDefinition = ConfigurationAllowDefinition.Everywhere;
70                         FileName = cfg.FileName;
71
72                         while (reader.MoveToNextAttribute ()) {
73                                 switch (reader.Name)
74                                 {
75                                         case "allowLocation":
76                                                 string allowLoc = reader.Value;
77                                                 AllowLocation = (allowLoc == "true");
78                                                 if (!AllowLocation && allowLoc != "false")
79                                                         ThrowException ("Invalid attribute value", reader);
80                                                 break;
81         
82                                         case "allowDefinition":
83                                                 string allowDef = reader.Value;
84                                                 try {
85                                                         allowDefinition = (ConfigurationAllowDefinition) Enum.Parse (
86                                                                            typeof (ConfigurationAllowDefinition), allowDef);
87                                                 } catch {
88                                                         ThrowException ("Invalid attribute value", reader);
89                                                 }
90                                                 break;
91         
92                                         case "type":
93                                                 TypeName = reader.Value;
94                                                 break;
95                                         
96                                         case "name":
97                                                 Name = reader.Value;
98                                                 if (Name == "location")
99                                                         ThrowException ("location is a reserved section name", reader);
100                                                 break;
101                                                 
102                                         default:
103                                                 ThrowException ("Unrecognized attribute.", reader);
104                                                 break;
105                                 }
106                         }
107
108                         if (Name == null || TypeName == null)
109                                 ThrowException ("Required attribute missing", reader);
110
111                         reader.MoveToElement();
112                         reader.Skip ();
113                 }
114                 
115                 public override void WriteConfig (Configuration cfg, XmlWriter writer, ConfigurationUpdateMode mode)
116                 {
117                         writer.WriteStartElement ("section");
118                         writer.WriteAttributeString ("name", Name);
119                         writer.WriteAttributeString ("type", TypeName);
120                         if (!AllowLocation)
121                                 writer.WriteAttributeString ("allowLocation", "false");
122                         if (AllowDefinition != ConfigurationAllowDefinition.Everywhere)
123                                 writer.WriteAttributeString ("allowDefinition", AllowDefinition.ToString ());
124                         writer.WriteEndElement ();
125                 }
126                 
127                 public override void ReadData (Configuration config, XmlTextReader reader)
128                 {
129                         config.SetSectionXml (this, reader.ReadOuterXml ());
130                 }
131                 
132                 public override void WriteData (Configuration config, XmlWriter writer, ConfigurationUpdateMode mode)
133                 {
134                         string xml;
135                         
136                         ConfigurationSection section = config.GetSectionInstance (this, false);
137                         if (section != null) {
138                                 ConfigurationSection parentSection = config.Parent != null ? config.Parent.GetSectionInstance (this, false) : null;
139                                 xml = section.WriteXml (parentSection, config, Name, mode);
140                         }
141                         else {
142                                 xml = config.GetSectionXml (this);
143                         }
144                         
145                         if (xml != null) {
146                                 XmlTextReader tr = new XmlTextReader (new StringReader (xml));
147                                 writer.WriteNode (tr, true);
148                                 tr.Close ();
149                         }
150                 }
151 #endif
152         }
153 }
154
155 #endif