void ExportEnumSchema(XmlTypeMapping map) { if (IsMapExported(map)) { return; } SetMapExported(map); XmlSchema schema = GetSchema(map.XmlTypeNamespace); XmlSchemaSimpleType stype = new XmlSchemaSimpleType(); stype.Name = map.ElementName; schema.Items.Add(stype); XmlSchemaSimpleTypeRestriction rest = new XmlSchemaSimpleTypeRestriction(); rest.BaseTypeName = new XmlQualifiedName("string", XmlSchema.Namespace); EnumMap emap = (EnumMap)map.ObjectMap; foreach (EnumMap.EnumMapMember emem in emap.Members) { XmlSchemaEnumerationFacet ef = new XmlSchemaEnumerationFacet(); ef.Value = emem.XmlName; rest.Facets.Add(ef); } if (emap.IsFlags) { XmlSchemaSimpleTypeList slist = new XmlSchemaSimpleTypeList(); XmlSchemaSimpleType restrictionType = new XmlSchemaSimpleType(); restrictionType.Content = rest; slist.ItemType = restrictionType; stype.Content = slist; } else { stype.Content = rest; } }
protected override void GenerateClass(XmlTypeMapping map, CodeTypeDeclaration codeClass, bool isTopLevel) { CodeAttributeDeclaration att = new CodeAttributeDeclaration("Mono.System.Xml.Serialization.XmlTypeAttribute"); if (map.XmlType != map.TypeData.TypeName) { att.Arguments.Add(GetArg(map.XmlType)); } if (map.XmlTypeNamespace != "") { att.Arguments.Add(GetArg("Namespace", map.XmlTypeNamespace)); } if (!map.IncludeInSchema) { att.Arguments.Add(GetArg("IncludeInSchema", false)); } AddCustomAttribute(codeClass, att, false); CodeAttributeDeclaration ratt = new CodeAttributeDeclaration("Mono.System.Xml.Serialization.XmlRootAttribute"); if (map.ElementName != map.XmlType) { ratt.Arguments.Add(GetArg(map.ElementName)); } if (isTopLevel) { ratt.Arguments.Add(GetArg("Namespace", map.Namespace)); ratt.Arguments.Add(GetArg("IsNullable", map.IsNullable)); } else { if (map.Namespace != "") { ratt.Arguments.Add(GetArg("Namespace", map.Namespace)); } } AddCustomAttribute(codeClass, ratt, isTopLevel); }
XmlTypeMapping ImportEnumMapping(TypeData typeData, string defaultNamespace) { Type type = typeData.Type; XmlTypeMapping map = helper.GetRegisteredClrType(type, GetTypeNamespace(typeData, defaultNamespace)); if (map != null) { return(map); } ReflectionHelper.CheckSerializableType(type, false); map = CreateTypeMapping(typeData, null, defaultNamespace); helper.RegisterClrType(map, type, map.Namespace); map.MultiReferenceType = true; string [] names = Enum.GetNames(type); EnumMap.EnumMapMember[] members = new EnumMap.EnumMapMember[names.Length]; for (int n = 0; n < names.Length; n++) { FieldInfo field = type.GetField(names[n]); string xmlName = names[n]; object[] atts = field.GetCustomAttributes(typeof(SoapEnumAttribute), false); if (atts.Length > 0) { xmlName = ((SoapEnumAttribute)atts[0]).Name; } long value = ((IConvertible)field.GetValue(null)).ToInt64(CultureInfo.InvariantCulture); members[n] = new EnumMap.EnumMapMember(XmlConvert.EncodeLocalName(xmlName), names[n], value); } bool isFlags = type.IsDefined(typeof(FlagsAttribute), false); map.ObjectMap = new EnumMap(members, isFlags); ImportTypeMapping(typeof(object), defaultNamespace).DerivedTypes.Add(map); return(map); }
internal XmlTypeMapping GetRealTypeMap(Type objectType) { if (TypeData.SchemaType == SchemaTypes.Enum) { return(this); } // Returns the map for a subtype of this map's type if (TypeData.Type == objectType) { return(this); } for (int n = 0; n < _derivedTypes.Count; n++) { XmlTypeMapping map = (XmlTypeMapping)_derivedTypes[n]; if (map.TypeData.Type == objectType) { return(map); } } return(null); }
void ExportMapCode(XmlTypeMapping map, bool isTopLevel) { switch (map.TypeData.SchemaType) { case SchemaTypes.Enum: ExportEnumCode(map, isTopLevel); break; case SchemaTypes.Array: ExportArrayCode(map); break; case SchemaTypes.Class: ExportClassCode(map, isTopLevel); break; case SchemaTypes.XmlSerializable: case SchemaTypes.XmlNode: case SchemaTypes.Primitive: // Ignore break; } }
object ReadListString(XmlTypeMapping typeMap, string values) { Type listType = typeMap.TypeData.Type; ListMap listMap = (ListMap)typeMap.ObjectMap; values = values.Trim(); if (values == string.Empty) { return(Array.CreateInstance(listType.GetElementType(), 0)); } string[] valueArray = values.Split(' '); Array list = Array.CreateInstance(listType.GetElementType(), valueArray.Length); XmlTypeMapElementInfo info = (XmlTypeMapElementInfo)listMap.ItemInfo[0]; for (int index = 0; index < valueArray.Length; index++) { list.SetValue(GetValueFromXmlString(valueArray[index], info.TypeData, info.MappedType), index); } return(list); }
object ReadEncodedObject(XmlTypeMapping typeMap) { object ob = null; Reader.MoveToContent(); if (Reader.NodeType == Mono.System.Xml.XmlNodeType.Element) { if (Reader.LocalName == typeMap.ElementName && Reader.NamespaceURI == typeMap.Namespace) { ob = ReadReferencedElement(); } else { throw CreateUnknownNodeException(); } } else { UnknownNode(null); } ReadReferencedElements(); return(ob); }
void ExportMembersMapSchema(XmlSchema schema, ClassMap map, XmlTypeMapping baseMap, XmlSchemaObjectCollection outAttributes, out XmlSchemaSequence particle, out XmlSchemaAnyAttribute anyAttribute) { particle = null; XmlSchemaSequence seq = new XmlSchemaSequence(); ICollection members = map.ElementMembers; if (members != null && !map.HasSimpleContent) { foreach (XmlTypeMapMemberElement member in members) { if (baseMap != null && DefinedInBaseMap(baseMap, member)) { continue; } Type memType = member.GetType(); if (memType == typeof(XmlTypeMapMemberFlatList)) { XmlSchemaParticle part = GetSchemaArrayElement(schema, member.ElementInfo); if (part != null) { seq.Items.Add(part); } } else if (memType == typeof(XmlTypeMapMemberAnyElement)) { seq.Items.Add(GetSchemaArrayElement(schema, member.ElementInfo)); } else if (memType == typeof(XmlTypeMapMemberElement)) { GetSchemaElement(schema, (XmlTypeMapElementInfo)member.ElementInfo [0], member.DefaultValue, true, new XmlSchemaObjectContainer(seq)); } else { GetSchemaElement(schema, (XmlTypeMapElementInfo)member.ElementInfo[0], true, new XmlSchemaObjectContainer(seq)); } } } if (seq.Items.Count > 0) { particle = seq; } // Write attributes ICollection attributes = map.AttributeMembers; if (attributes != null) { foreach (XmlTypeMapMemberAttribute attr in attributes) { if (baseMap != null && DefinedInBaseMap(baseMap, attr)) { continue; } outAttributes.Add(GetSchemaAttribute(schema, attr, true)); } } XmlTypeMapMember anyAttrMember = map.DefaultAnyAttributeMember; if (anyAttrMember != null) { anyAttribute = new XmlSchemaAnyAttribute(); } else { anyAttribute = null; } }
protected virtual void WriteEnumElement(XmlTypeMapping typeMap, object ob, string element, string namesp) { Writer.WriteString(GetEnumXmlValue(typeMap, ob)); }
void ExportClassSchema(XmlTypeMapping map) { if (IsMapExported(map)) { return; } SetMapExported(map); if (map.TypeData.Type == typeof(object)) { foreach (XmlTypeMapping dmap in map.DerivedTypes) { if (dmap.TypeData.SchemaType == SchemaTypes.Class) { ExportClassSchema(dmap); } } return; } XmlSchema schema = GetSchema(map.XmlTypeNamespace); XmlSchemaComplexType stype = new XmlSchemaComplexType(); stype.Name = map.XmlType; schema.Items.Add(stype); ClassMap cmap = (ClassMap)map.ObjectMap; if (cmap.HasSimpleContent) { XmlSchemaSimpleContent simple = new XmlSchemaSimpleContent(); stype.ContentModel = simple; XmlSchemaSimpleContentExtension ext = new XmlSchemaSimpleContentExtension(); simple.Content = ext; XmlSchemaSequence particle; XmlSchemaAnyAttribute anyAttribute; ExportMembersMapSchema(schema, cmap, map.BaseMap, ext.Attributes, out particle, out anyAttribute); ext.AnyAttribute = anyAttribute; if (map.BaseMap == null) { ext.BaseTypeName = cmap.SimpleContentBaseType; } else { ext.BaseTypeName = new XmlQualifiedName(map.BaseMap.XmlType, map.BaseMap.XmlTypeNamespace); ImportNamespace(schema, map.BaseMap.XmlTypeNamespace); ExportClassSchema(map.BaseMap); } } else if (map.BaseMap != null && map.BaseMap.IncludeInSchema) { XmlSchemaComplexContent cstype = new XmlSchemaComplexContent(); XmlSchemaComplexContentExtension ext = new XmlSchemaComplexContentExtension(); ext.BaseTypeName = new XmlQualifiedName(map.BaseMap.XmlType, map.BaseMap.XmlTypeNamespace); cstype.Content = ext; stype.ContentModel = cstype; XmlSchemaSequence particle; XmlSchemaAnyAttribute anyAttribute; ExportMembersMapSchema(schema, cmap, map.BaseMap, ext.Attributes, out particle, out anyAttribute); ext.Particle = particle; ext.AnyAttribute = anyAttribute; stype.IsMixed = HasMixedContent(map); cstype.IsMixed = BaseHasMixedContent(map); ImportNamespace(schema, map.BaseMap.XmlTypeNamespace); ExportClassSchema(map.BaseMap); } else { XmlSchemaSequence particle; XmlSchemaAnyAttribute anyAttribute; ExportMembersMapSchema(schema, cmap, map.BaseMap, stype.Attributes, out particle, out anyAttribute); stype.Particle = particle; stype.AnyAttribute = anyAttribute; stype.IsMixed = cmap.XmlTextCollector != null; } foreach (XmlTypeMapping dmap in map.DerivedTypes) { if (dmap.TypeData.SchemaType == SchemaTypes.Class) { ExportClassSchema(dmap); } } }
bool HasMixedContent(XmlTypeMapping map) { ClassMap cmap = (ClassMap)map.ObjectMap; return(cmap.XmlTextCollector != null && (map.BaseMap == null || !DefinedInBaseMap(map.BaseMap, cmap.XmlTextCollector))); }
protected virtual void GenerateClassInclude(CodeAttributeDeclarationCollection attributes, XmlTypeMapping map) { }
protected virtual void WriteObject(XmlTypeMapping typeMap, object ob, string element, string namesp, bool isNullable, bool needType, bool writeWrappingElem) { if (ob == null) { if (isNullable) { if (_format == SerializationFormat.Literal) { WriteNullTagLiteral(element, namesp); } else { WriteNullTagEncoded(element, namesp); } } return; } if (ob is XmlNode) { if (_format == SerializationFormat.Literal) { WriteElementLiteral((XmlNode)ob, "", "", true, false); } else { WriteElementEncoded((XmlNode)ob, "", "", true, false); } return; } if (typeMap.TypeData.SchemaType == SchemaTypes.XmlSerializable) { WriteSerializable((IXmlSerializable)ob, element, namesp, isNullable); return; } XmlTypeMapping map = typeMap.GetRealTypeMap(ob.GetType()); if (map == null) { // bug #81539 if (ob.GetType().IsArray&& typeof(XmlNode).IsAssignableFrom(ob.GetType().GetElementType())) { Writer.WriteStartElement(element, namesp); foreach (XmlNode node in (IEnumerable)ob) { node.WriteTo(Writer); } Writer.WriteEndElement(); } else { WriteTypedPrimitive(element, namesp, ob, true); } return; } if (writeWrappingElem) { if (map != typeMap || _format == SerializationFormat.Encoded) { needType = true; } WriteStartElement(element, namesp, ob); } if (needType) { WriteXsiType(map.XmlType, map.XmlTypeNamespace); } switch (map.TypeData.SchemaType) { case SchemaTypes.Class: WriteObjectElement(map, ob, element, namesp); break; case SchemaTypes.Array: WriteListElement(map, ob, element, namesp); break; case SchemaTypes.Primitive: WritePrimitiveElement(map, ob, element, namesp); break; case SchemaTypes.Enum: WriteEnumElement(map, ob, element, namesp); break; } if (writeWrappingElem) { WriteEndElement(ob); } }
void ExportMembersMapCode(CodeTypeDeclaration codeClass, ClassMap map, string defaultNamespace, XmlTypeMapping baseMap) { ICollection attributes = map.AttributeMembers; ICollection members = map.ElementMembers; // collect names if (attributes != null) { foreach (XmlTypeMapMemberAttribute attr in attributes) { identifiers.AddUnique(attr.Name, attr); } } if (members != null) { foreach (XmlTypeMapMemberElement member in members) { identifiers.AddUnique(member.Name, member); } } // Write attributes if (attributes != null) { foreach (XmlTypeMapMemberAttribute attr in attributes) { if (baseMap != null && DefinedInBaseMap(baseMap, attr)) { continue; } AddAttributeFieldMember(codeClass, attr, defaultNamespace); } } members = map.ElementMembers; if (members != null) { foreach (XmlTypeMapMemberElement member in members) { if (baseMap != null && DefinedInBaseMap(baseMap, member)) { continue; } Type memType = member.GetType(); if (memType == typeof(XmlTypeMapMemberList)) { AddArrayElementFieldMember(codeClass, (XmlTypeMapMemberList)member, defaultNamespace); } else if (memType == typeof(XmlTypeMapMemberFlatList)) { AddElementFieldMember(codeClass, member, defaultNamespace); } else if (memType == typeof(XmlTypeMapMemberAnyElement)) { AddAnyElementFieldMember(codeClass, member, defaultNamespace); } else if (memType == typeof(XmlTypeMapMemberElement)) { AddElementFieldMember(codeClass, member, defaultNamespace); } else { throw new InvalidOperationException("Member type " + memType + " not supported"); } } } XmlTypeMapMember anyAttrMember = map.DefaultAnyAttributeMember; if (anyAttrMember != null) { CodeTypeMember codeField = CreateFieldMember(codeClass, anyAttrMember.TypeData, anyAttrMember.Name); AddComments(codeField, anyAttrMember.Documentation); codeField.Attributes = MemberAttributes.Public; GenerateAnyAttribute(codeField); } }
void SetMapExported(XmlTypeMapping map, CodeTypeDeclaration declaration) { exportedMaps.Add(map.TypeData.FullTypeName, declaration); }
object ReadListElement(XmlTypeMapping typeMap, bool isNullable, object list, bool canCreateInstance) { Type listType = typeMap.TypeData.Type; ListMap listMap = (ListMap)typeMap.ObjectMap; if (listType.IsArray && ReadNull()) { return(null); } if (list == null) { if (canCreateInstance && typeMap.TypeData.HasPublicConstructor) { list = CreateList(listType); } else { throw CreateReadOnlyCollectionException(typeMap.TypeFullName); } } if (Reader.IsEmptyElement) { Reader.Skip(); if (listType.IsArray) { list = ShrinkArray((Array)list, 0, listType.GetElementType(), false); } return(list); } int index = 0; Reader.ReadStartElement(); Reader.MoveToContent(); while (Reader.NodeType != Mono.System.Xml.XmlNodeType.EndElement) { if (Reader.NodeType == Mono.System.Xml.XmlNodeType.Element) { XmlTypeMapElementInfo elemInfo = listMap.FindElement(Reader.LocalName, Reader.NamespaceURI); if (elemInfo != null) { AddListValue(typeMap.TypeData, ref list, index++, ReadObjectElement(elemInfo), false); } else { UnknownNode(null); } } else { UnknownNode(null); } Reader.MoveToContent(); } ReadEndElement(); if (listType.IsArray) { list = ShrinkArray((Array)list, index, listType.GetElementType(), false); } return(list); }
XmlQualifiedName ExportArraySchema(XmlTypeMapping map, string defaultNamespace) { ListMap lmap = (ListMap)map.ObjectMap; if (encodedFormat) { string name, ns, schemaNs; lmap.GetArrayType(-1, out name, out ns); if (ns == XmlSchema.Namespace) { schemaNs = defaultNamespace; } else { schemaNs = ns; } if (IsMapExported(map)) { return(new XmlQualifiedName(lmap.GetSchemaArrayName(), schemaNs)); } SetMapExported(map); XmlSchema schema = GetSchema(schemaNs); XmlSchemaComplexType stype = new XmlSchemaComplexType(); stype.Name = lmap.GetSchemaArrayName(); schema.Items.Add(stype); XmlSchemaComplexContent content = new XmlSchemaComplexContent(); content.IsMixed = false; stype.ContentModel = content; XmlSchemaComplexContentRestriction rest = new XmlSchemaComplexContentRestriction(); content.Content = rest; rest.BaseTypeName = new XmlQualifiedName("Array", XmlSerializer.EncodingNamespace); XmlSchemaAttribute at = new XmlSchemaAttribute(); rest.Attributes.Add(at); at.RefName = new XmlQualifiedName("arrayType", XmlSerializer.EncodingNamespace); XmlAttribute arrayType = Document.CreateAttribute("arrayType", XmlSerializer.WsdlNamespace); arrayType.Value = ns + (ns != "" ? ":" : "") + name; at.UnhandledAttributes = new XmlAttribute [] { arrayType }; ImportNamespace(schema, XmlSerializer.WsdlNamespace); XmlTypeMapElementInfo einfo = (XmlTypeMapElementInfo)lmap.ItemInfo[0]; if (einfo.MappedType != null) { switch (einfo.TypeData.SchemaType) { case SchemaTypes.Enum: ExportEnumSchema(einfo.MappedType); break; case SchemaTypes.Array: ExportArraySchema(einfo.MappedType, schemaNs); break; case SchemaTypes.Class: ExportClassSchema(einfo.MappedType); break; } } return(new XmlQualifiedName(lmap.GetSchemaArrayName(), schemaNs)); } else { if (IsMapExported(map)) { return(new XmlQualifiedName(map.XmlType, map.XmlTypeNamespace)); } SetMapExported(map); XmlSchema schema = GetSchema(map.XmlTypeNamespace); XmlSchemaComplexType stype = new XmlSchemaComplexType(); stype.Name = map.ElementName; schema.Items.Add(stype); XmlSchemaParticle spart = GetSchemaArrayElement(schema, lmap.ItemInfo); if (spart is XmlSchemaChoice) { stype.Particle = spart; } else { XmlSchemaSequence seq = new XmlSchemaSequence(); seq.Items.Add(spart); stype.Particle = seq; } return(new XmlQualifiedName(map.XmlType, map.XmlTypeNamespace)); } }
protected virtual void WritePrimitiveElement(XmlTypeMapping typeMap, object ob, string element, string namesp) { Writer.WriteString(GetStringValue(typeMap, typeMap.TypeData, ob)); }
void WritePrimitiveValueEncoded(object memberValue, string name, string ns, XmlQualifiedName xsiType, XmlTypeMapping mappedType, TypeData typeData, bool wrapped, bool isNullable) { if (!wrapped) { WriteValue(GetStringValue(mappedType, typeData, memberValue)); } else if (isNullable) { if (typeData.Type == typeof(XmlQualifiedName)) { WriteNullableQualifiedNameEncoded(name, ns, (XmlQualifiedName)memberValue, xsiType); } else { WriteNullableStringEncoded(name, ns, GetStringValue(mappedType, typeData, memberValue), xsiType); } } else { if (typeData.Type == typeof(XmlQualifiedName)) { WriteElementQualifiedName(name, ns, (XmlQualifiedName)memberValue, xsiType); } else { WriteElementString(name, ns, GetStringValue(mappedType, typeData, memberValue), xsiType); } } }
protected virtual void WriteObjectElementElements(XmlTypeMapping typeMap, object ob) { ClassMap map = (ClassMap)typeMap.ObjectMap; WriteElementMembers(map, ob, false); }
public void ExportTypeMapping(XmlTypeMapping xmlTypeMapping, bool isTopLevel) { ExportMapCode(xmlTypeMapping, isTopLevel); RemoveInclude(xmlTypeMapping); }
public CallbackInfo(XmlSerializationWriterInterpreter swi, XmlTypeMapping typeMap) { _swi = swi; _typeMap = typeMap; }
void ExportClassCode(XmlTypeMapping map, bool isTopLevel) { CodeTypeDeclaration codeClass; if (IsMapExported(map)) { codeClass = GetMapDeclaration(map); if (codeClass != null) { // Regenerate attributes, since things may have changed codeClass.CustomAttributes.Clear(); #if NET_2_0 AddClassAttributes(codeClass); #endif GenerateClass(map, codeClass, isTopLevel); ExportDerivedTypeAttributes(map, codeClass); } return; } if (map.TypeData.Type == typeof(object)) { exportedAnyType = map; SetMapExported(map, null); foreach (XmlTypeMapping dmap in exportedAnyType.DerivedTypes) { if (IsMapExported(dmap) || !dmap.IncludeInSchema) { continue; } ExportTypeMapping(dmap, false); AddInclude(dmap); } return; } codeClass = new CodeTypeDeclaration(map.TypeData.TypeName); SetMapExported(map, codeClass); AddCodeType(codeClass, map.Documentation); codeClass.Attributes = MemberAttributes.Public; #if NET_2_0 codeClass.IsPartial = CodeProvider.Supports(GeneratorSupport.PartialTypes); AddClassAttributes(codeClass); #endif GenerateClass(map, codeClass, isTopLevel); ExportDerivedTypeAttributes(map, codeClass); ExportMembersMapCode(codeClass, (ClassMap)map.ObjectMap, map.XmlTypeNamespace, map.BaseMap); if (map.BaseMap != null && map.BaseMap.TypeData.SchemaType != SchemaTypes.XmlNode) { CodeTypeReference ctr = GetDomType(map.BaseMap.TypeData, false); codeClass.BaseTypes.Add(ctr); if (map.BaseMap.IncludeInSchema) { ExportMapCode(map.BaseMap, false); AddInclude(map.BaseMap); } } ExportDerivedTypes(map, codeClass); }
public void ExportTypeMapping(XmlTypeMapping xmlTypeMapping) { codeGenerator.ExportTypeMapping(xmlTypeMapping, true); }
void RemoveInclude(XmlTypeMapping map) { includeMaps.Remove(map.TypeData.FullTypeName); }
protected override void GenerateClassInclude(CodeAttributeDeclarationCollection attributes, XmlTypeMapping map) { CodeAttributeDeclaration iatt = new CodeAttributeDeclaration("Mono.System.Xml.Serialization.SoapInclude"); iatt.Arguments.Add(new CodeAttributeArgument(new CodeTypeOfExpression(map.TypeData.FullTypeName))); attributes.Add(iatt); }
CodeTypeDeclaration GetMapDeclaration(XmlTypeMapping map) { return(exportedMaps [map.TypeData.FullTypeName] as CodeTypeDeclaration); }
void SetMapExported(XmlTypeMapping map) { exportedMaps [GetMapKey(map)] = map; }
protected virtual void GenerateEnum(XmlTypeMapping map, CodeTypeDeclaration codeEnum, bool isTopLevel) { }
void SetElementExported(XmlTypeMapping map) { exportedElements [GetMapKey(map)] = map; }