EntityResolvingXmlReader.cs 11 KB

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