XmlTextReader2.cs 17 KB

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