public static StringCollection GenerateWebReferences(WebReferenceCollection webReferences, CodeDomProvider codeProvider, System.CodeDom.CodeCompileUnit codeCompileUnit, WebReferenceOptions options) { if (codeCompileUnit != null) { codeCompileUnit.ReferencedAssemblies.Add("System.dll"); codeCompileUnit.ReferencedAssemblies.Add("System.Xml.dll"); codeCompileUnit.ReferencedAssemblies.Add("System.Web.Services.dll"); codeCompileUnit.ReferencedAssemblies.Add("System.EnterpriseServices.dll"); } Hashtable namespaces = new Hashtable(); Hashtable exportContext = new Hashtable(); foreach (WebReference reference in webReferences) { ServiceDescriptionImporter importer = new ServiceDescriptionImporter(codeCompileUnit); XmlSchemas schemas = new XmlSchemas(); ServiceDescriptionCollection descriptions = new ServiceDescriptionCollection(); foreach (DictionaryEntry entry in reference.Documents) { AddDocument((string)entry.Key, entry.Value, schemas, descriptions, reference.ValidationWarnings); } importer.Schemas.Add(schemas); foreach (ServiceDescription description in descriptions) { importer.AddServiceDescription(description, reference.AppSettingUrlKey, reference.AppSettingBaseUrl); } importer.CodeGenerator = codeProvider; importer.ProtocolName = reference.ProtocolName; importer.Style = options.Style; importer.CodeGenerationOptions = options.CodeGenerationOptions; foreach (string str in options.SchemaImporterExtensions) { importer.Extensions.Add(Type.GetType(str, true)); } System.Xml.Serialization.ImportContext importContext = Context(reference.ProxyCode, namespaces, options.Verbose); reference.Warnings = importer.Import(reference.ProxyCode, importContext, exportContext, reference.ValidationWarnings); if (reference.ValidationWarnings.Count != 0) { reference.Warnings |= ServiceDescriptionImportWarnings.SchemaValidation; } } StringCollection strings = new StringCollection(); if (options.Verbose) { foreach (System.Xml.Serialization.ImportContext context2 in namespaces.Values) { foreach (string str2 in context2.Warnings) { strings.Add(str2); } } } return(strings); }
internal SchemaImporter(XmlSchemas schemas, CodeGenerationOptions options, ImportContext context) { if (!schemas.Contains(XmlSchema.Namespace)) { schemas.AddReference(XmlSchemas.XsdSchema); schemas.SchemaSet.Add(XmlSchemas.XsdSchema); } if (!schemas.Contains(XmlReservedNs.NsXml)) { schemas.AddReference(XmlSchemas.XmlSchema); schemas.SchemaSet.Add(XmlSchemas.XmlSchema); } _schemas = schemas; _options = options; _context = context; Schemas.SetCache(Context.Cache, Context.ShareTypes); }
internal SchemaImporter(XmlSchemas schemas, CodeGenerationOptions options, CodeDomProvider codeProvider, ImportContext context) { if (!schemas.Contains(XmlSchema.Namespace)) { schemas.AddReference(XmlSchemas.XsdSchema); schemas.SchemaSet.Add(XmlSchemas.XsdSchema); } if (!schemas.Contains(XmlReservedNs.NsXml)) { schemas.AddReference(XmlSchemas.XmlSchema); schemas.SchemaSet.Add(XmlSchemas.XmlSchema); } this.schemas = schemas; this.options = options; this.codeProvider = codeProvider; this.context = context; Schemas.SetCache(Context.Cache, Context.ShareTypes); SchemaImporterExtensionsSection section = PrivilegedConfigurationManager.GetSection(ConfigurationStrings.SchemaImporterExtensionsSectionPath) as SchemaImporterExtensionsSection; if (section != null) extensions = section.SchemaImporterExtensionsInternal; else extensions = new SchemaImporterExtensionCollection(); }
internal bool GenerateCode(System.CodeDom.CodeNamespace codeNamespace, System.Xml.Serialization.ImportContext importContext, Hashtable exportContext) { this.bindingCount = 0; this.anyPorts = false; this.codeNamespace = codeNamespace; Hashtable hashtable = new Hashtable(); Hashtable hashtable2 = new Hashtable(); foreach (ServiceDescription description in this.ServiceDescriptions) { foreach (System.Web.Services.Description.Service service in description.Services) { foreach (System.Web.Services.Description.Port port in service.Ports) { System.Web.Services.Description.Binding key = this.ServiceDescriptions.GetBinding(port.Binding); if (!hashtable.Contains(key)) { System.Web.Services.Description.PortType portType = this.ServiceDescriptions.GetPortType(key.Type); this.MoveToBinding(service, port, key, portType); if (this.IsBindingSupported()) { this.bindingCount++; this.anyPorts = true; hashtable.Add(key, key); } else if (key != null) { hashtable2[key] = key; } } } } } if (this.bindingCount == 0) { foreach (ServiceDescription description2 in this.ServiceDescriptions) { foreach (System.Web.Services.Description.Binding binding2 in description2.Bindings) { if (!hashtable2.Contains(binding2)) { System.Web.Services.Description.PortType type2 = this.ServiceDescriptions.GetPortType(binding2.Type); this.MoveToBinding(binding2, type2); if (this.IsBindingSupported()) { this.bindingCount++; } } } } } if (this.bindingCount == 0) { return(codeNamespace.Comments.Count > 0); } this.importContext = importContext; this.exportContext = exportContext; this.BeginNamespace(); hashtable.Clear(); foreach (ServiceDescription description3 in this.ServiceDescriptions) { if (this.anyPorts) { foreach (System.Web.Services.Description.Service service2 in description3.Services) { foreach (System.Web.Services.Description.Port port2 in service2.Ports) { System.Web.Services.Description.Binding binding = this.ServiceDescriptions.GetBinding(port2.Binding); System.Web.Services.Description.PortType type3 = this.ServiceDescriptions.GetPortType(binding.Type); this.MoveToBinding(service2, port2, binding, type3); if (this.IsBindingSupported() && !hashtable.Contains(binding)) { this.GenerateClassForBinding(); hashtable.Add(binding, binding); } } } } else { foreach (System.Web.Services.Description.Binding binding4 in description3.Bindings) { System.Web.Services.Description.PortType type4 = this.ServiceDescriptions.GetPortType(binding4.Type); this.MoveToBinding(binding4, type4); if (this.IsBindingSupported()) { this.GenerateClassForBinding(); } } } } this.EndNamespace(); return(true); }
static Exception UnsupportedException(TypeDesc typeDesc, ImportContext context) { return new InvalidOperationException(Res.GetString(Res.XmlIllegalTypeContext, typeDesc.FullName, GetContextName(context))); }
internal SchemaImporter(XmlSchemas schemas, CodeGenerationOptions options, CodeDomProvider codeProvider, ImportContext context) { if (!schemas.Contains(XmlSchema.Namespace)) { schemas.AddReference(XmlSchemas.XsdSchema); schemas.SchemaSet.Add(XmlSchemas.XsdSchema); } if (!schemas.Contains(XmlReservedNs.NsXml)) { schemas.AddReference(XmlSchemas.XmlSchema); schemas.SchemaSet.Add(XmlSchemas.XmlSchema); } _schemas = schemas; _options = options; _codeProvider = codeProvider; _context = context; Schemas.SetCache(Context.Cache, Context.ShareTypes); SchemaImporterExtensionsSection section = PrivilegedConfigurationManager.GetSection(ConfigurationStrings.SchemaImporterExtensionsSectionPath) as SchemaImporterExtensionsSection; if (section != null) { _extensions = section.SchemaImporterExtensionsInternal; } else { _extensions = new SchemaImporterExtensionCollection(); } }
TypeMapping ImportTypeMapping(TypeModel model, string ns, ImportContext context, string dataType, XmlAttributes a) { return ImportTypeMapping(model, ns, context, dataType, a, false, false); }
SpecialMapping ImportSpecialMapping(Type type, TypeDesc typeDesc, string ns, ImportContext context) { if (specials == null) specials = new Hashtable(); SpecialMapping mapping = (SpecialMapping)specials[type]; if (mapping != null) { CheckContext(mapping.TypeDesc, context); return mapping; } if (typeDesc.Kind == TypeKind.Serializable) { SerializableMapping serializableMapping = null; // get the schema method info object[] attrs = type.GetCustomAttributes(typeof(XmlSchemaProviderAttribute), false); if (attrs.Length > 0) { // new IXmlSerializable XmlSchemaProviderAttribute provider = (XmlSchemaProviderAttribute)attrs[0]; MethodInfo method = GetMethodFromSchemaProvider(provider, type); serializableMapping = new SerializableMapping(method, provider.IsAny, ns); XmlQualifiedName qname = serializableMapping.XsiType; if (qname != null && !qname.IsEmpty) { if (serializables == null) serializables = new NameTable(); SerializableMapping existingMapping = (SerializableMapping)serializables[qname]; if (existingMapping != null) { if (existingMapping.Type == null) { serializableMapping = existingMapping; } else if (existingMapping.Type != type) { SerializableMapping next = existingMapping.Next; existingMapping.Next = serializableMapping; serializableMapping.Next = next; } } else { XmlSchemaType xsdType = serializableMapping.XsdType; if (xsdType != null) SetBase(serializableMapping, xsdType.DerivedFrom); serializables[qname] = serializableMapping; } serializableMapping.TypeName = qname.Name; serializableMapping.Namespace = qname.Namespace; } serializableMapping.TypeDesc = typeDesc; serializableMapping.Type = type; IncludeTypes(type); } else { // old IXmlSerializable serializableMapping = new SerializableMapping(); serializableMapping.TypeDesc = typeDesc; serializableMapping.Type = type; } mapping = serializableMapping; } else { mapping = new SpecialMapping(); mapping.TypeDesc = typeDesc; } CheckContext(typeDesc, context); specials.Add(type, mapping); typeScope.AddTypeMapping(mapping); return mapping; }
internal bool GenerateCode(CodeNamespace codeNamespace, ImportContext importContext, Hashtable exportContext) { bindingCount = 0; anyPorts = false; this.codeNamespace = codeNamespace; Hashtable supportedBindings = new Hashtable(); Hashtable unsupportedBindings = new Hashtable(); // look for ports with bindings foreach (ServiceDescription serviceDescription in ServiceDescriptions) { foreach (Service service in serviceDescription.Services) { foreach (Port port in service.Ports) { Binding binding = ServiceDescriptions.GetBinding(port.Binding); if (supportedBindings.Contains(binding)) continue; PortType portType = ServiceDescriptions.GetPortType(binding.Type); MoveToBinding(service, port, binding, portType); if (IsBindingSupported()) { bindingCount++; anyPorts = true; supportedBindings.Add(binding, binding); } else if (binding != null) unsupportedBindings[binding] = binding; } } } // no ports, look for bindings if (bindingCount == 0) { foreach (ServiceDescription serviceDescription in ServiceDescriptions) { foreach (Binding binding in serviceDescription.Bindings) { if (unsupportedBindings.Contains(binding)) continue; PortType portType = ServiceDescriptions.GetPortType(binding.Type); MoveToBinding(binding, portType); if (IsBindingSupported()) { bindingCount++; } } } } // give up if no bindings if (bindingCount == 0) { // if we generated comments return true so that the comments get written return codeNamespace.Comments.Count > 0; } this.importContext = importContext; this.exportContext = exportContext; BeginNamespace(); supportedBindings.Clear(); foreach (ServiceDescription serviceDescription in ServiceDescriptions) { if (anyPorts) { foreach (Service service in serviceDescription.Services) { foreach (Port port in service.Ports) { Binding binding = ServiceDescriptions.GetBinding(port.Binding); PortType portType = ServiceDescriptions.GetPortType(binding.Type); MoveToBinding(service, port, binding, portType); if (IsBindingSupported() && !supportedBindings.Contains(binding)) { GenerateClassForBinding(); supportedBindings.Add(binding, binding); } } } } else { foreach (Binding binding in serviceDescription.Bindings) { PortType portType = ServiceDescriptions.GetPortType(binding.Type); MoveToBinding(binding, portType); if (IsBindingSupported()) { GenerateClassForBinding(); } } } } EndNamespace(); return true; }
TypeMapping ImportTypeMapping(TypeModel model, string ns, ImportContext context, string dataType, XmlSchemaForm form, bool repeats) { if (dataType.Length > 0) { if (!model.TypeDesc.IsPrimitive) { throw new InvalidOperationException(Res.GetString(Res.XmlInvalidDataTypeUsage, dataType, "XmlElementAttribute.DataType")); } TypeDesc td = typeScope.GetTypeDesc(new XmlQualifiedName(dataType, XmlSchema.Namespace)); if (td == null) { throw new InvalidOperationException(Res.GetString(Res.XmlInvalidXsdDataType, dataType, "XmlElementAttribute.DataType", new XmlQualifiedName(dataType, XmlSchema.Namespace).ToString())); } if (model.TypeDesc.FullName != td.FullName) { throw new InvalidOperationException(Res.GetString(Res.XmlDataTypeMismatch, dataType, "XmlElementAttribute.DataType", model.TypeDesc.FullName)); } } XmlAttributes a = GetAttributes(model.Type); if ((a.XmlFlags & ~(XmlAttributeFlags.Type | XmlAttributeFlags.Root)) != 0) throw new InvalidOperationException(Res.GetString(Res.XmlInvalidTypeAttributes, model.Type.FullName)); switch (model.TypeDesc.Kind) { case TypeKind.Enum: return ImportEnumMapping((EnumModel)model, ns, repeats); case TypeKind.Primitive: if (a.XmlFlags != 0) throw InvalidAttributeUseException(model.Type); return ImportPrimitiveMapping((PrimitiveModel)model, context, dataType, repeats); case TypeKind.Array: case TypeKind.Collection: case TypeKind.Enumerable: if (a.XmlFlags != 0) throw InvalidAttributeUseException(model.Type); if (context != ImportContext.Element) throw UnsupportedException(model.TypeDesc, context); arrayNestingLevel++; ArrayMapping arrayMapping = ImportArrayLikeMapping((ArrayModel)model, ns, form); arrayNestingLevel--; return arrayMapping; case TypeKind.Root: case TypeKind.Class: case TypeKind.Struct: case TypeKind.Interface: if (context != ImportContext.Element) throw UnsupportedException(model.TypeDesc, context); return ImportStructLikeMapping((StructModel)model, ns, form); default: if (a.XmlFlags != 0) throw InvalidAttributeUseException(model.Type); if (model.TypeDesc.IsSpecial) return ImportSpecialMapping(model.Type, model.TypeDesc, ns, context); throw UnsupportedException(model.TypeDesc, context); } }
private static Exception UnsupportedException(TypeDesc typeDesc, ImportContext context) { return new InvalidOperationException(Res.GetString("XmlIllegalTypeContext", new object[] { typeDesc.FullName, GetContextName(context) })); }
private static string GetContextName(ImportContext context) { switch (context) { case ImportContext.Element: return "element"; case ImportContext.Attribute: return "attribute"; case ImportContext.Text: return "text"; default: throw new ArgumentException(SR.XmlInternalError, nameof(context)); } }
private SpecialMapping ImportSpecialMapping(Type type, TypeDesc typeDesc, string ns, ImportContext context, RecursionLimiter limiter) { if (this.specials == null) { this.specials = new Hashtable(); } SpecialMapping mapping = (SpecialMapping) this.specials[type]; if (mapping != null) { this.CheckContext(mapping.TypeDesc, context); return mapping; } if (typeDesc.Kind == TypeKind.Serializable) { SerializableMapping mapping2 = null; object[] customAttributes = type.GetCustomAttributes(typeof(XmlSchemaProviderAttribute), false); if (customAttributes.Length > 0) { XmlSchemaProviderAttribute provider = (XmlSchemaProviderAttribute) customAttributes[0]; mapping2 = new SerializableMapping(GetMethodFromSchemaProvider(provider, type), provider.IsAny, ns); XmlQualifiedName xsiType = mapping2.XsiType; if ((xsiType != null) && !xsiType.IsEmpty) { if (this.serializables == null) { this.serializables = new System.Xml.Serialization.NameTable(); } SerializableMapping mapping3 = (SerializableMapping) this.serializables[xsiType]; if (mapping3 != null) { if (mapping3.Type == null) { mapping2 = mapping3; } else if (mapping3.Type != type) { SerializableMapping next = mapping3.Next; mapping3.Next = mapping2; mapping2.Next = next; } } else { XmlSchemaType xsdType = mapping2.XsdType; if (xsdType != null) { this.SetBase(mapping2, xsdType.DerivedFrom); } this.serializables[xsiType] = mapping2; } mapping2.TypeName = xsiType.Name; mapping2.Namespace = xsiType.Namespace; } mapping2.TypeDesc = typeDesc; mapping2.Type = type; this.IncludeTypes(type); } else { mapping2 = new SerializableMapping { TypeDesc = typeDesc, Type = type }; } mapping = mapping2; } else { mapping = new SpecialMapping { TypeDesc = typeDesc }; } this.CheckContext(typeDesc, context); this.specials.Add(type, mapping); this.typeScope.AddTypeMapping(mapping); return mapping; }
private TypeMapping ImportTypeMapping(TypeModel model, string ns, ImportContext context, string dataType, XmlAttributes a, bool repeats, bool openModel, RecursionLimiter limiter) { TypeMapping mapping3; try { if (dataType.Length > 0) { TypeDesc desc = TypeScope.IsOptionalValue(model.Type) ? model.TypeDesc.BaseTypeDesc : model.TypeDesc; if (!desc.IsPrimitive) { throw new InvalidOperationException(Res.GetString("XmlInvalidDataTypeUsage", new object[] { dataType, "XmlElementAttribute.DataType" })); } TypeDesc typeDesc = this.typeScope.GetTypeDesc(dataType, "http://www.w3.org/2001/XMLSchema"); if (typeDesc == null) { throw new InvalidOperationException(Res.GetString("XmlInvalidXsdDataType", new object[] { dataType, "XmlElementAttribute.DataType", new XmlQualifiedName(dataType, "http://www.w3.org/2001/XMLSchema").ToString() })); } if (desc.FullName != typeDesc.FullName) { throw new InvalidOperationException(Res.GetString("XmlDataTypeMismatch", new object[] { dataType, "XmlElementAttribute.DataType", desc.FullName })); } } if (a == null) { a = this.GetAttributes(model.Type, false); } if ((a.XmlFlags & ~(XmlAttributeFlags.Type | XmlAttributeFlags.Root)) != ((XmlAttributeFlags) 0)) { throw new InvalidOperationException(Res.GetString("XmlInvalidTypeAttributes", new object[] { model.Type.FullName })); } switch (model.TypeDesc.Kind) { case TypeKind.Root: case TypeKind.Struct: case TypeKind.Class: if (context != ImportContext.Element) { throw UnsupportedException(model.TypeDesc, context); } goto Label_0216; case TypeKind.Primitive: if (a.XmlFlags != ((XmlAttributeFlags) 0)) { throw InvalidAttributeUseException(model.Type); } return this.ImportPrimitiveMapping((PrimitiveModel) model, context, dataType, repeats); case TypeKind.Enum: return this.ImportEnumMapping((EnumModel) model, ns, repeats); case TypeKind.Array: case TypeKind.Collection: case TypeKind.Enumerable: if (context != ImportContext.Element) { throw UnsupportedException(model.TypeDesc, context); } break; default: goto Label_02E5; } this.arrayNestingLevel++; ArrayMapping mapping = this.ImportArrayLikeMapping((ArrayModel) model, ns, limiter); this.arrayNestingLevel--; return mapping; Label_0216: if (model.TypeDesc.IsOptionalValue) { TypeDesc desc3 = string.IsNullOrEmpty(dataType) ? model.TypeDesc.BaseTypeDesc : this.typeScope.GetTypeDesc(dataType, "http://www.w3.org/2001/XMLSchema"); string typeName = (desc3.DataType == null) ? desc3.Name : desc3.DataType.Name; TypeMapping baseMapping = this.GetTypeMapping(typeName, ns, desc3, this.types, null); if (baseMapping == null) { baseMapping = this.ImportTypeMapping(this.modelScope.GetTypeModel(model.TypeDesc.BaseTypeDesc.Type), ns, context, dataType, null, repeats, openModel, limiter); } return this.CreateNullableMapping(baseMapping, model.TypeDesc.Type); } return this.ImportStructLikeMapping((StructModel) model, ns, openModel, a, limiter); Label_02E5: if (model.TypeDesc.Kind == TypeKind.Serializable) { if ((a.XmlFlags & ~XmlAttributeFlags.Root) != ((XmlAttributeFlags) 0)) { throw new InvalidOperationException(Res.GetString("XmlSerializableAttributes", new object[] { model.TypeDesc.FullName, typeof(XmlSchemaProviderAttribute).Name })); } } else if (a.XmlFlags != ((XmlAttributeFlags) 0)) { throw InvalidAttributeUseException(model.Type); } if (!model.TypeDesc.IsSpecial) { throw UnsupportedException(model.TypeDesc, context); } mapping3 = this.ImportSpecialMapping(model.Type, model.TypeDesc, ns, context, limiter); } catch (Exception exception) { if (((exception is ThreadAbortException) || (exception is StackOverflowException)) || (exception is OutOfMemoryException)) { throw; } throw this.CreateTypeReflectionException(model.TypeDesc.FullName, exception); } return mapping3; }
private PrimitiveMapping ImportPrimitiveMapping(PrimitiveModel model, ImportContext context, string dataType, bool repeats) { PrimitiveMapping mapping = new PrimitiveMapping(); if (dataType.Length > 0) { mapping.TypeDesc = this.typeScope.GetTypeDesc(dataType, "http://www.w3.org/2001/XMLSchema"); if (mapping.TypeDesc == null) { mapping.TypeDesc = this.typeScope.GetTypeDesc(dataType, "http://microsoft.com/wsdl/types/"); if (mapping.TypeDesc == null) { throw new InvalidOperationException(Res.GetString("XmlUdeclaredXsdType", new object[] { dataType })); } } } else { mapping.TypeDesc = model.TypeDesc; } mapping.TypeName = mapping.TypeDesc.DataType.Name; mapping.Namespace = mapping.TypeDesc.IsXsdType ? "http://www.w3.org/2001/XMLSchema" : "http://microsoft.com/wsdl/types/"; mapping.IsList = repeats; this.CheckContext(mapping.TypeDesc, context); return mapping; }
public SoapSchemaImporter(XmlSchemas schemas, CodeGenerationOptions options, CodeDomProvider codeProvider, ImportContext context) { _importer = new XmlSchemaImporter(schemas, options, codeProvider, context); _importer.UseEncodedFormat = true; }
private ServiceDescriptionImportWarnings Import(CodeNamespace codeNamespace, ImportContext importContext, Hashtable exportContext, StringCollection warnings) { allSchemas = new XmlSchemas(); foreach (XmlSchema schema in schemas) { allSchemas.Add(schema); } foreach (ServiceDescription description in serviceDescriptions) { foreach (XmlSchema schema in description.Types.Schemas) { allSchemas.Add(schema); } } Hashtable references = new Hashtable(); if (!allSchemas.Contains(ServiceDescription.Namespace)) { allSchemas.AddReference(ServiceDescription.Schema); references[ServiceDescription.Schema] = ServiceDescription.Schema; } if (!allSchemas.Contains(Soap.Encoding)) { allSchemas.AddReference(ServiceDescription.SoapEncodingSchema); references[ServiceDescription.SoapEncodingSchema] = ServiceDescription.SoapEncodingSchema; } allSchemas.Compile(null, false); // Segregate the schemas containing abstract types from those // containing regular XML definitions. This is important because // when you import something returning the ur-type (object), then // you need to import ALL types/elements within ALL schemas. We // don't want the RPC-based types leaking over into the XML-based // element definitions. This also occurs when you have derivation: // we need to search the schemas for derived types: but WHICH schemas // should we search. foreach (ServiceDescription description in serviceDescriptions) { foreach (Message message in description.Messages) { foreach (MessagePart part in message.Parts) { bool isEncoded; bool isLiteral; FindUse(part, out isEncoded, out isLiteral); if (part.Element != null && !part.Element.IsEmpty) { if (isEncoded) throw new InvalidOperationException(Res.GetString(Res.CanTSpecifyElementOnEncodedMessagePartsPart, part.Name, message.Name)); XmlSchemaElement element = (XmlSchemaElement)allSchemas.Find(part.Element, typeof(XmlSchemaElement)); if (element != null) { AddSchema(element.Parent as XmlSchema, isEncoded, isLiteral, abstractSchemas, concreteSchemas, references); if (element.SchemaTypeName != null && !element.SchemaTypeName.IsEmpty) { XmlSchemaType type = (XmlSchemaType)allSchemas.Find(element.SchemaTypeName, typeof(XmlSchemaType)); if (type != null) { AddSchema(type.Parent as XmlSchema, isEncoded, isLiteral, abstractSchemas, concreteSchemas, references); } } } } if (part.Type != null && !part.Type.IsEmpty) { XmlSchemaType type = (XmlSchemaType)allSchemas.Find(part.Type, typeof(XmlSchemaType)); if (type != null) { AddSchema(type.Parent as XmlSchema, isEncoded, isLiteral, abstractSchemas, concreteSchemas, references); } } } } } Hashtable imports; foreach (XmlSchemas xmlschemas in new XmlSchemas[] { abstractSchemas, concreteSchemas }) { // collect all imports imports = new Hashtable(); foreach (XmlSchema schema in xmlschemas) { AddImport(schema, imports); } // make sure we add them to the corresponding schema collections foreach (XmlSchema schema in imports.Keys) { if (references[schema] == null && !xmlschemas.Contains(schema)) { xmlschemas.Add(schema); } } } // If a schema was not referenced by either a literal or an encoded message part, // add it to both collections. There's no way to tell which it should be. imports = new Hashtable(); foreach (XmlSchema schema in allSchemas) { if (!abstractSchemas.Contains(schema) && !concreteSchemas.Contains(schema)) { AddImport(schema, imports); } } // make sure we add them to the corresponding schema collections foreach (XmlSchema schema in imports.Keys) { if (references[schema] != null) continue; if (!abstractSchemas.Contains(schema)) { abstractSchemas.Add(schema); } if (!concreteSchemas.Contains(schema)) { concreteSchemas.Add(schema); } } if (abstractSchemas.Count > 0) { foreach (XmlSchema schema in references.Values) { abstractSchemas.AddReference(schema); } StringCollection schemaWarnings = SchemaCompiler.Compile(abstractSchemas); foreach (string warning in schemaWarnings) warnings.Add(warning); } if (concreteSchemas.Count > 0) { foreach (XmlSchema schema in references.Values) { concreteSchemas.AddReference(schema); } StringCollection schemaWarnings = SchemaCompiler.Compile(concreteSchemas); foreach (string warning in schemaWarnings) warnings.Add(warning); } if (ProtocolName.Length > 0) { // If a protocol was specified, only try that one ProtocolImporter importer = FindImporterByName(ProtocolName); if (importer.GenerateCode(codeNamespace, importContext, exportContext)) return importer.Warnings; } else { // Otherwise, do "best" protocol (first one that generates something) for (int i = 0; i < importers.Length; i++) { ProtocolImporter importer = importers[i]; if (importer.GenerateCode(codeNamespace, importContext, exportContext)) { return importer.Warnings; } } } return ServiceDescriptionImportWarnings.NoCodeGenerated; }
/// <summary> /// Generates the data contracts for given xsd file(s). /// </summary> public CodeNamespace GenerateCode() { CodeCompileUnit codeCompileUnit = new CodeCompileUnit(); CodeNamespace codeNamespace = new CodeNamespace(options.ClrNamespace); codeCompileUnit.Namespaces.Add(codeNamespace); // Build the code generation options. GenerationOptions generationOptions = GenerationOptions.None; if (options.GenerateProperties) { generationOptions |= GenerationOptions.GenerateProperties; } if (options.EnableDataBinding) { generationOptions |= GenerationOptions.EnableDataBinding; } if (options.GenerateOrderIdentifiers) { generationOptions |= GenerationOptions.GenerateOrder; } // Build the CodeDom object graph. XmlCodeExporter codeExporter = new XmlCodeExporter(codeNamespace, codeCompileUnit, generationOptions, null); CodeIdentifiers codeIdentifiers = new CodeIdentifiers(); ImportContext importContext = new ImportContext(codeIdentifiers, false); XmlSchemaImporter schemaimporter = new XmlSchemaImporter(schemas, generationOptions, codeProvider, importContext); for (int si = 0; si < schemas.Count; si++) { XmlSchema schema = schemas[si]; IEnumerator enumerator = schema.Elements.Values.GetEnumerator(); IEnumerator enumerator2 = schema.SchemaTypes.Values.GetEnumerator(); try { while (enumerator.MoveNext()) { XmlSchemaElement element = (XmlSchemaElement)enumerator.Current; if (element.IsAbstract) continue; XmlTypeMapping typemapping = schemaimporter.ImportTypeMapping(element.QualifiedName); codeExporter.ExportTypeMapping(typemapping); } while (enumerator2.MoveNext()) { XmlSchemaType type = (XmlSchemaType)enumerator2.Current; if (CouldBeAnArray(type)) continue; XmlTypeMapping typemapping = schemaimporter.ImportSchemaType(type.QualifiedName); codeExporter.ExportTypeMapping(typemapping); } } finally { IDisposable disposableobject = enumerator as IDisposable; if (disposableobject != null) { disposableobject.Dispose(); } IDisposable disposableobject2 = enumerator2 as IDisposable; if (disposableobject2 != null) { disposableobject2.Dispose(); } } } if (codeNamespace.Types.Count == 0) { throw new Exception("No types were generated."); } return codeNamespace; }
/// <include file='doc\SoapSchemaImporter.uex' path='docs/doc[@for="SoapSchemaImporter.SoapSchemaImporter3"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public SoapSchemaImporter(XmlSchemas schemas, CodeGenerationOptions options, ImportContext context) : base(schemas, options, null, context) { }
TypeMapping ImportTypeMapping(TypeModel model, string ns, ImportContext context, string dataType, XmlAttributes a, RecursionLimiter limiter) { return ImportTypeMapping(model, ns, context, dataType, a, false, false, limiter); }
private SpecialMapping ImportSpecialMapping(Type type, TypeDesc typeDesc, string ns, ImportContext context, RecursionLimiter limiter) { if (_specials == null) _specials = new Hashtable(); SpecialMapping mapping = (SpecialMapping)_specials[type]; if (mapping != null) { CheckContext(mapping.TypeDesc, context); return mapping; } if (typeDesc.Kind == TypeKind.Serializable) { SerializableMapping serializableMapping = null; // get the schema method info object[] attrs = type.GetTypeInfo().GetCustomAttributes(typeof(XmlSchemaProviderAttribute), false).ToArray(); if (attrs.Length > 0) { // new IXmlSerializable XmlSchemaProviderAttribute provider = (XmlSchemaProviderAttribute)attrs[0]; MethodInfo method = GetMethodFromSchemaProvider(provider, type); serializableMapping = new SerializableMapping(method, provider.IsAny, ns); XmlQualifiedName qname = serializableMapping.XsiType; if (qname != null && !qname.IsEmpty) { serializableMapping.TypeName = qname.Name; serializableMapping.Namespace = qname.Namespace; } serializableMapping.TypeDesc = typeDesc; serializableMapping.Type = type; IncludeTypes(type.GetTypeInfo()); } else { // old IXmlSerializable serializableMapping = new SerializableMapping(); serializableMapping.TypeDesc = typeDesc; serializableMapping.Type = type; } mapping = serializableMapping; } else { mapping = new SpecialMapping(); mapping.TypeDesc = typeDesc; } CheckContext(typeDesc, context); _specials.Add(type, mapping); _typeScope.AddTypeMapping(mapping); return mapping; }
TypeMapping ImportTypeMapping(TypeModel model, string ns, ImportContext context, string dataType) { return ImportTypeMapping(model, ns, context, dataType, XmlSchemaForm.Qualified, false); }
private static Exception UnsupportedException(TypeDesc typeDesc, ImportContext context) { return new InvalidOperationException(SR.Format(SR.XmlIllegalTypeContext, typeDesc.FullName, GetContextName(context))); }
SpecialMapping ImportSpecialMapping(Type type, TypeDesc typeDesc, string ns, ImportContext context) { SpecialMapping mapping = (SpecialMapping)specials[type]; if (mapping != null) { CheckContext(mapping.TypeDesc, context); return mapping; } if (typeDesc.Kind == TypeKind.Serializable) { SerializableMapping serializableMapping = new SerializableMapping(); serializableMapping.Type = type; mapping = serializableMapping; } else { mapping = new SpecialMapping(); } mapping.TypeDesc = typeDesc; mapping.TypeName = typeDesc.Name; CheckContext(typeDesc, context); specials.Add(type, mapping); return mapping; }
PrimitiveMapping ImportPrimitiveMapping(PrimitiveModel model, ImportContext context, string dataType, bool repeats) { PrimitiveMapping mapping = new PrimitiveMapping(); if (dataType.Length > 0) { mapping.TypeDesc = typeScope.GetTypeDesc(dataType, XmlSchema.Namespace); if (mapping.TypeDesc == null) { // try it as a non-Xsd type mapping.TypeDesc = typeScope.GetTypeDesc(dataType, UrtTypes.Namespace); if (mapping.TypeDesc == null) { throw new InvalidOperationException(Res.GetString(Res.XmlUdeclaredXsdType, dataType)); } } } else { mapping.TypeDesc = model.TypeDesc; } mapping.TypeName = mapping.TypeDesc.DataType.Name; mapping.Namespace = mapping.TypeDesc.IsXsdType ? XmlSchema.Namespace : UrtTypes.Namespace; mapping.IsList = repeats; CheckContext(mapping.TypeDesc, context); return mapping; }
TypeMapping ImportTypeMapping(TypeModel model, string ns, ImportContext context, string dataType, XmlAttributes a, bool repeats, bool openModel) { try { if (dataType.Length > 0) { TypeDesc modelTypeDesc = TypeScope.IsOptionalValue(model.Type) ? model.TypeDesc.BaseTypeDesc : model.TypeDesc; if (!modelTypeDesc.IsPrimitive) { throw new InvalidOperationException(Res.GetString(Res.XmlInvalidDataTypeUsage, dataType, "XmlElementAttribute.DataType")); } TypeDesc td = typeScope.GetTypeDesc(dataType, XmlSchema.Namespace); if (td == null) { throw new InvalidOperationException(Res.GetString(Res.XmlInvalidXsdDataType, dataType, "XmlElementAttribute.DataType", new XmlQualifiedName(dataType, XmlSchema.Namespace).ToString())); } if (modelTypeDesc.FullName != td.FullName) { throw new InvalidOperationException(Res.GetString(Res.XmlDataTypeMismatch, dataType, "XmlElementAttribute.DataType", modelTypeDesc.FullName)); } } if (a == null) a = GetAttributes(model.Type, false); if ((a.XmlFlags & ~(XmlAttributeFlags.Type | XmlAttributeFlags.Root)) != 0) throw new InvalidOperationException(Res.GetString(Res.XmlInvalidTypeAttributes, model.Type.FullName)); switch (model.TypeDesc.Kind) { case TypeKind.Enum: return ImportEnumMapping((EnumModel)model, ns, repeats); case TypeKind.Primitive: if (a.XmlFlags != 0) throw InvalidAttributeUseException(model.Type); return ImportPrimitiveMapping((PrimitiveModel)model, context, dataType, repeats); case TypeKind.Array: case TypeKind.Collection: case TypeKind.Enumerable: //if (a.XmlFlags != 0) throw InvalidAttributeUseException(model.Type); if (context != ImportContext.Element) throw UnsupportedException(model.TypeDesc, context); arrayNestingLevel++; ArrayMapping arrayMapping = ImportArrayLikeMapping((ArrayModel)model, ns); arrayNestingLevel--; return arrayMapping; case TypeKind.Root: case TypeKind.Class: case TypeKind.Struct: if (context != ImportContext.Element) throw UnsupportedException(model.TypeDesc, context); if (model.TypeDesc.IsOptionalValue) { TypeMapping baseMapping = GetTypeMapping(model.TypeDesc.BaseTypeDesc.Name, ns, model.TypeDesc.BaseTypeDesc, types, null); if (baseMapping == null) baseMapping = ImportTypeMapping(modelScope.GetTypeModel(model.TypeDesc.BaseTypeDesc.Type, true), ns, context, dataType, null, repeats, openModel); return CreateNullableMapping(baseMapping, model.TypeDesc); } else { return ImportStructLikeMapping((StructModel)model, ns, openModel, a); } default: if (model.TypeDesc.Kind == TypeKind.Serializable) { // We allow XmlRoot attribute on IXmlSerializable, but not others if ((a.XmlFlags & ~XmlAttributeFlags.Root) != 0) { throw new InvalidOperationException(Res.GetString(Res.XmlSerializableAttributes, model.TypeDesc.FullName, typeof(XmlSchemaProviderAttribute).Name)); } } else { if (a.XmlFlags != 0) throw InvalidAttributeUseException(model.Type); } if (model.TypeDesc.IsSpecial) return ImportSpecialMapping(model.Type, model.TypeDesc, ns, context); throw UnsupportedException(model.TypeDesc, context); } } catch (Exception e) { if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) { throw; } throw CreateTypeReflectionException(model.TypeDesc.FullName, e); } catch { throw CreateTypeReflectionException(model.TypeDesc.FullName, null); } }
/// <include file='doc\SoapSchemaImporter.uex' path='docs/doc[@for="SoapSchemaImporter.SoapSchemaImporter3"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public SoapSchemaImporter(XmlSchemas schemas, CodeGenerationOptions options, ImportContext context) : base(schemas, options, null, context){}
static string GetContextName(ImportContext context) { switch (context) { case ImportContext.Element: return "element"; case ImportContext.Attribute: return "attribute"; case ImportContext.Text: return "text"; default: throw new ArgumentException(Res.GetString(Res.XmlInternalError), "context"); } }
public XmlSchemaImporter (XmlSchemas schemas, CodeGenerationOptions options, ImportContext context) { this.schemas = schemas; this.options = options; if (context != null) { typeIdentifiers = context.TypeIdentifiers; InitSharedData (context); } else typeIdentifiers = new CodeIdentifiers (); InitializeExtensions (); }
void CheckContext(TypeDesc typeDesc, ImportContext context) { switch (context) { case ImportContext.Element: if (typeDesc.CanBeElementValue) return; break; case ImportContext.Attribute: if (typeDesc.CanBeAttributeValue) return; break; case ImportContext.Text: if (typeDesc.CanBeTextValue || typeDesc.IsEnum || typeDesc.IsPrimitive) return; break; default: throw new ArgumentException(Res.GetString(Res.XmlInternalError), "context"); } throw UnsupportedException(typeDesc, context); }
/// <include file='doc\SoapSchemaImporter.uex' path='docs/doc[@for="SoapSchemaImporter.SoapSchemaImporter4"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public SoapSchemaImporter(XmlSchemas schemas, CodeGenerationOptions options, CodeDomProvider codeProvider, ImportContext context) : base(schemas, options, codeProvider, context) { }
[MonoTODO] // where to use Verbose and Extensions in options? public static StringCollection GenerateWebReferences ( WebReferenceCollection webReferences, CodeDomProvider codeGenerator, CodeCompileUnit codeCompileUnit, WebReferenceOptions options) { StringCollection allWarnings = new StringCollection (); ImportContext context = new ImportContext (new CodeIdentifiers(), true); foreach (WebReference reference in webReferences) { ServiceDescriptionImporter importer = new ServiceDescriptionImporter (); if (codeGenerator != null) importer.CodeGenerator = codeGenerator; importer.CodeGenerationOptions = options.CodeGenerationOptions; importer.Context = context; importer.Style = options.Style; importer.ProtocolName = reference.ProtocolName; importer.AddReference (reference); reference.Warnings = importer.Import (reference.ProxyCode, codeCompileUnit); reference.SetValidationWarnings (context.Warnings); foreach (string s in context.Warnings) allWarnings.Add (s); context.Warnings.Clear (); } return allWarnings; }
private ServiceDescriptionImportWarnings Import(CodeNamespace codeNamespace, System.Xml.Serialization.ImportContext importContext, Hashtable exportContext, StringCollection warnings) { Hashtable hashtable2; this.allSchemas = new XmlSchemas(); foreach (XmlSchema schema in this.schemas) { this.allSchemas.Add(schema); } foreach (ServiceDescription description in this.serviceDescriptions) { foreach (XmlSchema schema2 in description.Types.Schemas) { this.allSchemas.Add(schema2); } } Hashtable references = new Hashtable(); if (!this.allSchemas.Contains("http://schemas.xmlsoap.org/wsdl/")) { this.allSchemas.AddReference(ServiceDescription.Schema); references[ServiceDescription.Schema] = ServiceDescription.Schema; } if (!this.allSchemas.Contains("http://schemas.xmlsoap.org/soap/encoding/")) { this.allSchemas.AddReference(ServiceDescription.SoapEncodingSchema); references[ServiceDescription.SoapEncodingSchema] = ServiceDescription.SoapEncodingSchema; } this.allSchemas.Compile(null, false); foreach (ServiceDescription description2 in this.serviceDescriptions) { foreach (Message message in description2.Messages) { foreach (MessagePart part in message.Parts) { bool flag; bool flag2; this.FindUse(part, out flag, out flag2); if ((part.Element != null) && !part.Element.IsEmpty) { if (flag) { throw new InvalidOperationException(System.Web.Services.Res.GetString("CanTSpecifyElementOnEncodedMessagePartsPart", new object[] { part.Name, message.Name })); } XmlSchemaElement element = (XmlSchemaElement)this.allSchemas.Find(part.Element, typeof(XmlSchemaElement)); if (element != null) { AddSchema(element.Parent as XmlSchema, flag, flag2, this.abstractSchemas, this.concreteSchemas, references); if ((element.SchemaTypeName != null) && !element.SchemaTypeName.IsEmpty) { XmlSchemaType type = (XmlSchemaType)this.allSchemas.Find(element.SchemaTypeName, typeof(XmlSchemaType)); if (type != null) { AddSchema(type.Parent as XmlSchema, flag, flag2, this.abstractSchemas, this.concreteSchemas, references); } } } } if ((part.Type != null) && !part.Type.IsEmpty) { XmlSchemaType type2 = (XmlSchemaType)this.allSchemas.Find(part.Type, typeof(XmlSchemaType)); if (type2 != null) { AddSchema(type2.Parent as XmlSchema, flag, flag2, this.abstractSchemas, this.concreteSchemas, references); } } } } } foreach (XmlSchemas schemas in new XmlSchemas[] { this.abstractSchemas, this.concreteSchemas }) { hashtable2 = new Hashtable(); foreach (XmlSchema schema3 in schemas) { this.AddImport(schema3, hashtable2); } foreach (XmlSchema schema4 in hashtable2.Keys) { if ((references[schema4] == null) && !schemas.Contains(schema4)) { schemas.Add(schema4); } } } hashtable2 = new Hashtable(); foreach (XmlSchema schema5 in this.allSchemas) { if (!this.abstractSchemas.Contains(schema5) && !this.concreteSchemas.Contains(schema5)) { this.AddImport(schema5, hashtable2); } } foreach (XmlSchema schema6 in hashtable2.Keys) { if (references[schema6] == null) { if (!this.abstractSchemas.Contains(schema6)) { this.abstractSchemas.Add(schema6); } if (!this.concreteSchemas.Contains(schema6)) { this.concreteSchemas.Add(schema6); } } } if (this.abstractSchemas.Count > 0) { foreach (XmlSchema schema7 in references.Values) { this.abstractSchemas.AddReference(schema7); } foreach (string str in SchemaCompiler.Compile(this.abstractSchemas)) { warnings.Add(str); } } if (this.concreteSchemas.Count > 0) { foreach (XmlSchema schema8 in references.Values) { this.concreteSchemas.AddReference(schema8); } foreach (string str2 in SchemaCompiler.Compile(this.concreteSchemas)) { warnings.Add(str2); } } if (this.ProtocolName.Length > 0) { ProtocolImporter importer = this.FindImporterByName(this.ProtocolName); if (importer.GenerateCode(codeNamespace, importContext, exportContext)) { return(importer.Warnings); } } else { for (int i = 0; i < this.importers.Length; i++) { ProtocolImporter importer2 = this.importers[i]; if (importer2.GenerateCode(codeNamespace, importContext, exportContext)) { return(importer2.Warnings); } } } return(ServiceDescriptionImportWarnings.NoCodeGenerated); }
/// <include file='doc\SoapSchemaImporter.uex' path='docs/doc[@for="SoapSchemaImporter.SoapSchemaImporter4"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public SoapSchemaImporter(XmlSchemas schemas, CodeGenerationOptions options, CodeDomProvider codeProvider, ImportContext context) : base(schemas, options, codeProvider, context){}
public SoapSchemaImporter (XmlSchemas schemas,CodeGenerationOptions options, CodeDomProvider codeProvider, ImportContext context) { _importer = new XmlSchemaImporter (schemas, options, codeProvider, context); _importer.UseEncodedFormat = true; }
void InitSharedData (ImportContext context) { if (context.ShareTypes) { mappedTypes = context.MappedTypes; dataMappedTypes = context.DataMappedTypes; sharedAnonymousTypes = context.SharedAnonymousTypes; } }
internal bool GenerateCode(System.CodeDom.CodeNamespace codeNamespace, System.Xml.Serialization.ImportContext importContext, Hashtable exportContext) { this.bindingCount = 0; this.anyPorts = false; this.codeNamespace = codeNamespace; Hashtable hashtable = new Hashtable(); Hashtable hashtable2 = new Hashtable(); foreach (ServiceDescription description in this.ServiceDescriptions) { foreach (System.Web.Services.Description.Service service in description.Services) { foreach (System.Web.Services.Description.Port port in service.Ports) { System.Web.Services.Description.Binding key = this.ServiceDescriptions.GetBinding(port.Binding); if (!hashtable.Contains(key)) { System.Web.Services.Description.PortType portType = this.ServiceDescriptions.GetPortType(key.Type); this.MoveToBinding(service, port, key, portType); if (this.IsBindingSupported()) { this.bindingCount++; this.anyPorts = true; hashtable.Add(key, key); } else if (key != null) { hashtable2[key] = key; } } } } } if (this.bindingCount == 0) { foreach (ServiceDescription description2 in this.ServiceDescriptions) { foreach (System.Web.Services.Description.Binding binding2 in description2.Bindings) { if (!hashtable2.Contains(binding2)) { System.Web.Services.Description.PortType type2 = this.ServiceDescriptions.GetPortType(binding2.Type); this.MoveToBinding(binding2, type2); if (this.IsBindingSupported()) { this.bindingCount++; } } } } } if (this.bindingCount == 0) { return (codeNamespace.Comments.Count > 0); } this.importContext = importContext; this.exportContext = exportContext; this.BeginNamespace(); hashtable.Clear(); foreach (ServiceDescription description3 in this.ServiceDescriptions) { if (this.anyPorts) { foreach (System.Web.Services.Description.Service service2 in description3.Services) { foreach (System.Web.Services.Description.Port port2 in service2.Ports) { System.Web.Services.Description.Binding binding = this.ServiceDescriptions.GetBinding(port2.Binding); System.Web.Services.Description.PortType type3 = this.ServiceDescriptions.GetPortType(binding.Type); this.MoveToBinding(service2, port2, binding, type3); if (this.IsBindingSupported() && !hashtable.Contains(binding)) { this.GenerateClassForBinding(); hashtable.Add(binding, binding); } } } } else { foreach (System.Web.Services.Description.Binding binding4 in description3.Bindings) { System.Web.Services.Description.PortType type4 = this.ServiceDescriptions.GetPortType(binding4.Type); this.MoveToBinding(binding4, type4); if (this.IsBindingSupported()) { this.GenerateClassForBinding(); } } } } this.EndNamespace(); return true; }