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