2004-08-02 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.XML / System.Xml.XPath / XPathDocument.cs
1 //
2 // System.Xml.XPath.XPathDocument
3 //
4 // Authors:
5 //   Tim Coleman (tim@timcoleman.com)
6 //   Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
7 //
8 // (C) Copyright 2002 Tim Coleman
9 // (C) 2003 Atsushi Enomoto
10 //
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32 using System;
33 using System.Collections;
34 using System.ComponentModel;
35 using System.IO;
36 using System.Text;
37 using System.Xml;
38 using System.Xml.Schema;
39 using System.Xml.Serialization;
40 using Mono.Xml.XPath;
41
42 namespace System.Xml.XPath
43 {
44 #if NET_2_0
45         [XmlSchemaProvider ("GetSchema")]
46         public class XPathDocument : IXPathNavigable, IXPathEditable,
47                 IChangeTracking, IRevertibleChangeTracking, IXmlSerializable
48 #else
49         public class XPathDocument : IXPathNavigable
50 #endif
51         {
52                 DTMXPathDocument document;
53 #if NET_2_0
54                 XPathEditableDocument editable;
55 #endif
56
57 #region Constructors
58
59 #if NET_2_0
60                 [MonoTODO]
61                 public XPathDocument ()
62                         : this (new NameTable ())
63                 {
64                 }
65
66                 [MonoTODO]
67                 public XPathDocument (XmlNameTable nameTable)
68                 {
69                         editable = new XPathEditableDocument (new XmlDocument (nameTable));
70                 }
71
72                 public XPathDocument (Stream stream)
73                         : this (stream, true)
74                 {
75                 }
76
77                 public XPathDocument (string uri) 
78                         : this (uri, XmlSpace.None, true)
79                 {
80                 }
81
82                 public XPathDocument (string uri, bool acceptChangesOnLoad) 
83                         : this (uri, XmlSpace.None, acceptChangesOnLoad)
84                 {
85                 }
86
87                 public XPathDocument (TextReader reader)
88                         : this (reader, true)
89                 {
90                 }
91
92                 [MonoTODO]
93                 public XPathDocument (XmlReader reader)
94                         : this (reader, XmlSpace.None, true)
95                 {
96                 }
97
98                 [MonoTODO]
99                 public XPathDocument (XmlReader reader, bool acceptChangesOnLoad)
100                         : this (reader, XmlSpace.None, acceptChangesOnLoad)
101                 {
102                 }
103
104                 [MonoTODO]
105                 public XPathDocument (string uri, XmlSpace space)
106                         : this (uri, space, true)
107                 {
108                 }
109
110                 [MonoTODO]
111                 public XPathDocument (XmlReader reader, XmlSpace space)
112                         : this (reader, space, true)
113                 {
114                 }
115
116                 [MonoTODO]
117                 public XPathDocument (string uri, XmlSpace space, bool acceptChangesOnLoad)
118                 {
119                         XmlValidatingReader vr = null;
120                         try {
121                                 vr = new XmlValidatingReader (new XmlTextReader (uri));
122                                 vr.ValidationType = ValidationType.None;
123                                 Initialize (vr, space, acceptChangesOnLoad);
124                         } finally {
125                                 if (vr != null)
126                                         vr.Close ();
127                         }
128                 }
129
130                 [MonoTODO]
131                 public XPathDocument (Stream stream, bool acceptChangesOnLoad)
132                 {
133                         XmlValidatingReader vr = new XmlValidatingReader (new XmlTextReader (stream));
134                         vr.ValidationType = ValidationType.None;
135                         Initialize (vr, XmlSpace.None, acceptChangesOnLoad);
136                 }
137
138                 [MonoTODO]
139                 public XPathDocument (TextReader reader, bool acceptChangesOnLoad)
140                 {
141                         XmlValidatingReader vr = new XmlValidatingReader (new XmlTextReader (reader));
142                         vr.ValidationType = ValidationType.None;
143                         Initialize (vr, XmlSpace.None, acceptChangesOnLoad);
144                 }
145
146                 [MonoTODO]
147                 public XPathDocument (XmlReader reader, XmlSpace space, bool acceptChangesOnLoad)
148                 {
149                         Initialize (reader, space, acceptChangesOnLoad);
150                 }
151 #else
152                 public XPathDocument (Stream stream)
153                 {
154                         XmlValidatingReader vr = new XmlValidatingReader (new XmlTextReader (stream));
155                         vr.ValidationType = ValidationType.None;
156                         Initialize (vr, XmlSpace.None);
157                 }
158
159                 public XPathDocument (string uri) 
160                         : this (uri, XmlSpace.None)
161                 {
162                 }
163
164                 public XPathDocument (TextReader reader)
165                 {
166                         XmlValidatingReader vr = new XmlValidatingReader (new XmlTextReader (reader));
167                         vr.ValidationType = ValidationType.None;
168                         Initialize (vr, XmlSpace.None);
169                 }
170
171                 public XPathDocument (XmlReader reader)
172                         : this (reader, XmlSpace.None)
173                 {
174                 }
175
176                 public XPathDocument (string uri, XmlSpace space)
177                 {
178                         XmlValidatingReader vr = null;
179                         try {
180                                 vr = new XmlValidatingReader (new XmlTextReader (uri));
181                                 vr.ValidationType = ValidationType.None;
182                                 Initialize (vr, space);
183                         } finally {
184                                 if (vr != null)
185                                         vr.Close ();
186                         }
187                 }
188
189                 public XPathDocument (XmlReader reader, XmlSpace space)
190                 {
191                         Initialize (reader, space);
192                 }
193 #endif
194
195                 private void Initialize (XmlReader reader, XmlSpace space)
196                 {
197                         document = new DTMXPathDocumentBuilder (reader, space).CreateDocument ();
198                 }
199
200                 private void Initialize (XmlReader reader, XmlSpace space, bool acceptChangesOnLoad)
201                 {
202                         document = new DTMXPathDocumentBuilder (reader, space).CreateDocument ();
203                 }
204
205 #endregion
206
207 #region Events
208
209 #if NET_2_0
210
211                 public event NodeChangedEventHandler ChangeRejected;
212
213                 public event NodeChangedEventHandler ItemUpdated;
214
215                 public event NodeChangedEventHandler ItemUpdating;
216
217                 public event NodeChangedEventHandler ItemInserted;
218
219                 public event NodeChangedEventHandler ItemInserting;
220
221                 public event NodeChangedEventHandler ItemDeleted;
222
223                 public event NodeChangedEventHandler ItemDeleting;
224
225                 public event NodeChangedEventHandler RejectingChange;
226
227 #endif // NET_2_0
228
229 #endregion // Events
230
231 #region Properties
232
233 #if NET_2_0
234
235                 [MonoTODO]
236                 public bool EnableChangeTracking {
237                         get { throw new NotImplementedException (); }
238                         set { throw new NotImplementedException (); }
239                 }
240
241                 [MonoTODO]
242                 public Encoding Encoding {
243                         get { throw new NotImplementedException (); }
244                         set { throw new NotImplementedException (); }
245                 }
246
247                 [MonoTODO]
248                 bool IChangeTracking.IsChanged {
249                         get { throw new NotImplementedException (); }
250                 }
251
252                 [MonoTODO]
253                 public XmlNameTable NameTable {
254                         get { throw new NotImplementedException (); }
255                 }
256
257                 [MonoTODO]
258                 public bool PreserveWhiteSpace {
259                         get { throw new NotImplementedException (); }
260                 }
261
262                 [MonoTODO]
263                 public XmlSchemaSet Schemas {
264                         get { throw new NotImplementedException (); }
265                         set { throw new NotImplementedException (); }
266                 }
267
268 #endif // NET_2_0
269
270 #endregion // Properies
271
272
273 #region Methods
274
275 #if NET_2_0
276                 [MonoTODO]
277                 public void AcceptChanges ()
278                 {
279                         throw new NotImplementedException ();
280                 }
281
282                 /* It will disappear in 2.0 RTM
283                 [MonoTODO]
284                 public XPathChangeNavigator CreateChangeNavigator ()
285                 {
286                         throw new NotImplementedException ();
287                 }
288                 */
289
290                 [MonoTODO]
291                 public XPathEditableNavigator CreateEditor ()
292                 {
293                         if (editable == null)
294                                 throw new NotImplementedException ();
295                         return editable.CreateEditor ();
296                 }
297
298                 [MonoTODO ("This code is only for compatibility.")]
299                 public XPathNavigator CreateNavigator ()
300                 {
301                         if (editable == null)
302                                 return document.CreateNavigator ();
303                         else
304                                 return editable.CreateNavigator ();
305                 }
306
307                 public XmlWriter CreateWriter ()
308                 {
309                         return CreateEditor ().AppendChild ();
310                 }
311
312                 [MonoTODO]
313                 public virtual XmlSchema GetSchema ()
314                 {
315                         throw new NotImplementedException ();
316                 }
317
318                 [MonoTODO]
319                 public static XmlQualifiedName GetXPathDocumentSchema (XmlSchemaSet schemas)
320                 {
321                         throw new NotImplementedException ();
322                 }
323
324                 [MonoTODO]
325                 public bool HasChanges ()
326                 {
327                         if (editable == null)
328                                 throw new NotImplementedException ();
329                         return editable.HasChanges ();
330                 }
331
332                 [Obsolete]
333                 [MonoTODO]
334                 public void LoadXml (string xml)  
335                 {
336                         throw new NotImplementedException ();
337 //                      tree = new XPathDocumentTree (xmlReader);
338 //                      if (acceptChangesOnLoad)
339 //                              AcceptChanges ();
340                 }
341
342                 [MonoTODO]
343                 public void ReadXml (XmlReader reader)
344                 {
345                         if (editable == null)
346                                 throw new NotImplementedException ();
347                         editable.ReadXml (reader);
348                 }
349
350                 [MonoTODO]
351                 public void RejectChanges ()
352                 {
353                         if (editable == null)
354                                 throw new NotImplementedException ();
355                         editable.RejectChanges ();
356                 }
357
358                 [MonoTODO ("Confirm writer settings etc.")]
359                 public void Save (Stream stream)
360                 {
361                         Save (new XmlTextWriter (stream, null));
362                 }
363
364                 [MonoTODO ("Confirm writer settings etc.")]
365                 public void Save (string filename)
366                 {
367                         using (XmlWriter w = new XmlTextWriter (filename, null)) {
368                                 Save (w);
369                         }
370                 }
371
372                 [MonoTODO ("Confirm writer settings etc.")]
373                 public void Save (TextWriter writer)
374                 {
375                         Save (new XmlTextWriter (writer));
376                 }
377
378                 [MonoTODO]
379                 public void Save (XmlWriter writer)
380                 {
381                         throw new NotImplementedException ();
382                 }
383
384                 [MonoTODO]
385                 public XPathNodeIterator SelectNodes (string xpath)
386                 {
387                         return SelectNodes (xpath, null);
388                 }
389
390                 [MonoTODO]
391                 public XPathNodeIterator SelectNodes (XPathExpression expr)
392                 {
393                         throw new NotImplementedException ();
394                 }
395
396                 [MonoTODO]
397                 public XPathNodeIterator SelectNodes (string xpath ,IXmlNamespaceResolver nsResolver)
398                 {
399                         throw new NotImplementedException ();
400                 }
401
402                 [MonoTODO]
403                 public XPathEditableNavigator SelectSingleNode (string xpath)
404                 {
405                         return SelectSingleNode (xpath, null);
406                 }
407
408                 [MonoTODO]
409                 public XPathEditableNavigator SelectSingleNode (XPathExpression expr)
410                 {
411                         throw new NotImplementedException ();
412                 }
413
414                 [MonoTODO]
415                 public XPathEditableNavigator SelectSingleNode (string xpath ,IXmlNamespaceResolver nsResolver)
416                 {
417                         throw new NotImplementedException ();
418                 }
419
420                 [MonoTODO]
421                 public void WriteXml (XmlWriter writer)
422                 {
423                         throw new NotImplementedException ();
424                 }
425
426 #else // !NET_2_0
427
428                 public XPathNavigator CreateNavigator ()
429                 {
430                         return document.CreateNavigator ();
431                 }
432
433 #endif
434
435 #endregion
436
437         }
438
439 }
440
441