예제 #1
0
        CollectionDataContract ImportCollection(XmlQualifiedName typeName, XmlSchemaSequence rootSequence, XmlSchemaObjectCollection attributes, XmlSchemaAnnotation annotation, bool isReference)
        {
            CollectionDataContract dataContract = new CollectionDataContract(CollectionKind.Array);
            dataContract.StableName = typeName;
            AddDataContract(dataContract);

            dataContract.IsReference = isReference;

            // CheckIfCollection has already checked if sequence contains exactly one item with maxOccurs="unbounded" or maxOccurs > 1 
            XmlSchemaElement element = (XmlSchemaElement)rootSequence.Items[0];

            dataContract.IsItemTypeNullable = element.IsNillable;
            dataContract.ItemName = element.Name;

            XmlSchemaForm elementForm = (element.Form == XmlSchemaForm.None) ? SchemaHelper.GetSchemaWithType(SchemaObjects, schemaSet, typeName).ElementFormDefault : element.Form;
            if (elementForm != XmlSchemaForm.Qualified)
                ThrowArrayTypeCannotBeImportedException(typeName.Name, typeName.Namespace, SR.GetString(SR.ArrayItemFormMustBe, element.Name));
            CheckIfElementUsesUnsupportedConstructs(typeName, element);

            if (element.SchemaTypeName.IsEmpty)
            {
                if (element.SchemaType != null)
                {
                    XmlQualifiedName shortName = new XmlQualifiedName(element.Name, typeName.Namespace);
                    DataContract contract = dataContractSet[shortName];
                    if (contract == null)
                    {
                        dataContract.ItemContract = ImportAnonymousElement(element, shortName);
                    }
                    else
                    {
                        XmlQualifiedName fullName = new XmlQualifiedName(String.Format(CultureInfo.InvariantCulture, "{0}.{1}Type", typeName.Name, element.Name), typeName.Namespace);
                        dataContract.ItemContract = ImportAnonymousElement(element, fullName);
                    }
                }
                else if (!element.RefName.IsEmpty)
                    ThrowArrayTypeCannotBeImportedException(typeName.Name, typeName.Namespace, SR.GetString(SR.ElementRefOnLocalElementNotSupported, element.RefName.Name, element.RefName.Namespace));
                else
                    dataContract.ItemContract = ImportType(SchemaExporter.AnytypeQualifiedName);
            }
            else
            {
                dataContract.ItemContract = ImportType(element.SchemaTypeName);
            }

            if (IsDictionary(typeName, annotation))
            {
                ClassDataContract keyValueContract = dataContract.ItemContract as ClassDataContract;
                DataMember key = null, value = null;
                if (keyValueContract == null || keyValueContract.Members == null || keyValueContract.Members.Count != 2
                    || !(key = keyValueContract.Members[0]).IsRequired || !(value = keyValueContract.Members[1]).IsRequired)
                {
                    ThrowArrayTypeCannotBeImportedException(typeName.Name, typeName.Namespace, SR.GetString(SR.InvalidKeyValueType, element.Name));
                }
                if (keyValueContract.Namespace != dataContract.Namespace)
                {
                    ThrowArrayTypeCannotBeImportedException(typeName.Name, typeName.Namespace, SR.GetString(SR.InvalidKeyValueTypeNamespace, element.Name, keyValueContract.Namespace));
                }
                keyValueContract.IsValueType = true;
                dataContract.KeyName = key.Name;
                dataContract.ValueName = value.Name;
                if (element.SchemaType != null)
                {
                    dataContractSet.Remove(keyValueContract.StableName);

                    GenericInfo genericInfo = new GenericInfo(DataContract.GetStableName(Globals.TypeOfKeyValue), Globals.TypeOfKeyValue.FullName);
                    genericInfo.Add(GetGenericInfoForDataMember(key));
                    genericInfo.Add(GetGenericInfoForDataMember(value));
                    genericInfo.AddToLevel(0, 2);
                    dataContract.ItemContract.StableName = new XmlQualifiedName(genericInfo.GetExpandedStableName().Name, typeName.Namespace);
                }
            }

            return dataContract;
        }
 private CollectionDataContract ImportCollection(XmlQualifiedName typeName, XmlSchemaSequence rootSequence, XmlSchemaObjectCollection attributes, XmlSchemaAnnotation annotation, bool isReference)
 {
     CollectionDataContract dataContract = new CollectionDataContract(CollectionKind.Array) {
         StableName = typeName
     };
     this.AddDataContract(dataContract);
     dataContract.IsReference = isReference;
     XmlSchemaElement element = (XmlSchemaElement) rootSequence.Items[0];
     dataContract.IsItemTypeNullable = element.IsNillable;
     dataContract.ItemName = element.Name;
     XmlSchemaForm form = (element.Form == XmlSchemaForm.None) ? SchemaHelper.GetSchemaWithType(this.SchemaObjects, this.schemaSet, typeName).ElementFormDefault : element.Form;
     if (form != XmlSchemaForm.Qualified)
     {
         ThrowArrayTypeCannotBeImportedException(typeName.Name, typeName.Namespace, System.Runtime.Serialization.SR.GetString("ArrayItemFormMustBe", new object[] { element.Name }));
     }
     this.CheckIfElementUsesUnsupportedConstructs(typeName, element);
     if (element.SchemaTypeName.IsEmpty)
     {
         if (element.SchemaType != null)
         {
             XmlQualifiedName typeQName = new XmlQualifiedName(element.Name, typeName.Namespace);
             if (this.dataContractSet[typeQName] == null)
             {
                 dataContract.ItemContract = this.ImportAnonymousElement(element, typeQName);
             }
             else
             {
                 XmlQualifiedName name2 = new XmlQualifiedName(string.Format(CultureInfo.InvariantCulture, "{0}.{1}Type", new object[] { typeName.Name, element.Name }), typeName.Namespace);
                 dataContract.ItemContract = this.ImportAnonymousElement(element, name2);
             }
         }
         else if (!element.RefName.IsEmpty)
         {
             ThrowArrayTypeCannotBeImportedException(typeName.Name, typeName.Namespace, System.Runtime.Serialization.SR.GetString("ElementRefOnLocalElementNotSupported", new object[] { element.RefName.Name, element.RefName.Namespace }));
         }
         else
         {
             dataContract.ItemContract = this.ImportType(SchemaExporter.AnytypeQualifiedName);
         }
     }
     else
     {
         dataContract.ItemContract = this.ImportType(element.SchemaTypeName);
     }
     if (this.IsDictionary(typeName, annotation))
     {
         ClassDataContract itemContract = dataContract.ItemContract as ClassDataContract;
         DataMember dataMember = null;
         DataMember member2 = null;
         if (((itemContract == null) || (itemContract.Members == null)) || (((itemContract.Members.Count != 2) || !(dataMember = itemContract.Members[0]).IsRequired) || !(member2 = itemContract.Members[1]).IsRequired))
         {
             ThrowArrayTypeCannotBeImportedException(typeName.Name, typeName.Namespace, System.Runtime.Serialization.SR.GetString("InvalidKeyValueType", new object[] { element.Name }));
         }
         if (itemContract.Namespace != dataContract.Namespace)
         {
             ThrowArrayTypeCannotBeImportedException(typeName.Name, typeName.Namespace, System.Runtime.Serialization.SR.GetString("InvalidKeyValueTypeNamespace", new object[] { element.Name, itemContract.Namespace }));
         }
         itemContract.IsValueType = true;
         dataContract.KeyName = dataMember.Name;
         dataContract.ValueName = member2.Name;
         if (element.SchemaType != null)
         {
             this.dataContractSet.Remove(itemContract.StableName);
             GenericInfo info = new GenericInfo(DataContract.GetStableName(Globals.TypeOfKeyValue), Globals.TypeOfKeyValue.FullName);
             info.Add(this.GetGenericInfoForDataMember(dataMember));
             info.Add(this.GetGenericInfoForDataMember(member2));
             info.AddToLevel(0, 2);
             dataContract.ItemContract.StableName = new XmlQualifiedName(info.GetExpandedStableName().Name, typeName.Namespace);
         }
     }
     return dataContract;
 }