XmlTextReader2.cs 15 KB

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