ConfigXmlDocument.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 NET_2_0 && 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 NET_2_0 && 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. using (XmlTextReader rd = new XmlTextReader (filename)) {
  81. rd.MoveToContent ();
  82. LoadSingleElement (filename, rd);
  83. }
  84. }
  85. public void LoadSingleElement (string filename, XmlTextReader sourceReader)
  86. {
  87. fileName = filename;
  88. lineNumber = sourceReader.LineNumber;
  89. string xml = sourceReader.ReadOuterXml();
  90. reader = new XmlTextReader (new StringReader (xml), sourceReader.NameTable);
  91. Load (reader);
  92. reader.Close ();
  93. }
  94. public string Filename
  95. {
  96. get {
  97. if ((fileName != null) && (fileName.Length > 0) && SecurityManager.SecurityEnabled) {
  98. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fileName).Demand ();
  99. }
  100. return fileName;
  101. }
  102. }
  103. public int LineNumber
  104. {
  105. get {
  106. return lineNumber;
  107. }
  108. }
  109. #if NET_2_0 && CONFIGURATION_DEP
  110. string System.Configuration.Internal.IConfigErrorInfo.Filename {
  111. get { return Filename; }
  112. }
  113. int System.Configuration.Internal.IConfigErrorInfo.LineNumber {
  114. get { return LineNumber; }
  115. }
  116. #endif
  117. string IConfigXmlNode.Filename {
  118. get { return Filename; }
  119. }
  120. int IConfigXmlNode.LineNumber {
  121. get { return LineNumber; }
  122. }
  123. //
  124. // Wrappers for Xml* that just provide file name and line number addition
  125. //
  126. class ConfigXmlAttribute : XmlAttribute, IConfigXmlNode
  127. #if NET_2_0 && CONFIGURATION_DEP
  128. , IConfigErrorInfo
  129. #endif
  130. {
  131. string fileName;
  132. int lineNumber;
  133. public ConfigXmlAttribute (ConfigXmlDocument document,
  134. string prefix,
  135. string localName,
  136. string namespaceUri)
  137. : base (prefix, localName, namespaceUri, document)
  138. {
  139. fileName = document.fileName;
  140. lineNumber = document.LineNumber;
  141. }
  142. public string Filename
  143. {
  144. get {
  145. if ((fileName != null) && (fileName.Length > 0) && SecurityManager.SecurityEnabled) {
  146. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fileName).Demand ();
  147. }
  148. return fileName;
  149. }
  150. }
  151. public int LineNumber
  152. {
  153. get {
  154. return lineNumber;
  155. }
  156. }
  157. }
  158. class ConfigXmlCDataSection : XmlCDataSection, IConfigXmlNode
  159. #if NET_2_0 && CONFIGURATION_DEP
  160. , IConfigErrorInfo
  161. #endif
  162. {
  163. string fileName;
  164. int lineNumber;
  165. public ConfigXmlCDataSection (ConfigXmlDocument document, string data)
  166. : base (data, document)
  167. {
  168. fileName = document.fileName;
  169. lineNumber = document.LineNumber;
  170. }
  171. public string Filename
  172. {
  173. get {
  174. if ((fileName != null) && (fileName.Length > 0) && SecurityManager.SecurityEnabled) {
  175. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fileName).Demand ();
  176. }
  177. return fileName;
  178. }
  179. }
  180. public int LineNumber
  181. {
  182. get {
  183. return lineNumber;
  184. }
  185. }
  186. }
  187. class ConfigXmlComment : XmlComment, IConfigXmlNode
  188. {
  189. string fileName;
  190. int lineNumber;
  191. public ConfigXmlComment (ConfigXmlDocument document, string comment)
  192. : base (comment, document)
  193. {
  194. fileName = document.fileName;
  195. lineNumber = document.LineNumber;
  196. }
  197. public string Filename
  198. {
  199. get {
  200. if ((fileName != null) && (fileName.Length > 0) && SecurityManager.SecurityEnabled) {
  201. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fileName).Demand ();
  202. }
  203. return fileName;
  204. }
  205. }
  206. public int LineNumber
  207. {
  208. get {
  209. return lineNumber;
  210. }
  211. }
  212. }
  213. class ConfigXmlElement : XmlElement, IConfigXmlNode
  214. #if NET_2_0 && CONFIGURATION_DEP
  215. , IConfigErrorInfo
  216. #endif
  217. {
  218. string fileName;
  219. int lineNumber;
  220. public ConfigXmlElement (ConfigXmlDocument document,
  221. string prefix,
  222. string localName,
  223. string namespaceUri)
  224. : base (prefix, localName, namespaceUri, document)
  225. {
  226. fileName = document.fileName;
  227. lineNumber = document.LineNumber;
  228. }
  229. public string Filename
  230. {
  231. get {
  232. if ((fileName != null) && (fileName.Length > 0) && SecurityManager.SecurityEnabled) {
  233. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fileName).Demand ();
  234. }
  235. return fileName;
  236. }
  237. }
  238. public int LineNumber
  239. {
  240. get {
  241. return lineNumber;
  242. }
  243. }
  244. }
  245. class ConfigXmlText : XmlText, IConfigXmlNode
  246. #if NET_2_0 && CONFIGURATION_DEP
  247. , IConfigErrorInfo
  248. #endif
  249. {
  250. string fileName;
  251. int lineNumber;
  252. public ConfigXmlText (ConfigXmlDocument document, string data)
  253. : base (data, document)
  254. {
  255. fileName = document.fileName;
  256. lineNumber = document.LineNumber;
  257. }
  258. public string Filename
  259. {
  260. get {
  261. if ((fileName != null) && (fileName.Length > 0) && SecurityManager.SecurityEnabled) {
  262. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fileName).Demand ();
  263. }
  264. return fileName;
  265. }
  266. }
  267. public int LineNumber
  268. {
  269. get {
  270. return lineNumber;
  271. }
  272. }
  273. }
  274. }
  275. }
  276. #endif