2002-09-18 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.XML / System.Xml / XmlDocumentNavigator.cs
1 //
2 // System.Xml.XmlDocumentNavigator
3 //
4 // Author:
5 //   Jason Diamond <jason@injektilo.org>
6 //
7 // (C) 2002 Jason Diamond
8 //
9 \r
10 using System;\r
11 using System.Collections;\r
12 using System.Xml;\r
13 using System.Xml.XPath;\r
14 \r
15 namespace System.Xml\r
16 {\r
17         internal class XmlDocumentNavigator : XPathNavigator\r
18         {\r
19                 #region Constructors\r
20 \r
21                 [MonoTODO]
22                 internal XmlDocumentNavigator(XmlNode node)\r
23                 {\r
24                         this.node = node;\r
25                 }\r
26 \r
27                 #endregion\r
28 \r
29                 #region Fields\r
30 \r
31                 private XmlNode node;\r
32                 private IEnumerator attributesEnumerator;\r
33 \r
34                 #endregion\r
35
36                 #region Properties
37
38                 [MonoTODO]
39                 public override string BaseURI {
40                         get {
41                                 throw new NotImplementedException ();
42                         }
43                 }
44
45                 public override bool HasAttributes {
46                         get {
47                                 if (node.Attributes != null)
48                                         foreach (XmlAttribute attribute in node.Attributes)
49                                                 if (attribute.NamespaceURI != "http://www.w3.org/2000/xmlns/")
50                                                         return true;
51                                 return false;
52                         }
53                 }
54
55                 public override bool HasChildren {
56                         get {
57                                 XPathNodeType nodeType = NodeType;
58                                 bool canHaveChildren = nodeType == XPathNodeType.Root || nodeType == XPathNodeType.Element;
59                                 return canHaveChildren && node.FirstChild != null;
60                         }
61                 }
62
63                 public override bool IsEmptyElement {
64                         get {
65                                 return node.NodeType == XmlNodeType.Element && !HasChildren;
66                         }
67                 }
68
69                 public override string LocalName {
70                         get {
71                                 XPathNodeType nodeType = NodeType;
72                                 bool canHaveName = 
73                                         nodeType == XPathNodeType.Element || 
74                                         nodeType == XPathNodeType.Attribute || 
75                                         nodeType == XPathNodeType.ProcessingInstruction ||
76                                         nodeType == XPathNodeType.Namespace;
77                                 return canHaveName ? node.LocalName : String.Empty;
78                         }
79                 }
80
81                 public override string Name {
82                         get {
83                                 XPathNodeType nodeType = NodeType;
84                                 bool canHaveName = 
85                                         nodeType == XPathNodeType.Element || 
86                                         nodeType == XPathNodeType.Attribute || 
87                                         nodeType == XPathNodeType.ProcessingInstruction ||
88                                         nodeType == XPathNodeType.Namespace;
89                                 return canHaveName ? node.Name : String.Empty;
90                         }
91                 }
92
93                 public override string NamespaceURI {
94                         get {
95                                 return node.NamespaceURI;
96                         }
97                 }
98
99                 [MonoTODO]
100                 public override XmlNameTable NameTable {
101                         get {
102                                 throw new NotImplementedException ();
103                         }
104                 }
105
106                 public override XPathNodeType NodeType {
107                         get {
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;
125                                 }
126                                 throw new InvalidOperationException ();
127                         }
128                 }
129
130                 public override string Prefix {
131                         get {
132                                 return node.Prefix;
133                         }
134                 }
135
136                 public override string Value {
137                         get {
138                                 switch (NodeType) {
139                                 case XPathNodeType.Attribute:
140                                         return node.Value;
141                                 case XPathNodeType.Element:
142                                         return node.InnerText;
143                                 case XPathNodeType.Comment:
144                                         return node.Value;
145                                 case XPathNodeType.ProcessingInstruction:
146                                         return node.Value;
147                                 case XPathNodeType.Text:
148                                         return node.Value;
149                                 case XPathNodeType.Whitespace:
150                                         return node.Value;
151                                 case XPathNodeType.SignificantWhitespace:
152                                         return node.Value;
153                                 case XPathNodeType.Root:
154                                         return node.InnerText;
155                                 }
156                                 return String.Empty;
157                         }
158                 }
159
160                 [MonoTODO]
161                 public override string XmlLang {
162                         get {
163                                 throw new NotImplementedException ();
164                         }
165                 }
166
167                 #endregion
168
169                 #region Methods
170
171                 public override XPathNavigator Clone ()
172                 {
173                         return new XmlDocumentNavigator (node);
174                 }
175
176                 [MonoTODO]
177                 public override string GetAttribute (string localName, string namespaceURI)
178                 {
179                         throw new NotImplementedException ();
180                 }
181
182                 [MonoTODO]
183                 public override string GetNamespace (string name)
184                 {
185                         throw new NotImplementedException ();
186                 }
187                 
188                 public override bool IsSamePosition (XPathNavigator other)
189                 {
190                         XmlDocumentNavigator otherDocumentNavigator = other as XmlDocumentNavigator;
191                         if (otherDocumentNavigator != null)
192                                 return node == otherDocumentNavigator.node;
193                         return false;
194                 }
195
196                 public override bool MoveTo (XPathNavigator other)
197                 {
198                         XmlDocumentNavigator otherDocumentNavigator = other as XmlDocumentNavigator;
199                         if (otherDocumentNavigator != null) {
200                                 if (node.OwnerDocument == otherDocumentNavigator.node.OwnerDocument) {
201                                         node = otherDocumentNavigator.node;
202                                         return true;
203                                 }
204                         }
205                         return false;
206                 }
207
208                 [MonoTODO]
209                 public override bool MoveToAttribute (string localName, string namespaceURI)
210                 {
211                         throw new NotImplementedException ();
212                 }
213
214                 public override bool MoveToFirst ()
215                 {
216                         if (node.NodeType != XmlNodeType.Attribute && node.ParentNode != null) {
217                                 node = node.ParentNode.FirstChild;
218                                 return true;
219                         }
220                         return false;
221                 }
222
223                 public override bool MoveToFirstAttribute ()
224                 {
225                         if (NodeType == XPathNodeType.Element) {
226                                 attributesEnumerator = node.Attributes.GetEnumerator ();
227                                 return MoveToNextAttribute ();
228                         }
229                         return false;
230                 }
231
232                 public override bool MoveToFirstChild ()
233                 {
234                         if (HasChildren) {
235                                 node = node.FirstChild;
236                                 return true;
237                         }
238                         return false;
239                 }
240
241                 [MonoTODO]
242                 public override bool MoveToFirstNamespace (XPathNamespaceScope namespaceScope)
243                 {
244                         throw new NotImplementedException ();
245                 }
246
247                 public override bool MoveToId (string id)
248                 {
249                         XmlDocument doc;
250                         
251                         if (node.NodeType == XmlNodeType.Document)
252                                 doc = (XmlDocument) node;
253                         else
254                                 doc = node.OwnerDocument;
255
256                         XmlElement eltNew = doc.GetElementById (id);
257                         if (eltNew == null)
258                                 return false;
259
260                         node = eltNew;
261                         return true;
262                 }
263
264                 [MonoTODO]
265                 public override bool MoveToNamespace (string name)
266                 {
267                         throw new NotImplementedException ();
268                 }
269
270                 public override bool MoveToNext ()
271                 {
272                         if (node.NextSibling != null) {
273                                 node = node.NextSibling;
274                                 return true;
275                         }
276                         return false;
277                 }
278
279                 public override bool MoveToNextAttribute ()
280                 {
281                         if (attributesEnumerator != null && attributesEnumerator.MoveNext ()) {
282                                 node = attributesEnumerator.Current as XmlAttribute;
283                                 return true;
284                         }
285                         return false;
286                 }
287
288                 [MonoTODO]
289                 public override bool MoveToNextNamespace (XPathNamespaceScope namespaceScope)
290                 {
291                         throw new NotImplementedException ();
292                 }
293
294                 public override bool MoveToParent ()
295                 {
296                         if (node.NodeType == XmlNodeType.Attribute) {
297                                 XmlElement ownerElement = ((XmlAttribute)node).OwnerElement;
298                                 if (ownerElement != null) {
299                                         node = ownerElement;
300                                         return true;
301                                 }
302                         } else if (node.ParentNode != null) {
303                                 node = node.ParentNode;
304                                 return true;
305                         }
306                         return false;
307                 }
308
309                 public override bool MoveToPrevious ()
310                 {
311                         if (node.PreviousSibling != null) {
312                                 node = node.PreviousSibling;
313                                 return true;
314                         }
315                         return false;
316                 }
317
318                 public override void MoveToRoot ()
319                 {
320                         if (node.NodeType != XmlNodeType.Document)
321                                 node = node.OwnerDocument;
322                 }
323
324                 internal XmlNode Node { get { return node; } }
325
326                 #endregion
327         }\r
328 }\r