XPathNavigatorReader.cs 17 KB

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