2
0

ConfigXmlDocument.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. //
  2. // System.Configuration.ConfigXmlDocument
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if CONFIGURATION_DEP
  30. using System.Configuration.Internal;
  31. #endif
  32. using System.IO;
  33. using System.Security;
  34. using System.Security.Permissions;
  35. #if (XML_DEP)
  36. using System.Xml;
  37. namespace System.Configuration
  38. {
  39. [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
  40. public sealed class ConfigXmlDocument : XmlDocument, IConfigXmlNode
  41. #if CONFIGURATION_DEP
  42. , IConfigErrorInfo
  43. #endif
  44. {
  45. XmlTextReader reader;
  46. string fileName;
  47. int lineNumber;
  48. public override XmlAttribute CreateAttribute (string prefix,
  49. string localName,
  50. string namespaceUri)
  51. {
  52. return new ConfigXmlAttribute (this, prefix, localName, namespaceUri);
  53. }
  54. public override XmlCDataSection CreateCDataSection (string data)
  55. {
  56. return new ConfigXmlCDataSection (this, data);
  57. }
  58. public override XmlComment CreateComment (string comment)
  59. {
  60. return new ConfigXmlComment (this, comment);
  61. }
  62. public override XmlElement CreateElement (string prefix, string localName, string namespaceUri)
  63. {
  64. return new ConfigXmlElement (this, prefix, localName, namespaceUri);
  65. }
  66. public override XmlSignificantWhitespace CreateSignificantWhitespace (string data)
  67. {
  68. return base.CreateSignificantWhitespace (data);
  69. }
  70. public override XmlText CreateTextNode (string text)
  71. {
  72. return new ConfigXmlText (this, text);
  73. }
  74. public override XmlWhitespace CreateWhitespace (string data)
  75. {
  76. return base.CreateWhitespace (data);
  77. }
  78. public override void Load (string filename)
  79. {
  80. XmlTextReader rd = new XmlTextReader (filename);
  81. try {
  82. rd.MoveToContent ();
  83. LoadSingleElement (filename, rd);
  84. } finally {
  85. rd.Close ();
  86. }
  87. }
  88. public void LoadSingleElement (string filename, XmlTextReader sourceReader)
  89. {
  90. fileName = filename;
  91. lineNumber = sourceReader.LineNumber;
  92. string xml = sourceReader.ReadOuterXml();
  93. reader = new XmlTextReader (new StringReader (xml), sourceReader.NameTable);
  94. Load (reader);
  95. reader.Close ();
  96. }
  97. public string Filename
  98. {
  99. get {
  100. if ((fileName != null) && (fileName.Length > 0) && SecurityManager.SecurityEnabled) {
  101. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fileName).Demand ();
  102. }
  103. return fileName;
  104. }
  105. }
  106. public int LineNumber
  107. {
  108. get {
  109. return lineNumber;
  110. }
  111. }
  112. #if CONFIGURATION_DEP
  113. string System.Configuration.Internal.IConfigErrorInfo.Filename {
  114. get { return Filename; }
  115. }
  116. int System.Configuration.Internal.IConfigErrorInfo.LineNumber {
  117. get { return LineNumber; }
  118. }
  119. #endif
  120. string IConfigXmlNode.Filename {
  121. get { return Filename; }
  122. }
  123. int IConfigXmlNode.LineNumber {
  124. get { return LineNumber; }
  125. }
  126. //
  127. // Wrappers for Xml* that just provide file name and line number addition
  128. //
  129. class ConfigXmlAttribute : XmlAttribute, IConfigXmlNode
  130. #if CONFIGURATION_DEP
  131. , IConfigErrorInfo
  132. #endif
  133. {
  134. string fileName;
  135. int lineNumber;
  136. public ConfigXmlAttribute (ConfigXmlDocument document,
  137. string prefix,
  138. string localName,
  139. string namespaceUri)
  140. : base (prefix, localName, namespaceUri, document)
  141. {
  142. fileName = document.fileName;
  143. lineNumber = document.LineNumber;
  144. }
  145. public string Filename
  146. {
  147. get {
  148. if ((fileName != null) && (fileName.Length > 0) && SecurityManager.SecurityEnabled) {
  149. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fileName).Demand ();
  150. }
  151. return fileName;
  152. }
  153. }
  154. public int LineNumber
  155. {
  156. get {
  157. return lineNumber;
  158. }
  159. }
  160. }
  161. class ConfigXmlCDataSection : XmlCDataSection, IConfigXmlNode
  162. #if CONFIGURATION_DEP
  163. , IConfigErrorInfo
  164. #endif
  165. {
  166. string fileName;
  167. int lineNumber;
  168. public ConfigXmlCDataSection (ConfigXmlDocument document, string data)
  169. : base (data, document)
  170. {
  171. fileName = document.fileName;
  172. lineNumber = document.LineNumber;
  173. }
  174. public string Filename
  175. {
  176. get {
  177. if ((fileName != null) && (fileName.Length > 0) && SecurityManager.SecurityEnabled) {
  178. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fileName).Demand ();
  179. }
  180. return fileName;
  181. }
  182. }
  183. public int LineNumber
  184. {
  185. get {
  186. return lineNumber;
  187. }
  188. }
  189. }
  190. class ConfigXmlComment : XmlComment, IConfigXmlNode
  191. {
  192. string fileName;
  193. int lineNumber;
  194. public ConfigXmlComment (ConfigXmlDocument document, string comment)
  195. : base (comment, document)
  196. {
  197. fileName = document.fileName;
  198. lineNumber = document.LineNumber;
  199. }
  200. public string Filename
  201. {
  202. get {
  203. if ((fileName != null) && (fileName.Length > 0) && SecurityManager.SecurityEnabled) {
  204. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fileName).Demand ();
  205. }
  206. return fileName;
  207. }
  208. }
  209. public int LineNumber
  210. {
  211. get {
  212. return lineNumber;
  213. }
  214. }
  215. }
  216. class ConfigXmlElement : XmlElement, IConfigXmlNode
  217. #if CONFIGURATION_DEP
  218. , IConfigErrorInfo
  219. #endif
  220. {
  221. string fileName;
  222. int lineNumber;
  223. public ConfigXmlElement (ConfigXmlDocument document,
  224. string prefix,
  225. string localName,
  226. string namespaceUri)
  227. : base (prefix, localName, namespaceUri, document)
  228. {
  229. fileName = document.fileName;
  230. lineNumber = document.LineNumber;
  231. }
  232. public string Filename
  233. {
  234. get {
  235. if ((fileName != null) && (fileName.Length > 0) && SecurityManager.SecurityEnabled) {
  236. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fileName).Demand ();
  237. }
  238. return fileName;
  239. }
  240. }
  241. public int LineNumber
  242. {
  243. get {
  244. return lineNumber;
  245. }
  246. }
  247. }
  248. class ConfigXmlText : XmlText, IConfigXmlNode
  249. #if CONFIGURATION_DEP
  250. , IConfigErrorInfo
  251. #endif
  252. {
  253. string fileName;
  254. int lineNumber;
  255. public ConfigXmlText (ConfigXmlDocument document, string data)
  256. : base (data, document)
  257. {
  258. fileName = document.fileName;
  259. lineNumber = document.LineNumber;
  260. }
  261. public string Filename
  262. {
  263. get {
  264. if ((fileName != null) && (fileName.Length > 0) && SecurityManager.SecurityEnabled) {
  265. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fileName).Demand ();
  266. }
  267. return fileName;
  268. }
  269. }
  270. public int LineNumber
  271. {
  272. get {
  273. return lineNumber;
  274. }
  275. }
  276. }
  277. }
  278. }
  279. #endif