public void AddElementMemberAttributes(XmlTypeMapMemberElement member, string defaultNamespace, CodeAttributeDeclarationCollection attributes, bool forceUseMemberName) { TypeData defaultType = member.TypeData; bool addAlwaysAttr = false; if (member is XmlTypeMapMemberFlatList) { defaultType = defaultType.ListItemTypeData; addAlwaysAttr = true; } foreach (XmlTypeMapElementInfo einfo in member.ElementInfo) { if (einfo.MappedType != null) { ExportMapCode(einfo.MappedType, false); RemoveInclude(einfo.MappedType); } if (ExportExtraElementAttributes(attributes, einfo, defaultNamespace, defaultType)) { continue; } GenerateElementInfoMember(attributes, member, einfo, defaultType, defaultNamespace, addAlwaysAttr, forceUseMemberName | addAlwaysAttr); } GenerateElementMember(attributes, member); }
void AddAnyElementFieldMember(CodeTypeDeclaration codeClass, XmlTypeMapMemberElement member, string defaultNamespace) { CodeTypeMember codeField = CreateFieldMember(codeClass, member); CodeAttributeDeclarationCollection attributes = new CodeAttributeDeclarationCollection(); foreach (XmlTypeMapElementInfo einfo in member.ElementInfo) { ExportExtraElementAttributes(attributes, einfo, defaultNamespace, einfo.TypeData); } if (attributes.Count > 0) { codeField.CustomAttributes = attributes; } }
void AddElementFieldMember(CodeTypeDeclaration codeClass, XmlTypeMapMemberElement member, string defaultNamespace) { CodeTypeMember codeField = CreateFieldMember(codeClass, member); CodeAttributeDeclarationCollection attributes = codeField.CustomAttributes; if (attributes == null) { attributes = new CodeAttributeDeclarationCollection(); } AddElementMemberAttributes(member, defaultNamespace, attributes, false); if (attributes.Count > 0) { codeField.CustomAttributes = attributes; } if (member.TypeData.IsValueType && member.IsOptionalValueType) { codeField = CreateFieldMember(codeClass, typeof(bool), identifiers.MakeUnique(member.Name + "Specified")); codeField.Attributes = MemberAttributes.Public; GenerateSpecifierMember(codeField); } }
protected virtual void GenerateArrayElement(CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member, string defaultNamespace, bool forceUseMemberName) { }
protected virtual void GenerateElementMember(CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member) { }
protected virtual void GenerateElementInfoMember(CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member, XmlTypeMapElementInfo einfo, TypeData defaultType, string defaultNamespace, bool addAlwaysAttr, bool forceUseMemberName) { }
public void AddArrayAttributes(CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member, string defaultNamespace, bool forceUseMemberName) { GenerateArrayElement(attributes, member, defaultNamespace, forceUseMemberName); }
protected override void GenerateElementInfoMember(CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member, XmlTypeMapElementInfo einfo, TypeData defaultType, string defaultNamespace, bool addAlwaysAttr, bool forceUseMemberName) { CodeAttributeDeclaration att = new CodeAttributeDeclaration("Mono.System.Xml.Serialization.SoapElement"); if (forceUseMemberName || einfo.ElementName != member.Name) { att.Arguments.Add(GetArg(einfo.ElementName)); } // if (einfo.IsNullable) att.Arguments.Add (GetArg ("IsNullable", true)); MS seems to ignore this if (!TypeTranslator.IsDefaultPrimitiveTpeData(einfo.TypeData)) { att.Arguments.Add(GetArg("DataType", einfo.TypeData.XmlType)); } if (addAlwaysAttr || att.Arguments.Count > 0) { attributes.Add(att); } }
void ReadMembers(ClassMap map, object ob, bool isValueList, bool readBySoapOrder) { // Reads attributes ReadAttributeMembers(map, ob, isValueList); if (!isValueList) { Reader.MoveToElement(); if (Reader.IsEmptyElement) { SetListMembersDefaults(map, ob, isValueList); return; } Reader.ReadStartElement(); } // Reads elements bool[] readFlag = new bool[(map.ElementMembers != null) ? map.ElementMembers.Count : 0]; bool hasAnyReturnMember = (isValueList && _format == SerializationFormat.Encoded && map.ReturnMember != null); Reader.MoveToContent(); int[] indexes = null; object[] flatLists = null; object[] flatListsChoices = null; Fixup fixup = null; int ind = -1; int maxInd; if (readBySoapOrder) { if (map.ElementMembers != null) { maxInd = map.ElementMembers.Count; } else { maxInd = -1; } } else { maxInd = int.MaxValue; } if (map.FlatLists != null) { indexes = new int[map.FlatLists.Count]; flatLists = new object[map.FlatLists.Count]; foreach (XmlTypeMapMemberExpandable mem in map.FlatLists) { if (IsReadOnly(mem, mem.TypeData, ob, isValueList)) { flatLists [mem.FlatArrayIndex] = mem.GetValue(ob); } else if (mem.TypeData.Type.IsArray) { flatLists [mem.FlatArrayIndex] = InitializeList(mem.TypeData); } else { object list = mem.GetValue(ob); if (list == null) { list = InitializeList(mem.TypeData); SetMemberValue(mem, ob, list, isValueList); } flatLists [mem.FlatArrayIndex] = list; } if (mem.ChoiceMember != null) { if (flatListsChoices == null) { flatListsChoices = new object [map.FlatLists.Count]; } flatListsChoices [mem.FlatArrayIndex] = InitializeList(mem.ChoiceTypeData); } } } if (_format == SerializationFormat.Encoded && map.ElementMembers != null) { FixupCallbackInfo info = new FixupCallbackInfo(this, map, isValueList); fixup = new Fixup(ob, new XmlSerializationFixupCallback(info.FixupMembers), map.ElementMembers.Count); AddFixup(fixup); } XmlTypeMapMember previousMember = null; while (Reader.NodeType != Mono.System.Xml.XmlNodeType.EndElement && (ind < maxInd - 1)) { if (Reader.NodeType == Mono.System.Xml.XmlNodeType.Element) { XmlTypeMapElementInfo info; if (readBySoapOrder) { info = map.GetElement(Reader.LocalName, Reader.NamespaceURI, ind); } else if (hasAnyReturnMember) { info = (XmlTypeMapElementInfo)((XmlTypeMapMemberElement)map.ReturnMember).ElementInfo[0]; hasAnyReturnMember = false; } else { if (map.IsOrderDependentMap) { info = map.GetElement(Reader.LocalName, Reader.NamespaceURI, ind); } else { info = map.GetElement(Reader.LocalName, Reader.NamespaceURI); } } if (info != null && !readFlag[info.Member.Index]) { if (info.Member != previousMember) { ind = info.ExplicitOrder + 1; // If the member is a flat list don't increase the index, since the next element may // be another item of the list. This is a fix for Xamarin bug #9193. if (info.Member is XmlTypeMapMemberFlatList) { ind--; } previousMember = info.Member; } if (info.Member.GetType() == typeof(XmlTypeMapMemberList)) { if (_format == SerializationFormat.Encoded && info.MultiReferenceType) { object list = ReadReferencingElement(out fixup.Ids[info.Member.Index]); if (fixup.Ids[info.Member.Index] == null) // Already read { if (IsReadOnly(info.Member, info.TypeData, ob, isValueList)) { throw CreateReadOnlyCollectionException(info.TypeData.FullTypeName); } else { SetMemberValue(info.Member, ob, list, isValueList); } } else if (!info.MappedType.TypeData.Type.IsArray) { if (IsReadOnly(info.Member, info.TypeData, ob, isValueList)) { list = GetMemberValue(info.Member, ob, isValueList); } else { list = CreateList(info.MappedType.TypeData.Type); SetMemberValue(info.Member, ob, list, isValueList); } AddFixup(new CollectionFixup(list, new XmlSerializationCollectionFixupCallback(FillList), fixup.Ids[info.Member.Index])); fixup.Ids[info.Member.Index] = null; // The member already has the value, no further fix needed. } } else { if (IsReadOnly(info.Member, info.TypeData, ob, isValueList)) { ReadListElement(info.MappedType, info.IsNullable, GetMemberValue(info.Member, ob, isValueList), false); } else if (info.MappedType.TypeData.Type.IsArray) { object list = ReadListElement(info.MappedType, info.IsNullable, null, true); if (list != null || info.IsNullable) { SetMemberValue(info.Member, ob, list, isValueList); } } else { // If the member already has a list, reuse that list. No need to create a new one. object list = GetMemberValue(info.Member, ob, isValueList); if (list == null) { list = CreateList(info.MappedType.TypeData.Type); SetMemberValue(info.Member, ob, list, isValueList); } ReadListElement(info.MappedType, info.IsNullable, list, true); } } readFlag[info.Member.Index] = true; } else if (info.Member.GetType() == typeof(XmlTypeMapMemberFlatList)) { XmlTypeMapMemberFlatList mem = (XmlTypeMapMemberFlatList)info.Member; AddListValue(mem.TypeData, ref flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]++, ReadObjectElement(info), !IsReadOnly(info.Member, info.TypeData, ob, isValueList)); if (mem.ChoiceMember != null) { AddListValue(mem.ChoiceTypeData, ref flatListsChoices [mem.FlatArrayIndex], indexes[mem.FlatArrayIndex] - 1, info.ChoiceValue, true); } } else if (info.Member.GetType() == typeof(XmlTypeMapMemberAnyElement)) { XmlTypeMapMemberAnyElement mem = (XmlTypeMapMemberAnyElement)info.Member; if (mem.TypeData.IsListType) { AddListValue(mem.TypeData, ref flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]++, ReadXmlNode(mem.TypeData.ListItemTypeData, false), true); } else { SetMemberValue(mem, ob, ReadXmlNode(mem.TypeData, false), isValueList); } } else if (info.Member.GetType() == typeof(XmlTypeMapMemberElement)) { object val; readFlag[info.Member.Index] = true; if (_format == SerializationFormat.Encoded) { if (info.Member.TypeData.SchemaType != SchemaTypes.Primitive) { val = ReadReferencingElement(out fixup.Ids[info.Member.Index]); } else { val = ReadReferencingElement(info.Member.TypeData.XmlType, Mono.System.Xml.Schema.XmlSchema.Namespace, out fixup.Ids[info.Member.Index]); } if (info.MultiReferenceType) { if (fixup.Ids[info.Member.Index] == null) // already read { SetMemberValue(info.Member, ob, val, isValueList); } } else if (val != null) { SetMemberValue(info.Member, ob, val, isValueList); } } else { SetMemberValue(info.Member, ob, ReadObjectElement(info), isValueList); if (info.ChoiceValue != null) { XmlTypeMapMemberElement imem = (XmlTypeMapMemberElement)info.Member; imem.SetChoice(ob, info.ChoiceValue); } } } else { throw new InvalidOperationException("Unknown member type"); } } else if (map.DefaultAnyElementMember != null) { XmlTypeMapMemberAnyElement mem = map.DefaultAnyElementMember; if (mem.TypeData.IsListType) { AddListValue(mem.TypeData, ref flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]++, ReadXmlNode(mem.TypeData.ListItemTypeData, false), true); } else { SetMemberValue(mem, ob, ReadXmlNode(mem.TypeData, false), isValueList); } } else { ProcessUnknownElement(ob); } } else if ((Reader.NodeType == Mono.System.Xml.XmlNodeType.Text || Reader.NodeType == Mono.System.Xml.XmlNodeType.CDATA) && map.XmlTextCollector != null) { if (map.XmlTextCollector is XmlTypeMapMemberExpandable) { XmlTypeMapMemberExpandable mem = (XmlTypeMapMemberExpandable)map.XmlTextCollector; XmlTypeMapMemberFlatList flatl = mem as XmlTypeMapMemberFlatList; TypeData itype = (flatl == null) ? mem.TypeData.ListItemTypeData : flatl.ListMap.FindTextElement().TypeData; object val = (itype.Type == typeof(string)) ? (object)Reader.ReadString() : (object)ReadXmlNode(itype, false); AddListValue(mem.TypeData, ref flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]++, val, true); } else { XmlTypeMapMemberElement mem = (XmlTypeMapMemberElement)map.XmlTextCollector; XmlTypeMapElementInfo info = (XmlTypeMapElementInfo)mem.ElementInfo [0]; if (info.TypeData.Type == typeof(string)) { SetMemberValue(mem, ob, ReadString((string)GetMemberValue(mem, ob, isValueList)), isValueList); } else { SetMemberValue(mem, ob, GetValueFromXmlString(Reader.ReadString(), info.TypeData, info.MappedType), isValueList); } } } else { UnknownNode(ob); } Reader.MoveToContent(); } if (flatLists != null) { foreach (XmlTypeMapMemberExpandable mem in map.FlatLists) { Object list = flatLists[mem.FlatArrayIndex]; if (mem.TypeData.Type.IsArray) { list = ShrinkArray((Array)list, indexes[mem.FlatArrayIndex], mem.TypeData.Type.GetElementType(), true); } if (!IsReadOnly(mem, mem.TypeData, ob, isValueList) && mem.TypeData.Type.IsArray) { SetMemberValue(mem, ob, list, isValueList); } } } if (flatListsChoices != null) { foreach (XmlTypeMapMemberExpandable mem in map.FlatLists) { Object list = flatListsChoices[mem.FlatArrayIndex]; if (list == null) { continue; } list = ShrinkArray((Array)list, indexes[mem.FlatArrayIndex], mem.ChoiceTypeData.Type.GetElementType(), true); XmlTypeMapMember.SetValue(ob, mem.ChoiceMember, list); } } SetListMembersDefaults(map, ob, isValueList); }
protected override void GenerateArrayElement(CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member, string defaultNamespace, bool forceUseMemberName) { XmlTypeMapElementInfo einfo = (XmlTypeMapElementInfo)member.ElementInfo[0]; CodeAttributeDeclaration att = new CodeAttributeDeclaration("Mono.System.Xml.Serialization.XmlArray"); if (forceUseMemberName || (einfo.ElementName != member.Name)) { att.Arguments.Add(GetArg("ElementName", einfo.ElementName)); } if (einfo.Namespace != defaultNamespace) { att.Arguments.Add(GetArg("Namespace", einfo.Namespace)); } if (einfo.Form == XmlSchemaForm.Unqualified) { att.Arguments.Add(MapCodeGenerator.GetEnumArg("Form", "Mono.System.Xml.Schema.XmlSchemaForm", einfo.Form.ToString())); } if (einfo.IsNullable) { att.Arguments.Add(GetArg("IsNullable", true)); } if (att.Arguments.Count > 0) { attributes.Add(att); } }
protected override void GenerateElementMember(CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member) { if (member.ChoiceMember != null) { CodeAttributeDeclaration att = new CodeAttributeDeclaration("Mono.System.Xml.Serialization.XmlChoiceIdentifier"); att.Arguments.Add(GetArg(member.ChoiceMember)); attributes.Add(att); } if (member.Ignore) { attributes.Add(new CodeAttributeDeclaration("Mono.System.Xml.Serialization.XmlIgnoreAttribute")); } }
protected override void GenerateElementInfoMember(CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member, XmlTypeMapElementInfo einfo, TypeData defaultType, string defaultNamespace, bool addAlwaysAttr, bool forceUseMemberName) { CodeAttributeDeclaration att = new CodeAttributeDeclaration("Mono.System.Xml.Serialization.XmlElementAttribute"); if (forceUseMemberName || einfo.ElementName != member.Name) { att.Arguments.Add(GetArg(einfo.ElementName)); } if (einfo.TypeData.FullTypeName != defaultType.FullTypeName) { att.Arguments.Add(GetTypeArg("Type", einfo.TypeData.FullTypeName)); } if (einfo.Namespace != defaultNamespace) { att.Arguments.Add(GetArg("Namespace", einfo.Namespace)); } if (einfo.Form == XmlSchemaForm.Unqualified) { att.Arguments.Add(GetEnumArg("Form", "Mono.System.Xml.Schema.XmlSchemaForm", einfo.Form.ToString())); } if (einfo.IsNullable) { att.Arguments.Add(GetArg("IsNullable", true)); } if (!TypeTranslator.IsDefaultPrimitiveTpeData(einfo.TypeData)) { att.Arguments.Add(GetArg("DataType", einfo.TypeData.XmlType)); } if (addAlwaysAttr || att.Arguments.Count > 0) { attributes.Add(att); } }
private XmlTypeMapMember CreateMapMember(XmlReflectionMember rmember, string defaultNamespace) { XmlTypeMapMember mapMember; SoapAttributes atts = rmember.SoapAttributes; TypeData typeData = TypeTranslator.GetTypeData(rmember.MemberType); if (atts.SoapAttribute != null) { // An attribute if (typeData.SchemaType != SchemaTypes.Enum && typeData.SchemaType != SchemaTypes.Primitive) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Cannot serialize member '{0}' of type {1}. " + "SoapAttribute cannot be used to encode complex types.", rmember.MemberName, typeData.FullTypeName)); } if (atts.SoapElement != null) { throw new Exception("SoapAttributeAttribute and SoapElementAttribute cannot be applied to the same member"); } XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute(); if (atts.SoapAttribute.AttributeName.Length == 0) { mapAttribute.AttributeName = XmlConvert.EncodeLocalName(rmember.MemberName); } else { mapAttribute.AttributeName = XmlConvert.EncodeLocalName(atts.SoapAttribute.AttributeName); } mapAttribute.Namespace = (atts.SoapAttribute.Namespace != null) ? atts.SoapAttribute.Namespace : ""; if (typeData.IsComplexType) { mapAttribute.MappedType = ImportTypeMapping(typeData.Type, defaultNamespace); } typeData = TypeTranslator.GetTypeData(rmember.MemberType, atts.SoapAttribute.DataType); mapMember = mapAttribute; mapMember.DefaultValue = GetDefaultValue(typeData, atts.SoapDefaultValue); } else { if (typeData.SchemaType == SchemaTypes.Array) { mapMember = new XmlTypeMapMemberList(); } else { mapMember = new XmlTypeMapMemberElement(); } if (atts.SoapElement != null && atts.SoapElement.DataType.Length != 0) { typeData = TypeTranslator.GetTypeData(rmember.MemberType, atts.SoapElement.DataType); } // Creates an ElementInfo that identifies the element XmlTypeMapElementInfoList infoList = new XmlTypeMapElementInfoList(); XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo(mapMember, typeData); elem.ElementName = XmlConvert.EncodeLocalName((atts.SoapElement != null && atts.SoapElement.ElementName.Length != 0) ? atts.SoapElement.ElementName : rmember.MemberName); elem.Namespace = string.Empty; elem.IsNullable = (atts.SoapElement != null) ? atts.SoapElement.IsNullable : false; if (typeData.IsComplexType) { elem.MappedType = ImportTypeMapping(typeData.Type, defaultNamespace); } infoList.Add(elem); ((XmlTypeMapMemberElement)mapMember).ElementInfo = infoList; } mapMember.TypeData = typeData; mapMember.Name = rmember.MemberName; mapMember.IsReturnValue = rmember.IsReturnValue; return(mapMember); }