Merge pull request #554 from deplinenoise/ppc_fixes
[mono.git] / mcs / class / System.Xml.Linq / System.Xml.Linq / XNodeNavigator.cs
index 4e27b6f96d29ea0b21b18c8b54c5a509a9690641..55702bd1c265d7aeb4a4f298f8a81a5723e31659 100644 (file)
@@ -66,13 +66,22 @@ namespace System.Xml.Linq
 
                public override bool HasAttributes {
                        get {
+                               if (attr != null)
+                                       return false;
                                XElement el = node as XElement;
-                               return el != null && el.HasAttributes;
+                               if (el == null)
+                                       return false;
+                               foreach (var at in el.Attributes ())
+                                       if (!at.IsNamespaceDeclaration)
+                                               return true;
+                               return false;
                        }
                }
 
                public override bool HasChildren {
                        get {
+                               if (attr != null)
+                                       return false;
                                XContainer c = node as XContainer;
                                return c != null && c.FirstNode != null;
                        }
@@ -80,6 +89,8 @@ namespace System.Xml.Linq
 
                public override bool IsEmptyElement {
                        get {
+                               if (attr != null)
+                                       return false;
                                XElement el = node as XElement;
                                return el != null && el.IsEmpty;
                        }
@@ -89,7 +100,7 @@ namespace System.Xml.Linq
                        get {
                                switch (NodeType) {
                                case XPathNodeType.Namespace:
-                                       return attr.Name.Namespace == XNamespace.Blank ? String.Empty : attr.Name.LocalName;
+                                       return attr.Name.Namespace == XNamespace.None ? String.Empty : attr.Name.LocalName;
                                case XPathNodeType.Attribute:
                                        return attr.Name.LocalName;
                                case XPathNodeType.Element:
@@ -115,7 +126,7 @@ namespace System.Xml.Linq
                                default:
                                        return LocalName;
                                }
-                               if (name.Namespace == XNamespace.Blank)
+                               if (name.Namespace == XNamespace.None)
                                        return name.LocalName;
                                XElement el = (node as XElement) ?? node.Parent;
                                if (el == null)
@@ -128,8 +139,9 @@ namespace System.Xml.Linq
                public override string NamespaceURI {
                        get {
                                switch (NodeType) {
+                               case XPathNodeType.ProcessingInstruction:
                                case XPathNodeType.Namespace:
-                                       return attr.Value;
+                                       return String.Empty;
                                case XPathNodeType.Attribute:
                                        return attr.Name.NamespaceName;
                                case XPathNodeType.Element:
@@ -169,6 +181,9 @@ namespace System.Xml.Linq
                        get {
                                XName name = null;
                                switch (NodeType) {
+                               case XPathNodeType.ProcessingInstruction:
+                               case XPathNodeType.Namespace:
+                                       return String.Empty;
                                case XPathNodeType.Attribute:
                                        name = attr.Name;
                                        break;
@@ -178,7 +193,7 @@ namespace System.Xml.Linq
                                default:
                                        return LocalName;
                                }
-                               if (name.Namespace == XNamespace.Blank)
+                               if (name.Namespace == XNamespace.None)
                                        return String.Empty;
                                XElement el = (node as XElement) ?? node.Parent;
                                if (el == null)
@@ -187,9 +202,8 @@ namespace System.Xml.Linq
                        }
                }
 
-               [MonoTODO]
                public override IXmlSchemaInfo SchemaInfo {
-                       get { throw new NotImplementedException (); }
+                       get { return null; }
                }
 
                public override object UnderlyingObject {
@@ -207,7 +221,10 @@ namespace System.Xml.Linq
                                case XPathNodeType.ProcessingInstruction:
                                        return ((XPI) node).Data;
                                case XPathNodeType.Text:
-                                       return ((XText) node).Value;
+                                       string s = String.Empty;
+                                       for (var xn = node as XText; xn != null; xn = xn.NextNode as XText)
+                                               s += xn.Value;
+                                       return s;
                                case XPathNodeType.Element:
                                case XPathNodeType.Root:
                                        return GetInnerText ((XContainer) node);
@@ -256,7 +273,7 @@ namespace System.Xml.Linq
                public override bool MoveTo (XPathNavigator other)
                {
                        XNodeNavigator nav = other as XNodeNavigator;
-                       if (nav == null || nav.node.Owner != node.Owner)
+                       if (nav == null || nav.node.Document != node.Document)
                                return false;
                        node = nav.node;
                        attr = nav.attr;
@@ -265,6 +282,8 @@ namespace System.Xml.Linq
 
                public override bool MoveToFirstAttribute ()
                {
+                       if (attr != null)
+                               return false;
                        XElement el = node as XElement;
                        if (el == null || !el.HasAttributes)
                                return false;
@@ -278,8 +297,10 @@ namespace System.Xml.Linq
 
                public override bool MoveToFirstChild ()
                {
+                       if (attr != null)
+                               return false;
                        XContainer c = node as XContainer;
-                       if (c == null)
+                       if (c == null || c.FirstNode == null)
                                return false;
                        node = c.FirstNode;
                        attr = null;
@@ -288,6 +309,8 @@ namespace System.Xml.Linq
 
                public override bool MoveToFirstNamespace (XPathNamespaceScope scope)
                {
+                       if (NodeType != XPathNodeType.Element)
+                               return false;
                        for (XElement el = node as XElement; el != null; el = el.Parent) {
                                foreach (XAttribute a in el.Attributes ())
                                        if (a.IsNamespaceDeclaration) {
@@ -303,19 +326,23 @@ namespace System.Xml.Linq
                        return true;
                }
 
-               [MonoTODO]
                public override bool MoveToId (string id)
                {
-                       return false;
+                       throw new NotSupportedException ("This XPathNavigator does not support IDs");
                }
 
                public override bool MoveToNext ()
                {
-                       if (node.NextNode == null)
+                       XNode xn = node.NextNode;
+                       if (xn is XText)
+                               for (; xn != null; xn = xn.NextNode)
+                                       if (!(xn.NextNode is XText))
+                                               break;
+                       if (xn == null)
                                return false;
-                       node = node.NextNode;
+                       node = xn;
                        attr = null;
-                       return false;
+                       return true;
                }
 
                public override bool MoveToNextAttribute ()
@@ -345,6 +372,9 @@ namespace System.Xml.Linq
                        if (scope == XPathNamespaceScope.Local)
                                return false;
 
+                       if (attr == attr_ns_xml)
+                               return false; // no next attribute
+
                        for (XElement el = ((XElement) attr.Parent).Parent; el != null; el = el.Parent) {
                                foreach (XAttribute a in el.Attributes ())
                                        if (a.IsNamespaceDeclaration) {
@@ -364,9 +394,9 @@ namespace System.Xml.Linq
                                attr = null;
                                return true;
                        }
-                       if (node.Parent == null)
+                       if (node.Owner == null)
                                return false;
-                       node = node.Parent;
+                       node = node.Owner;
                        return true;
                }
 
@@ -381,8 +411,12 @@ namespace System.Xml.Linq
 
                public override void MoveToRoot ()
                {
-                       node = node.Owner;
                        attr = null;
+                       if (node.Document != null)
+                               node = node.Document;
+                       else
+                               while (node.Owner != null)
+                                       node = node.Owner;
                }
        }
 }