2
0

WebConfigurationSettings.cs 23 KB

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