XmlTextReader2.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. //
  2. // System.Xml.XmlTextReader2.cs - XmlTextReader for .NET 2.0
  3. //
  4. // Author:
  5. // Atsushi Enomoto ([email protected])
  6. //
  7. // Copyright (C) 2004 Novell, Inc.
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using XmlTextReaderImpl = Mono.Xml2.XmlTextReader;
  31. using System;
  32. using System.Collections;
  33. using System.Globalization;
  34. using System.IO;
  35. using System.Text;
  36. using System.Xml.Schema;
  37. using Mono.Xml;
  38. namespace System.Xml
  39. {
  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. private XmlTextReader (XmlTextReaderImpl entityContainer, bool insideAttribute)
  105. {
  106. source = entityContainer;
  107. this.entityInsideAttribute = insideAttribute;
  108. }
  109. #region Properties
  110. private XmlReader Current {
  111. get { return entity != null && entity.ReadState != ReadState.Initial ? (XmlReader) entity : source; }
  112. }
  113. public override int AttributeCount {
  114. get { return Current.AttributeCount; }
  115. }
  116. public override string BaseURI {
  117. get { return Current.BaseURI; }
  118. }
  119. public override bool CanReadBinaryContent {
  120. get { return true; }
  121. }
  122. public override bool CanReadValueChunk {
  123. get { return true; }
  124. }
  125. public override bool CanResolveEntity {
  126. get { return true; }
  127. }
  128. public override int Depth {
  129. get {
  130. // On EndEntity, depth is the same as that
  131. // of EntityReference.
  132. if (entity != null && entity.ReadState == ReadState.Interactive)
  133. return source.Depth + entity.Depth + 1;
  134. else
  135. return source.Depth;
  136. }
  137. }
  138. public override bool EOF {
  139. get { return source.EOF; }
  140. }
  141. public override bool HasValue {
  142. get { return Current.HasValue; }
  143. }
  144. public override bool IsDefault {
  145. get { return Current.IsDefault; }
  146. }
  147. public override bool IsEmptyElement {
  148. get { return Current.IsEmptyElement; }
  149. }
  150. public override string LocalName {
  151. get { return Current.LocalName; }
  152. }
  153. public override string Name {
  154. get { return Current.Name; }
  155. }
  156. public override string NamespaceURI {
  157. get { return Current.NamespaceURI; }
  158. }
  159. public override XmlNameTable NameTable {
  160. get { return Current.NameTable; }
  161. }
  162. public override XmlNodeType NodeType {
  163. get {
  164. if (Current == entity)
  165. return entity.EOF ? XmlNodeType.EndEntity : entity.NodeType;
  166. else
  167. return source.NodeType;
  168. }
  169. }
  170. internal XmlParserContext ParserContext {
  171. get { return ((IHasXmlParserContext) Current).ParserContext; }
  172. }
  173. XmlParserContext IHasXmlParserContext.ParserContext {
  174. get { return this.ParserContext; }
  175. }
  176. public override string Prefix {
  177. get { return Current.Prefix; }
  178. }
  179. public override char QuoteChar {
  180. get { return Current.QuoteChar; }
  181. }
  182. public override ReadState ReadState {
  183. get { return entity != null ? ReadState.Interactive : source.ReadState; }
  184. }
  185. public override XmlReaderSettings Settings {
  186. get { return base.Settings; }
  187. }
  188. public override string Value {
  189. get { return Current.Value; }
  190. }
  191. public override string XmlLang {
  192. get { return Current.XmlLang; }
  193. }
  194. public override XmlSpace XmlSpace {
  195. get { return Current.XmlSpace; }
  196. }
  197. // non-overrides
  198. internal bool CharacterChecking {
  199. get {
  200. if (entity != null)
  201. return entity.CharacterChecking;
  202. else
  203. return source.CharacterChecking;
  204. }
  205. set {
  206. if (entity != null)
  207. entity.CharacterChecking = value;
  208. source.CharacterChecking = value;
  209. }
  210. }
  211. internal bool CloseInput {
  212. get {
  213. if (entity != null)
  214. return entity.CloseInput;
  215. else
  216. return source.CloseInput;
  217. }
  218. set {
  219. if (entity != null)
  220. entity.CloseInput = value;
  221. source.CloseInput = value;
  222. }
  223. }
  224. internal ConformanceLevel Conformance {
  225. set {
  226. if (entity != null)
  227. entity.Conformance = value;
  228. source.Conformance = value;
  229. }
  230. }
  231. internal XmlResolver Resolver {
  232. get { return source.Resolver; }
  233. }
  234. private void CopyProperties (XmlTextReader other)
  235. {
  236. CharacterChecking = other.CharacterChecking;
  237. CloseInput = other.CloseInput;
  238. if (other.Settings != null)
  239. Conformance = other.Settings.ConformanceLevel;
  240. XmlResolver = other.Resolver;
  241. }
  242. // public members
  243. public Encoding Encoding {
  244. get {
  245. if (entity != null)
  246. return entity.Encoding;
  247. else
  248. return source.Encoding;
  249. }
  250. }
  251. public EntityHandling EntityHandling {
  252. get { return source.EntityHandling; }
  253. set {
  254. if (entity != null)
  255. entity.EntityHandling = value;
  256. source.EntityHandling = value;
  257. }
  258. }
  259. public int LineNumber {
  260. get {
  261. if (entity != null)
  262. return entity.LineNumber;
  263. else
  264. return source.LineNumber;
  265. }
  266. }
  267. public int LinePosition {
  268. get {
  269. if (entity != null)
  270. return entity.LinePosition;
  271. else
  272. return source.LinePosition;
  273. }
  274. }
  275. public bool Namespaces {
  276. get { return source.Namespaces; }
  277. set {
  278. if (entity != null)
  279. entity.Namespaces = value;
  280. source.Namespaces = value;
  281. }
  282. }
  283. public bool Normalization {
  284. get { return source.Normalization; }
  285. set {
  286. if (entity != null)
  287. entity.Normalization = value;
  288. source.Normalization = value;
  289. }
  290. }
  291. public bool ProhibitDtd {
  292. get { return source.ProhibitDtd; }
  293. set {
  294. if (entity != null)
  295. entity.ProhibitDtd = value;
  296. source.ProhibitDtd = value;
  297. }
  298. }
  299. public WhitespaceHandling WhitespaceHandling {
  300. get { return source.WhitespaceHandling; }
  301. set {
  302. if (entity != null)
  303. entity.WhitespaceHandling = value;
  304. source.WhitespaceHandling = value;
  305. }
  306. }
  307. public XmlResolver XmlResolver {
  308. set {
  309. if (entity != null)
  310. entity.XmlResolver = value;
  311. source.XmlResolver = value;
  312. }
  313. }
  314. #endregion
  315. #region Methods
  316. internal void AdjustLineInfoOffset (int lineNumberOffset, int linePositionOffset)
  317. {
  318. if (entity != null)
  319. entity.AdjustLineInfoOffset (lineNumberOffset, linePositionOffset);
  320. source.AdjustLineInfoOffset (lineNumberOffset, linePositionOffset);
  321. }
  322. internal void SetNameTable (XmlNameTable nameTable)
  323. {
  324. if (entity != null)
  325. entity.SetNameTable (nameTable);
  326. source.SetNameTable (nameTable);
  327. }
  328. // overrides
  329. public override void Close ()
  330. {
  331. if (entity != null)
  332. entity.Close ();
  333. source.Close ();
  334. }
  335. public override string GetAttribute (int i)
  336. {
  337. return Current.GetAttribute (i);
  338. }
  339. // MS.NET 1.0 msdn says that this method returns String.Empty
  340. // for absent attribute, but in fact it returns null.
  341. // This description is corrected in MS.NET 1.1 msdn.
  342. public override string GetAttribute (string name)
  343. {
  344. return Current.GetAttribute (name);
  345. }
  346. public override string GetAttribute (string localName, string namespaceURI)
  347. {
  348. return Current.GetAttribute (localName, namespaceURI);
  349. }
  350. public IDictionary GetNamespacesInScope (XmlNamespaceScope scope)
  351. {
  352. return ((IXmlNamespaceResolver) Current).GetNamespacesInScope (scope);
  353. }
  354. public override string LookupNamespace (string prefix)
  355. {
  356. return Current.LookupNamespace (prefix);
  357. }
  358. string IXmlNamespaceResolver.LookupPrefix (string ns)
  359. {
  360. return ((IXmlNamespaceResolver) Current).LookupPrefix (ns);
  361. }
  362. public override void MoveToAttribute (int i)
  363. {
  364. if (entity != null && entityInsideAttribute) {
  365. entity.Close ();
  366. entity = null;
  367. }
  368. Current.MoveToAttribute (i);
  369. insideAttribute = true;
  370. }
  371. public override bool MoveToAttribute (string name)
  372. {
  373. if (entity != null && !entityInsideAttribute)
  374. return entity.MoveToAttribute (name);
  375. if (!source.MoveToAttribute (name))
  376. return false;
  377. if (entity != null && entityInsideAttribute) {
  378. entity.Close ();
  379. entity = null;
  380. }
  381. insideAttribute = true;
  382. return true;
  383. }
  384. public override bool MoveToAttribute (string localName, string namespaceName)
  385. {
  386. if (entity != null && !entityInsideAttribute)
  387. return entity.MoveToAttribute (localName, namespaceName);
  388. if (!source.MoveToAttribute (localName, namespaceName))
  389. return false;
  390. if (entity != null && entityInsideAttribute) {
  391. entity.Close ();
  392. entity = null;
  393. }
  394. insideAttribute = true;
  395. return true;
  396. }
  397. public override bool MoveToElement ()
  398. {
  399. if (entity != null && entityInsideAttribute) {
  400. entity.Close ();
  401. entity = null;
  402. }
  403. if (!Current.MoveToElement ())
  404. return false;
  405. insideAttribute = false;
  406. return true;
  407. }
  408. public override bool MoveToFirstAttribute ()
  409. {
  410. if (entity != null && !entityInsideAttribute)
  411. return entity.MoveToFirstAttribute ();
  412. if (!source.MoveToFirstAttribute ())
  413. return false;
  414. if (entity != null && entityInsideAttribute) {
  415. entity.Close ();
  416. entity = null;
  417. }
  418. insideAttribute = true;
  419. return true;
  420. }
  421. public override bool MoveToNextAttribute ()
  422. {
  423. if (entity != null && !entityInsideAttribute)
  424. return entity.MoveToNextAttribute ();
  425. if (!source.MoveToNextAttribute ())
  426. return false;
  427. if (entity != null && entityInsideAttribute) {
  428. entity.Close ();
  429. entity = null;
  430. }
  431. insideAttribute = true;
  432. return true;
  433. }
  434. public override bool Read ()
  435. {
  436. insideAttribute = false;
  437. if (entity != null && (entityInsideAttribute || entity.EOF)) {
  438. entity.Close ();
  439. entity = null;
  440. }
  441. if (entity != null) {
  442. if (entity.Read ())
  443. return true;
  444. if (EntityHandling == EntityHandling.ExpandEntities) {
  445. // EndEntity must be skipped
  446. entity.Close ();
  447. entity = null;
  448. return Read ();
  449. }
  450. else
  451. return true; // either success or EndEntity
  452. }
  453. else {
  454. if (!source.Read ())
  455. return false;
  456. if (EntityHandling == EntityHandling.ExpandEntities
  457. && source.NodeType == XmlNodeType.EntityReference) {
  458. ResolveEntity ();
  459. return Read ();
  460. }
  461. return true;
  462. }
  463. }
  464. public override bool ReadAttributeValue ()
  465. {
  466. if (entity != null && entityInsideAttribute) {
  467. if (entity.EOF) {
  468. entity.Close ();
  469. entity = null;
  470. }
  471. else {
  472. entity.Read ();
  473. return true; // either success or EndEntity
  474. }
  475. }
  476. return Current.ReadAttributeValue ();
  477. }
  478. public override string ReadString ()
  479. {
  480. return base.ReadString ();
  481. }
  482. public void ResetState ()
  483. {
  484. if (entity != null)
  485. entity.ResetState ();
  486. source.ResetState ();
  487. }
  488. public override void ResolveEntity ()
  489. {
  490. if (entity != null)
  491. entity.ResolveEntity ();
  492. else {
  493. if (source.NodeType != XmlNodeType.EntityReference)
  494. throw new InvalidOperationException ("The current node is not an Entity Reference");
  495. XmlTextReaderImpl entReader =
  496. ParserContext.Dtd.GenerateEntityContentReader (source.Name, ParserContext);
  497. if (entReader == null)
  498. throw new XmlException (this as IXmlLineInfo, this.BaseURI, String.Format ("Reference to undeclared entity '{0}'.", source.Name));
  499. entity = new XmlTextReader (
  500. entReader, insideAttribute);
  501. entity.CopyProperties (this);
  502. }
  503. }
  504. public override void Skip ()
  505. {
  506. base.Skip ();
  507. }
  508. [MonoTODO ("Check how expanded entity is handled here.")]
  509. public TextReader GetRemainder ()
  510. {
  511. if (entity != null) {
  512. entity.Close ();
  513. entity = null;
  514. }
  515. return source.GetRemainder ();
  516. }
  517. public bool HasLineInfo ()
  518. {
  519. return true;
  520. }
  521. [MonoTODO ("Check how expanded entity is handled here.")]
  522. public int ReadBase64 (byte [] buffer, int offset, int length)
  523. {
  524. if (entity != null)
  525. return entity.ReadBase64 (buffer, offset, length);
  526. else
  527. return source.ReadBase64 (buffer, offset, length);
  528. }
  529. [MonoTODO ("Check how expanded entity is handled here.")]
  530. public int ReadBinHex (byte [] buffer, int offset, int length)
  531. {
  532. if (entity != null)
  533. return entity.ReadBinHex (buffer, offset, length);
  534. else
  535. return source.ReadBinHex (buffer, offset, length);
  536. }
  537. [MonoTODO ("Check how expanded entity is handled here.")]
  538. public int ReadChars (char [] buffer, int offset, int length)
  539. {
  540. if (entity != null)
  541. return entity.ReadChars (buffer, offset, length);
  542. else
  543. return source.ReadChars (buffer, offset, length);
  544. }
  545. [MonoTODO ("Check how expanded entity is handled here.")]
  546. public override int ReadContentAsBase64 (byte [] buffer, int offset, int length)
  547. {
  548. if (entity != null)
  549. return entity.ReadContentAsBase64 (buffer, offset, length);
  550. else
  551. return source.ReadContentAsBase64 (buffer, offset, length);
  552. }
  553. [MonoTODO ("Check how expanded entity is handled here.")]
  554. public override int ReadContentAsBinHex (byte [] buffer, int offset, int length)
  555. {
  556. if (entity != null)
  557. return entity.ReadContentAsBinHex (buffer, offset, length);
  558. else
  559. return source.ReadContentAsBinHex (buffer, offset, length);
  560. }
  561. [MonoTODO ("Check how expanded entity is handled here.")]
  562. public override int ReadElementContentAsBase64 (byte [] buffer, int offset, int length)
  563. {
  564. if (entity != null)
  565. return entity.ReadElementContentAsBase64 (buffer, offset, length);
  566. else
  567. return source.ReadElementContentAsBase64 (buffer, offset, length);
  568. }
  569. [MonoTODO ("Check how expanded entity is handled here.")]
  570. public override int ReadElementContentAsBinHex (byte [] buffer, int offset, int length)
  571. {
  572. if (entity != null)
  573. return entity.ReadElementContentAsBinHex (buffer, offset, length);
  574. else
  575. return source.ReadElementContentAsBinHex (buffer, offset, length);
  576. }
  577. #endregion
  578. }
  579. }
  580. #endif