XmlTextReader2.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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, false);
  357. }
  358. public override string LookupNamespace (string prefix, bool atomizedName)
  359. {
  360. return ((IXmlNamespaceResolver) Current).LookupNamespace (prefix, atomizedName);
  361. }
  362. string IXmlNamespaceResolver.LookupPrefix (string ns)
  363. {
  364. return ((IXmlNamespaceResolver) Current).LookupPrefix (ns, false);
  365. }
  366. public string LookupPrefix (string ns, bool atomizedName)
  367. {
  368. return ((IXmlNamespaceResolver) Current).LookupPrefix (ns, atomizedName);
  369. }
  370. public override void MoveToAttribute (int i)
  371. {
  372. if (entity != null && entityInsideAttribute) {
  373. entity.Close ();
  374. entity = null;
  375. }
  376. Current.MoveToAttribute (i);
  377. insideAttribute = true;
  378. }
  379. public override bool MoveToAttribute (string name)
  380. {
  381. if (entity != null && !entityInsideAttribute)
  382. return entity.MoveToAttribute (name);
  383. if (!source.MoveToAttribute (name))
  384. return false;
  385. if (entity != null && entityInsideAttribute) {
  386. entity.Close ();
  387. entity = null;
  388. }
  389. insideAttribute = true;
  390. return true;
  391. }
  392. public override bool MoveToAttribute (string localName, string namespaceName)
  393. {
  394. if (entity != null && !entityInsideAttribute)
  395. return entity.MoveToAttribute (localName, namespaceName);
  396. if (!source.MoveToAttribute (localName, namespaceName))
  397. return false;
  398. if (entity != null && entityInsideAttribute) {
  399. entity.Close ();
  400. entity = null;
  401. }
  402. insideAttribute = true;
  403. return true;
  404. }
  405. public override bool MoveToElement ()
  406. {
  407. if (entity != null && entityInsideAttribute) {
  408. entity.Close ();
  409. entity = null;
  410. }
  411. if (!Current.MoveToElement ())
  412. return false;
  413. insideAttribute = false;
  414. return true;
  415. }
  416. public override bool MoveToFirstAttribute ()
  417. {
  418. if (entity != null && !entityInsideAttribute)
  419. return entity.MoveToFirstAttribute ();
  420. if (!source.MoveToFirstAttribute ())
  421. return false;
  422. if (entity != null && entityInsideAttribute) {
  423. entity.Close ();
  424. entity = null;
  425. }
  426. insideAttribute = true;
  427. return true;
  428. }
  429. public override bool MoveToNextAttribute ()
  430. {
  431. if (entity != null && !entityInsideAttribute)
  432. return entity.MoveToNextAttribute ();
  433. if (!source.MoveToNextAttribute ())
  434. return false;
  435. if (entity != null && entityInsideAttribute) {
  436. entity.Close ();
  437. entity = null;
  438. }
  439. insideAttribute = true;
  440. return true;
  441. }
  442. public override bool Read ()
  443. {
  444. insideAttribute = false;
  445. if (entity != null && (entityInsideAttribute || entity.EOF)) {
  446. entity.Close ();
  447. entity = null;
  448. }
  449. if (entity != null) {
  450. if (entity.Read ())
  451. return true;
  452. if (EntityHandling == EntityHandling.ExpandEntities) {
  453. // EndEntity must be skipped
  454. entity.Close ();
  455. entity = null;
  456. return Read ();
  457. }
  458. else
  459. return true; // either success or EndEntity
  460. }
  461. else {
  462. if (!source.Read ())
  463. return false;
  464. if (EntityHandling == EntityHandling.ExpandEntities
  465. && source.NodeType == XmlNodeType.EntityReference) {
  466. ResolveEntity ();
  467. return Read ();
  468. }
  469. return true;
  470. }
  471. }
  472. public override bool ReadAttributeValue ()
  473. {
  474. if (entity != null && entityInsideAttribute) {
  475. if (entity.EOF) {
  476. entity.Close ();
  477. entity = null;
  478. }
  479. else {
  480. entity.Read ();
  481. return true; // either success or EndEntity
  482. }
  483. }
  484. return Current.ReadAttributeValue ();
  485. }
  486. public override string ReadString ()
  487. {
  488. return base.ReadString ();
  489. }
  490. public void ResetState ()
  491. {
  492. if (entity != null)
  493. entity.ResetState ();
  494. source.ResetState ();
  495. }
  496. public override void ResolveEntity ()
  497. {
  498. if (entity != null)
  499. entity.ResolveEntity ();
  500. else {
  501. if (source.NodeType != XmlNodeType.EntityReference)
  502. throw new InvalidOperationException ("The current node is not an Entity Reference");
  503. XmlTextReaderImpl entReader =
  504. ParserContext.Dtd.GenerateEntityContentReader (source.Name, ParserContext);
  505. if (entReader == null)
  506. throw new XmlException (this as IXmlLineInfo, this.BaseURI, String.Format ("Reference to undeclared entity '{0}'.", source.Name));
  507. entity = new XmlTextReader (
  508. entReader, insideAttribute);
  509. entity.CopyProperties (this);
  510. }
  511. }
  512. public override void Skip ()
  513. {
  514. base.Skip ();
  515. }
  516. [MonoTODO ("Check how expanded entity is handled here.")]
  517. public TextReader GetRemainder ()
  518. {
  519. if (entity != null) {
  520. entity.Close ();
  521. entity = null;
  522. }
  523. return source.GetRemainder ();
  524. }
  525. public bool HasLineInfo ()
  526. {
  527. return true;
  528. }
  529. [MonoTODO ("Check how expanded entity is handled here.")]
  530. public int ReadBase64 (byte [] buffer, int offset, int length)
  531. {
  532. if (entity != null)
  533. return entity.ReadBase64 (buffer, offset, length);
  534. else
  535. return source.ReadBase64 (buffer, offset, length);
  536. }
  537. [MonoTODO ("Check how expanded entity is handled here.")]
  538. public int ReadBinHex (byte [] buffer, int offset, int length)
  539. {
  540. if (entity != null)
  541. return entity.ReadBinHex (buffer, offset, length);
  542. else
  543. return source.ReadBinHex (buffer, offset, length);
  544. }
  545. [MonoTODO ("Check how expanded entity is handled here.")]
  546. public int ReadChars (char [] buffer, int offset, int length)
  547. {
  548. if (entity != null)
  549. return entity.ReadChars (buffer, offset, length);
  550. else
  551. return source.ReadChars (buffer, offset, length);
  552. }
  553. [MonoTODO ("Check how expanded entity is handled here.")]
  554. public override int ReadContentAsBase64 (byte [] buffer, int offset, int length)
  555. {
  556. if (entity != null)
  557. return entity.ReadContentAsBase64 (buffer, offset, length);
  558. else
  559. return source.ReadContentAsBase64 (buffer, offset, length);
  560. }
  561. [MonoTODO ("Check how expanded entity is handled here.")]
  562. public override int ReadContentAsBinHex (byte [] buffer, int offset, int length)
  563. {
  564. if (entity != null)
  565. return entity.ReadContentAsBinHex (buffer, offset, length);
  566. else
  567. return source.ReadContentAsBinHex (buffer, offset, length);
  568. }
  569. [MonoTODO ("Check how expanded entity is handled here.")]
  570. public override int ReadElementContentAsBase64 (byte [] buffer, int offset, int length)
  571. {
  572. if (entity != null)
  573. return entity.ReadElementContentAsBase64 (buffer, offset, length);
  574. else
  575. return source.ReadElementContentAsBase64 (buffer, offset, length);
  576. }
  577. [MonoTODO ("Check how expanded entity is handled here.")]
  578. public override int ReadElementContentAsBinHex (byte [] buffer, int offset, int length)
  579. {
  580. if (entity != null)
  581. return entity.ReadElementContentAsBinHex (buffer, offset, length);
  582. else
  583. return source.ReadElementContentAsBinHex (buffer, offset, length);
  584. }
  585. #endregion
  586. }
  587. }
  588. #endif