2002-12-24 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 //                      Encoding = input.Split (new char [] { ' ' }) [1].Split (new char [] { '=' }) [1];
110 //                      Standalone = input.Split (new char [] { ' ' }) [2].Split (new char [] { '=' }) [1];
111                         Match m = XmlDeclRegex.Match(input);
112                         if(!m.Success)
113                                 throw new XmlException("illegal XML declaration format.");
114 //                      Version = m.Result("${ver}");
115                         Encoding = m.Result("${enc}");
116                         Standalone = m.Result("${sta}");
117                 }
118
119                 // This regular expression matches XMLDecl of XML specification BNF[23]
120                 static Regex xmlDeclRegex;
121                 Regex XmlDeclRegex \r
122                 {
123                         get \r
124                         {
125                                 if(xmlDeclRegex == null) xmlDeclRegex = new Regex(allMatch, RegexOptions.Compiled);
126                                 return xmlDeclRegex;
127                         }
128                 }
129
130                 // This code makes some loss, but you may understand a bit easily.
131                 const string verMatch = "\\s*version\\s*=\\s*(\\'(?<ver>.*?)\\'|\\\"(?<ver>.*?)\")";
132                 const string encMatch = "\\s*encoding\\s*=\\s*(\\'(?<enc>.*?)\\'|\\\"(?<enc>.*?)\")";
133                 const string staMatch = "\\s*standalone\\s*=\\s*(\\'(?<sta>.*?)\\'|\\\"(?<sta>.*?)\")";
134                 const string allMatch = verMatch + "(" + encMatch + ")?(" + staMatch + ")?";
135         }
136 }