GenericInfo ImportGenericInfo(XmlElement typeElement, XmlSchemaType type) { XmlNode nameAttribute = typeElement.Attributes.GetNamedItem(Globals.GenericNameAttribute); string name = (nameAttribute == null) ? null : nameAttribute.Value; if (name == null) throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.GenericAnnotationAttributeNotFound, type.Name, Globals.GenericNameAttribute))); XmlNode nsAttribute = typeElement.Attributes.GetNamedItem(Globals.GenericNamespaceAttribute); string ns = (nsAttribute == null) ? null : nsAttribute.Value; if (ns == null) throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.GenericAnnotationAttributeNotFound, type.Name, Globals.GenericNamespaceAttribute))); if (typeElement.ChildNodes.Count > 0) //Generic Type name = DataContract.EncodeLocalName(name); int currentLevel = 0; GenericInfo genInfo = new GenericInfo(new XmlQualifiedName(name, ns), type.Name); foreach (XmlNode childNode in typeElement.ChildNodes) { XmlElement argumentElement = childNode as XmlElement; if (argumentElement == null) continue; if (argumentElement.LocalName != Globals.GenericParameterLocalName || argumentElement.NamespaceURI != Globals.SerializationNamespace) throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.GenericAnnotationHasInvalidElement, argumentElement.LocalName, argumentElement.NamespaceURI, type.Name))); XmlNode nestedLevelAttribute = argumentElement.Attributes.GetNamedItem(Globals.GenericParameterNestedLevelAttribute); int argumentLevel = 0; if (nestedLevelAttribute != null) { if (!Int32.TryParse(nestedLevelAttribute.Value, out argumentLevel)) throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.GenericAnnotationHasInvalidAttributeValue, argumentElement.LocalName, argumentElement.NamespaceURI, type.Name, nestedLevelAttribute.Value, nestedLevelAttribute.LocalName, Globals.TypeOfInt.Name))); } if (argumentLevel < currentLevel) throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.GenericAnnotationForNestedLevelMustBeIncreasing, argumentElement.LocalName, argumentElement.NamespaceURI, type.Name))); genInfo.Add(ImportGenericInfo(argumentElement, type)); genInfo.AddToLevel(argumentLevel, 1); currentLevel = argumentLevel; } XmlNode typeNestedLevelsAttribute = typeElement.Attributes.GetNamedItem(Globals.GenericParameterNestedLevelAttribute); if (typeNestedLevelsAttribute != null) { int nestedLevels = 0; if (!Int32.TryParse(typeNestedLevelsAttribute.Value, out nestedLevels)) throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.GenericAnnotationHasInvalidAttributeValue, typeElement.LocalName, typeElement.NamespaceURI, type.Name, typeNestedLevelsAttribute.Value, typeNestedLevelsAttribute.LocalName, Globals.TypeOfInt.Name))); if ((nestedLevels - 1) > currentLevel) genInfo.AddToLevel(nestedLevels - 1, 0); } return genInfo; }
GenericInfo GetGenericInfoForDataMember(DataMember dataMember) { GenericInfo genericInfo = null; if (dataMember.MemberTypeContract.IsValueType && dataMember.IsNullable) { genericInfo = new GenericInfo(DataContract.GetStableName(Globals.TypeOfNullable), Globals.TypeOfNullable.FullName); genericInfo.Add(new GenericInfo(dataMember.MemberTypeContract.StableName, null)); } else { genericInfo = new GenericInfo(dataMember.MemberTypeContract.StableName, null); } return genericInfo; }
private GenericInfo ImportGenericInfo(XmlElement typeElement, XmlSchemaType type) { System.Xml.XmlNode namedItem = typeElement.Attributes.GetNamedItem("Name"); string localName = (namedItem == null) ? null : namedItem.Value; if (localName == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(System.Runtime.Serialization.SR.GetString("GenericAnnotationAttributeNotFound", new object[] { type.Name, "Name" }))); } System.Xml.XmlNode node2 = typeElement.Attributes.GetNamedItem("Namespace"); string ns = (node2 == null) ? null : node2.Value; if (ns == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(System.Runtime.Serialization.SR.GetString("GenericAnnotationAttributeNotFound", new object[] { type.Name, "Namespace" }))); } if (typeElement.ChildNodes.Count > 0) { localName = DataContract.EncodeLocalName(localName); } int num = 0; GenericInfo info = new GenericInfo(new XmlQualifiedName(localName, ns), type.Name); foreach (System.Xml.XmlNode node3 in typeElement.ChildNodes) { XmlElement element = node3 as XmlElement; if (element != null) { if ((element.LocalName != "GenericParameter") || (element.NamespaceURI != "http://schemas.microsoft.com/2003/10/Serialization/")) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(System.Runtime.Serialization.SR.GetString("GenericAnnotationHasInvalidElement", new object[] { element.LocalName, element.NamespaceURI, type.Name }))); } System.Xml.XmlNode node4 = element.Attributes.GetNamedItem("NestedLevel"); int result = 0; if ((node4 != null) && !int.TryParse(node4.Value, out result)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(System.Runtime.Serialization.SR.GetString("GenericAnnotationHasInvalidAttributeValue", new object[] { element.LocalName, element.NamespaceURI, type.Name, node4.Value, node4.LocalName, Globals.TypeOfInt.Name }))); } if (result < num) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(System.Runtime.Serialization.SR.GetString("GenericAnnotationForNestedLevelMustBeIncreasing", new object[] { element.LocalName, element.NamespaceURI, type.Name }))); } info.Add(this.ImportGenericInfo(element, type)); info.AddToLevel(result, 1); num = result; } } System.Xml.XmlNode node5 = typeElement.Attributes.GetNamedItem("NestedLevel"); if (node5 != null) { int num3 = 0; if (!int.TryParse(node5.Value, out num3)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(System.Runtime.Serialization.SR.GetString("GenericAnnotationHasInvalidAttributeValue", new object[] { typeElement.LocalName, typeElement.NamespaceURI, type.Name, node5.Value, node5.LocalName, Globals.TypeOfInt.Name }))); } if ((num3 - 1) > num) { info.AddToLevel(num3 - 1, 0); } } return info; }
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; }