XPathEditableNavigator.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. [MonoTODO ("No implementation as yet")]
  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. 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. [MonoTODO ("No implementation as yet")]
  96. public abstract XmlWriter CreateAttributes ();
  97. public virtual XPathEditableNavigator CreateEditor ()
  98. {
  99. return (XPathEditableNavigator) Clone ();
  100. }
  101. // LAMESPEC: documented as public abstract, but it conflicts
  102. // with XPathNavigator.CreateNavigator ().
  103. /*
  104. [MonoTODO]
  105. public override XPathNavigator CreateNavigator ()
  106. {
  107. }
  108. */
  109. [MonoTODO ("No implementation as yet")]
  110. public abstract bool DeleteCurrent ();
  111. public virtual XmlWriter InsertAfter ()
  112. {
  113. XPathEditableNavigator nav = (XPathEditableNavigator) Clone ();
  114. if (nav.MoveToNext ())
  115. return nav.InsertBefore ();
  116. else
  117. return AppendChild ();
  118. }
  119. public virtual XPathEditableNavigator InsertAfter (string xmlFragments)
  120. {
  121. return InsertAfter (new XmlTextReader (xmlFragments, XmlNodeType.Element, null));
  122. }
  123. [MonoTODO]
  124. public virtual XPathEditableNavigator InsertAfter (XmlReader reader)
  125. {
  126. using (XmlWriter w = InsertAfter ()) {
  127. w.WriteNode (reader, false);
  128. }
  129. XPathEditableNavigator nav = (XPathEditableNavigator) Clone ();
  130. nav.MoveToNext ();
  131. return nav;
  132. }
  133. [MonoTODO]
  134. public virtual XPathEditableNavigator InsertAfter (XPathNavigator nav)
  135. {
  136. return InsertAfter (new XPathNavigatorReader (nav));
  137. }
  138. [MonoTODO ("No implementation as yet")]
  139. public abstract XmlWriter InsertBefore ();
  140. public virtual XPathEditableNavigator InsertBefore (string xmlFragments)
  141. {
  142. return InsertBefore (new XmlTextReader (xmlFragments, XmlNodeType.Element, null));
  143. }
  144. [MonoTODO]
  145. public virtual XPathEditableNavigator InsertBefore (XmlReader reader)
  146. {
  147. using (XmlWriter w = InsertBefore ()) {
  148. w.WriteNode (reader, false);
  149. }
  150. XPathEditableNavigator nav = (XPathEditableNavigator) Clone ();
  151. nav.MoveToPrevious ();
  152. return nav;
  153. }
  154. [MonoTODO]
  155. public virtual XPathEditableNavigator InsertBefore (XPathNavigator nav)
  156. {
  157. return InsertBefore (new XPathNavigatorReader (nav));
  158. }
  159. public virtual void InsertElementAfter (string prefix,
  160. string localName, string namespaceURI, string value)
  161. {
  162. using (XmlWriter w = InsertAfter ()) {
  163. w.WriteElementString (prefix, localName, namespaceURI, value);
  164. }
  165. }
  166. public virtual void InsertElementBefore (string prefix,
  167. string localName, string namespaceURI, string value)
  168. {
  169. using (XmlWriter w = InsertBefore ()) {
  170. w.WriteElementString (prefix, localName, namespaceURI, value);
  171. }
  172. }
  173. public virtual XmlWriter PrependChild ()
  174. {
  175. XPathEditableNavigator nav = (XPathEditableNavigator) Clone ();
  176. if (nav.MoveToFirstChild ())
  177. return nav.InsertBefore ();
  178. else
  179. return InsertBefore ();
  180. }
  181. public virtual XPathEditableNavigator PrependChild (string xmlFragments)
  182. {
  183. return PrependChild (new XmlTextReader (xmlFragments, XmlNodeType.Element, null));
  184. }
  185. [MonoTODO]
  186. public virtual XPathEditableNavigator PrependChild (XmlReader reader)
  187. {
  188. using (XmlWriter w = PrependChild ()) {
  189. w.WriteNode (reader, false);
  190. }
  191. XPathEditableNavigator nav = (XPathEditableNavigator) Clone ();
  192. nav.MoveToFirstChild ();
  193. return nav;
  194. }
  195. [MonoTODO]
  196. public virtual XPathEditableNavigator PrependChild (XPathNavigator nav)
  197. {
  198. return PrependChild (new XPathNavigatorReader (nav));
  199. }
  200. public virtual void PrependChildElement (string prefix,
  201. string localName, string namespaceURI, string value)
  202. {
  203. using (XmlWriter w = PrependChild ()) {
  204. w.WriteElementString (prefix, localName, namespaceURI, value);
  205. }
  206. }
  207. // Dunno the exact purpose, but maybe internal editor use
  208. [MonoTODO]
  209. public virtual void SetFromObject (object value)
  210. {
  211. throw new NotImplementedException ();
  212. }
  213. [MonoTODO ("No implementation as yet")]
  214. public abstract void SetValue (object value);
  215. [MonoTODO]
  216. public virtual void Validate (XmlSchemaSet schemas, ValidationEventHandler handler)
  217. {
  218. throw new NotImplementedException ();
  219. }
  220. [MonoTODO]
  221. public override string InnerXml {
  222. get {
  223. XmlReader r = ReadSubtree ();
  224. r.Read (); // start
  225. // skip the element itself (or will reach to
  226. // EOF if other than element) unless writing
  227. // doc itself
  228. int depth = r.Depth;
  229. if (NodeType != XPathNodeType.Root)
  230. r.Read ();
  231. StringWriter sw = new StringWriter ();
  232. XmlWriter xtw = XmlWriter.Create (sw);
  233. while (!r.EOF && r.Depth > depth)
  234. xtw.WriteNode (r, false);
  235. return sw.ToString ();
  236. }
  237. set {
  238. DeleteChildren ();
  239. if (NodeType == XPathNodeType.Attribute) {
  240. SetValue (value);
  241. return;
  242. }
  243. AppendChild (new XmlTextReader (value, XmlNodeType.Element, null));
  244. }
  245. }
  246. private void DeleteChildren ()
  247. {
  248. switch (NodeType) {
  249. case XPathNodeType.Namespace:
  250. throw new InvalidOperationException ("Removing namespace node content is not supported.");
  251. case XPathNodeType.Attribute:
  252. return;
  253. case XPathNodeType.Text:
  254. case XPathNodeType.SignificantWhitespace:
  255. case XPathNodeType.Whitespace:
  256. case XPathNodeType.ProcessingInstruction:
  257. case XPathNodeType.Comment:
  258. DeleteCurrent ();
  259. return;
  260. }
  261. if (!HasChildren)
  262. return;
  263. XPathEditableNavigator nav = (XPathEditableNavigator) Clone ();
  264. nav.MoveToFirstChild ();
  265. while (!nav.IsSamePosition (this))
  266. nav.DeleteCurrent ();
  267. }
  268. [MonoTODO]
  269. public override string OuterXml {
  270. get {
  271. StringWriter sw = new StringWriter ();
  272. XmlWriter xw = XmlWriter.Create (sw);
  273. WriteSubtree (xw);
  274. return sw.ToString ();
  275. }
  276. set {
  277. if (NodeType == XPathNodeType.Root) {
  278. InnerXml = value;
  279. return;
  280. }
  281. XPathEditableNavigator nav = (XPathEditableNavigator) Clone ();
  282. nav.MoveToParent ();
  283. DeleteCurrent ();
  284. nav.AppendChild (new XmlTextReader (value, XmlNodeType.Document, null));
  285. }
  286. }
  287. }
  288. }
  289. #endif