EntityResolvingXmlReader.cs 11 KB

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