XmlTextReader2.cs 17 KB

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