Initial commit
[mono.git] / mcs / class / referencesource / System.Xml / System / Xml / Dom / XmlDeclaration.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="XmlDeclaration.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 // <owner current="true" primary="true">[....]</owner>
6 //------------------------------------------------------------------------------
7
8 namespace System.Xml {
9     using System.Text;
10     using System.Diagnostics;
11
12     // Represents the xml declaration nodes: <?xml version='1.0' ...?>
13     public class XmlDeclaration : XmlLinkedNode {
14
15         const string YES = "yes";
16         const string NO = "no";
17
18         private string  version;
19         private string  encoding;
20         private string  standalone;
21
22         protected internal XmlDeclaration( string version, string encoding, string standalone, XmlDocument doc ) : base( doc ) {
23             if ( !IsValidXmlVersion( version ) )
24                 throw new ArgumentException( Res.GetString( Res.Xdom_Version ) );
25             if( ( standalone != null ) && ( standalone.Length > 0 )  )
26                 if ( ( standalone != YES ) && ( standalone != NO ) )
27                     throw new ArgumentException( Res.GetString(Res.Xdom_standalone, standalone) );
28             this.Encoding = encoding;
29             this.Standalone = standalone;
30             this.Version = version;
31         }
32
33
34         // The version attribute for <?xml version= '1.0' ... ?>
35         public string Version {
36             get { return this.version; }
37             internal set { this.version = value; }
38         }
39
40         // Specifies the value of the encoding attribute, as for
41         // <?xml version= '1.0' encoding= 'UTF-8' ?>
42         public string Encoding {
43             get { return this.encoding; }
44             set { this.encoding = ( (value == null) ? String.Empty : value ); }
45         }
46
47         // Specifies the value of the standalone attribute.
48         public string Standalone {
49             get { return this.standalone; }
50             set {
51                 if ( value == null )
52                     this.standalone = String.Empty;
53                 else if ( value.Length == 0 || value == YES || value == NO )
54                     this.standalone = value;
55                 else
56                     throw new ArgumentException( Res.GetString(Res.Xdom_standalone, value) );
57             }
58         }
59
60         public override String Value {
61             get { return InnerText; }
62             set { InnerText = value; }
63         }
64
65
66         // Gets or sets the concatenated values of the node and
67         // all its children.
68         public override string InnerText {
69             get {
70                 StringBuilder strb = new StringBuilder("version=\"" + Version + "\"");
71                 if ( Encoding.Length > 0 ) {
72                     strb.Append(" encoding=\"");
73                     strb.Append(Encoding);
74                     strb.Append("\"");
75                 }
76                 if ( Standalone.Length > 0 ) {
77                     strb.Append(" standalone=\"");
78                     strb.Append(Standalone);
79                     strb.Append("\"");
80                 }
81                 return strb.ToString();
82             }
83
84             set {
85                 string tempVersion = null;
86                 string tempEncoding = null;
87                 string tempStandalone = null;
88                 string orgEncoding   = this.Encoding;
89                 string orgStandalone = this.Standalone;
90                 string orgVersion = this.Version;
91
92                 XmlLoader.ParseXmlDeclarationValue( value, out tempVersion, out tempEncoding, out tempStandalone );
93
94                 try {
95                     if ( tempVersion != null && !IsValidXmlVersion(tempVersion) )
96                         throw new ArgumentException(Res.GetString(Res.Xdom_Version));
97                     Version = tempVersion;
98
99                     if ( tempEncoding != null )
100                         Encoding = tempEncoding;
101                     if ( tempStandalone != null )
102                         Standalone = tempStandalone;
103                 }
104                 catch {
105                     Encoding = orgEncoding;
106                     Standalone = orgStandalone;
107                     Version = orgVersion;
108                     throw;
109                 }
110             }
111         }
112
113         //override methods and properties from XmlNode
114
115         // Gets the name of the node.
116         public override String Name {
117             get {
118                 return "xml";
119             }
120         }
121
122         // Gets the name of the current node without the namespace prefix.
123         public override string LocalName {
124             get { return Name;}
125         }
126
127         // Gets the type of the current node.
128         public override XmlNodeType NodeType {
129             get { return XmlNodeType.XmlDeclaration;}
130         }
131
132         // Creates a duplicate of this node.
133         public override XmlNode CloneNode(bool deep) {
134             Debug.Assert( OwnerDocument != null );
135             return OwnerDocument.CreateXmlDeclaration( Version, Encoding, Standalone );
136         }
137
138         // Saves the node to the specified XmlWriter.
139         public override void WriteTo(XmlWriter w) {
140             w.WriteProcessingInstruction(Name, InnerText);
141         }
142
143
144         // Saves all the children of the node to the specified XmlWriter.
145         public override void WriteContentTo(XmlWriter w) {
146             // Intentionally do nothing since the node doesn't have children.
147         }
148
149         private bool IsValidXmlVersion(string ver) {
150             return ver.Length >= 3 && ver[0] == '1' && ver[1] == '.' && XmlCharType.IsOnlyDigits(ver, 2, ver.Length - 2);
151         }
152     }
153 }