| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- //
- // XPathEditableNavigator.cs
- //
- // Author:
- // Atsushi Enomoto <[email protected]>
- //
- // (C)2003 Atsushi Enomoto
- //
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- #if NET_2_0
- using System;
- using System.Collections;
- using System.Collections.Specialized;
- using System.ComponentModel;
- using System.IO;
- using System.Security.Policy;
- using System.Xml.Schema;
- using System.Xml.XPath;
- using Mono.Xml.XPath;
- //using MS.Internal.Xml;
- namespace System.Xml.XPath
- {
- public abstract class XPathEditableNavigator
- : XPathNavigator, IXPathEditable
- {
- protected XPathEditableNavigator ()
- {
- }
- [MonoTODO ("No implementation as yet")]
- public abstract XmlWriter AppendChild ();
- [MonoTODO]
- public virtual XPathEditableNavigator AppendChild (
- string xmlFragments)
- {
- // FIXME: should XmlParserContext be something?
- return AppendChild (new XmlTextReader (xmlFragments, XmlNodeType.Element, null));
- }
- [MonoTODO]
- public virtual XPathEditableNavigator AppendChild (
- XmlReader reader)
- {
- XmlWriter w = AppendChild ();
- w.WriteNode (reader, false);
- w.Close ();
- XPathEditableNavigator nav = (XPathEditableNavigator) Clone ();
- nav.MoveToFirstChild ();
- while (!nav.MoveToNext ())
- ;
- return nav;
- }
- [MonoTODO]
- public virtual XPathEditableNavigator AppendChild (
- XPathNavigator nav)
- {
- return AppendChild (new XPathNavigatorReader (nav));
- }
- public void AppendChildElement (string prefix, string name, string ns, string value)
- {
- XmlWriter xw = AppendChild ();
- xw.WriteStartElement (prefix, name, ns);
- xw.WriteString (value);
- xw.WriteEndElement ();
- xw.Close ();
- }
- [MonoTODO]
- protected void BuildSubTree (XmlReader reader, XmlWriter writer, bool useValidity)
- {
- throw new NotImplementedException ();
- }
- public virtual void CreateAttribute (string prefix, string localName, string namespaceURI, string value)
- {
- using (XmlWriter w = CreateAttributes ()) {
- w.WriteAttributeString (prefix, localName, namespaceURI, value);
- }
- }
- [MonoTODO ("No implementation as yet")]
- public abstract XmlWriter CreateAttributes ();
- public virtual XPathEditableNavigator CreateEditor ()
- {
- return (XPathEditableNavigator) Clone ();
- }
- // LAMESPEC: documented as public abstract, but it conflicts
- // with XPathNavigator.CreateNavigator ().
- /*
- [MonoTODO]
- public override XPathNavigator CreateNavigator ()
- {
- }
- */
- [MonoTODO ("No implementation as yet")]
- public abstract bool DeleteCurrent ();
- public virtual XmlWriter InsertAfter ()
- {
- XPathEditableNavigator nav = (XPathEditableNavigator) Clone ();
- if (nav.MoveToNext ())
- return nav.InsertBefore ();
- else
- return AppendChild ();
- }
- public virtual XPathEditableNavigator InsertAfter (string xmlFragments)
- {
- return InsertAfter (new XmlTextReader (xmlFragments, XmlNodeType.Element, null));
- }
- [MonoTODO]
- public virtual XPathEditableNavigator InsertAfter (XmlReader reader)
- {
- using (XmlWriter w = InsertAfter ()) {
- w.WriteNode (reader, false);
- }
- XPathEditableNavigator nav = (XPathEditableNavigator) Clone ();
- nav.MoveToNext ();
- return nav;
- }
- [MonoTODO]
- public virtual XPathEditableNavigator InsertAfter (XPathNavigator nav)
- {
- return InsertAfter (new XPathNavigatorReader (nav));
- }
- [MonoTODO ("No implementation as yet")]
- public abstract XmlWriter InsertBefore ();
- public virtual XPathEditableNavigator InsertBefore (string xmlFragments)
- {
- return InsertBefore (new XmlTextReader (xmlFragments, XmlNodeType.Element, null));
- }
- [MonoTODO]
- public virtual XPathEditableNavigator InsertBefore (XmlReader reader)
- {
- using (XmlWriter w = InsertBefore ()) {
- w.WriteNode (reader, false);
- }
- XPathEditableNavigator nav = (XPathEditableNavigator) Clone ();
- nav.MoveToPrevious ();
- return nav;
- }
- [MonoTODO]
- public virtual XPathEditableNavigator InsertBefore (XPathNavigator nav)
- {
- return InsertBefore (new XPathNavigatorReader (nav));
- }
- public virtual void InsertElementAfter (string prefix,
- string localName, string namespaceURI, string value)
- {
- using (XmlWriter w = InsertAfter ()) {
- w.WriteElementString (prefix, localName, namespaceURI, value);
- }
- }
- public virtual void InsertElementBefore (string prefix,
- string localName, string namespaceURI, string value)
- {
- using (XmlWriter w = InsertBefore ()) {
- w.WriteElementString (prefix, localName, namespaceURI, value);
- }
- }
- public virtual XmlWriter PrependChild ()
- {
- XPathEditableNavigator nav = (XPathEditableNavigator) Clone ();
- if (nav.MoveToFirstChild ())
- return nav.InsertBefore ();
- else
- return InsertBefore ();
- }
- public virtual XPathEditableNavigator PrependChild (string xmlFragments)
- {
- return PrependChild (new XmlTextReader (xmlFragments, XmlNodeType.Element, null));
- }
- [MonoTODO]
- public virtual XPathEditableNavigator PrependChild (XmlReader reader)
- {
- using (XmlWriter w = PrependChild ()) {
- w.WriteNode (reader, false);
- }
- XPathEditableNavigator nav = (XPathEditableNavigator) Clone ();
- nav.MoveToFirstChild ();
- return nav;
- }
- [MonoTODO]
- public virtual XPathEditableNavigator PrependChild (XPathNavigator nav)
- {
- return PrependChild (new XPathNavigatorReader (nav));
- }
- public virtual void PrependChildElement (string prefix,
- string localName, string namespaceURI, string value)
- {
- using (XmlWriter w = PrependChild ()) {
- w.WriteElementString (prefix, localName, namespaceURI, value);
- }
- }
- // Dunno the exact purpose, but maybe internal editor use
- [MonoTODO]
- public virtual void SetFromObject (object value)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO ("No implementation as yet")]
- public abstract void SetValue (object value);
- [MonoTODO]
- public virtual void Validate (XmlSchemaSet schemas, ValidationEventHandler handler)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public override string InnerXml {
- get {
- XmlReader r = ReadSubtree ();
- r.Read (); // start
- // skip the element itself (or will reach to
- // EOF if other than element) unless writing
- // doc itself
- int depth = r.Depth;
- if (NodeType != XPathNodeType.Root)
- r.Read ();
- StringWriter sw = new StringWriter ();
- XmlWriter xtw = XmlWriter.Create (sw);
- while (!r.EOF && r.Depth > depth)
- xtw.WriteNode (r, false);
- return sw.ToString ();
- }
- set {
- DeleteChildren ();
- if (NodeType == XPathNodeType.Attribute) {
- SetValue (value);
- return;
- }
- AppendChild (new XmlTextReader (value, XmlNodeType.Element, null));
- }
- }
- private void DeleteChildren ()
- {
- switch (NodeType) {
- case XPathNodeType.Namespace:
- throw new InvalidOperationException ("Removing namespace node content is not supported.");
- case XPathNodeType.Attribute:
- return;
- case XPathNodeType.Text:
- case XPathNodeType.SignificantWhitespace:
- case XPathNodeType.Whitespace:
- case XPathNodeType.ProcessingInstruction:
- case XPathNodeType.Comment:
- DeleteCurrent ();
- return;
- }
- if (!HasChildren)
- return;
- XPathEditableNavigator nav = (XPathEditableNavigator) Clone ();
- nav.MoveToFirstChild ();
- while (!nav.IsSamePosition (this))
- nav.DeleteCurrent ();
- }
- [MonoTODO]
- public override string OuterXml {
- get {
- StringWriter sw = new StringWriter ();
- XmlWriter xw = XmlWriter.Create (sw);
- WriteSubtree (xw);
- return sw.ToString ();
- }
- set {
- if (NodeType == XPathNodeType.Root) {
- InnerXml = value;
- return;
- }
- XPathEditableNavigator nav = (XPathEditableNavigator) Clone ();
- nav.MoveToParent ();
- DeleteCurrent ();
- nav.AppendChild (new XmlTextReader (value, XmlNodeType.Document, null));
- }
- }
- }
- }
- #endif
|