private static bool ShouldInclude(ArrayMapping arrayMapping) { if (arrayMapping.ReferencedByElement) { return(false); } if (arrayMapping.Next != null) { return(false); } if (arrayMapping.Elements.Length == 1) { TypeKind kind = arrayMapping.Elements[0].Mapping.TypeDesc.Kind; if (kind == TypeKind.Node) { return(false); } } for (int i = 0; i < arrayMapping.Elements.Length; i++) { if (arrayMapping.Elements[i].Name != arrayMapping.Elements[i].Mapping.DefaultElementName) { // in the case we need custom attributes to serialize an array instance, we cannot include arrau mapping without explicit reference. return(false); } } return(true); }
internal void ExportRoot(StructMapping mapping, Type includeType) { if (!rootExported) { rootExported = true; ExportDerivedStructs(mapping); for (StructMapping derived = mapping.DerivedMappings; derived != null; derived = derived.NextDerivedMapping) { if (!derived.ReferencedByElement && derived.IncludeInSchema && !derived.IsAnonymousType) { CodeAttributeDeclaration include = new CodeAttributeDeclaration(includeType.FullName); include.Arguments.Add(new CodeAttributeArgument(new CodeTypeOfExpression(derived.TypeDesc.FullName))); includeMetadata.Add(include); } } Hashtable typesIncluded = new Hashtable(); foreach (TypeMapping m in Scope.TypeMappings) { if (m is ArrayMapping) { ArrayMapping arrayMapping = (ArrayMapping)m; if (ShouldInclude(arrayMapping) && !typesIncluded.Contains(arrayMapping.TypeDesc.FullName)) { CodeAttributeDeclaration include = new CodeAttributeDeclaration(includeType.FullName); include.Arguments.Add(new CodeAttributeArgument(new CodeTypeOfExpression(arrayMapping.TypeDesc.FullName))); includeMetadata.Add(include); typesIncluded.Add(arrayMapping.TypeDesc.FullName, string.Empty); EnsureTypesExported(arrayMapping.Elements, arrayMapping.Namespace); } } } } }
private static bool IsNeedNullableMember(ElementAccessor element) { if (element.Mapping is ArrayMapping) { ArrayMapping mapping = (ArrayMapping)element.Mapping; return(((mapping.Elements != null) && (mapping.Elements.Length == 1)) && IsNeedNullableMember(mapping.Elements[0])); } return(element.IsNullable && element.Mapping.TypeDesc.IsValueType); }
XmlQualifiedName ExportArrayMapping(ArrayMapping mapping, string ns) { // for the Rpc ArrayMapping different mappings could have the same schema type // we link all mappings corresponding to the same type together // loop through all mapping that will map to the same complexType, and export only one, // the obvious choice is the last one. while (mapping.Next != null) { mapping = mapping.Next; } XmlSchemaComplexType type = (XmlSchemaComplexType)types[mapping]; if (type == null) { CheckForDuplicateType(mapping.TypeName, mapping.Namespace); type = new XmlSchemaComplexType(); type.Name = mapping.TypeName; types.Add(mapping, type); // we need to add the type first, to make sure that the schema get created AddSchemaItem(type, mapping.Namespace, ns); AddSchemaImport(Soap.Encoding, mapping.Namespace); AddSchemaImport(Wsdl.Namespace, mapping.Namespace); XmlSchemaComplexContentRestriction restriction = new XmlSchemaComplexContentRestriction(); XmlQualifiedName qname = ExportTypeMapping(mapping.Elements[0].Mapping, mapping.Namespace); if (qname.IsEmpty) { // this is a root mapping qname = new XmlQualifiedName(Soap.UrType, XmlSchema.Namespace); } //<attribute ref="soapenc:arrayType" wsdl:arrayType="xsd:float[]"/> XmlSchemaAttribute attr = new XmlSchemaAttribute(); attr.RefName = ArrayTypeQName; XmlAttribute attribute = new XmlAttribute("wsdl", Wsdl.ArrayType, Wsdl.Namespace, Document); attribute.Value = qname.Namespace + ":" + qname.Name + "[]"; attr.UnhandledAttributes = new XmlAttribute[] { attribute }; restriction.Attributes.Add(attr); restriction.BaseTypeName = ArrayQName; XmlSchemaComplexContent model = new XmlSchemaComplexContent(); model.Content = restriction; type.ContentModel = model; if (qname.Namespace != XmlSchema.Namespace) { AddSchemaImport(qname.Namespace, mapping.Namespace); } } else { AddSchemaImport(mapping.Namespace, ns); } return(new XmlQualifiedName(mapping.TypeName, mapping.Namespace)); }
void SetArrayMappingType(ArrayMapping mapping) { bool useDefaultNs = false; string itemTypeName; string itemTypeNamespace; TypeMapping itemTypeMapping; if (mapping.Elements.Length == 1) { itemTypeMapping = mapping.Elements[0].Mapping; } else { itemTypeMapping = null; } if (itemTypeMapping is EnumMapping) { itemTypeNamespace = itemTypeMapping.Namespace; itemTypeName = itemTypeMapping.TypeName; } else if (itemTypeMapping is PrimitiveMapping) { itemTypeNamespace = itemTypeMapping.TypeDesc.IsXsdType ? XmlSchema.Namespace : UrtTypes.Namespace; itemTypeName = itemTypeMapping.TypeDesc.DataType.Name; useDefaultNs = true; } else if (itemTypeMapping is StructMapping) { if (itemTypeMapping.TypeDesc.IsRoot) { itemTypeNamespace = XmlSchema.Namespace; itemTypeName = Soap.UrType; useDefaultNs = true; } else { itemTypeNamespace = itemTypeMapping.Namespace; itemTypeName = itemTypeMapping.TypeName; } } else if (itemTypeMapping is ArrayMapping) { itemTypeNamespace = itemTypeMapping.Namespace; itemTypeName = itemTypeMapping.TypeName; } else { throw new InvalidOperationException(Res.GetString(Res.XmlInvalidSoapArray, mapping.TypeDesc.FullName)); } mapping.Namespace = useDefaultNs ? defaultNs : itemTypeNamespace; mapping.TypeName = "ArrayOf" + CodeIdentifier.MakePascal(itemTypeName); }
private bool WriteEnumAndArrayTypes(StructMapping structMapping, object o, string n, string ns, XmlMapping parentMapping) { if (o is Enum) { Writer.WriteStartElement(n, ns); EnumMapping enumMapping = null; Type enumType = o.GetType(); foreach (var m in parentMapping.Scope.TypeMappings) { var em = m as EnumMapping; if (em != null && em.TypeDesc.Type == enumType) { enumMapping = em; break; } } Debug.Assert(enumMapping != null); WriteXsiType(enumMapping.TypeName, ns); Writer.WriteString(WriteEnumMethod(enumMapping, o)); Writer.WriteEndElement(); return(true); } if (o is Array) { Debug.Assert(parentMapping != null); Writer.WriteStartElement(n, ns); ArrayMapping arrayMapping = null; Type arrayType = o.GetType(); foreach (var m in parentMapping.Scope.TypeMappings) { var am = m as ArrayMapping; if (am != null && am.TypeDesc.Type == arrayType) { arrayMapping = am; break; } } Debug.Assert(arrayMapping != null); WriteXsiType(arrayMapping.TypeName, ns); WriteMember(o, null, arrayMapping.ElementsSortedByDerivation, null, null, arrayMapping.TypeDesc, true); Writer.WriteEndElement(); return(true); } return(false); }
private void WriteArray(ref object o, ArrayMapping arrayMapping, bool readOnly, bool isNullable, string defaultNamespace) { if (arrayMapping.IsSoap) { throw new PlatformNotSupportedException("arrayMapping.IsSoap"); } else { if (!ReadNull()) { MemberMapping memberMapping = new MemberMapping(); memberMapping.Elements = arrayMapping.Elements; memberMapping.TypeDesc = arrayMapping.TypeDesc; memberMapping.ReadOnly = readOnly; Type collectionType = memberMapping.TypeDesc.Type; if (o == null) { o = ReflectionCreateObject(memberMapping.TypeDesc.Type); } if (memberMapping.ChoiceIdentifier != null) { // #10588: To Support ArrayMapping Types Having ChoiceIdentifier throw new NotImplementedException("memberMapping.ChoiceIdentifier != null"); } var collectionMember = new CollectionMember(); if ((readOnly && o == null) || Reader.IsEmptyElement) { Reader.Skip(); } else { Reader.ReadStartElement(); Reader.MoveToContent(); UnknownNodeAction unknownNode = UnknownNodeAction.ReadUnknownNode; while (Reader.NodeType != XmlNodeType.EndElement && Reader.NodeType != XmlNodeType.None) { Member temp; WriteMemberElements(ref o, collectionMember, out temp, new Member[] { new Member(memberMapping) }, unknownNode, unknownNode, null, null); Reader.MoveToContent(); } ReadEndElement(); } SetCollectionObjectWithCollectionMember(ref o, collectionMember, collectionType); } } }
XmlQualifiedName ExportArrayMapping(ArrayMapping mapping, string ns) { // some of the items in the linked list differ only by CLR type. We don't need to // export different schema types for these. Look further down the list for another // entry with the same elements. If there is one, it will be exported later so // just return its name now. ArrayMapping currentMapping = mapping; while (currentMapping.Next != null) { currentMapping = currentMapping.Next; } XmlSchemaComplexType type = (XmlSchemaComplexType)types[currentMapping]; if (type == null) { CheckForDuplicateType(currentMapping.TypeName, currentMapping.Namespace); type = new XmlSchemaComplexType(); type.Name = mapping.TypeName; types.Add(currentMapping, type); AddSchemaItem(type, mapping.Namespace, ns); XmlSchemaSequence seq = new XmlSchemaSequence(); ExportElementAccessors(seq, mapping.Elements, true, false, mapping.Namespace); if (seq.Items.Count > 0) { #if DEBUG // we can have only one item for the array mapping if (seq.Items.Count != 1) { throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Type " + mapping.TypeName + " from namespace '" + ns + "' is an invalid array mapping")); } #endif if (seq.Items[0] is XmlSchemaChoice) { type.Particle = (XmlSchemaChoice)seq.Items[0]; } else { type.Particle = seq; } } } else { AddSchemaImport(mapping.Namespace, ns); } return(new XmlQualifiedName(type.Name, mapping.Namespace)); }
static bool IsNeedNullableMember(ElementAccessor element) { if (element.Mapping is ArrayMapping) { ArrayMapping arrayMapping = (ArrayMapping)element.Mapping; if (arrayMapping.Elements != null && arrayMapping.Elements.Length == 1) { return(IsNeedNullableMember(arrayMapping.Elements[0])); } return(false); } else { return(element.IsNullable && element.Mapping.TypeDesc.IsValueType); } }
private XmlQualifiedName ExportArrayMapping(ArrayMapping mapping, string ns) { while (mapping.Next != null) { mapping = mapping.Next; } if (((XmlSchemaComplexType)this.types[mapping]) == null) { this.CheckForDuplicateType(mapping.TypeName, mapping.Namespace); XmlSchemaComplexType type = new XmlSchemaComplexType { Name = mapping.TypeName }; this.types.Add(mapping, type); this.AddSchemaItem(type, mapping.Namespace, ns); this.AddSchemaImport("http://schemas.xmlsoap.org/soap/encoding/", mapping.Namespace); this.AddSchemaImport("http://schemas.xmlsoap.org/wsdl/", mapping.Namespace); XmlSchemaComplexContentRestriction restriction = new XmlSchemaComplexContentRestriction(); XmlQualifiedName name = this.ExportTypeMapping(mapping.Elements[0].Mapping, mapping.Namespace); if (name.IsEmpty) { name = new XmlQualifiedName("anyType", "http://www.w3.org/2001/XMLSchema"); } XmlSchemaAttribute item = new XmlSchemaAttribute { RefName = ArrayTypeQName }; XmlAttribute attribute2 = new XmlAttribute("wsdl", "arrayType", "http://schemas.xmlsoap.org/wsdl/", this.Document) { Value = name.Namespace + ":" + name.Name + "[]" }; item.UnhandledAttributes = new XmlAttribute[] { attribute2 }; restriction.Attributes.Add(item); restriction.BaseTypeName = ArrayQName; XmlSchemaComplexContent content = new XmlSchemaComplexContent { Content = restriction }; type.ContentModel = content; if (name.Namespace != "http://www.w3.org/2001/XMLSchema") { this.AddSchemaImport(name.Namespace, mapping.Namespace); } } else { this.AddSchemaImport(mapping.Namespace, ns); } return(new XmlQualifiedName(mapping.TypeName, mapping.Namespace)); }
private ArrayMapping ImportArrayLikeMapping(ArrayModel model, RecursionLimiter limiter) { ArrayMapping mapping = new ArrayMapping(); mapping.IsSoap = true; TypeMapping itemTypeMapping = ImportTypeMapping(model.Element, limiter); if (itemTypeMapping.TypeDesc.IsValueType && !itemTypeMapping.TypeDesc.IsPrimitive && !itemTypeMapping.TypeDesc.IsEnum) { throw new NotSupportedException(SR.Format(SR.XmlRpcArrayOfValueTypes, model.TypeDesc.FullName)); } mapping.TypeDesc = model.TypeDesc; mapping.Elements = new ElementAccessor[] { CreateElementAccessor(itemTypeMapping, mapping.Namespace) }; SetArrayMappingType(mapping); // in the case of an ArrayMapping we can have more that one mapping correspond to a type // examples of that are ArrayList and object[] both will map tp ArrayOfur-type // so we create a link list for all mappings of the same XSD type ArrayMapping existingMapping = (ArrayMapping)_types[mapping.TypeName, mapping.Namespace]; if (existingMapping != null) { ArrayMapping first = existingMapping; while (existingMapping != null) { if (existingMapping.TypeDesc == model.TypeDesc) { return(existingMapping); } existingMapping = existingMapping.Next; } mapping.Next = first; _types[mapping.TypeName, mapping.Namespace] = mapping; return(mapping); } _typeScope.AddTypeMapping(mapping); _types.Add(mapping.TypeName, mapping.Namespace, mapping); IncludeTypes(model.Type); return(mapping); }
private static bool ShouldInclude(ArrayMapping arrayMapping) { if (arrayMapping.ReferencedByElement) { return(false); } if (arrayMapping.Next != null) { return(false); } if (arrayMapping.Elements.Length == 1) { TypeKind kind = arrayMapping.Elements[0].Mapping.TypeDesc.Kind; if (kind == TypeKind.Node) { return(false); } } return(true); }
private ElementAccessor ImportArray(XmlSchemaElement element, string ns) { if (element.SchemaType == null) { return(null); } if (!element.IsMultipleOccurrence) { return(null); } XmlSchemaType schemaType = element.SchemaType; ArrayMapping mapping = this.ImportArrayMapping(schemaType, ns); if (mapping == null) { return(null); } return(new ElementAccessor { IsSoap = true, Name = element.Name, Namespace = ns, Mapping = mapping, IsNullable = false, Form = XmlSchemaForm.None }); }
void ExportArrayElements(CodeAttributeDeclarationCollection metadata, ArrayMapping array, string ns, TypeDesc elementTypeDesc, int nestingLevel) { for (int i = 0; i < array.Elements.Length; i++) { ElementAccessor arrayElement = array.Elements[i]; TypeMapping elementMapping = arrayElement.Mapping; string elementName = Accessor.UnescapeName(arrayElement.Name); bool sameName = elementName == arrayElement.Mapping.TypeName; bool sameElementType = elementMapping.TypeDesc == elementTypeDesc; bool sameElementNs = arrayElement.Namespace == ns; bool sameNullable = arrayElement.IsNullable == elementMapping.TypeDesc.IsNullable; if (!sameName || !sameElementType || !sameElementNs || !sameNullable || arrayElement.Form != XmlSchemaForm.Qualified || nestingLevel > 0) { ExportArrayItem(metadata, sameName ? null : elementName, sameElementNs ? null : arrayElement.Namespace, sameElementType ? null : elementMapping.TypeDesc, elementMapping.TypeDesc, arrayElement.IsNullable, arrayElement.Form, nestingLevel); } if (elementMapping is ArrayMapping) { ExportArrayElements(metadata, (ArrayMapping)elementMapping, ns, elementTypeDesc.ArrayElementTypeDesc, nestingLevel + 1); } } }
private static bool ShouldInclude(ArrayMapping arrayMapping) { if (arrayMapping.ReferencedByElement) { return(false); } if (arrayMapping.Next != null) { return(false); } if ((arrayMapping.Elements.Length == 1) && (arrayMapping.Elements[0].Mapping.TypeDesc.Kind == TypeKind.Node)) { return(false); } for (int i = 0; i < arrayMapping.Elements.Length; i++) { if (arrayMapping.Elements[i].Name != arrayMapping.Elements[i].Mapping.DefaultElementName) { return(false); } } return(true); }
private ArrayMapping ImportArrayLikeMapping(ArrayModel model, RecursionLimiter limiter) { ArrayMapping mapping = new ArrayMapping { IsSoap = true }; TypeMapping mapping2 = this.ImportTypeMapping(model.Element, limiter); if ((mapping2.TypeDesc.IsValueType && !mapping2.TypeDesc.IsPrimitive) && !mapping2.TypeDesc.IsEnum) { throw new NotSupportedException(Res.GetString("XmlRpcArrayOfValueTypes", new object[] { model.TypeDesc.FullName })); } mapping.TypeDesc = model.TypeDesc; mapping.Elements = new ElementAccessor[] { CreateElementAccessor(mapping2, mapping.Namespace) }; this.SetArrayMappingType(mapping); ArrayMapping next = (ArrayMapping)this.types[mapping.TypeName, mapping.Namespace]; if (next != null) { ArrayMapping mapping4 = next; while (next != null) { if (next.TypeDesc == model.TypeDesc) { return(next); } next = next.Next; } mapping.Next = mapping4; this.types[mapping.TypeName, mapping.Namespace] = mapping; return(mapping); } this.typeScope.AddTypeMapping(mapping); this.types.Add(mapping.TypeName, mapping.Namespace, mapping); this.IncludeTypes(model.Type); return(mapping); }
void CreateArrayElementsFromAttributes(ArrayMapping arrayMapping, XmlArrayItemAttributes attributes, Type arrayElementType, string arrayElementNs) { NameTable arrayItemElements = new NameTable(); // xmlelementname + xmlns -> ElementAccessor for (int i = 0; attributes != null && i < attributes.Count; i++) { XmlArrayItemAttribute xmlArrayItem = attributes[i]; if (xmlArrayItem.NestingLevel != arrayNestingLevel) continue; Type targetType = xmlArrayItem.Type != null ? xmlArrayItem.Type : arrayElementType; TypeDesc targetTypeDesc = typeScope.GetTypeDesc(targetType); ElementAccessor arrayItemElement = new ElementAccessor(); arrayItemElement.Namespace = xmlArrayItem.Namespace == null ? arrayElementNs : xmlArrayItem.Namespace; arrayItemElement.Mapping = ImportTypeMapping(modelScope.GetTypeModel(targetType), arrayItemElement.Namespace, ImportContext.Element, xmlArrayItem.DataType, null); arrayItemElement.Name = xmlArrayItem.ElementName.Length == 0 ? arrayItemElement.Mapping.DefaultElementName : XmlConvert.EncodeLocalName(xmlArrayItem.ElementName); arrayItemElement.IsNullable = xmlArrayItem.IsNullableSpecified ? xmlArrayItem.IsNullable : targetTypeDesc.IsNullable || targetTypeDesc.IsOptionalValue; arrayItemElement.Form = xmlArrayItem.Form == XmlSchemaForm.None ? XmlSchemaForm.Qualified : xmlArrayItem.Form; CheckForm(arrayItemElement.Form, arrayElementNs != arrayItemElement.Namespace); CheckNullable(arrayItemElement.IsNullable, targetTypeDesc, arrayItemElement.Mapping); AddUniqueAccessor(arrayItemElements, arrayItemElement); } arrayMapping.Elements = (ElementAccessor[])arrayItemElements.ToArray(typeof(ElementAccessor)); }
void WriteArray(string source, string arrayName, ArrayMapping arrayMapping, bool readOnly, bool isNullable, int fixupIndex) { if (arrayMapping.IsSoap) { Writer.Write("object rre = "); Writer.Write(fixupIndex >= 0 ? "ReadReferencingElement" : "ReadReferencedElement"); Writer.Write("("); WriteID(arrayMapping.TypeName); Writer.Write(", "); WriteID(arrayMapping.Namespace); if (fixupIndex >= 0) { Writer.Write(", "); Writer.Write("out fixup.Ids["); Writer.Write((fixupIndex).ToString(CultureInfo.InvariantCulture)); Writer.Write("]"); } Writer.WriteLine(");"); TypeDesc td = arrayMapping.TypeDesc; if (td.IsEnumerable || td.IsCollection) { Writer.WriteLine("if (rre != null) {"); Writer.Indent++; WriteAddCollectionFixup(td, readOnly, source, "rre"); Writer.Indent--; Writer.WriteLine("}"); } else { Writer.WriteLine("try {"); Writer.Indent++; WriteSourceBeginTyped(source, arrayMapping.TypeDesc); Writer.Write("rre"); WriteSourceEnd(source); Writer.WriteLine(";"); WriteCatchCastException(arrayMapping.TypeDesc, "rre", null); } } else { Writer.WriteLine("if (!ReadNull()) {"); Writer.Indent++; MemberMapping memberMapping = new MemberMapping(); memberMapping.Elements = arrayMapping.Elements; memberMapping.TypeDesc = arrayMapping.TypeDesc; memberMapping.ReadOnly = readOnly; Member member = new Member(this, source, arrayName, 0, memberMapping, false); member.IsNullable = false;//Note, [....]: IsNullable is set to false since null condition (xsi:nil) is already handled by 'ReadNull()' Member[] members = new Member[] { member }; WriteMemberBegin(members); if (readOnly) { Writer.Write("if (((object)("); Writer.Write(member.ArrayName); Writer.Write(") == null) || "); } else { Writer.Write("if ("); } Writer.WriteLine("(Reader.IsEmptyElement)) {"); Writer.Indent++; Writer.WriteLine("Reader.Skip();"); Writer.Indent--; Writer.WriteLine("}"); Writer.WriteLine("else {"); Writer.Indent++; Writer.WriteLine("Reader.ReadStartElement();"); int loopIndex = WriteWhileNotLoopStart(); Writer.Indent++; string unknownNode = "UnknownNode(null, " + ExpectedElements(members) + ");"; WriteMemberElements(members, unknownNode, unknownNode, null, null, null); Writer.WriteLine("Reader.MoveToContent();"); WriteWhileLoopEnd(loopIndex); Writer.Indent--; Writer.WriteLine("ReadEndElement();"); Writer.WriteLine("}"); WriteMemberEnd(members, false); Writer.Indent--; Writer.WriteLine("}"); if (isNullable) { Writer.WriteLine("else {"); Writer.Indent++; member.IsNullable = true; WriteMemberBegin(members); WriteMemberEnd(members); Writer.Indent--; Writer.WriteLine("}"); } } }
private ArrayMapping ImportArrayMapping(XmlSchemaType type, string ns) { ArrayMapping mapping; if ((type.Name == "Array") && (ns == "http://schemas.xmlsoap.org/soap/encoding/")) { mapping = new ArrayMapping(); TypeMapping rootMapping = base.GetRootMapping(); ElementAccessor accessor = new ElementAccessor { IsSoap = true, Name = "anyType", Namespace = ns, Mapping = rootMapping, IsNullable = true, Form = XmlSchemaForm.None }; mapping.Elements = new ElementAccessor[] { accessor }; mapping.TypeDesc = accessor.Mapping.TypeDesc.CreateArrayTypeDesc(); mapping.TypeName = "ArrayOf" + CodeIdentifier.MakePascal(accessor.Mapping.TypeName); return(mapping); } if ((type.DerivedFrom.Name != "Array") || (type.DerivedFrom.Namespace != "http://schemas.xmlsoap.org/soap/encoding/")) { return(null); } XmlSchemaContentModel contentModel = ((XmlSchemaComplexType)type).ContentModel; if (!(contentModel.Content is XmlSchemaComplexContentRestriction)) { return(null); } mapping = new ArrayMapping(); XmlSchemaComplexContentRestriction content = (XmlSchemaComplexContentRestriction)contentModel.Content; for (int i = 0; i < content.Attributes.Count; i++) { XmlSchemaAttribute parent = content.Attributes[i] as XmlSchemaAttribute; if (((parent != null) && (parent.RefName.Name == "arrayType")) && (parent.RefName.Namespace == "http://schemas.xmlsoap.org/soap/encoding/")) { string str = null; if (parent.UnhandledAttributes != null) { foreach (XmlAttribute attribute2 in parent.UnhandledAttributes) { if ((attribute2.LocalName == "arrayType") && (attribute2.NamespaceURI == "http://schemas.xmlsoap.org/wsdl/")) { str = attribute2.Value; break; } } } if (str != null) { string str2; TypeMapping mapping3; XmlQualifiedName name = TypeScope.ParseWsdlArrayType(str, out str2, parent); TypeDesc typeDesc = base.Scope.GetTypeDesc(name.Name, name.Namespace); if ((typeDesc != null) && typeDesc.IsPrimitive) { mapping3 = new PrimitiveMapping { TypeDesc = typeDesc, TypeName = typeDesc.DataType.Name }; } else { mapping3 = this.ImportType(name, false); } ElementAccessor accessor2 = new ElementAccessor { IsSoap = true, Name = name.Name, Namespace = ns, Mapping = mapping3, IsNullable = true, Form = XmlSchemaForm.None }; mapping.Elements = new ElementAccessor[] { accessor2 }; mapping.TypeDesc = accessor2.Mapping.TypeDesc.CreateArrayTypeDesc(); mapping.TypeName = "ArrayOf" + CodeIdentifier.MakePascal(accessor2.Mapping.TypeName); return(mapping); } } } XmlSchemaParticle particle = content.Particle; if (!(particle is XmlSchemaAll) && !(particle is XmlSchemaSequence)) { return(null); } XmlSchemaGroupBase base2 = (XmlSchemaGroupBase)particle; if ((base2.Items.Count != 1) || !(base2.Items[0] is XmlSchemaElement)) { return(null); } XmlSchemaElement element = (XmlSchemaElement)base2.Items[0]; if (!element.IsMultipleOccurrence) { return(null); } ElementAccessor accessor3 = this.ImportElement(element, ns); mapping.Elements = new ElementAccessor[] { accessor3 }; mapping.TypeDesc = accessor3.Mapping.TypeDesc.CreateArrayTypeDesc(); return(mapping); }
private ArrayMapping ImportArrayMapping(XmlSchemaType type, string identifier, string ns, bool repeats) { if (!(type is XmlSchemaComplexType)) { return null; } if (!type.DerivedFrom.IsEmpty) { return null; } if (IsMixed(type)) { return null; } Mapping mapping = (Mapping) base.ImportedMappings[type]; if (mapping != null) { if (mapping is ArrayMapping) { return (ArrayMapping) mapping; } return null; } TypeItems typeItems = this.GetTypeItems(type); if ((typeItems.Attributes != null) && (typeItems.Attributes.Count > 0)) { return null; } if (typeItems.AnyAttribute != null) { return null; } if (typeItems.Particle == null) { return null; } XmlSchemaGroupBase particle = typeItems.Particle; ArrayMapping typeMapping = new ArrayMapping { TypeName = identifier, Namespace = ns }; if (particle is XmlSchemaChoice) { XmlSchemaChoice group = (XmlSchemaChoice) particle; if (!group.IsMultipleOccurrence) { return null; } bool needExplicitOrder = false; MemberMapping mapping3 = this.ImportChoiceGroup(group, identifier, null, null, null, ns, true, ref needExplicitOrder, false); if (mapping3.ChoiceIdentifier != null) { return null; } typeMapping.TypeDesc = mapping3.TypeDesc; typeMapping.Elements = mapping3.Elements; typeMapping.TypeName = ((type.Name == null) || (type.Name.Length == 0)) ? ("ArrayOf" + CodeIdentifier.MakePascal(typeMapping.TypeDesc.Name)) : type.Name; } else { if (!(particle is XmlSchemaAll) && !(particle is XmlSchemaSequence)) { return null; } if ((particle.Items.Count != 1) || !(particle.Items[0] is XmlSchemaElement)) { return null; } XmlSchemaElement element = (XmlSchemaElement) particle.Items[0]; if (!element.IsMultipleOccurrence) { return null; } List<string> identifiers = new List<string>(1) { identifier }; if (this.IsCyclicReferencedType(element, identifiers)) { return null; } ElementAccessor accessor = this.ImportElement(element, identifier, typeof(TypeMapping), null, ns, false); if (accessor.Any) { return null; } typeMapping.Elements = new ElementAccessor[] { accessor }; typeMapping.TypeDesc = accessor.Mapping.TypeDesc.CreateArrayTypeDesc(); typeMapping.TypeName = ((type.Name == null) || (type.Name.Length == 0)) ? ("ArrayOf" + CodeIdentifier.MakePascal(accessor.Mapping.TypeDesc.Name)) : type.Name; } base.ImportedMappings[type] = typeMapping; base.Scope.AddTypeMapping(typeMapping); typeMapping.TopLevelMapping = this.ImportStructType(type, ns, identifier, null, true); typeMapping.TopLevelMapping.ReferencedByTopLevelElement = true; if ((type.Name != null) && (type.Name.Length != 0)) { this.ImportDerivedTypes(new XmlQualifiedName(identifier, ns)); } return typeMapping; }
private ArrayMapping ImportArrayMapping(XmlSchemaType type, string ns) { ArrayMapping mapping; if ((type.Name == "Array") && (ns == "http://schemas.xmlsoap.org/soap/encoding/")) { mapping = new ArrayMapping(); TypeMapping rootMapping = base.GetRootMapping(); ElementAccessor accessor = new ElementAccessor { IsSoap = true, Name = "anyType", Namespace = ns, Mapping = rootMapping, IsNullable = true, Form = XmlSchemaForm.None }; mapping.Elements = new ElementAccessor[] { accessor }; mapping.TypeDesc = accessor.Mapping.TypeDesc.CreateArrayTypeDesc(); mapping.TypeName = "ArrayOf" + CodeIdentifier.MakePascal(accessor.Mapping.TypeName); return mapping; } if ((type.DerivedFrom.Name != "Array") || (type.DerivedFrom.Namespace != "http://schemas.xmlsoap.org/soap/encoding/")) { return null; } XmlSchemaContentModel contentModel = ((XmlSchemaComplexType) type).ContentModel; if (!(contentModel.Content is XmlSchemaComplexContentRestriction)) { return null; } mapping = new ArrayMapping(); XmlSchemaComplexContentRestriction content = (XmlSchemaComplexContentRestriction) contentModel.Content; for (int i = 0; i < content.Attributes.Count; i++) { XmlSchemaAttribute parent = content.Attributes[i] as XmlSchemaAttribute; if (((parent != null) && (parent.RefName.Name == "arrayType")) && (parent.RefName.Namespace == "http://schemas.xmlsoap.org/soap/encoding/")) { string str = null; if (parent.UnhandledAttributes != null) { foreach (XmlAttribute attribute2 in parent.UnhandledAttributes) { if ((attribute2.LocalName == "arrayType") && (attribute2.NamespaceURI == "http://schemas.xmlsoap.org/wsdl/")) { str = attribute2.Value; break; } } } if (str != null) { string str2; TypeMapping mapping3; XmlQualifiedName name = TypeScope.ParseWsdlArrayType(str, out str2, parent); TypeDesc typeDesc = base.Scope.GetTypeDesc(name.Name, name.Namespace); if ((typeDesc != null) && typeDesc.IsPrimitive) { mapping3 = new PrimitiveMapping { TypeDesc = typeDesc, TypeName = typeDesc.DataType.Name }; } else { mapping3 = this.ImportType(name, false); } ElementAccessor accessor2 = new ElementAccessor { IsSoap = true, Name = name.Name, Namespace = ns, Mapping = mapping3, IsNullable = true, Form = XmlSchemaForm.None }; mapping.Elements = new ElementAccessor[] { accessor2 }; mapping.TypeDesc = accessor2.Mapping.TypeDesc.CreateArrayTypeDesc(); mapping.TypeName = "ArrayOf" + CodeIdentifier.MakePascal(accessor2.Mapping.TypeName); return mapping; } } } XmlSchemaParticle particle = content.Particle; if (!(particle is XmlSchemaAll) && !(particle is XmlSchemaSequence)) { return null; } XmlSchemaGroupBase base2 = (XmlSchemaGroupBase) particle; if ((base2.Items.Count != 1) || !(base2.Items[0] is XmlSchemaElement)) { return null; } XmlSchemaElement element = (XmlSchemaElement) base2.Items[0]; if (!element.IsMultipleOccurrence) { return null; } ElementAccessor accessor3 = this.ImportElement(element, ns); mapping.Elements = new ElementAccessor[] { accessor3 }; mapping.TypeDesc = accessor3.Mapping.TypeDesc.CreateArrayTypeDesc(); return mapping; }
private void AddMemberMetadata(CodeMemberField field, CodeAttributeDeclarationCollection metadata, MemberMapping member, string ns, bool forceUseMemberName, CodeCommentStatementCollection comments, CodeConstructor ctor) { if (member.Xmlns != null) { CodeAttributeDeclaration declaration = new CodeAttributeDeclaration(typeof(XmlNamespaceDeclarationsAttribute).FullName); metadata.Add(declaration); } else if (member.Attribute != null) { AttributeAccessor attribute = member.Attribute; if (attribute.Any) { this.ExportAnyAttribute(metadata); } else { TypeMapping mapping = attribute.Mapping; string str = Accessor.UnescapeName(attribute.Name); bool flag = (mapping.TypeDesc == member.TypeDesc) || (member.TypeDesc.IsArrayLike && (mapping.TypeDesc == member.TypeDesc.ArrayElementTypeDesc)); bool flag2 = (str == member.Name) && !forceUseMemberName; bool flag3 = attribute.Namespace == ns; bool flag4 = attribute.Form != XmlSchemaForm.Qualified; this.ExportAttribute(metadata, flag2 ? null : str, (flag3 || flag4) ? null : attribute.Namespace, flag ? null : mapping.TypeDesc, mapping.TypeDesc, flag4 ? XmlSchemaForm.None : attribute.Form); this.AddDefaultValueAttribute(field, metadata, attribute.Default, mapping, comments, member.TypeDesc, attribute, ctor); } } else { if (member.Text != null) { TypeMapping mapping2 = member.Text.Mapping; bool flag5 = (mapping2.TypeDesc == member.TypeDesc) || (member.TypeDesc.IsArrayLike && (mapping2.TypeDesc == member.TypeDesc.ArrayElementTypeDesc)); this.ExportText(metadata, flag5 ? null : mapping2.TypeDesc, mapping2.TypeDesc.IsAmbiguousDataType ? mapping2.TypeDesc.DataType.Name : null); } if (member.Elements.Length == 1) { ElementAccessor accessor = member.Elements[0]; TypeMapping mapping3 = accessor.Mapping; string name = Accessor.UnescapeName(accessor.Name); bool flag6 = (name == member.Name) && !forceUseMemberName; bool flag7 = mapping3 is ArrayMapping; bool flag8 = accessor.Namespace == ns; bool flag9 = accessor.Form != XmlSchemaForm.Unqualified; if (accessor.Any) { this.ExportAnyElement(metadata, name, accessor.Namespace, member.SequenceId); } else if (flag7) { TypeDesc typeDesc = mapping3.TypeDesc; TypeDesc desc2 = member.TypeDesc; ArrayMapping array = (ArrayMapping)mapping3; if ((!flag6 || !flag8) || ((accessor.IsNullable || !flag9) || (member.SequenceId != -1))) { this.ExportArray(metadata, flag6 ? null : name, flag8 ? null : accessor.Namespace, accessor.IsNullable, flag9 ? XmlSchemaForm.None : accessor.Form, member.SequenceId); } else if (mapping3.TypeDesc.ArrayElementTypeDesc == new TypeScope().GetTypeDesc(typeof(byte))) { this.ExportArray(metadata, null, null, false, XmlSchemaForm.None, member.SequenceId); } this.ExportArrayElements(metadata, array, accessor.Namespace, member.TypeDesc.ArrayElementTypeDesc, 0); } else { bool flag10 = (mapping3.TypeDesc == member.TypeDesc) || (member.TypeDesc.IsArrayLike && (mapping3.TypeDesc == member.TypeDesc.ArrayElementTypeDesc)); if (member.TypeDesc.IsArrayLike) { flag6 = false; } this.ExportElement(metadata, flag6 ? null : name, flag8 ? null : accessor.Namespace, flag10 ? null : mapping3.TypeDesc, mapping3.TypeDesc, accessor.IsNullable, flag9 ? XmlSchemaForm.None : accessor.Form, member.SequenceId); } this.AddDefaultValueAttribute(field, metadata, accessor.Default, mapping3, comments, member.TypeDesc, accessor, ctor); } else { for (int i = 0; i < member.Elements.Length; i++) { ElementAccessor accessor3 = member.Elements[i]; string str3 = Accessor.UnescapeName(accessor3.Name); bool flag11 = accessor3.Namespace == ns; if (accessor3.Any) { this.ExportAnyElement(metadata, str3, accessor3.Namespace, member.SequenceId); } else { bool flag12 = accessor3.Form != XmlSchemaForm.Unqualified; this.ExportElement(metadata, str3, flag11 ? null : accessor3.Namespace, accessor3.Mapping.TypeDesc, accessor3.Mapping.TypeDesc, accessor3.IsNullable, flag12 ? XmlSchemaForm.None : accessor3.Form, member.SequenceId); } } } if (member.ChoiceIdentifier != null) { CodeAttributeDeclaration declaration2 = new CodeAttributeDeclaration(typeof(XmlChoiceIdentifierAttribute).FullName); declaration2.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(member.ChoiceIdentifier.MemberName))); metadata.Add(declaration2); } if (member.Ignore) { CodeAttributeDeclaration declaration3 = new CodeAttributeDeclaration(typeof(XmlIgnoreAttribute).FullName); metadata.Add(declaration3); } } }
private void SetArrayMappingType(ArrayMapping mapping) { string typeName; string str2; TypeMapping mapping2; bool flag = false; if (mapping.Elements.Length == 1) { mapping2 = mapping.Elements[0].Mapping; } else { mapping2 = null; } if (mapping2 is EnumMapping) { str2 = mapping2.Namespace; typeName = mapping2.TypeName; } else if (mapping2 is PrimitiveMapping) { str2 = mapping2.TypeDesc.IsXsdType ? "http://www.w3.org/2001/XMLSchema" : "http://microsoft.com/wsdl/types/"; typeName = mapping2.TypeDesc.DataType.Name; flag = true; } else if (mapping2 is StructMapping) { if (mapping2.TypeDesc.IsRoot) { str2 = "http://www.w3.org/2001/XMLSchema"; typeName = "anyType"; flag = true; } else { str2 = mapping2.Namespace; typeName = mapping2.TypeName; } } else { if (!(mapping2 is ArrayMapping)) { throw new InvalidOperationException(Res.GetString("XmlInvalidSoapArray", new object[] { mapping.TypeDesc.FullName })); } str2 = mapping2.Namespace; typeName = mapping2.TypeName; } typeName = CodeIdentifier.MakePascal(typeName); string str3 = "ArrayOf" + typeName; string str4 = flag ? this.defaultNs : str2; int num = 1; TypeMapping mapping3 = (TypeMapping)this.types[str3, str4]; while (mapping3 != null) { if (mapping3 is ArrayMapping) { ArrayMapping mapping4 = (ArrayMapping)mapping3; if (AccessorMapping.ElementsMatch(mapping4.Elements, mapping.Elements)) { break; } } str3 = typeName + num.ToString(CultureInfo.InvariantCulture); mapping3 = (TypeMapping)this.types[str3, str4]; num++; } mapping.Namespace = str4; mapping.TypeName = str3; }
private bool IsNeedXmlSerializationAttributes(ArrayMapping arrayMapping) { if (arrayMapping.Elements.Length != 1) return true; ElementAccessor item = arrayMapping.Elements[0]; TypeMapping itemMapping = item.Mapping; if (item.Name != itemMapping.DefaultElementName) return true; if (item.Form != XmlSchemaForm.None && item.Form != XmlSchemaExporter.elementFormDefault) return true; if (item.Mapping.TypeDesc != null) { if (item.IsNullable != item.Mapping.TypeDesc.IsNullable) return true; if (item.Mapping.TypeDesc.IsAmbiguousDataType) return true; } return false; }
void AddMemberMetadata(CodeMemberField field, CodeAttributeDeclarationCollection metadata, MemberMapping member, string ns, bool forceUseMemberName) { if (member.Xmlns != null) { CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(typeof(XmlNamespaceDeclarationsAttribute).FullName); metadata.Add(attribute); } else if (member.Attribute != null) { AttributeAccessor attribute = member.Attribute; if (attribute.Any) { ExportAnyAttribute(metadata); } else { TypeMapping mapping = (TypeMapping)attribute.Mapping; string attrName = Accessor.UnescapeName(attribute.Name); bool sameType = mapping.TypeDesc == member.TypeDesc || (member.TypeDesc.IsArrayLike && mapping.TypeDesc == member.TypeDesc.ArrayElementTypeDesc); bool sameName = attrName == member.Name && !forceUseMemberName; bool sameNs = attribute.Namespace == ns; bool defaultForm = attribute.Form != XmlSchemaForm.Qualified; ExportAttribute(metadata, sameName ? null : attrName, sameNs ? null : attribute.Namespace, sameType ? null : mapping.TypeDesc, mapping.TypeDesc, defaultForm ? XmlSchemaForm.None : attribute.Form); if (attribute.HasDefault) { AddDefaultValueAttribute(field, metadata, attribute.Default, mapping); } } } else { if (member.Text != null) { TypeMapping mapping = (TypeMapping)member.Text.Mapping; bool sameType = mapping.TypeDesc == member.TypeDesc || (member.TypeDesc.IsArrayLike && mapping.TypeDesc == member.TypeDesc.ArrayElementTypeDesc); ExportText(metadata, sameType ? null : mapping.TypeDesc, mapping.TypeDesc.IsAmbiguousDataType ? mapping.TypeDesc.DataType.Name : null); } if (member.Elements.Length == 1) { ElementAccessor element = member.Elements[0]; TypeMapping mapping = (TypeMapping)element.Mapping; string elemName = Accessor.UnescapeName(element.Name); bool sameName = ((elemName == member.Name) && !forceUseMemberName); bool isArray = mapping is ArrayMapping; bool sameNs = element.Namespace == ns; bool defaultForm = element.Form != XmlSchemaForm.Unqualified; if (element.Any) { ExportAnyElement(metadata, elemName, sameNs ? null : element.Namespace); } else if (isArray) { bool sameType = mapping.TypeDesc == member.TypeDesc; ArrayMapping array = (ArrayMapping)mapping; if (!sameName || !sameNs || element.IsNullable || !defaultForm) { ExportArray(metadata, sameName ? null : elemName, sameNs ? null : element.Namespace, element.IsNullable, defaultForm ? XmlSchemaForm.None : element.Form); } ExportArrayElements(metadata, array, ns, member.TypeDesc.ArrayElementTypeDesc, 0); } else { bool sameType = mapping.TypeDesc == member.TypeDesc || (member.TypeDesc.IsArrayLike && mapping.TypeDesc == member.TypeDesc.ArrayElementTypeDesc); if (member.TypeDesc.IsArrayLike) { sameName = false; } if (member.TypeDesc.IsAmbiguousDataType || member.TypeDesc.IsArrayLike || !sameName || !sameType || !sameNs || element.IsNullable || !defaultForm) { ExportElement(metadata, sameName ? null : elemName, sameNs ? null : element.Namespace, sameType ? null : mapping.TypeDesc, mapping.TypeDesc, element.IsNullable, defaultForm ? XmlSchemaForm.None : element.Form); } } if (element.HasDefault) { AddDefaultValueAttribute(field, metadata, element.Default, mapping); } } else { for (int i = 0; i < member.Elements.Length; i++) { ElementAccessor element = member.Elements[i]; string elemName = Accessor.UnescapeName(element.Name); bool sameNs = element.Namespace == ns; if (element.Any) { ExportAnyElement(metadata, elemName, sameNs ? null : element.Namespace); } else { bool defaultForm = element.Form != XmlSchemaForm.Unqualified; ExportElement(metadata, elemName, sameNs ? null : element.Namespace, ((TypeMapping)element.Mapping).TypeDesc, ((TypeMapping)element.Mapping).TypeDesc, element.IsNullable, defaultForm ? XmlSchemaForm.None : element.Form); } } } if (member.ChoiceIdentifier != null) { CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(typeof(XmlChoiceIdentifierAttribute).FullName); attribute.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(member.ChoiceIdentifier.MemberName))); metadata.Add(attribute); } if (member.Ignore) { CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(typeof(XmlIgnoreAttribute).FullName); metadata.Add(attribute); } } }
private static bool ShouldInclude(ArrayMapping arrayMapping) { if (arrayMapping.ReferencedByElement) return false; if (arrayMapping.Next != null) return false; if (arrayMapping.Elements.Length == 1) { TypeKind kind = arrayMapping.Elements[0].Mapping.TypeDesc.Kind; if (kind == TypeKind.Node) return false; } return true; }
XmlQualifiedName ExportArrayMapping(ArrayMapping mapping, string ns) { // for the Rpc ArrayMapping different mappings could have the same schema type // we link all mappings corresponding to the same type together // loop through all mapping that will map to the same complexType, and export only one, // the obvious choice is the last one. while (mapping.Next != null) { mapping = mapping.Next; } XmlSchemaComplexType type = (XmlSchemaComplexType)types[mapping]; if (type == null) { CheckForDuplicateType(mapping.TypeName, mapping.Namespace); type = new XmlSchemaComplexType(); type.Name = mapping.TypeName; types.Add(mapping, type); // we need to add the type first, to make sure that the schema get created AddSchemaItem(type, mapping.Namespace, ns); AddSchemaImport(Soap.Encoding, mapping.Namespace); AddSchemaImport(Wsdl.Namespace, mapping.Namespace); XmlSchemaComplexContentRestriction restriction = new XmlSchemaComplexContentRestriction(); XmlQualifiedName qname = ExportTypeMapping(mapping.Elements[0].Mapping, mapping.Namespace); if (qname.IsEmpty) { // this is a root mapping qname = new XmlQualifiedName(Soap.UrType, XmlSchema.Namespace); } //<attribute ref="soapenc:arrayType" wsdl:arrayType="xsd:float[]"/> XmlSchemaAttribute attr = new XmlSchemaAttribute(); attr.RefName = ArrayTypeQName; XmlAttribute attribute = new XmlAttribute("wsdl", Wsdl.ArrayType, Wsdl.Namespace, Document); attribute.Value = qname.Namespace + ":" + qname.Name + "[]"; attr.UnhandledAttributes = new XmlAttribute[] {attribute}; restriction.Attributes.Add(attr); restriction.BaseTypeName = ArrayQName; XmlSchemaComplexContent model = new XmlSchemaComplexContent(); model.Content = restriction; type.ContentModel = model; } else { AddSchemaImport(mapping.Namespace, ns); } return new XmlQualifiedName(mapping.TypeName, mapping.Namespace); }
private void CreateArrayElementsFromAttributes(ArrayMapping arrayMapping, XmlArrayItemAttributes attributes, Type arrayElementType, string arrayElementNs, RecursionLimiter limiter) { System.Xml.Serialization.NameTable scope = new System.Xml.Serialization.NameTable(); for (int i = 0; (attributes != null) && (i < attributes.Count); i++) { XmlArrayItemAttribute attribute = attributes[i]; if (attribute.NestingLevel == this.arrayNestingLevel) { ElementAccessor accessor; Type type = (attribute.Type != null) ? attribute.Type : arrayElementType; TypeDesc typeDesc = this.typeScope.GetTypeDesc(type); accessor = new ElementAccessor { Namespace = (attribute.Namespace == null) ? arrayElementNs : attribute.Namespace, Mapping = this.ImportTypeMapping(this.modelScope.GetTypeModel(type), accessor.Namespace, ImportContext.Element, attribute.DataType, null, limiter), Name = (attribute.ElementName.Length == 0) ? accessor.Mapping.DefaultElementName : XmlConvert.EncodeLocalName(attribute.ElementName), IsNullable = attribute.IsNullableSpecified ? attribute.IsNullable : (typeDesc.IsNullable || typeDesc.IsOptionalValue), Form = (attribute.Form == XmlSchemaForm.None) ? XmlSchemaForm.Qualified : attribute.Form }; CheckForm(accessor.Form, arrayElementNs != accessor.Namespace); CheckNullable(accessor.IsNullable, typeDesc, accessor.Mapping); AddUniqueAccessor(scope, accessor); } } arrayMapping.Elements = (ElementAccessor[]) scope.ToArray(typeof(ElementAccessor)); }
private void SetArrayMappingType(ArrayMapping mapping, string defaultNs, Type type) { XmlAttributes a = this.GetAttributes(type, false); if (IsAnonymousType(a, defaultNs)) { mapping.TypeName = null; mapping.Namespace = defaultNs; } else { string defaultElementName; string str2; TypeMapping mapping2; ElementAccessor accessor = null; if (mapping.Elements.Length == 1) { accessor = mapping.Elements[0]; mapping2 = accessor.Mapping; } else { mapping2 = null; } bool flag2 = true; if (a.XmlType != null) { str2 = a.XmlType.Namespace; defaultElementName = XmlConvert.EncodeLocalName(this.XsdTypeName(type, a, a.XmlType.TypeName)); flag2 = defaultElementName == null; } else if (mapping2 is EnumMapping) { str2 = mapping2.Namespace; defaultElementName = mapping2.DefaultElementName; } else if (mapping2 is PrimitiveMapping) { str2 = defaultNs; defaultElementName = mapping2.TypeDesc.DataType.Name; } else if ((mapping2 is StructMapping) && mapping2.TypeDesc.IsRoot) { str2 = defaultNs; defaultElementName = "anyType"; } else if (mapping2 != null) { str2 = (mapping2.Namespace == "http://www.w3.org/2001/XMLSchema") ? defaultNs : mapping2.Namespace; defaultElementName = mapping2.DefaultElementName; } else { str2 = defaultNs; defaultElementName = "Choice" + this.choiceNum++; } if (defaultElementName == null) { defaultElementName = "Any"; } if (accessor != null) { str2 = accessor.Namespace; } if (str2 == null) { str2 = defaultNs; } string str3 = defaultElementName = flag2 ? ("ArrayOf" + CodeIdentifier.MakePascal(defaultElementName)) : defaultElementName; int num = 1; TypeMapping mapping3 = (TypeMapping) this.types[str3, str2]; while (mapping3 != null) { if (mapping3 is ArrayMapping) { ArrayMapping mapping4 = (ArrayMapping) mapping3; if (AccessorMapping.ElementsMatch(mapping4.Elements, mapping.Elements)) { break; } } str3 = defaultElementName + num.ToString(CultureInfo.InvariantCulture); mapping3 = (TypeMapping) this.types[str3, str2]; num++; } mapping.TypeName = str3; mapping.Namespace = str2; } }
private ArrayMapping ImportArrayLikeMapping(ArrayModel model, string ns, RecursionLimiter limiter) { ArrayMapping arrayMapping = new ArrayMapping { TypeDesc = model.TypeDesc }; if (this.savedArrayItemAttributes == null) { this.savedArrayItemAttributes = new XmlArrayItemAttributes(); } if (CountAtLevel(this.savedArrayItemAttributes, this.arrayNestingLevel) == 0) { this.savedArrayItemAttributes.Add(CreateArrayItemAttribute(this.typeScope.GetTypeDesc(model.Element.Type), this.arrayNestingLevel)); } this.CreateArrayElementsFromAttributes(arrayMapping, this.savedArrayItemAttributes, model.Element.Type, (this.savedArrayNamespace == null) ? ns : this.savedArrayNamespace, limiter); this.SetArrayMappingType(arrayMapping, ns, model.Type); for (int i = 0; i < arrayMapping.Elements.Length; i++) { arrayMapping.Elements[i] = this.ReconcileLocalAccessor(arrayMapping.Elements[i], arrayMapping.Namespace); } this.IncludeTypes(model.Type); ArrayMapping next = (ArrayMapping) this.types[arrayMapping.TypeName, arrayMapping.Namespace]; if (next != null) { ArrayMapping mapping3 = next; while (next != null) { if (next.TypeDesc == model.TypeDesc) { return next; } next = next.Next; } arrayMapping.Next = mapping3; if (!arrayMapping.IsAnonymousType) { this.types[arrayMapping.TypeName, arrayMapping.Namespace] = arrayMapping; return arrayMapping; } this.anonymous[model.Type] = arrayMapping; return arrayMapping; } this.typeScope.AddTypeMapping(arrayMapping); if (!arrayMapping.IsAnonymousType) { this.types.Add(arrayMapping.TypeName, arrayMapping.Namespace, arrayMapping); return arrayMapping; } this.anonymous[model.Type] = arrayMapping; return arrayMapping; }
void SetArrayMappingType(ArrayMapping mapping, string defaultNs, Type type) { XmlAttributes a = GetAttributes(type, false); bool isAnonymous = a.XmlType == null ? false : a.XmlType.AnonymousType; if (isAnonymous) { mapping.TypeName = null; mapping.Namespace = defaultNs; return; } string name; string ns; TypeMapping itemTypeMapping; ElementAccessor element = null; if (mapping.Elements.Length == 1) { element = mapping.Elements[0]; itemTypeMapping = element.Mapping; } else { itemTypeMapping = null; } bool generateTypeName = true; if (a.XmlType != null) { ns = a.XmlType.Namespace; name = XsdTypeName(type, a, a.XmlType.TypeName); name = XmlConvert.EncodeLocalName(name); generateTypeName = name == null; } else if (itemTypeMapping is EnumMapping) { ns = itemTypeMapping.Namespace; name = itemTypeMapping.DefaultElementName; } else if (itemTypeMapping is PrimitiveMapping) { ns = defaultNs; name = itemTypeMapping.TypeDesc.DataType.Name; } else if (itemTypeMapping is StructMapping && itemTypeMapping.TypeDesc.IsRoot) { ns = defaultNs; name = Soap.UrType; } else if (itemTypeMapping != null) { ns = itemTypeMapping.Namespace == XmlSchema.Namespace ? defaultNs : itemTypeMapping.Namespace; name = itemTypeMapping.DefaultElementName; } else { ns = defaultNs; name = "Choice" + (choiceNum++); } if (name == null) name = "Any"; if (element != null) ns = element.Namespace; if (ns == null) ns = defaultNs; string uniqueName = name = generateTypeName ? "ArrayOf" + CodeIdentifier.MakePascal(name) : name; int i = 1; TypeMapping existingMapping = (TypeMapping)types[uniqueName, ns]; while (existingMapping != null) { if (existingMapping is ArrayMapping) { ArrayMapping arrayMapping = (ArrayMapping)existingMapping; if (AccessorMapping.ElementsMatch(arrayMapping.Elements, mapping.Elements)) { break; } } // need to re-name the mapping uniqueName = name + i.ToString(CultureInfo.InvariantCulture); existingMapping = (TypeMapping)types[uniqueName, ns]; i++; } mapping.TypeName = uniqueName; mapping.Namespace = ns; }
private void ExportArrayElements(CodeAttributeDeclarationCollection metadata, ArrayMapping array, string ns, TypeDesc elementTypeDesc, int nestingLevel) { for (int i = 0; i < array.Elements.Length; i++) { ElementAccessor accessor = array.Elements[i]; TypeMapping mapping = accessor.Mapping; string str = Accessor.UnescapeName(accessor.Name); bool flag = !accessor.Mapping.TypeDesc.IsArray && (str == accessor.Mapping.TypeName); bool flag2 = mapping.TypeDesc == elementTypeDesc; bool flag3 = (accessor.Form == XmlSchemaForm.Unqualified) || (accessor.Namespace == ns); bool flag4 = accessor.IsNullable == mapping.TypeDesc.IsNullable; bool flag5 = accessor.Form != XmlSchemaForm.Unqualified; if (((!flag || !flag2) || (!flag3 || !flag4)) || (!flag5 || (nestingLevel > 0))) { this.ExportArrayItem(metadata, flag ? null : str, flag3 ? null : accessor.Namespace, flag2 ? null : mapping.TypeDesc, mapping.TypeDesc, accessor.IsNullable, flag5 ? XmlSchemaForm.None : accessor.Form, nestingLevel); } if (mapping is ArrayMapping) { this.ExportArrayElements(metadata, (ArrayMapping) mapping, ns, elementTypeDesc.ArrayElementTypeDesc, nestingLevel + 1); } } }
ArrayMapping ImportArrayLikeMapping(ArrayModel model, string ns) { ArrayMapping mapping = new ArrayMapping(); mapping.TypeDesc = model.TypeDesc; if (savedArrayItemAttributes == null) savedArrayItemAttributes = new XmlArrayItemAttributes(); if (CountAtLevel(savedArrayItemAttributes, arrayNestingLevel) == 0) savedArrayItemAttributes.Add(CreateArrayItemAttribute(typeScope.GetTypeDesc(model.Element.Type), arrayNestingLevel)); CreateArrayElementsFromAttributes(mapping, savedArrayItemAttributes, model.Element.Type, savedArrayNamespace == null ? ns : savedArrayNamespace); SetArrayMappingType(mapping, ns, model.Type); // reconcile accessors now that we have the ArrayMapping namespace for (int i = 0; i < mapping.Elements.Length; i++) { mapping.Elements[i] = ReconcileLocalAccessor(mapping.Elements[i], mapping.Namespace); } IncludeTypes(model.Type); // in the case of an ArrayMapping we can have more that one mapping correspond to a type // examples of that are ArrayList and object[] both will map tp ArrayOfur-type // so we create a link list for all mappings of the same XSD type ArrayMapping existingMapping = (ArrayMapping)types[mapping.TypeName, mapping.Namespace]; if (existingMapping != null) { ArrayMapping first = existingMapping; while (existingMapping != null) { if (existingMapping.TypeDesc == model.TypeDesc) return existingMapping; existingMapping = existingMapping.Next; } mapping.Next = first; if (!mapping.IsAnonymousType) types[mapping.TypeName, mapping.Namespace] = mapping; else anonymous[model.Type] = mapping; return mapping; } typeScope.AddTypeMapping(mapping); if (!mapping.IsAnonymousType) types.Add(mapping.TypeName, mapping.Namespace, mapping); else anonymous[model.Type] = mapping; return mapping; }
private void ExportArrayMapping(ArrayMapping mapping, string ns, XmlSchemaElement element) { ArrayMapping next = mapping; while (next.Next != null) { next = next.Next; } XmlSchemaComplexType item = (XmlSchemaComplexType) this.types[next]; if (item == null) { this.CheckForDuplicateType(next, next.Namespace); item = new XmlSchemaComplexType(); if (!mapping.IsAnonymousType) { item.Name = mapping.TypeName; this.AddSchemaItem(item, mapping.Namespace, ns); } if (!next.IsAnonymousType) { this.types.Add(next, item); } XmlSchemaSequence group = new XmlSchemaSequence(); this.ExportElementAccessors(group, mapping.Elements, true, false, mapping.Namespace); if (group.Items.Count > 0) { if (group.Items[0] is XmlSchemaChoice) { item.Particle = (XmlSchemaChoice) group.Items[0]; } else { item.Particle = group; } } } else { this.AddSchemaImport(mapping.Namespace, ns); } if (element != null) { if (mapping.IsAnonymousType) { element.SchemaType = item; } else { element.SchemaTypeName = new XmlQualifiedName(item.Name, mapping.Namespace); } } }
void ExportArrayMapping(ArrayMapping mapping, string ns, XmlSchemaElement element) { // some of the items in the linked list differ only by CLR type. We don't need to // export different schema types for these. Look further down the list for another // entry with the same elements. If there is one, it will be exported later so // just return its name now. ArrayMapping currentMapping = mapping; while (currentMapping.Next != null) { currentMapping = currentMapping.Next; } XmlSchemaComplexType type = (XmlSchemaComplexType) types[currentMapping]; if (type == null) { CheckForDuplicateType(currentMapping, currentMapping.Namespace); type = new XmlSchemaComplexType(); if (!mapping.IsAnonymousType) { type.Name = mapping.TypeName; AddSchemaItem(type, mapping.Namespace, ns); } if (!currentMapping.IsAnonymousType) types.Add(currentMapping, type); XmlSchemaSequence seq = new XmlSchemaSequence(); ExportElementAccessors(seq, mapping.Elements, true, false, mapping.Namespace); if (seq.Items.Count > 0) { #if DEBUG // we can have only one item for the array mapping if (seq.Items.Count != 1) throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Type " + mapping.TypeName + " from namespace '" + ns + "' is an invalid array mapping")); #endif if (seq.Items[0] is XmlSchemaChoice) { type.Particle = (XmlSchemaChoice)seq.Items[0]; } else { type.Particle = seq; } } } else { AddSchemaImport(mapping.Namespace, ns); } if (element != null) { if (mapping.IsAnonymousType) { element.SchemaType = type; } else { element.SchemaTypeName = new XmlQualifiedName(type.Name, mapping.Namespace); } } }
private ArrayMapping ImportArrayMapping(XmlSchemaType type, string ns) { ArrayMapping arrayMapping; if (type.Name == Soap.Array && ns == Soap.Encoding) { arrayMapping = new ArrayMapping(); TypeMapping mapping = GetRootMapping(); ElementAccessor itemAccessor = new ElementAccessor(); itemAccessor.IsSoap = true; itemAccessor.Name = Soap.UrType; itemAccessor.Namespace = ns; itemAccessor.Mapping = mapping; itemAccessor.IsNullable = true; itemAccessor.Form = XmlSchemaForm.None; arrayMapping.Elements = new ElementAccessor[] { itemAccessor }; arrayMapping.TypeDesc = itemAccessor.Mapping.TypeDesc.CreateArrayTypeDesc(); arrayMapping.TypeName = "ArrayOf" + CodeIdentifier.MakePascal(itemAccessor.Mapping.TypeName); return(arrayMapping); } if (!(type.DerivedFrom.Name == Soap.Array && type.DerivedFrom.Namespace == Soap.Encoding)) { return(null); } // the type should be a XmlSchemaComplexType XmlSchemaContentModel model = ((XmlSchemaComplexType)type).ContentModel; // the Content should be an restriction if (!(model.Content is XmlSchemaComplexContentRestriction)) { return(null); } arrayMapping = new ArrayMapping(); XmlSchemaComplexContentRestriction restriction = (XmlSchemaComplexContentRestriction)model.Content; for (int i = 0; i < restriction.Attributes.Count; i++) { XmlSchemaAttribute attribute = restriction.Attributes[i] as XmlSchemaAttribute; if (attribute != null && attribute.RefName.Name == Soap.ArrayType && attribute.RefName.Namespace == Soap.Encoding) { // read the value of the wsdl:arrayType attribute string arrayType = null; if (attribute.UnhandledAttributes != null) { foreach (XmlAttribute a in attribute.UnhandledAttributes) { if (a.LocalName == Wsdl.ArrayType && a.NamespaceURI == Wsdl.Namespace) { arrayType = a.Value; break; } } } if (arrayType != null) { string dims; XmlQualifiedName typeName = TypeScope.ParseWsdlArrayType(arrayType, out dims, attribute); TypeMapping mapping; TypeDesc td = Scope.GetTypeDesc(typeName.Name, typeName.Namespace); if (td != null && td.IsPrimitive) { mapping = new PrimitiveMapping(); mapping.TypeDesc = td; mapping.TypeName = td.DataType.Name; } else { mapping = ImportType(typeName, false); } ElementAccessor itemAccessor = new ElementAccessor(); itemAccessor.IsSoap = true; itemAccessor.Name = typeName.Name; itemAccessor.Namespace = ns; itemAccessor.Mapping = mapping; itemAccessor.IsNullable = true; itemAccessor.Form = XmlSchemaForm.None; arrayMapping.Elements = new ElementAccessor[] { itemAccessor }; arrayMapping.TypeDesc = itemAccessor.Mapping.TypeDesc.CreateArrayTypeDesc(); arrayMapping.TypeName = "ArrayOf" + CodeIdentifier.MakePascal(itemAccessor.Mapping.TypeName); return(arrayMapping); } } } XmlSchemaParticle particle = restriction.Particle; if (particle is XmlSchemaAll || particle is XmlSchemaSequence) { XmlSchemaGroupBase group = (XmlSchemaGroupBase)particle; if (group.Items.Count != 1 || !(group.Items[0] is XmlSchemaElement)) { return(null); } XmlSchemaElement itemElement = (XmlSchemaElement)group.Items[0]; if (!itemElement.IsMultipleOccurrence) { return(null); } ElementAccessor itemAccessor = ImportElement(itemElement, ns); arrayMapping.Elements = new ElementAccessor[] { itemAccessor }; arrayMapping.TypeDesc = ((TypeMapping)itemAccessor.Mapping).TypeDesc.CreateArrayTypeDesc(); } else { return(null); } return(arrayMapping); }
ArrayMapping ImportArrayLikeMapping(ArrayModel model) { ArrayMapping mapping = new ArrayMapping(); mapping.IsSoap = true; TypeMapping itemTypeMapping = ImportTypeMapping(model.Element); if (itemTypeMapping.TypeDesc.IsValueType && !itemTypeMapping.TypeDesc.IsPrimitive && !itemTypeMapping.TypeDesc.IsEnum) throw new NotSupportedException(Res.GetString(Res.XmlRpcArrayOfValueTypes, model.TypeDesc.FullName)); mapping.TypeDesc = model.TypeDesc; mapping.Elements = new ElementAccessor[] { CreateElementAccessor(itemTypeMapping, mapping.Namespace) }; SetArrayMappingType(mapping); // in the case of an ArrayMapping we can have more that one mapping correspond to a type // examples of that are ArrayList and object[] both will map tp ArrayOfur-type // so we create a link list for all mappings of the same XSD type ArrayMapping existingMapping = (ArrayMapping)types[mapping.TypeName, mapping.Namespace]; if (existingMapping != null) { ArrayMapping first = existingMapping; while (existingMapping != null) { if (existingMapping.TypeDesc == model.TypeDesc) return existingMapping; existingMapping = existingMapping.Next; } mapping.Next = first; types[mapping.TypeName, mapping.Namespace] = mapping; return mapping; } typeScope.AddTypeMapping(mapping); types.Add(mapping.TypeName, mapping.Namespace, mapping); IncludeTypes(model.Type); return mapping; }
private bool IsNeedXmlSerializationAttributes(ArrayMapping arrayMapping) { if (arrayMapping.Elements.Length != 1) { return true; } ElementAccessor accessor = arrayMapping.Elements[0]; TypeMapping mapping = accessor.Mapping; if (accessor.Name != mapping.DefaultElementName) { return true; } if ((accessor.Form != XmlSchemaForm.None) && (accessor.Form != XmlSchemaForm.Qualified)) { return true; } if (accessor.Mapping.TypeDesc != null) { if (accessor.IsNullable != accessor.Mapping.TypeDesc.IsNullable) { return true; } if (accessor.Mapping.TypeDesc.IsAmbiguousDataType) { return true; } } return false; }
private void WriteArray(string source, string arrayName, ArrayMapping arrayMapping, bool readOnly, bool isNullable, int fixupIndex) { if (arrayMapping.IsSoap) { base.Writer.Write("object rre = "); base.Writer.Write((fixupIndex >= 0) ? "ReadReferencingElement" : "ReadReferencedElement"); base.Writer.Write("("); this.WriteID(arrayMapping.TypeName); base.Writer.Write(", "); this.WriteID(arrayMapping.Namespace); if (fixupIndex >= 0) { base.Writer.Write(", "); base.Writer.Write("out fixup.Ids["); base.Writer.Write(fixupIndex.ToString(CultureInfo.InvariantCulture)); base.Writer.Write("]"); } base.Writer.WriteLine(");"); TypeDesc typeDesc = arrayMapping.TypeDesc; if (typeDesc.IsEnumerable || typeDesc.IsCollection) { base.Writer.WriteLine("if (rre != null) {"); IndentedWriter writer = base.Writer; writer.Indent++; this.WriteAddCollectionFixup(typeDesc, readOnly, source, "rre"); IndentedWriter writer2 = base.Writer; writer2.Indent--; base.Writer.WriteLine("}"); } else { base.Writer.WriteLine("try {"); IndentedWriter writer3 = base.Writer; writer3.Indent++; this.WriteSourceBeginTyped(source, arrayMapping.TypeDesc); base.Writer.Write("rre"); this.WriteSourceEnd(source); base.Writer.WriteLine(";"); this.WriteCatchCastException(arrayMapping.TypeDesc, "rre", null); } } else { base.Writer.WriteLine("if (!ReadNull()) {"); IndentedWriter writer4 = base.Writer; writer4.Indent++; MemberMapping mapping = new MemberMapping { Elements = arrayMapping.Elements, TypeDesc = arrayMapping.TypeDesc, ReadOnly = readOnly }; Member member = new Member(this, source, arrayName, 0, mapping, false) { IsNullable = false }; Member[] members = new Member[] { member }; this.WriteMemberBegin(members); if (readOnly) { base.Writer.Write("if (((object)("); base.Writer.Write(member.ArrayName); base.Writer.Write(") == null) || "); } else { base.Writer.Write("if ("); } base.Writer.WriteLine("(Reader.IsEmptyElement)) {"); IndentedWriter writer5 = base.Writer; writer5.Indent++; base.Writer.WriteLine("Reader.Skip();"); IndentedWriter writer6 = base.Writer; writer6.Indent--; base.Writer.WriteLine("}"); base.Writer.WriteLine("else {"); IndentedWriter writer7 = base.Writer; writer7.Indent++; base.Writer.WriteLine("Reader.ReadStartElement();"); int loopIndex = this.WriteWhileNotLoopStart(); IndentedWriter writer8 = base.Writer; writer8.Indent++; string elementElseString = "UnknownNode(null, " + this.ExpectedElements(members) + ");"; this.WriteMemberElements(members, elementElseString, elementElseString, null, null, null); base.Writer.WriteLine("Reader.MoveToContent();"); this.WriteWhileLoopEnd(loopIndex); IndentedWriter writer9 = base.Writer; writer9.Indent--; base.Writer.WriteLine("ReadEndElement();"); base.Writer.WriteLine("}"); this.WriteMemberEnd(members, false); IndentedWriter writer10 = base.Writer; writer10.Indent--; base.Writer.WriteLine("}"); if (isNullable) { base.Writer.WriteLine("else {"); IndentedWriter writer11 = base.Writer; writer11.Indent++; member.IsNullable = true; this.WriteMemberBegin(members); this.WriteMemberEnd(members); IndentedWriter writer12 = base.Writer; writer12.Indent--; base.Writer.WriteLine("}"); } } }
void WriteArray(string source, string arrayName, ArrayMapping arrayMapping, bool readOnly, bool isNullable, int fixupIndex, int elementIndex) { MethodInfo XmlSerializationReader_ReadNull = typeof(XmlSerializationReader).GetMethod( "ReadNull", CodeGenerator.InstanceBindingFlags, null, CodeGenerator.EmptyTypeArray, null ); ilg.Ldarg(0); ilg.Call(XmlSerializationReader_ReadNull); ilg.IfNot(); MemberMapping memberMapping = new MemberMapping(); memberMapping.Elements = arrayMapping.Elements; memberMapping.TypeDesc = arrayMapping.TypeDesc; memberMapping.ReadOnly = readOnly; if (source.StartsWith("o.@", StringComparison.Ordinal)) { Debug.Assert(memberInfos.ContainsKey(source.Substring(3))); memberMapping.MemberInfo = memberInfos[source.Substring(3)]; } Member member = new Member(this, source, arrayName, elementIndex, memberMapping, false); member.IsNullable = false;//Note, [....]: IsNullable is set to false since null condition (xsi:nil) is already handled by 'ReadNull()' Member[] members = new Member[] { member }; WriteMemberBegin(members); Label labelTrue = ilg.DefineLabel(); Label labelEnd = ilg.DefineLabel(); if (readOnly) { ilg.Load(ilg.GetVariable(member.ArrayName)); ilg.Load(null); ilg.Beq(labelTrue); } else { } MethodInfo XmlSerializationReader_get_Reader = typeof(XmlSerializationReader).GetMethod( "get_Reader", CodeGenerator.InstanceBindingFlags, null, CodeGenerator.EmptyTypeArray, null ); MethodInfo XmlReader_get_IsEmptyElement = typeof(XmlReader).GetMethod( "get_IsEmptyElement", CodeGenerator.InstanceBindingFlags, null, CodeGenerator.EmptyTypeArray, null ); ilg.Ldarg(0); ilg.Call(XmlSerializationReader_get_Reader); ilg.Call(XmlReader_get_IsEmptyElement); if (readOnly) { ilg.Br_S(labelEnd); ilg.MarkLabel(labelTrue); ilg.Ldc(true); ilg.MarkLabel(labelEnd); } ilg.If(); MethodInfo XmlReader_Skip = typeof(XmlReader).GetMethod( "Skip", CodeGenerator.InstanceBindingFlags, null, CodeGenerator.EmptyTypeArray, null ); ilg.Ldarg(0); ilg.Call(XmlSerializationReader_get_Reader); ilg.Call(XmlReader_Skip); ilg.Else(); MethodInfo XmlReader_ReadStartElement = typeof(XmlReader).GetMethod( "ReadStartElement", CodeGenerator.InstanceBindingFlags, null, CodeGenerator.EmptyTypeArray, null ); ilg.Ldarg(0); ilg.Call(XmlSerializationReader_get_Reader); ilg.Call(XmlReader_ReadStartElement); int loopIndex = WriteWhileNotLoopStart(); string unknownNode = "UnknownNode(null, " + ExpectedElements(members) + ");"; WriteMemberElements(members, unknownNode, unknownNode, null, null); MethodInfo XmlReader_MoveToContent = typeof(XmlReader).GetMethod( "MoveToContent", CodeGenerator.InstanceBindingFlags, null, CodeGenerator.EmptyTypeArray, null ); ilg.Ldarg(0); ilg.Call(XmlSerializationReader_get_Reader); ilg.Call(XmlReader_MoveToContent); ilg.Pop(); WriteWhileLoopEnd(loopIndex); MethodInfo XmlSerializationReader_ReadEndElement = typeof(XmlSerializationReader).GetMethod( "ReadEndElement", CodeGenerator.InstanceBindingFlags, null, CodeGenerator.EmptyTypeArray, null ); ilg.Ldarg(0); ilg.Call(XmlSerializationReader_ReadEndElement); ilg.EndIf(); WriteMemberEnd(members, false); if (isNullable) { ilg.Else(); member.IsNullable = true; WriteMemberBegin(members); WriteMemberEnd(members); } ilg.EndIf(); }
private XmlQualifiedName ExportArrayMapping(ArrayMapping mapping, string ns) { while (mapping.Next != null) { mapping = mapping.Next; } if (((XmlSchemaComplexType) this.types[mapping]) == null) { this.CheckForDuplicateType(mapping.TypeName, mapping.Namespace); XmlSchemaComplexType type = new XmlSchemaComplexType { Name = mapping.TypeName }; this.types.Add(mapping, type); this.AddSchemaItem(type, mapping.Namespace, ns); this.AddSchemaImport("http://schemas.xmlsoap.org/soap/encoding/", mapping.Namespace); this.AddSchemaImport("http://schemas.xmlsoap.org/wsdl/", mapping.Namespace); XmlSchemaComplexContentRestriction restriction = new XmlSchemaComplexContentRestriction(); XmlQualifiedName name = this.ExportTypeMapping(mapping.Elements[0].Mapping, mapping.Namespace); if (name.IsEmpty) { name = new XmlQualifiedName("anyType", "http://www.w3.org/2001/XMLSchema"); } XmlSchemaAttribute item = new XmlSchemaAttribute { RefName = ArrayTypeQName }; XmlAttribute attribute2 = new XmlAttribute("wsdl", "arrayType", "http://schemas.xmlsoap.org/wsdl/", this.Document) { Value = name.Namespace + ":" + name.Name + "[]" }; item.UnhandledAttributes = new XmlAttribute[] { attribute2 }; restriction.Attributes.Add(item); restriction.BaseTypeName = ArrayQName; XmlSchemaComplexContent content = new XmlSchemaComplexContent { Content = restriction }; type.ContentModel = content; if (name.Namespace != "http://www.w3.org/2001/XMLSchema") { this.AddSchemaImport(name.Namespace, mapping.Namespace); } } else { this.AddSchemaImport(mapping.Namespace, ns); } return new XmlQualifiedName(mapping.TypeName, mapping.Namespace); }
ArrayMapping ImportArrayMapping(XmlSchemaType type, string identifier, string ns, bool repeats) { if (!(type is XmlSchemaComplexType)) return null; if (!type.DerivedFrom.IsEmpty) return null; if (IsMixed(type)) return null; Mapping previousMapping = (Mapping)mappings[type]; if (previousMapping != null) { if (previousMapping is ArrayMapping) return (ArrayMapping)previousMapping; else return null; } TypeItems items = GetTypeItems(type); if (items.Attributes != null && items.Attributes.Count > 0) return null; if (items.AnyAttribute != null) return null; if (items.Particle == null) return null; XmlSchemaGroupBase item = items.Particle; ArrayMapping arrayMapping = new ArrayMapping(); arrayMapping.TypeName = identifier; arrayMapping.Namespace = ns; if (item is XmlSchemaChoice) { XmlSchemaChoice choice = (XmlSchemaChoice)item; if (!choice.IsMultipleOccurrence) return null; MemberMapping choiceMember = ImportChoiceGroup(choice, identifier, null, ns, true); arrayMapping.TypeDesc = choiceMember.TypeDesc; arrayMapping.Elements = choiceMember.Elements; arrayMapping.TypeName = "ArrayOf" + CodeIdentifier.MakePascal(arrayMapping.TypeDesc.Name); } else if (item is XmlSchemaAll || item is XmlSchemaSequence) { if (item.Items.Count != 1 || !(item.Items[0] is XmlSchemaElement)) return null; XmlSchemaElement itemElement = (XmlSchemaElement)item.Items[0]; if (!itemElement.IsMultipleOccurrence) return null; ElementAccessor itemAccessor; if ((itemAccessor = ImportSerializable(itemElement, identifier, ns, repeats)) == null) { itemAccessor = ImportElement(itemElement, identifier, typeof(TypeMapping), null, ns); } arrayMapping.Elements = new ElementAccessor[] { itemAccessor }; arrayMapping.TypeDesc = ((TypeMapping)itemAccessor.Mapping).TypeDesc.CreateArrayTypeDesc(); arrayMapping.TypeName = "ArrayOf" + CodeIdentifier.MakePascal(itemAccessor.Mapping.TypeName); } else { return null; } if (type.Name != null && type.Name.Length != 0) mappings[type] = arrayMapping; scope.AddTypeMapping(arrayMapping); // for the array-like mappings we need to create a struct mapping for the case when it referenced by the top-level element arrayMapping.TopLevelMapping = ImportStructType(type, ns, identifier, null, true); arrayMapping.TopLevelMapping.ReferencedByTopLevelElement = true; return arrayMapping; }
ArrayMapping ImportArrayMapping(XmlSchemaType type, string ns) { ArrayMapping arrayMapping; if (type.Name == Soap.Array && ns == Soap.Encoding) { arrayMapping = new ArrayMapping(); TypeMapping mapping = GetRootMapping(); ElementAccessor itemAccessor = new ElementAccessor(); itemAccessor.IsSoap = true; itemAccessor.Name = Soap.UrType; itemAccessor.Namespace = ns; itemAccessor.Mapping = mapping; itemAccessor.IsNullable = true; itemAccessor.Form = XmlSchemaForm.None; arrayMapping.Elements = new ElementAccessor[] { itemAccessor }; arrayMapping.TypeDesc = itemAccessor.Mapping.TypeDesc.CreateArrayTypeDesc(); arrayMapping.TypeName = "ArrayOf" + CodeIdentifier.MakePascal(itemAccessor.Mapping.TypeName); return arrayMapping; } if (!(type.DerivedFrom.Name == Soap.Array && type.DerivedFrom.Namespace == Soap.Encoding)) return null; // the type should be a XmlSchemaComplexType XmlSchemaContentModel model = ((XmlSchemaComplexType)type).ContentModel; // the Content should be an restriction if (!(model.Content is XmlSchemaComplexContentRestriction)) return null; arrayMapping = new ArrayMapping(); XmlSchemaComplexContentRestriction restriction = (XmlSchemaComplexContentRestriction)model.Content; for (int i = 0; i < restriction.Attributes.Count; i++) { XmlSchemaAttribute attribute = restriction.Attributes[i] as XmlSchemaAttribute; if (attribute != null && attribute.RefName.Name == Soap.ArrayType && attribute.RefName.Namespace == Soap.Encoding) { // read the value of the wsdl:arrayType attribute string arrayType = null; if (attribute.UnhandledAttributes != null) { foreach (XmlAttribute a in attribute.UnhandledAttributes) { if (a.LocalName == Wsdl.ArrayType && a.NamespaceURI == Wsdl.Namespace) { arrayType = a.Value; break; } } } if (arrayType != null) { string dims; XmlQualifiedName typeName = TypeScope.ParseWsdlArrayType(arrayType, out dims, attribute); TypeMapping mapping; TypeDesc td = Scope.GetTypeDesc(typeName.Name, typeName.Namespace); if (td != null && td.IsPrimitive) { mapping = new PrimitiveMapping(); mapping.TypeDesc = td; mapping.TypeName = td.DataType.Name; } else { mapping = ImportType(typeName, false); } ElementAccessor itemAccessor = new ElementAccessor(); itemAccessor.IsSoap = true; itemAccessor.Name = typeName.Name; itemAccessor.Namespace = ns; itemAccessor.Mapping = mapping; itemAccessor.IsNullable = true; itemAccessor.Form = XmlSchemaForm.None; arrayMapping.Elements = new ElementAccessor[] { itemAccessor }; arrayMapping.TypeDesc = itemAccessor.Mapping.TypeDesc.CreateArrayTypeDesc(); arrayMapping.TypeName = "ArrayOf" + CodeIdentifier.MakePascal(itemAccessor.Mapping.TypeName); return arrayMapping; } } } XmlSchemaParticle particle = restriction.Particle; if (particle is XmlSchemaAll || particle is XmlSchemaSequence) { XmlSchemaGroupBase group = (XmlSchemaGroupBase)particle; if (group.Items.Count != 1 || !(group.Items[0] is XmlSchemaElement)) return null; XmlSchemaElement itemElement = (XmlSchemaElement)group.Items[0]; if (!itemElement.IsMultipleOccurrence) return null; ElementAccessor itemAccessor = ImportElement(itemElement, ns); arrayMapping.Elements = new ElementAccessor[] { itemAccessor }; arrayMapping.TypeDesc = ((TypeMapping)itemAccessor.Mapping).TypeDesc.CreateArrayTypeDesc(); } else { return null; } return arrayMapping; }
void ExportArrayElements(CodeAttributeDeclarationCollection metadata, ArrayMapping array, string ns, TypeDesc elementTypeDesc, int nestingLevel) { for (int i = 0; i < array.Elements.Length; i++) { ElementAccessor arrayElement = array.Elements[i]; TypeMapping elementMapping = arrayElement.Mapping; string elementName = Accessor.UnescapeName(arrayElement.Name); bool sameName = arrayElement.Mapping.TypeDesc.IsArray ? false : elementName == arrayElement.Mapping.TypeName; bool sameElementType = elementMapping.TypeDesc == elementTypeDesc; bool sameElementNs = arrayElement.Form == XmlSchemaForm.Unqualified || arrayElement.Namespace == ns; bool sameNullable = arrayElement.IsNullable == elementMapping.TypeDesc.IsNullable; bool defaultForm = arrayElement.Form != XmlSchemaForm.Unqualified; if (!sameName || !sameElementType || !sameElementNs || !sameNullable || !defaultForm || nestingLevel > 0) ExportArrayItem(metadata, sameName ? null : elementName, sameElementNs ? null : arrayElement.Namespace, sameElementType ? null : elementMapping.TypeDesc, elementMapping.TypeDesc, arrayElement.IsNullable, defaultForm ? XmlSchemaForm.None : arrayElement.Form, nestingLevel); if (elementMapping is ArrayMapping) ExportArrayElements(metadata, (ArrayMapping) elementMapping, ns, elementTypeDesc.ArrayElementTypeDesc, nestingLevel+1); } }
// UNDONE Nullable private void SetArrayMappingType(ArrayMapping mapping) { bool useDefaultNs = false; string itemTypeName; string itemTypeNamespace; TypeMapping itemTypeMapping; if (mapping.Elements.Length == 1) { itemTypeMapping = mapping.Elements[0].Mapping; } else { itemTypeMapping = null; } if (itemTypeMapping is EnumMapping) { itemTypeNamespace = itemTypeMapping.Namespace; itemTypeName = itemTypeMapping.TypeName; } else if (itemTypeMapping is PrimitiveMapping) { itemTypeNamespace = itemTypeMapping.TypeDesc.IsXsdType ? XmlSchema.Namespace : UrtTypes.Namespace; itemTypeName = itemTypeMapping.TypeDesc.DataType.Name; useDefaultNs = true; } else if (itemTypeMapping is StructMapping) { if (itemTypeMapping.TypeDesc.IsRoot) { itemTypeNamespace = XmlSchema.Namespace; itemTypeName = Soap.UrType; useDefaultNs = true; } else { itemTypeNamespace = itemTypeMapping.Namespace; itemTypeName = itemTypeMapping.TypeName; } } else if (itemTypeMapping is ArrayMapping) { itemTypeNamespace = itemTypeMapping.Namespace; itemTypeName = itemTypeMapping.TypeName; } else { throw new InvalidOperationException(SR.Format(SR.XmlInvalidSoapArray, mapping.TypeDesc.FullName)); } itemTypeName = CodeIdentifier.MakePascal(itemTypeName); string uniqueName = "ArrayOf" + itemTypeName; string ns = useDefaultNs ? _defaultNs : itemTypeNamespace; int i = 1; TypeMapping existingMapping = (TypeMapping)_types[uniqueName, ns]; while (existingMapping != null) { if (existingMapping is ArrayMapping) { ArrayMapping arrayMapping = (ArrayMapping)existingMapping; if (AccessorMapping.ElementsMatch(arrayMapping.Elements, mapping.Elements)) { break; } } // need to re-name the mapping uniqueName = itemTypeName + i.ToString(CultureInfo.InvariantCulture); existingMapping = (TypeMapping)_types[uniqueName, ns]; i++; } mapping.Namespace = ns; mapping.TypeName = uniqueName; }
void SetArrayMappingType(ArrayMapping mapping, string defaultNs) { string name; string ns; TypeMapping itemTypeMapping; if (mapping.Elements.Length == 1) itemTypeMapping = mapping.Elements[0].Mapping; else itemTypeMapping = null; if (itemTypeMapping is EnumMapping) { ns = itemTypeMapping.Namespace; name = itemTypeMapping.TypeName; } else if (itemTypeMapping is PrimitiveMapping) { ns = defaultNs; name = itemTypeMapping.TypeDesc.DataType.Name; } else if (itemTypeMapping is StructMapping && itemTypeMapping.TypeDesc.IsRoot) { ns = defaultNs; name = Soap.UrType; } else if (itemTypeMapping != null) { ns = itemTypeMapping.Namespace; name = itemTypeMapping.TypeName; } else { ns = defaultNs; name = "Choice" + (choiceNum++); } if (name == null) name = "Any"; if (ns == null) ns = defaultNs; string uniqueName = name = "ArrayOf" + CodeIdentifier.MakePascal(name); int i = 1; ArrayMapping existingMapping = (ArrayMapping)types[uniqueName, ns]; while (existingMapping != null && (!AccessorMapping.ElementsMatch(existingMapping.Elements, mapping.Elements))) { // need to re-name the mapping uniqueName = name + i.ToString(); existingMapping = (ArrayMapping)types[uniqueName, ns]; i++; } mapping.TypeName = uniqueName; mapping.Namespace = ns; }
void WriteArray(string source, string arrayName, ArrayMapping arrayMapping, bool readOnly, bool isNullable, int fixupIndex) { if (arrayMapping.IsSoap) { writer.Write("object rre = "); if (fixupIndex >= 0) { writer.Write("ReadReferencingElement("); writer.Write("out fixup.Ids["); writer.Write((fixupIndex).ToString()); writer.Write("]"); } else writer.Write("ReadReferencedElement("); writer.WriteLine(");"); TypeDesc td = arrayMapping.TypeDesc; if (td.IsEnumerable || td.IsCollection) { writer.WriteLine("if (rre != null) {"); writer.Indent++; WriteAddCollectionFixup(td, readOnly, source, "rre"); writer.Indent--; writer.WriteLine("}"); } else { writer.WriteLine("try {"); writer.Indent++; WriteSourceBeginTyped(source, arrayMapping.TypeDesc); writer.Write("rre"); WriteSourceEnd(source); writer.WriteLine(";"); WriteCatchCastException(arrayMapping.TypeDesc, "rre"); } } else { writer.WriteLine("if (!ReadNull()) {"); writer.Indent++; MemberMapping memberMapping = new MemberMapping(); memberMapping.Elements = arrayMapping.Elements; memberMapping.TypeDesc = arrayMapping.TypeDesc; memberMapping.ReadOnly = readOnly; Member member = new Member(source, arrayName, 0, memberMapping, false); member.IsNullable = false;//Note, sowmys: IsNullable is set to false since null condition (xsi:nil) is already handled by 'ReadNull()' Member[] members = new Member[] { member }; WriteMemberBegin(members); writer.WriteLine("if (Reader.IsEmptyElement) {"); writer.Indent++; writer.WriteLine("Reader.Skip();"); writer.Indent--; writer.WriteLine("}"); writer.WriteLine("else {"); writer.Indent++; writer.WriteLine("Reader.ReadStartElement();"); WriteWhileNotEndTag(); writer.Indent++; WriteMemberElements(members, "UnknownNode(null);", "UnknownNode(null);", null, null, null); writer.WriteLine("Reader.MoveToContent();"); writer.Indent--; writer.WriteLine("}"); writer.Indent--; writer.WriteLine("ReadEndElement();"); writer.WriteLine("}"); WriteMemberEnd(members, false); writer.Indent--; writer.WriteLine("}"); if (isNullable) { writer.WriteLine("else {"); writer.Indent++; member.IsNullable = true; WriteMemberBegin(members); WriteMemberEnd(members); writer.Indent--; writer.WriteLine("}"); } } }
private void ExportArrayElements(CodeAttributeDeclarationCollection metadata, ArrayMapping array, string ns, TypeDesc elementTypeDesc, int nestingLevel) { for (int i = 0; i < array.Elements.Length; i++) { ElementAccessor accessor = array.Elements[i]; TypeMapping mapping = accessor.Mapping; string str = Accessor.UnescapeName(accessor.Name); bool flag = !accessor.Mapping.TypeDesc.IsArray && (str == accessor.Mapping.TypeName); bool flag2 = mapping.TypeDesc == elementTypeDesc; bool flag3 = (accessor.Form == XmlSchemaForm.Unqualified) || (accessor.Namespace == ns); bool flag4 = accessor.IsNullable == mapping.TypeDesc.IsNullable; bool flag5 = accessor.Form != XmlSchemaForm.Unqualified; if (((!flag || !flag2) || (!flag3 || !flag4)) || (!flag5 || (nestingLevel > 0))) { this.ExportArrayItem(metadata, flag ? null : str, flag3 ? null : accessor.Namespace, flag2 ? null : mapping.TypeDesc, mapping.TypeDesc, accessor.IsNullable, flag5 ? XmlSchemaForm.None : accessor.Form, nestingLevel); } if (mapping is ArrayMapping) { this.ExportArrayElements(metadata, (ArrayMapping)mapping, ns, elementTypeDesc.ArrayElementTypeDesc, nestingLevel + 1); } } }
private static bool ShouldInclude(ArrayMapping arrayMapping) { if (arrayMapping.ReferencedByElement) return false; if (arrayMapping.Next != null) return false; if (arrayMapping.Elements.Length == 1) { TypeKind kind = arrayMapping.Elements[0].Mapping.TypeDesc.Kind; if (kind == TypeKind.Node) return false; } for (int i = 0; i < arrayMapping.Elements.Length; i++) { if (arrayMapping.Elements[i].Name != arrayMapping.Elements[i].Mapping.DefaultElementName) { // in the case we need custom attributes to serialize an array instance, we cannot include arrau mapping without explicit reference. return false; } } return true; }
private static bool ShouldInclude(ArrayMapping arrayMapping) { if (arrayMapping.ReferencedByElement) { return false; } if (arrayMapping.Next != null) { return false; } if ((arrayMapping.Elements.Length == 1) && (arrayMapping.Elements[0].Mapping.TypeDesc.Kind == TypeKind.Node)) { return false; } for (int i = 0; i < arrayMapping.Elements.Length; i++) { if (arrayMapping.Elements[i].Name != arrayMapping.Elements[i].Mapping.DefaultElementName) { return false; } } return true; }
void SetArrayMappingType(ArrayMapping mapping) { bool useDefaultNs = false; string itemTypeName; string itemTypeNamespace; TypeMapping itemTypeMapping; if (mapping.Elements.Length == 1) itemTypeMapping = mapping.Elements[0].Mapping; else itemTypeMapping = null; if (itemTypeMapping is EnumMapping) { itemTypeNamespace = itemTypeMapping.Namespace; itemTypeName = itemTypeMapping.TypeName; } else if (itemTypeMapping is PrimitiveMapping) { itemTypeNamespace = itemTypeMapping.TypeDesc.IsXsdType ? XmlSchema.Namespace : UrtTypes.Namespace; itemTypeName = itemTypeMapping.TypeDesc.DataType.Name; useDefaultNs = true; } else if (itemTypeMapping is StructMapping) { if (itemTypeMapping.TypeDesc.IsRoot) { itemTypeNamespace = XmlSchema.Namespace; itemTypeName = Soap.UrType; useDefaultNs = true; } else { itemTypeNamespace = itemTypeMapping.Namespace; itemTypeName = itemTypeMapping.TypeName; } } else if (itemTypeMapping is ArrayMapping) { itemTypeNamespace = itemTypeMapping.Namespace; itemTypeName = itemTypeMapping.TypeName; } else { throw new InvalidOperationException(Res.GetString(Res.XmlInvalidSoapArray, mapping.TypeDesc.FullName)); } itemTypeName = CodeIdentifier.MakePascal(itemTypeName); string uniqueName = "ArrayOf" + itemTypeName; string ns = useDefaultNs ? defaultNs : itemTypeNamespace; int i = 1; TypeMapping existingMapping = (TypeMapping)types[uniqueName, ns]; while (existingMapping != null) { if (existingMapping is ArrayMapping) { ArrayMapping arrayMapping = (ArrayMapping)existingMapping; if (AccessorMapping.ElementsMatch(arrayMapping.Elements, mapping.Elements)) { break; } } // need to re-name the mapping uniqueName = itemTypeName + i.ToString(CultureInfo.InvariantCulture); existingMapping = (TypeMapping)types[uniqueName, ns]; i++; } mapping.Namespace = ns; mapping.TypeName = uniqueName; }
void SetArrayMappingType(ArrayMapping mapping) { bool useDefaultNs = false; string itemTypeName; string itemTypeNamespace; TypeMapping itemTypeMapping; if (mapping.Elements.Length == 1) itemTypeMapping = mapping.Elements[0].Mapping; else itemTypeMapping = null; if (itemTypeMapping is EnumMapping) { itemTypeNamespace = itemTypeMapping.Namespace; itemTypeName = itemTypeMapping.TypeName; } else if (itemTypeMapping is PrimitiveMapping) { itemTypeNamespace = itemTypeMapping.TypeDesc.IsXsdType ? XmlSchema.Namespace : UrtTypes.Namespace; itemTypeName = itemTypeMapping.TypeDesc.DataType.Name; useDefaultNs = true; } else if (itemTypeMapping is StructMapping) { if (itemTypeMapping.TypeDesc.IsRoot) { itemTypeNamespace = XmlSchema.Namespace; itemTypeName = Soap.UrType; useDefaultNs = true; } else { itemTypeNamespace = itemTypeMapping.Namespace; itemTypeName = itemTypeMapping.TypeName; } } else if (itemTypeMapping is ArrayMapping) { itemTypeNamespace = itemTypeMapping.Namespace; itemTypeName = itemTypeMapping.TypeName; } else { throw new InvalidOperationException(Res.GetString(Res.XmlInvalidSoapArray, mapping.TypeDesc.FullName)); } mapping.Namespace = useDefaultNs ? defaultNs : itemTypeNamespace; mapping.TypeName = "ArrayOf" + CodeIdentifier.MakePascal(itemTypeName); }