File: XmlDictionaryWriter.cs

package info (click to toggle)
mono 2.6.7-5.1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 327,344 kB
  • ctags: 413,649
  • sloc: cs: 2,471,883; xml: 1,768,594; ansic: 350,665; sh: 13,644; makefile: 8,640; perl: 1,784; asm: 717; cpp: 209; python: 146; sql: 81; sed: 16
file content (407 lines) | stat: -rw-r--r-- 11,772 bytes parent folder | download | duplicates (2)
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//
// XmlDictionaryWriter.cs
//
// Author:
//	Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2005 Novell, Inc.  http://www.novell.com
//
// 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.
//
#if NET_2_0
using System;
using System.IO;
using System.Text;

namespace System.Xml
{
	public abstract partial class XmlDictionaryWriter : XmlWriter
	{
		static readonly Encoding utf8_unmarked = new UTF8Encoding (false);

		int depth;

		protected XmlDictionaryWriter ()
		{
		}

		internal int Depth {
			get { return depth; }
			set { depth = value; }
		}

		public virtual bool CanCanonicalize {
			get { return false; }
		}

		public static XmlDictionaryWriter CreateBinaryWriter (
			Stream stream)
		{
			return CreateBinaryWriter (stream, null, null, false);
		}

		public static XmlDictionaryWriter CreateBinaryWriter (
			Stream stream, IXmlDictionary dictionary)
		{
			return CreateBinaryWriter (stream, dictionary, null, false);
		}

		public static XmlDictionaryWriter CreateBinaryWriter (
			Stream stream, IXmlDictionary dictionary,
			XmlBinaryWriterSession session)
		{
			return CreateBinaryWriter (stream, dictionary, session, false);
		}

		public static XmlDictionaryWriter CreateBinaryWriter (
			Stream stream, IXmlDictionary dictionary,
			XmlBinaryWriterSession session, bool ownsStream)
		{
			return new XmlBinaryDictionaryWriter (stream,
				dictionary, session, ownsStream);
		}

		public static XmlDictionaryWriter CreateDictionaryWriter (XmlWriter writer)
		{
			return new XmlSimpleDictionaryWriter (writer);
		}
#if !NET_2_1
		public static XmlDictionaryWriter CreateMtomWriter (
			Stream stream, Encoding encoding, int maxSizeInBytes,
			string startInfo)
		{
			return CreateMtomWriter (stream, encoding,
				maxSizeInBytes, startInfo, Guid.NewGuid () + "id=1", "http://tempuri.org/0/" + DateTime.Now.Ticks, true, false);
		}

		public static XmlDictionaryWriter CreateMtomWriter (
			Stream stream, Encoding encoding, int maxSizeInBytes,
			string startInfo, string boundary, string startUri,
			bool writeMessageHeaders, bool ownsStream)
		{
			return new XmlMtomDictionaryWriter (stream, encoding, maxSizeInBytes, startInfo, boundary, startUri, writeMessageHeaders, ownsStream);
		}
#endif
		public static XmlDictionaryWriter CreateTextWriter (
			Stream stream)
		{
			return CreateTextWriter (stream, Encoding.UTF8);
		}

		public static XmlDictionaryWriter CreateTextWriter (
			Stream stream, Encoding encoding)
		{
			return CreateTextWriter (stream, encoding, false);
		}

		// BTW looks like it creates an instance of different
		// implementation than those from XmlWriter.Create().
		public static XmlDictionaryWriter CreateTextWriter (
			Stream stream, Encoding encoding, bool ownsStream)
		{
			if (stream == null)
				throw new ArgumentNullException ("stream");
			if (encoding == null)
				throw new ArgumentNullException ("encoding");

			switch (encoding.CodePage) {
			case 1200:
			case 1201: // utf-16
			case 65001: // utf-8
				encoding = utf8_unmarked;
				break;
			default:
				throw new XmlException (String.Format ("XML declaration is required for encoding code page {0} but this XmlWriter does not support XML declaration.", encoding.CodePage));
			}

			XmlWriterSettings s = new XmlWriterSettings ();
			s.Encoding = encoding;
			s.CloseOutput = ownsStream;
			s.OmitXmlDeclaration = true;
			return CreateDictionaryWriter (XmlWriter.Create (stream, s));
		}

		public virtual void EndCanonicalization ()
		{
			throw new NotSupportedException ();
		}

		public virtual void StartCanonicalization (
			Stream stream, bool includeComments,
			string [] inclusivePrefixes)
		{
			throw new NotSupportedException ();
		}

		public void WriteAttributeString (
			XmlDictionaryString localName,
			XmlDictionaryString namespaceUri,
			string value)
		{
			WriteAttributeString (null, localName, namespaceUri, value);
		}

		public void WriteAttributeString (string prefix,
			XmlDictionaryString localName,
			XmlDictionaryString namespaceUri,
			string value)
		{
			WriteStartAttribute (prefix, localName, namespaceUri);
			WriteString (value);
			WriteEndAttribute ();
		}

		public void WriteElementString (
			XmlDictionaryString localName,
			XmlDictionaryString namespaceUri,
			string value)
		{
			WriteElementString (null, localName, namespaceUri, value);
		}

		public void WriteElementString (string prefix,
			XmlDictionaryString localName,
			XmlDictionaryString namespaceUri,
			string value)
		{
			WriteStartElement (prefix, localName, namespaceUri);
			WriteString (value);
			WriteEndElement ();
		}

		public virtual void WriteNode (XmlDictionaryReader reader,
			bool defattr)
		{
			if (reader == null)
				throw new ArgumentNullException ("reader");

			switch (reader.NodeType) {
			case XmlNodeType.Element:
				// gratuitously copied from System.XML/System.Xml/XmlWriter.cs:WriteNode(XmlReader,bool)
				// as there doesn't seem to be a way to hook into attribute writing w/o handling Element.
				XmlDictionaryString ename, ens;
				if (reader.TryGetLocalNameAsDictionaryString (out ename) && reader.TryGetLocalNameAsDictionaryString (out ens))
					WriteStartElement (reader.Prefix, ename, ens);
				else
					WriteStartElement (reader.Prefix, reader.LocalName, reader.NamespaceURI);
				// Well, I found that MS.NET took this way, since
				// there was a error-prone SgmlReader that fails
				// MoveToNextAttribute().
				if (reader.HasAttributes) {
					for (int i = 0; i < reader.AttributeCount; i++) {
						reader.MoveToAttribute (i);
						WriteAttribute (reader, defattr);
					}
					reader.MoveToElement ();
				}
				if (reader.IsEmptyElement)
					WriteEndElement ();
				else {
					int depth = reader.Depth;
					reader.Read ();
					if (reader.NodeType != XmlNodeType.EndElement) {
						do {
							WriteNode (reader, defattr);
						} while (depth < reader.Depth);
					}
					WriteFullEndElement ();
				}
				reader.Read ();
				break;
			case XmlNodeType.Attribute:
			case XmlNodeType.Text:
				WriteTextNode (reader, defattr);
				break;
			default:
				base.WriteNode (reader, defattr);
				break;
			}
		}

		private void WriteAttribute (XmlDictionaryReader reader, bool defattr)
		{
			if (!defattr && reader.IsDefault)
				return;

			XmlDictionaryString name, ns;
			if (reader.TryGetLocalNameAsDictionaryString (out name) && reader.TryGetLocalNameAsDictionaryString (out ns))
				WriteStartAttribute (reader.Prefix, name, ns);
			else
				WriteStartAttribute (reader.Prefix, reader.LocalName, reader.NamespaceURI);
#if NET_2_1
			// no ReadAttributeValue() in 2.1 profile.
			WriteTextNode (reader, true);
#else
			while (reader.ReadAttributeValue ()) {
				switch (reader.NodeType) {
				case XmlNodeType.Text:
					WriteTextNode (reader, true);
					break;
				case XmlNodeType.EntityReference:
					WriteEntityRef (reader.Name);
					break;
				}
			}
#endif
			WriteEndAttribute ();
		}

		public override void WriteNode (XmlReader reader, bool defattr)
		{
			if (reader == null)
				throw new ArgumentNullException ("reader");

			XmlDictionaryReader dr = reader as XmlDictionaryReader;
			if (dr != null)
				WriteNode (dr, defattr);
			else
				base.WriteNode (reader, defattr);
		}

		public virtual void WriteQualifiedName (
			XmlDictionaryString localName,
			XmlDictionaryString namespaceUri)
		{
			WriteQualifiedName (localName.Value, namespaceUri.Value);
		}

		public void WriteStartAttribute (
			XmlDictionaryString localName,
			XmlDictionaryString namespaceUri)
		{
			WriteStartAttribute (localName.Value, namespaceUri.Value);
		}

		public virtual void WriteStartAttribute (string prefix,
			XmlDictionaryString localName,
			XmlDictionaryString namespaceUri)
		{
			WriteStartAttribute (prefix, localName.Value, namespaceUri.Value);
		}

		public void WriteStartElement (
			XmlDictionaryString localName,
			XmlDictionaryString namespaceUri)
		{
			WriteStartElement (null, localName, namespaceUri);
		}

		public virtual void WriteStartElement (string prefix,
			XmlDictionaryString localName,
			XmlDictionaryString namespaceUri)
		{
			if (localName == null)
				throw new ArgumentException ("localName must not be null.", "localName");
			WriteStartElement (prefix, localName.Value,
					namespaceUri != null ? namespaceUri.Value : null);
		}

		public virtual void WriteString (XmlDictionaryString value)
		{
			WriteString (value.Value);
		}

		protected virtual void WriteTextNode (XmlDictionaryReader reader, bool isAttribute)
		{
			WriteString (reader.Value);
			if (!isAttribute)
				reader.Read ();
		}

		public virtual void WriteValue (Guid guid)
		{
			WriteString (guid.ToString ());
		}

		public virtual void WriteValue (IStreamProvider value)
		{
			if (value == null)
				throw new ArgumentNullException ("value");

			Stream stream = value.GetStream ();
			byte[] buf = new byte [Math.Min (2048, stream.CanSeek ? stream.Length : 2048)];
			int read;
			while ((read = stream.Read (buf, 0, buf.Length)) > 0) {
				WriteBase64 (buf, 0, read);
			}
			value.ReleaseStream (stream);
		}

		public virtual void WriteValue (TimeSpan duration)
		{
			WriteString (XmlConvert.ToString (duration));
		}

		public virtual void WriteValue (UniqueId id)
		{
			if (id == null)
				throw new ArgumentNullException ("id");
			WriteString (id.ToString ());
		}

		public virtual void WriteValue (XmlDictionaryString value)
		{
			WriteValue (value.Value);
		}

		public virtual void WriteXmlAttribute (string localName, string value)
		{
			WriteAttributeString ("xml", localName, "http://www.w3.org/XML/1998/namespace", value);
		}

		public virtual void WriteXmlAttribute (XmlDictionaryString localName,
			XmlDictionaryString value)
		{
			WriteXmlAttribute (localName.Value, value.Value);
		}

		public virtual void WriteXmlnsAttribute (
			string prefix, string namespaceUri)
		{
			// BTW .NET 2.0 those XmlWriters from XmlWrite.Create()
			// rejects namespace overriding i.e.
			//
			//	xw.WriteStartElement ("foo", "urn:foo");
			//	xw.WriteXmlnsAttribute ("foo", "urn:bar");
			//
			// causes an XmlException. We need fix in sys.xml.dll

			// When the prefix is null, this writer must mock
			// a dummy namespace up. It is then up to the actual
			// writer how it is determined in the output. (When
			// there is a duplicate, then it will be further 
			// modified.)
			if (prefix == null)
				prefix = "d" + Depth + "p1";

			if (prefix == String.Empty)
				WriteAttributeString ("xmlns", namespaceUri);
			else
				WriteAttributeString ("xmlns", prefix, "http://www.w3.org/2000/xmlns/", namespaceUri);
		}

		public virtual void WriteXmlnsAttribute (string prefix,
			XmlDictionaryString namespaceUri)
		{
			WriteXmlnsAttribute (prefix, namespaceUri.Value);
		}
	}
}
#endif