2 // System.Xml.XmlDeclaration
5 // Duncan Mak (duncan@ximian.com)
6 // Atsushi Enomotot (atsushi@ximian.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Globalization;
38 public class XmlDeclaration : XmlLinkedNode
40 string encoding = "UTF-8"; // defaults to UTF-8
44 protected internal XmlDeclaration (string version, string encoding,
45 string standalone, XmlDocument doc)
51 if (standalone == null)
54 this.version = version;
55 this.encoding = encoding;
56 this.standalone = standalone;
59 public string Encoding {
60 get { return encoding; }
61 set { encoding = (value == null) ? String.Empty : value; }
64 public override string InnerText {
66 set { ParseInput (value); }
69 public override string LocalName {
73 public override string Name {
77 public override XmlNodeType NodeType {
78 get { return XmlNodeType.XmlDeclaration; }
81 public string Standalone {
82 get { return standalone; }
86 if (String.Compare (value, "YES", true, CultureInfo.InvariantCulture) == 0)
88 if (String.Compare (value, "NO", true, CultureInfo.InvariantCulture) == 0)
92 standalone = String.Empty;
96 public override string Value {
98 string formatEncoding = "";
99 string formatStandalone = "";
101 if (encoding != String.Empty)
102 formatEncoding = String.Format (" encoding=\"{0}\"", encoding);
104 if (standalone != String.Empty)
105 formatStandalone = String.Format (" standalone=\"{0}\"", standalone);
107 return String.Format ("version=\"{0}\"{1}{2}", Version, formatEncoding, formatStandalone);
109 set { ParseInput (value); }
112 public string Version {
113 get { return version; }
116 public override XmlNode CloneNode (bool deep)
118 return new XmlDeclaration (Version, Encoding, standalone, OwnerDocument);
121 public override void WriteContentTo (XmlWriter w) {}
123 public override void WriteTo (XmlWriter w)
125 // This doesn't seem to match up very well with w.WriteStartDocument()
126 // so writing out custom here.
127 w.WriteRaw (String.Format ("<?xml {0}?>", Value));
130 private int SkipWhitespace (string input, int index)
132 while (index < input.Length) {
133 if (XmlChar.IsWhitespace (input [index]))
141 void ParseInput (string input)
143 int index = SkipWhitespace (input, 0);
144 if (index + 7 > input.Length || input.IndexOf ("version", index, 7) != index)
145 throw new XmlException ("Missing 'version' specification.");
146 index = SkipWhitespace (input, index + 7);
148 char c = input [index];
150 throw new XmlException ("Invalid 'version' specification.");
152 index = SkipWhitespace (input, index);
154 if (c != '"' && c != '\'')
155 throw new XmlException ("Invalid 'version' specification.");
157 int end = input.IndexOf (c, index);
158 if (end < 0 || input.IndexOf ("1.0", index, 3) != index)
159 throw new XmlException ("Invalid 'version' specification.");
161 if (index == input.Length)
163 if (!XmlChar.IsWhitespace (input [index]))
164 throw new XmlException ("Invalid XML declaration.");
165 index = SkipWhitespace (input, index + 1);
166 if (index == input.Length)
169 if (input.Length > index + 8 && input.IndexOf ("encoding", index, 8) > 0) {
170 index = SkipWhitespace (input, index + 8);
173 throw new XmlException ("Invalid 'version' specification.");
175 index = SkipWhitespace (input, index);
177 if (c != '"' && c != '\'')
178 throw new XmlException ("Invalid 'encoding' specification.");
179 end = input.IndexOf (c, index + 1);
181 throw new XmlException ("Invalid 'encoding' specification.");
182 Encoding = input.Substring (index + 1, end - index - 1);
184 if (index == input.Length)
186 if (!XmlChar.IsWhitespace (input [index]))
187 throw new XmlException ("Invalid XML declaration.");
188 index = SkipWhitespace (input, index + 1);
191 if (input.Length > index + 10 && input.IndexOf ("standalone", index, 10) > 0) {
192 index = SkipWhitespace (input, index + 10);
195 throw new XmlException ("Invalid 'version' specification.");
197 index = SkipWhitespace (input, index);
199 if (c != '"' && c != '\'')
200 throw new XmlException ("Invalid 'standalone' specification.");
201 end = input.IndexOf (c, index + 1);
203 throw new XmlException ("Invalid 'standalone' specification.");
204 string tmp = input.Substring (index + 1, end - index - 1);
210 throw new XmlException ("Invalid standalone specification.");
214 index = SkipWhitespace (input, index);
216 if (index != input.Length)
217 throw new XmlException ("Invalid XML declaration.");