XPathEditableNavigator.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. //
  2. // XPathEditableNavigator.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // (C)2003 Atsushi Enomoto
  8. //
  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. #if NET_2_0
  30. using System;
  31. using System.Collections;
  32. using System.Collections.Specialized;
  33. using System.ComponentModel;
  34. using System.IO;
  35. using System.Security.Policy;
  36. using System.Xml.Schema;
  37. using System.Xml.XPath;
  38. using Mono.Xml.XPath;
  39. //using MS.Internal.Xml;
  40. namespace System.Xml.XPath
  41. {
  42. public abstract class XPathEditableNavigator
  43. : XPathNavigator//, IXPathEditable
  44. {
  45. protected XPathEditableNavigator ()
  46. {
  47. }
  48. #if false
  49. public abstract XmlWriter AppendChild ();
  50. [MonoTODO]
  51. public virtual XPathEditableNavigator AppendChild (
  52. string xmlFragments)
  53. {
  54. // FIXME: should XmlParserContext be something?
  55. return AppendChild (new XmlTextReader (xmlFragments, XmlNodeType.Element, null));
  56. }
  57. [MonoTODO]
  58. public virtual XPathEditableNavigator AppendChild (
  59. XmlReader reader)
  60. {
  61. XmlWriter w = AppendChild ();
  62. while (!reader.EOF)
  63. w.WriteNode (reader, false);
  64. w.Close ();
  65. XPathEditableNavigator nav = (XPathEditableNavigator) Clone ();
  66. nav.MoveToFirstChild ();
  67. while (nav.MoveToNext ())
  68. ;
  69. return nav;
  70. }
  71. [MonoTODO]
  72. public virtual XPathEditableNavigator AppendChild (
  73. XPathNavigator nav)
  74. {
  75. return AppendChild (new XPathNavigatorReader (nav));
  76. }
  77. public void AppendChildElement (string prefix, string name, string ns, string value)
  78. {
  79. XmlWriter xw = AppendChild ();
  80. xw.WriteStartElement (prefix, name, ns);
  81. xw.WriteString (value);
  82. xw.WriteEndElement ();
  83. xw.Close ();
  84. }
  85. [MonoTODO]
  86. protected void BuildSubTree (XmlReader reader, XmlWriter writer, bool useValidity)
  87. {
  88. throw new NotImplementedException ();
  89. }
  90. public virtual void CreateAttribute (string prefix, string localName, string namespaceURI, string value)
  91. {
  92. using (XmlWriter w = CreateAttributes ()) {
  93. w.WriteAttributeString (prefix, localName, namespaceURI, value);
  94. }
  95. }
  96. public abstract XmlWriter CreateAttributes ();
  97. /*
  98. public virtual XPathEditableNavigator CreateEditor ()
  99. {
  100. return (XPathEditableNavigator) Clone ();
  101. }
  102. // LAMESPEC: documented as public abstract, but it conflicts
  103. // with XPathNavigator.CreateNavigator ().
  104. [MonoTODO]
  105. public override XPathNavigator CreateNavigator ()
  106. {
  107. }
  108. */
  109. public abstract bool DeleteCurrent ();
  110. public virtual XmlWriter InsertAfter ()
  111. {
  112. XPathEditableNavigator nav = (XPathEditableNavigator) Clone ();
  113. if (nav.MoveToNext ())
  114. return nav.InsertBefore ();
  115. else
  116. return AppendChild ();
  117. }
  118. public virtual XPathEditableNavigator InsertAfter (string xmlFragments)
  119. {
  120. return InsertAfter (new XmlTextReader (xmlFragments, XmlNodeType.Element, null));
  121. }
  122. [MonoTODO]
  123. public virtual XPathEditableNavigator InsertAfter (XmlReader reader)
  124. {
  125. using (XmlWriter w = InsertAfter ()) {
  126. w.WriteNode (reader, false);
  127. }
  128. XPathEditableNavigator nav = (XPathEditableNavigator) Clone ();
  129. nav.MoveToNext ();
  130. return nav;
  131. }
  132. [MonoTODO]
  133. public virtual XPathEditableNavigator InsertAfter (XPathNavigator nav)
  134. {
  135. return InsertAfter (new XPathNavigatorReader (nav));
  136. }
  137. public abstract XmlWriter InsertBefore ();
  138. public virtual XPathEditableNavigator InsertBefore (string xmlFragments)
  139. {
  140. return InsertBefore (new XmlTextReader (xmlFragments, XmlNodeType.Element, null));
  141. }
  142. [MonoTODO]
  143. public virtual XPathEditableNavigator InsertBefore (XmlReader reader)
  144. {
  145. using (XmlWriter w = InsertBefore ()) {
  146. w.WriteNode (reader, false);
  147. }
  148. XPathEditableNavigator nav = (XPathEditableNavigator) Clone ();
  149. nav.MoveToPrevious ();
  150. return nav;
  151. }
  152. [MonoTODO]
  153. public virtual XPathEditableNavigator InsertBefore (XPathNavigator nav)
  154. {
  155. return InsertBefore (new XPathNavigatorReader (nav));
  156. }
  157. public virtual void InsertElementAfter (string prefix,
  158. string localName, string namespaceURI, string value)
  159. {
  160. using (XmlWriter w = InsertAfter ()) {
  161. w.WriteElementString (prefix, localName, namespaceURI, value);
  162. }
  163. }
  164. public virtual void InsertElementBefore (string prefix,
  165. string localName, string namespaceURI, string value)
  166. {
  167. using (XmlWriter w = InsertBefore ()) {
  168. w.WriteElementString (prefix, localName, namespaceURI, value);
  169. }
  170. }
  171. public virtual XmlWriter PrependChild ()
  172. {
  173. XPathEditableNavigator nav = (XPathEditableNavigator) Clone ();
  174. if (nav.MoveToFirstChild ())
  175. return nav.InsertBefore ();
  176. else
  177. return InsertBefore ();
  178. }
  179. public virtual XPathEditableNavigator PrependChild (string xmlFragments)
  180. {
  181. return PrependChild (new XmlTextReader (xmlFragments, XmlNodeType.Element, null));
  182. }
  183. [MonoTODO]
  184. public virtual XPathEditableNavigator PrependChild (XmlReader reader)
  185. {
  186. using (XmlWriter w = PrependChild ()) {
  187. w.WriteNode (reader, false);
  188. }
  189. XPathEditableNavigator nav = (XPathEditableNavigator) Clone ();
  190. nav.MoveToFirstChild ();
  191. return nav;
  192. }
  193. [MonoTODO]
  194. public virtual XPathEditableNavigator PrependChild (XPathNavigator nav)
  195. {
  196. return PrependChild (new XPathNavigatorReader (nav));
  197. }
  198. public virtual void PrependChildElement (string prefix,
  199. string localName, string namespaceURI, string value)
  200. {
  201. using (XmlWriter w = PrependChild ()) {
  202. w.WriteElementString (prefix, localName, namespaceURI, value);
  203. }
  204. }
  205. // Dunno the exact purpose, but maybe internal editor use
  206. [MonoTODO]
  207. public virtual void SetFromObject (object value)
  208. {
  209. throw new NotImplementedException ();
  210. }
  211. public abstract void SetValue (object value);
  212. [MonoTODO]
  213. public virtual void Validate (XmlSchemaSet schemas, ValidationEventHandler handler)
  214. {
  215. throw new NotImplementedException ();
  216. }
  217. [MonoTODO]
  218. // It seems like setter is virtual but getter is overriden.
  219. public virtual new string InnerXml {
  220. set {
  221. DeleteChildren ();
  222. if (NodeType == XPathNodeType.Attribute) {
  223. SetValue (value);
  224. return;
  225. }
  226. AppendChild (new XmlTextReader (value, XmlNodeType.Element, null));
  227. }
  228. }
  229. private void DeleteChildren ()
  230. {
  231. switch (NodeType) {
  232. case XPathNodeType.Namespace:
  233. throw new InvalidOperationException ("Removing namespace node content is not supported.");
  234. case XPathNodeType.Attribute:
  235. return;
  236. case XPathNodeType.Text:
  237. case XPathNodeType.SignificantWhitespace:
  238. case XPathNodeType.Whitespace:
  239. case XPathNodeType.ProcessingInstruction:
  240. case XPathNodeType.Comment:
  241. DeleteCurrent ();
  242. return;
  243. }
  244. if (!HasChildren)
  245. return;
  246. XPathEditableNavigator nav = (XPathEditableNavigator) Clone ();
  247. nav.MoveToFirstChild ();
  248. while (!nav.IsSamePosition (this))
  249. nav.DeleteCurrent ();
  250. }
  251. [MonoTODO]
  252. // It seems like setter is virtual but getter is overriden.
  253. public virtual new string OuterXml {
  254. get {
  255. return base.OuterXml;
  256. }
  257. set {
  258. switch (NodeType) {
  259. case XPathNodeType.Root:
  260. case XPathNodeType.Attribute:
  261. case XPathNodeType.Namespace:
  262. throw new XmlException ("Setting OuterXml Root, Attribute and Namespace is not supported.");
  263. }
  264. // XPathEditableNavigator nav = (XPathEditableNavigator) Clone ();
  265. // nav.MoveToParent ();
  266. // MoveToParent ();
  267. DeleteCurrent ();
  268. AppendChild (value);
  269. MoveToFirstChild ();
  270. }
  271. }
  272. #endif
  273. }
  274. }
  275. #endif