XPathEditableNavigator.cs 8.5 KB

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