Author: atsushi
Date: 2007-05-06 19:27:26 -0400 (Sun, 06 May 2007)
New Revision: 76804

Added:
   trunk/olive/class/System.Xml.Linq/System.Xml.Linq/XNodeNavigator.cs
Modified:
   trunk/olive/class/System.Xml.Linq/System.Xml.Linq.dll.sources
   trunk/olive/class/System.Xml.Linq/System.Xml.Linq/ChangeLog
   trunk/olive/class/System.Xml.Linq/System.Xml.XPath/ChangeLog
   trunk/olive/class/System.Xml.Linq/System.Xml.XPath/Extensions.cs
Log:
2007-05-06  Atsushi Enomoto  <[EMAIL PROTECTED]>

        * XNodeNavigator.cs : implemented XPathNavigator (untested!)

        * Extensions.cs : implemented.



Modified: trunk/olive/class/System.Xml.Linq/System.Xml.Linq/ChangeLog
===================================================================
--- trunk/olive/class/System.Xml.Linq/System.Xml.Linq/ChangeLog 2007-05-06 
22:16:42 UTC (rev 76803)
+++ trunk/olive/class/System.Xml.Linq/System.Xml.Linq/ChangeLog 2007-05-06 
23:27:26 UTC (rev 76804)
@@ -1,5 +1,9 @@
 2007-05-06  Atsushi Enomoto  <[EMAIL PROTECTED]>
 
+       * XNodeNavigator.cs : implemented XPathNavigator (untested!)
+
+2007-05-06  Atsushi Enomoto  <[EMAIL PROTECTED]>
+
        * XContainer.cs
          XNodeWriter.cs : implemented CreateWriter().
        * XElement.cs : when writing default xmlns attribute, namespace URI

Added: trunk/olive/class/System.Xml.Linq/System.Xml.Linq/XNodeNavigator.cs
===================================================================
--- trunk/olive/class/System.Xml.Linq/System.Xml.Linq/XNodeNavigator.cs 
2007-05-06 22:16:42 UTC (rev 76803)
+++ trunk/olive/class/System.Xml.Linq/System.Xml.Linq/XNodeNavigator.cs 
2007-05-06 23:27:26 UTC (rev 76804)
@@ -0,0 +1,362 @@
+using System;
+using System.IO;
+using System.Text;
+using System.Xml;
+using System.Xml.Schema;
+using System.Xml.XPath;
+
+using XPI = System.Xml.Linq.XProcessingInstruction;
+
+namespace System.Xml.Linq
+{
+       internal class XNodeNavigator : XPathNavigator
+       {
+               static readonly XAttribute attr_ns_xml = new XAttribute 
(XNamespace.Xmlns.GetName ("xml"), XNamespace.Xml.NamespaceName);
+
+               XNode node;
+               XAttribute attr;
+               XmlNameTable name_table;
+
+               public XNodeNavigator (XNode node, XmlNameTable nameTable)
+               {
+                       this.node = node;
+                       this.name_table = nameTable;
+               }
+
+               public XNodeNavigator (XNodeNavigator other)
+               {
+                       this.node = other.node;
+                       this.attr = other.attr;
+                       this.name_table = other.name_table;
+               }
+
+               public override string BaseURI {
+                       get { return node.BaseUri ?? String.Empty; }
+               }
+
+               public override bool CanEdit {
+                       get { return true; }
+               }
+
+               public override bool HasAttributes {
+                       get {
+                               XElement el = node as XElement;
+                               return el != null && el.HasAttributes;
+                       }
+               }
+
+               public override bool HasChildren {
+                       get {
+                               XContainer c = node as XContainer;
+                               return c != null && c.FirstNode != null;
+                       }
+               }
+
+               public override bool IsEmptyElement {
+                       get {
+                               XElement el = node as XElement;
+                               return el != null && el.IsEmpty;
+                       }
+               }
+
+               public override string LocalName {
+                       get {
+                               switch (NodeType) {
+                               case XPathNodeType.Namespace:
+                                       return attr.Name.Namespace == 
XNamespace.Blank ? String.Empty : attr.Name.LocalName;
+                               case XPathNodeType.Attribute:
+                                       return attr.Name.LocalName;
+                               case XPathNodeType.Element:
+                                       return ((XElement) node).Name.LocalName;
+                               case XPathNodeType.ProcessingInstruction:
+                                       return ((XPI) node).Target;
+                               default:
+                                       return String.Empty;
+                               }
+                       }
+               }
+
+               public override string Name {
+                       get {
+                               XName name = null;
+                               switch (NodeType) {
+                               case XPathNodeType.Attribute:
+                                       name = attr.Name;
+                                       break;
+                               case XPathNodeType.Element:
+                                       name = ((XElement) node).Name;
+                                       break;
+                               default:
+                                       return LocalName;
+                               }
+                               if (name.Namespace == XNamespace.Blank)
+                                       return name.LocalName;
+                               XElement el = (node as XElement) ?? node.Parent;
+                               if (el == null)
+                                       return name.LocalName;
+                               string prefix = el.GetPrefixOfNamespace 
(name.Namespace);
+                               return prefix.Length > 0 ? String.Concat 
(prefix, ":", name.LocalName) : name.LocalName;
+                       }
+               }
+
+               public override string NamespaceURI {
+                       get {
+                               switch (NodeType) {
+                               case XPathNodeType.Namespace:
+                                       return attr.Value;
+                               case XPathNodeType.Attribute:
+                                       return attr.Name.NamespaceName;
+                               case XPathNodeType.Element:
+                                       return ((XElement) 
node).Name.NamespaceName;
+                               default:
+                                       return String.Empty;
+                               }
+                       }
+               }
+
+               public override XmlNameTable NameTable {
+                       get { return name_table; }
+               }
+
+               public override XPathNodeType NodeType {
+                       get {
+                               if (attr != null)
+                                       return  attr.IsNamespaceDeclaration ?
+                                               XPathNodeType.Namespace :
+                                               XPathNodeType.Attribute;
+                               switch (node.NodeType) {
+                               case XmlNodeType.Element:
+                                       return XPathNodeType.Element;
+                               case XmlNodeType.Document:
+                                       return XPathNodeType.Root;
+                               case XmlNodeType.Comment:
+                                       return XPathNodeType.Comment;
+                               case XmlNodeType.ProcessingInstruction:
+                                       return 
XPathNodeType.ProcessingInstruction;
+                               default:
+                                       return XPathNodeType.Text;
+                               }
+                       }
+               }
+
+               public override string Prefix {
+                       get {
+                               XName name = null;
+                               switch (NodeType) {
+                               case XPathNodeType.Attribute:
+                                       name = attr.Name;
+                                       break;
+                               case XPathNodeType.Element:
+                                       name = ((XElement) node).Name;
+                                       break;
+                               default:
+                                       return LocalName;
+                               }
+                               if (name.Namespace == XNamespace.Blank)
+                                       return String.Empty;
+                               XElement el = (node as XElement) ?? node.Parent;
+                               if (el == null)
+                                       return String.Empty;
+                               return el.GetPrefixOfNamespace (name.Namespace);
+                       }
+               }
+
+               [MonoTODO]
+               public override IXmlSchemaInfo SchemaInfo {
+                       get { throw new NotImplementedException (); }
+               }
+
+               public override object UnderlyingObject {
+                       get { return attr != null ? (object) attr : node; }
+               }
+
+               public override string Value {
+                       get {
+                               if (attr != null)
+                                       return attr.Value;
+                               else
+                               switch (NodeType) {
+                               case XPathNodeType.Comment:
+                                       return ((XComment) node).Value;
+                               case XPathNodeType.ProcessingInstruction:
+                                       return ((XPI) node).Data;
+                               case XPathNodeType.Text:
+                                       return ((XText) node).Value;
+                               case XPathNodeType.Element:
+                               case XPathNodeType.Root:
+                                       return GetInnerText ((XContainer) node);
+                               }
+                               return String.Empty;
+                       }
+               }
+
+               string GetInnerText (XContainer node)
+               {
+                       StringBuilder sb = null;
+                       foreach (XNode n in node.Nodes ())
+                               GetInnerText (n, ref sb);
+                       return sb != null ? sb.ToString () : String.Empty;
+               }
+
+               void GetInnerText (XNode n, ref StringBuilder sb)
+               {
+                       switch (n.NodeType) {
+                       case XmlNodeType.Element:
+                               foreach (XNode c in ((XElement) n).Nodes ())
+                                       GetInnerText (c, ref sb);
+                               break;
+                       case XmlNodeType.Text:
+                       case XmlNodeType.CDATA:
+                               if (sb == null)
+                                       sb = new StringBuilder ();
+                               sb.Append (((XText) n).Value);
+                               break;
+                       }
+               }
+
+               public override XPathNavigator Clone ()
+               {
+                       return new XNodeNavigator (this);
+               }
+
+               public override bool IsSamePosition (XPathNavigator other)
+               {
+                       XNodeNavigator nav = other as XNodeNavigator;
+                       if (nav == null || nav.node.Owner != node.Owner)
+                               return false;
+                       return node == nav.node && attr == nav.attr;
+               }
+
+               public override bool MoveTo (XPathNavigator other)
+               {
+                       XNodeNavigator nav = other as XNodeNavigator;
+                       if (nav == null || nav.node.Owner != node.Owner)
+                               return false;
+                       node = nav.node;
+                       attr = nav.attr;
+                       return true;
+               }
+
+               public override bool MoveToFirstAttribute ()
+               {
+                       XElement el = node as XElement;
+                       if (el == null || !el.HasAttributes)
+                               return false;
+                       foreach (XAttribute a in el.Attributes ())
+                               if (!a.IsNamespaceDeclaration) {
+                                       attr = a;
+                                       return true;
+                               }
+                       return false;
+               }
+
+               public override bool MoveToFirstChild ()
+               {
+                       XContainer c = node as XContainer;
+                       if (c == null)
+                               return false;
+                       node = c.FirstNode;
+                       attr = null;
+                       return true;
+               }
+
+               public override bool MoveToFirstNamespace (XPathNamespaceScope 
scope)
+               {
+                       for (XElement el = node as XElement; el != null; el = 
el.Parent) {
+                               foreach (XAttribute a in el.Attributes ())
+                                       if (a.IsNamespaceDeclaration) {
+                                               attr = a;
+                                               return true;
+                                       }
+                               if (scope == XPathNamespaceScope.Local)
+                                       return false;
+                       }
+                       if (scope != XPathNamespaceScope.All)
+                               return false;
+                       attr = attr_ns_xml;
+                       return true;
+               }
+
+               [MonoTODO]
+               public override bool MoveToId (string id)
+               {
+                       return false;
+               }
+
+               public override bool MoveToNext ()
+               {
+                       if (node.NextNode == null)
+                               return false;
+                       node = node.NextNode;
+                       attr = null;
+                       return false;
+               }
+
+               public override bool MoveToNextAttribute ()
+               {
+                       if (attr == null)
+                               return false;
+                       if (attr.NextAttribute == null)
+                               return false;
+                       for (XAttribute a = attr.NextAttribute; a != null; a = 
a.NextAttribute)
+                               if (!a.IsNamespaceDeclaration) {
+                                       attr = a;
+                                       return true;
+                               }
+                       return false;
+               }
+
+               public override bool MoveToNextNamespace (XPathNamespaceScope 
scope)
+               {
+                       if (attr == null)
+                               return false;
+                       for (XAttribute a = attr.NextAttribute; a != null; a = 
a.NextAttribute)
+                               if (a.IsNamespaceDeclaration) {
+                                       attr = a;
+                                       return true;
+                               }
+
+                       if (scope == XPathNamespaceScope.Local)
+                               return false;
+
+                       for (XElement el = ((XElement) attr.Parent).Parent; el 
!= null; el = el.Parent) {
+                               foreach (XAttribute a in el.Attributes ())
+                                       if (a.IsNamespaceDeclaration) {
+                                               attr = a;
+                                               return true;
+                                       }
+                       }
+                       if (scope != XPathNamespaceScope.All)
+                               return false;
+                       attr = attr_ns_xml;
+                       return true;
+               }
+
+               public override bool MoveToParent ()
+               {
+                       if (attr != null) {
+                               attr = null;
+                               return true;
+                       }
+                       if (node.Parent == null)
+                               return false;
+                       node = node.Parent;
+                       return true;
+               }
+
+               public override bool MoveToPrevious ()
+               {
+                       if (node.PreviousNode == null)
+                               return false;
+                       node = node.PreviousNode;
+                       attr = null;
+                       return true;
+               }
+
+               public override void MoveToRoot ()
+               {
+                       node = node.Owner;
+                       attr = null;
+               }
+       }
+}

Modified: trunk/olive/class/System.Xml.Linq/System.Xml.Linq.dll.sources
===================================================================
--- trunk/olive/class/System.Xml.Linq/System.Xml.Linq.dll.sources       
2007-05-06 22:16:42 UTC (rev 76803)
+++ trunk/olive/class/System.Xml.Linq/System.Xml.Linq.dll.sources       
2007-05-06 23:27:26 UTC (rev 76804)
@@ -19,6 +19,7 @@
 System.Xml.Linq/XNode.cs
 System.Xml.Linq/XNodeDocumentOrderComparer.cs
 System.Xml.Linq/XNodeEqualityComparer.cs
+System.Xml.Linq/XNodeNavigator.cs
 System.Xml.Linq/XNodeReader.cs
 System.Xml.Linq/XNodeWriter.cs
 System.Xml.Linq/XObject.cs

Modified: trunk/olive/class/System.Xml.Linq/System.Xml.XPath/ChangeLog
===================================================================
--- trunk/olive/class/System.Xml.Linq/System.Xml.XPath/ChangeLog        
2007-05-06 22:16:42 UTC (rev 76803)
+++ trunk/olive/class/System.Xml.Linq/System.Xml.XPath/ChangeLog        
2007-05-06 23:27:26 UTC (rev 76804)
@@ -1,3 +1,7 @@
+2007-05-06  Atsushi Enomoto  <[EMAIL PROTECTED]>
+
+       * Extensions.cs : implemented.
+
 2007-05-02  Atsushi Enomoto  <[EMAIL PROTECTED]>
 
        * Extensions.cs : new stub file.

Modified: trunk/olive/class/System.Xml.Linq/System.Xml.XPath/Extensions.cs
===================================================================
--- trunk/olive/class/System.Xml.Linq/System.Xml.XPath/Extensions.cs    
2007-05-06 22:16:42 UTC (rev 76803)
+++ trunk/olive/class/System.Xml.Linq/System.Xml.XPath/Extensions.cs    
2007-05-06 23:27:26 UTC (rev 76804)
@@ -5,57 +5,50 @@
 
 namespace System.Xml.XPath
 {
-       [MonoTODO]
        public static class Extensions
        {
-               [MonoTODO]
                public static XPathNavigator CreateNavigator (this XNode node)
                {
                        return CreateNavigator (node, new NameTable ());
                }
 
-               [MonoTODO]
                public static XPathNavigator CreateNavigator (this XNode node, 
XmlNameTable nameTable)
                {
-                       throw new NotImplementedException ();
+                       return new XNodeNavigator (node, nameTable);
                }
 
-               [MonoTODO]
                public static object XPathEvaluate (this XNode node, string 
expression)
                {
                        return XPathEvaluate (node, expression, null);
                }
 
-               [MonoTODO]
                public static object XPathEvaluate (this XNode node, string 
expression, IXmlNamespaceResolver nsResolver)
                {
                        return CreateNavigator (node).Evaluate (expression, 
nsResolver);
                }
 
-               [MonoTODO]
                public static XElement XPathSelectElement (this XNode node, 
string xpath)
                {
                        return XPathSelectElement (node, xpath, null);
                }
 
-               [MonoTODO]
                public static XElement XPathSelectElement (this XNode node, 
string xpath, IXmlNamespaceResolver nsResolver)
                {
                        XPathNavigator nav = CreateNavigator 
(node).SelectSingleNode (xpath, nsResolver);
-                       throw new NotImplementedException ();
+                       return nav.UnderlyingObject as XElement;
                }
 
-               [MonoTODO]
                public static IEnumerable<XElement> XPathSelectElements (this 
XNode node, string xpath)
                {
                        return XPathSelectElements (node, xpath, null);
                }
 
-               [MonoTODO]
                public static IEnumerable<XElement> XPathSelectElements (this 
XNode node, string xpath, IXmlNamespaceResolver nsResolver)
                {
                        XPathNodeIterator iter = CreateNavigator (node).Select 
(xpath, nsResolver);
-                       throw new NotImplementedException ();
+                       foreach (XPathNavigator nav in iter)
+                               if (nav.UnderlyingObject is XElement)
+                                       yield return (XElement) 
nav.UnderlyingObject;
                }
        }
 }

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to