Exemplo n.º 1
0
        public void AddMappingMetadata(CodeAttributeDeclarationCollection metadata, XmlMemberMapping member, string ns, bool forceUseMemberName)
        {
            CodeAttributeDeclaration att;
            TypeData memType = member.TypeMapMember.TypeData;

            if (member.Any)
            {
                XmlTypeMapElementInfoList list = (XmlTypeMapElementInfoList)((XmlTypeMapMemberElement)member.TypeMapMember).ElementInfo;
                foreach (XmlTypeMapElementInfo info in list)
                {
                    if (info.IsTextElement)
                    {
                        metadata.Add(new CodeAttributeDeclaration("Mpd.Xml.Serialization.XmlText"));
                    }
                    else
                    {
                        att = new CodeAttributeDeclaration("Mpd.Xml.Serialization.XmlAnyElement");
                        if (!info.IsUnnamedAnyElement)
                        {
                            att.Arguments.Add(MapCodeGenerator.GetArg("Name", info.ElementName));
                            if (info.Namespace != ns)
                            {
                                att.Arguments.Add(MapCodeGenerator.GetArg("Namespace", member.Namespace));
                            }
                        }
                        metadata.Add(att);
                    }
                }
            }
            else if (member.TypeMapMember is XmlTypeMapMemberList)
            {
                // Array parameter
                XmlTypeMapMemberList list = member.TypeMapMember as XmlTypeMapMemberList;
                ListMap listMap           = (ListMap)list.ListTypeMapping.ObjectMap;

                codeGenerator.AddArrayAttributes(metadata, list, ns, forceUseMemberName);
                codeGenerator.AddArrayItemAttributes(metadata, listMap, memType.ListItemTypeData, list.Namespace, 0);
            }
            else if (member.TypeMapMember is XmlTypeMapMemberElement)
            {
                codeGenerator.AddElementMemberAttributes((XmlTypeMapMemberElement)member.TypeMapMember, ns, metadata, forceUseMemberName);
            }
            else if (member.TypeMapMember is XmlTypeMapMemberAttribute)
            {
                codeGenerator.AddAttributeMemberAttributes((XmlTypeMapMemberAttribute)member.TypeMapMember, ns, metadata, forceUseMemberName);
            }
            else
            {
                throw new NotSupportedException("Schema type not supported");
            }
        }
Exemplo n.º 2
0
        XmlTypeMapping ImportListMapping(TypeData typeData, string defaultNamespace)
        {
            Type type = typeData.Type;

            XmlTypeMapping map = helper.GetRegisteredClrType(type, XmlSerializer.EncodingNamespace);

            if (map != null)
            {
                return(map);
            }

            ListMap  obmap        = new ListMap();
            TypeData itemTypeData = typeData.ListItemTypeData;

            map = CreateTypeMapping(typeData, "Array", XmlSerializer.EncodingNamespace);
            helper.RegisterClrType(map, type, XmlSerializer.EncodingNamespace);
            map.MultiReferenceType = true;
            map.ObjectMap          = obmap;

            XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo(null, itemTypeData);

            if (elem.TypeData.IsComplexType)
            {
                elem.MappedType = ImportTypeMapping(typeData.ListItemType, defaultNamespace);
                elem.TypeData   = elem.MappedType.TypeData;
            }

            elem.ElementName = "Item";
            elem.Namespace   = string.Empty;
            elem.IsNullable  = true;            // By default, items are nullable

            XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();

            list.Add(elem);

            obmap.ItemInfo = list;
            XmlTypeMapping objMap = ImportTypeMapping(typeof(object), defaultNamespace);

            objMap.DerivedTypes.Add(map);

            // Register any of the including types as a derived class of object
            SoapIncludeAttribute[] includes = (SoapIncludeAttribute[])type.GetCustomAttributes(typeof(SoapIncludeAttribute), false);
            for (int i = 0; i < includes.Length; i++)
            {
                Type includedType = includes[i].Type;
                objMap.DerivedTypes.Add(ImportTypeMapping(includedType, defaultNamespace));
            }

            return(map);
        }
Exemplo n.º 3
0
        void ExportArrayCode(XmlTypeMapping map)
        {
            ListMap listMap = (ListMap)map.ObjectMap;

            foreach (XmlTypeMapElementInfo ainfo in listMap.ItemInfo)
            {
                if (ainfo.MappedType != null)
                {
                    if (!IsMapExported(ainfo.MappedType) && includeArrayTypes)
                    {
                        AddInclude(ainfo.MappedType);
                    }
                    ExportMapCode(ainfo.MappedType, false);
                }
            }
        }
Exemplo n.º 4
0
        void AddArrayElementFieldMember(CodeTypeDeclaration codeClass, XmlTypeMapMemberList member, string defaultNamespace)
        {
            CodeTypeMember codeField = CreateFieldMember(codeClass, member.TypeData, member.Name);

            CodeAttributeDeclarationCollection attributes = new CodeAttributeDeclarationCollection();

            AddArrayAttributes(attributes, member, defaultNamespace, false);

            ListMap listMap = (ListMap)member.ListTypeMapping.ObjectMap;

            AddArrayItemAttributes(attributes, listMap, member.TypeData.ListItemTypeData, defaultNamespace, 0);

            if (attributes.Count > 0)
            {
                codeField.CustomAttributes = attributes;
            }
        }
Exemplo n.º 5
0
        public override bool Equals(object other)
        {
            ListMap lmap = other as ListMap;

            if (lmap == null)
            {
                return(false);
            }

            if (_itemInfo.Count != lmap._itemInfo.Count)
            {
                return(false);
            }
            for (int n = 0; n < _itemInfo.Count; n++)
            {
                if (!_itemInfo[n].Equals(lmap._itemInfo[n]))
                {
                    return(false);
                }
            }
            return(true);
        }
        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);
        }
Exemplo n.º 7
0
        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));
            }
        }
 void WriteListContent(object container, TypeData listType, ListMap map, object ob, StringBuilder targetString)
 {
     if (listType.Type.IsArray)
     {
         Array array = (Array)ob;
         for (int n = 0; n < array.Length; n++)
         {
             object item = array.GetValue(n);
             XmlTypeMapElementInfo info = map.FindElement(container, n, item);
             if (info != null && targetString == null)
             {
                 WriteMemberElement(info, item);
             }
             else if (info != null && targetString != null)
             {
                 targetString.Append(GetStringValue(info.MappedType, info.TypeData, item)).Append(" ");
             }
             else if (item != null)
             {
                 throw CreateUnknownTypeException(item);
             }
         }
     }
     else if (ob is ICollection)
     {
         int          count    = (int)ob.GetType().GetProperty("Count").GetValue(ob, null);
         PropertyInfo itemProp = TypeData.GetIndexerProperty(listType.Type);
         object[]     index    = new object[1];
         for (int n = 0; n < count; n++)
         {
             index[0] = n;
             object item = itemProp.GetValue(ob, index);
             XmlTypeMapElementInfo info = map.FindElement(container, n, item);
             if (info != null && targetString == null)
             {
                 WriteMemberElement(info, item);
             }
             else if (info != null && targetString != null)
             {
                 targetString.Append(GetStringValue(info.MappedType, info.TypeData, item)).Append(" ");
             }
             else if (item != null)
             {
                 throw CreateUnknownTypeException(item);
             }
         }
     }
     else if (ob is IEnumerable)
     {
         IEnumerable e = (IEnumerable)ob;
         foreach (object item in e)
         {
             XmlTypeMapElementInfo info = map.FindElement(container, -1, item);
             if (info != null && targetString == null)
             {
                 WriteMemberElement(info, item);
             }
             else if (info != null && targetString != null)
             {
                 targetString.Append(GetStringValue(info.MappedType, info.TypeData, item)).Append(" ");
             }
             else if (item != null)
             {
                 throw CreateUnknownTypeException(item);
             }
         }
     }
     else
     {
         throw new Exception("Unsupported collection type");
     }
 }
        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 != System.Xml.XmlNodeType.EndElement)
            {
                if (Reader.NodeType == 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);
        }
Exemplo n.º 10
0
 protected virtual void GenerateArrayItemAttributes(CodeAttributeDeclarationCollection attributes, ListMap listMap, TypeData type, XmlTypeMapElementInfo ainfo, string defaultName, string defaultNamespace, int nestingLevel)
 {
 }
Exemplo n.º 11
0
        public void AddArrayItemAttributes(CodeAttributeDeclarationCollection attributes, ListMap listMap, TypeData type, string defaultNamespace, int nestingLevel)
        {
            foreach (XmlTypeMapElementInfo ainfo in listMap.ItemInfo)
            {
                string defaultName;
                if (ainfo.MappedType != null)
                {
                    defaultName = ainfo.MappedType.ElementName;
                }
                else
                {
                    defaultName = ainfo.TypeData.XmlType;
                }

                GenerateArrayItemAttributes(attributes, listMap, type, ainfo, defaultName, defaultNamespace, nestingLevel);
                if (ainfo.MappedType != null)
                {
                    if (!IsMapExported(ainfo.MappedType) && includeArrayTypes)
                    {
                        AddInclude(ainfo.MappedType);
                    }
                    ExportMapCode(ainfo.MappedType, false);
                }
            }

            if (listMap.IsMultiArray)
            {
                XmlTypeMapping nmap = listMap.NestedArrayMapping;
                AddArrayItemAttributes(attributes, (ListMap)nmap.ObjectMap, nmap.TypeData.ListItemTypeData, defaultNamespace, nestingLevel + 1);
            }
        }
Exemplo n.º 12
0
        protected override void GenerateArrayItemAttributes(CodeAttributeDeclarationCollection attributes, ListMap listMap, TypeData type, XmlTypeMapElementInfo ainfo, string defaultName, string defaultNamespace, int nestingLevel)
        {
            bool needsType = (listMap.ItemInfo.Count > 1) ||
                             (ainfo.TypeData.FullTypeName != type.FullTypeName && !listMap.IsMultiArray);

            CodeAttributeDeclaration att = new CodeAttributeDeclaration("Mpd.Xml.Serialization.XmlArrayItem");

            if (ainfo.ElementName != defaultName)
            {
                att.Arguments.Add(GetArg("ElementName", ainfo.ElementName));
            }
            if (ainfo.Namespace != defaultNamespace && ainfo.Namespace != XmlSchema.Namespace)
            {
                att.Arguments.Add(GetArg("Namespace", ainfo.Namespace));
            }
            if (needsType)
            {
                att.Arguments.Add(GetTypeArg("Type", ainfo.TypeData.FullTypeName));
            }
            if (!ainfo.IsNullable)
            {
                att.Arguments.Add(GetArg("IsNullable", false));
            }
            if (ainfo.Form == XmlSchemaForm.Unqualified)
            {
                att.Arguments.Add(MapCodeGenerator.GetEnumArg("Form", "System.Xml.Schema.XmlSchemaForm", ainfo.Form.ToString()));
            }
            if (att.Arguments.Count > 0 && nestingLevel > 0)
            {
                att.Arguments.Add(GetArg("NestingLevel", nestingLevel));
            }

            if (att.Arguments.Count > 0)
            {
                attributes.Add(att);
            }
        }