2003-01-18 Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
[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                                 return node.XPathNodeType;
109                         }
110                 }
111
112                 public override string Prefix {
113                         get {
114                                 return node.Prefix;
115                         }
116                 }
117
118                 public override string Value {
119                         get {
120                                 switch (NodeType) {
121                                 case XPathNodeType.Attribute:
122                                 case XPathNodeType.Comment:
123                                 case XPathNodeType.ProcessingInstruction:
124                                 case XPathNodeType.Text:
125                                 case XPathNodeType.Whitespace:
126                                 case XPathNodeType.SignificantWhitespace:
127                                         return node.Value;
128                                 case XPathNodeType.Element:
129                                 case XPathNodeType.Root:
130                                         return node.InnerText;
131                                 }
132                                 return String.Empty;
133                         }
134                 }
135
136                 [MonoTODO]
137                 public override string XmlLang {
138                         get {
139                                 throw new NotImplementedException ();
140                         }
141                 }
142
143                 #endregion
144
145                 #region Methods
146
147                 public override XPathNavigator Clone ()
148                 {
149                         return new XmlDocumentNavigator (node);
150                 }
151
152                 [MonoTODO]
153                 public override string GetAttribute (string localName, string namespaceURI)
154                 {
155                         throw new NotImplementedException ();
156                 }
157
158                 [MonoTODO]
159                 public override string GetNamespace (string name)
160                 {
161                         throw new NotImplementedException ();
162                 }
163                 
164                 public override bool IsSamePosition (XPathNavigator other)
165                 {
166                         XmlDocumentNavigator otherDocumentNavigator = other as XmlDocumentNavigator;
167                         if (otherDocumentNavigator != null)
168                                 return node == otherDocumentNavigator.node;
169                         return false;
170                 }
171
172                 public override bool MoveTo (XPathNavigator other)
173                 {
174                         XmlDocumentNavigator otherDocumentNavigator = other as XmlDocumentNavigator;
175                         if (otherDocumentNavigator != null) {
176                                 if (node.OwnerDocument == otherDocumentNavigator.node.OwnerDocument) {
177                                         node = otherDocumentNavigator.node;
178                                         return true;
179                                 }
180                         }
181                         return false;
182                 }
183
184                 [MonoTODO]
185                 public override bool MoveToAttribute (string localName, string namespaceURI)
186                 {
187                         throw new NotImplementedException ();
188                 }
189
190                 public override bool MoveToFirst ()
191                 {
192                         if (node.NodeType != XmlNodeType.Attribute && node.ParentNode != null) {
193                                 node = node.ParentNode.FirstChild;
194                                 return true;
195                         }
196                         return false;
197                 }
198
199                 public override bool MoveToFirstAttribute ()
200                 {
201                         if (NodeType == XPathNodeType.Element) {
202                                 attributesEnumerator = node.Attributes.GetEnumerator ();
203                                 return MoveToNextAttribute ();
204                         }
205                         return false;
206                 }
207
208                 public override bool MoveToFirstChild ()
209                 {
210                         if (HasChildren) {
211                                 node = node.FirstChild;
212                                 return true;
213                         }
214                         return false;
215                 }
216
217                 [MonoTODO]
218                 public override bool MoveToFirstNamespace (XPathNamespaceScope namespaceScope)
219                 {
220                         throw new NotImplementedException ();
221                 }
222
223                 public override bool MoveToId (string id)
224                 {
225                         XmlDocument doc;
226                         
227                         if (node.NodeType == XmlNodeType.Document)
228                                 doc = (XmlDocument) node;
229                         else
230                                 doc = node.OwnerDocument;
231
232                         XmlElement eltNew = doc.GetElementById (id);
233                         if (eltNew == null)
234                                 return false;
235
236                         node = eltNew;
237                         return true;
238                 }
239
240                 [MonoTODO]
241                 public override bool MoveToNamespace (string name)
242                 {
243                         throw new NotImplementedException ();
244                 }
245
246                 public override bool MoveToNext ()
247                 {
248                         if (node.NextSibling != null) {
249                                 node = node.NextSibling;
250                                 return true;
251                         }
252                         return false;
253                 }
254
255                 public override bool MoveToNextAttribute ()
256                 {
257                         if (attributesEnumerator != null && attributesEnumerator.MoveNext ()) {
258                                 node = attributesEnumerator.Current as XmlAttribute;
259                                 return true;
260                         }
261                         return false;
262                 }
263
264                 [MonoTODO]
265                 public override bool MoveToNextNamespace (XPathNamespaceScope namespaceScope)
266                 {
267                         throw new NotImplementedException ();
268                 }
269
270                 public override bool MoveToParent ()
271                 {
272                         if (node.NodeType == XmlNodeType.Attribute) {
273                                 XmlElement ownerElement = ((XmlAttribute)node).OwnerElement;
274                                 if (ownerElement != null) {
275                                         node = ownerElement;
276                                         return true;
277                                 }
278                         } else if (node.ParentNode != null) {
279                                 node = node.ParentNode;
280                                 return true;
281                         }
282                         return false;
283                 }
284
285                 public override bool MoveToPrevious ()
286                 {
287                         if (node.PreviousSibling != null) {
288                                 node = node.PreviousSibling;
289                                 return true;
290                         }
291                         return false;
292                 }
293
294                 public override void MoveToRoot ()
295                 {
296                         if (node.NodeType != XmlNodeType.Document)
297                                 node = node.OwnerDocument;
298                 }
299
300                 internal XmlNode Node { get { return node; } }
301
302                 #endregion
303         }\r
304 }\r