XmlTextReader2.cs 16 KB

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