* SessionStateModule.cs: If using cookieless sessions add an
[mono.git] / mcs / class / System.Web / System.Web.Configuration / WebConfigurationSettings.cs
1 //
2 // System.Configuration.WebConfigurationSettings.cs
3 //
4 // Authors:
5 //   Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (c) 2003 Novell, Inc. (http://www.novell.com)
8 //
9
10 using System;
11 using System.Configuration;
12 using System.Collections;
13 using System.IO;
14 using System.Reflection;
15 using System.Web.Util;
16 using System.Xml;
17
18 namespace System.Web.Configuration
19 {
20         class WebConfigurationSettings
21         {
22                 static IConfigurationSystem oldConfig;
23                 static WebDefaultConfig config;
24                 static string machineConfigPath;
25                 const BindingFlags privStatic = BindingFlags.NonPublic | BindingFlags.Static;
26                         
27                 private WebConfigurationSettings ()
28                 {
29                 }
30
31                 public static void Init ()
32                 {
33                         lock (typeof (WebConfigurationSettings)) {
34                                 if (config != null)
35                                         return;
36
37                                 WebDefaultConfig settings = WebDefaultConfig.GetInstance ();
38                                 Type t = typeof (ConfigurationSettings);
39                                 MethodInfo changeConfig = t.GetMethod ("ChangeConfigurationSystem",
40                                                                       privStatic);
41
42                                 if (changeConfig == null)
43                                         throw new ConfigurationException ("Cannot find method CCS");
44
45                                 object [] args = new object [] {settings};
46                                 oldConfig = (IConfigurationSystem) changeConfig.Invoke (null, args);
47                                 config = settings;
48                         }
49                 }
50
51                 public static void Init (HttpContext context)
52                 {
53                         Init ();
54                         config.Init (context);
55                 }
56                 
57                 public static object GetConfig (string sectionName)
58                 {
59                         return config.GetConfig (sectionName);
60                 }
61
62                 public static object GetConfig (string sectionName, HttpContext context)
63                 {
64                         return config.GetConfig (sectionName, context);
65                 }
66
67                 public static string MachineConfigPath {
68                         get {
69                                 lock (typeof (WebConfigurationSettings)) {
70                                         if (machineConfigPath != null)
71                                                 return machineConfigPath;
72
73                                         Type t = oldConfig.GetType ();
74                                         MethodInfo getMC = t.GetMethod ("GetMachineConfigPath",
75                                                                         privStatic);
76
77                                         if (getMC == null)
78                                                 throw new ConfigurationException ("Cannot find method GMC");
79
80                                         machineConfigPath = (string) getMC.Invoke (null, null);
81                                         return machineConfigPath;
82                                 }
83                         }
84                 }
85         }
86
87         //
88         // class WebDefaultConfig: read configuration from machine.config file and application
89         // config file if available.
90         //
91         class WebDefaultConfig : IConfigurationSystem
92         {
93                 static WebDefaultConfig instance;
94                 Hashtable fileToConfig;
95                 HttpContext firstContext;
96                 bool initCalled;
97
98                 static WebDefaultConfig ()
99                 {
100                         instance = new WebDefaultConfig ();
101                 }
102
103                 private WebDefaultConfig ()
104                 {
105                         fileToConfig = new Hashtable ();
106                 }
107
108                 public static WebDefaultConfig GetInstance ()
109                 {
110                         return instance;
111                 }
112
113                 public object GetConfig (string sectionName)
114                 {
115                         HttpContext current = HttpContext.Current;
116                         if (current == null)
117                                 current = firstContext;
118                         return GetConfig (sectionName, current);
119                 }
120
121                 public object GetConfig (string sectionName, HttpContext context)
122                 {
123                         if (context == null)
124                                 return null;
125
126                         ConfigurationData config = GetConfigFromFileName (context.Request.FilePath, context);
127                         if (config == null)
128                                 return null;
129
130                         return config.GetConfig (sectionName, context);
131                 }
132
133                 ConfigurationData GetConfigFromFileName (string filepath, HttpContext context)
134                 {
135                         if (filepath == "") {
136                                 return (ConfigurationData) fileToConfig [WebConfigurationSettings.MachineConfigPath];
137                         }
138
139                         string dir = UrlUtils.GetDirectory (filepath);
140                         if (fileToConfig.ContainsKey (dir)) {
141                                 ConfigurationData data = (ConfigurationData) fileToConfig [dir];
142                                 if (CheckFileCache (data))
143                                         return data;
144                         }
145
146                         string realpath = context.Request.MapPath (dir);
147                         string lower = Path.Combine (realpath, "web.config");
148                         string upper = Path.Combine (realpath, "Web.config");
149                         bool isUpper = File.Exists (upper);
150                         bool isLower = File.Exists (lower);
151                         if (Path.DirectorySeparatorChar == '/' && isUpper && isLower)
152                                 throw new ConfigurationException ("Both web.config and Web.config exist for " + dir);
153
154                         if (dir == "/")
155                                 dir = "";
156
157                         string wcfile = (isUpper) ? upper : (isLower) ? lower : null;
158                         ConfigurationData parent = GetConfigFromFileName (dir, context);
159                         if (wcfile == null)
160                                 return parent;
161
162                         ConfigurationData child = new ConfigurationData (parent);
163                         child.DirName = dir;
164                         child.LoadFromFile (wcfile);
165                         fileToConfig [dir] = child;
166                         return child;
167                 }
168
169                 bool CheckFileCache (ConfigurationData data)
170                 {
171                         if (data == null)
172                                 return true;
173
174                         if (data.FileCache [""] == FileWatcherCache.Changed)
175                                 return false;
176
177                         return CheckFileCache (data.Parent);
178                 }
179
180                 public void Init ()
181                 {
182                         // nothing. We need a context.
183                 }
184
185                 public void Init (HttpContext context)
186                 {
187                         if (initCalled)
188                                 return;
189
190                         lock (this) {
191                                 if (initCalled)
192                                         return;
193
194                                 firstContext = context;
195                                 ConfigurationData data = new ConfigurationData ();
196                                 if (!data.LoadFromFile (WebConfigurationSettings.MachineConfigPath))
197                                         throw new ConfigurationException ("Cannot find " + WebConfigurationSettings.MachineConfigPath);
198
199                                 fileToConfig [WebConfigurationSettings.MachineConfigPath] = data;
200                                 initCalled = true;
201                         }
202                 }
203
204                 static string GetAppConfigPath ()
205                 {
206                         AppDomainSetup currentInfo = AppDomain.CurrentDomain.SetupInformation;
207
208                         string configFile = currentInfo.ConfigurationFile;
209                         if (configFile == null || configFile.Length == 0)
210                                 return null;
211
212                         return configFile;
213
214                 }
215         }
216
217         //
218         // TODO: this should be changed to use the FileSystemWatcher
219         //
220         //  -eric@5stops.com 9.20.2003
221         //
222         class FileWatcherCache
223         {
224                 Hashtable cacheTable;
225                 DateTime lastWriteTime;
226                 string filename;
227                 public static readonly object Changed = new object ();
228                 static TimeSpan seconds = new TimeSpan (0, 0, 2);
229
230                 public FileWatcherCache (string filename)
231                 {
232                         cacheTable = Hashtable.Synchronized (new Hashtable ());
233                         lastWriteTime = new FileInfo (filename).LastWriteTime;
234                         this.filename = filename;
235                 }
236
237                 bool CheckFileChange ()
238                 {
239                         FileInfo info = new FileInfo (filename);
240
241                         if (!info.Exists) {
242                                 lastWriteTime = DateTime.MinValue;
243                                 cacheTable.Clear ();
244                                 return false;
245                         }
246
247                         DateTime writeTime = info.LastWriteTime;
248                         TimeSpan ts = (info.LastWriteTime - lastWriteTime);
249                         if (ts >= seconds) {
250                                 lastWriteTime = writeTime;
251                                 cacheTable.Clear ();
252                                 return false;
253                         }
254
255                         return true;
256                 }
257
258                 public object this [string key] {
259                         get {
260                                 if (!CheckFileChange ())
261                                         return Changed;
262
263                                 return cacheTable [key];
264                         }
265
266                         set {
267                                 cacheTable [key] = value;
268                         }
269                 }
270         }
271
272         enum AllowDefinition
273         {
274                 Everywhere,
275                 MachineOnly,
276                 MachineToApplication
277         }
278         
279         class SectionData
280         {
281                 public readonly string SectionName;
282                 public readonly string TypeName;
283                 public readonly bool AllowLocation;
284                 public readonly AllowDefinition AllowDefinition;
285                 public string FileName;
286
287                 public SectionData (string sectionName, string typeName,
288                                     bool allowLocation, AllowDefinition allowDefinition)
289                 {
290                         SectionName = sectionName;
291                         TypeName = typeName;
292                         AllowLocation = allowLocation;
293                         AllowDefinition = allowDefinition;
294                 }
295         }
296
297         class ConfigurationData
298         {
299                 ConfigurationData parent;
300                 Hashtable factories;
301                 Hashtable pending;
302                 Hashtable locations;
303                 string fileName;
304                 string dirname;
305                 static object removedMark = new object ();
306                 static object groupMark = new object ();
307                 static object emptyMark = new object ();
308                 FileWatcherCache fileCache;
309                 static char [] forbiddenPathChars = new char [] {
310                                         ';', '?', ':', '@', '&', '=', '+',
311                                         '$', ',','\\', '*', '\"', '<', '>'
312                                         };
313
314                 static string forbiddenStr = "';', '?', ':', '@', '&', '=', '+', '$', ',', '\\', '*', '\"', '<', '>'";
315
316                 internal FileWatcherCache FileCache {
317                         get {
318                                 lock (this) {
319                                         if (fileCache != null)
320                                                 return fileCache;
321
322                                         fileCache = new FileWatcherCache (fileName);
323                                 }
324
325                                 return fileCache;
326                         }
327                 }
328
329                 internal string FileName {
330                         get { return fileName; }
331                 }
332
333                 internal ConfigurationData Parent {
334                         get { return parent; }
335                 }
336
337                 internal string DirName {
338                         get { return dirname; }
339                         set { dirname = value; }
340                 }
341
342
343                 public ConfigurationData () : this (null)
344                 {
345                 }
346
347                 public ConfigurationData (ConfigurationData parent)
348                 {
349                         this.parent = (parent == this) ? null : parent;
350                         factories = new Hashtable ();
351                 }
352
353                 public bool LoadFromFile (string fileName)
354                 {
355                         this.fileName = fileName;
356                         if (fileName == null || !File.Exists (fileName))
357                                 return false;
358
359                         XmlTextReader reader = null;
360
361                         try {
362                                 FileStream fs = new FileStream (fileName, FileMode.Open, FileAccess.Read);
363                                 reader = new XmlTextReader (fs);
364                                 InitRead (reader);
365                                 ReadConfig (reader, false);
366                         } catch (ConfigurationException) {
367                                 throw;
368                         } catch (Exception e) {
369                                 throw new ConfigurationException ("Error reading " + fileName, e);
370                         } finally {
371                                 if (reader != null)
372                                         reader.Close();
373                         }
374
375                         return true;
376                 }
377
378                 public void LoadFromReader (XmlTextReader reader, string fakeFileName, bool isLocation)
379                 {
380                         fileName = fakeFileName;
381                         MoveToNextElement (reader);
382                         ReadConfig (reader, isLocation);
383                 }
384
385                 object GetHandler (string sectionName)
386                 {
387                         lock (factories) {
388                                 object o = factories [sectionName];
389                                 if (o == null || o == removedMark) {
390                                         if (parent != null)
391                                                 return parent.GetHandler (sectionName);
392
393                                         return null;
394                                 }
395
396                                 if (o is IConfigurationSectionHandler)
397                                         return (IConfigurationSectionHandler) o;
398
399                                 o = CreateNewHandler (sectionName, (SectionData) o);
400                                 factories [sectionName] = o;
401                                 return o;
402                         }
403                 }
404
405                 object CreateNewHandler (string sectionName, SectionData section)
406                 {
407                         Type t = Type.GetType (section.TypeName);
408                         if (t == null)
409                                 throw new ConfigurationException ("Cannot get Type for " + section.TypeName);
410
411                         Type iconfig = typeof (IConfigurationSectionHandler);
412                         if (!iconfig.IsAssignableFrom (t))
413                                 throw new ConfigurationException (sectionName + " does not implement " + iconfig);
414                         
415                         object o = Activator.CreateInstance (t, true);
416                         if (o == null)
417                                 throw new ConfigurationException ("Cannot get instance for " + t);
418
419                         return o;
420                 }
421
422                 XmlDocument GetInnerDoc (XmlDocument doc, int i, string [] sectionPath)
423                 {
424                         if (++i >= sectionPath.Length)
425                                 return doc;
426
427                         if (doc.DocumentElement == null)
428                                 return null;
429
430                         XmlNode node = doc.DocumentElement.FirstChild;
431                         while (node != null) {
432                                 if (node.Name == sectionPath [i]) {
433                                         ConfigXmlDocument result = new ConfigXmlDocument ();
434                                         result.Load (new StringReader (node.OuterXml));
435                                         return GetInnerDoc (result, i, sectionPath);
436                                 }
437                                 node = node.NextSibling;
438                         }
439
440                         return null;
441                 }
442
443                 XmlDocument GetDocumentForSection (string sectionName)
444                 {
445                         ConfigXmlDocument doc = new ConfigXmlDocument ();
446                         if (pending == null)
447                                 return doc;
448
449                         string [] sectionPath = sectionName.Split ('/');
450                         string outerxml = pending [sectionPath [0]] as string;
451                         if (outerxml == null)
452                                 return doc;
453                         
454                         doc.Load (new StringReader (outerxml));
455                         return GetInnerDoc (doc, 0, sectionPath);
456                 }
457                 
458                 object GetConfigInternal (string sectionName, HttpContext context, bool useLoc)
459                 {
460                         object handler = GetHandler (sectionName);
461                         IConfigurationSectionHandler iconf = handler as IConfigurationSectionHandler;
462                         if (iconf == null)
463                                 return handler;
464
465                         object parentConfig = null;
466                         if (parent != null) {
467                                 if (useLoc)
468                                         parentConfig = parent.GetConfig (sectionName, context);
469                                 else
470                                         parentConfig = parent.GetConfigOptLocation (sectionName, context, false);
471                         }
472
473                         XmlDocument doc = GetDocumentForSection (sectionName);
474                         if (doc == null || doc.DocumentElement == null)
475                                 return parentConfig;
476                         
477                         return iconf.Create (parentConfig, fileName, doc.DocumentElement);
478                 }
479
480                 public object GetConfig (string sectionName, HttpContext context)
481                 {
482                         if (locations != null && dirname != null) {
483                                 string reduced = UrlUtils.MakeRelative (context.Request.FilePath, dirname);
484                                 Location location = locations [reduced] as Location;
485                                 if (location == null) {
486                                         location = locations ["*"] as Location;
487                                 }
488
489                                 if (location != null && location.Config != null) {
490                                         object o = location.Config.GetConfigOptLocation (sectionName, context, false);
491                                         if (o != null) {
492                                                 return o;
493                                         }
494                                 }
495                         }
496
497                         return GetConfigOptLocation (sectionName, context, true);
498                 }
499
500                 object GetConfigOptLocation (string sectionName, HttpContext context, bool useLoc)
501                 {
502                         object config = this.FileCache [sectionName];
503                         if (config == emptyMark)
504                                 return null;
505
506                         if (config != null)
507                                 return config;
508
509                         lock (this) {
510                                 config = GetConfigInternal (sectionName, context, useLoc);
511                                 this.FileCache [sectionName] = (config == null) ? emptyMark : config;
512                         }
513
514                         return config;
515                 }
516
517                 private object LookForFactory (string key)
518                 {
519                         object o = factories [key];
520                         if (o != null)
521                                 return o;
522
523                         if (parent != null)
524                                 return parent.LookForFactory (key);
525
526                         return null;
527                 }
528                 
529                 private void InitRead (XmlTextReader reader)
530                 {
531                         reader.MoveToContent ();
532                         if (reader.NodeType != XmlNodeType.Element || reader.Name != "configuration")
533                                 ThrowException ("Configuration file does not have a valid root element", reader);
534
535                         if (reader.HasAttributes)
536                                 ThrowException ("Unrecognized attribute in root element", reader);
537
538                         MoveToNextElement (reader);
539                 }
540
541                 internal void MoveToNextElement (XmlTextReader reader)
542                 {
543                         while (reader.Read ()) {
544                                 XmlNodeType ntype = reader.NodeType;
545                                 if (ntype == XmlNodeType.Element)
546                                         return;
547
548                                 if (ntype != XmlNodeType.Whitespace &&
549                                     ntype != XmlNodeType.Comment &&
550                                     ntype != XmlNodeType.SignificantWhitespace &&
551                                     ntype != XmlNodeType.EndElement)
552                                         ThrowException ("Unrecognized element", reader);
553                         }
554                 }
555
556                 private void ReadSection (XmlTextReader reader, string sectionName)
557                 {
558                         string attName;
559                         string nameValue = null;
560                         string typeValue = null;
561                         string allowLoc = null, allowDef = null;
562                         bool allowLocation = true;
563                         AllowDefinition allowDefinition = AllowDefinition.Everywhere;
564
565                         while (reader.MoveToNextAttribute ()) {
566                                 attName = reader.Name;
567                                 if (attName == null)
568                                         continue;
569
570                                 if (attName == "allowLocation") {
571                                         if (allowLoc != null)
572                                                 ThrowException ("Duplicated allowLocation attribute.", reader);
573
574                                         allowLoc = reader.Value;
575                                         allowLocation = (allowLoc == "true");
576                                         if (!allowLocation && allowLoc != "false")
577                                                 ThrowException ("Invalid attribute value", reader);
578
579                                         continue;
580                                 }
581
582                                 if (attName == "allowDefinition") {
583                                         if (allowDef != null)
584                                                 ThrowException ("Duplicated allowDefinition attribute.", reader);
585
586                                         allowDef = reader.Value;
587                                         try {
588                                                 allowDefinition = (AllowDefinition) Enum.Parse (
589                                                                    typeof (AllowDefinition), allowDef);
590                                         } catch {
591                                                 ThrowException ("Invalid attribute value", reader);
592                                         }
593
594                                         continue;
595                                 }
596
597                                 if (attName == "type")  {
598                                         if (typeValue != null)
599                                                 ThrowException ("Duplicated type attribute.", reader);
600                                         typeValue = reader.Value;
601                                         continue;
602                                 }
603                                 
604                                 if (attName == "name")  {
605                                         if (nameValue != null)
606                                                 ThrowException ("Duplicated name attribute.", reader);
607
608                                         nameValue = reader.Value;
609                                         if (nameValue == "location")
610                                                 ThrowException ("location is a reserved section name", reader);
611                                         continue;
612                                 }
613
614                                 ThrowException ("Unrecognized attribute.", reader);
615                         }
616
617                         if (nameValue == null || typeValue == null)
618                                 ThrowException ("Required attribute missing", reader);
619
620                         if (sectionName != null)
621                                 nameValue = sectionName + '/' + nameValue;
622
623                         reader.MoveToElement();
624                         object o = LookForFactory (nameValue);
625                         if (o != null && o != removedMark)
626                                 ThrowException ("Already have a factory for " + nameValue, reader);
627
628                         SectionData section = new SectionData (nameValue, typeValue, allowLocation, allowDefinition);
629                         section.FileName = fileName;
630                         factories [nameValue] = section;
631                         MoveToNextElement (reader);
632                 }
633
634                 private void ReadRemoveSection (XmlTextReader reader, string sectionName)
635                 {
636                         if (!reader.MoveToNextAttribute () || reader.Name != "name")
637                                 ThrowException ("Unrecognized attribute.", reader);
638
639                         string removeValue = reader.Value;
640                         if (removeValue == null || removeValue.Length == 0)
641                                 ThrowException ("Empty name to remove", reader);
642
643                         reader.MoveToElement ();
644
645                         if (sectionName != null)
646                                 removeValue = sectionName + '/' + removeValue;
647
648                         object o = LookForFactory (removeValue);
649                         if (o != null && o == removedMark)
650                                 ThrowException ("No factory for " + removeValue, reader);
651
652                         factories [removeValue] = removedMark;
653                         MoveToNextElement (reader);
654                 }
655
656                 private void ReadSectionGroup (XmlTextReader reader, string configSection)
657                 {
658                         if (!reader.MoveToNextAttribute ())
659                                 ThrowException ("sectionGroup must have a 'name' attribute.", reader);
660
661                         if (reader.Name != "name")
662                                 ThrowException ("Unrecognized attribute.", reader);
663
664                         if (reader.MoveToNextAttribute ())
665                                 ThrowException ("Unrecognized attribute.", reader);
666
667                         string value = reader.Value;
668                         if (value == "location")
669                                 ThrowException ("location is a reserved section name", reader);
670                         
671                         if (configSection != null)
672                                 value = configSection + '/' + value;
673
674                         object o = LookForFactory (value);
675                         if (o != null && o != removedMark && o != groupMark)
676                                 ThrowException ("Already have a factory for " + value, reader);
677
678                         factories [value] = groupMark;
679                         MoveToNextElement (reader);
680                         ReadSections (reader, value);
681                 }
682
683                 private void ReadSections (XmlTextReader reader, string configSection)
684                 {
685                         int depth = reader.Depth;
686                         while (reader.Depth == depth) {
687                                 string name = reader.Name;
688                                 if (name == "section") {
689                                         ReadSection (reader, configSection);
690                                         continue;
691                                 } 
692                                 
693                                 if (name == "remove") {
694                                         ReadRemoveSection (reader, configSection);
695                                         continue;
696                                 }
697
698                                 if (name == "clear") {
699                                         if (reader.HasAttributes)
700                                                 ThrowException ("Unrecognized attribute.", reader);
701
702                                         factories.Clear ();
703                                         MoveToNextElement (reader);
704                                         continue;
705                                 }
706
707                                 if (name == "sectionGroup") {
708                                         ReadSectionGroup (reader, configSection);
709                                         continue;
710                                 }
711
712                                 ThrowException ("Unrecognized element: " + reader.Name, reader);
713                         }
714                 }
715
716                 void StoreLocation (string name, XmlTextReader reader)
717                 {
718                         if (locations == null) {
719                                 locations = new Hashtable ();
720                         }
721
722                         string path = null;
723                         bool haveAllow = false;
724                         bool allowOverride = true;
725
726                         while (reader.MoveToNextAttribute ()) {
727                                 string att = reader.Name;
728
729                                 if (att == "path") {
730                                         if (path != null)
731                                                 ThrowException ("Duplicate path attribute", reader);
732
733                                         path = reader.Value;
734                                         if (path.StartsWith ("."))
735                                                 ThrowException ("Path cannot begin with '.'", reader);
736
737                                         if (path.IndexOfAny (forbiddenPathChars) != -1)
738                                                 ThrowException ("Path cannot contain " + forbiddenStr, reader);
739
740                                         continue;
741                                 }
742
743                                 if (att == "allowOverride") {
744                                         if (haveAllow)
745                                                 ThrowException ("Duplicate allowOverride attribute", reader);
746
747                                         haveAllow = true;
748                                         allowOverride = (reader.Value == "true");
749                                         if (!allowOverride && reader.Value != "false")
750                                                 ThrowException ("allowOverride must be either true or false", reader);
751                                         continue;
752                                 }
753
754                                 ThrowException ("Unrecognized attribute.", reader);
755                         }
756
757                         Location loc = new Location (this, path, allowOverride);
758                         if (locations.ContainsKey (loc.Path))
759                                 ThrowException ("Duplicated location path: " + loc.Path, reader);
760
761                         reader.MoveToElement ();
762                         loc.LoadFromString (reader.ReadInnerXml ());
763                         locations [loc.Path] = loc;
764                         if (!loc.AllowOverride) {
765                                 XmlTextReader inner = loc.GetReader ();
766                                 if (inner != null) {
767                                         MoveToNextElement (inner);
768                                         ReadConfig (loc.GetReader (), true);
769                                 }
770                         }
771
772                         loc.XmlStr = null;
773                 }
774
775                 void StorePending (string name, XmlTextReader reader)
776                 {
777                         if (pending == null)
778                                 pending = new Hashtable ();
779
780                         if (pending.ContainsKey (name))
781                                 ThrowException ("Sections can only appear once: " + name, reader);
782
783                         pending [name] = reader.ReadOuterXml ();
784                 }
785
786                 void ReadConfig (XmlTextReader reader, bool isLocation)
787                 {
788                         int depth = reader.Depth;
789                         while (!reader.EOF && reader.Depth == depth) {
790                                 string name = reader.Name;
791
792                                 if (name == "configSections") {
793                                         if (isLocation)
794                                                 ThrowException ("<configSections> inside <location>", reader);
795
796                                         if (reader.HasAttributes)
797                                                 ThrowException ("Unrecognized attribute in <configSections>.", reader);
798
799                                         MoveToNextElement (reader);
800                                         ReadSections (reader, null);
801                                 } else if (name == "location") {
802                                         if (isLocation)
803                                                 ThrowException ("<location> inside <location>", reader);
804
805                                         StoreLocation (name, reader);
806                                         MoveToNextElement (reader);
807                                 } else if (name != null && name != ""){
808                                         StorePending (name, reader);
809                                         MoveToNextElement (reader);
810                                 } else {
811                                         MoveToNextElement (reader);
812                                 }
813                         }
814                 }
815                                 
816                 void ThrowException (string text, XmlTextReader reader)
817                 {
818                         throw new ConfigurationException (text, fileName, reader.LineNumber);
819                 }
820         }
821
822         class Location
823         {
824                 string path;
825                 bool allowOverride;
826                 ConfigurationData parent;
827                 ConfigurationData thisOne;
828                 string xmlstr;
829
830                 public Location (ConfigurationData parent, string path, bool allowOverride)
831                 {
832                         this.parent = parent;
833                         this.allowOverride = allowOverride;
834                         this.path = (path == null || path == "") ? "*" : path;
835                 }
836
837                 public bool AllowOverride {
838                         get { return (path != "*" || allowOverride); }
839                 }
840
841                 public string Path {
842                         get { return path; }
843                 }
844                 
845                 public string XmlStr {
846                         set { xmlstr = value; }
847                 }
848                 
849                 public void LoadFromString (string str)
850                 {
851                         if (str == null)
852                                 throw new ArgumentNullException ("str");
853
854                         if (thisOne != null)
855                                 throw new InvalidOperationException ();
856
857                         this.xmlstr = str.Trim ();
858                         if (xmlstr == "")
859                                 return;
860
861                         XmlTextReader reader = new XmlTextReader (new StringReader (str));
862                         thisOne = new ConfigurationData (parent);
863                         thisOne.LoadFromReader (reader, parent.FileName, true);
864                 }
865
866                 public XmlTextReader GetReader ()
867                 {
868                         if (xmlstr == "")
869                                 return null;
870
871                         XmlTextReader reader = new XmlTextReader (new StringReader (xmlstr));
872                         return reader;
873                 }
874
875                 public ConfigurationData Config {
876                         get { return thisOne; }
877                 }
878         }
879 }
880
881