File: XmlBinaryDictionaryWriter.cs

package info (click to toggle)
mono-reference-assemblies 3.12.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 604,240 kB
  • ctags: 625,505
  • sloc: cs: 3,967,741; xml: 2,793,081; ansic: 418,042; java: 60,435; sh: 14,833; makefile: 11,576; sql: 7,956; perl: 1,467; cpp: 1,446; yacc: 1,203; python: 598; asm: 422; sed: 16; php: 1
file content (1102 lines) | stat: -rw-r--r-- 28,441 bytes parent folder | download | duplicates (3)
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
//
// XmlBinaryDictionaryWriter.cs
//
// Author:
//	Atsushi Enomoto  <atsushi@ximian.com>
//
// Copyright (C) 2005, 2007 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.
//
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;

using BF = System.Xml.XmlBinaryFormat;

namespace System.Xml
{
	internal partial class XmlBinaryDictionaryWriter : XmlDictionaryWriter
	{
		class MyBinaryWriter : BinaryWriter
		{
			public MyBinaryWriter (Stream s)
				: base (s)
			{
			}

			public void WriteFlexibleInt (int value)
			{
				Write7BitEncodedInt (value);
			}
		}

		#region Fields
		MyBinaryWriter original, writer, buffer_writer;
		IXmlDictionary dict_ext;
		XmlDictionary dict_int = new XmlDictionary ();
		XmlBinaryWriterSession session;
		bool owns_stream;
		Encoding utf8Enc = new UTF8Encoding ();
		MemoryStream buffer = new MemoryStream ();

		const string XmlNamespace = "http://www.w3.org/XML/1998/namespace";
		const string XmlnsNamespace = "http://www.w3.org/2000/xmlns/";

		WriteState state = WriteState.Start;
		bool open_start_element = false;
		// transient current node info
		List<KeyValuePair<string,object>> namespaces = new List<KeyValuePair<string,object>> ();
		string xml_lang = null;
		XmlSpace xml_space = XmlSpace.None;
		int ns_index = 0;
		// stacked info
		Stack<int> ns_index_stack = new Stack<int> ();
		Stack<string> xml_lang_stack = new Stack<string> ();
		Stack<XmlSpace> xml_space_stack = new Stack<XmlSpace> ();
		Stack<string> element_ns_stack = new Stack<string> ();
		string element_ns = String.Empty;
		int element_count;
		string element_prefix; // only meaningful at Element state
		// current attribute info
		string attr_value;
		string current_attr_prefix;
		object current_attr_name, current_attr_ns;
		bool attr_typed_value;
		SaveTarget save_target;

		enum SaveTarget {
			None,
			Namespaces,
			XmlLang,
			XmlSpace
		}

		// XmlWriterSettings support

		#endregion

		#region Constructors

		public XmlBinaryDictionaryWriter (Stream stream,
			IXmlDictionary dictionary,
			XmlBinaryWriterSession session, bool ownsStream)
		{
			if (dictionary == null)
				dictionary = new XmlDictionary ();
			if (session == null)
				session = new XmlBinaryWriterSession ();

			original = new MyBinaryWriter (stream);
			this.writer = original;
			buffer_writer = new MyBinaryWriter (buffer);
			this.dict_ext = dictionary;
			this.session = session;
			owns_stream = ownsStream;

			AddNamespace ("xml", "http://www.w3.org/XML/1998/namespace");
			AddNamespace ("xml", "http://www.w3.org/2000/xmlns/");
			ns_index = 2;
		}

		#endregion

		#region Properties

		public override WriteState WriteState {
			get { return state; }
		}
		
		public override string XmlLang {
			get { return xml_lang; }
		}

		public override XmlSpace XmlSpace {
			get { return xml_space; }
		}

		#endregion

		#region Methods

		private void AddMissingElementXmlns ()
		{
			// push new namespaces to manager.
			for (int i = ns_index; i < namespaces.Count; i++) {
				var ent = namespaces [i];
				string prefix = (string) ent.Key;
				string ns = ent.Value as string;
				XmlDictionaryString dns = ent.Value as XmlDictionaryString;
				if (ns != null) {
					if (prefix.Length > 0) {
						writer.Write (BF.PrefixNSString);
						writer.Write (prefix);
					}
					else
						writer.Write (BF.DefaultNSString);
					writer.Write (ns);
				} else {
					if (prefix.Length > 0) {
						writer.Write (BF.PrefixNSIndex);
						writer.Write (prefix);
					}
					else
						writer.Write (BF.DefaultNSIndex);
					WriteDictionaryIndex (dns);
				}
			}
			ns_index = namespaces.Count;
		}

		private void CheckState ()
		{
			if (state == WriteState.Closed) {
				throw new InvalidOperationException ("The Writer is closed.");
			}
		}

		void ProcessStateForContent ()
		{
			CheckState ();

			if (state == WriteState.Element)
				CloseStartElement ();

			ProcessPendingBuffer (false, false);
			if (state != WriteState.Attribute)
				writer = buffer_writer;
		}

		void ProcessTypedValue ()
		{
			ProcessStateForContent ();
			if (state == WriteState.Attribute) {
				if (attr_typed_value)
					throw new InvalidOperationException (String.Format ("A typed value for the attribute '{0}' in namespace '{1}' was already written", current_attr_name, current_attr_ns));
				attr_typed_value = true;
			}
		}

		void ProcessPendingBuffer (bool last, bool endElement)
		{
			if (buffer.Position > 0) {
				byte [] arr = buffer.GetBuffer ();
				if (endElement)
					arr [0]++;
				original.Write (arr, 0, (int) buffer.Position);
				buffer.SetLength (0);
			}
			if (last)
				writer = original;
		}

		public override void Close ()
		{
			CloseOpenAttributeAndElements ();

			if (owns_stream)
				writer.Close ();
			else if (state != WriteState.Closed)
				writer.Flush ();
			state = WriteState.Closed;
		}

		private void CloseOpenAttributeAndElements ()
		{
			CloseStartElement ();

			 while (element_count > 0)
				WriteEndElement ();
		}

		private void CloseStartElement ()
		{
			if (!open_start_element)
				return;

			if (state == WriteState.Attribute)
				WriteEndAttribute ();

			AddMissingElementXmlns ();

			state = WriteState.Content;
			open_start_element = false;
		}

		public override void Flush ()
		{
			writer.Flush ();
		}

		public override string LookupPrefix (string ns)
		{
			if (ns == null || ns == String.Empty)
				throw new ArgumentException ("The Namespace cannot be empty.");

			var de = namespaces.LastOrDefault (i => i.Value.ToString () == ns);
			return de.Key; // de is KeyValuePair and its default key is null.
		}

		public override void WriteBase64 (byte[] buffer, int index, int count)
		{
			if (count < 0)
				throw new IndexOutOfRangeException ("Negative count");
			ProcessStateForContent ();

			if (count < 0x100) {
				writer.Write (BF.Bytes8);
				writer.Write ((byte) count);
				writer.Write (buffer, index, count);
			} else if (count < 0x10000) {
				writer.Write (BF.Bytes8);
				writer.Write ((ushort) count);
				writer.Write (buffer, index, count);
			} else {
				writer.Write (BF.Bytes32);
				writer.Write (count);
				writer.Write (buffer, index, count);
			}
		}

		public override void WriteCData (string text)
		{
			if (text.IndexOf ("]]>") >= 0)
				throw new ArgumentException ("CDATA section cannot contain text \"]]>\".");

			ProcessStateForContent ();

			WriteTextBinary (text);
		}

		public override void WriteCharEntity (char ch)
		{
			WriteChars (new char [] {ch}, 0, 1);
		}

		public override void WriteChars (char[] buffer, int index, int count)
		{
			ProcessStateForContent ();

			int blen = Encoding.UTF8.GetByteCount (buffer, index, count);

			if (blen == 0)
				writer.Write (BF.EmptyText);
			else if (count == 1 && buffer [0] == '0')
				writer.Write (BF.Zero);
			else if (count == 1 && buffer [0] == '1')
				writer.Write (BF.One);
			else if (blen < 0x100) {
				writer.Write (BF.Chars8);
				writer.Write ((byte) blen);
				writer.Write (buffer, index, count);
			} else if (blen < 0x10000) {
				writer.Write (BF.Chars16);
				writer.Write ((ushort) blen);
				writer.Write (buffer, index, count);
			} else {
				writer.Write (BF.Chars32);
				writer.Write (blen);
				writer.Write (buffer, index, count);
			}
		}

		public override void WriteComment (string text)
		{
			if (text.EndsWith("-"))
				throw new ArgumentException ("An XML comment cannot contain \"--\" inside.");
			else if (text.IndexOf("--") > 0)
				throw new ArgumentException ("An XML comment cannot end with \"-\".");

			ProcessStateForContent ();

			if (state == WriteState.Attribute)
				throw new InvalidOperationException ("Comment node is not allowed inside an attribute");

			writer.Write (BF.Comment);
			writer.Write (text);
		}

		public override void WriteDocType (string name, string pubid, string sysid, string subset)
		{
			throw new NotSupportedException ("This XmlWriter implementation does not support document type.");
		}

		public override void WriteEndAttribute ()
		{
			if (state != WriteState.Attribute)
				throw new InvalidOperationException("Token EndAttribute in state Start would result in an invalid XML document.");

			CheckState ();

			if (attr_value == null)
				attr_value = String.Empty;

			switch (save_target) {
			case SaveTarget.XmlLang:
				xml_lang = attr_value;
				goto default;
			case SaveTarget.XmlSpace:
				switch (attr_value) {
				case "preserve":
					xml_space = XmlSpace.Preserve;
					break;
				case "default":
					xml_space = XmlSpace.Default;
					break;
				default:
					throw new ArgumentException (String.Format ("Invalid xml:space value: '{0}'", attr_value));
				}
				goto default;
			case SaveTarget.Namespaces:
				if (current_attr_name.ToString ().Length > 0 && attr_value.Length == 0)
					throw new ArgumentException ("Cannot use prefix with an empty namespace.");

				AddNamespaceChecked (current_attr_name.ToString (), attr_value);
				break;
			default:
				if (!attr_typed_value)
					WriteTextBinary (attr_value);
				break;
			}

			if (current_attr_prefix.Length > 0 && save_target != SaveTarget.Namespaces)
				AddNamespaceChecked (current_attr_prefix, current_attr_ns);

			state = WriteState.Element;
			current_attr_prefix = null;
			current_attr_name = null;
			current_attr_ns = null;
			attr_value = null;
			attr_typed_value = false;
		}

		public override void WriteEndDocument ()
		{
			CloseOpenAttributeAndElements ();

			switch (state) {
			case WriteState.Start:
				throw new InvalidOperationException ("Document has not started.");
			case WriteState.Prolog:
				throw new ArgumentException ("This document does not have a root element.");
			}

			state = WriteState.Start;
		}

		bool SupportsCombinedEndElementSupport (byte operation)
		{
			switch (operation) {
			case BF.Comment:
				return false;
			}
			return true;
		}

		public override void WriteEndElement ()
		{
			if (element_count-- == 0)
				throw new InvalidOperationException("There was no XML start tag open.");

			if (state == WriteState.Attribute)
				WriteEndAttribute ();

			// Comment+EndElement does not exist
			bool needExplicitEndElement = buffer.Position == 0 || !SupportsCombinedEndElementSupport (buffer.GetBuffer () [0]);
			ProcessPendingBuffer (true, !needExplicitEndElement);
			CheckState ();
			AddMissingElementXmlns ();

			if (needExplicitEndElement)
				writer.Write (BF.EndElement);

			element_ns = element_ns_stack.Pop ();
			xml_lang = xml_lang_stack.Pop ();
			xml_space = xml_space_stack.Pop ();
			int cur = namespaces.Count;
			ns_index = ns_index_stack.Pop ();
			namespaces.RemoveRange (ns_index, cur - ns_index);
			open_start_element = false;

			Depth--;
		}

		public override void WriteEntityRef (string name)
		{
			throw new NotSupportedException ("This XmlWriter implementation does not support entity references.");
		}

		public override void WriteFullEndElement ()
		{
			WriteEndElement ();
		}

		public override void WriteProcessingInstruction (string name, string text)
		{
			if (name != "xml")
				throw new ArgumentException ("Processing instructions are not supported. ('xml' is allowed for XmlDeclaration; this is because of design problem of ECMA XmlWriter)");
			// Otherwise, silently ignored. WriteStartDocument()
			// is still callable after this method(!)
		}

		public override void WriteQualifiedName (XmlDictionaryString local, XmlDictionaryString ns)
		{
			string prefix = namespaces.LastOrDefault (i => i.Value.ToString () == ns.ToString ()).Key;
			bool use_dic = prefix != null;
			if (prefix == null)
				prefix = LookupPrefix (ns.Value);
			if (prefix == null)
				throw new ArgumentException (String.Format ("Namespace URI '{0}' is not bound to any of the prefixes", ns));

			ProcessTypedValue ();

			if (use_dic && prefix.Length == 1) {
				writer.Write (BF.QNameIndex);
				writer.Write ((byte) (prefix [0] - 'a'));
				WriteDictionaryIndex (local);
			} else {
				// QNameIndex is not applicable.
				WriteString (prefix);
				WriteString (":");
				WriteString (local);
			}
		}

		public override void WriteRaw (string data)
		{
			WriteString (data);
		}

		public override void WriteRaw (char[] buffer, int index, int count)
		{
			WriteChars (buffer, index, count);
		}

		void CheckStateForAttribute ()
		{
			CheckState ();

			if (state != WriteState.Element)
				throw new InvalidOperationException ("Token StartAttribute in state " + WriteState + " would result in an invalid XML document.");
		}

		string CreateNewPrefix ()
		{
			return CreateNewPrefix (String.Empty);
		}
		
		string CreateNewPrefix (string p)
		{
			for (char c = 'a'; c <= 'z'; c++)
				if (!namespaces.Any (iter => iter.Key == p + c))
					return p + c;
			for (char c = 'a'; c <= 'z'; c++) {
				var s = CreateNewPrefix (c.ToString ());
				if (s != null)
					return s;
			}
			throw new InvalidOperationException ("too many prefix population");
		}

		bool CollectionContains (ICollection col, string value)
		{
			foreach (string v in col)
				if (v == value)
					return true;
			return false;
		}

		void ProcessStartAttributeCommon (ref string prefix, string localName, string ns, object nameObj, object nsObj)
		{
			// dummy prefix is created here, while the caller
			// still uses empty string as the prefix there.
			if (prefix.Length == 0 && ns.Length > 0) {
				prefix = LookupPrefix (ns);
				// Not only null but also ""; when the returned
				// string is empty, then it still needs a dummy
				// prefix, since default namespace does not
				// apply to attribute
				if (String.IsNullOrEmpty (prefix))
					prefix = CreateNewPrefix ();
			}
			else if (prefix.Length > 0 && ns.Length == 0) {
				switch (prefix) {
				case "xml":
					nsObj = ns = XmlNamespace;
					break;
				case "xmlns":
					nsObj = ns = XmlnsNamespace;
					break;
				default:
					throw new ArgumentException ("Cannot use prefix with an empty namespace.");
				}
			}
			// here we omit such cases that it is used for writing
			// namespace-less xml, unlike XmlTextWriter.
			if (prefix == "xmlns" && ns != XmlnsNamespace)
				throw new ArgumentException (String.Format ("The 'xmlns' attribute is bound to the reserved namespace '{0}'", XmlnsNamespace));

			CheckStateForAttribute ();

			state = WriteState.Attribute;

			save_target = SaveTarget.None;
			switch (prefix) {
			case "xml":
				// MS.NET looks to allow other names than 
				// lang and space (e.g. xml:link, xml:hack).
				ns = XmlNamespace;
				switch (localName) {
				case "lang":
					save_target = SaveTarget.XmlLang;
					break;
				case "space":
					save_target = SaveTarget.XmlSpace;
					break;
				}
				break;
			case "xmlns":
				save_target = SaveTarget.Namespaces;
				break;
			}

			current_attr_prefix = prefix;
			current_attr_name = nameObj;
			current_attr_ns = nsObj;
		}

		public override void WriteStartAttribute (string prefix, string localName, string ns)
		{
			if (prefix == null)
				prefix = String.Empty;
			if (ns == null)
				ns = String.Empty;
			if (localName == "xmlns" && prefix.Length == 0) {
				prefix = "xmlns";
				localName = String.Empty;
			}

			ProcessStartAttributeCommon (ref prefix, localName, ns, localName, ns);

			// for namespace nodes we don't write attribute node here.
			if (save_target == SaveTarget.Namespaces)
				return;

			byte op = prefix.Length == 1 && 'a' <= prefix [0] && prefix [0] <= 'z' ?
				  (byte) (prefix [0] - 'a' + BF.PrefixNAttrStringStart) :
				  prefix.Length == 0 ? BF.AttrString :
				  BF.AttrStringPrefix;

			if (BF.PrefixNAttrStringStart <= op && op <= BF.PrefixNAttrStringEnd) {
				writer.Write (op);
				writer.Write (localName);
			} else {
				writer.Write (op);
				if (prefix.Length > 0)
					writer.Write (prefix);
				writer.Write (localName);
			}
		}

		public override void WriteStartDocument ()
		{
			WriteStartDocument (false);
		}

		public override void WriteStartDocument (bool standalone)
		{
			if (state != WriteState.Start)
				throw new InvalidOperationException("WriteStartDocument should be the first call.");

			CheckState ();

			// write nothing to stream.

			state = WriteState.Prolog;
		}

		void PrepareStartElement ()
		{
			ProcessPendingBuffer (true, false);
			CheckState ();
			CloseStartElement ();

			Depth++;

			element_ns_stack.Push (element_ns);
			xml_lang_stack.Push (xml_lang);
			xml_space_stack.Push (xml_space);
			ns_index_stack.Push (ns_index);
		}

		public override void WriteStartElement (string prefix, string localName, string ns)
		{
			PrepareStartElement ();

			if ((prefix != null && prefix != String.Empty) && ((ns == null) || (ns == String.Empty)))
				throw new ArgumentException ("Cannot use a prefix with an empty namespace.");

			if (ns == null)
				ns = String.Empty;
			if (ns == String.Empty)
				prefix = String.Empty;
			if (prefix == null)
				prefix = String.Empty;

			byte op = prefix.Length == 1 && 'a' <= prefix [0] && prefix [0] <= 'z' ?
				  (byte) (prefix [0] - 'a' + BF.PrefixNElemStringStart) :
				  prefix.Length == 0 ? BF.ElemString :
				  BF.ElemStringPrefix;

			if (BF.PrefixNElemStringStart <= op && op <= BF.PrefixNElemStringEnd) {
				writer.Write (op);
				writer.Write (localName);
			} else {
				writer.Write (op);
				if (prefix.Length > 0)
					writer.Write (prefix);
				writer.Write (localName);
			}

			OpenElement (prefix, ns);
		}

		void OpenElement (string prefix, object nsobj)
		{
			string ns = nsobj.ToString ();

			state = WriteState.Element;
			open_start_element = true;
			element_prefix = prefix;
			element_count++;
			element_ns = nsobj.ToString ();

			if (element_ns != String.Empty && LookupPrefix (element_ns) != prefix)
				AddNamespace (prefix, nsobj);
		}

		void AddNamespace (string prefix, object nsobj)
		{
			namespaces.Add (new KeyValuePair<string,object> (prefix, nsobj));
		}

		void CheckIfTextAllowed ()
		{
			switch (state) {
			case WriteState.Start:
			case WriteState.Prolog:
				throw new InvalidOperationException ("Token content in state Prolog would result in an invalid XML document.");
			}
		}

		public override void WriteString (string text)
		{
			if (text == null)
				throw new ArgumentNullException ("text");
			CheckIfTextAllowed ();

			if (text == null)
				text = String.Empty;

			ProcessStateForContent ();

			if (state == WriteState.Attribute)
				attr_value += text;
			else
				WriteTextBinary (text);
		}

		public override void WriteString (XmlDictionaryString text)
		{
			if (text == null)
				throw new ArgumentNullException ("text");
			CheckIfTextAllowed ();

			if (text == null)
				text = XmlDictionaryString.Empty;

			ProcessStateForContent ();

			if (state == WriteState.Attribute)
				attr_value += text.Value;
			else if (text.Equals (XmlDictionary.Empty))
				writer.Write (BF.EmptyText);
			else {
				writer.Write (BF.TextIndex);
				WriteDictionaryIndex (text);
			}
		}

		public override void WriteSurrogateCharEntity (char lowChar, char highChar)
		{
			WriteChars (new char [] {highChar, lowChar}, 0, 2);
		}

		public override void WriteWhitespace (string ws)
		{
			for (int i = 0; i < ws.Length; i++) {
				switch (ws [i]) {
				case ' ': case '\t': case '\r': case '\n':
					continue;
				default:
					throw new ArgumentException ("Invalid Whitespace");
				}
			}

			ProcessStateForContent ();

			WriteTextBinary (ws);
		}

		public override void WriteXmlnsAttribute (string prefix, string namespaceUri)
		{
			if (namespaceUri == null)
				throw new ArgumentNullException ("namespaceUri");

			if (String.IsNullOrEmpty (prefix))
				prefix = CreateNewPrefix ();

			CheckStateForAttribute ();

			AddNamespaceChecked (prefix, namespaceUri);

			state = WriteState.Element;
		}

		void AddNamespaceChecked (string prefix, object ns)
		{
			switch (ns.ToString ()) {
			case XmlnsNamespace:
			case XmlNamespace:
				return;
			}

			if (prefix == null)
				throw new InvalidOperationException ();
			var o = namespaces.LastOrDefault (i => i.Key == prefix);
			if (o.Key != null) { // i.e. exists
				if (o.Value.ToString () != ns.ToString ()) {
					if (namespaces.LastIndexOf (o) >= ns_index)
						throw new ArgumentException (String.Format ("The prefix '{0}' is already mapped to another namespace URI '{1}' in this element scope and cannot be mapped to '{2}'", prefix ?? "(null)", o.Value ?? "(null)", ns.ToString ()));
					else
						AddNamespace  (prefix, ns);
				}
			}
			else
				AddNamespace  (prefix, ns);
		}

		#region DictionaryString

		void WriteDictionaryIndex (XmlDictionaryString ds)
		{
			XmlDictionaryString ds2;
			bool isSession = false;
			int idx = ds.Key;
			if (ds.Dictionary != dict_ext) {
				isSession = true;
				if (dict_int.TryLookup (ds.Value, out ds2))
					ds = ds2;
				if (!session.TryLookup (ds, out idx))
					session.TryAdd (dict_int.Add (ds.Value), out idx);
			}
			if (idx >= 0x80) {
				writer.Write ((byte) (0x80 + ((idx % 0x80) << 1) + (isSession ? 1 : 0)));
				writer.Write ((byte) ((byte) (idx / 0x80) << 1));
			}
			else
				writer.Write ((byte) (((idx % 0x80) << 1) + (isSession ? 1 : 0)));
		}

		public override void WriteStartElement (string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri)
		{
			PrepareStartElement ();

			if (prefix == null)
				prefix = String.Empty;

			byte op = prefix.Length == 1 && 'a' <= prefix [0] && prefix [0] <= 'z' ?
				  (byte) (prefix [0] - 'a' + BF.PrefixNElemIndexStart) :
				  prefix.Length == 0 ? BF.ElemIndex :
				  BF.ElemIndexPrefix;

			if (BF.PrefixNElemIndexStart <= op && op <= BF.PrefixNElemIndexEnd) {
				writer.Write (op);
				WriteDictionaryIndex (localName);
			} else {
				writer.Write (op);
				if (prefix.Length > 0)
					writer.Write (prefix);
				WriteDictionaryIndex (localName);
			}

			OpenElement (prefix, namespaceUri);
		}

		public override void WriteStartAttribute (string prefix, XmlDictionaryString localName, XmlDictionaryString ns)
		{
			if (localName == null)
				throw new ArgumentNullException ("localName");
			if (prefix == null)
				prefix = String.Empty;
			if (ns == null)
				ns = XmlDictionaryString.Empty;
			if (localName.Value == "xmlns" && prefix.Length == 0) {
				prefix = "xmlns";
				localName = XmlDictionaryString.Empty;
			}

			ProcessStartAttributeCommon (ref prefix, localName.Value, ns.Value, localName, ns);

			if (save_target == SaveTarget.Namespaces)
				return;

			if (prefix.Length == 1 && 'a' <= prefix [0] && prefix [0] <= 'z') {
				writer.Write ((byte) (prefix [0] - 'a' + BF.PrefixNAttrIndexStart));
				WriteDictionaryIndex (localName);
			} else {
				byte op = ns.Value.Length == 0 ? BF.AttrIndex :BF.AttrIndexPrefix;
				// Write to Stream
				writer.Write (op);
				if (prefix.Length > 0)
					writer.Write (prefix);
				WriteDictionaryIndex (localName);
			}
		}

		public override void WriteXmlnsAttribute (string prefix, XmlDictionaryString namespaceUri)
		{
			if (namespaceUri == null)
				throw new ArgumentNullException ("namespaceUri");

			if (String.IsNullOrEmpty (prefix))
				prefix = CreateNewPrefix ();

			CheckStateForAttribute ();

			AddNamespaceChecked (prefix, namespaceUri);

			state = WriteState.Element;
		}
		#endregion

		#region WriteValue
		public override void WriteValue (bool value)
		{
			ProcessTypedValue ();
			writer.Write ((byte) (value ? BF.BoolTrue : BF.BoolFalse));
		}

		public override void WriteValue (int value)
		{
			WriteValue ((long) value);
		}

		public override void WriteValue (long value)
		{
			ProcessTypedValue ();

			if (value == 0)
				writer.Write (BF.Zero);
			else if (value == 1)
				writer.Write (BF.One);
			else if (value < 0 || value > uint.MaxValue) {
				writer.Write (BF.Int64);
				for (int i = 0; i < 8; i++) {
					writer.Write ((byte) (value & 0xFF));
					value >>= 8;
				}
			} else if (value <= byte.MaxValue) {
				writer.Write (BF.Int8);
				writer.Write ((byte) value);
			} else if (value <= short.MaxValue) {
				writer.Write (BF.Int16);
				writer.Write ((byte) (value & 0xFF));
				writer.Write ((byte) (value >> 8));
			} else if (value <= int.MaxValue) {
				writer.Write (BF.Int32);
				for (int i = 0; i < 4; i++) {
					writer.Write ((byte) (value & 0xFF));
					value >>= 8;
				}
			}
		}

		public override void WriteValue (float value)
		{
			ProcessTypedValue ();
			writer.Write (BF.Single);
			WriteValueContent (value);
		}

		void WriteValueContent (float value)
		{
			writer.Write (value);
		}

		public override void WriteValue (double value)
		{
			ProcessTypedValue ();
			writer.Write (BF.Double);
			WriteValueContent (value);
		}

		void WriteValueContent (double value)
		{
			writer.Write (value);
		}

		public override void WriteValue (decimal value)
		{
			ProcessTypedValue ();
			writer.Write (BF.Decimal);
			WriteValueContent (value);
		}

		void WriteValueContent (decimal value)
		{
			int [] bits = Decimal.GetBits (value);
			// so, looks like it is saved as its internal form,
			// not the returned order.
			// BinaryWriter.Write(Decimal) is useless here.
			writer.Write (bits [3]);
			writer.Write (bits [2]);
			writer.Write (bits [0]);
			writer.Write (bits [1]);
		}

		public override void WriteValue (DateTime value)
		{
			ProcessTypedValue ();
			writer.Write (BF.DateTime);
			WriteValueContent (value);
		}

		void WriteValueContent (DateTime value)
		{
			writer.Write (value.ToBinary ());
		}

		public override void WriteValue (Guid value)
		{
			ProcessTypedValue ();
			writer.Write (BF.Guid);
			WriteValueContent (value);
		}

		void WriteValueContent (Guid value)
		{
			byte [] bytes = value.ToByteArray ();
			writer.Write (bytes, 0, bytes.Length);
		}

		public override void WriteValue (UniqueId value)
		{
			if (value == null)
				throw new ArgumentNullException ("value");

			Guid guid;
			if (value.TryGetGuid (out guid)) {
				// this conditional branching is required for
				// attr_typed_value not being true.
				ProcessTypedValue ();

				writer.Write (BF.UniqueId);
				byte [] bytes = guid.ToByteArray ();
				writer.Write (bytes, 0, bytes.Length);
			} else {
				WriteValue (value.ToString ());
			}
		}

		public override void WriteValue (TimeSpan value)
		{
			ProcessTypedValue ();

			writer.Write (BF.TimeSpan);
			WriteValueContent (value);
		}

		void WriteValueContent (TimeSpan value)
		{
			WriteBigEndian (value.Ticks, 8);
		}
		#endregion

		private void WriteBigEndian (long value, int digits)
		{
			long v = 0;
			for (int i = 0; i < digits; i++) {
				v = (v << 8) + (value & 0xFF);
				value >>= 8;
			}
			for (int i = 0; i < digits; i++) {
				writer.Write ((byte) (v & 0xFF));
				v >>= 8;
			}
		}

		private void WriteTextBinary (string text)
		{
			if (text.Length == 0)
				writer.Write (BF.EmptyText);
			else {
				char [] arr = text.ToCharArray ();
				WriteChars (arr, 0, arr.Length);
			}
		}

		#endregion

		#region Write typed array content

		// they are packed in WriteValue(), so we cannot reuse
		// them for array content.

		void WriteValueContent (bool value)
		{
			writer.Write (value ? (byte) 1 : (byte) 0);
		}

		void WriteValueContent (short value)
		{
			writer.Write (value);
		}

		void WriteValueContent (int value)
		{
			writer.Write (value);
		}

		void WriteValueContent (long value)
		{
			writer.Write (value);
		}

		#endregion
	}
}