private static void AddSchema(XmlSchema schema, bool isEncoded, bool isLiteral, XmlSchemas abstractSchemas, XmlSchemas concreteSchemas, Hashtable references) { if (schema != null) { if (isEncoded && !abstractSchemas.Contains(schema)) { if (references.Contains(schema)) { abstractSchemas.AddReference(schema); } else { abstractSchemas.Add(schema); } } if (isLiteral && !concreteSchemas.Contains(schema)) { if (references.Contains(schema)) { concreteSchemas.AddReference(schema); } else { concreteSchemas.Add(schema); } } } }
void AddIncludingSchema(XmlSchemas list, string ns) { foreach (XmlSchema sc in Schemas) { if (sc.TargetNamespace == ns && !list.Contains(sc)) { list.Add(sc); foreach (XmlSchemaObject ob in sc.Includes) { XmlSchemaImport import = ob as XmlSchemaImport; if (import != null) { AddIncludingSchema(list, import.Namespace); } } } } }
void AddIncludingSchema(XmlSchemas list, string ns) { XmlSchema sc = Schemas [ns]; if (sc == null || list.Contains(sc)) { return; } list.Add(sc); foreach (XmlSchemaObject ob in sc.Includes) { XmlSchemaImport import = ob as XmlSchemaImport; if (import != null) { AddIncludingSchema(list, import.Namespace); } } }
/// <summary> /// /// Resolve the pointers to included schemas. /// /// When we generate the schemas from types, and more than one schema is generated, /// the "master" schema that contains the schema for the type that is the parameter /// for this method will "include" the other schemas that define the /// types it inherits from or the types that it returns in it's properties. /// If we read this schema off the disk, then the XmlSchemaExternal class's Schema property /// would contain a pointer to the included schema. This is not the case for generated /// schemas. /// /// What we do in this method is recurse through the generated schemas, and look for /// included schemas who's pointers are null. Then we find out what namespoace those /// include schemas are in, and look for that in the generated schemas collection. /// /// If we find it, we "fix" the pointer by setting it to the proper generated schema. /// /// This method modified the schemas in the schema collection /// </summary> /// <param name="xmlSchema">The master schema</param> /// <param name="schemas">the collection of generated schemas</param> private void ResolveImportedSchemas(XmlSchema xmlSchema, XmlSchemas schemas) { string baseSchemaName = GenerateName(); if (string.IsNullOrEmpty(xmlSchema.SourceUri)) { xmlSchema.Id = Path.GetFileNameWithoutExtension(baseSchemaName); xmlSchema.SourceUri = baseSchemaName; } foreach (XmlSchemaObject externalSchemaObj in xmlSchema.Includes) { XmlSchemaExternal externalSchema = externalSchemaObj as XmlSchemaExternal; if (externalSchema == null) { continue; } // if the external schema is not null, we have nothing to worry about if (externalSchema.Schema != null) { continue; } // if the external schema is null, find it in the schema set XmlSchemaImport importSchema = externalSchemaObj as XmlSchemaImport; if (importSchema != null) { if (schemas.Contains(importSchema.Namespace)) { XmlSchema referencedSchema = schemas[importSchema.Namespace]; importSchema.Schema = referencedSchema; string name = GenerateName(); importSchema.Schema.Id = Path.GetFileNameWithoutExtension(name); importSchema.Schema.SourceUri = name; importSchema.SchemaLocation = MakeRelative(xmlSchema.SourceUri, name); } } // resolve any included schemas in the external schema ResolveImportedSchemas(externalSchema.Schema, schemas); } }
/// <summary> /// /// Resolve the pointers to included schemas. /// /// When we generate the schemas from types, and more than one schema is generated, /// the "master" schema that contains the schema for the type that is the parameter /// for this method will "include" the other schemas that define the /// types it inherits from or the types that it returns in it's properties. /// If we read this schema off the disk, then the XmlSchemaExternal class's Schema property /// would contain a pointer to the included schema. This is not the case for generated /// schemas. /// /// What we do in this method is recurse through the generated schemas, and look for /// included schemas who's pointers are null. Then we find out what namespoace those /// include schemas are in, and look for that in the generated schemas collection. /// /// If we find it, we "fix" the pointer by setting it to the proper generated schema. /// /// This method modified the schemas in the schema collection /// </summary> /// <param name="xmlSchema">The master schema</param> /// <param name="schemas">the collection of generated schemas</param> private static void ResolveImportedSchemas(XmlSchema xmlSchema, XmlSchemas schemas) { string baseSchemaName = NameGenerator.GenerateName(); if (string.IsNullOrEmpty(xmlSchema.SourceUri)) { xmlSchema.Id = baseSchemaName; xmlSchema.SourceUri = "file:///" + baseSchemaName + ".xsd"; } foreach (XmlSchemaObject externalSchemaObj in xmlSchema.Includes) { XmlSchemaExternal externalSchema = externalSchemaObj as XmlSchemaExternal; if (externalSchema == null) { continue; } // if the external schema is not null, we have nothing to worry about if (externalSchema.Schema != null) { continue; } // if the external schema is null, find it in the schema set XmlSchemaImport importSchema = externalSchemaObj as XmlSchemaImport; if (importSchema != null) { if (schemas.Contains(importSchema.Namespace)) { XmlSchema referencedSchema = schemas[importSchema.Namespace]; importSchema.Schema = referencedSchema; string name = NameGenerator.GenerateName(); importSchema.Schema.Id = name; importSchema.Schema.SourceUri = "file:///" + name + ".xsd"; importSchema.SchemaLocation = name + ".xsd"; } } // resolve any included schemas in the external schema ResolveImportedSchemas(externalSchema.Schema, schemas); } }
void ClasifySchemas(ArrayList importInfo) { // I don't like this, but I could not find any other way of clasifying // schemas between encoded and literal. xmlSchemas = new XmlSchemas(); soapSchemas = new XmlSchemas(); foreach (ImportInfo info in importInfo) { foreach (Service service in info.ServiceDescription.Services) { foreach (Port port in service.Ports) { this.iinfo = info; this.port = port; binding = ServiceDescriptions.GetBinding(port.Binding); if (binding == null) { continue; } portType = ServiceDescriptions.GetPortType(binding.Type); if (portType == null) { continue; } foreach (OperationBinding oper in binding.Operations) { operationBinding = oper; operation = FindPortOperation(); if (operation == null) { continue; } foreach (OperationMessage omsg in operation.Messages) { Message msg = ServiceDescriptions.GetMessage(omsg.Message); if (msg == null) { continue; } if (omsg is OperationInput) { inputMessage = msg; } else { outputMessage = msg; } } if (GetMessageEncoding(oper.Input) == SoapBindingUse.Encoded) { AddMessageSchema(soapSchemas, oper.Input, inputMessage); } else { AddMessageSchema(xmlSchemas, oper.Input, inputMessage); } if (oper.Output != null) { if (GetMessageEncoding(oper.Output) == SoapBindingUse.Encoded) { AddMessageSchema(soapSchemas, oper.Output, outputMessage); } else { AddMessageSchema(xmlSchemas, oper.Output, outputMessage); } } } } } } XmlSchemas defaultList = xmlSchemas; if (xmlSchemas.Count == 0 && soapSchemas.Count > 0) { defaultList = soapSchemas; } // Schemas not referenced by any message foreach (XmlSchema sc in Schemas) { if (!soapSchemas.Contains(sc) && !xmlSchemas.Contains(sc)) { if (ImportsEncodedNamespace(sc)) { soapSchemas.Add(sc); } else { defaultList.Add(sc); } } } }
/// <summary> /// Add documents to either service description collection or /// schema collection /// </summary> /// <param name="path"></param> /// <param name="document"></param> private void AddDocument(string path, object document) { ServiceDescription serviceDescription = document as ServiceDescription; if (serviceDescription != null) { // if the service description has not already been imported, // add it to the "descriptions" collection if (!m_descriptions.Contains(serviceDescription)) { m_descriptions.Add(serviceDescription); #region write out the WSDL string to the m_wsdl StringWriter stringWriter = new StringWriter(); using (XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter)) { xmlWriter.Formatting = Formatting.Indented; serviceDescription.Write(xmlWriter); } m_wsdl = stringWriter.ToString(); #endregion } else { Trace.TraceWarning( string.Format("Warning: Ignoring duplicate service description with TargetNamespace='{0}' from '{1}'.", serviceDescription.TargetNamespace, path)); } } else { XmlSchema schema = document as XmlSchema; if (schema != null) { // if this schema is no already in the "schemas" collection, // then add it if (!m_schemas.Contains(schema.TargetNamespace)) { m_schemas.Add(schema); #region write the schema text out to the m_xsds StringCollection //StringWriter stringWriter = new StringWriter(); //using (XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter)) //{ // xmlWriter.Formatting = Formatting.Indented; // schema.Write(xmlWriter); //} //this.m_xsds.Add(stringWriter.ToString()); #endregion } else { Trace.TraceWarning(string.Format("Warning: Ignoring duplicate schema description with TargetNamespace='{0}' from '{1}'.", schema.TargetNamespace, path)); } } } }
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); }
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); }
private ServiceDescriptionImportWarnings Import(CodeNamespace codeNamespace) { 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); } } // 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 Exception(Res.GetString(Res.CanTSpecifyElementOnEncodedMessagePartsPart, part.Name, message.Name)); } string ns = part.Element.Namespace; XmlSchema schema = allSchemas[ns]; if (schema != null) { if (isEncoded && abstractSchemas[ns] == null) { abstractSchemas.Add(schema); } if (isLiteral && concreteSchemas[ns] == null) { concreteSchemas.Add(schema); } } } if (part.Type != null && !part.Type.IsEmpty) { string ns = part.Type.Namespace; XmlSchema schema = allSchemas[ns]; if (schema != null) { if (isEncoded && abstractSchemas[ns] == null) { abstractSchemas.Add(schema); } if (isLiteral && concreteSchemas[ns] == null) { concreteSchemas.Add(schema); } } } } } } foreach (XmlSchemas schemas in new XmlSchemas[] { abstractSchemas, concreteSchemas }) { XmlSchemas additionalSchemas = new XmlSchemas(); foreach (XmlSchema schema in schemas) { foreach (object include in schema.Includes) { if (include is XmlSchemaImport) { XmlSchemaImport import = (XmlSchemaImport)include; if (import.Schema != null && !schemas.Contains(import.Schema) && !additionalSchemas.Contains(import.Schema)) { additionalSchemas.Add(import.Schema); } else if (import.Namespace != null && allSchemas[import.Namespace] != null && schemas[import.Namespace] == null && additionalSchemas[import.Namespace] == null) { additionalSchemas.Add(allSchemas[import.Namespace]); } } } } foreach (XmlSchema schema in additionalSchemas) { schemas.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. foreach (XmlSchema schema in allSchemas) { if (!abstractSchemas.Contains(schema) && !concreteSchemas.Contains(schema)) { abstractSchemas.Add(schema); concreteSchemas.Add(schema); } } if (ProtocolName.Length > 0) { // If a protocol was specified, only try that one ProtocolImporter importer = FindImporterByName(ProtocolName); if (importer.GenerateCode(codeNamespace)) { 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)) { return(importer.Warnings); } } } return(ServiceDescriptionImportWarnings.NoCodeGenerated); }