BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[mono.git] / mcs / class / System.XML / System.Xml / XmlWriter.cs
1 //
2 // System.Xml.XmlWriter
3 //
4 // Authors:
5 //   Kral Ferch <kral_ferch@hotmail.com>
6 //   Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
7 //
8 // (C) 2002 Kral Ferch
9 // (C) 2002-2003 Atsushi Enomoto
10 // (C) 2004-2007 Novell, Inc.
11 //
12
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System;
35 using System.Collections;
36 using System.IO;
37 using System.Text;
38 #if !MOONLIGHT
39 using System.Xml.XPath;
40 #endif
41
42 namespace System.Xml
43 {
44         public abstract class XmlWriter : IDisposable
45         {
46 #if NET_2_0
47                 XmlWriterSettings settings;
48 #endif
49
50                 #region Constructors
51
52                 protected XmlWriter () { }
53
54                 #endregion
55
56                 #region Properties
57
58 #if NET_2_0
59                 public virtual XmlWriterSettings Settings {
60                         get { return settings; }
61                 }
62 #endif
63
64                 public abstract WriteState WriteState { get; }
65                 
66
67 #if NET_2_0
68                 public virtual string XmlLang {
69                         get { return null; }
70                 }
71
72                 public virtual XmlSpace XmlSpace {
73                         get { return XmlSpace.None; }
74                 }
75 #else
76                 public abstract string XmlLang { get; }
77
78                 public abstract XmlSpace XmlSpace { get; }
79 #endif
80
81                 #endregion
82
83                 #region Methods
84
85                 public abstract void Close ();
86
87 #if NET_2_0
88                 public static XmlWriter Create (Stream stream)
89                 {
90                         return Create (stream, null);
91                 }
92
93                 public static XmlWriter Create (string file)
94                 {
95                         return Create (file, null);
96                 }
97
98                 public static XmlWriter Create (TextWriter writer)
99                 {
100                         return Create (writer, null);
101                 }
102
103                 public static XmlWriter Create (XmlWriter writer)
104                 {
105                         return Create (writer, null);
106                 }
107
108                 public static XmlWriter Create (StringBuilder builder)
109                 {
110                         return Create (builder, null);
111                 }
112
113                 public static XmlWriter Create (Stream stream, XmlWriterSettings settings)
114                 {
115                         Encoding enc = settings != null ? settings.Encoding : Encoding.UTF8;
116                         return Create (new StreamWriter (stream, enc), settings);
117                 }
118
119                 public static XmlWriter Create (string file, XmlWriterSettings settings)
120                 {
121                         Encoding enc = settings != null ? settings.Encoding : Encoding.UTF8;
122                         return CreateTextWriter (new StreamWriter (file, false, enc), settings, true);
123                 }
124
125                 public static XmlWriter Create (StringBuilder builder, XmlWriterSettings settings)
126                 {
127                         return Create (new StringWriter (builder), settings);
128                 }
129
130                 public static XmlWriter Create (TextWriter writer, XmlWriterSettings settings)
131                 {
132                         if (settings == null)
133                                 settings = new XmlWriterSettings ();
134                         return CreateTextWriter (writer, settings, settings.CloseOutput);
135                 }
136
137                 public static XmlWriter Create (XmlWriter writer, XmlWriterSettings settings)
138                 {
139                         if (settings == null)
140                                 settings = new XmlWriterSettings ();
141                         else
142                                 settings = settings.Clone ();
143
144                         var src = writer.Settings;
145                         if (src == null) {
146                                 settings.ConformanceLevel = ConformanceLevel.Document; // Huh? Why??
147                                 writer = new DefaultXmlWriter (writer);
148                                 writer.settings = settings;
149                         } else {
150                                 ConformanceLevel dst = src.ConformanceLevel;
151                                 switch (src.ConformanceLevel) {
152                                 case ConformanceLevel.Auto:
153                                         dst = settings.ConformanceLevel;
154                                         break;
155                                 case ConformanceLevel.Document:
156                                 case ConformanceLevel.Fragment:
157                                         if (settings.ConformanceLevel != ConformanceLevel.Auto)
158                                                 dst = settings.ConformanceLevel;
159                                         break;
160                                 }
161
162                                 settings.MergeFrom (src);
163
164                                 // It returns a new XmlWriter instance if 1) Settings is null, or 2) Settings ConformanceLevel (or might be other members as well) give significant difference.
165                                 if (src.ConformanceLevel != dst) {
166                                         writer = new DefaultXmlWriter (writer, false);
167                                         writer.settings = settings;
168                                 }
169                         }
170
171                         return writer;
172                 }
173
174                 private static XmlWriter CreateTextWriter (TextWriter writer, XmlWriterSettings settings, bool closeOutput)
175                 {
176                         if (settings == null)
177                                 settings = new XmlWriterSettings ();
178                         XmlTextWriter xtw = new XmlTextWriter (writer, settings, closeOutput);
179                         return Create (xtw, settings);
180                 }
181
182                 protected virtual void Dispose (bool disposing)
183                 {
184                         Close ();
185                 }
186
187                 void IDisposable.Dispose ()
188                 {
189                         Dispose (false);
190                 }
191 #endif
192
193                 public abstract void Flush ();
194
195                 public abstract string LookupPrefix (string ns);
196
197                 private void WriteAttribute (XmlReader reader, bool defattr)
198                 {
199                         if (!defattr && reader.IsDefault)
200                                 return;
201
202                         WriteStartAttribute (reader.Prefix, reader.LocalName, reader.NamespaceURI);
203 #if MOONLIGHT
204                         // no ReadAttributeValue() in 2.1 profile.
205                         WriteString (reader.Value);
206 #else
207                         while (reader.ReadAttributeValue ()) {
208                                 switch (reader.NodeType) {
209                                 case XmlNodeType.Text:
210                                         WriteString (reader.Value);
211                                         break;
212                                 case XmlNodeType.EntityReference:
213                                         WriteEntityRef (reader.Name);
214                                         break;
215                                 }
216                         }
217 #endif
218                         WriteEndAttribute ();
219                 }
220
221                 public virtual void WriteAttributes (XmlReader reader, bool defattr)
222                 {
223                         if(reader == null)
224                                 throw new ArgumentException("null XmlReader specified.", "reader");
225
226                         switch (reader.NodeType) {
227                         case XmlNodeType.XmlDeclaration:
228                                 WriteAttributeString ("version", reader ["version"]);
229                                 if (reader ["encoding"] != null)
230                                         WriteAttributeString ("encoding", reader ["encoding"]);
231                                 if (reader ["standalone"] != null)
232                                         WriteAttributeString ("standalone", reader ["standalone"]);
233                                 break;
234                         case XmlNodeType.Element:
235                                 if (reader.MoveToFirstAttribute ())
236                                         goto case XmlNodeType.Attribute;
237                                 break;
238                         case XmlNodeType.Attribute:
239                                 do {
240                                         WriteAttribute (reader, defattr);
241                                 } while (reader.MoveToNextAttribute ());
242                                 reader.MoveToElement ();
243                                 break;
244                         default:
245                                 throw new XmlException("NodeType is not one of Element, Attribute, nor XmlDeclaration.");
246                         }
247                 }
248
249                 public void WriteAttributeString (string localName, string value)
250                 {
251                         WriteAttributeString ("", localName, null, value);
252                 }
253
254                 public void WriteAttributeString (string localName, string ns, string value)
255                 {
256                         WriteAttributeString ("", localName, ns, value);
257                 }
258
259                 public void WriteAttributeString (string prefix, string localName, string ns, string value)
260                 {
261                         // In MS.NET (1.0), this check is done *here*, not at WriteStartAttribute.
262                         // (XmlTextWriter.WriteStartAttribute("xmlns", "anyname", null) throws an exception.
263
264                         WriteStartAttribute (prefix, localName, ns);
265                         if (value != null && value.Length > 0)
266                                 WriteString (value);
267                         WriteEndAttribute ();
268                 }
269
270                 public abstract void WriteBase64 (byte[] buffer, int index, int count);
271
272 #if NET_2_0
273                 public virtual void WriteBinHex (byte [] buffer, int index, int count)
274                 {
275                         StringWriter sw = new StringWriter ();
276                         XmlConvert.WriteBinHex (buffer, index, count, sw);
277                         WriteString (sw.ToString ());
278                 }
279 #else
280                 public abstract void WriteBinHex (byte[] buffer, int index, int count);
281 #endif
282
283                 public abstract void WriteCData (string text);
284
285                 public abstract void WriteCharEntity (char ch);
286
287                 public abstract void WriteChars (char[] buffer, int index, int count);
288
289                 public abstract void WriteComment (string text);
290
291                 public abstract void WriteDocType (string name, string pubid, string sysid, string subset);
292
293                 public void WriteElementString (string localName, string value)
294                 {
295                         WriteStartElement(localName);
296                         if (value != null && value.Length > 0)
297                                 WriteString(value);
298                         WriteEndElement();
299                 }
300
301                 public void WriteElementString (string localName, string ns, string value)
302                 {
303                         WriteStartElement(localName, ns);
304                         if (value != null && value.Length > 0)
305                                 WriteString(value);
306                         WriteEndElement();
307                 }
308
309 #if NET_2_0
310                 public void WriteElementString (string prefix, string localName, string ns, string value)
311                 {
312                         WriteStartElement(prefix, localName, ns);
313                         if (value != null && value.Length > 0)
314                                 WriteString(value);
315                         WriteEndElement();
316                 }
317 #endif
318
319                 public abstract void WriteEndAttribute ();
320
321                 public abstract void WriteEndDocument ();
322
323                 public abstract void WriteEndElement ();
324
325                 public abstract void WriteEntityRef (string name);
326
327                 public abstract void WriteFullEndElement ();
328
329 #if NET_2_0
330                 public virtual void WriteName (string name)
331                 {
332                         WriteNameInternal (name);
333                 }
334
335                 public virtual void WriteNmToken (string name)
336                 {
337                         WriteNmTokenInternal (name);
338                 }
339
340                 public virtual void WriteQualifiedName (string localName, string ns)
341                 {
342                         WriteQualifiedNameInternal (localName, ns);
343                 }
344 #else
345                 public abstract void WriteName (string name);
346
347                 public abstract void WriteNmToken (string name);
348
349                 public abstract void WriteQualifiedName (string localName, string ns);
350 #endif
351
352                 internal void WriteNameInternal (string name)
353                 {
354 #if NET_2_0
355                         switch (Settings.ConformanceLevel) {
356                         case ConformanceLevel.Document:
357                         case ConformanceLevel.Fragment:
358                                 XmlConvert.VerifyName (name);
359                                 break;
360                         }
361 #else
362                         XmlConvert.VerifyName (name);
363 #endif
364                         WriteString (name);
365                 }
366
367                 internal virtual void WriteNmTokenInternal (string name)
368                 {
369                         bool valid = true;
370 #if NET_2_0
371                         switch (Settings.ConformanceLevel) {
372                         case ConformanceLevel.Document:
373                         case ConformanceLevel.Fragment:
374                                 valid = XmlChar.IsNmToken (name);
375                                         break;
376                         }
377 #else
378                         valid = XmlChar.IsNmToken (name);
379 #endif
380                         if (!valid)
381                                 throw new ArgumentException ("Argument name is not a valid NMTOKEN.");
382                         WriteString (name);
383                 }
384
385                 internal void WriteQualifiedNameInternal (string localName, string ns)
386                 {
387                         if (localName == null || localName == String.Empty)
388                                 throw new ArgumentException ();
389                         if (ns == null)
390                                 ns = String.Empty;
391
392 #if NET_2_0
393                         if (Settings != null) {
394                                 switch (Settings.ConformanceLevel) {
395                                 case ConformanceLevel.Document:
396                                 case ConformanceLevel.Fragment:
397                                         XmlConvert.VerifyNCName (localName);
398                                         break;
399                                 }
400                         }
401                         else
402                                 XmlConvert.VerifyNCName (localName);
403 #else
404                         XmlConvert.VerifyNCName (localName);
405 #endif
406
407                         string prefix = ns.Length > 0 ? LookupPrefix (ns) : String.Empty;
408                         if (prefix == null)
409                                 throw new ArgumentException (String.Format ("Namespace '{0}' is not declared.", ns));
410
411                         if (prefix != String.Empty) {
412                                 WriteString (prefix);
413                                 WriteString (":");
414                                 WriteString (localName);
415                         }
416                         else
417                                 WriteString (localName);
418                 }
419
420 #if !MOONLIGHT
421                 public virtual void WriteNode (XPathNavigator navigator, bool defattr)
422                 {
423                         if (navigator == null)
424                                 throw new ArgumentNullException ("navigator");
425                         switch (navigator.NodeType) {
426                         case XPathNodeType.Attribute:
427                                 // no operation
428                                 break;
429                         case XPathNodeType.Namespace:
430                                 // no operation
431                                 break;
432                         case XPathNodeType.Text:
433                                 WriteString (navigator.Value);
434                                 break;
435                         case XPathNodeType.SignificantWhitespace:
436                                 WriteWhitespace (navigator.Value);
437                                 break;
438                         case XPathNodeType.Whitespace:
439                                 WriteWhitespace (navigator.Value);
440                                 break;
441                         case XPathNodeType.Comment:
442                                 WriteComment (navigator.Value);
443                                 break;
444                         case XPathNodeType.ProcessingInstruction:
445                                 WriteProcessingInstruction (navigator.Name, navigator.Value);
446                                 break;
447                         case XPathNodeType.Root:
448                                 if (navigator.MoveToFirstChild ()) {
449                                         do {
450                                                 WriteNode (navigator, defattr);
451                                         } while (navigator.MoveToNext ());
452                                         navigator.MoveToParent ();
453                                 }
454                                 break;
455                         case XPathNodeType.Element:
456                                 WriteStartElement (navigator.Prefix, navigator.LocalName, navigator.NamespaceURI);
457                                 if (navigator.MoveToFirstNamespace (XPathNamespaceScope.Local)) {
458                                         do {
459                                                 if (defattr || navigator.SchemaInfo == null || navigator.SchemaInfo.IsDefault)
460                                                         WriteAttributeString (navigator.Prefix,
461                                                                 navigator.LocalName == String.Empty ? "xmlns" : navigator.LocalName,
462                                                                 "http://www.w3.org/2000/xmlns/",
463                                                                 navigator.Value);
464                                         } while (navigator.MoveToNextNamespace (XPathNamespaceScope.Local));
465                                         navigator.MoveToParent ();
466                                 }
467                                 if (navigator.MoveToFirstAttribute ()) {
468                                         do {
469                                                 if (defattr || navigator.SchemaInfo == null || navigator.SchemaInfo.IsDefault)
470                                                         WriteAttributeString (navigator.Prefix, navigator.LocalName, navigator.NamespaceURI, navigator.Value);
471
472                                         } while (navigator.MoveToNextAttribute ());
473                                         navigator.MoveToParent ();
474                                 }
475                                 if (navigator.MoveToFirstChild ()) {
476                                         do {
477                                                 WriteNode (navigator, defattr);
478                                         } while (navigator.MoveToNext ());
479                                         navigator.MoveToParent ();
480                                 }
481                                 if (navigator.IsEmptyElement)
482                                         WriteEndElement ();
483                                 else
484                                         WriteFullEndElement ();
485                                 break;
486                         default:
487                                 throw new NotSupportedException ();
488                         }
489                 }
490 #endif
491
492                 public virtual void WriteNode (XmlReader reader, bool defattr)
493                 {
494                         if (reader == null)
495                                 throw new ArgumentException ();
496
497                         if (reader.ReadState == ReadState.Initial) {
498                                 reader.Read ();
499                                 do {
500                                         WriteNode (reader, defattr);
501                                 } while (!reader.EOF);
502                                 return;
503                         }
504
505                         switch (reader.NodeType) {
506                         case XmlNodeType.Element:
507                                 WriteStartElement (reader.Prefix, reader.LocalName, reader.NamespaceURI);
508 #if false
509                                 WriteAttributes (reader, defattr);
510                                 reader.MoveToElement ();
511 #else
512                                 // Well, I found that MS.NET took this way, since
513                                 // there was a error-prone SgmlReader that fails
514                                 // MoveToNextAttribute().
515                                 if (reader.HasAttributes) {
516                                         for (int i = 0; i < reader.AttributeCount; i++) {
517                                                 reader.MoveToAttribute (i);
518                                                 WriteAttribute (reader, defattr);
519                                         }
520                                         reader.MoveToElement ();
521                                 }
522 #endif
523                                 if (reader.IsEmptyElement)
524                                         WriteEndElement ();
525                                 else {
526                                         int depth = reader.Depth;
527                                         reader.Read ();
528                                         if (reader.NodeType != XmlNodeType.EndElement) {
529                                                 do {
530                                                         WriteNode (reader, defattr);
531                                                 } while (depth < reader.Depth);
532                                         }
533                                         WriteFullEndElement ();
534                                 }
535                                 break;
536                         // In case of XmlAttribute, don't proceed reader, and it will never be written.
537                         case XmlNodeType.Attribute:
538                                 return;
539                         case XmlNodeType.Text:
540                                 WriteString (reader.Value);
541                                 break;
542                         case XmlNodeType.CDATA:
543                                 WriteCData (reader.Value);
544                                 break;
545                         case XmlNodeType.EntityReference:
546                                 WriteEntityRef (reader.Name);
547                                 break;
548                         case XmlNodeType.XmlDeclaration:
549                                 // LAMESPEC: It means that XmlWriter implementation _must not_ check
550                                 // whether PI name is "xml" (it is XML error) or not.
551                         case XmlNodeType.ProcessingInstruction:
552                                 WriteProcessingInstruction (reader.Name, reader.Value);
553                                 break;
554                         case XmlNodeType.Comment:
555                                 WriteComment (reader.Value);
556                                 break;
557                         case XmlNodeType.DocumentType:
558                                 WriteDocType (reader.Name,
559                                         reader ["PUBLIC"], reader ["SYSTEM"], reader.Value);
560                                 break;
561                         case XmlNodeType.SignificantWhitespace:
562                                 goto case XmlNodeType.Whitespace;
563                         case XmlNodeType.Whitespace:
564                                 WriteWhitespace (reader.Value);
565                                 break;
566                         case XmlNodeType.EndElement:
567                                 WriteFullEndElement ();
568                                 break;
569                         case XmlNodeType.EndEntity:
570                                 break;
571                         case XmlNodeType.None:
572                                 break;  // Do nothing, nor reporting errors.
573                         default:
574                                 throw new XmlException ("Unexpected node " + reader.Name + " of type " + reader.NodeType);
575                         }
576                         reader.Read ();
577                 }
578
579                 public abstract void WriteProcessingInstruction (string name, string text);
580
581                 public abstract void WriteRaw (string data);
582
583                 public abstract void WriteRaw (char[] buffer, int index, int count);
584
585 #if NET_2_0
586                 public void WriteStartAttribute (string localName)
587                 {
588                         WriteStartAttribute (null, localName, null);
589                 }
590 #endif
591
592                 public void WriteStartAttribute (string localName, string ns)
593                 {
594                         WriteStartAttribute (null, localName, ns);
595                 }
596
597                 public abstract void WriteStartAttribute (string prefix, string localName, string ns);
598
599                 public abstract void WriteStartDocument ();
600
601                 public abstract void WriteStartDocument (bool standalone);
602
603                 public void WriteStartElement (string localName)
604                 {
605                         WriteStartElement (null, localName, null);
606                 }
607
608                 public void WriteStartElement (string localName, string ns)
609                 {
610                         WriteStartElement (null, localName, ns);
611                 }
612
613                 public abstract void WriteStartElement (string prefix, string localName, string ns);
614
615                 public abstract void WriteString (string text);
616
617                 public abstract void WriteSurrogateCharEntity (char lowChar, char highChar);
618
619                 public abstract void WriteWhitespace (string ws);
620
621 #if NET_2_0
622                 public virtual void WriteValue (bool value)
623                 {
624                         WriteString (XQueryConvert.BooleanToString (value));
625                 }
626
627                 public virtual void WriteValue (DateTime value)
628                 {
629                         WriteString (XmlConvert.ToString (value));
630                 }
631
632                 public virtual void WriteValue (decimal value)
633                 {
634                         WriteString (XQueryConvert.DecimalToString (value));
635                 }
636
637                 public virtual void WriteValue (double value)
638                 {
639                         WriteString (XQueryConvert.DoubleToString (value));
640                 }
641
642                 public virtual void WriteValue (int value)
643                 {
644                         WriteString (XQueryConvert.IntToString (value));
645                 }
646
647                 public virtual void WriteValue (long value)
648                 {
649                         WriteString (XQueryConvert.IntegerToString (value));
650                 }
651
652                 public virtual void WriteValue (object value)
653                 {
654                         if (value == null)
655                                 throw new ArgumentNullException ("value");
656
657                         if (value is string)
658                                 WriteString ((string) value);
659                         else if (value is bool)
660                                 WriteValue ((bool) value);
661                         else if (value is byte)
662                                 WriteValue ((int) value);
663                         else if (value is byte [])
664                                 WriteBase64 ((byte []) value, 0, ((byte []) value).Length);
665                         else if (value is char [])
666                                 WriteChars ((char []) value, 0, ((char []) value).Length);
667                         else if (value is DateTime)
668                                 WriteValue ((DateTime) value);
669                         else if (value is decimal)
670                                 WriteValue ((decimal) value);
671                         else if (value is double)
672                                 WriteValue ((double) value);
673                         else if (value is short)
674                                 WriteValue ((int) value);
675                         else if (value is int)
676                                 WriteValue ((int) value);
677                         else if (value is long)
678                                 WriteValue ((long) value);
679                         else if (value is float)
680                                 WriteValue ((float) value);
681                         else if (value is TimeSpan) // undocumented
682                                 WriteString (XmlConvert.ToString ((TimeSpan) value));
683                         else if (value is Uri)
684                                 WriteString (((Uri) value).ToString ());
685                         else if (value is XmlQualifiedName) {
686                                 XmlQualifiedName qname = (XmlQualifiedName) value;
687                                 if (!qname.Equals (XmlQualifiedName.Empty)) {
688                                         if (qname.Namespace.Length > 0 && LookupPrefix (qname.Namespace) == null)
689                                                 throw new InvalidCastException (String.Format ("The QName '{0}' cannot be written. No corresponding prefix is declared", qname));
690                                         WriteQualifiedName (qname.Name, qname.Namespace);
691                                 }
692                                 else
693                                         WriteString (String.Empty);
694                         }
695                         else if (value is IEnumerable) {
696                                 bool follow = false;
697                                 foreach (object obj in (IEnumerable) value) {
698                                         if (follow)
699                                                 WriteString (" ");
700                                         else
701                                                 follow = true;
702                                         WriteValue (obj);
703                                 }
704                         }
705                         else
706                                 throw new InvalidCastException (String.Format ("Type '{0}' cannot be cast to string", value.GetType ()));
707                 }
708
709                 public virtual void WriteValue (float value)
710                 {
711                         WriteString (XQueryConvert.FloatToString (value));
712                 }
713
714                 public virtual void WriteValue (string value)
715                 {
716                         WriteString (value);
717                 }
718 #endif
719
720                 #endregion
721         }
722 }