XmlTextReader2.cs 17 KB

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