EntityResolvingXmlReader.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. //
  2. // EntityResolvingXmlReader.cs - XmlReader that handles entity resolution
  3. //
  4. // Author:
  5. // Atsushi Enomoto ([email protected])
  6. //
  7. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if NET_2_0
  29. using System.Collections.Generic;
  30. #endif
  31. using System;
  32. using System.Globalization;
  33. using System.IO;
  34. using System.Security.Permissions;
  35. using System.Text;
  36. using System.Xml.Schema;
  37. using System.Xml;
  38. namespace Mono.Xml
  39. {
  40. [PermissionSet (SecurityAction.InheritanceDemand, Unrestricted = true)]
  41. internal class EntityResolvingXmlReader : XmlReader,
  42. #if NET_2_0
  43. IXmlNamespaceResolver,
  44. #endif
  45. IXmlLineInfo, IHasXmlParserContext
  46. {
  47. EntityResolvingXmlReader entity;
  48. XmlReader source;
  49. XmlParserContext context;
  50. XmlResolver resolver;
  51. EntityHandling entity_handling;
  52. bool entity_inside_attr;
  53. bool inside_attr;
  54. bool do_resolve;
  55. public EntityResolvingXmlReader (XmlReader source, XmlParserContext context)
  56. {
  57. this.source = source;
  58. this.context = context;
  59. }
  60. EntityResolvingXmlReader (XmlReader entityContainer,
  61. bool inside_attr)
  62. {
  63. source = entityContainer;
  64. this.entity_inside_attr = inside_attr;
  65. }
  66. #region Properties
  67. private XmlReader Current {
  68. get { return entity != null && entity.ReadState != ReadState.Initial ? (XmlReader) entity : source; }
  69. }
  70. #if NET_2_0
  71. #else
  72. public override string this [int i] {
  73. get { return GetAttribute (i); }
  74. }
  75. public override string this [string name] {
  76. get { return GetAttribute (name); }
  77. }
  78. public override string this [string localName, string namespaceName] {
  79. get { return GetAttribute (localName, namespaceName); }
  80. }
  81. #endif
  82. public override int AttributeCount {
  83. get { return Current.AttributeCount; }
  84. }
  85. public override string BaseURI {
  86. get { return Current.BaseURI; }
  87. }
  88. public override bool CanResolveEntity {
  89. get { return true; }
  90. }
  91. public override int Depth {
  92. get {
  93. // On EndEntity, depth is the same as that
  94. // of EntityReference.
  95. if (entity != null && entity.ReadState == ReadState.Interactive)
  96. return source.Depth + entity.Depth + 1;
  97. else
  98. return source.Depth;
  99. }
  100. }
  101. public override bool EOF {
  102. get { return source.EOF; }
  103. }
  104. public override bool HasValue {
  105. get { return Current.HasValue; }
  106. }
  107. public override bool IsDefault {
  108. get { return Current.IsDefault; }
  109. }
  110. public override bool IsEmptyElement {
  111. get { return Current.IsEmptyElement; }
  112. }
  113. public override string LocalName {
  114. get { return Current.LocalName; }
  115. }
  116. public override string Name {
  117. get { return Current.Name; }
  118. }
  119. public override string NamespaceURI {
  120. get { return Current.NamespaceURI; }
  121. }
  122. public override XmlNameTable NameTable {
  123. get { return Current.NameTable; }
  124. }
  125. public override XmlNodeType NodeType {
  126. get {
  127. if (entity != null) {
  128. if (entity.ReadState == ReadState.Initial)
  129. return source.NodeType;
  130. return entity.EOF ? XmlNodeType.EndEntity : entity.NodeType;
  131. }
  132. return source.NodeType;
  133. }
  134. }
  135. internal XmlParserContext ParserContext {
  136. get { return context; }
  137. }
  138. XmlParserContext IHasXmlParserContext.ParserContext {
  139. get { return context; }
  140. }
  141. public override string Prefix {
  142. get { return Current.Prefix; }
  143. }
  144. public override char QuoteChar {
  145. get { return Current.QuoteChar; }
  146. }
  147. public override ReadState ReadState {
  148. get { return entity != null ? ReadState.Interactive : source.ReadState; }
  149. }
  150. public override string Value {
  151. get { return Current.Value; }
  152. }
  153. public override string XmlLang {
  154. get { return Current.XmlLang; }
  155. }
  156. public override XmlSpace XmlSpace {
  157. get { return Current.XmlSpace; }
  158. }
  159. // non-overrides
  160. private void CopyProperties (EntityResolvingXmlReader other)
  161. {
  162. context = other.context;
  163. resolver = other.resolver;
  164. entity_handling = other.entity_handling;
  165. }
  166. // public members
  167. public EntityHandling EntityHandling {
  168. get { return entity_handling; }
  169. set {
  170. if (entity != null)
  171. entity.EntityHandling = value;
  172. entity_handling = value;
  173. }
  174. }
  175. public int LineNumber {
  176. get {
  177. IXmlLineInfo li = Current as IXmlLineInfo;
  178. return li == null ? 0 : li.LineNumber;
  179. }
  180. }
  181. public int LinePosition {
  182. get {
  183. IXmlLineInfo li = Current as IXmlLineInfo;
  184. return li == null ? 0 : li.LinePosition;
  185. }
  186. }
  187. public XmlResolver XmlResolver {
  188. set {
  189. if (entity != null)
  190. entity.XmlResolver = value;
  191. resolver = value;
  192. }
  193. }
  194. #endregion
  195. #region Methods
  196. // overrides
  197. public override void Close ()
  198. {
  199. if (entity != null)
  200. entity.Close ();
  201. source.Close ();
  202. }
  203. public override string GetAttribute (int i)
  204. {
  205. return Current.GetAttribute (i);
  206. }
  207. // MS.NET 1.0 msdn says that this method returns String.Empty
  208. // for absent attribute, but in fact it returns null.
  209. // This description is corrected in MS.NET 1.1 msdn.
  210. public override string GetAttribute (string name)
  211. {
  212. return Current.GetAttribute (name);
  213. }
  214. public override string GetAttribute (string localName, string namespaceURI)
  215. {
  216. return Current.GetAttribute (localName, namespaceURI);
  217. }
  218. #if NET_2_0
  219. public IDictionary<string, string> GetNamespacesInScope (XmlNamespaceScope scope)
  220. {
  221. return ((IXmlNamespaceResolver) Current).GetNamespacesInScope (scope);
  222. }
  223. IDictionary<string, string> IXmlNamespaceResolver.GetNamespacesInScope (XmlNamespaceScope scope)
  224. {
  225. return GetNamespacesInScope (scope);
  226. }
  227. string IXmlNamespaceResolver.LookupPrefix (string ns)
  228. {
  229. return ((IXmlNamespaceResolver) Current).LookupPrefix (ns);
  230. }
  231. #endif
  232. public override string LookupNamespace (string prefix)
  233. {
  234. return Current.LookupNamespace (prefix);
  235. }
  236. public override void MoveToAttribute (int i)
  237. {
  238. if (entity != null && entity_inside_attr) {
  239. entity.Close ();
  240. entity = null;
  241. }
  242. Current.MoveToAttribute (i);
  243. inside_attr = true;
  244. }
  245. public override bool MoveToAttribute (string name)
  246. {
  247. if (entity != null && !entity_inside_attr)
  248. return entity.MoveToAttribute (name);
  249. if (!source.MoveToAttribute (name))
  250. return false;
  251. if (entity != null && entity_inside_attr) {
  252. entity.Close ();
  253. entity = null;
  254. }
  255. inside_attr = true;
  256. return true;
  257. }
  258. public override bool MoveToAttribute (string localName, string namespaceName)
  259. {
  260. if (entity != null && !entity_inside_attr)
  261. return entity.MoveToAttribute (localName, namespaceName);
  262. if (!source.MoveToAttribute (localName, namespaceName))
  263. return false;
  264. if (entity != null && entity_inside_attr) {
  265. entity.Close ();
  266. entity = null;
  267. }
  268. inside_attr = true;
  269. return true;
  270. }
  271. public override bool MoveToElement ()
  272. {
  273. if (entity != null && entity_inside_attr) {
  274. entity.Close ();
  275. entity = null;
  276. }
  277. if (!Current.MoveToElement ())
  278. return false;
  279. inside_attr = false;
  280. return true;
  281. }
  282. public override bool MoveToFirstAttribute ()
  283. {
  284. if (entity != null && !entity_inside_attr)
  285. return entity.MoveToFirstAttribute ();
  286. if (!source.MoveToFirstAttribute ())
  287. return false;
  288. if (entity != null && entity_inside_attr) {
  289. entity.Close ();
  290. entity = null;
  291. }
  292. inside_attr = true;
  293. return true;
  294. }
  295. public override bool MoveToNextAttribute ()
  296. {
  297. if (entity != null && !entity_inside_attr)
  298. return entity.MoveToNextAttribute ();
  299. if (!source.MoveToNextAttribute ())
  300. return false;
  301. if (entity != null && entity_inside_attr) {
  302. entity.Close ();
  303. entity = null;
  304. }
  305. inside_attr = true;
  306. return true;
  307. }
  308. public override bool Read ()
  309. {
  310. if (do_resolve) {
  311. DoResolveEntity ();
  312. do_resolve = false;
  313. }
  314. inside_attr = false;
  315. if (entity != null && (entity_inside_attr || entity.EOF)) {
  316. entity.Close ();
  317. entity = null;
  318. }
  319. if (entity != null) {
  320. if (entity.Read ())
  321. return true;
  322. if (EntityHandling == EntityHandling.ExpandEntities) {
  323. // EndEntity must be skipped
  324. entity.Close ();
  325. entity = null;
  326. return Read ();
  327. }
  328. else
  329. return true; // either success or EndEntity
  330. }
  331. else {
  332. if (!source.Read ())
  333. return false;
  334. if (EntityHandling == EntityHandling.ExpandEntities
  335. && source.NodeType == XmlNodeType.EntityReference) {
  336. ResolveEntity ();
  337. return Read ();
  338. }
  339. return true;
  340. }
  341. }
  342. public override bool ReadAttributeValue ()
  343. {
  344. if (entity != null && entity_inside_attr) {
  345. if (entity.EOF) {
  346. entity.Close ();
  347. entity = null;
  348. }
  349. else {
  350. entity.Read ();
  351. return true; // either success or EndEntity
  352. }
  353. }
  354. return Current.ReadAttributeValue ();
  355. }
  356. public override string ReadString ()
  357. {
  358. return base.ReadString ();
  359. }
  360. public override void ResolveEntity ()
  361. {
  362. #if NET_2_0
  363. DoResolveEntity ();
  364. #else
  365. do_resolve = true;
  366. #endif
  367. }
  368. void DoResolveEntity ()
  369. {
  370. if (entity != null)
  371. entity.ResolveEntity ();
  372. else {
  373. if (source.NodeType != XmlNodeType.EntityReference)
  374. throw new InvalidOperationException ("The current node is not an Entity Reference");
  375. if (ParserContext.Dtd == null)
  376. throw new XmlException (this as IXmlLineInfo, this.BaseURI, String.Format ("Cannot resolve entity without DTD: '{0}'", source.Name));
  377. XmlReader entReader = ParserContext.Dtd.GenerateEntityContentReader (
  378. source.Name, ParserContext);
  379. if (entReader == null)
  380. throw new XmlException (this as IXmlLineInfo, this.BaseURI, String.Format ("Reference to undeclared entity '{0}'.", source.Name));
  381. entity = new EntityResolvingXmlReader (
  382. entReader, inside_attr);
  383. entity.CopyProperties (this);
  384. }
  385. }
  386. public override void Skip ()
  387. {
  388. base.Skip ();
  389. }
  390. public bool HasLineInfo ()
  391. {
  392. IXmlLineInfo li = Current as IXmlLineInfo;
  393. return li == null ? false : li.HasLineInfo ();
  394. }
  395. #endregion
  396. }
  397. }