XPathNavigatorReader.cs 17 KB

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