XPathNavigatorReader.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. //
  2. // XPathNavigatorReader.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // 2003 Atsushi Enomoto. No rights reserved.
  8. // Copyright (C) 2004 Novell Inc.
  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. using System;
  30. using System.Text;
  31. using System.Xml;
  32. using System.Xml.Schema;
  33. using System.Xml.XPath;
  34. namespace Mono.Xml.XPath
  35. {
  36. internal class XPathNavigatorReader : XmlReader
  37. {
  38. public XPathNavigatorReader (XPathNavigator nav)
  39. {
  40. // It seems that this class have only to support linked
  41. // node as its parameter
  42. switch (nav.NodeType) {
  43. case XPathNodeType.Attribute:
  44. case XPathNodeType.Namespace:
  45. throw new InvalidOperationException (String.Format ("NodeType {0} is not supported to read as a subtree of an XPathNavigator.", nav.NodeType));
  46. }
  47. root = nav.Clone ();
  48. current = nav.Clone ();
  49. }
  50. XPathNavigator root;
  51. XPathNavigator current;
  52. bool started;
  53. bool closed;
  54. bool endElement;
  55. bool attributeValueConsumed;
  56. StringBuilder readStringBuffer = new StringBuilder ();
  57. #if NET_2_0
  58. StringBuilder innerXmlBuilder = new StringBuilder ();
  59. #endif
  60. int depth = 0;
  61. int attributeCount = 0;
  62. bool eof;
  63. bool nextIsEOF;
  64. #region Properties
  65. #if NET_2_0
  66. public override bool CanReadBinaryContent {
  67. get { return true; }
  68. }
  69. public override bool CanReadValueChunk {
  70. get { return true; }
  71. }
  72. #endif
  73. public override XmlNodeType NodeType
  74. {
  75. get {
  76. if (ReadState != ReadState.Interactive)
  77. return XmlNodeType.None;
  78. if (endElement)
  79. return XmlNodeType.EndElement;
  80. if (attributeValueConsumed)
  81. // Is there any way to get other kind of nodes than Text?
  82. return XmlNodeType.Text;
  83. switch (current.NodeType) {
  84. case XPathNodeType.Namespace:
  85. case XPathNodeType.Attribute:
  86. return XmlNodeType.Attribute;
  87. case XPathNodeType.Comment:
  88. return XmlNodeType.Comment;
  89. case XPathNodeType.Element:
  90. return XmlNodeType.Element;
  91. case XPathNodeType.ProcessingInstruction:
  92. return XmlNodeType.ProcessingInstruction;
  93. case XPathNodeType.Root:
  94. // It is actually Document, but in XmlReader there is no such situation to return Document.
  95. return XmlNodeType.None;
  96. case XPathNodeType.SignificantWhitespace:
  97. return XmlNodeType.SignificantWhitespace;
  98. case XPathNodeType.Text:
  99. return XmlNodeType.Text;
  100. case XPathNodeType.Whitespace:
  101. return XmlNodeType.Whitespace;
  102. default:
  103. throw new InvalidOperationException (String.Format ("Current XPathNavigator status is {0} which is not acceptable to XmlReader.", current.NodeType));
  104. }
  105. }
  106. }
  107. public override string Name {
  108. get {
  109. if (eof)
  110. return String.Empty;
  111. else if (current.NodeType == XPathNodeType.Namespace)
  112. return current.Name == String.Empty ? "xmlns" : "xmlns:" + current.Name;
  113. else
  114. return current.Name;
  115. }
  116. }
  117. public override string LocalName {
  118. get {
  119. if (eof)
  120. return String.Empty;
  121. else if (current.NodeType == XPathNodeType.Namespace && current.LocalName == String.Empty)
  122. return "xmlns";
  123. else
  124. return current.LocalName;
  125. }
  126. }
  127. public override string NamespaceURI {
  128. get {
  129. if (eof)
  130. return String.Empty;
  131. else if (current.NodeType == XPathNodeType.Namespace)
  132. return "http://www.w3.org/2000/xmlns/";
  133. else
  134. return current.NamespaceURI;
  135. }
  136. }
  137. public override string Prefix {
  138. get {
  139. if (eof)
  140. return String.Empty;
  141. else if (current.NodeType == XPathNodeType.Namespace && current.LocalName != String.Empty)
  142. return "xmlns";
  143. else
  144. return current.Prefix;
  145. }
  146. }
  147. public override bool HasValue {
  148. get {
  149. switch (current.NodeType) {
  150. case XPathNodeType.Namespace:
  151. case XPathNodeType.Attribute:
  152. case XPathNodeType.Comment:
  153. case XPathNodeType.ProcessingInstruction:
  154. case XPathNodeType.SignificantWhitespace:
  155. case XPathNodeType.Text:
  156. case XPathNodeType.Whitespace:
  157. return true;
  158. }
  159. return false;
  160. }
  161. }
  162. public override int Depth {
  163. get {
  164. switch (ReadState) {
  165. case ReadState.EndOfFile:
  166. case ReadState.Initial:
  167. case ReadState.Closed:
  168. return 0;
  169. }
  170. return depth;
  171. }
  172. }
  173. public override string Value {
  174. get {
  175. switch (current.NodeType) {
  176. case XPathNodeType.Namespace:
  177. case XPathNodeType.Attribute:
  178. case XPathNodeType.Comment:
  179. case XPathNodeType.ProcessingInstruction:
  180. case XPathNodeType.SignificantWhitespace:
  181. case XPathNodeType.Text:
  182. case XPathNodeType.Whitespace:
  183. return current.Value;
  184. case XPathNodeType.Element:
  185. case XPathNodeType.Root:
  186. return String.Empty;
  187. default:
  188. throw new InvalidOperationException ("Current XPathNavigator status is {0} which is not acceptable to XmlReader.");
  189. }
  190. }
  191. }
  192. public override string BaseURI {
  193. get { return current.BaseURI; }
  194. }
  195. public override bool IsEmptyElement {
  196. get { return current.IsEmptyElement; }
  197. }
  198. public override bool IsDefault {
  199. get {
  200. #if NET_2_0
  201. IXmlSchemaInfo si = current as IXmlSchemaInfo;
  202. return si != null && si.IsDefault;
  203. #else
  204. return false; // no way to check this.
  205. #endif
  206. }
  207. }
  208. // It makes no sense.
  209. public override char QuoteChar {
  210. get { return '\"'; }
  211. }
  212. #if NET_2_0
  213. public override IXmlSchemaInfo SchemaInfo {
  214. get { return current.SchemaInfo; }
  215. }
  216. #endif
  217. public override string XmlLang {
  218. get { return current.XmlLang; }
  219. }
  220. // It is meaningless.
  221. public override XmlSpace XmlSpace {
  222. get { return XmlSpace.None; }
  223. }
  224. public override int AttributeCount {
  225. get { return attributeCount; }
  226. }
  227. private int GetAttributeCount ()
  228. {
  229. int count = 0;
  230. if (current.MoveToFirstAttribute ()) {
  231. do {
  232. count++;
  233. } while (current.MoveToNextAttribute ());
  234. current.MoveToParent ();
  235. }
  236. if (current.MoveToFirstNamespace (XPathNamespaceScope.Local)) {
  237. do {
  238. count++;
  239. } while (current.MoveToNextNamespace (XPathNamespaceScope.Local));
  240. current.MoveToParent ();
  241. }
  242. return count;
  243. }
  244. private bool MoveToAttributeNavigator (int i)
  245. {
  246. switch (current.NodeType) {
  247. case XPathNodeType.Namespace:
  248. case XPathNodeType.Attribute:
  249. this.MoveToElement ();
  250. goto case XPathNodeType.Element;
  251. case XPathNodeType.Element:
  252. int count = 0;
  253. if (MoveToFirstAttribute ()) {
  254. if (i == 0)
  255. return true;
  256. }
  257. for (count++; this.MoveToNextAttribute (); count++) {
  258. if (count == i)
  259. return true;
  260. }
  261. break;
  262. }
  263. return false;
  264. }
  265. public override string this [int i] {
  266. get {
  267. XPathNavigator backup = current.Clone ();
  268. try {
  269. if (MoveToAttributeNavigator (i))
  270. return Value;
  271. else
  272. throw new ArgumentOutOfRangeException ();
  273. } finally {
  274. current.MoveTo (backup);
  275. }
  276. }
  277. }
  278. private void SplitName (string name, out string localName, out string ns)
  279. {
  280. localName = name;
  281. ns = String.Empty;
  282. int colon = name.IndexOf (':');
  283. if (colon > 0) {
  284. localName = name.Substring (colon + 1, name.Length - colon - 1);
  285. ns = this.LookupNamespace (name.Substring (0, colon));
  286. }
  287. }
  288. public override string this [string name] {
  289. get {
  290. string localName;
  291. string ns;
  292. SplitName (name, out localName, out ns);
  293. return this [localName, ns];
  294. }
  295. }
  296. public override string this [string localName, string namespaceURI] {
  297. get {
  298. string v = current.GetAttribute (localName, namespaceURI);
  299. if (v != String.Empty)
  300. return v;
  301. XPathNavigator tmp = current.Clone ();
  302. return tmp.MoveToAttribute (localName, namespaceURI) ? String.Empty : null;
  303. }
  304. }
  305. public override bool EOF {
  306. get { return ReadState == ReadState.EndOfFile; }
  307. }
  308. public override ReadState ReadState {
  309. get {
  310. if (eof)
  311. return ReadState.EndOfFile;
  312. if (closed)
  313. return ReadState.Closed;
  314. else if (!started)
  315. return ReadState.Initial;
  316. return ReadState.Interactive;
  317. }
  318. }
  319. public override XmlNameTable NameTable {
  320. get { return current.NameTable; }
  321. }
  322. #endregion
  323. #region Methods
  324. public override string GetAttribute (string name)
  325. {
  326. string localName;
  327. string ns;
  328. SplitName (name, out localName, out ns);
  329. return this [localName, ns];
  330. }
  331. public override string GetAttribute (string localName, string namespaceURI)
  332. {
  333. return this [localName, namespaceURI];
  334. }
  335. public override string GetAttribute (int i)
  336. {
  337. return this [i];
  338. }
  339. private bool CheckAttributeMove (bool b)
  340. {
  341. if (b)
  342. attributeValueConsumed = false;
  343. return b;
  344. }
  345. public override bool MoveToAttribute (string name)
  346. {
  347. string localName;
  348. string ns;
  349. SplitName (name, out localName, out ns);
  350. return CheckAttributeMove (MoveToAttribute (localName, ns));
  351. }
  352. public override bool MoveToAttribute (string localName, string namespaceURI)
  353. {
  354. XPathNavigator backup = null;
  355. switch (current.NodeType) {
  356. case XPathNodeType.Attribute:
  357. backup = current.Clone ();
  358. this.MoveToElement ();
  359. goto case XPathNodeType.Element;
  360. case XPathNodeType.Element:
  361. while (MoveToNextAttribute ())
  362. if (current.LocalName == localName && current.NamespaceURI == namespaceURI) {
  363. attributeValueConsumed = false;
  364. return true;
  365. }
  366. break;
  367. }
  368. if (backup != null)
  369. current = backup;
  370. return false;
  371. }
  372. public override void MoveToAttribute (int i)
  373. {
  374. if (!MoveToAttributeNavigator (i))
  375. throw new ArgumentOutOfRangeException ();
  376. }
  377. public override bool MoveToFirstAttribute ()
  378. {
  379. bool b = CheckAttributeMove (current.MoveToFirstNamespace (XPathNamespaceScope.Local));
  380. if (b)
  381. return true;
  382. return CheckAttributeMove (current.MoveToFirstAttribute ());
  383. }
  384. public override bool MoveToNextAttribute ()
  385. {
  386. if (current.NodeType == XPathNodeType.Namespace) {
  387. bool b = CheckAttributeMove (current.MoveToNextNamespace (XPathNamespaceScope.Local));
  388. if (b)
  389. return true;
  390. current.MoveToParent ();
  391. b = CheckAttributeMove (current.MoveToFirstAttribute ());
  392. if (b)
  393. return true;
  394. }
  395. return CheckAttributeMove (current.MoveToNextAttribute ());
  396. }
  397. public override bool MoveToElement ()
  398. {
  399. if (current.NodeType == XPathNodeType.Attribute ||
  400. current.NodeType == XPathNodeType.Namespace) {
  401. attributeValueConsumed = false;
  402. return current.MoveToParent ();
  403. }
  404. return false;
  405. }
  406. public override void Close ()
  407. {
  408. closed = true;
  409. eof = true;
  410. }
  411. public override bool Read ()
  412. {
  413. #if NET_2_0
  414. if (Binary != null)
  415. Binary.Reset ();
  416. #endif
  417. switch (ReadState) {
  418. case ReadState.EndOfFile:
  419. case ReadState.Closed:
  420. case ReadState.Error:
  421. return false;
  422. case ReadState.Initial:
  423. started = true;
  424. switch (current.NodeType) {
  425. case XPathNodeType.Root:
  426. // recurse, but as Interactive
  427. return Read ();
  428. case XPathNodeType.Element:
  429. if (current.IsEmptyElement)
  430. nextIsEOF = true;
  431. attributeCount = GetAttributeCount ();
  432. return true;
  433. default:
  434. nextIsEOF = true;
  435. return true;
  436. }
  437. break;
  438. }
  439. if (nextIsEOF) {
  440. nextIsEOF = false;
  441. eof = true;
  442. return false;
  443. }
  444. MoveToElement ();
  445. if (endElement || !current.MoveToFirstChild ()) {
  446. if (current.IsSamePosition (root)) { // It should happen only when the root node was empty.
  447. eof = true;
  448. return false;
  449. }
  450. if (!current.MoveToNext ()) {
  451. current.MoveToParent ();
  452. depth--;
  453. endElement = (current.NodeType == XPathNodeType.Element);
  454. if (current.IsSamePosition (root)) {
  455. if (current.NodeType == XPathNodeType.Element)
  456. nextIsEOF = true;
  457. else {
  458. endElement = false;
  459. eof = true;
  460. return false;
  461. }
  462. }
  463. } else
  464. endElement = false;
  465. }
  466. else if (!endElement)
  467. depth++;
  468. attributeCount = GetAttributeCount ();
  469. return true;
  470. }
  471. public override string ReadString ()
  472. {
  473. readStringBuffer.Length = 0;
  474. switch (NodeType) {
  475. default:
  476. return String.Empty;
  477. case XmlNodeType.Element:
  478. if (IsEmptyElement)
  479. return String.Empty;
  480. do {
  481. Read ();
  482. switch (NodeType) {
  483. case XmlNodeType.Text:
  484. case XmlNodeType.CDATA:
  485. case XmlNodeType.Whitespace:
  486. case XmlNodeType.SignificantWhitespace:
  487. readStringBuffer.Append (Value);
  488. continue;
  489. }
  490. break;
  491. } while (true);
  492. break;
  493. case XmlNodeType.Text:
  494. case XmlNodeType.CDATA:
  495. case XmlNodeType.Whitespace:
  496. case XmlNodeType.SignificantWhitespace:
  497. do {
  498. switch (NodeType) {
  499. case XmlNodeType.Text:
  500. case XmlNodeType.CDATA:
  501. case XmlNodeType.Whitespace:
  502. case XmlNodeType.SignificantWhitespace:
  503. readStringBuffer.Append (Value);
  504. Read ();
  505. continue;
  506. }
  507. break;
  508. } while (true);
  509. break;
  510. }
  511. string ret = readStringBuffer.ToString ();
  512. readStringBuffer.Length = 0;
  513. return ret;
  514. }
  515. #if NET_1_1
  516. #else
  517. public override string ReadInnerXml ()
  518. {
  519. if (ReadState != ReadState.Interactive)
  520. return String.Empty;
  521. switch (NodeType) {
  522. case XmlNodeType.Attribute:
  523. return Value;
  524. case XmlNodeType.Element:
  525. if (IsEmptyElement)
  526. return String.Empty;
  527. int startDepth = Depth;
  528. innerXmlBuilder.Length = 0;
  529. bool loop = true;
  530. do {
  531. Read ();
  532. if (NodeType ==XmlNodeType.None)
  533. throw new InvalidOperationException ("unexpected end of xml.");
  534. else if (NodeType == XmlNodeType.EndElement && Depth == startDepth) {
  535. loop = false;
  536. Read ();
  537. }
  538. else
  539. innerXmlBuilder.Append (GetCurrentTagMarkup ());
  540. } while (loop);
  541. string xml = innerXmlBuilder.ToString ();
  542. innerXmlBuilder.Length = 0;
  543. return xml;
  544. case XmlNodeType.None:
  545. // MS document is incorrect. Seems not to progress.
  546. return String.Empty;
  547. default:
  548. Read ();
  549. return String.Empty;
  550. }
  551. }
  552. StringBuilder atts = new StringBuilder ();
  553. private string GetCurrentTagMarkup ()
  554. {
  555. switch (NodeType) {
  556. case XmlNodeType.CDATA:
  557. return String.Format ("<![CDATA[{0}]]>", Value.Replace ("]]>", "]]&gt;"));
  558. case XmlNodeType.Text:
  559. return Value.Replace ("<", "&lt;");
  560. case XmlNodeType.Comment:
  561. return String.Format ("<!--{0}-->", Value);
  562. case XmlNodeType.SignificantWhitespace:
  563. case XmlNodeType.Whitespace:
  564. return Value;
  565. case XmlNodeType.EndElement:
  566. return String.Format ("</{0}>", Name);
  567. }
  568. bool isEmpty = IsEmptyElement;
  569. string name = Name;
  570. atts.Length = 0;
  571. XPathNavigator temp = current.Clone ();
  572. while (temp.MoveToNextAttribute ())
  573. atts.AppendFormat (" {0}='{1}'", temp.Name, temp.Value.Replace ("'", "&apos;"));
  574. if (!IsEmptyElement)
  575. return String.Format ("<{0}{1}>", name, atts);
  576. else
  577. return String.Format ("<{0}{1} />", name, atts);
  578. }
  579. // Arranged copy of XmlTextReader.ReadOuterXml()
  580. public override string ReadOuterXml ()
  581. {
  582. if (ReadState != ReadState.Interactive)
  583. return String.Empty;
  584. switch (NodeType) {
  585. case XmlNodeType.Attribute:
  586. // strictly incompatible with MS... (it holds spaces attribute between name, value and "=" char (very trivial).
  587. return String.Format ("{0}={1}{2}{1}", Name, QuoteChar, ReadInnerXml ());
  588. case XmlNodeType.Element:
  589. bool isEmpty = IsEmptyElement;
  590. string name = Name;
  591. StringBuilder atts = new StringBuilder ();
  592. XPathNavigator temp = current.Clone ();
  593. while (temp.MoveToNextAttribute ())
  594. atts.AppendFormat (" {0}='{1}'", temp.Name, temp.Value.Replace ("'", "&apos;"));
  595. if (!isEmpty)
  596. return String.Format ("{0}{1}</{2}>", GetCurrentTagMarkup (), atts, ReadInnerXml (), name);
  597. else
  598. return String.Format ("{0}", GetCurrentTagMarkup ());
  599. case XmlNodeType.None:
  600. // MS document is incorrect. Seems not to progress.
  601. return String.Empty;
  602. default:
  603. Read ();
  604. return String.Empty;
  605. }
  606. }
  607. #endif
  608. public override string LookupNamespace (string prefix)
  609. {
  610. XPathNavigator backup = current.Clone ();
  611. try {
  612. this.MoveToElement ();
  613. if (current.MoveToFirstNamespace ()) {
  614. do {
  615. if (current.LocalName == prefix)
  616. return current.Value;
  617. } while (current.MoveToNextNamespace ());
  618. }
  619. return null;
  620. } finally {
  621. current = backup;
  622. }
  623. }
  624. // It does not support entity resolution.
  625. public override void ResolveEntity ()
  626. {
  627. throw new InvalidOperationException ();
  628. }
  629. public override bool ReadAttributeValue () {
  630. if (NodeType != XmlNodeType.Attribute)
  631. return false;
  632. if (attributeValueConsumed)
  633. return false;
  634. attributeValueConsumed = true;
  635. return true;
  636. }
  637. #endregion
  638. }
  639. }