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
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.Runtime.Serialization
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.Diagnostics;
// using System.ServiceModel.Diagnostics;
using System.Security;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.Runtime.Serialization.Diagnostics;
class SchemaExporter
{
XmlSchemaSet schemas;
XmlDocument xmlDoc;
DataContractSet dataContractSet;
internal SchemaExporter(XmlSchemaSet schemas, DataContractSet dataContractSet)
{
this.schemas = schemas;
this.dataContractSet = dataContractSet;
}
XmlSchemaSet Schemas
{
get { return schemas; }
}
XmlDocument XmlDoc
{
get
{
if (xmlDoc == null)
xmlDoc = new XmlDocument();
return xmlDoc;
}
}
internal void Export()
{
try
{
// Remove this if we decide to publish serialization schema at well-known location
ExportSerializationSchema();
foreach (KeyValuePair<XmlQualifiedName, DataContract> pair in dataContractSet)
{
DataContract dataContract = pair.Value;
if (!dataContractSet.IsContractProcessed(dataContract))
{
ExportDataContract(dataContract);
dataContractSet.SetContractProcessed(dataContract);
}
}
}
finally
{
xmlDoc = null;
dataContractSet = null;
}
}
void ExportSerializationSchema()
{
if (!Schemas.Contains(Globals.SerializationNamespace))
{
StringReader reader = new StringReader(Globals.SerializationSchema);
XmlSchema schema = XmlSchema.Read(new XmlTextReader(reader) { DtdProcessing = DtdProcessing.Prohibit }, null);
if (schema == null)
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.CouldNotReadSerializationSchema, Globals.SerializationNamespace)));
Schemas.Add(schema);
}
}
void ExportDataContract(DataContract dataContract)
{
if (dataContract.IsBuiltInDataContract)
return;
else if (dataContract is XmlDataContract)
ExportXmlDataContract((XmlDataContract)dataContract);
else
{
XmlSchema schema = GetSchema(dataContract.StableName.Namespace);
if (dataContract is ClassDataContract)
{
ClassDataContract classDataContract = (ClassDataContract)dataContract;
if (classDataContract.IsISerializable)
ExportISerializableDataContract(classDataContract, schema);
else
ExportClassDataContract(classDataContract, schema);
}
else if (dataContract is CollectionDataContract)
ExportCollectionDataContract((CollectionDataContract)dataContract, schema);
else if (dataContract is EnumDataContract)
ExportEnumDataContract((EnumDataContract)dataContract, schema);
ExportTopLevelElement(dataContract, schema);
Schemas.Reprocess(schema);
}
}
XmlSchemaElement ExportTopLevelElement(DataContract dataContract, XmlSchema schema)
{
if (schema == null || dataContract.StableName.Namespace != dataContract.TopLevelElementNamespace.Value)
schema = GetSchema(dataContract.TopLevelElementNamespace.Value);
XmlSchemaElement topLevelElement = new XmlSchemaElement();
topLevelElement.Name = dataContract.TopLevelElementName.Value;
SetElementType(topLevelElement, dataContract, schema);
topLevelElement.IsNillable = true;
schema.Items.Add(topLevelElement);
return topLevelElement;
}
void ExportClassDataContract(ClassDataContract classDataContract, XmlSchema schema)
{
XmlSchemaComplexType type = new XmlSchemaComplexType();
type.Name = classDataContract.StableName.Name;
schema.Items.Add(type);
XmlElement genericInfoElement = null;
if (classDataContract.UnderlyingType.IsGenericType)
genericInfoElement = ExportGenericInfo(classDataContract.UnderlyingType, Globals.GenericTypeLocalName, Globals.SerializationNamespace);
XmlSchemaSequence rootSequence = new XmlSchemaSequence();
for (int i = 0; i < classDataContract.Members.Count; i++)
{
DataMember dataMember = classDataContract.Members[i];
XmlSchemaElement element = new XmlSchemaElement();
element.Name = dataMember.Name;
XmlElement actualTypeElement = null;
DataContract memberTypeContract = dataContractSet.GetMemberTypeDataContract(dataMember);
if (CheckIfMemberHasConflict(dataMember))
{
element.SchemaTypeName = AnytypeQualifiedName;
actualTypeElement = ExportActualType(memberTypeContract.StableName);
SchemaHelper.AddSchemaImport(memberTypeContract.StableName.Namespace, schema);
}
else
SetElementType(element, memberTypeContract, schema);
SchemaHelper.AddElementForm(element, schema);
if (dataMember.IsNullable)
element.IsNillable = true;
if (!dataMember.IsRequired)
element.MinOccurs = 0;
element.Annotation = GetSchemaAnnotation(actualTypeElement, ExportSurrogateData(dataMember), ExportEmitDefaultValue(dataMember));
rootSequence.Items.Add(element);
}
XmlElement isValueTypeElement = null;
if (classDataContract.BaseContract != null)
{
XmlSchemaComplexContentExtension extension = CreateTypeContent(type, classDataContract.BaseContract.StableName, schema);
extension.Particle = rootSequence;
if (classDataContract.IsReference && !classDataContract.BaseContract.IsReference)
{
AddReferenceAttributes(extension.Attributes, schema);
}
}
else
{
type.Particle = rootSequence;
if (classDataContract.IsValueType)
isValueTypeElement = GetAnnotationMarkup(IsValueTypeName, XmlConvert.ToString(classDataContract.IsValueType), schema);
if (classDataContract.IsReference)
AddReferenceAttributes(type.Attributes, schema);
}
type.Annotation = GetSchemaAnnotation(genericInfoElement, ExportSurrogateData(classDataContract), isValueTypeElement);
}
void AddReferenceAttributes(XmlSchemaObjectCollection attributes, XmlSchema schema)
{
SchemaHelper.AddSchemaImport(Globals.SerializationNamespace, schema);
schema.Namespaces.Add(Globals.SerPrefixForSchema, Globals.SerializationNamespace);
attributes.Add(IdAttribute);
attributes.Add(RefAttribute);
}
void SetElementType(XmlSchemaElement element, DataContract dataContract, XmlSchema schema)
{
XmlDataContract xmlDataContract = dataContract as XmlDataContract;
if (xmlDataContract != null && xmlDataContract.IsAnonymous)
{
element.SchemaType = xmlDataContract.XsdType;
}
else
{
element.SchemaTypeName = dataContract.StableName;
if (element.SchemaTypeName.Namespace.Equals(Globals.SerializationNamespace))
schema.Namespaces.Add(Globals.SerPrefixForSchema, Globals.SerializationNamespace);
SchemaHelper.AddSchemaImport(dataContract.StableName.Namespace, schema);
}
}
bool CheckIfMemberHasConflict(DataMember dataMember)
{
if (dataMember.HasConflictingNameAndType)
return true;
DataMember conflictingMember = dataMember.ConflictingMember;
while (conflictingMember != null)
{
if (conflictingMember.HasConflictingNameAndType)
return true;
conflictingMember = conflictingMember.ConflictingMember;
}
return false;
}
private XmlElement ExportEmitDefaultValue(DataMember dataMember)
{
if (dataMember.EmitDefaultValue)
return null;
XmlElement defaultValueElement = XmlDoc.CreateElement(DefaultValueAnnotation.Name, DefaultValueAnnotation.Namespace);
XmlAttribute emitDefaultValueAttribute = XmlDoc.CreateAttribute(Globals.EmitDefaultValueAttribute);
emitDefaultValueAttribute.Value = Globals.False;
defaultValueElement.Attributes.Append(emitDefaultValueAttribute);
return defaultValueElement;
}
XmlElement ExportActualType(XmlQualifiedName typeName)
{
return ExportActualType(typeName, XmlDoc);
}
static XmlElement ExportActualType(XmlQualifiedName typeName, XmlDocument xmlDoc)
{
XmlElement actualTypeElement = xmlDoc.CreateElement(ActualTypeAnnotationName.Name, ActualTypeAnnotationName.Namespace);
XmlAttribute nameAttribute = xmlDoc.CreateAttribute(Globals.ActualTypeNameAttribute);
nameAttribute.Value = typeName.Name;
actualTypeElement.Attributes.Append(nameAttribute);
XmlAttribute nsAttribute = xmlDoc.CreateAttribute(Globals.ActualTypeNamespaceAttribute);
nsAttribute.Value = typeName.Namespace;
actualTypeElement.Attributes.Append(nsAttribute);
return actualTypeElement;
}
XmlElement ExportGenericInfo(Type clrType, string elementName, string elementNs)
{
Type itemType;
int nestedCollectionLevel = 0;
while (CollectionDataContract.IsCollection(clrType, out itemType))
{
if (DataContract.GetBuiltInDataContract(clrType) != null
|| CollectionDataContract.IsCollectionDataContract(clrType))
{
break;
}
clrType = itemType;
nestedCollectionLevel++;
}
Type[] genericArguments = null;
IList<int> genericArgumentCounts = null;
if (clrType.IsGenericType)
{
genericArguments = clrType.GetGenericArguments();
string typeName;
if (clrType.DeclaringType == null)
typeName = clrType.Name;
else
{
int nsLen = (clrType.Namespace == null) ? 0 : clrType.Namespace.Length;
if (nsLen > 0)
nsLen++; //include the . following namespace
typeName = DataContract.GetClrTypeFullName(clrType).Substring(nsLen).Replace('+', '.');
}
int iParam = typeName.IndexOf('[');
if (iParam >= 0)
typeName = typeName.Substring(0, iParam);
genericArgumentCounts = DataContract.GetDataContractNameForGenericName(typeName, null);
clrType = clrType.GetGenericTypeDefinition();
}
XmlQualifiedName dcqname = DataContract.GetStableName(clrType);
if (nestedCollectionLevel > 0)
{
string collectionName = dcqname.Name;
for (int n = 0; n < nestedCollectionLevel; n++)
collectionName = Globals.ArrayPrefix + collectionName;
dcqname = new XmlQualifiedName(collectionName, DataContract.GetCollectionNamespace(dcqname.Namespace));
}
XmlElement typeElement = XmlDoc.CreateElement(elementName, elementNs);
XmlAttribute nameAttribute = XmlDoc.CreateAttribute(Globals.GenericNameAttribute);
nameAttribute.Value = genericArguments != null ? XmlConvert.DecodeName(dcqname.Name) : dcqname.Name;
//nameAttribute.Value = dcqname.Name;
typeElement.Attributes.Append(nameAttribute);
XmlAttribute nsAttribute = XmlDoc.CreateAttribute(Globals.GenericNamespaceAttribute);
nsAttribute.Value = dcqname.Namespace;
typeElement.Attributes.Append(nsAttribute);
if (genericArguments != null)
{
int argIndex = 0;
int nestedLevel = 0;
foreach (int genericArgumentCount in genericArgumentCounts)
{
for (int i = 0; i < genericArgumentCount; i++, argIndex++)
{
XmlElement argumentElement = ExportGenericInfo(genericArguments[argIndex], Globals.GenericParameterLocalName, Globals.SerializationNamespace);
if (nestedLevel > 0)
{
XmlAttribute nestedLevelAttribute = XmlDoc.CreateAttribute(Globals.GenericParameterNestedLevelAttribute);
nestedLevelAttribute.Value = nestedLevel.ToString(CultureInfo.InvariantCulture);
argumentElement.Attributes.Append(nestedLevelAttribute);
}
typeElement.AppendChild(argumentElement);
}
nestedLevel++;
}
if (genericArgumentCounts[nestedLevel - 1] == 0)
{
XmlAttribute typeNestedLevelsAttribute = XmlDoc.CreateAttribute(Globals.GenericParameterNestedLevelAttribute);
typeNestedLevelsAttribute.Value = genericArgumentCounts.Count.ToString(CultureInfo.InvariantCulture);
typeElement.Attributes.Append(typeNestedLevelsAttribute);
}
}
return typeElement;
}
XmlElement ExportSurrogateData(object key)
{
object surrogateData = dataContractSet.GetSurrogateData(key);
if (surrogateData == null)
return null;
StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture);
XmlWriterSettings writerSettings = new XmlWriterSettings();
writerSettings.OmitXmlDeclaration = true;
XmlWriter xmlWriter = XmlWriter.Create(stringWriter, writerSettings);
Collection<Type> knownTypes = new Collection<Type>();
DataContractSurrogateCaller.GetKnownCustomDataTypes(dataContractSet.DataContractSurrogate, knownTypes);
DataContractSerializer serializer = new DataContractSerializer(Globals.TypeOfObject,
SurrogateDataAnnotationName.Name, SurrogateDataAnnotationName.Namespace, knownTypes, Int32.MaxValue,
false /*ignoreExtensionDataObject*/, true /*preserveObjectReferences*/, null /*dataContractSurrogate*/);
serializer.WriteObject(xmlWriter, surrogateData);
xmlWriter.Flush();
return (XmlElement)XmlDoc.ReadNode(XmlReader.Create(new StringReader(stringWriter.ToString())));
}
void ExportCollectionDataContract(CollectionDataContract collectionDataContract, XmlSchema schema)
{
XmlSchemaComplexType type = new XmlSchemaComplexType();
type.Name = collectionDataContract.StableName.Name;
schema.Items.Add(type);
XmlElement genericInfoElement = null, isDictionaryElement = null;
if (collectionDataContract.UnderlyingType.IsGenericType && CollectionDataContract.IsCollectionDataContract(collectionDataContract.UnderlyingType))
genericInfoElement = ExportGenericInfo(collectionDataContract.UnderlyingType, Globals.GenericTypeLocalName, Globals.SerializationNamespace);
if (collectionDataContract.IsDictionary)
isDictionaryElement = ExportIsDictionary();
type.Annotation = GetSchemaAnnotation(isDictionaryElement, genericInfoElement, ExportSurrogateData(collectionDataContract));
XmlSchemaSequence rootSequence = new XmlSchemaSequence();
XmlSchemaElement element = new XmlSchemaElement();
element.Name = collectionDataContract.ItemName;
element.MinOccurs = 0;
element.MaxOccursString = Globals.OccursUnbounded;
if (collectionDataContract.IsDictionary)
{
ClassDataContract keyValueContract = collectionDataContract.ItemContract as ClassDataContract;
XmlSchemaComplexType keyValueType = new XmlSchemaComplexType();
XmlSchemaSequence keyValueSequence = new XmlSchemaSequence();
foreach (DataMember dataMember in keyValueContract.Members)
{
XmlSchemaElement keyValueElement = new XmlSchemaElement();
keyValueElement.Name = dataMember.Name;
SetElementType(keyValueElement, dataContractSet.GetMemberTypeDataContract(dataMember), schema);
SchemaHelper.AddElementForm(keyValueElement, schema);
if (dataMember.IsNullable)
keyValueElement.IsNillable = true;
keyValueElement.Annotation = GetSchemaAnnotation(ExportSurrogateData(dataMember));
keyValueSequence.Items.Add(keyValueElement);
}
keyValueType.Particle = keyValueSequence;
element.SchemaType = keyValueType;
}
else
{
if (collectionDataContract.IsItemTypeNullable)
element.IsNillable = true;
DataContract itemContract = dataContractSet.GetItemTypeDataContract(collectionDataContract);
SetElementType(element, itemContract, schema);
}
SchemaHelper.AddElementForm(element, schema);
rootSequence.Items.Add(element);
type.Particle = rootSequence;
if (collectionDataContract.IsReference)
AddReferenceAttributes(type.Attributes, schema);
}
XmlElement ExportIsDictionary()
{
XmlElement isDictionaryElement = XmlDoc.CreateElement(IsDictionaryAnnotationName.Name, IsDictionaryAnnotationName.Namespace);
isDictionaryElement.InnerText = Globals.True;
return isDictionaryElement;
}
void ExportEnumDataContract(EnumDataContract enumDataContract, XmlSchema schema)
{
XmlSchemaSimpleType type = new XmlSchemaSimpleType();
type.Name = enumDataContract.StableName.Name;
XmlElement actualTypeElement = (enumDataContract.BaseContractName == DefaultEnumBaseTypeName) ? null : ExportActualType(enumDataContract.BaseContractName);
type.Annotation = GetSchemaAnnotation(actualTypeElement, ExportSurrogateData(enumDataContract));
schema.Items.Add(type);
XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction();
restriction.BaseTypeName = StringQualifiedName;
SchemaHelper.AddSchemaImport(enumDataContract.BaseContractName.Namespace, schema);
if (enumDataContract.Values != null)
{
for (int i = 0; i < enumDataContract.Values.Count; i++)
{
XmlSchemaEnumerationFacet facet = new XmlSchemaEnumerationFacet();
facet.Value = enumDataContract.Members[i].Name;
if (enumDataContract.Values[i] != GetDefaultEnumValue(enumDataContract.IsFlags, i))
facet.Annotation = GetSchemaAnnotation(EnumerationValueAnnotationName, enumDataContract.GetStringFromEnumValue(enumDataContract.Values[i]), schema);
restriction.Facets.Add(facet);
}
}
if (enumDataContract.IsFlags)
{
XmlSchemaSimpleTypeList list = new XmlSchemaSimpleTypeList();
XmlSchemaSimpleType anonymousType = new XmlSchemaSimpleType();
anonymousType.Content = restriction;
list.ItemType = anonymousType;
type.Content = list;
}
else
type.Content = restriction;
}
internal static long GetDefaultEnumValue(bool isFlags, int index)
{
return isFlags ? (long)Math.Pow(2, index) : index;
}
void ExportISerializableDataContract(ClassDataContract dataContract, XmlSchema schema)
{
XmlSchemaComplexType type = new XmlSchemaComplexType();
type.Name = dataContract.StableName.Name;
schema.Items.Add(type);
XmlElement genericInfoElement = null;
if (dataContract.UnderlyingType.IsGenericType)
genericInfoElement = ExportGenericInfo(dataContract.UnderlyingType, Globals.GenericTypeLocalName, Globals.SerializationNamespace);
XmlElement isValueTypeElement = null;
if (dataContract.BaseContract != null)
{
XmlSchemaComplexContentExtension extension = CreateTypeContent(type, dataContract.BaseContract.StableName, schema);
}
else
{
schema.Namespaces.Add(Globals.SerPrefixForSchema, Globals.SerializationNamespace);
type.Particle = ISerializableSequence;
XmlSchemaAttribute iSerializableFactoryTypeAttribute = ISerializableFactoryTypeAttribute;
type.Attributes.Add(iSerializableFactoryTypeAttribute);
SchemaHelper.AddSchemaImport(ISerializableFactoryTypeAttribute.RefName.Namespace, schema);
if (dataContract.IsValueType)
isValueTypeElement = GetAnnotationMarkup(IsValueTypeName, XmlConvert.ToString(dataContract.IsValueType), schema);
}
type.Annotation = GetSchemaAnnotation(genericInfoElement, ExportSurrogateData(dataContract), isValueTypeElement);
}
XmlSchemaComplexContentExtension CreateTypeContent(XmlSchemaComplexType type, XmlQualifiedName baseTypeName, XmlSchema schema)
{
SchemaHelper.AddSchemaImport(baseTypeName.Namespace, schema);
XmlSchemaComplexContentExtension extension = new XmlSchemaComplexContentExtension();
extension.BaseTypeName = baseTypeName;
type.ContentModel = new XmlSchemaComplexContent();
type.ContentModel.Content = extension;
return extension;
}
void ExportXmlDataContract(XmlDataContract dataContract)
{
XmlQualifiedName typeQName;
bool hasRoot;
XmlSchemaType xsdType;
Type clrType = dataContract.UnderlyingType;
if (!IsSpecialXmlType(clrType, out typeQName, out xsdType, out hasRoot))
if (!InvokeSchemaProviderMethod(clrType, schemas, out typeQName, out xsdType, out hasRoot))
InvokeGetSchemaMethod(clrType, schemas, typeQName);
if (hasRoot)
{
if (!(typeQName.Equals(dataContract.StableName)))
{
Fx.Assert("XML data contract type name does not match schema name");
}
XmlSchema schema;
if (SchemaHelper.GetSchemaElement(Schemas,
new XmlQualifiedName(dataContract.TopLevelElementName.Value, dataContract.TopLevelElementNamespace.Value),
out schema) == null)
{
XmlSchemaElement topLevelElement = ExportTopLevelElement(dataContract, schema);
topLevelElement.IsNillable = dataContract.IsTopLevelElementNullable;
ReprocessAll(schemas);
}
XmlSchemaType anonymousType = xsdType;
xsdType = SchemaHelper.GetSchemaType(schemas, typeQName, out schema);
if (anonymousType == null && xsdType == null && typeQName.Namespace != XmlSchema.Namespace)
{
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.MissingSchemaType, typeQName, DataContract.GetClrTypeFullName(clrType))));
}
if (xsdType != null)
xsdType.Annotation = GetSchemaAnnotation(
ExportSurrogateData(dataContract),
dataContract.IsValueType ?
GetAnnotationMarkup(IsValueTypeName, XmlConvert.ToString(dataContract.IsValueType), schema) :
null
);
else if (DiagnosticUtility.ShouldTraceVerbose)
{
TraceUtility.Trace(TraceEventType.Verbose, TraceCode.XsdExportAnnotationFailed,
SR.GetString(SR.TraceCodeXsdExportAnnotationFailed), new StringTraceRecord("Type", typeQName.Namespace + ":" + typeQName.Name));
}
}
}
static void ReprocessAll(XmlSchemaSet schemas)// and remove duplicate items
{
Hashtable elements = new Hashtable();
Hashtable types = new Hashtable();
XmlSchema[] schemaArray = new XmlSchema[schemas.Count];
schemas.CopyTo(schemaArray, 0);
for (int i = 0; i < schemaArray.Length; i++)
{
XmlSchema schema = schemaArray[i];
XmlSchemaObject[] itemArray = new XmlSchemaObject[schema.Items.Count];
schema.Items.CopyTo(itemArray, 0);
for (int j = 0; j < itemArray.Length; j++)
{
XmlSchemaObject item = itemArray[j];
Hashtable items;
XmlQualifiedName qname;
if (item is XmlSchemaElement)
{
items = elements;
qname = new XmlQualifiedName(((XmlSchemaElement)item).Name, schema.TargetNamespace);
}
else if (item is XmlSchemaType)
{
items = types;
qname = new XmlQualifiedName(((XmlSchemaType)item).Name, schema.TargetNamespace);
}
else
continue;
object otherItem = items[qname];
if (otherItem != null)
{
if (DiagnosticUtility.ShouldTraceWarning)
{
Dictionary<string, string> values = new Dictionary<string, string>(2)
{
{ "ItemType", item.ToString() },
{ "Name", qname.Namespace + ":" + qname.Name }
};
TraceUtility.Trace(TraceEventType.Warning, TraceCode.XsdExportDupItems,
SR.GetString(SR.TraceCodeXsdExportDupItems), new DictionaryTraceRecord(values));
}
schema.Items.Remove(item);
}
else
items.Add(qname, item);
}
schemas.Reprocess(schema);
}
}
internal static void GetXmlTypeInfo(Type type, out XmlQualifiedName stableName, out XmlSchemaType xsdType, out bool hasRoot)
{
if (IsSpecialXmlType(type, out stableName, out xsdType, out hasRoot))
return;
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.XmlResolver = null;
InvokeSchemaProviderMethod(type, schemas, out stableName, out xsdType, out hasRoot);
if (stableName.Name == null || stableName.Name.Length == 0)
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.InvalidXmlDataContractName, DataContract.GetClrTypeFullName(type))));
}
static bool InvokeSchemaProviderMethod(Type clrType, XmlSchemaSet schemas, out XmlQualifiedName stableName, out XmlSchemaType xsdType, out bool hasRoot)
{
xsdType = null;
hasRoot = true;
object[] attrs = clrType.GetCustomAttributes(Globals.TypeOfXmlSchemaProviderAttribute, false);
if (attrs == null || attrs.Length == 0)
{
stableName = DataContract.GetDefaultStableName(clrType);
return false;
}
XmlSchemaProviderAttribute provider = (XmlSchemaProviderAttribute)attrs[0];
if (provider.IsAny)
{
xsdType = CreateAnyElementType();
hasRoot = false;
}
string methodName = provider.MethodName;
if (methodName == null || methodName.Length == 0)
{
if (!provider.IsAny)
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.InvalidGetSchemaMethod, DataContract.GetClrTypeFullName(clrType))));
stableName = DataContract.GetDefaultStableName(clrType);
}
else
{
MethodInfo getMethod = clrType.GetMethod(methodName, /*BindingFlags.DeclaredOnly |*/ BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public, null, new Type[] { typeof(XmlSchemaSet) }, null);
if (getMethod == null)
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.MissingGetSchemaMethod, DataContract.GetClrTypeFullName(clrType), methodName)));
if (!(Globals.TypeOfXmlQualifiedName.IsAssignableFrom(getMethod.ReturnType)) && !(Globals.TypeOfXmlSchemaType.IsAssignableFrom(getMethod.ReturnType)))
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.InvalidReturnTypeOnGetSchemaMethod, DataContract.GetClrTypeFullName(clrType), methodName, DataContract.GetClrTypeFullName(getMethod.ReturnType), DataContract.GetClrTypeFullName(Globals.TypeOfXmlQualifiedName), typeof(XmlSchemaType))));
object typeInfo = getMethod.Invoke(null, new object[] { schemas });
if (provider.IsAny)
{
if (typeInfo != null)
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.InvalidNonNullReturnValueByIsAny, DataContract.GetClrTypeFullName(clrType), methodName)));
stableName = DataContract.GetDefaultStableName(clrType);
}
else if (typeInfo == null)
{
xsdType = CreateAnyElementType();
hasRoot = false;
stableName = DataContract.GetDefaultStableName(clrType);
}
else
{
XmlSchemaType providerXsdType = typeInfo as XmlSchemaType;
if (providerXsdType != null)
{
string typeName = providerXsdType.Name;
string typeNs = null;
if (typeName == null || typeName.Length == 0)
{
DataContract.GetDefaultStableName(DataContract.GetClrTypeFullName(clrType), out typeName, out typeNs);
stableName = new XmlQualifiedName(typeName, typeNs);
providerXsdType.Annotation = GetSchemaAnnotation(ExportActualType(stableName, new XmlDocument()));
xsdType = providerXsdType;
}
else
{
foreach (XmlSchema schema in schemas.Schemas())
{
foreach (XmlSchemaObject schemaItem in schema.Items)
{
if ((object)schemaItem == (object)providerXsdType)
{
typeNs = schema.TargetNamespace;
if (typeNs == null)
typeNs = String.Empty;
break;
}
}
if (typeNs != null)
break;
}
if (typeNs == null)
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.MissingSchemaType, typeName, DataContract.GetClrTypeFullName(clrType))));
stableName = new XmlQualifiedName(typeName, typeNs);
}
}
else
stableName = (XmlQualifiedName)typeInfo;
}
}
return true;
}
static void InvokeGetSchemaMethod(Type clrType, XmlSchemaSet schemas, XmlQualifiedName stableName)
{
IXmlSerializable ixmlSerializable = (IXmlSerializable)Activator.CreateInstance(clrType);
XmlSchema schema = ixmlSerializable.GetSchema();
if (schema == null)
{
AddDefaultDatasetType(schemas, stableName.Name, stableName.Namespace);
}
else
{
if (schema.Id == null || schema.Id.Length == 0)
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.InvalidReturnSchemaOnGetSchemaMethod, DataContract.GetClrTypeFullName(clrType))));
AddDefaultTypedDatasetType(schemas, schema, stableName.Name, stableName.Namespace);
}
}
internal static void AddDefaultXmlType(XmlSchemaSet schemas, string localName, string ns)
{
XmlSchemaComplexType defaultXmlType = CreateAnyType();
defaultXmlType.Name = localName;
XmlSchema schema = SchemaHelper.GetSchema(ns, schemas);
schema.Items.Add(defaultXmlType);
schemas.Reprocess(schema);
}
static XmlSchemaComplexType CreateAnyType()
{
XmlSchemaComplexType anyType = new XmlSchemaComplexType();
anyType.IsMixed = true;
anyType.Particle = new XmlSchemaSequence();
XmlSchemaAny any = new XmlSchemaAny();
any.MinOccurs = 0;
any.MaxOccurs = Decimal.MaxValue;
any.ProcessContents = XmlSchemaContentProcessing.Lax;
((XmlSchemaSequence)anyType.Particle).Items.Add(any);
anyType.AnyAttribute = new XmlSchemaAnyAttribute();
return anyType;
}
static XmlSchemaComplexType CreateAnyElementType()
{
XmlSchemaComplexType anyElementType = new XmlSchemaComplexType();
anyElementType.IsMixed = false;
anyElementType.Particle = new XmlSchemaSequence();
XmlSchemaAny any = new XmlSchemaAny();
any.MinOccurs = 0;
any.ProcessContents = XmlSchemaContentProcessing.Lax;
((XmlSchemaSequence)anyElementType.Particle).Items.Add(any);
return anyElementType;
}
internal static bool IsSpecialXmlType(Type type, out XmlQualifiedName typeName, out XmlSchemaType xsdType, out bool hasRoot)
{
xsdType = null;
hasRoot = true;
if (type == Globals.TypeOfXmlElement || type == Globals.TypeOfXmlNodeArray)
{
string name = null;
if (type == Globals.TypeOfXmlElement)
{
xsdType = CreateAnyElementType();
name = "XmlElement";
hasRoot = false;
}
else
{
xsdType = CreateAnyType();
name = "ArrayOfXmlNode";
hasRoot = true;
}
typeName = new XmlQualifiedName(name, DataContract.GetDefaultStableNamespace(type));
return true;
}
typeName = null;
return false;
}
static void AddDefaultDatasetType(XmlSchemaSet schemas, string localName, string ns)
{
XmlSchemaComplexType type = new XmlSchemaComplexType();
type.Name = localName;
type.Particle = new XmlSchemaSequence();
XmlSchemaElement schemaRefElement = new XmlSchemaElement();
schemaRefElement.RefName = new XmlQualifiedName(Globals.SchemaLocalName, XmlSchema.Namespace);
((XmlSchemaSequence)type.Particle).Items.Add(schemaRefElement);
XmlSchemaAny any = new XmlSchemaAny();
((XmlSchemaSequence)type.Particle).Items.Add(any);
XmlSchema schema = SchemaHelper.GetSchema(ns, schemas);
schema.Items.Add(type);
schemas.Reprocess(schema);
}
static void AddDefaultTypedDatasetType(XmlSchemaSet schemas, XmlSchema datasetSchema, string localName, string ns)
{
XmlSchemaComplexType type = new XmlSchemaComplexType();
type.Name = localName;
type.Particle = new XmlSchemaSequence();
XmlSchemaAny any = new XmlSchemaAny();
any.Namespace = (datasetSchema.TargetNamespace == null) ? String.Empty : datasetSchema.TargetNamespace;
((XmlSchemaSequence)type.Particle).Items.Add(any);
schemas.Add(datasetSchema);
XmlSchema schema = SchemaHelper.GetSchema(ns, schemas);
schema.Items.Add(type);
schemas.Reprocess(datasetSchema);
schemas.Reprocess(schema);
}
XmlSchemaAnnotation GetSchemaAnnotation(XmlQualifiedName annotationQualifiedName, string innerText, XmlSchema schema)
{
XmlSchemaAnnotation annotation = new XmlSchemaAnnotation();
XmlSchemaAppInfo appInfo = new XmlSchemaAppInfo();
XmlElement annotationElement = GetAnnotationMarkup(annotationQualifiedName, innerText, schema);
appInfo.Markup = new XmlNode[1] { annotationElement };
annotation.Items.Add(appInfo);
return annotation;
}
static XmlSchemaAnnotation GetSchemaAnnotation(params XmlNode[] nodes)
{
if (nodes == null || nodes.Length == 0)
return null;
bool hasAnnotation = false;
for (int i = 0; i < nodes.Length; i++)
if (nodes[i] != null)
{
hasAnnotation = true;
break;
}
if (!hasAnnotation)
return null;
XmlSchemaAnnotation annotation = new XmlSchemaAnnotation();
XmlSchemaAppInfo appInfo = new XmlSchemaAppInfo();
annotation.Items.Add(appInfo);
appInfo.Markup = nodes;
return annotation;
}
XmlElement GetAnnotationMarkup(XmlQualifiedName annotationQualifiedName, string innerText, XmlSchema schema)
{
XmlElement annotationElement = XmlDoc.CreateElement(annotationQualifiedName.Name, annotationQualifiedName.Namespace);
SchemaHelper.AddSchemaImport(annotationQualifiedName.Namespace, schema);
annotationElement.InnerText = innerText;
return annotationElement;
}
XmlSchema GetSchema(string ns)
{
return SchemaHelper.GetSchema(ns, Schemas);
}
// Property is not stored in a local because XmlSchemaSequence is mutable.
// The schema export process should not expose objects that may be modified later.
internal static XmlSchemaSequence ISerializableSequence
{
get
{
XmlSchemaSequence iSerializableSequence = new XmlSchemaSequence();
iSerializableSequence.Items.Add(ISerializableWildcardElement);
return iSerializableSequence;
}
}
// Property is not stored in a local because XmlSchemaAny is mutable.
// The schema export process should not expose objects that may be modified later.
internal static XmlSchemaAny ISerializableWildcardElement
{
get
{
XmlSchemaAny iSerializableWildcardElement = new XmlSchemaAny();
iSerializableWildcardElement.MinOccurs = 0;
iSerializableWildcardElement.MaxOccursString = Globals.OccursUnbounded;
iSerializableWildcardElement.Namespace = "##local";
iSerializableWildcardElement.ProcessContents = XmlSchemaContentProcessing.Skip;
return iSerializableWildcardElement;
}
}
[Fx.Tag.SecurityNote(Critical = "Static fields are marked SecurityCritical or readonly to prevent"
+ " data from being modified or leaked to other components in appdomain.")]
[SecurityCritical]
static XmlQualifiedName anytypeQualifiedName;
internal static XmlQualifiedName AnytypeQualifiedName
{
[Fx.Tag.SecurityNote(Critical = "Fetches the critical anytypeQualifiedName field.",
Safe = "Get-only properties only needs to be protected for write; initialized in getter if null.")]
[SecuritySafeCritical]
get
{
if (anytypeQualifiedName == null)
anytypeQualifiedName = new XmlQualifiedName(Globals.AnyTypeLocalName, Globals.SchemaNamespace);
return anytypeQualifiedName;
}
}
[Fx.Tag.SecurityNote(Critical = "Static fields are marked SecurityCritical or readonly to prevent"
+ " data from being modified or leaked to other components in appdomain.")]
[SecurityCritical]
static XmlQualifiedName stringQualifiedName;
internal static XmlQualifiedName StringQualifiedName
{
[Fx.Tag.SecurityNote(Critical = "Fetches the critical stringQualifiedName field.",
Safe = "Get-only properties only needs to be protected for write; initialized in getter if null.")]
[SecuritySafeCritical]
get
{
if (stringQualifiedName == null)
stringQualifiedName = new XmlQualifiedName(Globals.StringLocalName, Globals.SchemaNamespace);
return stringQualifiedName;
}
}
[Fx.Tag.SecurityNote(Critical = "Static fields are marked SecurityCritical or readonly to prevent"
+ " data from being modified or leaked to other components in appdomain.")]
[SecurityCritical]
static XmlQualifiedName defaultEnumBaseTypeName;
internal static XmlQualifiedName DefaultEnumBaseTypeName
{
[Fx.Tag.SecurityNote(Critical = "Fetches the critical defaultEnumBaseTypeName field.",
Safe = "Get-only properties only needs to be protected for write; initialized in getter if null.")]
[SecuritySafeCritical]
get
{
if (defaultEnumBaseTypeName == null)
defaultEnumBaseTypeName = new XmlQualifiedName(Globals.IntLocalName, Globals.SchemaNamespace);
return defaultEnumBaseTypeName;
}
}
[Fx.Tag.SecurityNote(Critical = "Static fields are marked SecurityCritical or readonly to prevent"
+ " data from being modified or leaked to other components in appdomain.")]
[SecurityCritical]
static XmlQualifiedName enumerationValueAnnotationName;
internal static XmlQualifiedName EnumerationValueAnnotationName
{
[Fx.Tag.SecurityNote(Critical = "Fetches the critical enumerationValueAnnotationName field.",
Safe = "Get-only properties only needs to be protected for write; initialized in getter if null.")]
[SecuritySafeCritical]
get
{
if (enumerationValueAnnotationName == null)
enumerationValueAnnotationName = new XmlQualifiedName(Globals.EnumerationValueLocalName, Globals.SerializationNamespace);
return enumerationValueAnnotationName;
}
}
[Fx.Tag.SecurityNote(Critical = "Static fields are marked SecurityCritical or readonly to prevent"
+ " data from being modified or leaked to other components in appdomain.")]
[SecurityCritical]
static XmlQualifiedName surrogateDataAnnotationName;
internal static XmlQualifiedName SurrogateDataAnnotationName
{
[Fx.Tag.SecurityNote(Critical = "Fetches the critical surrogateDataAnnotationName field.",
Safe = "Get-only properties only needs to be protected for write; initialized in getter if null.")]
[SecuritySafeCritical]
get
{
if (surrogateDataAnnotationName == null)
surrogateDataAnnotationName = new XmlQualifiedName(Globals.SurrogateDataLocalName, Globals.SerializationNamespace);
return surrogateDataAnnotationName;
}
}
[Fx.Tag.SecurityNote(Critical = "Static fields are marked SecurityCritical or readonly to prevent"
+ " data from being modified or leaked to other components in appdomain.")]
[SecurityCritical]
static XmlQualifiedName defaultValueAnnotation;
internal static XmlQualifiedName DefaultValueAnnotation
{
[Fx.Tag.SecurityNote(Critical = "Fetches the critical defaultValueAnnotation field.",
Safe = "Get-only properties only needs to be protected for write; initialized in getter if null.")]
[SecuritySafeCritical]
get
{
if (defaultValueAnnotation == null)
defaultValueAnnotation = new XmlQualifiedName(Globals.DefaultValueLocalName, Globals.SerializationNamespace);
return defaultValueAnnotation;
}
}
[Fx.Tag.SecurityNote(Critical = "Static fields are marked SecurityCritical or readonly to prevent"
+ " data from being modified or leaked to other components in appdomain.")]
[SecurityCritical]
static XmlQualifiedName actualTypeAnnotationName;
internal static XmlQualifiedName ActualTypeAnnotationName
{
[Fx.Tag.SecurityNote(Critical = "Fetches the critical actualTypeAnnotationName field.",
Safe = "Get-only properties only needs to be protected for write; initialized in getter if null.")]
[SecuritySafeCritical]
get
{
if (actualTypeAnnotationName == null)
actualTypeAnnotationName = new XmlQualifiedName(Globals.ActualTypeLocalName, Globals.SerializationNamespace);
return actualTypeAnnotationName;
}
}
[Fx.Tag.SecurityNote(Critical = "Static fields are marked SecurityCritical or readonly to prevent"
+ " data from being modified or leaked to other components in appdomain.")]
[SecurityCritical]
static XmlQualifiedName isDictionaryAnnotationName;
internal static XmlQualifiedName IsDictionaryAnnotationName
{
[Fx.Tag.SecurityNote(Critical = "Fetches the critical isDictionaryAnnotationName field.",
Safe = "Get-only properties only needs to be protected for write; initialized in getter if null.")]
[SecuritySafeCritical]
get
{
if (isDictionaryAnnotationName == null)
isDictionaryAnnotationName = new XmlQualifiedName(Globals.IsDictionaryLocalName, Globals.SerializationNamespace);
return isDictionaryAnnotationName;
}
}
[Fx.Tag.SecurityNote(Critical = "Static fields are marked SecurityCritical or readonly to prevent"
+ " data from being modified or leaked to other components in appdomain.")]
[SecurityCritical]
static XmlQualifiedName isValueTypeName;
internal static XmlQualifiedName IsValueTypeName
{
[Fx.Tag.SecurityNote(Critical = "Fetches the critical isValueTypeName field.",
Safe = "Get-only properties only needs to be protected for write; initialized in getter if null.")]
[SecuritySafeCritical]
get
{
if (isValueTypeName == null)
isValueTypeName = new XmlQualifiedName(Globals.IsValueTypeLocalName, Globals.SerializationNamespace);
return isValueTypeName;
}
}
// Property is not stored in a local because XmlSchemaAttribute is mutable.
// The schema export process should not expose objects that may be modified later.
internal static XmlSchemaAttribute ISerializableFactoryTypeAttribute
{
get
{
XmlSchemaAttribute iSerializableFactoryTypeAttribute = new XmlSchemaAttribute();
iSerializableFactoryTypeAttribute.RefName = new XmlQualifiedName(Globals.ISerializableFactoryTypeLocalName, Globals.SerializationNamespace);
return iSerializableFactoryTypeAttribute;
}
}
// Property is not stored in a local because XmlSchemaAttribute is mutable.
// The schema export process should not expose objects that may be modified later.
internal static XmlSchemaAttribute RefAttribute
{
get
{
XmlSchemaAttribute refAttribute = new XmlSchemaAttribute();
refAttribute.RefName = Globals.RefQualifiedName;
return refAttribute;
}
}
// Property is not stored in a local because XmlSchemaAttribute is mutable.
// The schema export process should not expose objects that may be modified later.
internal static XmlSchemaAttribute IdAttribute
{
get
{
XmlSchemaAttribute idAttribute = new XmlSchemaAttribute();
idAttribute.RefName = Globals.IdQualifiedName;
return idAttribute;
}
}
}
}
|