XPathNavigatorReader.cs 16 KB

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