| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785 |
- //
- // Mono.Xml.XPath.XPathEditableDocument
- //
- // Author:
- // Atsushi Enomoto <[email protected]>
- //
- // (C)2004 Novell Inc.
- //
- // Yet another implementation of XPathEditableNavigator.
- // (Even runnable under MS.NET 2.0)
- //
- // By rewriting XPathEditableDocument.CreateNavigator() as just to
- // create XmlDocumentNavigator, XmlDocumentEditableNavigator could be used
- // as to implement XmlDocument.CreateNavigator().
- //
- //
- // 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.ComponentModel;
- using System.IO;
- using System.Xml;
- using System.Xml.Schema;
- using System.Xml.XPath;
- using System.Xml.Serialization;
- namespace Mono.Xml.XPath
- {
- public class XPathEditableDocument
- : IXPathNavigable, IXPathEditable,
- IRevertibleChangeTracking, IChangeTracking, IXmlSerializable
- {
- /*
- public static void Main ()
- {
- try {
- #if true
- XmlDocument doc = new XmlDocument ();
- XPathEditableDocument pd = new XPathEditableDocument (doc);
- XPathEditableNavigator nav = pd.CreateEditor ();
- IChangeTracking xp = pd;
- #else
- XPathDocument doc = new XPathDocument ();
- XPathEditableNavigator nav = doc.CreateEditor ();
- IChangeTracking xp = doc;
- #endif
- doc.LoadXml ("<root/>");
- nav.MoveToFirstChild (); // root
- XmlWriter w = nav.AppendChild ();
- Console.WriteLine (((IChangeTracking) xp).IsChanged);
- w.WriteElementString ("foo", "foo_text");
- w.WriteElementString ("bar", "bar_text");
- w.WriteStartElement ("hoge");
- w.WriteAttributeString ("fuga", "fugafuga");
- w.WriteAttributeString ("unya", "unyaunya");
- w.WriteFullEndElement ();
- w.Close ();
- w = nav.CreateAttributes ();
- w.WriteStartAttribute ("namara");
- w.WriteString ("mokera");
- w.WriteEndAttribute ();
- w.WriteAttributeString ("beccho", "giccho");
- w.Close ();
- nav.MoveToRoot ();
- nav.MoveToFirstChild ();
- nav.MoveToFirstChild ();
- nav.DeleteCurrent (); // delete foo
- Console.WriteLine (nav.Name);
- nav.MoveToNext ();
- Console.WriteLine (nav.Name);
- Console.WriteLine (nav.MoveToFirstAttribute ());
- nav.DeleteCurrent (); // delete fuga
- doc.Save (Console.Out);
- } catch (Exception ex) {
- Console.WriteLine (ex);
- }
- }
- */
- XmlDocument document;
- ArrayList changes = new ArrayList ();
- public XPathEditableDocument (XmlDocument doc)
- {
- document = doc;
- }
- public XPathNavigator CreateNavigator ()
- {
- return document.CreateNavigator ();
- }
- public XPathEditableNavigator CreateEditor ()
- {
- return new XmlDocumentEditableNavigator (this);
- }
- public XmlWriter CreateWriter ()
- {
- return CreateEditor ().AppendChild ();
- }
- public bool HasChanges ()
- {
- return IsChanged;
- }
- #region IRevertibleChangeTracking/IChangeTracking
- public bool IsChanged {
- get { return changes.Count != 0; }
- }
- public void AcceptChanges ()
- {
- changes.Clear ();
- }
- public void RejectChanges ()
- {
- for (int i = changes.Count - 1; i >= 0; i--) {
- Insertion ins = changes [i] as Insertion;
- if (ins != null) {
- ins.ParentNode.RemoveChild (ins.InsertedNode);
- continue;
- }
-
- Removal rem = changes [i] as Removal;
- if (rem != null) {
- if (rem.RemovedNode.NodeType == XmlNodeType.Attribute) {
- XmlElement el = (XmlElement) rem.OwnerNode;
- el.SetAttributeNode ((XmlAttribute) rem.RemovedNode);
- }
- else
- rem.OwnerNode.InsertBefore (rem.RemovedNode, rem.NextSibling);
- continue;
- }
- AttributeUpdate au = changes [i] as AttributeUpdate;
- if (au != null) {
- if (au.OldAttribute != null)
- au.Element.SetAttributeNode (au.OldAttribute);
- else
- au.Element.RemoveAttributeNode (au.NewAttribute);
- continue;
- }
- }
- changes.Clear ();
- }
- #endregion
- #region IXmlSerializable
- public void WriteXml (XmlWriter writer)
- {
- throw new NotImplementedException ();
- }
- public void ReadXml (XmlReader reader)
- {
- throw new NotImplementedException ();
- }
- public XmlSchema GetSchema ()
- {
- throw new NotImplementedException ();
- }
- #endregion
- internal bool DeleteNode (XmlNode node)
- {
- Removal rem = new Removal ();
- if (node.NodeType == XmlNodeType.Attribute) {
- XmlAttribute attr = node as XmlAttribute;
- rem.OwnerNode = attr.OwnerElement;
- rem.RemovedNode = node;
- attr.OwnerElement.RemoveAttributeNode (attr);
- return false;
- } else {
- rem.OwnerNode = node.ParentNode;
- rem.NextSibling = node.NextSibling;
- rem.RemovedNode = node;
- node.ParentNode.RemoveChild (node);
- return rem.NextSibling != null;
- }
- }
- internal XmlWriter CreateInsertionWriter (XmlNode owner, XmlNode previousSibling)
- {
- return new XmlDocumentInsertionWriter (owner, previousSibling, this);
- }
- internal XmlWriter CreateAttributesWriter (XmlNode owner)
- {
- return new XmlDocumentAttributeWriter (owner, this);
- }
- internal void AttributeUpdate (XmlElement element, XmlAttribute oldAttr, XmlAttribute newAttr)
- {
- AttributeUpdate au = new AttributeUpdate ();
- au.Element = element;
- au.OldAttribute = oldAttr;
- au.NewAttribute = newAttr;
- changes.Add (au);
- }
-
- internal void AppendChild (XmlNode parent, XmlNode child)
- {
- Insertion ins = new Insertion ();
- ins.ParentNode = parent;
- ins.InsertedNode = child;
- changes.Add (ins);
- }
- }
- public class XmlDocumentInsertionWriter : XmlWriter
- {
- XmlNode current;
- XmlNode previousSibling;
- XPathEditableDocument document;
- Stack nodeStack = new Stack ();
- public XmlDocumentInsertionWriter (XmlNode owner, XmlNode previousSibling, XPathEditableDocument doc)
- {
- this.current = (XmlNode) owner;
- if (current == null)
- throw new InvalidOperationException ();
- this.previousSibling = previousSibling;
- this.document = doc;
- state = WriteState.Content;
- }
- WriteState state;
- XmlAttribute attribute;
- public override WriteState WriteState {
- get { return state; }
- }
- public override void Close ()
- {
- }
- public override void Flush ()
- {
- }
- public override string LookupPrefix (string ns)
- {
- return current.GetPrefixOfNamespace (ns);
- }
- public override void WriteStartAttribute (string prefix, string name, string ns)
- {
- if (state != WriteState.Content)
- throw new InvalidOperationException ("Current state is not inside element. Cannot start attribute.");
- attribute = current.OwnerDocument.CreateAttribute (prefix, name, ns);
- state = WriteState.Attribute;
- }
- public override void WriteProcessingInstruction (string name, string value)
- {
- XmlProcessingInstruction pi = current.OwnerDocument.CreateProcessingInstruction (name, value);
- current.AppendChild (pi);
- document.AppendChild (current, pi);
- }
- public override void WriteComment (string text)
- {
- XmlComment comment = current.OwnerDocument.CreateComment (text);
- current.AppendChild (comment);
- document.AppendChild (current, comment);
- }
- public override void WriteCData (string text)
- {
- XmlCDataSection cdata = current.OwnerDocument.CreateCDataSection (text);
- current.AppendChild (cdata);
- document.AppendChild (current, cdata);
- }
- public override void WriteStartElement (string prefix, string name, string ns)
- {
- if (current.OwnerDocument == null)
- throw new Exception ("Should not happen.");
- XmlElement el = current.OwnerDocument.CreateElement (prefix, name, ns);
- current.AppendChild (el);
- document.AppendChild (current, el);
- nodeStack.Push (current);
- current = el;
- }
- public override void WriteEndElement ()
- {
- if (nodeStack.Count == 0)
- throw new InvalidOperationException ("No element is opened.");
- current = nodeStack.Pop () as XmlNode;
- }
- public override void WriteFullEndElement ()
- {
- WriteEndElement ();
- XmlElement el = current as XmlElement;
- if (el != null)
- el.IsEmpty = false;
- }
- public override void WriteDocType (string name, string pubid, string systemId, string intsubset)
- {
- throw new NotSupportedException ();
- }
- public override void WriteStartDocument ()
- {
- throw new NotSupportedException ();
- }
- public override void WriteStartDocument (bool standalone)
- {
- throw new NotSupportedException ();
- }
- public override void WriteEndDocument ()
- {
- throw new NotSupportedException ();
- }
- public override void WriteBase64 (byte [] data, int start, int length)
- {
- WriteString (Convert.ToBase64String (data, start, length));
- }
- public override void WriteRaw (char [] raw, int start, int length)
- {
- throw new NotSupportedException ();
- }
- public override void WriteRaw (string raw)
- {
- throw new NotSupportedException ();
- }
- public override void WriteSurrogateCharEntity (char msb, char lsb)
- {
- throw new NotSupportedException ();
- }
- public override void WriteCharEntity (char c)
- {
- throw new NotSupportedException ();
- }
- public override void WriteEntityRef (string entname)
- {
- if (state != WriteState.Attribute)
- throw new InvalidOperationException ("Current state is not inside attribute. Cannot write attribute value.");
- attribute.AppendChild (attribute.OwnerDocument.CreateEntityReference (entname));
- }
- public override void WriteChars (char [] data, int start, int length)
- {
- WriteString (new string (data, start, length));
- }
- public override void WriteString (string text)
- {
- if (attribute != null)
- attribute.Value += text;
- else
- current.AppendChild (current.OwnerDocument.CreateTextNode (text));
- }
- public override void WriteWhitespace (string text)
- {
- if (state != WriteState.Attribute)
- current.AppendChild (current.OwnerDocument.CreateTextNode (text));
- else if (attribute.ChildNodes.Count == 0)
- attribute.AppendChild (attribute.OwnerDocument.CreateWhitespace (text));
- else
- attribute.Value += text;
- }
- public override void WriteEndAttribute ()
- {
- XmlElement element = current as XmlElement;
- if (state != WriteState.Attribute || element == null)
- throw new InvalidOperationException ("Current state is not inside attribute. Cannot close attribute.");
- document.AttributeUpdate (element, element.SetAttributeNode (attribute), attribute);
- attribute = null;
- state = WriteState.Content;
- }
- }
- public class XmlDocumentAttributeWriter : XmlWriter
- {
- XmlElement element;
- XPathEditableDocument document;
- public XmlDocumentAttributeWriter (XmlNode owner, XPathEditableDocument doc)
- {
- element = owner as XmlElement;
- if (element == null)
- throw new ArgumentException ("To write attributes, current node must be an element.");
- state = WriteState.Content;
- document = doc;
- }
- WriteState state;
- XmlAttribute attribute;
- public override WriteState WriteState {
- get { return state; }
- }
- public override void Close ()
- {
- }
- public override void Flush ()
- {
- }
- public override string LookupPrefix (string ns)
- {
- return element.GetPrefixOfNamespace (ns);
- }
- public override void WriteStartAttribute (string prefix, string name, string ns)
- {
- if (state != WriteState.Content)
- throw new InvalidOperationException ("Current state is not inside element. Cannot start attribute.");
- attribute = element.OwnerDocument.CreateAttribute (prefix, name, ns);
- state = WriteState.Attribute;
- }
- public override void WriteProcessingInstruction (string name, string value)
- {
- throw new NotSupportedException ();
- }
- public override void WriteComment (string text)
- {
- throw new NotSupportedException ();
- }
- public override void WriteCData (string text)
- {
- throw new NotSupportedException ();
- }
- public override void WriteStartElement (string prefix, string name, string ns)
- {
- throw new NotSupportedException ();
- }
- public override void WriteEndElement ()
- {
- throw new NotSupportedException ();
- }
- public override void WriteFullEndElement ()
- {
- throw new NotSupportedException ();
- }
- public override void WriteDocType (string name, string pubid, string systemId, string intsubset)
- {
- throw new NotSupportedException ();
- }
- public override void WriteStartDocument ()
- {
- throw new NotSupportedException ();
- }
- public override void WriteStartDocument (bool standalone)
- {
- throw new NotSupportedException ();
- }
- public override void WriteEndDocument ()
- {
- throw new NotSupportedException ();
- }
- public override void WriteBase64 (byte [] data, int start, int length)
- {
- throw new NotSupportedException ();
- }
- public override void WriteRaw (char [] raw, int start, int length)
- {
- throw new NotSupportedException ();
- }
- public override void WriteRaw (string raw)
- {
- throw new NotSupportedException ();
- }
- public override void WriteSurrogateCharEntity (char msb, char lsb)
- {
- throw new NotSupportedException ();
- }
- public override void WriteCharEntity (char c)
- {
- throw new NotSupportedException ();
- }
- public override void WriteEntityRef (string entname)
- {
- if (state != WriteState.Attribute)
- throw new InvalidOperationException ("Current state is not inside attribute. Cannot write attribute value.");
- attribute.AppendChild (attribute.OwnerDocument.CreateEntityReference (entname));
- }
- public override void WriteChars (char [] data, int start, int length)
- {
- WriteString (new string (data, start, length));
- }
- public override void WriteString (string text)
- {
- if (state != WriteState.Attribute)
- throw new InvalidOperationException ("Current state is not inside attribute. Cannot write attribute value.");
- attribute.Value += text;
- }
- public override void WriteWhitespace (string text)
- {
- if (state != WriteState.Attribute)
- throw new InvalidOperationException ("Current state is not inside attribute. Cannot write attribute value.");
- if (attribute.ChildNodes.Count == 0)
- attribute.AppendChild (attribute.OwnerDocument.CreateWhitespace (text));
- else
- attribute.Value += text;
- }
- public override void WriteEndAttribute ()
- {
- if (state != WriteState.Attribute)
- throw new InvalidOperationException ("Current state is not inside attribute. Cannot close attribute.");
- document.AttributeUpdate (element, element.SetAttributeNode (attribute), attribute);
- attribute = null;
- state = WriteState.Content;
- }
- }
- public class Insertion
- {
- // AppendChild : last child / true
- // InsertBefore : current node / false
- // InsertAfter : current node / true
- // PrependChild : first child / false
- public XmlNode ParentNode;
- public XmlNode InsertedNode;
- public bool Afterward;
- }
- public class Removal
- {
- public XmlNode OwnerNode;
- public XmlNode NextSibling;
- public XmlNode RemovedNode;
- }
- public class AttributeUpdate
- {
- public XmlElement Element;
- public XmlAttribute NewAttribute;
- public XmlAttribute OldAttribute;
- }
- public class XmlDocumentEditableNavigator : XPathEditableNavigator, IHasXmlNode
- {
- XPathEditableDocument document;
- XPathNavigator navigator;
- public XmlDocumentEditableNavigator (XPathEditableDocument doc)
- {
- document = doc;
- navigator = doc.CreateNavigator ();
- }
- public XmlDocumentEditableNavigator (XmlDocumentEditableNavigator nav)
- {
- document = nav.document;
- navigator = nav.navigator.Clone ();
- }
- public override string BaseURI {
- get { return navigator.BaseURI; }
- }
- public override bool IsEmptyElement {
- get { return navigator.IsEmptyElement; }
- }
- public override string LocalName {
- get { return navigator.LocalName; }
- }
- public override XmlNameTable NameTable {
- get { return navigator.NameTable; }
- }
- public override string Name {
- get { return navigator.Name; }
- }
- public override string NamespaceURI {
- get { return navigator.NamespaceURI; }
- }
- public override XPathNodeType NodeType {
- get { return navigator.NodeType; }
- }
- public override string Prefix {
- get { return navigator.Prefix; }
- }
- public override string Value {
- get { return navigator.Value; }
- }
- public override XPathNavigator Clone ()
- {
- return new XmlDocumentEditableNavigator (this);
- }
- public override XPathNavigator CreateNavigator ()
- {
- return navigator.Clone ();
- }
- public XmlNode GetNode ()
- {
- return ((IHasXmlNode) navigator).GetNode ();
- }
- public override bool IsSamePosition (XPathNavigator other)
- {
- XmlDocumentEditableNavigator nav = other as XmlDocumentEditableNavigator;
- if (nav != null)
- return navigator.IsSamePosition (nav.navigator);
- else
- return navigator.IsSamePosition (nav);
- }
- public override bool MoveTo (XPathNavigator other)
- {
- XmlDocumentEditableNavigator nav = other as XmlDocumentEditableNavigator;
- if (nav != null)
- return navigator.MoveTo (nav.navigator);
- else
- return navigator.MoveTo (nav);
- }
- public override bool MoveToFirstAttribute ()
- {
- return navigator.MoveToFirstAttribute ();
- }
- public override bool MoveToFirstChild ()
- {
- return navigator.MoveToFirstChild ();
- }
- public override bool MoveToFirstNamespace (XPathNamespaceScope scope)
- {
- return navigator.MoveToFirstNamespace (scope);
- }
- public override bool MoveToId (string id)
- {
- return navigator.MoveToId (id);
- }
- public override bool MoveToNext ()
- {
- return navigator.MoveToNext ();
- }
- public override bool MoveToNextAttribute ()
- {
- return navigator.MoveToNextAttribute ();
- }
- public override bool MoveToNextNamespace (XPathNamespaceScope scope)
- {
- return navigator.MoveToNextNamespace (scope);
- }
- public override bool MoveToParent ()
- {
- return navigator.MoveToParent ();
- }
- public override bool MoveToPrevious ()
- {
- return navigator.MoveToPrevious ();
- }
- public override void Validate (XmlSchemaSet schemas, ValidationEventHandler handler)
- {
- throw new NotImplementedException ();
- /*
- // FIXME: use handler
- XmlReaderSettings settings = new XmlReaderSettings ();
- settings.Schemas.Add (schemas);
- settings.NameTable = this.NameTable;
- settings.XsdValidate = true;
- settings.DtdValidate = false;
- XmlReader xvr = XmlReader.Create (new XPathNavigatorReader (this), settings);
- while (!xvr.EOF)
- xvr.Read ();
- */
- }
- public override XmlWriter AppendChild ()
- {
- XmlNode n = ((IHasXmlNode) navigator).GetNode ();
- if (n == null)
- throw new InvalidOperationException ("Should not happen.");
- return document.CreateInsertionWriter (n, null);
- }
- public override XmlWriter InsertBefore ()
- {
- XmlNode n = ((IHasXmlNode) navigator).GetNode ();
- return document.CreateInsertionWriter (n.ParentNode, n.PreviousSibling);
- }
- public override XmlWriter CreateAttributes ()
- {
- XmlNode n = ((IHasXmlNode) navigator).GetNode ();
- return document.CreateInsertionWriter (n, null);
- }
- public override bool DeleteCurrent ()
- {
- XmlNode n = ((IHasXmlNode) navigator).GetNode ();
- if (!navigator.MoveToNext ())
- navigator.MoveToParent ();
- return document.DeleteNode (n);
- }
- public override void SetValue (object value)
- {
- XmlNode n = ((IHasXmlNode) navigator).GetNode ();
- foreach (XmlNode c in n.ChildNodes)
- document.DeleteNode (c);
- XmlWriter w = document.CreateInsertionWriter (n, null);
- // FIXME: Hmm, it does not look like using it.
- w.WriteFromObject (value);
- w.Close ();
- }
- }
- }
- #endif
|