Bläddra i källkod

When will I ever remember to add my new files before committing?

svn path=/trunk/mcs/; revision=4972
Jason Diamond 23 år sedan
förälder
incheckning
a71db778e8

+ 310 - 0
mcs/class/System.XML/System.Xml/XmlDocumentNavigator.cs

@@ -0,0 +1,310 @@
+//
+// System.Xml.XmlDocumentNavigator
+//
+// Author:
+//   Jason Diamond <[email protected]>
+//
+// (C) 2002 Jason Diamond
+//
+
+using System;
+using System.Collections;
+using System.Xml;
+using System.Xml.XPath;
+
+namespace System.Xml
+{
+	internal class XmlDocumentNavigator : XPathNavigator
+	{
+		#region Constructors
+
+		[MonoTODO]
+		internal XmlDocumentNavigator(XmlNode node)
+		{
+			this.node = node;
+		}
+
+		#endregion
+
+		#region Fields
+
+		private XmlNode node;
+		private IEnumerator attributesEnumerator;
+
+		#endregion
+
+		#region Properties
+
+		[MonoTODO]
+		public override string BaseURI {
+			get {
+				throw new NotImplementedException ();
+			}
+		}
+
+		public override bool HasAttributes {
+			get {
+				if (node.Attributes != null)
+					foreach (XmlAttribute attribute in node.Attributes)
+						if (attribute.NamespaceURI != "http://www.w3.org/2000/xmlns/")
+							return true;
+				return false;
+			}
+		}
+
+		public override bool HasChildren {
+			get {
+				XPathNodeType nodeType = NodeType;
+				bool canHaveChildren = nodeType == XPathNodeType.Root || nodeType == XPathNodeType.Element;
+				return canHaveChildren && node.FirstChild != null;
+			}
+		}
+
+		public override bool IsEmptyElement {
+			get {
+				return node.NodeType == XmlNodeType.Element && !HasChildren;
+			}
+		}
+
+		public override string LocalName {
+			get {
+				XPathNodeType nodeType = NodeType;
+				bool canHaveName = 
+					nodeType == XPathNodeType.Element || 
+					nodeType == XPathNodeType.Attribute || 
+					nodeType == XPathNodeType.ProcessingInstruction ||
+					nodeType == XPathNodeType.Namespace;
+				return canHaveName ? node.LocalName : String.Empty;
+			}
+		}
+
+		public override string Name {
+			get {
+				XPathNodeType nodeType = NodeType;
+				bool canHaveName = 
+					nodeType == XPathNodeType.Element || 
+					nodeType == XPathNodeType.Attribute || 
+					nodeType == XPathNodeType.ProcessingInstruction ||
+					nodeType == XPathNodeType.Namespace;
+				return canHaveName ? node.Name : String.Empty;
+			}
+		}
+
+		public override string NamespaceURI {
+			get {
+				return node.NamespaceURI;
+			}
+		}
+
+		[MonoTODO]
+		public override XmlNameTable NameTable {
+			get {
+				throw new NotImplementedException ();
+			}
+		}
+
+		public override XPathNodeType NodeType {
+			get {
+				switch (node.NodeType) {
+				case XmlNodeType.Document:
+					return XPathNodeType.Root;
+				case XmlNodeType.Element:
+					return XPathNodeType.Element;
+				case XmlNodeType.Attribute:
+					return XPathNodeType.Attribute;
+				case XmlNodeType.Text:
+					return XPathNodeType.Text;
+				case XmlNodeType.Whitespace:
+					return XPathNodeType.Whitespace;
+				case XmlNodeType.SignificantWhitespace:
+					return XPathNodeType.SignificantWhitespace;
+				case XmlNodeType.Comment:
+					return XPathNodeType.Comment;
+				case XmlNodeType.ProcessingInstruction:
+					return XPathNodeType.ProcessingInstruction;
+				}
+				throw new InvalidOperationException ();
+			}
+		}
+
+		public override string Prefix {
+			get {
+				return node.Prefix;
+			}
+		}
+
+		public override string Value {
+			get {
+				switch (NodeType) {
+				case XPathNodeType.Attribute:
+					return node.Value;
+				case XPathNodeType.Element:
+					return node.InnerText;
+				case XPathNodeType.Comment:
+					return node.Value;
+				case XPathNodeType.ProcessingInstruction:
+					return node.Value;
+				case XPathNodeType.Text:
+					return node.Value;
+				case XPathNodeType.Whitespace:
+					return node.Value;
+				case XPathNodeType.SignificantWhitespace:
+					return node.Value;
+				case XPathNodeType.Root:
+					return node.InnerText;
+				}
+				return String.Empty;
+			}
+		}
+
+		[MonoTODO]
+		public override string XmlLang {
+			get {
+				throw new NotImplementedException ();
+			}
+		}
+
+		#endregion
+
+		#region Methods
+
+		[MonoTODO]
+		public override XPathNavigator Clone ()
+		{
+			throw new NotImplementedException ();
+		}
+
+		[MonoTODO]
+		public override string GetAttribute (string localName, string namespaceURI)
+		{
+			throw new NotImplementedException ();
+		}
+
+		[MonoTODO]
+		public override string GetNamespace (string name)
+		{
+			throw new NotImplementedException ();
+		}
+		
+		public override bool IsSamePosition (XPathNavigator other)
+		{
+			XmlDocumentNavigator otherDocumentNavigator = other as XmlDocumentNavigator;
+			if (otherDocumentNavigator != null)
+				return node == otherDocumentNavigator.node;
+			return false;
+		}
+
+		public override bool MoveTo (XPathNavigator other)
+		{
+			XmlDocumentNavigator otherDocumentNavigator = other as XmlDocumentNavigator;
+			if (otherDocumentNavigator != null) {
+				if (node.OwnerDocument == otherDocumentNavigator.node.OwnerDocument) {
+					node = otherDocumentNavigator.node;
+					return true;
+				}
+			}
+			return false;
+		}
+
+		[MonoTODO]
+		public override bool MoveToAttribute (string localName, string namespaceURI)
+		{
+			throw new NotImplementedException ();
+		}
+
+		public override bool MoveToFirst ()
+		{
+			if (node.NodeType != XmlNodeType.Attribute && node.ParentNode != null) {
+				node = node.ParentNode.FirstChild;
+				return true;
+			}
+			return false;
+		}
+
+		public override bool MoveToFirstAttribute ()
+		{
+			if (NodeType == XPathNodeType.Element) {
+				attributesEnumerator = node.Attributes.GetEnumerator ();
+				return MoveToNextAttribute ();
+			}
+			return false;
+		}
+
+		public override bool MoveToFirstChild ()
+		{
+			if (HasChildren) {
+				node = node.FirstChild;
+				return true;
+			}
+			return false;
+		}
+
+		[MonoTODO]
+		public override bool MoveToFirstNamespace (XPathNamespaceScope namespaceScope)
+		{
+			throw new NotImplementedException ();
+		}
+
+		[MonoTODO]
+		public override bool MoveToId (string id)
+		{
+			throw new NotImplementedException ();
+		}
+
+		[MonoTODO]
+		public override bool MoveToNamespace (string name)
+		{
+			throw new NotImplementedException ();
+		}
+
+		public override bool MoveToNext ()
+		{
+			if (node.NextSibling != null) {
+				node = node.NextSibling;
+				return true;
+			}
+			return false;
+		}
+
+		public override bool MoveToNextAttribute ()
+		{
+			if (attributesEnumerator != null && attributesEnumerator.MoveNext ()) {
+				node = attributesEnumerator.Current as XmlAttribute;
+				return true;
+			}
+			return false;
+		}
+
+		[MonoTODO]
+		public override bool MoveToNextNamespace (XPathNamespaceScope namespaceScope)
+		{
+			throw new NotImplementedException ();
+		}
+
+		public override bool MoveToParent ()
+		{
+			if (node.ParentNode != null) {
+				node = node.ParentNode;
+				return true;
+			}
+			return false;
+		}
+
+		public override bool MoveToPrevious ()
+		{
+			if (node.PreviousSibling != null) {
+				node = node.PreviousSibling;
+				return true;
+			}
+			return false;
+		}
+
+		public override void MoveToRoot ()
+		{
+			if (node.NodeType != XmlNodeType.Document)
+				node = node.OwnerDocument;
+		}
+
+		#endregion
+	}
+}

+ 172 - 0
mcs/class/System.XML/Test/XPathNavigatorTests.cs

@@ -0,0 +1,172 @@
+//
+// MonoTests.System.Xml.XPathNavigatorTests
+//
+// Author:
+//   Jason Diamond <[email protected]>
+//
+// (C) 2002 Jason Diamond
+//
+
+using System;
+using System.Xml;
+using System.Xml.XPath;
+
+using NUnit.Framework;
+
+namespace MonoTests.System.Xml
+{
+	public class XPathNavigatorTests : TestCase
+	{
+		public XPathNavigatorTests () : base ("MonoTests.System.Xml.XPathNavigatorTests testsuite") {}
+		public XPathNavigatorTests (string name) : base (name) {}
+
+		public void TestCreateNavigator ()
+		{
+			XmlDocument document = new XmlDocument ();
+			document.LoadXml ("<foo />");
+			XPathNavigator navigator = document.CreateNavigator ();
+			AssertNotNull (navigator);
+		}
+
+		public void TestPropertiesOnDocument ()
+		{
+			XmlDocument document = new XmlDocument ();
+			document.LoadXml ("<foo:bar xmlns:foo='#foo' />");
+			XPathNavigator navigator = document.CreateNavigator ();
+			
+			AssertEquals (XPathNodeType.Root, navigator.NodeType);
+			AssertEquals (String.Empty, navigator.Name);
+			AssertEquals (String.Empty, navigator.LocalName);
+			AssertEquals (String.Empty, navigator.NamespaceURI);
+			AssertEquals (String.Empty, navigator.Prefix);
+			Assert (!navigator.HasAttributes);
+			Assert (navigator.HasChildren);
+			Assert (!navigator.IsEmptyElement);
+		}
+
+		public void TestPropertiesOnElement ()
+		{
+			XmlDocument document = new XmlDocument ();
+			document.LoadXml ("<foo:bar xmlns:foo='#foo' />");
+			XPathNavigator navigator = document.DocumentElement.CreateNavigator ();
+			
+			AssertEquals (XPathNodeType.Element, navigator.NodeType);
+			AssertEquals ("foo:bar", navigator.Name);
+			AssertEquals ("bar", navigator.LocalName);
+			AssertEquals ("#foo", navigator.NamespaceURI);
+			AssertEquals ("foo", navigator.Prefix);
+			Assert (!navigator.HasAttributes);
+			Assert (!navigator.HasChildren);
+			Assert (navigator.IsEmptyElement);
+		}
+
+		public void TestPropertiesOnAttribute ()
+		{
+			XmlDocument document = new XmlDocument ();
+			document.LoadXml ("<foo bar:baz='quux' xmlns:bar='#bar' />");
+			XPathNavigator navigator = document.DocumentElement.GetAttributeNode("baz", "#bar").CreateNavigator ();
+			
+			AssertEquals (XPathNodeType.Attribute, navigator.NodeType);
+			AssertEquals ("bar:baz", navigator.Name);
+			AssertEquals ("baz", navigator.LocalName);
+			AssertEquals ("#bar", navigator.NamespaceURI);
+			AssertEquals ("bar", navigator.Prefix);
+			Assert (!navigator.HasAttributes);
+			Assert (!navigator.HasChildren);
+			Assert (!navigator.IsEmptyElement);
+		}
+
+		public void TestNavigation ()
+		{
+			XmlDocument document = new XmlDocument ();
+			document.LoadXml ("<foo><bar /><baz /></foo>");
+			XPathNavigator navigator = document.DocumentElement.CreateNavigator ();
+			
+			AssertEquals ("foo", navigator.Name);
+			Assert (navigator.MoveToFirstChild ());
+			AssertEquals ("bar", navigator.Name);
+			Assert (navigator.MoveToNext ());
+			AssertEquals ("baz", navigator.Name);
+			Assert (!navigator.MoveToNext ());
+			AssertEquals ("baz", navigator.Name);
+			Assert (navigator.MoveToPrevious ());
+			AssertEquals ("bar", navigator.Name);
+			Assert (!navigator.MoveToPrevious ());
+			Assert (navigator.MoveToParent ());
+			AssertEquals ("foo", navigator.Name);
+			navigator.MoveToRoot ();
+			AssertEquals (XPathNodeType.Root, navigator.NodeType);
+			Assert (!navigator.MoveToParent ());
+			AssertEquals (XPathNodeType.Root, navigator.NodeType);
+			Assert (navigator.MoveToFirstChild ());
+			AssertEquals ("foo", navigator.Name);
+			Assert (navigator.MoveToFirst ());
+			AssertEquals ("foo", navigator.Name);
+			Assert (navigator.MoveToFirstChild ());
+			AssertEquals ("bar", navigator.Name);
+			Assert (navigator.MoveToNext ());
+			AssertEquals ("baz", navigator.Name);
+			Assert (navigator.MoveToFirst ());
+			AssertEquals ("bar", navigator.Name);
+		}
+
+		public void TestMoveToAndIsSamePosition ()
+		{
+			XmlDocument document1 = new XmlDocument ();
+			document1.LoadXml ("<foo><bar /></foo>");
+			XPathNavigator navigator1a = document1.DocumentElement.CreateNavigator ();
+			XPathNavigator navigator1b = document1.DocumentElement.CreateNavigator ();
+
+			XmlDocument document2 = new XmlDocument ();
+			document2.LoadXml ("<foo><bar /></foo>");
+			XPathNavigator navigator2 = document2.DocumentElement.CreateNavigator ();
+
+			AssertEquals ("foo", navigator1a.Name);
+			Assert (navigator1a.MoveToFirstChild ());
+			AssertEquals ("bar", navigator1a.Name);
+
+			Assert (!navigator1b.IsSamePosition (navigator1a));
+			AssertEquals ("foo", navigator1b.Name);
+			Assert (navigator1b.MoveTo (navigator1a));
+			Assert (navigator1b.IsSamePosition (navigator1a));
+			AssertEquals ("bar", navigator1b.Name);
+
+			Assert (!navigator2.IsSamePosition (navigator1a));
+			AssertEquals ("foo", navigator2.Name);
+			Assert (!navigator2.MoveTo (navigator1a));
+			AssertEquals ("foo", navigator2.Name);
+		}
+
+		public void TestAttributeNavigation ()
+		{
+			XmlDocument document = new XmlDocument ();
+			document.LoadXml ("<foo bar='baz' quux='quuux' />");
+			XPathNavigator navigator = document.DocumentElement.CreateNavigator ();
+
+			AssertEquals (XPathNodeType.Element, navigator.NodeType);
+			AssertEquals ("foo", navigator.Name);
+			Assert (navigator.MoveToFirstAttribute ());
+			AssertEquals (XPathNodeType.Attribute, navigator.NodeType);
+			AssertEquals ("bar", navigator.Name);
+			AssertEquals ("baz", navigator.Value);
+			Assert (navigator.MoveToNextAttribute ());
+			AssertEquals (XPathNodeType.Attribute, navigator.NodeType);
+			AssertEquals ("quux", navigator.Name);
+			AssertEquals ("quuux", navigator.Value);
+		}
+
+		public void TestElementAndRootValues()
+		{
+			XmlDocument document = new XmlDocument ();
+			document.LoadXml ("<foo><bar>baz</bar><quux>quuux</quux></foo>");
+			XPathNavigator navigator = document.DocumentElement.CreateNavigator ();
+
+			AssertEquals (XPathNodeType.Element, navigator.NodeType);
+			AssertEquals ("foo", navigator.Name);
+			//AssertEquals ("bazquuux", navigator.Value);
+
+			navigator.MoveToRoot ();
+			//AssertEquals ("bazquuux", navigator.Value);
+		}
+	}
+}