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
|
//------------------------------------------------------------------------------
// <copyright file="SoapSchemaExporter.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
//------------------------------------------------------------------------------
namespace System.Xml.Serialization {
using System;
using System.Collections;
using System.Xml.Schema;
using System.Xml;
using System.ComponentModel;
using System.Diagnostics;
/// <include file='doc\SoapSchemaExporter.uex' path='docs/doc[@for="SoapSchemaExporter"]/*' />
/// <internalonly/>
public class SoapSchemaExporter {
internal const XmlSchemaForm elementFormDefault = XmlSchemaForm.Qualified;
XmlSchemas schemas;
Hashtable types = new Hashtable(); // StructMapping/EnumMapping -> XmlSchemaComplexType/XmlSchemaSimpleType
bool exportedRoot;
TypeScope scope;
XmlDocument document;
static XmlQualifiedName ArrayQName = new XmlQualifiedName(Soap.Array, Soap.Encoding);
static XmlQualifiedName ArrayTypeQName = new XmlQualifiedName(Soap.ArrayType, Soap.Encoding);
/// <include file='doc\SoapSchemaExporter.uex' path='docs/doc[@for="SoapSchemaExporter.SoapSchemaExporter"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public SoapSchemaExporter(XmlSchemas schemas) {
this.schemas = schemas;
}
/// <include file='doc\SoapSchemaExporter.uex' path='docs/doc[@for="SoapSchemaExporter.ExportTypeMapping"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public void ExportTypeMapping(XmlTypeMapping xmlTypeMapping) {
CheckScope(xmlTypeMapping.Scope);
ExportTypeMapping(xmlTypeMapping.Mapping, null);
}
/// <include file='doc\SoapSchemaExporter.uex' path='docs/doc[@for="SoapSchemaExporter.ExportMembersMapping"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public void ExportMembersMapping(XmlMembersMapping xmlMembersMapping) {
ExportMembersMapping(xmlMembersMapping, false);
}
/// <include file='doc\SoapSchemaExporter.uex' path='docs/doc[@for="SoapSchemaExporter.ExportMembersMapping1"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public void ExportMembersMapping(XmlMembersMapping xmlMembersMapping, bool exportEnclosingType) {
CheckScope(xmlMembersMapping.Scope);
MembersMapping membersMapping = (MembersMapping)xmlMembersMapping.Accessor.Mapping;
if (exportEnclosingType) {
ExportTypeMapping(membersMapping, null);
}
else {
foreach (MemberMapping memberMapping in membersMapping.Members) {
if (memberMapping.Elements.Length > 0)
ExportTypeMapping(memberMapping.Elements[0].Mapping, null);
}
}
}
void CheckScope(TypeScope scope) {
if (this.scope == null) {
this.scope = scope;
}
else if (this.scope != scope) {
throw new InvalidOperationException(Res.GetString(Res.XmlMappingsScopeMismatch));
}
}
internal XmlDocument Document {
get {
if (document == null) document = new XmlDocument();
return document;
}
}
void CheckForDuplicateType(string newTypeName, string newNamespace){
XmlSchema schema = schemas[newNamespace];
if (schema != null){
foreach (XmlSchemaObject o in schema.Items) {
XmlSchemaType type = o as XmlSchemaType;
if ( type != null && type.Name == newTypeName)
throw new InvalidOperationException(Res.GetString(Res.XmlDuplicateTypeName, newTypeName, newNamespace));
}
}
}
void AddSchemaItem(XmlSchemaObject item, string ns, string referencingNs) {
if (!SchemaContainsItem(item, ns)) {
XmlSchema schema = schemas[ns];
if (schema == null) {
schema = new XmlSchema();
schema.TargetNamespace = ns == null || ns.Length == 0 ? null : ns;
#pragma warning disable 429 // unreachable code detected: elementFormDefault is const so it will never be Unqualified
schema.ElementFormDefault = elementFormDefault == XmlSchemaForm.Unqualified ? XmlSchemaForm.None : elementFormDefault;
#pragma warning restore 429
schemas.Add(schema);
}
schema.Items.Add(item);
}
if (referencingNs != null)
AddSchemaImport(ns, referencingNs);
}
void AddSchemaImport(string ns, string referencingNs) {
if (referencingNs == null || ns == null) return;
if (ns == referencingNs) return;
XmlSchema schema = schemas[referencingNs];
if (schema == null) throw new InvalidOperationException(Res.GetString(Res.XmlMissingSchema, referencingNs));
if (ns != null && ns.Length > 0 && FindImport(schema, ns) == null) {
XmlSchemaImport import = new XmlSchemaImport();
import.Namespace = ns;
schema.Includes.Add(import);
}
}
bool SchemaContainsItem(XmlSchemaObject item, string ns) {
XmlSchema schema = schemas[ns];
if (schema != null) {
return schema.Items.Contains(item);
}
return false;
}
XmlSchemaImport FindImport(XmlSchema schema, string ns) {
foreach (object item in schema.Includes) {
if (item is XmlSchemaImport) {
XmlSchemaImport import = (XmlSchemaImport)item;
if (import.Namespace == ns) {
return import;
}
}
}
return null;
}
XmlQualifiedName ExportTypeMapping(TypeMapping mapping, string ns) {
if (mapping is ArrayMapping)
return ExportArrayMapping((ArrayMapping)mapping, ns);
else if (mapping is EnumMapping)
return ExportEnumMapping((EnumMapping)mapping, ns);
else if (mapping is PrimitiveMapping) {
PrimitiveMapping pm = (PrimitiveMapping)mapping;
if (pm.TypeDesc.IsXsdType) {
return ExportPrimitiveMapping(pm);
}
else {
return ExportNonXsdPrimitiveMapping(pm, ns);
}
}
else if (mapping is StructMapping)
return ExportStructMapping((StructMapping)mapping, ns);
else if (mapping is NullableMapping)
return ExportTypeMapping(((NullableMapping)mapping).BaseMapping, ns);
else if (mapping is MembersMapping)
return ExportMembersMapping((MembersMapping)mapping, ns);
else
throw new ArgumentException(Res.GetString(Res.XmlInternalError), "mapping");
}
XmlQualifiedName ExportNonXsdPrimitiveMapping(PrimitiveMapping mapping, string ns) {
XmlSchemaType type = mapping.TypeDesc.DataType;
if (!SchemaContainsItem(type, UrtTypes.Namespace)) {
AddSchemaItem(type, UrtTypes.Namespace, ns);
}
else {
AddSchemaImport(UrtTypes.Namespace, ns);
}
return new XmlQualifiedName(mapping.TypeDesc.DataType.Name, UrtTypes.Namespace);
}
XmlQualifiedName ExportPrimitiveMapping(PrimitiveMapping mapping) {
return new XmlQualifiedName(mapping.TypeDesc.DataType.Name, XmlSchema.Namespace);
}
XmlQualifiedName ExportArrayMapping(ArrayMapping mapping, string ns) {
// for the Rpc ArrayMapping different mappings could have the same schema type
// we link all mappings corresponding to the same type together
// loop through all mapping that will map to the same complexType, and export only one,
// the obvious choice is the last one.
while (mapping.Next != null) {
mapping = mapping.Next;
}
XmlSchemaComplexType type = (XmlSchemaComplexType)types[mapping];
if (type == null) {
CheckForDuplicateType(mapping.TypeName, mapping.Namespace);
type = new XmlSchemaComplexType();
type.Name = mapping.TypeName;
types.Add(mapping, type);
// we need to add the type first, to make sure that the schema get created
AddSchemaItem(type, mapping.Namespace, ns);
AddSchemaImport(Soap.Encoding, mapping.Namespace);
AddSchemaImport(Wsdl.Namespace, mapping.Namespace);
XmlSchemaComplexContentRestriction restriction = new XmlSchemaComplexContentRestriction();
XmlQualifiedName qname = ExportTypeMapping(mapping.Elements[0].Mapping, mapping.Namespace);
if (qname.IsEmpty) {
// this is a root mapping
qname = new XmlQualifiedName(Soap.UrType, XmlSchema.Namespace);
}
//<attribute ref="soapenc:arrayType" wsdl:arrayType="xsd:float[]"/>
XmlSchemaAttribute attr = new XmlSchemaAttribute();
attr.RefName = ArrayTypeQName;
XmlAttribute attribute = new XmlAttribute("wsdl", Wsdl.ArrayType, Wsdl.Namespace, Document);
attribute.Value = qname.Namespace + ":" + qname.Name + "[]";
attr.UnhandledAttributes = new XmlAttribute[] {attribute};
restriction.Attributes.Add(attr);
restriction.BaseTypeName = ArrayQName;
XmlSchemaComplexContent model = new XmlSchemaComplexContent();
model.Content = restriction;
type.ContentModel = model;
if (qname.Namespace != XmlSchema.Namespace)
AddSchemaImport(qname.Namespace, mapping.Namespace);
}
else {
AddSchemaImport(mapping.Namespace, ns);
}
return new XmlQualifiedName(mapping.TypeName, mapping.Namespace);
}
void ExportElementAccessors(XmlSchemaGroupBase group, ElementAccessor[] accessors, bool repeats, bool valueTypeOptional, string ns) {
if (accessors.Length == 0) return;
if (accessors.Length == 1) {
ExportElementAccessor(group, accessors[0], repeats, valueTypeOptional, ns);
}
else {
XmlSchemaChoice choice = new XmlSchemaChoice();
choice.MaxOccurs = repeats ? decimal.MaxValue : 1;
choice.MinOccurs = repeats ? 0 : 1;
for (int i = 0; i < accessors.Length; i++)
ExportElementAccessor(choice, accessors[i], false, valueTypeOptional, ns);
if (choice.Items.Count > 0) group.Items.Add(choice);
}
}
void ExportElementAccessor(XmlSchemaGroupBase group, ElementAccessor accessor, bool repeats, bool valueTypeOptional, string ns) {
XmlSchemaElement element = new XmlSchemaElement();
element.MinOccurs = repeats || valueTypeOptional ? 0 : 1;
element.MaxOccurs = repeats ? decimal.MaxValue : 1;
element.Name = accessor.Name;
element.IsNillable = accessor.IsNullable || accessor.Mapping is NullableMapping;
element.Form = XmlSchemaForm.Unqualified;
element.SchemaTypeName = ExportTypeMapping(accessor.Mapping, accessor.Namespace);
group.Items.Add(element);
}
XmlQualifiedName ExportRootMapping(StructMapping mapping) {
if (!exportedRoot) {
exportedRoot = true;
ExportDerivedMappings(mapping);
}
return new XmlQualifiedName(Soap.UrType, XmlSchema.Namespace);
}
XmlQualifiedName ExportStructMapping(StructMapping mapping, string ns) {
if (mapping.TypeDesc.IsRoot) return ExportRootMapping(mapping);
XmlSchemaComplexType type = (XmlSchemaComplexType)types[mapping];
if (type == null) {
if (!mapping.IncludeInSchema) throw new InvalidOperationException(Res.GetString(Res.XmlSoapCannotIncludeInSchema, mapping.TypeDesc.Name));
CheckForDuplicateType(mapping.TypeName, mapping.Namespace);
type = new XmlSchemaComplexType();
type.Name = mapping.TypeName;
types.Add(mapping, type);
AddSchemaItem(type, mapping.Namespace, ns);
type.IsAbstract = mapping.TypeDesc.IsAbstract;
if (mapping.BaseMapping != null && mapping.BaseMapping.IncludeInSchema) {
XmlSchemaComplexContentExtension extension = new XmlSchemaComplexContentExtension();
extension.BaseTypeName = ExportStructMapping(mapping.BaseMapping, mapping.Namespace);
XmlSchemaComplexContent model = new XmlSchemaComplexContent();
model.Content = extension;
type.ContentModel = model;
}
ExportTypeMembers(type, mapping.Members, mapping.Namespace);
ExportDerivedMappings(mapping);
}
else {
AddSchemaImport(mapping.Namespace, ns);
}
return new XmlQualifiedName(type.Name, mapping.Namespace);
}
XmlQualifiedName ExportMembersMapping(MembersMapping mapping, string ns) {
XmlSchemaComplexType type = (XmlSchemaComplexType)types[mapping];
if(type == null) {
CheckForDuplicateType(mapping.TypeName, mapping.Namespace);
type = new XmlSchemaComplexType();
type.Name = mapping.TypeName;
types.Add(mapping, type);
AddSchemaItem(type, mapping.Namespace, ns);
ExportTypeMembers(type, mapping.Members, mapping.Namespace);
}
else {
AddSchemaImport(mapping.Namespace, ns);
}
return new XmlQualifiedName(type.Name, mapping.Namespace);
}
void ExportTypeMembers(XmlSchemaComplexType type, MemberMapping[] members, string ns) {
XmlSchemaGroupBase seq = new XmlSchemaSequence();
for (int i = 0; i < members.Length; i++) {
MemberMapping member = members[i];
if (member.Elements.Length > 0) {
bool valueTypeOptional = member.CheckSpecified != SpecifiedAccessor.None || member.CheckShouldPersist || !member.TypeDesc.IsValueType;
ExportElementAccessors(seq, member.Elements, false, valueTypeOptional, ns);
}
}
if (seq.Items.Count > 0) {
if (type.ContentModel != null) {
if (type.ContentModel.Content is XmlSchemaComplexContentExtension)
((XmlSchemaComplexContentExtension)type.ContentModel.Content).Particle = seq;
else if (type.ContentModel.Content is XmlSchemaComplexContentRestriction)
((XmlSchemaComplexContentRestriction)type.ContentModel.Content).Particle = seq;
else
throw new InvalidOperationException(Res.GetString(Res.XmlInvalidContent, type.ContentModel.Content.GetType().Name));
}
else {
type.Particle = seq;
}
}
}
void ExportDerivedMappings(StructMapping mapping) {
for (StructMapping derived = mapping.DerivedMappings; derived != null; derived = derived.NextDerivedMapping) {
if (derived.IncludeInSchema) ExportStructMapping(derived, mapping.TypeDesc.IsRoot ? null : mapping.Namespace);
}
}
XmlQualifiedName ExportEnumMapping(EnumMapping mapping, string ns) {
XmlSchemaSimpleType dataType = (XmlSchemaSimpleType)types[mapping];
if (dataType == null) {
CheckForDuplicateType(mapping.TypeName, mapping.Namespace);
dataType = new XmlSchemaSimpleType();
dataType.Name = mapping.TypeName;
types.Add(mapping, dataType);
AddSchemaItem(dataType, mapping.Namespace, ns);
XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction();
restriction.BaseTypeName = new XmlQualifiedName("string", XmlSchema.Namespace);
for (int i = 0; i < mapping.Constants.Length; i++) {
ConstantMapping constant = mapping.Constants[i];
XmlSchemaEnumerationFacet enumeration = new XmlSchemaEnumerationFacet();
enumeration.Value = constant.XmlName;
restriction.Facets.Add(enumeration);
}
if (!mapping.IsFlags) {
dataType.Content = restriction;
}
else {
XmlSchemaSimpleTypeList list = new XmlSchemaSimpleTypeList();
XmlSchemaSimpleType enumType = new XmlSchemaSimpleType();
enumType.Content = restriction;
list.ItemType = enumType;
dataType.Content = list;
}
}
else {
AddSchemaImport(mapping.Namespace, ns);
}
return new XmlQualifiedName(mapping.TypeName, mapping.Namespace);
}
}
}
|