XPathNavigator2.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. //
  2. // XPathNavigator2.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // (C)2003 Atsushi Enomoto
  8. //
  9. #if NET_1_2
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Specialized;
  13. using System.ComponentModel;
  14. using System.IO;
  15. using System.Security.Policy;
  16. //using System.Xml.Query;
  17. using System.Xml.Schema;
  18. using System.Xml.XPath;
  19. //using Mono.Xml.XPath2;
  20. //using MS.Internal.Xml;
  21. namespace System.Xml
  22. {
  23. public abstract class XPathNavigator2
  24. : ICloneable, ICustomTypeDescriptor, IXmlDataEvidence,
  25. IXmlNamespaceResolver, IXPathNavigator//,
  26. // MS.Internal.Xml.IXmlInfoItem
  27. {
  28. protected XPathNavigator2 ()
  29. {
  30. }
  31. public abstract string BaseUri { get; }
  32. public virtual Evidence [] Evidences {
  33. get { throw new NotImplementedException (); }
  34. }
  35. public bool HasAttributes {
  36. get {
  37. if (ItemType != XmlInfoItemType.Element)
  38. return false;
  39. if (MoveToFirstAttribute () == null)
  40. return false;
  41. MoveToParent ();
  42. return true;
  43. }
  44. }
  45. public bool HasChildren {
  46. get {
  47. if (MoveToFirstChild () == null)
  48. return false;
  49. MoveToParent ();
  50. return true;
  51. }
  52. }
  53. public string InnerXml {
  54. get { throw new NotImplementedException (); }
  55. }
  56. public abstract XmlInfoItemType ItemType { get; }
  57. public abstract string LocalName { get; }
  58. public abstract string Name { get; }
  59. public abstract string Namespace { get; }
  60. public abstract XmlNameTable NameTable { get; }
  61. public string OuterXml {
  62. get { throw new NotImplementedException (); }
  63. }
  64. public abstract string Prefix { get; }
  65. // public MS.Internal.Xml.IXmlSchemaInfo SchemaInfo {
  66. // get { throw new NotImplementedException (); }
  67. // }
  68. public virtual object Schemas {
  69. get { throw new NotImplementedException (); }
  70. }
  71. public virtual Type StorageType {
  72. get { throw new NotImplementedException (); }
  73. }
  74. public virtual object UnderlyingObject {
  75. get { throw new NotImplementedException (); }
  76. }
  77. // public virtual IXmlType XmlType {
  78. // get { throw new NotImplementedException (); }
  79. // }
  80. public virtual bool CheckValidity (XmlSchemaSet schemas, ValidationEventHandler validationEventHandler)
  81. {
  82. throw new NotImplementedException ();
  83. }
  84. public abstract XPathNavigator2 Clone ();
  85. IXPathNavigator IXPathNavigator.Clone ()
  86. {
  87. return this.Clone ();
  88. }
  89. public abstract XmlNodeOrder ComparePosition (IXPathNavigator other);
  90. // public IXmlInfosetReader CopyToReader ()
  91. // {
  92. // throw new NotImplementedException ();
  93. // }
  94. // IMHO it should be virtual, even if Microsoft has excellent implementation.
  95. public string GetAttribute (string localName, string namespaceName)
  96. {
  97. string value = null;
  98. XmlInfoItemType cacheItemType = ItemType;
  99. string currentName = LocalName;
  100. string currentNs = Namespace;
  101. IXPathNavigator tmp = MoveToAttribute (localName, namespaceName);
  102. if (tmp != null) {
  103. value = ReadStringValue ();
  104. MoveToParent ();
  105. switch (cacheItemType) {
  106. case XmlInfoItemType.Attribute:
  107. MoveToAttribute (currentName, currentNs);
  108. break;
  109. case XmlInfoItemType.Namespace:
  110. MoveToNamespace (currentNs);
  111. break;
  112. }
  113. }
  114. return value;
  115. }
  116. public virtual StringDictionary GetNamespacesInScope (XmlNamespaceScope scope)
  117. {
  118. StringDictionary dict = new StringDictionary ();
  119. XPathNavigator2 nav = Clone ();
  120. IXPathNavigator ns = nav.MoveToFirstNamespace (scope);
  121. if (ns != null) {
  122. do {
  123. dict.Add (nav.LocalName, nav.Namespace);
  124. ns = nav.MoveToNextNamespace (scope);
  125. } while (ns != null);
  126. }
  127. return dict;
  128. }
  129. /*private*/ object ICloneable.Clone()
  130. {
  131. return Clone ();
  132. }
  133. /*private*/ AttributeCollection ICustomTypeDescriptor.GetAttributes ()
  134. {
  135. throw new NotImplementedException ();
  136. }
  137. /*private*/ string ICustomTypeDescriptor.GetClassName()
  138. {
  139. throw new NotImplementedException ();
  140. }
  141. /*private*/ string ICustomTypeDescriptor.GetComponentName()
  142. {
  143. throw new NotImplementedException ();
  144. }
  145. /*private*/ TypeConverter ICustomTypeDescriptor.GetConverter()
  146. {
  147. throw new NotImplementedException ();
  148. }
  149. /*private*/ EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
  150. {
  151. throw new NotImplementedException ();
  152. }
  153. /*private*/ PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
  154. {
  155. throw new NotImplementedException ();
  156. }
  157. /*private*/ object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
  158. {
  159. throw new NotImplementedException ();
  160. }
  161. /*private*/ EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
  162. {
  163. throw new NotImplementedException ();
  164. }
  165. /*private*/ EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute[] attrs)
  166. {
  167. throw new NotImplementedException ();
  168. }
  169. /*private*/ PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
  170. {
  171. throw new NotImplementedException ();
  172. }
  173. /*private*/ PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties (Attribute [] attrs)
  174. {
  175. throw new NotImplementedException ();
  176. }
  177. /*private*/ object ICustomTypeDescriptor.GetPropertyOwner (PropertyDescriptor pd)
  178. {
  179. throw new NotImplementedException ();
  180. }
  181. public virtual bool IsDescendant (IXPathNavigator other)
  182. {
  183. IXPathNavigator nav = other as XPathNavigator2;
  184. if (nav == null)
  185. throw new ArgumentException ();
  186. nav = nav.Clone ();
  187. do {
  188. if (IsSamePosition (nav))
  189. return true;
  190. nav = nav.MoveToParent ();
  191. } while (nav != null);
  192. return false;
  193. }
  194. public virtual bool IsSamePosition (IXPathNavigator other)
  195. {
  196. return (ComparePosition (other) == XmlNodeOrder.Same);
  197. }
  198. /*
  199. MS.Internal.Xml.IXmlInfosetReader IXmlInfoItem.CopyToReader ()
  200. {
  201. throw new NotImplementedException ();
  202. }
  203. IXPathNavigator IXPathNavigator.Clone ()
  204. {
  205. return Clone ();
  206. }
  207. */
  208. public virtual string LookupNamespace (string prefix)
  209. {
  210. return LookupNamespace (prefix, false);
  211. }
  212. public virtual string LookupNamespace (string prefix, bool atomizedNames)
  213. {
  214. return LookupNamespaceNode (prefix, atomizedNames, true);
  215. }
  216. public virtual string LookupPrefix (string ns)
  217. {
  218. return LookupPrefix (ns, false);
  219. }
  220. public virtual string LookupPrefix (string ns, bool atomizedNames)
  221. {
  222. return LookupNamespaceNode (ns, atomizedNames, false);
  223. }
  224. private string LookupNamespaceNode (string str, bool atomizedNames, bool isPrefix)
  225. {
  226. string value = null;
  227. XmlInfoItemType cacheItemType = ItemType;
  228. string currentName = LocalName;
  229. string currentNs = Namespace;
  230. IXPathNavigator tmp = MoveToFirstNamespace (XmlNamespaceScope.All);
  231. if (tmp == null)
  232. return null;
  233. do {
  234. if (atomizedNames) {
  235. if (isPrefix) {
  236. if (Object.ReferenceEquals (LocalName, str)) {
  237. value = Namespace;
  238. break;
  239. }
  240. } else {
  241. if (Object.ReferenceEquals (Namespace, str)) {
  242. value = LocalName;
  243. break;
  244. }
  245. }
  246. } else {
  247. if (isPrefix) {
  248. if (LocalName == str) {
  249. value = Namespace;
  250. break;
  251. }
  252. } else {
  253. if (Namespace == str) {
  254. value = Name;
  255. break;
  256. }
  257. }
  258. }
  259. tmp = MoveToNextNamespace (XmlNamespaceScope.All);
  260. } while (tmp != null);
  261. MoveToParent ();
  262. switch (cacheItemType) {
  263. case XmlInfoItemType.Attribute:
  264. MoveToAttribute (currentName, currentNs);
  265. break;
  266. case XmlInfoItemType.Namespace:
  267. MoveToNamespace (currentNs);
  268. break;
  269. }
  270. return value;
  271. }
  272. public abstract IXPathNavigator MoveTo (IXPathNavigator other);
  273. // In the meantime, we should wait System.Data.SqlXml.dll became public.
  274. // public abstract XPathNavigator2 MoveTo (XmlCommand command, XmlQueryArgumentList argList);
  275. public abstract XPathNavigator2 MoveTo (string query);
  276. public abstract XPathNavigator2 MoveTo (string query, IXmlNamespaceResolver namespaceResolver);
  277. public abstract XPathNavigator2 MoveTo (string query, IXmlNamespaceResolver namespaceResolver, XmlQueryDialect dialect);
  278. public virtual XPathNavigator2 MoveToAttribute (string localName, string namespaceName)
  279. {
  280. return MoveToAttribute (localName, namespaceName, false) as XPathNavigator2;
  281. }
  282. public virtual IXPathNavigator MoveToAttribute (string localName, string namespaceName, bool atomizedNames)
  283. {
  284. XmlInfoItemType cacheItemType = ItemType;
  285. string currentName = LocalName;
  286. string currentNs = Namespace;
  287. IXPathNavigator tmp = MoveToFirstAttribute ();
  288. if (tmp == null)
  289. return null;
  290. do {
  291. if (atomizedNames) {
  292. if (Object.ReferenceEquals (LocalName, localName) && Object.ReferenceEquals (Namespace, namespaceName))
  293. return this;
  294. } else {
  295. if (LocalName == localName && Namespace == namespaceName)
  296. return this;
  297. }
  298. tmp = MoveToNextAttribute ();
  299. } while (tmp != null);
  300. MoveToParent ();
  301. switch (cacheItemType) {
  302. case XmlInfoItemType.Attribute:
  303. MoveToAttribute (currentName, currentNs);
  304. break;
  305. case XmlInfoItemType.Namespace:
  306. MoveToNamespace (currentNs);
  307. break;
  308. }
  309. return null;
  310. }
  311. public virtual IXPathNavigator MoveToChild (string localName, string namespaceName, bool atomizedNames)
  312. {
  313. return MoveToChild (localName, namespaceName, atomizedNames, XmlInfoItemType.Document, false);
  314. }
  315. public virtual IXPathNavigator MoveToChild (XmlInfoItemType type)
  316. {
  317. return MoveToChild (null, null, false, type, true);
  318. }
  319. public virtual XPathNavigator2 MoveToChild (string localName, string namespaceName)
  320. {
  321. return MoveToChild (localName, namespaceName, false) as XPathNavigator2;
  322. }
  323. private IXPathNavigator MoveToChild (string localName, string namespaceName, bool atomizedNames, XmlInfoItemType type, bool byType)
  324. {
  325. XmlInfoItemType cacheItemType = ItemType;
  326. string currentName = LocalName;
  327. string currentNs = Namespace;
  328. IXPathNavigator tmp = MoveToFirstChild ();
  329. if (tmp == null)
  330. return null;
  331. do {
  332. if (byType) {
  333. if (type == ItemType)
  334. return this;
  335. } else if (atomizedNames) {
  336. if (Object.ReferenceEquals (LocalName, localName) && Object.ReferenceEquals (Namespace, namespaceName))
  337. return this;
  338. } else {
  339. if (LocalName == localName && Namespace == namespaceName)
  340. return this;
  341. }
  342. tmp = MoveToNextSibling ();
  343. } while (tmp != null);
  344. MoveToParent ();
  345. switch (cacheItemType) {
  346. case XmlInfoItemType.Attribute:
  347. MoveToAttribute (currentName, currentNs);
  348. break;
  349. case XmlInfoItemType.Namespace:
  350. MoveToNamespace (currentNs);
  351. break;
  352. }
  353. return null;
  354. }
  355. public virtual IXPathNavigator MoveToDescendantOf (IXPathNavigator root, string localName, string namespaceName, bool atomizedNames)
  356. {
  357. throw new NotImplementedException ();
  358. }
  359. public virtual IXPathNavigator MoveToDescendantOf (IXPathNavigator root, XmlInfoItemType type)
  360. {
  361. throw new NotImplementedException ();
  362. }
  363. public abstract IXPathNavigator MoveToFirstAttribute ();
  364. public abstract IXPathNavigator MoveToFirstChild ();
  365. public abstract IXPathNavigator MoveToFirstNamespace (XmlNamespaceScope scope);
  366. public abstract IXPathNavigator MoveToFirstValue ();
  367. public virtual IXPathNavigator MoveToId (string id)
  368. {
  369. throw new NotImplementedException ();
  370. }
  371. public XPathNavigator2 MoveToNamespace (string prefix)
  372. {
  373. return MoveToNamespace (prefix, false);
  374. }
  375. public XPathNavigator2 MoveToNamespace (string prefix, bool atomizedNames)
  376. {
  377. throw new NotImplementedException ();
  378. }
  379. public abstract IXPathNavigator MoveToNextAttribute ();
  380. public abstract IXPathNavigator MoveToNextNamespace (XmlNamespaceScope scope);
  381. public abstract IXPathNavigator MoveToNextSibling ();
  382. public abstract IXPathNavigator MoveToNextValue ();
  383. public abstract IXPathNavigator MoveToParent ();
  384. public virtual IXPathNavigator MoveToPreviousSibling ()
  385. {
  386. IXPathNavigator backup = Clone ();
  387. IXPathNavigator tmp = MoveToParent ();
  388. if (tmp == null)
  389. return null;
  390. tmp = MoveToFirstChild ();
  391. do {
  392. if (tmp.IsSamePosition (backup))
  393. return this;
  394. tmp = MoveToNextSibling ();
  395. } while (tmp != null);
  396. return null;
  397. }
  398. public virtual IXPathNavigator MoveToRoot ()
  399. {
  400. IXPathNavigator tmp = MoveToParent ();
  401. while (tmp != null)
  402. MoveToParent ();
  403. return this;
  404. }
  405. public virtual XPathNavigator2 MoveToSibling (string localName, string namespaceName)
  406. {
  407. return (XPathNavigator2) MoveToSibling (localName, namespaceName, false);
  408. }
  409. public virtual IXPathNavigator MoveToSibling (string localName, string namespaceName, bool atomizedNames)
  410. {
  411. return MoveToSibling (localName, namespaceName, atomizedNames, XmlInfoItemType.Document, false);
  412. }
  413. public virtual IXPathNavigator MoveToSibling (XmlInfoItemType type)
  414. {
  415. return MoveToSibling (null, null, false, type, true);
  416. }
  417. private IXPathNavigator MoveToSibling (string localName, string namespaceName, bool atomizedNames, XmlInfoItemType type, bool byType)
  418. {
  419. XmlInfoItemType cacheItemType = ItemType;
  420. string currentName = LocalName;
  421. string currentNs = Namespace;
  422. IXPathNavigator tmp = MoveToNextSibling ();
  423. if (tmp == null)
  424. return null;
  425. int count = 0;
  426. while (tmp != null) {
  427. count++;
  428. if (byType) {
  429. if (type == ItemType)
  430. return this;
  431. } else if (atomizedNames) {
  432. if (Object.ReferenceEquals (LocalName, localName) && Object.ReferenceEquals (Namespace, namespaceName))
  433. return this;
  434. } else {
  435. if (LocalName == localName && Namespace == namespaceName)
  436. return this;
  437. }
  438. tmp = MoveToNextSibling ();
  439. }
  440. for (int i = 0; i < count; i++)
  441. MoveToPreviousSibling ();
  442. return null;
  443. }
  444. public virtual bool ReadboolValue ()
  445. {
  446. throw new NotImplementedException ();
  447. }
  448. public virtual byte ReadByteValue ()
  449. {
  450. throw new NotImplementedException ();
  451. }
  452. public virtual char ReadCharValue ()
  453. {
  454. throw new NotImplementedException ();
  455. }
  456. public virtual DateTime ReadDateTimeValue ()
  457. {
  458. throw new NotImplementedException ();
  459. }
  460. public virtual decimal ReadDecimalValue ()
  461. {
  462. throw new NotImplementedException ();
  463. }
  464. public virtual double ReadDoubleValue ()
  465. {
  466. throw new NotImplementedException ();
  467. }
  468. public virtual short ReadInt16Value ()
  469. {
  470. throw new NotImplementedException ();
  471. }
  472. public virtual int ReadintValue ()
  473. {
  474. throw new NotImplementedException ();
  475. }
  476. public virtual long ReadInt64Value ()
  477. {
  478. throw new NotImplementedException ();
  479. }
  480. public virtual float ReadSingleValue ()
  481. {
  482. throw new NotImplementedException ();
  483. }
  484. public virtual string ReadStringValue ()
  485. {
  486. throw new NotImplementedException ();
  487. }
  488. public virtual object ReadValue ()
  489. {
  490. throw new NotImplementedException ();
  491. }
  492. public virtual object ReadValue (Type type)
  493. {
  494. throw new NotImplementedException ();
  495. }
  496. // TODO
  497. // In the meantime, we should wait System.Data.SqlXml.dll became public.
  498. // public IEnumerable Select (XmlCommand command, XmlQueryArgumentList argList)
  499. // {
  500. // throw new NotImplementedException ();
  501. // }
  502. public IEnumerable Select (string query)
  503. {
  504. return Select (query, null);
  505. }
  506. public IEnumerable Select (string query, IXmlNamespaceResolver namespaceResolver)
  507. {
  508. // TODO: check the true default dialect
  509. return Select (query, namespaceResolver, XmlQueryDialect.XPath1);
  510. }
  511. // TODO
  512. public IEnumerable Select (string query, IXmlNamespaceResolver namespaceResolver, XmlQueryDialect dialect)
  513. {
  514. throw new NotImplementedException ();
  515. }
  516. // TODO
  517. // In the meantime, we should wait System.Data.SqlXml.dll became public.
  518. // public object SelectSingleValue (XmlCommand command, XmlQueryArgumentList argList)
  519. // {
  520. // throw new NotImplementedException ();
  521. // }
  522. public object SelectSingleValue (string query)
  523. {
  524. return SelectSingleValue (query, null);
  525. }
  526. public object SelectSingleValue (string query, IXmlNamespaceResolver namespaceResolver)
  527. {
  528. return SelectSingleValue (query, namespaceResolver, XmlQueryDialect.XPath1);
  529. }
  530. // TODO
  531. public object SelectSingleValue (string query, IXmlNamespaceResolver namespaceResolver, XmlQueryDialect dialect)
  532. {
  533. throw new NotImplementedException ();
  534. }
  535. }
  536. }
  537. #endif