2003-03-23 Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
[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 using System.Text.RegularExpressions;
12
13 namespace System.Xml
14 {
15         public class XmlDeclaration : XmlLinkedNode
16         {
17                 string encoding = "UTF-8"; // defaults to UTF-8
18                 string standalone;
19                 string version;
20
21                 protected internal XmlDeclaration (string version, string encoding,
22                                                    string standalone, XmlDocument doc)
23                         : base (doc)
24                 {
25                         if (encoding == null)
26                                 encoding = "";
27
28                         if (standalone == null)
29                                 standalone = "";
30
31                         this.version = version;
32                         this.encoding = encoding;
33                         this.standalone = standalone;
34                 }
35
36                 public string Encoding  {
37                         get { return encoding; } 
38                         set { encoding = (value == null) ? String.Empty : value; }
39                 }
40
41                 public override string InnerText {
42                         get { return Value; }
43                         set { ParseInput (value); }
44                 }
45                 
46                 public override string LocalName {
47                         get { return "xml"; }
48                 }
49
50                 public override string Name {
51                         get { return "xml"; }
52                 }
53
54                 public override XmlNodeType NodeType {
55                         get { return XmlNodeType.XmlDeclaration; }
56                 }
57
58                 public string Standalone {
59                         get { return standalone; }
60                         set {
61                                 if(value != null)
62                                 {
63                                         if (value.ToUpper() == "YES")
64                                                 standalone = "yes";
65                                         if (value.ToUpper() == "NO")
66                                                 standalone = "no";
67                                 }
68                                 else
69                                         standalone = String.Empty;
70                         }
71                 }
72
73                 public override string Value {
74                         get {
75                                 string formatEncoding = "";
76                                 string formatStandalone = "";
77
78                                 if (encoding != String.Empty)
79                                         formatEncoding = String.Format (" encoding=\"{0}\"", encoding);
80
81                                 if (standalone != String.Empty)
82                                         formatStandalone = String.Format (" standalone=\"{0}\"", standalone);
83
84                                 return String.Format ("version=\"{0}\"{1}{2}", Version, formatEncoding, formatStandalone);
85                         }
86                         set { ParseInput (value); }
87                 }
88
89                 public string Version {
90                         get { return version; }
91                 }
92
93                 public override XmlNode CloneNode (bool deep)
94                 {
95                         return new XmlDeclaration (Version, Encoding, standalone, OwnerDocument);
96                 }
97
98                 public override void WriteContentTo (XmlWriter w) {}
99
100                 public override void WriteTo (XmlWriter w)
101                 {
102                         // This doesn't seem to match up very well with w.WriteStartDocument()
103                         // so writing out custom here.
104                         w.WriteRaw (String.Format ("<?xml {0}?>", Value));
105                 }
106
107                 void ParseInput (string input)
108                 {
109                         char [] sep = new char [] {'\'', '"'};
110                         if (!input.Trim ().StartsWith ("version"))
111                                 throw new XmlException("missing \"version\".");
112                         int start = input.IndexOf ("encoding");
113                         int sstart = -1;
114                         if (start > 0) {
115                                 int valStart = input.IndexOfAny (sep, start) + 1;
116                                 int valEnd = input.IndexOfAny (sep, valStart);
117                                 Encoding = input.Substring (valStart, valEnd - valStart);
118                                 sstart = input.IndexOf ("standalone");
119                         }
120                         else
121                                 sstart = input.IndexOf ("standalone");
122
123                         if (sstart > 0) {
124                                 int svalStart = input.IndexOfAny (sep, sstart) + 1;
125                                 int svalEnd = input.IndexOfAny (sep, svalStart);
126                                 Standalone = input.Substring (svalStart, svalEnd - svalStart);
127                         }       // TODO: some error check
128                 }
129         }
130 }