XmlTextReader2.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. //
  2. // System.Xml.XmlTextReader2.cs - XmlTextReader for .NET 2.0
  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 XmlTextReaderImpl = Mono.Xml2.XmlTextReader;
  30. using System.Collections.Generic;
  31. using System.Globalization;
  32. using System.IO;
  33. using System.Security.Permissions;
  34. using System.Text;
  35. using System.Xml.Schema;
  36. using Mono.Xml;
  37. namespace System.Xml
  38. {
  39. [PermissionSet (SecurityAction.InheritanceDemand, Unrestricted = true)]
  40. public class XmlTextReader : XmlReader,
  41. IXmlLineInfo, IXmlNamespaceResolver, IHasXmlParserContext
  42. {
  43. XmlTextReader entity;
  44. XmlTextReaderImpl source;
  45. bool entityInsideAttribute;
  46. bool insideAttribute;
  47. string cachedAttributeValue;
  48. bool attributeValueConsumed;
  49. protected XmlTextReader ()
  50. {
  51. }
  52. public XmlTextReader (Stream input)
  53. : this (new XmlStreamReader (input))
  54. {
  55. }
  56. public XmlTextReader (string url)
  57. : this(url, new NameTable ())
  58. {
  59. }
  60. public XmlTextReader (TextReader input)
  61. : this (input, new NameTable ())
  62. {
  63. }
  64. protected XmlTextReader (XmlNameTable nt)
  65. : this (String.Empty, XmlNodeType.Element, null)
  66. {
  67. }
  68. public XmlTextReader (Stream input, XmlNameTable nt)
  69. : this(new XmlStreamReader (input), nt)
  70. {
  71. }
  72. public XmlTextReader (string url, Stream input)
  73. : this (url, new XmlStreamReader (input))
  74. {
  75. }
  76. public XmlTextReader (string url, TextReader input)
  77. : this (url, input, new NameTable ())
  78. {
  79. }
  80. public XmlTextReader (string url, XmlNameTable nt)
  81. {
  82. source = new XmlTextReaderImpl (url, nt);
  83. }
  84. public XmlTextReader (TextReader input, XmlNameTable nt)
  85. : this (String.Empty, input, nt)
  86. {
  87. }
  88. public XmlTextReader (Stream xmlFragment, XmlNodeType fragType, XmlParserContext context)
  89. {
  90. source = new XmlTextReaderImpl (xmlFragment, fragType, context);
  91. }
  92. public XmlTextReader (string url, Stream input, XmlNameTable nt)
  93. : this (url, new XmlStreamReader (input), nt)
  94. {
  95. }
  96. public XmlTextReader (string url, TextReader input, XmlNameTable nt)
  97. {
  98. source = new XmlTextReaderImpl (url, input, nt);
  99. }
  100. public XmlTextReader (string xmlFragment, XmlNodeType fragType, XmlParserContext context)
  101. {
  102. source = new XmlTextReaderImpl (xmlFragment, fragType, context);
  103. }
  104. internal XmlTextReader (string baseURI, TextReader xmlFragment, XmlNodeType fragType)
  105. {
  106. source = new XmlTextReaderImpl (baseURI, xmlFragment, fragType);
  107. }
  108. internal XmlTextReader (string baseURI, TextReader xmlFragment, XmlNodeType fragType, XmlParserContext context)
  109. {
  110. source = new XmlTextReaderImpl (baseURI, xmlFragment, fragType, context);
  111. }
  112. internal XmlTextReader (bool dummy, string url, XmlNodeType fragType, XmlParserContext context)
  113. {
  114. source = new XmlTextReaderImpl (dummy, url, fragType, context);
  115. }
  116. private XmlTextReader (XmlTextReaderImpl entityContainer, bool insideAttribute)
  117. {
  118. source = entityContainer;
  119. this.entityInsideAttribute = insideAttribute;
  120. }
  121. #region Properties
  122. private XmlReader Current {
  123. get { return entity != null && entity.ReadState != ReadState.Initial ? (XmlReader) entity : source; }
  124. }
  125. public override int AttributeCount {
  126. get { return Current.AttributeCount; }
  127. }
  128. public override string BaseURI {
  129. get { return Current.BaseURI; }
  130. }
  131. public override bool CanReadBinaryContent {
  132. get { return true; }
  133. }
  134. public override bool CanReadValueChunk {
  135. get { return true; }
  136. }
  137. public override bool CanResolveEntity {
  138. get { return true; }
  139. }
  140. public override int Depth {
  141. get {
  142. // On EndEntity, depth is the same as that
  143. // of EntityReference.
  144. if (entity != null && entity.ReadState == ReadState.Interactive)
  145. return source.Depth + entity.Depth + 1;
  146. else
  147. return source.Depth;
  148. }
  149. }
  150. public override bool EOF {
  151. get { return source.EOF; }
  152. }
  153. public override bool HasValue {
  154. get { return Current.HasValue; }
  155. }
  156. public override bool IsDefault {
  157. get { return Current.IsDefault; }
  158. }
  159. public override bool IsEmptyElement {
  160. get { return Current.IsEmptyElement; }
  161. }
  162. public override string LocalName {
  163. get { return Current.LocalName; }
  164. }
  165. public override string Name {
  166. get { return Current.Name; }
  167. }
  168. public override string NamespaceURI {
  169. get { return Current.NamespaceURI; }
  170. }
  171. public override XmlNameTable NameTable {
  172. get { return Current.NameTable; }
  173. }
  174. public override XmlNodeType NodeType {
  175. get {
  176. if (Current == entity)
  177. return entity.EOF ? XmlNodeType.EndEntity : entity.NodeType;
  178. else
  179. return source.NodeType;
  180. }
  181. }
  182. internal XmlParserContext ParserContext {
  183. get { return ((IHasXmlParserContext) Current).ParserContext; }
  184. }
  185. XmlParserContext IHasXmlParserContext.ParserContext {
  186. get { return this.ParserContext; }
  187. }
  188. public override string Prefix {
  189. get { return Current.Prefix; }
  190. }
  191. public override char QuoteChar {
  192. get { return Current.QuoteChar; }
  193. }
  194. public override ReadState ReadState {
  195. get { return entity != null ? ReadState.Interactive : source.ReadState; }
  196. }
  197. public override XmlReaderSettings Settings {
  198. get { return base.Settings; }
  199. }
  200. public override string Value {
  201. get { return Current.Value; }
  202. }
  203. public override string XmlLang {
  204. get { return Current.XmlLang; }
  205. }
  206. public override XmlSpace XmlSpace {
  207. get { return Current.XmlSpace; }
  208. }
  209. // non-overrides
  210. internal bool CharacterChecking {
  211. get {
  212. if (entity != null)
  213. return entity.CharacterChecking;
  214. else
  215. return source.CharacterChecking;
  216. }
  217. set {
  218. if (entity != null)
  219. entity.CharacterChecking = value;
  220. source.CharacterChecking = value;
  221. }
  222. }
  223. internal bool CloseInput {
  224. get {
  225. if (entity != null)
  226. return entity.CloseInput;
  227. else
  228. return source.CloseInput;
  229. }
  230. set {
  231. if (entity != null)
  232. entity.CloseInput = value;
  233. source.CloseInput = value;
  234. }
  235. }
  236. internal ConformanceLevel Conformance {
  237. set {
  238. if (entity != null)
  239. entity.Conformance = value;
  240. source.Conformance = value;
  241. }
  242. }
  243. internal XmlResolver Resolver {
  244. get { return source.Resolver; }
  245. }
  246. private void CopyProperties (XmlTextReader other)
  247. {
  248. CharacterChecking = other.CharacterChecking;
  249. CloseInput = other.CloseInput;
  250. if (other.Settings != null)
  251. Conformance = other.Settings.ConformanceLevel;
  252. XmlResolver = other.Resolver;
  253. }
  254. // public members
  255. public Encoding Encoding {
  256. get {
  257. if (entity != null)
  258. return entity.Encoding;
  259. else
  260. return source.Encoding;
  261. }
  262. }
  263. public EntityHandling EntityHandling {
  264. get { return source.EntityHandling; }
  265. set {
  266. if (entity != null)
  267. entity.EntityHandling = value;
  268. source.EntityHandling = value;
  269. }
  270. }
  271. public int LineNumber {
  272. get {
  273. if (entity != null)
  274. return entity.LineNumber;
  275. else
  276. return source.LineNumber;
  277. }
  278. }
  279. public int LinePosition {
  280. get {
  281. if (entity != null)
  282. return entity.LinePosition;
  283. else
  284. return source.LinePosition;
  285. }
  286. }
  287. public bool Namespaces {
  288. get { return source.Namespaces; }
  289. set {
  290. if (entity != null)
  291. entity.Namespaces = value;
  292. source.Namespaces = value;
  293. }
  294. }
  295. public bool Normalization {
  296. get { return source.Normalization; }
  297. set {
  298. if (entity != null)
  299. entity.Normalization = value;
  300. source.Normalization = value;
  301. }
  302. }
  303. public bool ProhibitDtd {
  304. get { return source.ProhibitDtd; }
  305. set {
  306. if (entity != null)
  307. entity.ProhibitDtd = value;
  308. source.ProhibitDtd = value;
  309. }
  310. }
  311. public WhitespaceHandling WhitespaceHandling {
  312. get { return source.WhitespaceHandling; }
  313. set {
  314. if (entity != null)
  315. entity.WhitespaceHandling = value;
  316. source.WhitespaceHandling = value;
  317. }
  318. }
  319. public XmlResolver XmlResolver {
  320. set {
  321. if (entity != null)
  322. entity.XmlResolver = value;
  323. source.XmlResolver = value;
  324. }
  325. }
  326. #endregion
  327. #region Methods
  328. internal void AdjustLineInfoOffset (int lineNumberOffset, int linePositionOffset)
  329. {
  330. if (entity != null)
  331. entity.AdjustLineInfoOffset (lineNumberOffset, linePositionOffset);
  332. source.AdjustLineInfoOffset (lineNumberOffset, linePositionOffset);
  333. }
  334. internal void SetNameTable (XmlNameTable nameTable)
  335. {
  336. if (entity != null)
  337. entity.SetNameTable (nameTable);
  338. source.SetNameTable (nameTable);
  339. }
  340. // overrides
  341. public override void Close ()
  342. {
  343. if (entity != null)
  344. entity.Close ();
  345. source.Close ();
  346. }
  347. public override string GetAttribute (int i)
  348. {
  349. return Current.GetAttribute (i);
  350. }
  351. // MS.NET 1.0 msdn says that this method returns String.Empty
  352. // for absent attribute, but in fact it returns null.
  353. // This description is corrected in MS.NET 1.1 msdn.
  354. public override string GetAttribute (string name)
  355. {
  356. return Current.GetAttribute (name);
  357. }
  358. public override string GetAttribute (string localName, string namespaceURI)
  359. {
  360. return Current.GetAttribute (localName, namespaceURI);
  361. }
  362. public IDictionary<string, string> GetNamespacesInScope (XmlNamespaceScope scope)
  363. {
  364. return ((IXmlNamespaceResolver) Current).GetNamespacesInScope (scope);
  365. }
  366. IDictionary<string, string> IXmlNamespaceResolver.GetNamespacesInScope (XmlNamespaceScope scope)
  367. {
  368. return GetNamespacesInScope (scope);
  369. }
  370. public override string LookupNamespace (string prefix)
  371. {
  372. return Current.LookupNamespace (prefix);
  373. }
  374. string IXmlNamespaceResolver.LookupPrefix (string ns)
  375. {
  376. return ((IXmlNamespaceResolver) Current).LookupPrefix (ns);
  377. }
  378. public override void MoveToAttribute (int i)
  379. {
  380. if (entity != null && entityInsideAttribute) {
  381. entity.Close ();
  382. entity = null;
  383. }
  384. Current.MoveToAttribute (i);
  385. insideAttribute = true;
  386. }
  387. public override bool MoveToAttribute (string name)
  388. {
  389. if (entity != null && !entityInsideAttribute)
  390. return entity.MoveToAttribute (name);
  391. if (!source.MoveToAttribute (name))
  392. return false;
  393. if (entity != null && entityInsideAttribute) {
  394. entity.Close ();
  395. entity = null;
  396. }
  397. insideAttribute = true;
  398. return true;
  399. }
  400. public override bool MoveToAttribute (string localName, string namespaceName)
  401. {
  402. if (entity != null && !entityInsideAttribute)
  403. return entity.MoveToAttribute (localName, namespaceName);
  404. if (!source.MoveToAttribute (localName, namespaceName))
  405. return false;
  406. if (entity != null && entityInsideAttribute) {
  407. entity.Close ();
  408. entity = null;
  409. }
  410. insideAttribute = true;
  411. return true;
  412. }
  413. public override bool MoveToElement ()
  414. {
  415. if (entity != null && entityInsideAttribute) {
  416. entity.Close ();
  417. entity = null;
  418. }
  419. if (!Current.MoveToElement ())
  420. return false;
  421. insideAttribute = false;
  422. return true;
  423. }
  424. public override bool MoveToFirstAttribute ()
  425. {
  426. if (entity != null && !entityInsideAttribute)
  427. return entity.MoveToFirstAttribute ();
  428. if (!source.MoveToFirstAttribute ())
  429. return false;
  430. if (entity != null && entityInsideAttribute) {
  431. entity.Close ();
  432. entity = null;
  433. }
  434. insideAttribute = true;
  435. return true;
  436. }
  437. public override bool MoveToNextAttribute ()
  438. {
  439. if (entity != null && !entityInsideAttribute)
  440. return entity.MoveToNextAttribute ();
  441. if (!source.MoveToNextAttribute ())
  442. return false;
  443. if (entity != null && entityInsideAttribute) {
  444. entity.Close ();
  445. entity = null;
  446. }
  447. insideAttribute = true;
  448. return true;
  449. }
  450. public override bool Read ()
  451. {
  452. insideAttribute = false;
  453. if (entity != null && (entityInsideAttribute || entity.EOF)) {
  454. entity.Close ();
  455. entity = null;
  456. }
  457. if (entity != null) {
  458. if (entity.Read ())
  459. return true;
  460. if (EntityHandling == EntityHandling.ExpandEntities) {
  461. // EndEntity must be skipped
  462. entity.Close ();
  463. entity = null;
  464. return Read ();
  465. }
  466. else
  467. return true; // either success or EndEntity
  468. }
  469. else {
  470. if (!source.Read ())
  471. return false;
  472. if (EntityHandling == EntityHandling.ExpandEntities
  473. && source.NodeType == XmlNodeType.EntityReference) {
  474. ResolveEntity ();
  475. return Read ();
  476. }
  477. return true;
  478. }
  479. }
  480. public override bool ReadAttributeValue ()
  481. {
  482. if (entity != null && entityInsideAttribute) {
  483. if (entity.EOF) {
  484. entity.Close ();
  485. entity = null;
  486. }
  487. else {
  488. entity.Read ();
  489. return true; // either success or EndEntity
  490. }
  491. }
  492. return Current.ReadAttributeValue ();
  493. }
  494. public override string ReadString ()
  495. {
  496. return base.ReadString ();
  497. }
  498. public void ResetState ()
  499. {
  500. if (entity != null)
  501. entity.ResetState ();
  502. source.ResetState ();
  503. }
  504. public override void ResolveEntity ()
  505. {
  506. if (entity != null)
  507. entity.ResolveEntity ();
  508. else {
  509. if (source.NodeType != XmlNodeType.EntityReference)
  510. throw new InvalidOperationException ("The current node is not an Entity Reference");
  511. XmlTextReaderImpl entReader =
  512. ParserContext.Dtd.GenerateEntityContentReader (source.Name, ParserContext);
  513. if (entReader == null)
  514. throw new XmlException (this as IXmlLineInfo, this.BaseURI, String.Format ("Reference to undeclared entity '{0}'.", source.Name));
  515. entity = new XmlTextReader (
  516. entReader, insideAttribute);
  517. entity.CopyProperties (this);
  518. }
  519. }
  520. public override void Skip ()
  521. {
  522. base.Skip ();
  523. }
  524. [MonoTODO ("Check how expanded entity is handled here.")]
  525. public TextReader GetRemainder ()
  526. {
  527. if (entity != null) {
  528. entity.Close ();
  529. entity = null;
  530. }
  531. return source.GetRemainder ();
  532. }
  533. public bool HasLineInfo ()
  534. {
  535. return true;
  536. }
  537. [MonoTODO ("Check how expanded entity is handled here.")]
  538. public int ReadBase64 (byte [] buffer, int offset, int length)
  539. {
  540. if (entity != null)
  541. return entity.ReadBase64 (buffer, offset, length);
  542. else
  543. return source.ReadBase64 (buffer, offset, length);
  544. }
  545. [MonoTODO ("Check how expanded entity is handled here.")]
  546. public int ReadBinHex (byte [] buffer, int offset, int length)
  547. {
  548. if (entity != null)
  549. return entity.ReadBinHex (buffer, offset, length);
  550. else
  551. return source.ReadBinHex (buffer, offset, length);
  552. }
  553. [MonoTODO ("Check how expanded entity is handled here.")]
  554. public int ReadChars (char [] buffer, int offset, int length)
  555. {
  556. if (entity != null)
  557. return entity.ReadChars (buffer, offset, length);
  558. else
  559. return source.ReadChars (buffer, offset, length);
  560. }
  561. [MonoTODO ("Check how expanded entity is handled here.")]
  562. public override int ReadContentAsBase64 (byte [] buffer, int offset, int length)
  563. {
  564. if (entity != null)
  565. return entity.ReadContentAsBase64 (buffer, offset, length);
  566. else
  567. return source.ReadContentAsBase64 (buffer, offset, length);
  568. }
  569. [MonoTODO ("Check how expanded entity is handled here.")]
  570. public override int ReadContentAsBinHex (byte [] buffer, int offset, int length)
  571. {
  572. if (entity != null)
  573. return entity.ReadContentAsBinHex (buffer, offset, length);
  574. else
  575. return source.ReadContentAsBinHex (buffer, offset, length);
  576. }
  577. [MonoTODO ("Check how expanded entity is handled here.")]
  578. public override int ReadElementContentAsBase64 (byte [] buffer, int offset, int length)
  579. {
  580. if (entity != null)
  581. return entity.ReadElementContentAsBase64 (buffer, offset, length);
  582. else
  583. return source.ReadElementContentAsBase64 (buffer, offset, length);
  584. }
  585. [MonoTODO ("Check how expanded entity is handled here.")]
  586. public override int ReadElementContentAsBinHex (byte [] buffer, int offset, int length)
  587. {
  588. if (entity != null)
  589. return entity.ReadElementContentAsBinHex (buffer, offset, length);
  590. else
  591. return source.ReadElementContentAsBinHex (buffer, offset, length);
  592. }
  593. #endregion
  594. }
  595. }
  596. #endif