Welcome to mirror list, hosted at ThFree Co, Russian Federation.

XmlDeclaration.cs « System.Xml « System.XML « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dc74b4b899ebfc514ae19566169662911ffa8c44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//
// System.Xml.XmlDeclaration
//
// Author:
//	Duncan Mak  (duncan@ximian.com)
//	Atsushi Enomotot  (atsushi@ximian.com)
//
// (C) Ximian, Inc.

//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Globalization;
using System.Text;
using System.Xml;

namespace System.Xml
{
	public class XmlDeclaration : XmlLinkedNode
	{
		string encoding = "UTF-8"; // defaults to UTF-8
		string standalone;
		string version;

		protected internal XmlDeclaration (string version, string encoding,
						   string standalone, XmlDocument doc)
			: base (doc)
		{
			if (encoding == null)
				encoding = "";

			if (standalone == null)
				standalone = "";

			this.version = version;
			this.encoding = encoding;
			this.standalone = standalone;
		}

		public string Encoding  {
			get { return encoding; } 
			set { encoding = (value == null) ? String.Empty : value; }
		}

		public override string InnerText {
			get { return Value; }
			set { ParseInput (value); }
		}
		
		public override string LocalName {
			get { return "xml"; }
		}

		public override string Name {
			get { return "xml"; }
		}

		public override XmlNodeType NodeType {
			get { return XmlNodeType.XmlDeclaration; }
		}

		public string Standalone {
			get { return standalone; }
			set {
				if(value != null)
				{
					if (String.Compare (value, "YES", true, CultureInfo.InvariantCulture) == 0)
						standalone = "yes";
					if (String.Compare (value, "NO", true, CultureInfo.InvariantCulture) == 0)
						standalone = "no";
				}
				else
					standalone = String.Empty;
			}
		}

		public override string Value {
			get {
				string formatEncoding = "";
				string formatStandalone = "";

				if (encoding != String.Empty)
					formatEncoding = String.Format (" encoding=\"{0}\"", encoding);

				if (standalone != String.Empty)
					formatStandalone = String.Format (" standalone=\"{0}\"", standalone);

				return String.Format ("version=\"{0}\"{1}{2}", Version, formatEncoding, formatStandalone);
			}
			set { ParseInput (value); }
		}

		public string Version {
			get { return version; }
		}

		public override XmlNode CloneNode (bool deep)
		{
			return new XmlDeclaration (Version, Encoding, standalone, OwnerDocument);
		}

		public override void WriteContentTo (XmlWriter w) {}

		public override void WriteTo (XmlWriter w)
		{
			// This doesn't seem to match up very well with w.WriteStartDocument()
			// so writing out custom here.
			w.WriteRaw (String.Format ("<?xml {0}?>", Value));
		}

		private int SkipWhitespace (string input, int index)
		{
			while (index < input.Length) {
				if (XmlChar.IsWhitespace (input [index]))
					index++;
				else
					break;
			}
			return index;
		}

		void ParseInput (string input)
		{
			int index = SkipWhitespace (input, 0);
			if (index + 7 > input.Length || input.IndexOf ("version", index, 7) != index)
				throw new XmlException ("Missing 'version' specification.");
			index = SkipWhitespace (input, index + 7);

			char c = input [index];
			if (c != '=')
				throw new XmlException ("Invalid 'version' specification.");
			index++;
			index = SkipWhitespace (input, index);
			c = input [index];
			if (c != '"' && c != '\'')
				throw new XmlException ("Invalid 'version' specification.");
			index++;
			int end = input.IndexOf (c, index);
			if (end < 0 || input.IndexOf ("1.0", index, 3) != index)
				throw new XmlException ("Invalid 'version' specification.");
			index += 4;
			if (index == input.Length)
				return;
			if (!XmlChar.IsWhitespace (input [index]))
				throw new XmlException ("Invalid XML declaration.");
			index = SkipWhitespace (input, index + 1);
			if (index == input.Length)
				return;

			if (input.Length > index + 8 && input.IndexOf ("encoding", index, 8) > 0) {
				index = SkipWhitespace (input, index + 8);
				c = input [index];
				if (c != '=')
					throw new XmlException ("Invalid 'version' specification.");
				index++;
				index = SkipWhitespace (input, index);
				c = input [index];
				if (c != '"' && c != '\'')
					throw new XmlException ("Invalid 'encoding' specification.");
				end = input.IndexOf (c, index + 1);
				if (end < 0)
					throw new XmlException ("Invalid 'encoding' specification.");
				Encoding = input.Substring (index + 1, end - index - 1);
				index = end + 1;
				if (index == input.Length)
					return;
				if (!XmlChar.IsWhitespace (input [index]))
					throw new XmlException ("Invalid XML declaration.");
				index = SkipWhitespace (input, index + 1);
					return;
			}

			if (input.Length > index + 10 && input.IndexOf ("standalone", index, 10) > 0) {
				index = SkipWhitespace (input, index + 10);
				c = input [index];
				if (c != '=')
					throw new XmlException ("Invalid 'version' specification.");
				index++;
				index = SkipWhitespace (input, index);
				c = input [index];
				if (c != '"' && c != '\'')
					throw new XmlException ("Invalid 'standalone' specification.");
				end = input.IndexOf (c, index + 1);
				if (end < 0)
					throw new XmlException ("Invalid 'standalone' specification.");
				string tmp = input.Substring (index + 1, end - index - 1);
				switch (tmp) {
				case "yes":
				case "no":
					break;
				default:
					throw new XmlException ("Invalid standalone specification.");
				}
				Standalone = tmp;
				index = end + 1;
				index = SkipWhitespace (input, index);
			}
			if (index != input.Length)
				throw new XmlException ("Invalid XML declaration.");
		}
	}
}