a644b076656e3f864e6bacf6c46f3017c83f8c49
[mono.git] / mcs / class / System.XML / System.Xml / XmlDocument.cs
1 // -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 //
3 // System.Xml.XmlDocument
4 //
5 // Author:
6 //   Daniel Weber (daniel-weber@austin.rr.com)
7 //
8 // (C) 2001 Daniel Weber
9
10 using System;
11 using System.IO;
12
13 namespace System.Xml
14 {
15
16         public delegate void XmlNodeChangedEventHandler (XmlNodeChangedEventArgs args);
17
18         /// <summary>
19         /// Abstract class XmlNodeList.
20         /// </summary>
21         public class XmlDocument : XmlNode
22         {
23                 // Private data members
24                 XmlResolver _resolver = null;
25
26                 // Public events
27                 //===========================================================================
28                 public event XmlNodeChangedEventHandler NodeChanged;
29
30                 public event XmlNodeChangedEventHandler NodeChanging;
31
32                 public event XmlNodeChangedEventHandler NodeInserted;
33
34                 public event XmlNodeChangedEventHandler NodeInserting;
35
36                 public event XmlNodeChangedEventHandler NodeRemoved;
37
38                 public event XmlNodeChangedEventHandler NodeRemoving;
39
40                 // public properties
41
42                 /// <summary>
43                 /// Get the base URI for this document (the location from where the document was loaded)
44                 /// </summary>
45                 /// <example>If a document was loaded with doc.Load("c:\tmp\mydoc.xml"),
46                 /// then BaseURI would hold "c:\tmp\mydoc.xml"</example>
47                 public override string BaseURI
48                 {
49                         get
50                         {
51                                 // TODO - implement XmlDocument.BaseURI {get;}
52                                 throw new NotImplementedException("BaseURI.get not implemented");
53                         }
54                 }
55
56                 /// <summary>
57                 /// Get the root element for the document.  If no root exists, null is returned.
58                 /// </summary>
59                 public XmlElement DocumentElement
60                 {
61                         get
62                         {
63                                 XmlNode node = FirstChild;
64
65                                 while (node != null) {
66                                         if (node is XmlElement)
67                                                 break;
68                                         node = node.NextSibling;
69                                 }
70
71                                 return node != null ? node as XmlElement : null;
72                         }
73                 }
74
75                 /// <summary>
76                 /// Gets the node containing the DOCTYPE declaration.
77                 /// </summary>
78                 public virtual XmlDocumentType DocumentType
79                 {
80                         get
81                         {
82                                 // TODO - implement XmlDocument.DocumentType
83                                 throw new NotImplementedException("XmlDocument.DocumentType not implemented");
84                         }
85                 }
86
87
88                 /// <summary>
89                 /// Get the XmlImplemenation for the current document.
90                 /// </summary>
91                 public XmlImplementation Implementation
92                 {
93                         get
94                         {
95                                 // TODO - implement XmlDocument.Implementation
96                                 throw new NotImplementedException("Implementation not implemented");
97                         }
98                 }
99
100
101                 /// <summary>
102                 /// Get/Set the markup representing the children of the document.
103                 /// </summary>
104                 public override string InnerXml
105                 {
106                         get
107                         {
108                                 // TODO - implement XmlDocument.InnerXml {get;}
109                                 throw new NotImplementedException("InnerXml get not implemented");
110                         }
111                         set
112                         {
113                                 // TODO - implement XmlDocument.InnerXml {set;}
114                                 throw new NotImplementedException("InnerXml set not implemented");
115                         }
116                 }
117
118                 /// <summary>
119                 /// Get a value indicating if the document is read-only.
120                 /// </summary>
121                 public override bool IsReadOnly
122                 {
123                         get
124                         {
125                                 return false;
126                         }
127                 }
128
129                 /// <summary>
130                 /// Get the local name of the node.  For documents, returns "#document"
131                 /// </summary>
132                 public override string LocalName {
133                         get
134                         {
135                                 return "#document";
136                         }
137                 }
138
139                 /// <summary>
140                 /// Get the qualified name of the node.  For documents, returns "#document"
141                 /// </summary>
142                 public override string Name
143                 {
144                         get
145                         {
146                                 return "#document";
147                         }
148                 }
149
150                 public XmlNameTable NameTable
151                 {
152                         get
153                         {
154                                 // TODO - implement XmlDocument.NameTable {get;}
155                                 throw new NotImplementedException("NameTable get not implemented");
156                         }
157                 }
158
159
160                 public override XmlNodeType NodeType
161                 {
162                         get
163                         {
164                                 return XmlNodeType.Document;
165                         }
166                 }
167
168                 /// <summary>
169                 /// Returns OwnerDocument.  For an XmlDocument, this property is always null.
170                 /// </summary>
171                 public override XmlDocument OwnerDocument
172                 {
173                         get
174                         {
175                                 return null;
176                         }
177                 }
178
179                 public bool PreserveWhitespace
180                 {
181                         get
182                         {
183                                 // TODO - implement XmlDocument.PreserveWhitespace {get;}
184                                 throw new NotImplementedException("PreserveWhitespace get not implemented");
185                         }
186                         set
187                         {
188                                 // TODO - implement XmlDocument.PreserveWhitespace {set;}
189                                 throw new NotImplementedException("PreserveWhitespace set not implemented");
190                         }
191                 }
192
193                 public XmlResolver XmlResolver
194                 {
195                         set
196                         {
197                                 // TODO - Finish/test XmlDocument.XmlResolver {set;}
198                                 _resolver = value;
199                         }
200                 }
201
202                 // Public Methods
203                 //===========================================================================
204                 public override XmlNode CloneNode(bool deep)
205                 {
206                         // TODO - implement XmlDocument.CloneNode(bool)
207                         throw new NotImplementedException("CloneNode(bool) not implemented");
208                 }
209
210                 public XmlAttribute CreateAttribute(string name)
211                 {
212                         // TODO - implement XmlDocument.CreateAttribute(string name)
213                         throw new NotImplementedException("CreateAttribute(string name) not implemented");
214                 }
215
216                 public XmlAttribute CreateAttribute(string qualifiedName,string namespaceURI)
217                 {
218                         // TODO - implement XmlDocument.CreateAttribute(string, string)
219                         throw new NotImplementedException("CreateAttribute(string, string) not implemented");
220                 }
221
222                 public virtual XmlAttribute CreateAttribute(
223                         string prefix,
224                         string localName,
225                         string namespaceURI
226                         )
227                 {
228                         // TODO - implement XmlDocument.CreateAttribute(prefix, localName, namespaceURI)
229                         throw new NotImplementedException("CreateAttribute(prefix, localName, namespaceURI) not implemented");
230                 }
231
232                 public virtual XmlCDataSection CreateCDataSection(string data)
233                 {
234                         // TODO - implement XmlDocument.CreateCDataSection(string data)
235                         throw new NotImplementedException("CreateCDataSection(string data) not implemented");
236                 }
237
238
239                 public virtual XmlComment CreateComment(string data)
240                 {
241                         // TODO - implement XmlDocument.CreateComment(string data)
242                         throw new NotImplementedException("CreateComment(string data) not implemented");
243                 }
244
245                 public virtual XmlDocumentFragment CreateDocumentFragment()
246                 {
247                         // TODO - implement XmlDocument.CreateDocumentFragment
248                         throw new NotImplementedException("CreateDocumentFragment not implemented");
249                 }
250
251                 public virtual XmlDocumentType CreateDocumentType(
252                         string name,
253                         string publicId,
254                         string systemId,
255                         string internalSubset
256                         )
257                 {
258                         // TODO - implement XmlDocument.CreateDocumentType
259                         throw new NotImplementedException("CreateDocumentType not implemented");
260                 }
261
262                 public XmlElement CreateElement(string name)
263                 {
264                         // TODO - implement XmlDocument.CreateElement(string name)
265                         throw new NotImplementedException("CreateElement(string name) not implemented");
266                 }
267
268                 public XmlElement CreateElement(
269                         string qualifiedName,
270                         string namespaceURI
271                         )
272                 {
273                         // TODO - implement XmlDocument.CreateElement(string qualifiedName,     string namespaceURI)
274                         throw new NotImplementedException("CreateElement(string qualifiedName,  string namespaceURI) not implemented");
275                 }
276
277                 public virtual XmlElement CreateElement(
278                         string prefix,
279                         string localName,
280                         string namespaceURI
281                         )
282                 {
283                         return new XmlElement(prefix, localName, namespaceURI, this);
284                 }
285
286
287                 public virtual XmlEntityReference CreateEntityReference(string name)
288                 {
289                         // TODO - implement XmlDocument.CreateEntityReference
290                         throw new NotImplementedException("XmlDocument.CreateEntityReference not implemented.");
291                 }
292
293                 public virtual XmlNode CreateNode(
294                         string nodeTypeString,
295                         string name,
296                         string namespaceURI
297                         )
298                 {
299                         // TODO - implement XmlDocument.CreateNode(string, string, string)
300                         throw new NotImplementedException("XmlDocument.CreateNode not implemented.");
301                 }
302
303                 public virtual XmlNode CreateNode(
304                         XmlNodeType type,
305                         string name,
306                         string namespaceURI
307                         )
308                 {
309                         // TODO - implement XmlDocument.CreateNode(XmlNodeType, string, string)
310                         throw new NotImplementedException("XmlDocument.CreateNode not implemented.");
311                 }
312
313                 public virtual XmlNode CreateNode(
314                         XmlNodeType type,
315                         string prefix,
316                         string name,
317                         string namespaceURI
318                         )
319                 {
320                         // TODO - implement XmlDocument.CreateNode(XmlNodeType, string, string, string)
321                         throw new NotImplementedException("XmlDocument.CreateNode not implemented.");
322                 }
323
324                 public virtual XmlProcessingInstruction CreateProcessingInstruction(
325                         string target,
326                         string data
327                         )
328                 {
329                         // TODO - implement XmlDocument.CreateProcessingInstruction
330                         throw new NotImplementedException("XmlDocument.CreateProcessingInstruction not implemented.");
331                 }
332
333                 public virtual XmlSignificantWhitespace CreateSignificantWhitespace(string text )
334                 {
335                         // TODO - implement XmlDocument.CreateSignificantWhitespace
336                         throw new NotImplementedException("XmlDocument.CreateSignificantWhitespace not implemented.");
337                 }
338
339                 public virtual XmlText CreateTextNode(string text)
340                 {
341                         // TODO - implement XmlDocument.CreateTextNode
342                         throw new NotImplementedException("XmlDocument.CreateTextNode not implemented.");
343                 }
344
345                 public virtual XmlWhitespace CreateWhitespace(string text)
346                 {
347                         // TODO - implement XmlDocument.CreateWhitespace
348                         throw new NotImplementedException("XmlDocument.CreateWhitespace not implemented.");
349                 }
350
351                 public virtual XmlDeclaration CreateXmlDeclaration(
352                         string version,
353                         string encoding,
354                         string standalone
355                         )
356                 {
357                         // TODO - implement XmlDocument.CreateXmlDeclaration
358                         throw new NotImplementedException("XmlDocument.CreateXmlDeclaration not implemented.");
359                 }
360
361                 public virtual XmlElement GetElementById(string elementId)
362                 {
363                         // TODO - implement XmlDocument.GetElementById
364                         throw new NotImplementedException("XmlDocument.GetElementById not implemented.");
365                 }
366
367                 public virtual XmlNodeList GetElementsByTagName(string name)
368                 {
369                         // TODO - implement XmlDocument.GetElementsByTagName(name)
370                         throw new NotImplementedException("XmlDocument.GetElementsByTagName not implemented.");
371                 }
372
373                 public virtual XmlNodeList GetElementsByTagName(
374                         string localName,
375                         string namespaceURI
376                         )
377                 {
378                         // TODO - implement XmlDocument.GetElementsByTagName(localName, namespaceURI)
379                         throw new NotImplementedException("XmlDocument.GetElementsByTagName not implemented.");
380                 }
381
382                 public virtual XmlNode ImportNode(
383                         XmlNode node,
384                         bool deep
385                         )
386                 {
387                         // TODO - implement XmlDocument.ImportNode
388                         throw new NotImplementedException("XmlDocument.ImportNode not implemented.");
389                 }
390
391                 public virtual void Load(Stream inStream)
392                 {
393                         // TODO - implement XmlDocument.Load(Stream)
394                         throw new NotImplementedException("XmlDocument.Load(Stream) not implemented.");
395                 }
396
397                 public virtual void Load(string filename)
398                 {
399                         // TODO - implement XmlDocument.Load(string)
400                         throw new NotImplementedException("XmlDocument.Load(string) not implemented.");
401                 }
402
403                 public virtual void Load(TextReader txtReader)
404                 {
405                         // TODO - implement XmlDocument.Load(TextReader)
406                         throw new NotImplementedException("XmlDocument.Load(TextReader) not implemented.");
407                 }
408
409                 public virtual void Load(XmlReader reader)
410                 {
411                         // TODO - implement XmlDocument.Load(XmlReader)
412                         throw new NotImplementedException("XmlDocument.Load(XmlReader) not implemented.");
413                 }
414
415                 public virtual void LoadXml(string xml)
416                 {
417                         XmlReader       xmlReader = new XmlTextReader(new StringReader(xml));
418                         XmlNode         currentNode = this;
419                         XmlNode         newNode;
420
421                         // Reset our document
422                         // For now this just means removing all our children but later this
423                         // may turn out o need to call a private method that resets other things
424                         // like properties we have, etc.
425                         RemoveAll();
426
427                         // Wrapping in try/catch for now until XmlTextReader starts throwing XmlException
428                         try 
429                         {
430                                 while (xmlReader.Read())
431                                 {
432                                         switch(xmlReader.NodeType)
433                                         {
434                                                 case XmlNodeType.Element:
435                                                         newNode = CreateElement(xmlReader.Name, xmlReader.LocalName, xmlReader.NamespaceURI);
436                                                         currentNode.AppendChild(newNode);
437                                                         if (!xmlReader.IsEmptyElement)
438                                                         {
439                                                                 currentNode = newNode;
440                                                         }
441                                                         break;
442                                                 
443                                                 case XmlNodeType.Text:
444                                                         newNode = CreateTextNode(xmlReader.Value);
445                                                         currentNode.AppendChild(newNode);
446                                                         break;
447
448                                                 case XmlNodeType.EndElement:
449                                                         currentNode = currentNode.ParentNode;
450                                                         break;
451                                         }
452                                 }
453                         }
454                         catch(Exception e)
455                         {
456                                 throw new XmlException(e.Message, e);
457                         }
458                 }
459
460                 public virtual void Save(Stream outStream)
461                 {
462                         // TODO - implement XmlDocument.Save(Stream)
463                         throw new NotImplementedException("XmlDocument.Save(Stream) not implemented.");
464                 }
465
466                 public virtual void Save(string filename)
467                 {
468                         // TODO - implement XmlDocument.Save(string)
469                         throw new NotImplementedException("XmlDocument.Save(string) not implemented.");
470                 }
471
472                 public virtual void Save(TextWriter writer)
473                 {
474                         // TODO - implement XmlDocument.Save(TextWriter)
475                         throw new NotImplementedException("XmlDocument.Save(TextWriter) not implemented.");
476                 }
477
478                 public virtual void Save(XmlWriter writer)
479                 {
480                         // TODO - implement XmlDocument.Save(XmlWriter)
481                         throw new NotImplementedException("XmlDocument.Save(XmlWriter) not implemented.");
482                 }
483
484                 public override void WriteContentTo(XmlWriter w)
485                 {
486                         // TODO - implement XmlDocument.WriteContentTo
487                         throw new NotImplementedException("XmlDocument.WriteContentTo not implemented.");
488                 }
489
490                 public override void WriteTo(XmlWriter w)
491                 {
492                         // TODO - implement XmlDocument.WriteTo
493                         throw new NotImplementedException("XmlDocument.WriteTo not implemented.");
494                 }
495
496
497                 // Internal functions
498                 //===========================================================================
499                 internal void onNodeChanging(XmlNode node, XmlNode Parent)
500                 {
501                         if (NodeInserting != null)
502                                 NodeChanging( new XmlNodeChangedEventArgs(XmlNodeChangedAction.Change,
503                                         node, Parent, Parent));
504                 }
505
506                 internal void onNodeChanged(XmlNode node, XmlNode Parent)
507                 {
508                         if (NodeChanged != null)
509                                 NodeInserted( new XmlNodeChangedEventArgs(XmlNodeChangedAction.Change,
510                                         node, Parent, Parent));
511                 }
512
513                 internal void onNodeInserting(XmlNode node, XmlNode newParent)
514                 {
515                         if (NodeInserting != null)
516                                 NodeInserting( new XmlNodeChangedEventArgs(XmlNodeChangedAction.Insert,
517                                         node, null, newParent));
518                 }
519
520                 internal void onNodeInserted(XmlNode node, XmlNode newParent)
521                 {
522                         if (NodeInserted != null)
523                                 NodeInserted( new XmlNodeChangedEventArgs(XmlNodeChangedAction.Insert,
524                                         node, null, newParent));
525                 }
526
527                 internal void onNodeRemoving(XmlNode node, XmlNode oldParent)
528                 {
529                         if (NodeRemoving != null)
530                                 NodeRemoving(new XmlNodeChangedEventArgs(XmlNodeChangedAction.Remove,
531                                         node, oldParent, null));
532                 }
533
534                 internal void onNodeRemoved(XmlNode node, XmlNode oldParent)
535                 {
536                         if (NodeRemoved != null)
537                                 NodeRemoved(new XmlNodeChangedEventArgs(XmlNodeChangedAction.Remove,
538                                         node, oldParent, null));
539
540                 }
541
542                 // Constructors
543                 //===========================================================================
544                 public XmlDocument() : base(null)
545                 {
546                         FOwnerDocument = this;
547                 }
548
549
550         }
551 }