EntityResolvingXmlReader.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 (Current == entity)
  128. return entity.EOF ? XmlNodeType.EndEntity : entity.NodeType;
  129. else
  130. return source.NodeType;
  131. }
  132. }
  133. internal XmlParserContext ParserContext {
  134. get { return context; }
  135. }
  136. XmlParserContext IHasXmlParserContext.ParserContext {
  137. get { return context; }
  138. }
  139. public override string Prefix {
  140. get { return Current.Prefix; }
  141. }
  142. public override char QuoteChar {
  143. get { return Current.QuoteChar; }
  144. }
  145. public override ReadState ReadState {
  146. get { return entity != null ? ReadState.Interactive : source.ReadState; }
  147. }
  148. public override string Value {
  149. get { return Current.Value; }
  150. }
  151. public override string XmlLang {
  152. get { return Current.XmlLang; }
  153. }
  154. public override XmlSpace XmlSpace {
  155. get { return Current.XmlSpace; }
  156. }
  157. // non-overrides
  158. private void CopyProperties (EntityResolvingXmlReader other)
  159. {
  160. context = other.context;
  161. resolver = other.resolver;
  162. entity_handling = other.entity_handling;
  163. }
  164. // public members
  165. public EntityHandling EntityHandling {
  166. get { return entity_handling; }
  167. set {
  168. if (entity != null)
  169. entity.EntityHandling = value;
  170. entity_handling = value;
  171. }
  172. }
  173. public int LineNumber {
  174. get {
  175. IXmlLineInfo li = Current as IXmlLineInfo;
  176. return li == null ? 0 : li.LineNumber;
  177. }
  178. }
  179. public int LinePosition {
  180. get {
  181. IXmlLineInfo li = Current as IXmlLineInfo;
  182. return li == null ? 0 : li.LinePosition;
  183. }
  184. }
  185. public XmlResolver XmlResolver {
  186. set {
  187. if (entity != null)
  188. entity.XmlResolver = value;
  189. resolver = value;
  190. }
  191. }
  192. #endregion
  193. #region Methods
  194. // overrides
  195. public override void Close ()
  196. {
  197. if (entity != null)
  198. entity.Close ();
  199. source.Close ();
  200. }
  201. public override string GetAttribute (int i)
  202. {
  203. return Current.GetAttribute (i);
  204. }
  205. // MS.NET 1.0 msdn says that this method returns String.Empty
  206. // for absent attribute, but in fact it returns null.
  207. // This description is corrected in MS.NET 1.1 msdn.
  208. public override string GetAttribute (string name)
  209. {
  210. return Current.GetAttribute (name);
  211. }
  212. public override string GetAttribute (string localName, string namespaceURI)
  213. {
  214. return Current.GetAttribute (localName, namespaceURI);
  215. }
  216. #if NET_2_0
  217. public IDictionary<string, string> GetNamespacesInScope (XmlNamespaceScope scope)
  218. {
  219. return ((IXmlNamespaceResolver) Current).GetNamespacesInScope (scope);
  220. }
  221. IDictionary<string, string> IXmlNamespaceResolver.GetNamespacesInScope (XmlNamespaceScope scope)
  222. {
  223. return GetNamespacesInScope (scope);
  224. }
  225. string IXmlNamespaceResolver.LookupPrefix (string ns)
  226. {
  227. return ((IXmlNamespaceResolver) Current).LookupPrefix (ns);
  228. }
  229. #endif
  230. public override string LookupNamespace (string prefix)
  231. {
  232. return Current.LookupNamespace (prefix);
  233. }
  234. public override void MoveToAttribute (int i)
  235. {
  236. if (entity != null && entity_inside_attr) {
  237. entity.Close ();
  238. entity = null;
  239. }
  240. Current.MoveToAttribute (i);
  241. inside_attr = true;
  242. }
  243. public override bool MoveToAttribute (string name)
  244. {
  245. if (entity != null && !entity_inside_attr)
  246. return entity.MoveToAttribute (name);
  247. if (!source.MoveToAttribute (name))
  248. return false;
  249. if (entity != null && entity_inside_attr) {
  250. entity.Close ();
  251. entity = null;
  252. }
  253. inside_attr = true;
  254. return true;
  255. }
  256. public override bool MoveToAttribute (string localName, string namespaceName)
  257. {
  258. if (entity != null && !entity_inside_attr)
  259. return entity.MoveToAttribute (localName, namespaceName);
  260. if (!source.MoveToAttribute (localName, namespaceName))
  261. return false;
  262. if (entity != null && entity_inside_attr) {
  263. entity.Close ();
  264. entity = null;
  265. }
  266. inside_attr = true;
  267. return true;
  268. }
  269. public override bool MoveToElement ()
  270. {
  271. if (entity != null && entity_inside_attr) {
  272. entity.Close ();
  273. entity = null;
  274. }
  275. if (!Current.MoveToElement ())
  276. return false;
  277. inside_attr = false;
  278. return true;
  279. }
  280. public override bool MoveToFirstAttribute ()
  281. {
  282. if (entity != null && !entity_inside_attr)
  283. return entity.MoveToFirstAttribute ();
  284. if (!source.MoveToFirstAttribute ())
  285. return false;
  286. if (entity != null && entity_inside_attr) {
  287. entity.Close ();
  288. entity = null;
  289. }
  290. inside_attr = true;
  291. return true;
  292. }
  293. public override bool MoveToNextAttribute ()
  294. {
  295. if (entity != null && !entity_inside_attr)
  296. return entity.MoveToNextAttribute ();
  297. if (!source.MoveToNextAttribute ())
  298. return false;
  299. if (entity != null && entity_inside_attr) {
  300. entity.Close ();
  301. entity = null;
  302. }
  303. inside_attr = true;
  304. return true;
  305. }
  306. public override bool Read ()
  307. {
  308. if (do_resolve) {
  309. DoResolveEntity ();
  310. do_resolve = false;
  311. }
  312. inside_attr = false;
  313. if (entity != null && (entity_inside_attr || entity.EOF)) {
  314. entity.Close ();
  315. entity = null;
  316. }
  317. if (entity != null) {
  318. if (entity.Read ())
  319. return true;
  320. if (EntityHandling == EntityHandling.ExpandEntities) {
  321. // EndEntity must be skipped
  322. entity.Close ();
  323. entity = null;
  324. return Read ();
  325. }
  326. else
  327. return true; // either success or EndEntity
  328. }
  329. else {
  330. if (!source.Read ())
  331. return false;
  332. if (EntityHandling == EntityHandling.ExpandEntities
  333. && source.NodeType == XmlNodeType.EntityReference) {
  334. ResolveEntity ();
  335. return Read ();
  336. }
  337. return true;
  338. }
  339. }
  340. public override bool ReadAttributeValue ()
  341. {
  342. if (entity != null && entity_inside_attr) {
  343. if (entity.EOF) {
  344. entity.Close ();
  345. entity = null;
  346. }
  347. else {
  348. entity.Read ();
  349. return true; // either success or EndEntity
  350. }
  351. }
  352. return Current.ReadAttributeValue ();
  353. }
  354. public override string ReadString ()
  355. {
  356. return base.ReadString ();
  357. }
  358. public override void ResolveEntity ()
  359. {
  360. #if NET_2_0
  361. DoResolveEntity ();
  362. #else
  363. do_resolve = true;
  364. #endif
  365. }
  366. void DoResolveEntity ()
  367. {
  368. if (entity != null)
  369. entity.ResolveEntity ();
  370. else {
  371. if (source.NodeType != XmlNodeType.EntityReference)
  372. throw new InvalidOperationException ("The current node is not an Entity Reference");
  373. if (ParserContext.Dtd == null)
  374. throw new XmlException (this as IXmlLineInfo, this.BaseURI, String.Format ("Cannot resolve entity without DTD: '{0}'", source.Name));
  375. XmlReader entReader = ParserContext.Dtd.GenerateEntityContentReader (
  376. source.Name, ParserContext);
  377. if (entReader == null)
  378. throw new XmlException (this as IXmlLineInfo, this.BaseURI, String.Format ("Reference to undeclared entity '{0}'.", source.Name));
  379. entity = new EntityResolvingXmlReader (
  380. entReader, inside_attr);
  381. entity.CopyProperties (this);
  382. }
  383. }
  384. public override void Skip ()
  385. {
  386. base.Skip ();
  387. }
  388. public bool HasLineInfo ()
  389. {
  390. IXmlLineInfo li = Current as IXmlLineInfo;
  391. return li == null ? false : li.HasLineInfo ();
  392. }
  393. #endregion
  394. }
  395. }