2 // System.Xml.XmlDocumentNavigator
5 // Jason Diamond <jason@injektilo.org>
7 // (C) 2002 Jason Diamond
11 using System.Collections;
\r
13 using System.Xml.XPath;
\r
15 namespace System.Xml
\r
17 internal class XmlDocumentNavigator : XPathNavigator
\r
19 #region Constructors
\r
22 internal XmlDocumentNavigator(XmlNode node)
\r
31 private XmlNode node;
\r
32 private IEnumerator attributesEnumerator;
\r
39 public override string BaseURI {
41 throw new NotImplementedException ();
45 public override bool HasAttributes {
47 if (node.Attributes != null)
48 foreach (XmlAttribute attribute in node.Attributes)
49 if (attribute.NamespaceURI != "http://www.w3.org/2000/xmlns/")
55 public override bool HasChildren {
57 XPathNodeType nodeType = NodeType;
58 bool canHaveChildren = nodeType == XPathNodeType.Root || nodeType == XPathNodeType.Element;
59 return canHaveChildren && node.FirstChild != null;
63 public override bool IsEmptyElement {
65 return node.NodeType == XmlNodeType.Element && !HasChildren;
69 public override string LocalName {
71 XPathNodeType nodeType = NodeType;
73 nodeType == XPathNodeType.Element ||
74 nodeType == XPathNodeType.Attribute ||
75 nodeType == XPathNodeType.ProcessingInstruction ||
76 nodeType == XPathNodeType.Namespace;
77 return canHaveName ? node.LocalName : String.Empty;
81 public override string Name {
83 XPathNodeType nodeType = NodeType;
85 nodeType == XPathNodeType.Element ||
86 nodeType == XPathNodeType.Attribute ||
87 nodeType == XPathNodeType.ProcessingInstruction ||
88 nodeType == XPathNodeType.Namespace;
89 return canHaveName ? node.Name : String.Empty;
93 public override string NamespaceURI {
95 return node.NamespaceURI;
100 public override XmlNameTable NameTable {
102 throw new NotImplementedException ();
106 public override XPathNodeType NodeType {
108 return node.XPathNodeType;
112 public override string Prefix {
118 public override string Value {
121 case XPathNodeType.Attribute:
122 case XPathNodeType.Comment:
123 case XPathNodeType.ProcessingInstruction:
124 case XPathNodeType.Text:
125 case XPathNodeType.Whitespace:
126 case XPathNodeType.SignificantWhitespace:
128 case XPathNodeType.Element:
129 case XPathNodeType.Root:
130 return node.InnerText;
137 public override string XmlLang {
139 throw new NotImplementedException ();
147 public override XPathNavigator Clone ()
149 return new XmlDocumentNavigator (node);
153 public override string GetAttribute (string localName, string namespaceURI)
155 throw new NotImplementedException ();
159 public override string GetNamespace (string name)
161 throw new NotImplementedException ();
164 public override bool IsSamePosition (XPathNavigator other)
166 XmlDocumentNavigator otherDocumentNavigator = other as XmlDocumentNavigator;
167 if (otherDocumentNavigator != null)
168 return node == otherDocumentNavigator.node;
172 public override bool MoveTo (XPathNavigator other)
174 XmlDocumentNavigator otherDocumentNavigator = other as XmlDocumentNavigator;
175 if (otherDocumentNavigator != null) {
176 if (node.OwnerDocument == otherDocumentNavigator.node.OwnerDocument) {
177 node = otherDocumentNavigator.node;
185 public override bool MoveToAttribute (string localName, string namespaceURI)
187 throw new NotImplementedException ();
190 public override bool MoveToFirst ()
192 if (node.NodeType != XmlNodeType.Attribute && node.ParentNode != null) {
193 node = node.ParentNode.FirstChild;
199 public override bool MoveToFirstAttribute ()
201 if (NodeType == XPathNodeType.Element) {
202 attributesEnumerator = node.Attributes.GetEnumerator ();
203 return MoveToNextAttribute ();
208 public override bool MoveToFirstChild ()
211 node = node.FirstChild;
218 public override bool MoveToFirstNamespace (XPathNamespaceScope namespaceScope)
220 throw new NotImplementedException ();
223 public override bool MoveToId (string id)
227 if (node.NodeType == XmlNodeType.Document)
228 doc = (XmlDocument) node;
230 doc = node.OwnerDocument;
232 XmlElement eltNew = doc.GetElementById (id);
241 public override bool MoveToNamespace (string name)
243 throw new NotImplementedException ();
246 public override bool MoveToNext ()
248 if (node.NextSibling != null) {
249 node = node.NextSibling;
255 public override bool MoveToNextAttribute ()
257 if (attributesEnumerator != null && attributesEnumerator.MoveNext ()) {
258 node = attributesEnumerator.Current as XmlAttribute;
265 public override bool MoveToNextNamespace (XPathNamespaceScope namespaceScope)
267 throw new NotImplementedException ();
270 public override bool MoveToParent ()
272 if (node.NodeType == XmlNodeType.Attribute) {
273 XmlElement ownerElement = ((XmlAttribute)node).OwnerElement;
274 if (ownerElement != null) {
278 } else if (node.ParentNode != null) {
279 node = node.ParentNode;
285 public override bool MoveToPrevious ()
287 if (node.PreviousSibling != null) {
288 node = node.PreviousSibling;
294 public override void MoveToRoot ()
296 if (node.NodeType != XmlNodeType.Document)
297 node = node.OwnerDocument;
300 internal XmlNode Node { get { return node; } }