ConfigXmlDocument.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. using System.IO;
  30. using System.Security;
  31. using System.Security.Permissions;
  32. #if (XML_DEP)
  33. using System.Xml;
  34. namespace System.Configuration
  35. {
  36. [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
  37. public sealed class ConfigXmlDocument : XmlDocument, IConfigXmlNode
  38. #if NET_2_0 && CONFIGURATION_DEP
  39. , System.Configuration.Internal.IConfigErrorInfo
  40. #endif
  41. {
  42. XmlTextReader reader;
  43. string fileName;
  44. int lineNumber;
  45. public override XmlAttribute CreateAttribute (string prefix,
  46. string localName,
  47. string namespaceUri)
  48. {
  49. return new ConfigXmlAttribute (this, prefix, localName, namespaceUri);
  50. }
  51. public override XmlCDataSection CreateCDataSection (string data)
  52. {
  53. return new ConfigXmlCDataSection (this, data);
  54. }
  55. public override XmlComment CreateComment (string comment)
  56. {
  57. return new ConfigXmlComment (this, comment);
  58. }
  59. public override XmlElement CreateElement (string prefix, string localName, string namespaceUri)
  60. {
  61. return new ConfigXmlElement (this, prefix, localName, namespaceUri);
  62. }
  63. public override XmlSignificantWhitespace CreateSignificantWhitespace (string data)
  64. {
  65. return base.CreateSignificantWhitespace (data);
  66. }
  67. public override XmlText CreateTextNode (string text)
  68. {
  69. return new ConfigXmlText (this, text);
  70. }
  71. public override XmlWhitespace CreateWhitespace (string data)
  72. {
  73. return base.CreateWhitespace (data);
  74. }
  75. public override void Load (string filename)
  76. {
  77. XmlTextReader rd = new XmlTextReader (filename);
  78. rd.MoveToContent ();
  79. LoadSingleElement (filename, rd);
  80. }
  81. public void LoadSingleElement (string filename, XmlTextReader sourceReader)
  82. {
  83. fileName = filename;
  84. lineNumber = sourceReader.LineNumber;
  85. string xml = sourceReader.ReadOuterXml();
  86. reader = new XmlTextReader (new StringReader (xml), sourceReader.NameTable);
  87. Load (reader);
  88. reader.Close ();
  89. }
  90. public string Filename
  91. {
  92. get {
  93. if ((fileName != null) && (fileName.Length > 0) && SecurityManager.SecurityEnabled) {
  94. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fileName).Demand ();
  95. }
  96. return fileName;
  97. }
  98. }
  99. public int LineNumber
  100. {
  101. get {
  102. return lineNumber;
  103. }
  104. }
  105. #if NET_2_0 && CONFIGURATION_DEP
  106. string System.Configuration.Internal.IConfigErrorInfo.Filename {
  107. get { return Filename; }
  108. }
  109. int System.Configuration.Internal.IConfigErrorInfo.LineNumber {
  110. get { return LineNumber; }
  111. }
  112. #endif
  113. //
  114. // Wrappers for Xml* that just provide file name and line number addition
  115. //
  116. class ConfigXmlAttribute : XmlAttribute, IConfigXmlNode
  117. {
  118. string fileName;
  119. int lineNumber;
  120. public ConfigXmlAttribute (ConfigXmlDocument document,
  121. string prefix,
  122. string localName,
  123. string namespaceUri)
  124. : base (prefix, localName, namespaceUri, document)
  125. {
  126. fileName = document.fileName;
  127. lineNumber = document.LineNumber;
  128. }
  129. public string Filename
  130. {
  131. get {
  132. if ((fileName != null) && (fileName.Length > 0) && SecurityManager.SecurityEnabled) {
  133. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fileName).Demand ();
  134. }
  135. return fileName;
  136. }
  137. }
  138. public int LineNumber
  139. {
  140. get {
  141. return lineNumber;
  142. }
  143. }
  144. }
  145. class ConfigXmlCDataSection : XmlCDataSection, IConfigXmlNode
  146. {
  147. string fileName;
  148. int lineNumber;
  149. public ConfigXmlCDataSection (ConfigXmlDocument document, string data)
  150. : base (data, document)
  151. {
  152. fileName = document.fileName;
  153. lineNumber = document.LineNumber;
  154. }
  155. public string Filename
  156. {
  157. get {
  158. if ((fileName != null) && (fileName.Length > 0) && SecurityManager.SecurityEnabled) {
  159. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fileName).Demand ();
  160. }
  161. return fileName;
  162. }
  163. }
  164. public int LineNumber
  165. {
  166. get {
  167. return lineNumber;
  168. }
  169. }
  170. }
  171. class ConfigXmlComment : XmlComment, IConfigXmlNode
  172. {
  173. string fileName;
  174. int lineNumber;
  175. public ConfigXmlComment (ConfigXmlDocument document, string comment)
  176. : base (comment, document)
  177. {
  178. fileName = document.fileName;
  179. lineNumber = document.LineNumber;
  180. }
  181. public string Filename
  182. {
  183. get {
  184. if ((fileName != null) && (fileName.Length > 0) && SecurityManager.SecurityEnabled) {
  185. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fileName).Demand ();
  186. }
  187. return fileName;
  188. }
  189. }
  190. public int LineNumber
  191. {
  192. get {
  193. return lineNumber;
  194. }
  195. }
  196. }
  197. class ConfigXmlElement : XmlElement, IConfigXmlNode
  198. {
  199. string fileName;
  200. int lineNumber;
  201. public ConfigXmlElement (ConfigXmlDocument document,
  202. string prefix,
  203. string localName,
  204. string namespaceUri)
  205. : base (prefix, localName, namespaceUri, document)
  206. {
  207. fileName = document.fileName;
  208. lineNumber = document.LineNumber;
  209. }
  210. public string Filename
  211. {
  212. get {
  213. if ((fileName != null) && (fileName.Length > 0) && SecurityManager.SecurityEnabled) {
  214. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fileName).Demand ();
  215. }
  216. return fileName;
  217. }
  218. }
  219. public int LineNumber
  220. {
  221. get {
  222. return lineNumber;
  223. }
  224. }
  225. }
  226. class ConfigXmlText : XmlText, IConfigXmlNode
  227. {
  228. string fileName;
  229. int lineNumber;
  230. public ConfigXmlText (ConfigXmlDocument document, string data)
  231. : base (data, document)
  232. {
  233. fileName = document.fileName;
  234. lineNumber = document.LineNumber;
  235. }
  236. public string Filename
  237. {
  238. get {
  239. if ((fileName != null) && (fileName.Length > 0) && SecurityManager.SecurityEnabled) {
  240. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fileName).Demand ();
  241. }
  242. return fileName;
  243. }
  244. }
  245. public int LineNumber
  246. {
  247. get {
  248. return lineNumber;
  249. }
  250. }
  251. }
  252. }
  253. }
  254. #endif