2002-08-16 Jason Diamond <jason@injektilo.org>
[mono.git] / mcs / class / System.XML / System.Xml / XmlDeclaration.cs
1 //
2 // System.Xml.XmlDeclaration
3 //
4 // Author:
5 //      Duncan Mak  (duncan@ximian.com)
6 //
7 // (C) Ximian, Inc.
8
9 using System;
10 using System.Xml;
11
12 namespace System.Xml
13 {
14         public class XmlDeclaration : XmlLinkedNode
15         {
16                 string encoding = "UTF-8"; // defaults to UTF-8
17                 string standalone;
18                 string version;
19
20                 protected internal XmlDeclaration (string version, string encoding,
21                                                    string standalone, XmlDocument doc)
22                         : base (doc)
23                 {
24                         if (encoding == null)
25                                 encoding = "";
26
27                         if (standalone == null)
28                                 standalone = "";
29
30                         this.version = version;
31                         this.encoding = encoding;
32                         this.standalone = standalone;
33                 }
34
35                 public string Encoding  {
36                         get { return encoding; } 
37                         set { encoding = (value == null) ? String.Empty : value; }
38                 }
39
40                 public override string InnerText {
41                         get { return Value; }
42                         set { ParseInput (value); }
43                 }
44                 
45                 public override string LocalName {
46                         get { return "xml"; }
47                 }
48
49                 public override string Name {
50                         get { return "xml"; }
51                 }
52
53                 public override XmlNodeType NodeType {
54                         get { return XmlNodeType.XmlDeclaration; }
55                 }
56
57                 public string Standalone {
58                         get { return standalone; }
59                         set {
60                                 if (value.ToUpper() == "YES")
61                                         standalone = "yes";
62                                 if (value.ToUpper() == "NO")
63                                         standalone = "no";
64                         }
65                 }
66
67                 public override string Value {
68                         get {
69                                 string formatEncoding = "";
70                                 string formatStandalone = "";
71
72                                 if (encoding != String.Empty)
73                                         formatEncoding = String.Format (" encoding=\"{0}\"", encoding);
74
75                                 if (standalone != String.Empty)
76                                         formatStandalone = String.Format (" standalone=\"{0}\"", standalone);
77
78                                 return String.Format ("version=\"{0}\"{1}{2}", Version, formatEncoding, formatStandalone);
79                         }
80                         set { ParseInput (value); }
81                 }
82
83                 public string Version {
84                         get { return version; }
85                 }
86
87                 public override XmlNode CloneNode (bool deep)
88                 {
89                         return new XmlDeclaration (Version, Encoding, standalone, OwnerDocument);
90                 }
91
92                 public override void WriteContentTo (XmlWriter w) {}
93
94                 public override void WriteTo (XmlWriter w)
95                 {
96                         // This doesn't seem to match up very well with w.WriteStartDocument()
97                         // so writing out custom here.
98                         w.WriteRaw (String.Format ("<?xml {0}?>", Value));
99                 }
100
101                 void ParseInput (string input)
102                 {                       
103                         Encoding = input.Split (new char [] { ' ' }) [1].Split (new char [] { '=' }) [1];
104                         Standalone = input.Split (new char [] { ' ' }) [2].Split (new char [] { '=' }) [1];
105                 }
106         }
107 }