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 switch (node.NodeType) {
109 case XmlNodeType.Document:
110 return XPathNodeType.Root;
111 case XmlNodeType.Element:
112 return XPathNodeType.Element;
113 case XmlNodeType.Attribute:
114 return XPathNodeType.Attribute;
115 case XmlNodeType.Text:
116 return XPathNodeType.Text;
117 case XmlNodeType.Whitespace:
118 return XPathNodeType.Whitespace;
119 case XmlNodeType.SignificantWhitespace:
120 return XPathNodeType.SignificantWhitespace;
121 case XmlNodeType.Comment:
122 return XPathNodeType.Comment;
123 case XmlNodeType.ProcessingInstruction:
124 return XPathNodeType.ProcessingInstruction;
126 throw new InvalidOperationException ();
130 public override string Prefix {
136 public override string Value {
139 case XPathNodeType.Attribute:
141 case XPathNodeType.Element:
142 return node.InnerText;
143 case XPathNodeType.Comment:
145 case XPathNodeType.ProcessingInstruction:
147 case XPathNodeType.Text:
149 case XPathNodeType.Whitespace:
151 case XPathNodeType.SignificantWhitespace:
153 case XPathNodeType.Root:
154 return node.InnerText;
161 public override string XmlLang {
163 throw new NotImplementedException ();
171 public override XPathNavigator Clone ()
173 return new XmlDocumentNavigator (node);
177 public override string GetAttribute (string localName, string namespaceURI)
179 throw new NotImplementedException ();
183 public override string GetNamespace (string name)
185 throw new NotImplementedException ();
188 public override bool IsSamePosition (XPathNavigator other)
190 XmlDocumentNavigator otherDocumentNavigator = other as XmlDocumentNavigator;
191 if (otherDocumentNavigator != null)
192 return node == otherDocumentNavigator.node;
196 public override bool MoveTo (XPathNavigator other)
198 XmlDocumentNavigator otherDocumentNavigator = other as XmlDocumentNavigator;
199 if (otherDocumentNavigator != null) {
200 if (node.OwnerDocument == otherDocumentNavigator.node.OwnerDocument) {
201 node = otherDocumentNavigator.node;
209 public override bool MoveToAttribute (string localName, string namespaceURI)
211 throw new NotImplementedException ();
214 public override bool MoveToFirst ()
216 if (node.NodeType != XmlNodeType.Attribute && node.ParentNode != null) {
217 node = node.ParentNode.FirstChild;
223 public override bool MoveToFirstAttribute ()
225 if (NodeType == XPathNodeType.Element) {
226 attributesEnumerator = node.Attributes.GetEnumerator ();
227 return MoveToNextAttribute ();
232 public override bool MoveToFirstChild ()
235 node = node.FirstChild;
242 public override bool MoveToFirstNamespace (XPathNamespaceScope namespaceScope)
244 throw new NotImplementedException ();
247 public override bool MoveToId (string id)
251 if (node.NodeType == XmlNodeType.Document)
252 doc = (XmlDocument) node;
254 doc = node.OwnerDocument;
256 XmlElement eltNew = doc.GetElementById (id);
265 public override bool MoveToNamespace (string name)
267 throw new NotImplementedException ();
270 public override bool MoveToNext ()
272 if (node.NextSibling != null) {
273 node = node.NextSibling;
279 public override bool MoveToNextAttribute ()
281 if (attributesEnumerator != null && attributesEnumerator.MoveNext ()) {
282 node = attributesEnumerator.Current as XmlAttribute;
289 public override bool MoveToNextNamespace (XPathNamespaceScope namespaceScope)
291 throw new NotImplementedException ();
294 public override bool MoveToParent ()
296 if (node.NodeType == XmlNodeType.Attribute) {
297 XmlElement ownerElement = ((XmlAttribute)node).OwnerElement;
298 if (ownerElement != null) {
302 } else if (node.ParentNode != null) {
303 node = node.ParentNode;
309 public override bool MoveToPrevious ()
311 if (node.PreviousSibling != null) {
312 node = node.PreviousSibling;
318 public override void MoveToRoot ()
320 if (node.NodeType != XmlNodeType.Document)
321 node = node.OwnerDocument;
324 internal XmlNode Node { get { return node; } }