예제 #1
0
 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);
             }
         }
     }
 }
예제 #2
0
        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);
        }