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
|
//
// System.Xml.Serialization.XmlSerializationWriter.cs
//
// Author:
// Tim Coleman (tim@timcoleman.com)
// Lluis Sanchez Gual (lluis@ximian.com)
//
// Copyright (C) Tim Coleman, 2002
//
//
// 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.Generic;
using System.Globalization;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Runtime.Serialization;
using System.Reflection;
namespace System.Xml.Serialization
{
public abstract class XmlSerializationWriter
#if NET_2_0
: XmlSerializationGeneratedCode
#endif
{
#region Fields
ObjectIDGenerator idGenerator;
int qnameCount;
bool topLevelElement = false;
ArrayList namespaces;
XmlWriter writer;
Queue referencedElements;
Hashtable callbacks;
Hashtable serializedObjects;
const string xmlNamespace = "http://www.w3.org/2000/xmlns/";
const string unexpectedTypeError = "The type {0} was not expected. Use the" +
" XmlInclude or SoapInclude attribute to specify types that are not known statically.";
#endregion // Fields
#region Constructors
protected XmlSerializationWriter ()
{
qnameCount = 0;
serializedObjects = new Hashtable ();
}
internal void Initialize (XmlWriter writer, XmlSerializerNamespaces nss)
{
this.writer = writer;
if (nss != null)
{
namespaces = new ArrayList ();
foreach (XmlQualifiedName ns in nss.ToArray())
if (ns.Name != "" && ns.Namespace != "")
namespaces.Add (ns);
}
}
#endregion // Constructors
#region Properties
protected ArrayList Namespaces {
get { return namespaces; }
set { namespaces = value; }
}
protected XmlWriter Writer {
get { return writer; }
set { writer = value; }
}
#endregion // Properties
#region Methods
protected void AddWriteCallback (Type type, string typeName, string typeNs, XmlSerializationWriteCallback callback)
{
WriteCallbackInfo info = new WriteCallbackInfo ();
info.Type = type;
info.TypeName = typeName;
info.TypeNs = typeNs;
info.Callback = callback;
if (callbacks == null) callbacks = new Hashtable ();
callbacks.Add (type, info);
}
protected Exception CreateChoiceIdentifierValueException (string value, string identifier, string name, string ns)
{
string message = string.Format ("Value '{0}' of the choice"
+ " identifier '{1}' does not match element '{2}'"
+ " from namespace '{3}'.", value, identifier,
name, ns);
return new InvalidOperationException (message);
}
protected Exception CreateInvalidChoiceIdentifierValueException (string type, string identifier)
{
string message = string.Format ("Invalid or missing choice"
+ " identifier '{0}' of type '{1}'.", identifier,
type);
return new InvalidOperationException (message);
}
protected Exception CreateMismatchChoiceException (string value, string elementName, string enumValue)
{
string message = String.Format ("Value of {0} mismatches the type of {1}, you need to set it to {2}.", elementName, value, enumValue);
return new InvalidOperationException (message);
}
protected Exception CreateUnknownAnyElementException (string name, string ns)
{
string message = String.Format ("The XML element named '{0}' from namespace '{1}' was not expected. The XML element name and namespace must match those provided via XmlAnyElementAttribute(s).", name, ns);
return new InvalidOperationException (message);
}
protected Exception CreateUnknownTypeException (object o)
{
return CreateUnknownTypeException (o.GetType ());
}
protected Exception CreateUnknownTypeException (Type type)
{
string message = String.Format ("The type {0} may not be used in this context.", type);
return new InvalidOperationException (message);
}
protected static byte[] FromByteArrayBase64 (byte[] value)
{
return value;
}
protected static string FromByteArrayHex (byte[] value)
{
return XmlCustomFormatter.FromByteArrayHex (value);
}
protected static string FromChar (char value)
{
return XmlCustomFormatter.FromChar (value);
}
protected static string FromDate (DateTime value)
{
return XmlCustomFormatter.FromDate (value);
}
protected static string FromDateTime (DateTime value)
{
return XmlCustomFormatter.FromDateTime (value);
}
protected static string FromEnum (long value, string[] values, long[] ids)
{
return XmlCustomFormatter.FromEnum (value, values, ids);
}
protected static string FromTime (DateTime value)
{
return XmlCustomFormatter.FromTime (value);
}
protected static string FromXmlName (string name)
{
return XmlCustomFormatter.FromXmlName (name);
}
protected static string FromXmlNCName (string ncName)
{
return XmlCustomFormatter.FromXmlNCName (ncName);
}
protected static string FromXmlNmToken (string nmToken)
{
return XmlCustomFormatter.FromXmlNmToken (nmToken);
}
protected static string FromXmlNmTokens (string nmTokens)
{
return XmlCustomFormatter.FromXmlNmTokens (nmTokens);
}
protected string FromXmlQualifiedName (XmlQualifiedName xmlQualifiedName)
{
if (xmlQualifiedName == null || xmlQualifiedName == XmlQualifiedName.Empty)
return null;
return GetQualifiedName (xmlQualifiedName.Name, xmlQualifiedName.Namespace);
}
private string GetId (object o, bool addToReferencesList)
{
if (idGenerator == null) idGenerator = new ObjectIDGenerator ();
bool firstTime;
long lid = idGenerator.GetId (o, out firstTime);
return String.Format (CultureInfo.InvariantCulture, "id{0}", lid);
}
bool AlreadyQueued (object ob)
{
if (idGenerator == null) return false;
bool firstTime;
idGenerator.HasId (ob, out firstTime);
return !firstTime;
}
private string GetNamespacePrefix (string ns)
{
string prefix = Writer.LookupPrefix (ns);
if (prefix == null)
{
prefix = String.Format (CultureInfo.InvariantCulture, "q{0}", ++qnameCount);
WriteAttribute ("xmlns", prefix, null, ns);
}
return prefix;
}
private string GetQualifiedName (string name, string ns)
{
if (ns == String.Empty) return name;
string prefix = GetNamespacePrefix (ns);
if (prefix == String.Empty)
return name;
else
return String.Format ("{0}:{1}", prefix, name);
}
protected abstract void InitCallbacks ();
protected void TopLevelElement ()
{
topLevelElement = true;
}
protected void WriteAttribute (string localName, byte[] value)
{
WriteAttribute (localName, String.Empty, value);
}
protected void WriteAttribute (string localName, string value)
{
WriteAttribute (String.Empty, localName, String.Empty, value);
}
protected void WriteAttribute (string localName, string ns, byte[] value)
{
if (value == null)
return;
Writer.WriteStartAttribute (localName, ns);
WriteValue (value);
Writer.WriteEndAttribute ();
}
protected void WriteAttribute (string localName, string ns, string value)
{
WriteAttribute (null, localName, ns, value);
}
protected void WriteAttribute (string prefix, string localName, string ns, string value)
{
if (value == null)
return;
Writer.WriteAttributeString (prefix, localName, ns, value);
}
void WriteXmlNode (XmlNode node)
{
if (node is XmlDocument)
node = ((XmlDocument) node).DocumentElement;
node.WriteTo (Writer);
}
protected void WriteElementEncoded (XmlNode node, string name, string ns, bool isNullable, bool any)
{
if (name != string.Empty)
{
if (node == null)
{
if (isNullable)
WriteNullTagEncoded (name, ns);
}
else if (any)
WriteXmlNode (node);
else
{
Writer.WriteStartElement (name, ns);
WriteXmlNode (node);
Writer.WriteEndElement ();
}
}
else
WriteXmlNode(node);
}
protected void WriteElementLiteral (XmlNode node, string name, string ns, bool isNullable, bool any)
{
if (name != string.Empty)
{
if (node == null)
{
if (isNullable)
WriteNullTagLiteral (name, ns);
}
else if (any)
WriteXmlNode (node);
else
{
Writer.WriteStartElement (name, ns);
WriteXmlNode (node);
Writer.WriteEndElement ();
}
}
else
WriteXmlNode (node);
}
protected void WriteElementQualifiedName (string localName, XmlQualifiedName value)
{
WriteElementQualifiedName (localName, String.Empty, value, null);
}
protected void WriteElementQualifiedName (string localName, string ns, XmlQualifiedName value)
{
WriteElementQualifiedName (localName, ns, value, null);
}
protected void WriteElementQualifiedName (string localName, XmlQualifiedName value, XmlQualifiedName xsiType)
{
WriteElementQualifiedName (localName, String.Empty, value, xsiType);
}
protected void WriteElementQualifiedName (string localName, string ns, XmlQualifiedName value, XmlQualifiedName xsiType)
{
localName = XmlCustomFormatter.FromXmlNCName (localName);
WriteStartElement (localName, ns);
if (xsiType != null) WriteXsiType (xsiType.Name, xsiType.Namespace);
Writer.WriteString (FromXmlQualifiedName (value));
WriteEndElement ();
}
protected void WriteElementString (string localName, string value)
{
WriteElementString (localName, String.Empty, value, null);
}
protected void WriteElementString (string localName, string ns, string value)
{
WriteElementString (localName, ns, value, null);
}
protected void WriteElementString (string localName, string value, XmlQualifiedName xsiType)
{
WriteElementString (localName, String.Empty, value, xsiType);
}
protected void WriteElementString (string localName, string ns, string value, XmlQualifiedName xsiType)
{
if (value == null) return;
if (xsiType != null) {
localName = XmlCustomFormatter.FromXmlNCName (localName);
WriteStartElement (localName, ns);
WriteXsiType (xsiType.Name, xsiType.Namespace);
Writer.WriteString (value);
WriteEndElement ();
}
else
Writer.WriteElementString (localName, ns, value);
}
protected void WriteElementStringRaw (string localName, byte[] value)
{
WriteElementStringRaw (localName, String.Empty, value, null);
}
protected void WriteElementStringRaw (string localName, string value)
{
WriteElementStringRaw (localName, String.Empty, value, null);
}
protected void WriteElementStringRaw (string localName, byte[] value, XmlQualifiedName xsiType)
{
WriteElementStringRaw (localName, String.Empty, value, xsiType);
}
protected void WriteElementStringRaw (string localName, string ns, byte[] value)
{
WriteElementStringRaw (localName, ns, value, null);
}
protected void WriteElementStringRaw (string localName, string ns, string value)
{
WriteElementStringRaw (localName, ns, value, null);
}
protected void WriteElementStringRaw (string localName, string value, XmlQualifiedName xsiType)
{
WriteElementStringRaw (localName, String.Empty, value, null);
}
protected void WriteElementStringRaw (string localName, string ns, byte[] value, XmlQualifiedName xsiType)
{
if (value == null)
return;
WriteStartElement (localName, ns);
if (xsiType != null)
WriteXsiType (xsiType.Name, xsiType.Namespace);
if (value.Length > 0)
Writer.WriteBase64(value,0,value.Length);
WriteEndElement ();
}
protected void WriteElementStringRaw (string localName, string ns, string value, XmlQualifiedName xsiType)
{
localName = XmlCustomFormatter.FromXmlNCName (localName);
WriteStartElement (localName, ns);
if (xsiType != null)
WriteXsiType (xsiType.Name, xsiType.Namespace);
Writer.WriteRaw (value);
WriteEndElement ();
}
protected void WriteEmptyTag (string name)
{
WriteEmptyTag (name, String.Empty);
}
protected void WriteEmptyTag (string name, string ns)
{
name = XmlCustomFormatter.FromXmlName (name);
WriteStartElement (name, ns);
WriteEndElement ();
}
protected void WriteEndElement ()
{
WriteEndElement (null);
}
protected void WriteEndElement (object o)
{
if (o != null)
serializedObjects.Remove (o);
Writer.WriteEndElement ();
}
protected void WriteId (object o)
{
WriteAttribute ("id", GetId (o, true));
}
protected void WriteNamespaceDeclarations (XmlSerializerNamespaces xmlns)
{
if (xmlns == null)
return;
ICollection namespaces = xmlns.Namespaces.Values;
foreach (XmlQualifiedName qn in namespaces) {
if (qn.Namespace != String.Empty && Writer.LookupPrefix (qn.Namespace) != qn.Name)
WriteAttribute ("xmlns", qn.Name, xmlNamespace, qn.Namespace);
}
}
protected void WriteNullableQualifiedNameEncoded (string name, string ns, XmlQualifiedName value, XmlQualifiedName xsiType)
{
if (value != null)
WriteElementQualifiedName (name, ns, value, xsiType);
else
WriteNullTagEncoded (name, ns);
}
protected void WriteNullableQualifiedNameLiteral (string name, string ns, XmlQualifiedName value)
{
if (value != null)
WriteElementQualifiedName (name, ns, value);
else
WriteNullTagLiteral (name, ns);
}
protected void WriteNullableStringEncoded (string name, string ns, string value, XmlQualifiedName xsiType)
{
if (value != null)
WriteElementString (name, ns, value, xsiType);
else
WriteNullTagEncoded (name, ns);
}
protected void WriteNullableStringEncodedRaw (string name, string ns, byte[] value, XmlQualifiedName xsiType)
{
if (value == null)
WriteNullTagEncoded (name, ns);
else
WriteElementStringRaw (name, ns, value, xsiType);
}
protected void WriteNullableStringEncodedRaw (string name, string ns, string value, XmlQualifiedName xsiType)
{
if (value == null)
WriteNullTagEncoded (name, ns);
else
WriteElementStringRaw (name, ns, value, xsiType);
}
protected void WriteNullableStringLiteral (string name, string ns, string value)
{
if (value != null)
WriteElementString (name, ns, value, null);
else
WriteNullTagLiteral (name, ns);
}
protected void WriteNullableStringLiteralRaw (string name, string ns, byte[] value)
{
if (value == null)
WriteNullTagLiteral (name, ns);
else
WriteElementStringRaw (name, ns, value);
}
protected void WriteNullableStringLiteralRaw (string name, string ns, string value)
{
if (value == null)
WriteNullTagLiteral (name, ns);
else
WriteElementStringRaw (name, ns, value);
}
protected void WriteNullTagEncoded (string name)
{
WriteNullTagEncoded (name, String.Empty);
}
protected void WriteNullTagEncoded (string name, string ns)
{
Writer.WriteStartElement (name, ns);
Writer.WriteAttributeString ("nil", XmlSchema.InstanceNamespace, "true");
Writer.WriteEndElement ();
}
protected void WriteNullTagLiteral (string name)
{
WriteNullTagLiteral (name, String.Empty);
}
protected void WriteNullTagLiteral (string name, string ns)
{
WriteStartElement (name, ns);
Writer.WriteAttributeString ("nil", XmlSchema.InstanceNamespace, "true");
WriteEndElement ();
}
protected void WritePotentiallyReferencingElement (string n, string ns, object o)
{
WritePotentiallyReferencingElement (n, ns, o, null, false, false);
}
protected void WritePotentiallyReferencingElement (string n, string ns, object o, Type ambientType)
{
WritePotentiallyReferencingElement (n, ns, o, ambientType, false, false);
}
protected void WritePotentiallyReferencingElement (string n, string ns, object o, Type ambientType, bool suppressReference)
{
WritePotentiallyReferencingElement (n, ns, o, ambientType, suppressReference, false);
}
protected void WritePotentiallyReferencingElement (string n, string ns, object o, Type ambientType, bool suppressReference, bool isNullable)
{
if (o == null)
{
if (isNullable) WriteNullTagEncoded (n, ns);
return;
}
var t = o.GetType ();
WriteStartElement (n, ns, true);
CheckReferenceQueue ();
if (callbacks != null && callbacks.ContainsKey (o.GetType ()))
{
WriteCallbackInfo info = (WriteCallbackInfo) callbacks[t];
if (t.IsEnum) {
info.Callback (o);
}
else if (suppressReference) {
Writer.WriteAttributeString ("id", GetId (o, false));
if (ambientType != t) WriteXsiType(info.TypeName, info.TypeNs);
info.Callback (o);
}
else {
if (!AlreadyQueued (o)) referencedElements.Enqueue (o);
Writer.WriteAttributeString ("href", "#" + GetId (o, true));
}
}
else
{
// Must be a primitive type or array of primitives
TypeData td = TypeTranslator.GetTypeData (t, null, true);
if (td.SchemaType == SchemaTypes.Primitive) {
if (t != ambientType)
WriteXsiType (td.XmlType, XmlSchema.Namespace);
Writer.WriteString (XmlCustomFormatter.ToXmlString (td, o));
} else if (IsPrimitiveArray (td)) {
if (!AlreadyQueued (o)) referencedElements.Enqueue (o);
Writer.WriteAttributeString ("href", "#" + GetId (o, true));
} else {
throw new InvalidOperationException ("Invalid type: " + t.FullName);
}
}
WriteEndElement ();
}
protected void WriteReferencedElements ()
{
if (referencedElements == null) return;
if (callbacks == null) return;
while (referencedElements.Count > 0)
{
object o = referencedElements.Dequeue ();
TypeData td = TypeTranslator.GetTypeData (o.GetType ());
WriteCallbackInfo info = (WriteCallbackInfo) callbacks[o.GetType()];
if (info != null) {
WriteStartElement (info.TypeName, info.TypeNs, true);
Writer.WriteAttributeString ("id", GetId (o, false));
if (td.SchemaType != SchemaTypes.Array) // Array use its own "arrayType" attribute
WriteXsiType(info.TypeName, info.TypeNs);
info.Callback (o);
WriteEndElement ();
} else if (IsPrimitiveArray (td)) {
WriteArray (o, td);
}
}
}
bool IsPrimitiveArray (TypeData td)
{
if (td.SchemaType == SchemaTypes.Array) {
if (td.ListItemTypeData.SchemaType == SchemaTypes.Primitive || td.ListItemType == typeof(object))
return true;
return IsPrimitiveArray (td.ListItemTypeData);
} else
return false;
}
void WriteArray (object o, TypeData td)
{
TypeData itemTypeData = td;
string xmlType;
int nDims = -1;
do {
itemTypeData = itemTypeData.ListItemTypeData;
xmlType = itemTypeData.XmlType;
nDims++;
}
while (itemTypeData.SchemaType == SchemaTypes.Array );
while (nDims-- > 0)
xmlType += "[]";
WriteStartElement("Array", XmlSerializer.EncodingNamespace, true);
Writer.WriteAttributeString("id", GetId(o, false));
if (td.SchemaType == SchemaTypes.Array) {
Array a = (Array)o;
int len = a.Length;
Writer.WriteAttributeString("arrayType", XmlSerializer.EncodingNamespace, GetQualifiedName(xmlType, XmlSchema.Namespace) + "[" + len.ToString() + "]");
for (int i = 0; i < len; i++) {
WritePotentiallyReferencingElement("Item", "", a.GetValue(i), td.ListItemType, false, true);
}
}
WriteEndElement();
}
protected void WriteReferencingElement (string n, string ns, object o)
{
WriteReferencingElement (n, ns, o, false);
}
protected void WriteReferencingElement (string n, string ns, object o, bool isNullable)
{
if (o == null)
{
if (isNullable) WriteNullTagEncoded (n, ns);
return;
}
else
{
CheckReferenceQueue ();
if (!AlreadyQueued (o)) referencedElements.Enqueue (o);
Writer.WriteStartElement (n, ns);
Writer.WriteAttributeString ("href", "#" + GetId (o, true));
Writer.WriteEndElement ();
}
}
void CheckReferenceQueue ()
{
if (referencedElements == null)
{
referencedElements = new Queue ();
InitCallbacks ();
}
}
[MonoTODO]
protected void WriteRpcResult (string name, string ns)
{
throw new NotImplementedException ();
}
protected void WriteSerializable (IXmlSerializable serializable, string name, string ns, bool isNullable)
{
WriteSerializable (serializable, name, ns, isNullable, true);
}
#if NET_2_0
protected
#endif
void WriteSerializable (IXmlSerializable serializable, string name, string ns, bool isNullable, bool wrapped)
{
if (serializable == null)
{
if (isNullable && wrapped) WriteNullTagLiteral (name, ns);
return;
}
else
{
if (wrapped)
Writer.WriteStartElement (name, ns);
serializable.WriteXml (Writer);
if (wrapped)
Writer.WriteEndElement ();
}
}
protected void WriteStartDocument ()
{
if (Writer.WriteState == WriteState.Start)
Writer.WriteStartDocument ();
}
protected void WriteStartElement (string name)
{
WriteStartElement (name, String.Empty, null, false);
}
protected void WriteStartElement (string name, string ns)
{
WriteStartElement (name, ns, null, false);
}
protected void WriteStartElement (string name, string ns, bool writePrefixed)
{
WriteStartElement (name, ns, null, writePrefixed);
}
protected void WriteStartElement (string name, string ns, object o)
{
WriteStartElement (name, ns, o, false);
}
protected void WriteStartElement (string name, string ns, object o, bool writePrefixed)
{
WriteStartElement (name, ns, o, writePrefixed, namespaces);
}
#if NET_2_0
protected void WriteStartElement (string name, string ns, Object o, bool writePrefixed, XmlSerializerNamespaces xmlns)
{
WriteStartElement (name, ns, o, writePrefixed, xmlns != null ? xmlns.ToArray () : null);
}
#endif
void WriteStartElement (string name, string ns, object o, bool writePrefixed, ICollection namespaces)
{
if (o != null)
{
if (serializedObjects.Contains (o))
throw new InvalidOperationException ("A circular reference was detected while serializing an object of type " + o.GetType().Name);
else
serializedObjects [o] = o;
}
string prefix = null;
if (topLevelElement && ns != null && ns.Length != 0)
{
if (namespaces != null)
foreach (XmlQualifiedName qn in namespaces)
if (qn.Namespace == ns) {
prefix = qn.Name;
writePrefixed = true;
break;
}
}
if (writePrefixed && ns != string.Empty)
{
name = XmlCustomFormatter.FromXmlName (name);
if (prefix == null)
prefix = Writer.LookupPrefix (ns);
if (prefix == null || prefix.Length == 0)
prefix = "q" + (++qnameCount);
Writer.WriteStartElement (prefix, name, ns);
} else
Writer.WriteStartElement (name, ns);
if (topLevelElement)
{
if (namespaces != null) {
foreach (XmlQualifiedName qn in namespaces)
{
string currentPrefix = Writer.LookupPrefix (qn.Namespace);
if (currentPrefix != null && currentPrefix.Length != 0) continue;
WriteAttribute ("xmlns",qn.Name,xmlNamespace,qn.Namespace);
}
}
topLevelElement = false;
}
}
protected void WriteTypedPrimitive (string name, string ns, object o, bool xsiType)
{
string value;
TypeData td = TypeTranslator.GetTypeData (o.GetType (), null, true);
if (td.SchemaType != SchemaTypes.Primitive)
throw new InvalidOperationException (String.Format ("The type of the argument object '{0}' is not primitive.", td.FullTypeName));
if (name == null) {
ns = td.IsXsdType ? XmlSchema.Namespace : XmlSerializer.WsdlTypesNamespace;
name = td.XmlType;
}
else
name = XmlCustomFormatter.FromXmlName (name);
Writer.WriteStartElement (name, ns);
if (o is XmlQualifiedName)
value = FromXmlQualifiedName ((XmlQualifiedName) o);
else
value = XmlCustomFormatter.ToXmlString (td, o);
if (xsiType)
{
if (td.SchemaType != SchemaTypes.Primitive)
throw new InvalidOperationException (string.Format (unexpectedTypeError, o.GetType().FullName));
WriteXsiType (td.XmlType, td.IsXsdType ? XmlSchema.Namespace : XmlSerializer.WsdlTypesNamespace);
}
WriteValue (value);
Writer.WriteEndElement ();
}
protected void WriteValue (byte[] value)
{
Writer.WriteBase64 (value, 0, value.Length);
}
protected void WriteValue (string value)
{
if (value != null)
Writer.WriteString (value);
}
protected void WriteXmlAttribute (XmlNode node)
{
WriteXmlAttribute (node, null);
}
protected void WriteXmlAttribute (XmlNode node, object container)
{
XmlAttribute attr = node as XmlAttribute;
if (attr == null)
throw new InvalidOperationException ("The node must be either type XmlAttribute or a derived type.");
if (attr.NamespaceURI == XmlSerializer.WsdlNamespace)
{
// The wsdl arrayType attribute needs special handling
if (attr.LocalName == "arrayType") {
string ns, type, dimensions;
TypeTranslator.ParseArrayType (attr.Value, out type, out ns, out dimensions);
string value = GetQualifiedName (type + dimensions, ns);
WriteAttribute (attr.Prefix, attr.LocalName, attr.NamespaceURI, value);
return;
}
}
WriteAttribute (attr.Prefix, attr.LocalName, attr.NamespaceURI, attr.Value);
}
protected void WriteXsiType (string name, string ns)
{
if (ns != null && ns != string.Empty)
WriteAttribute ("type", XmlSchema.InstanceNamespace, GetQualifiedName (name, ns));
else
WriteAttribute ("type", XmlSchema.InstanceNamespace, name);
}
#if NET_2_0
protected Exception CreateInvalidAnyTypeException (object o)
{
if (o == null)
return new InvalidOperationException ("null is invalid as anyType in XmlSerializer");
else
return CreateInvalidAnyTypeException (o.GetType ());
}
protected Exception CreateInvalidAnyTypeException (Type type)
{
return new InvalidOperationException (String.Format ("An object of type '{0}' is invalid as anyType in XmlSerializer", type));
}
protected Exception CreateInvalidEnumValueException (object value, string typeName)
{
return new InvalidOperationException (string.Format(CultureInfo.CurrentCulture,
"'{0}' is not a valid value for {1}.", value, typeName));
}
protected static string FromEnum (long value, string[] values, long[] ids, string typeName)
{
return XmlCustomFormatter.FromEnum (value, values, ids, typeName);
}
[MonoTODO]
protected string FromXmlQualifiedName (XmlQualifiedName xmlQualifiedName, bool ignoreEmpty)
{
throw new NotImplementedException ();
}
[MonoTODO]
protected static Assembly ResolveDynamicAssembly (string assemblyFullName)
{
throw new NotImplementedException ();
}
[MonoTODO]
protected bool EscapeName
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
#endif
#endregion
class WriteCallbackInfo
{
public Type Type;
public string TypeName;
public string TypeNs;
public XmlSerializationWriteCallback Callback;
}
}
}
|