| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- //
- // System.Xml.XmlNode
- //
- // Author:
- // Kral Ferch <[email protected]>
- //
- // (C) 2002 Kral Ferch
- //
- using System;
- using System.Collections;
- using System.IO;
- using System.Text;
- using System.Xml.XPath;
- namespace System.Xml
- {
- public abstract class XmlNode : ICloneable, IEnumerable, IXPathNavigable
- {
- #region Fields
- XmlDocument ownerDocument;
- XmlNode parentNode;
- #endregion
- #region Constructors
- internal XmlNode (XmlDocument ownerDocument)
- {
- this.ownerDocument = ownerDocument;
- }
- #endregion
- #region Properties
- public virtual XmlAttributeCollection Attributes
- {
- get { return null; }
- }
- public virtual string BaseURI
- {
- get { return ParentNode.BaseURI; }
- }
- public virtual XmlNodeList ChildNodes {
- get {
- return new XmlNodeListChildren(this);
- }
- }
- public virtual XmlNode FirstChild {
- get {
- if (LastChild != null) {
- return LastLinkedChild.NextLinkedSibling;
- }
- else {
- return null;
- }
- }
- }
- public virtual bool HasChildNodes {
- get { return LastChild != null; }
- }
- [MonoTODO]
- public virtual string InnerText {
- get {
- StringBuilder builder = new StringBuilder ();
- AppendChildValues (this, builder);
- return builder.ToString ();
- }
- set { throw new NotImplementedException (); }
- }
- private void AppendChildValues (XmlNode parent, StringBuilder builder)
- {
- XmlNode node = parent.FirstChild;
- while (node != null) {
- if (node.NodeType == XmlNodeType.Text)
- builder.Append (node.Value);
- AppendChildValues (node, builder);
- node = node.NextSibling;
- }
- }
- [MonoTODO("Setter.")]
- public virtual string InnerXml {
- get {
- StringWriter sw = new StringWriter ();
- XmlTextWriter xtw = new XmlTextWriter (sw);
- WriteContentTo(xtw);
- return sw.GetStringBuilder().ToString();
- }
- set { throw new NotImplementedException (); }
- }
- public virtual bool IsReadOnly {
- get { return false; }
- }
- [System.Runtime.CompilerServices.IndexerName("Item")]
- public virtual XmlElement this [string name] {
- get {
- foreach (XmlNode node in ChildNodes) {
- if ((node.NodeType == XmlNodeType.Element) &&
- (node.Name == name)) {
- return (XmlElement) node;
- }
- }
- return null;
- }
- }
- [System.Runtime.CompilerServices.IndexerName("Item")]
- public virtual XmlElement this [string localname, string ns] {
- get {
- foreach (XmlNode node in ChildNodes) {
- if ((node.NodeType == XmlNodeType.Element) &&
- (node.LocalName == localname) &&
- (node.NamespaceURI == ns)) {
- return (XmlElement) node;
- }
- }
- return null;
- }
- }
- public virtual XmlNode LastChild {
- get { return LastLinkedChild; }
- }
- internal virtual XmlLinkedNode LastLinkedChild {
- get { return null; }
- set { }
- }
- public abstract string LocalName { get; }
- public abstract string Name { get; }
- public virtual string NamespaceURI {
- get { return String.Empty; }
- }
- public virtual XmlNode NextSibling {
- get { return null; }
- }
- public abstract XmlNodeType NodeType { get; }
- public virtual string OuterXml {
- get {
- StringWriter sw = new StringWriter ();
- XmlTextWriter xtw = new XmlTextWriter (sw);
- WriteTo(xtw);
- return sw.GetStringBuilder().ToString();
- }
- }
- public virtual XmlDocument OwnerDocument {
- get { return ownerDocument; }
- }
- public virtual XmlNode ParentNode {
- get { return parentNode; }
- }
- public virtual string Prefix {
- get { return String.Empty; }
- set {}
- }
- public virtual XmlNode PreviousSibling {
- get { return null; }
- }
- public virtual string Value {
- get { return null; }
- set { throw new InvalidOperationException ("This node does not have a value"); }
- }
- #endregion
- #region Methods
- public virtual XmlNode AppendChild (XmlNode newChild)
- {
- XmlDocument ownerDoc = (NodeType == XmlNodeType.Document) ? (XmlDocument)this : OwnerDocument;
- ownerDoc.onNodeInserting (newChild, this);
- if (NodeType == XmlNodeType.Document || NodeType == XmlNodeType.Element || NodeType == XmlNodeType.Attribute) {
-
- if (newChild.OwnerDocument != ownerDoc)
- throw new ArgumentException ("Can't append a node created by another document.");
- XmlLinkedNode newLinkedChild = (XmlLinkedNode) newChild;
- XmlLinkedNode lastLinkedChild = LastLinkedChild;
- newLinkedChild.parentNode = this;
-
- if (lastLinkedChild != null) {
- newLinkedChild.NextLinkedSibling = lastLinkedChild.NextLinkedSibling;
- lastLinkedChild.NextLinkedSibling = newLinkedChild;
- } else
- newLinkedChild.NextLinkedSibling = newLinkedChild;
-
- LastLinkedChild = newLinkedChild;
- ownerDoc.onNodeInserted (newChild, newChild.ParentNode);
- return newChild;
- } else
- throw new InvalidOperationException();
- }
- [MonoTODO]
- public virtual XmlNode Clone ()
- {
- throw new NotImplementedException ();
- }
- public abstract XmlNode CloneNode (bool deep);
- [MonoTODO]
- public XPathNavigator CreateNavigator ()
- {
- return new XmlDocumentNavigator(this);
- }
- public IEnumerator GetEnumerator ()
- {
- return new XmlNodeListChildren(this).GetEnumerator();
- }
- [MonoTODO]
- public virtual string GetNamespaceOfPrefix (string prefix)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public virtual string GetPrefixOfNamespace (string namespaceURI)
- {
- throw new NotImplementedException ();
- }
- object ICloneable.Clone ()
- {
- return Clone ();
- }
- IEnumerator IEnumerable.GetEnumerator ()
- {
- return GetEnumerator ();
- }
- [MonoTODO]
- public virtual XmlNode InsertAfter (XmlNode newChild, XmlNode refChild)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public virtual XmlNode InsertBefore (XmlNode newChild, XmlNode refChild)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public virtual void Normalize ()
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public virtual XmlNode PrependChild (XmlNode newChild)
- {
- throw new NotImplementedException ();
- }
- public virtual void RemoveAll ()
- {
- XmlDocument ownerDoc = (NodeType == XmlNodeType.Document) ? (XmlDocument)this : OwnerDocument;
- ownerDoc.onNodeRemoving (this, this.ParentNode);
- LastLinkedChild = null;
- ownerDoc.onNodeRemoved (this, this.ParentNode);
- }
- public virtual XmlNode RemoveChild (XmlNode oldChild)
- {
- OwnerDocument.onNodeRemoving (oldChild, oldChild.ParentNode);
- if (NodeType == XmlNodeType.Document || NodeType == XmlNodeType.Element || NodeType == XmlNodeType.Attribute)
- {
- if (IsReadOnly)
- throw new ArgumentException();
- if (Object.ReferenceEquals(LastLinkedChild, LastLinkedChild.NextLinkedSibling) && Object.ReferenceEquals(LastLinkedChild, oldChild))
- LastLinkedChild = null;
- else {
- XmlLinkedNode oldLinkedChild = (XmlLinkedNode)oldChild;
- XmlLinkedNode beforeLinkedChild = LastLinkedChild;
-
- while (!Object.ReferenceEquals(beforeLinkedChild.NextLinkedSibling, LastLinkedChild) && !Object.ReferenceEquals(beforeLinkedChild.NextLinkedSibling, oldLinkedChild))
- beforeLinkedChild = beforeLinkedChild.NextLinkedSibling;
- if (!Object.ReferenceEquals(beforeLinkedChild.NextLinkedSibling, oldLinkedChild))
- throw new ArgumentException();
- beforeLinkedChild.NextLinkedSibling = oldLinkedChild.NextLinkedSibling;
- oldLinkedChild.NextLinkedSibling = null;
- }
- OwnerDocument.onNodeRemoved (oldChild, oldChild.ParentNode);
- return oldChild;
- }
- else
- throw new ArgumentException();
- }
- [MonoTODO]
- public virtual XmlNode ReplaceChild (XmlNode newChild, XmlNode oldChild)
- {
- throw new NotImplementedException ();
- }
- public XmlNodeList SelectNodes (string xpath)
- {
- return SelectNodes (xpath, null);
- }
- [MonoTODO]
- public XmlNodeList SelectNodes (string xpath, XmlNamespaceManager nsmgr)
- {
- XPathNavigator nav = CreateNavigator ();
- XPathExpression expr = nav.Compile (xpath);
- if (nsmgr != null)
- expr.SetContext (nsmgr);
- XPathNodeIterator iter = nav.Select (expr);
- ArrayList rgNodes = new ArrayList ();
- while (iter.MoveNext ())
- {
- rgNodes.Add (((XmlDocumentNavigator) iter.Current).Node);
- }
- return new XmlNodeArrayList (rgNodes);
- }
- public XmlNode SelectSingleNode (string xpath)
- {
- return SelectSingleNode (xpath, null);
- }
- [MonoTODO]
- public XmlNode SelectSingleNode (string xpath, XmlNamespaceManager nsmgr)
- {
- XPathNavigator nav = CreateNavigator ();
- XPathExpression expr = nav.Compile (xpath);
- if (nsmgr != null)
- expr.SetContext (nsmgr);
- XPathNodeIterator iter = nav.Select (expr);
- if (!iter.MoveNext ())
- return null;
- return ((XmlDocumentNavigator) iter.Current).Node;
- }
- internal void SetParentNode (XmlNode parent)
- {
- parentNode = parent;
- }
- [MonoTODO]
- public virtual bool Supports (string feature, string version)
- {
- throw new NotImplementedException ();
- }
- public abstract void WriteContentTo (XmlWriter w);
- public abstract void WriteTo (XmlWriter w);
- #endregion
- }
- }
|