WebConfigurationSettings.cs 22 KB

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