Inheritance: ObjectMap
示例#1
0
        private object ReadListElement(XmlTypeMapping typeMap, bool isNullable, object list, bool canCreateInstance)
        {
            Type    type    = typeMap.TypeData.Type;
            ListMap listMap = (ListMap)typeMap.ObjectMap;

            if (type.IsArray && base.ReadNull())
            {
                return(null);
            }
            if (list == null)
            {
                if (!canCreateInstance || !typeMap.TypeData.HasPublicConstructor)
                {
                    throw base.CreateReadOnlyCollectionException(typeMap.TypeFullName);
                }
                list = this.CreateList(type);
            }
            if (base.Reader.IsEmptyElement)
            {
                base.Reader.Skip();
                if (type.IsArray)
                {
                    list = base.ShrinkArray((Array)list, 0, type.GetElementType(), false);
                }
                return(list);
            }
            int length = 0;

            base.Reader.ReadStartElement();
            base.Reader.MoveToContent();
            while (base.Reader.NodeType != XmlNodeType.EndElement)
            {
                if (base.Reader.NodeType == XmlNodeType.Element)
                {
                    XmlTypeMapElementInfo xmlTypeMapElementInfo = listMap.FindElement(base.Reader.LocalName, base.Reader.NamespaceURI);
                    if (xmlTypeMapElementInfo != null)
                    {
                        this.AddListValue(typeMap.TypeData, ref list, length++, this.ReadObjectElement(xmlTypeMapElementInfo), false);
                    }
                    else
                    {
                        base.UnknownNode(null);
                    }
                }
                else
                {
                    base.UnknownNode(null);
                }
                base.Reader.MoveToContent();
            }
            base.ReadEndElement();
            if (type.IsArray)
            {
                list = base.ShrinkArray((Array)list, length, type.GetElementType(), false);
            }
            return(list);
        }
示例#2
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("System.Xml.Serialization.XmlText"));
                    }
                    else
                    {
                        att = new CodeAttributeDeclaration("System.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");
            }
        }
        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);
        }
示例#4
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);
                }
            }
        }
示例#5
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;
            }
        }
示例#6
0
        private XmlTypeMapping ImportListMapping(TypeData typeData, string defaultNamespace)
        {
            Type           type           = typeData.Type;
            XmlTypeMapping xmlTypeMapping = this.helper.GetRegisteredClrType(type, "http://schemas.xmlsoap.org/soap/encoding/");

            if (xmlTypeMapping != null)
            {
                return(xmlTypeMapping);
            }
            ListMap  listMap          = new ListMap();
            TypeData listItemTypeData = typeData.ListItemTypeData;

            xmlTypeMapping = this.CreateTypeMapping(typeData, "Array", "http://schemas.xmlsoap.org/soap/encoding/");
            this.helper.RegisterClrType(xmlTypeMapping, type, "http://schemas.xmlsoap.org/soap/encoding/");
            xmlTypeMapping.MultiReferenceType = true;
            xmlTypeMapping.ObjectMap          = listMap;
            XmlTypeMapElementInfo xmlTypeMapElementInfo = new XmlTypeMapElementInfo(null, listItemTypeData);

            if (xmlTypeMapElementInfo.TypeData.IsComplexType)
            {
                xmlTypeMapElementInfo.MappedType = this.ImportTypeMapping(typeData.ListItemType, defaultNamespace);
                xmlTypeMapElementInfo.TypeData   = xmlTypeMapElementInfo.MappedType.TypeData;
            }
            xmlTypeMapElementInfo.ElementName = "Item";
            xmlTypeMapElementInfo.Namespace   = string.Empty;
            xmlTypeMapElementInfo.IsNullable  = true;
            listMap.ItemInfo = new XmlTypeMapElementInfoList
            {
                xmlTypeMapElementInfo
            };
            XmlTypeMapping xmlTypeMapping2 = this.ImportTypeMapping(typeof(object), defaultNamespace);

            xmlTypeMapping2.DerivedTypes.Add(xmlTypeMapping);
            SoapIncludeAttribute[] array = (SoapIncludeAttribute[])type.GetCustomAttributes(typeof(SoapIncludeAttribute), false);
            for (int i = 0; i < array.Length; i++)
            {
                Type type2 = array[i].Type;
                xmlTypeMapping2.DerivedTypes.Add(this.ImportTypeMapping(type2, defaultNamespace));
            }
            return(xmlTypeMapping);
        }
示例#7
0
        void AddArrayElementFieldMember(CodeTypeDeclaration codeClass, XmlTypeMapMemberList member, string defaultNamespace)
        {
            CodeMemberField codeField = new CodeMemberField(GetDomType(member.TypeData), member.Name);

            AddComments(codeField, member.Documentation);
            codeField.Attributes = MemberAttributes.Public;
            codeClass.Members.Add(codeField);

            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;
            }
        }
示例#8
0
        public override bool Equals(object other)
        {
            ListMap listMap = other as ListMap;

            if (listMap == null)
            {
                return(false);
            }
            if (this._itemInfo.Count != listMap._itemInfo.Count)
            {
                return(false);
            }
            for (int i = 0; i < this._itemInfo.Count; i++)
            {
                if (!this._itemInfo[i].Equals(listMap._itemInfo[i]))
                {
                    return(false);
                }
            }
            return(true);
        }
        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);
        }
示例#10
0
        private object ReadListString(XmlTypeMapping typeMap, string values)
        {
            Type    type    = typeMap.TypeData.Type;
            ListMap listMap = (ListMap)typeMap.ObjectMap;

            values = values.Trim();
            if (values == string.Empty)
            {
                return(Array.CreateInstance(type.GetElementType(), 0));
            }
            string[] array = values.Split(new char[]
            {
                ' '
            });
            Array array2 = Array.CreateInstance(type.GetElementType(), array.Length);
            XmlTypeMapElementInfo xmlTypeMapElementInfo = (XmlTypeMapElementInfo)listMap.ItemInfo[0];

            for (int i = 0; i < array.Length; i++)
            {
                array2.SetValue(this.GetValueFromXmlString(array[i], xmlTypeMapElementInfo.TypeData, xmlTypeMapElementInfo.MappedType), i);
            }
            return(array2);
        }
        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);
        }
示例#12
0
 protected virtual void GenerateArrayItemAttributes(CodeAttributeDeclarationCollection attributes, ListMap listMap, TypeData type, XmlTypeMapElementInfo ainfo, string defaultName, string defaultNamespace, int nestingLevel)
 {
 }
示例#13
0
 private void WriteListContent(object container, TypeData listType, ListMap map, object ob, StringBuilder targetString)
 {
     if (listType.Type.IsArray)
     {
         Array array = (Array)ob;
         for (int i = 0; i < array.Length; i++)
         {
             object value = array.GetValue(i);
             XmlTypeMapElementInfo xmlTypeMapElementInfo = map.FindElement(container, i, value);
             if (xmlTypeMapElementInfo != null && targetString == null)
             {
                 this.WriteMemberElement(xmlTypeMapElementInfo, value);
             }
             else if (xmlTypeMapElementInfo != null && targetString != null)
             {
                 targetString.Append(this.GetStringValue(xmlTypeMapElementInfo.MappedType, xmlTypeMapElementInfo.TypeData, value)).Append(" ");
             }
             else if (value != null)
             {
                 throw base.CreateUnknownTypeException(value);
             }
         }
     }
     else if (ob is ICollection)
     {
         int          num             = (int)ob.GetType().GetProperty("Count").GetValue(ob, null);
         PropertyInfo indexerProperty = TypeData.GetIndexerProperty(listType.Type);
         object[]     array2          = new object[1];
         for (int j = 0; j < num; j++)
         {
             array2[0] = j;
             object value2 = indexerProperty.GetValue(ob, array2);
             XmlTypeMapElementInfo xmlTypeMapElementInfo2 = map.FindElement(container, j, value2);
             if (xmlTypeMapElementInfo2 != null && targetString == null)
             {
                 this.WriteMemberElement(xmlTypeMapElementInfo2, value2);
             }
             else if (xmlTypeMapElementInfo2 != null && targetString != null)
             {
                 targetString.Append(this.GetStringValue(xmlTypeMapElementInfo2.MappedType, xmlTypeMapElementInfo2.TypeData, value2)).Append(" ");
             }
             else if (value2 != null)
             {
                 throw base.CreateUnknownTypeException(value2);
             }
         }
     }
     else
     {
         if (!(ob is IEnumerable))
         {
             throw new Exception("Unsupported collection type");
         }
         IEnumerable enumerable = (IEnumerable)ob;
         foreach (object obj in enumerable)
         {
             XmlTypeMapElementInfo xmlTypeMapElementInfo3 = map.FindElement(container, -1, obj);
             if (xmlTypeMapElementInfo3 != null && targetString == null)
             {
                 this.WriteMemberElement(xmlTypeMapElementInfo3, obj);
             }
             else if (xmlTypeMapElementInfo3 != null && targetString != null)
             {
                 targetString.Append(this.GetStringValue(xmlTypeMapElementInfo3.MappedType, xmlTypeMapElementInfo3.TypeData, obj)).Append(" ");
             }
             else if (obj != null)
             {
                 throw base.CreateUnknownTypeException(obj);
             }
         }
     }
 }
示例#14
0
		XmlTypeMapping ImportListMapping (TypeData typeData, XmlRootAttribute root, string defaultNamespace, XmlAttributes atts, int nestingLevel)
		{
			Type type = typeData.Type;
			ListMap obmap = new ListMap ();

			if (!allowPrivateTypes)
				ReflectionHelper.CheckSerializableType (type, true);
			
			if (atts == null) atts = new XmlAttributes();
			Type itemType = typeData.ListItemType;

			// warning: byte[][] should not be considered multiarray
			bool isMultiArray = (type.IsArray && (TypeTranslator.GetTypeData(itemType).SchemaType == SchemaTypes.Array) && itemType.IsArray);

			XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();

			foreach (XmlArrayItemAttribute att in atts.XmlArrayItems)
			{
				if (att.Namespace != null && att.Form == XmlSchemaForm.Unqualified)
					throw new InvalidOperationException ("XmlArrayItemAttribute.Form must not be Unqualified when it has an explicit Namespace value.");
				if (att.NestingLevel != nestingLevel) continue;
				Type elemType = (att.Type != null) ? att.Type : itemType;
				XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (null, TypeTranslator.GetTypeData(elemType, att.DataType));
				elem.Namespace = att.Namespace != null ? att.Namespace : defaultNamespace;
				if (elem.Namespace == null) elem.Namespace = "";
				elem.Form = att.Form;
				if (att.Form == XmlSchemaForm.Unqualified)
					elem.Namespace = string.Empty;
				elem.IsNullable = att.IsNullable && CanBeNull (elem.TypeData);
				elem.NestingLevel = att.NestingLevel;

				if (isMultiArray) {
					elem.MappedType = ImportListMapping (elemType, null, elem.Namespace, atts, nestingLevel + 1);
				} else if (elem.TypeData.IsComplexType) {
					elem.MappedType = ImportTypeMapping (elemType, null, elem.Namespace);
				}

				if (att.ElementName.Length != 0) {
					elem.ElementName = XmlConvert.EncodeLocalName (att.ElementName);
				} else if (elem.MappedType != null) {
					elem.ElementName = elem.MappedType.ElementName;
				} else {
					elem.ElementName = TypeTranslator.GetTypeData (elemType).XmlType;
				}

				list.Add (elem);
			}

			if (list.Count == 0)
			{
				XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (null, TypeTranslator.GetTypeData (itemType));
				if (isMultiArray)
					elem.MappedType = ImportListMapping (itemType, null, defaultNamespace, atts, nestingLevel + 1);
				else if (elem.TypeData.IsComplexType)
					elem.MappedType = ImportTypeMapping (itemType, null, defaultNamespace);

				if (elem.MappedType != null) {
					elem.ElementName = elem.MappedType.XmlType;
				} else {
					elem.ElementName = TypeTranslator.GetTypeData (itemType).XmlType;
				}

				elem.Namespace = (defaultNamespace != null) ? defaultNamespace : "";
				elem.IsNullable = CanBeNull (elem.TypeData);
				list.Add (elem);
			}

			obmap.ItemInfo = list;

			// If there can be different element names (types) in the array, then its name cannot
			// be "ArrayOfXXX" it must be something like ArrayOfChoiceNNN

			string baseName;
			if (list.Count > 1) {
				baseName = "ArrayOfChoice" + (arrayChoiceCount++);
			} else {
				XmlTypeMapElementInfo elem = ((XmlTypeMapElementInfo) list[0]);
				if (elem.MappedType != null) {
					baseName = TypeTranslator.GetArrayName (elem.MappedType.XmlType);
				} else {
					baseName = TypeTranslator.GetArrayName (elem.ElementName);
				}
			}

			// Avoid name colisions

			int nameCount = 1;
			string name = baseName;

			do {
				XmlTypeMapping foundMap = helper.GetRegisteredSchemaType (name, defaultNamespace);
				if (foundMap == null) nameCount = -1;
				else if (obmap.Equals (foundMap.ObjectMap) && typeData.Type == foundMap.TypeData.Type) return foundMap;
				else name = baseName + (nameCount++);
			}
			while (nameCount != -1);

			XmlTypeMapping map = CreateTypeMapping (typeData, root, name, defaultNamespace);
			map.ObjectMap = obmap;
			
			// Register any of the including types as a derived class of object
			XmlIncludeAttribute[] includes = (XmlIncludeAttribute[])type.GetCustomAttributes (typeof (XmlIncludeAttribute), false);
			
			XmlTypeMapping objectMapping = ImportTypeMapping (typeof(object));
			for (int i = 0; i < includes.Length; i++)
			{
				Type includedType = includes[i].Type;
				objectMapping.DerivedTypes.Add(ImportTypeMapping (includedType, null, defaultNamespace));
			}
			
			// Register this map as a derived class of object

			helper.RegisterSchemaType (map, name, defaultNamespace);
			ImportTypeMapping (typeof(object)).DerivedTypes.Add (map);

			return map;
		}
示例#15
0
		XmlTypeMapping GetTypeMapping (TypeData typeData)
		{
			if (typeData.Type == typeof(object) && !anyTypeImported)
				ImportAllObjectTypes ();
				
			XmlTypeMapping map = GetRegisteredTypeMapping (typeData);
			if (map != null) return map;
			
			if (typeData.IsListType)
			{
				// Create an array map for the type

				XmlTypeMapping itemMap = GetTypeMapping (typeData.ListItemTypeData);
				
				map = new XmlTypeMapping (typeData.XmlType, itemMap.Namespace, typeData, typeData.XmlType, itemMap.Namespace);
				map.IncludeInSchema = true;

				ListMap listMap = new ListMap ();
				listMap.ItemInfo = new XmlTypeMapElementInfoList();
				listMap.ItemInfo.Add (CreateElementInfo (itemMap.Namespace, null, typeData.ListItemTypeData.XmlType, typeData.ListItemTypeData, false, XmlSchemaForm.None, -1));
				map.ObjectMap = listMap;
				
				RegisterTypeMapping (new XmlQualifiedName(map.ElementName, map.Namespace), typeData, map);
				return map;
			}
			else if (typeData.SchemaType == SchemaTypes.Primitive || typeData.Type == typeof(object) || typeof(XmlNode).IsAssignableFrom(typeData.Type))
			{
				return CreateSystemMap (typeData);
			}
			
			throw new InvalidOperationException ("Map for type " + typeData.TypeName + " not found");
		}
示例#16
0
		void ImportChoiceContent (XmlQualifiedName typeQName, ClassMap cmap, XmlSchemaChoice choice, CodeIdentifiers classIds, bool multiValue)
		{
			XmlTypeMapElementInfoList choices = new XmlTypeMapElementInfoList ();
			multiValue = ImportChoices (typeQName, null, choices, choice.Items) || multiValue;
			if (choices.Count == 0) return;

			if (choice.MaxOccurs > 1) multiValue = true;

			XmlTypeMapMemberElement member;
			if (multiValue)
			{
				member = new XmlTypeMapMemberFlatList ();
				member.Name = classIds.AddUnique ("Items", member);
				ListMap listMap = new ListMap ();
				listMap.ItemInfo = choices;
				((XmlTypeMapMemberFlatList)member).ListMap = listMap;
			}
			else
			{
				member = new XmlTypeMapMemberElement ();
				member.Name = classIds.AddUnique ("Item", member);
			}
			
			// If all choices have the same type, use that type for the member.
			// If not use System.Object.
			// If there are at least two choices with the same type, use a choice
			// identifier attribute

			TypeData typeData = null;
			bool twoEqual = false;
			bool allEqual = true;
			Hashtable types = new Hashtable ();

			for (int n = choices.Count - 1; n >= 0; n--)
			{
				XmlTypeMapElementInfo einfo = (XmlTypeMapElementInfo) choices [n];
				
				// In some complex schemas, we may end up with several options
				// with the same name. It is better to ignore the extra options
				// than to crash. It's the best we can do, and btw it works
				// better than in MS.NET.
				
				if (cmap.GetElement (einfo.ElementName, einfo.Namespace, einfo.ExplicitOrder) != null ||
					choices.IndexOfElement (einfo.ElementName, einfo.Namespace) != n)
				{
					choices.RemoveAt (n);
					continue;
				}
					
				if (types.ContainsKey (einfo.TypeData)) twoEqual = true;
				else types.Add (einfo.TypeData, einfo);

				TypeData choiceType = einfo.TypeData;
				if (choiceType.SchemaType == SchemaTypes.Class)
				{
					// When comparing class types, use the most generic class in the
					// inheritance hierarchy

					XmlTypeMapping choiceMap = GetTypeMapping (choiceType);
					BuildPendingMap (choiceMap);
					while (choiceMap.BaseMap != null) {
						choiceMap = choiceMap.BaseMap;
						BuildPendingMap (choiceMap);
						choiceType = choiceMap.TypeData;
					}
				}
				
				if (typeData == null) typeData = choiceType;
				else if (typeData != choiceType) allEqual = false;
			}

			if (!allEqual)
				typeData = TypeTranslator.GetTypeData (typeof(object));

			if (twoEqual)
			{
				// Create the choice member
				XmlTypeMapMemberElement choiceMember = new XmlTypeMapMemberElement ();
				choiceMember.Ignore = true;
				choiceMember.Name = classIds.AddUnique (member.Name + "ElementName", choiceMember);
				member.ChoiceMember = choiceMember.Name;

				// Create the choice enum
				XmlTypeMapping enumMap = CreateTypeMapping (new XmlQualifiedName (member.Name + "ChoiceType", typeQName.Namespace), SchemaTypes.Enum, null);
				enumMap.IncludeInSchema = false;

				CodeIdentifiers codeIdents = new CodeIdentifiers ();
				EnumMap.EnumMapMember[] members = new EnumMap.EnumMapMember [choices.Count];
				for (int n=0; n<choices.Count; n++)
				{
					XmlTypeMapElementInfo it =(XmlTypeMapElementInfo) choices[n];
					bool extraNs = (it.Namespace != null && it.Namespace != "" && it.Namespace != typeQName.Namespace);
					string xmlName = extraNs ? it.Namespace + ":" + it.ElementName : it.ElementName;
					string enumName = codeIdents.AddUnique (CodeIdentifier.MakeValid (it.ElementName), it);
					members [n] = new EnumMap.EnumMapMember (xmlName, enumName);
				}
				enumMap.ObjectMap = new EnumMap (members, false);

				choiceMember.TypeData = multiValue ? enumMap.TypeData.ListTypeData : enumMap.TypeData;
				choiceMember.ElementInfo.Add (CreateElementInfo (typeQName.Namespace, choiceMember, choiceMember.Name, choiceMember.TypeData, false, XmlSchemaForm.None, -1));
				cmap.AddMember (choiceMember);
			}
			
			if (typeData == null)
				return;
	
			if (multiValue)
				typeData = typeData.ListTypeData;

			member.ElementInfo = choices;
			member.Documentation = GetDocumentation (choice);
			member.TypeData = typeData;
			cmap.AddMember (member);
		}
		string GenerateWriteListContent (string container, TypeData listType, ListMap map, string ob, bool writeToString)
		{
			string targetString = null;
			
			if (writeToString)
			{
				targetString = GetStrTempVar ();
				WriteLine ("System.Text.StringBuilder " + targetString + " = new System.Text.StringBuilder();");
			}
			
			if (listType.Type.IsArray)
			{
				string itemVar = GetNumTempVar ();
				WriteLineInd ("for (int "+itemVar+" = 0; "+itemVar+" < " + ob + ".Length; "+itemVar+"++) {");
				GenerateListLoop (container, map, ob + "["+itemVar+"]", itemVar, listType.ListItemTypeData, targetString);
				WriteLineUni ("}");
			}
			else if (typeof(ICollection).IsAssignableFrom (listType.Type))
			{
				string itemVar = GetNumTempVar ();
				WriteLineInd ("for (int "+itemVar+" = 0; "+itemVar+" < " + ob + ".Count; "+itemVar+"++) {");
				GenerateListLoop (container, map, ob + "["+itemVar+"]", itemVar, listType.ListItemTypeData, targetString);
				WriteLineUni ("}");
			}
			else if (typeof(IEnumerable).IsAssignableFrom (listType.Type))
			{
				string itemVar = GetObTempVar ();
				WriteLineInd ("foreach (" + listType.ListItemTypeData.CSharpFullName + " " + itemVar + " in " + ob + ") {");
				GenerateListLoop (container, map, itemVar, null, listType.ListItemTypeData, targetString);
				WriteLineUni ("}");
			}
			else
				throw new Exception ("Unsupported collection type");

			return targetString;
		}
        private XmlQualifiedName ExportArraySchema(XmlTypeMapping map, string defaultNamespace)
        {
            ListMap listMap = (ListMap)map.ObjectMap;

            if (this.encodedFormat)
            {
                string str;
                string text;
                listMap.GetArrayType(-1, out str, out text);
                string text2;
                if (text == "http://www.w3.org/2001/XMLSchema")
                {
                    text2 = defaultNamespace;
                }
                else
                {
                    text2 = text;
                }
                if (this.IsMapExported(map))
                {
                    return(new XmlQualifiedName(listMap.GetSchemaArrayName(), text2));
                }
                this.SetMapExported(map);
                XmlSchema            schema = this.GetSchema(text2);
                XmlSchemaComplexType xmlSchemaComplexType = new XmlSchemaComplexType();
                xmlSchemaComplexType.Name = listMap.GetSchemaArrayName();
                schema.Items.Add(xmlSchemaComplexType);
                XmlSchemaComplexContent xmlSchemaComplexContent = new XmlSchemaComplexContent();
                xmlSchemaComplexContent.IsMixed   = false;
                xmlSchemaComplexType.ContentModel = xmlSchemaComplexContent;
                XmlSchemaComplexContentRestriction xmlSchemaComplexContentRestriction = new XmlSchemaComplexContentRestriction();
                xmlSchemaComplexContent.Content = xmlSchemaComplexContentRestriction;
                xmlSchemaComplexContentRestriction.BaseTypeName = new XmlQualifiedName("Array", "http://schemas.xmlsoap.org/soap/encoding/");
                XmlSchemaAttribute xmlSchemaAttribute = new XmlSchemaAttribute();
                xmlSchemaComplexContentRestriction.Attributes.Add(xmlSchemaAttribute);
                xmlSchemaAttribute.RefName = new XmlQualifiedName("arrayType", "http://schemas.xmlsoap.org/soap/encoding/");
                XmlAttribute xmlAttribute = this.Document.CreateAttribute("arrayType", "http://schemas.xmlsoap.org/wsdl/");
                xmlAttribute.Value = text + ((!(text != string.Empty)) ? string.Empty : ":") + str;
                xmlSchemaAttribute.UnhandledAttributes = new XmlAttribute[]
                {
                    xmlAttribute
                };
                this.ImportNamespace(schema, "http://schemas.xmlsoap.org/wsdl/");
                XmlTypeMapElementInfo xmlTypeMapElementInfo = (XmlTypeMapElementInfo)listMap.ItemInfo[0];
                if (xmlTypeMapElementInfo.MappedType != null)
                {
                    switch (xmlTypeMapElementInfo.TypeData.SchemaType)
                    {
                    case SchemaTypes.Enum:
                        this.ExportEnumSchema(xmlTypeMapElementInfo.MappedType);
                        break;

                    case SchemaTypes.Array:
                        this.ExportArraySchema(xmlTypeMapElementInfo.MappedType, text2);
                        break;

                    case SchemaTypes.Class:
                        this.ExportClassSchema(xmlTypeMapElementInfo.MappedType);
                        break;
                    }
                }
                return(new XmlQualifiedName(listMap.GetSchemaArrayName(), text2));
            }
            else
            {
                if (this.IsMapExported(map))
                {
                    return(new XmlQualifiedName(map.XmlType, map.XmlTypeNamespace));
                }
                this.SetMapExported(map);
                XmlSchema            schema2 = this.GetSchema(map.XmlTypeNamespace);
                XmlSchemaComplexType xmlSchemaComplexType2 = new XmlSchemaComplexType();
                xmlSchemaComplexType2.Name = map.ElementName;
                schema2.Items.Add(xmlSchemaComplexType2);
                XmlSchemaParticle schemaArrayElement = this.GetSchemaArrayElement(schema2, listMap.ItemInfo);
                if (schemaArrayElement is XmlSchemaChoice)
                {
                    xmlSchemaComplexType2.Particle = schemaArrayElement;
                }
                else
                {
                    xmlSchemaComplexType2.Particle = new XmlSchemaSequence
                    {
                        Items =
                        {
                            schemaArrayElement
                        }
                    };
                }
                return(new XmlQualifiedName(map.XmlType, map.XmlTypeNamespace));
            }
        }
示例#19
0
        XmlTypeMapping ImportListMapping(Type type, XmlRootAttribute root, string defaultNamespace, XmlAttributes atts, int nestingLevel)
        {
            TypeData typeData = TypeTranslator.GetTypeData(type);
            ListMap  obmap    = new ListMap();

            if (!allowPrivateTypes)
            {
                ReflectionHelper.CheckSerializableType(type);
            }

            if (atts == null)
            {
                atts = new XmlAttributes();
            }
            Type itemType = typeData.ListItemType;

            // warning: byte[][] should not be considered multiarray
            bool isMultiArray = (type.IsArray && (TypeTranslator.GetTypeData(itemType).SchemaType == SchemaTypes.Array) && itemType.IsArray);

            XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();

            foreach (XmlArrayItemAttribute att in atts.XmlArrayItems)
            {
                if (att.NestingLevel != nestingLevel)
                {
                    continue;
                }
                Type elemType = (att.Type != null) ? att.Type : itemType;
                XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo(null, TypeTranslator.GetTypeData(elemType, att.DataType));
                elem.Namespace = att.Namespace != null ? att.Namespace : defaultNamespace;
                if (elem.Namespace == null)
                {
                    elem.Namespace = "";
                }
                elem.Form         = att.Form;
                elem.IsNullable   = att.IsNullable && CanBeNull(elem.TypeData);
                elem.NestingLevel = att.NestingLevel;

                if (isMultiArray)
                {
                    elem.MappedType = ImportListMapping(elemType, null, elem.Namespace, atts, nestingLevel + 1);
                }
                else if (elem.TypeData.IsComplexType)
                {
                    elem.MappedType = ImportTypeMapping(elemType, null, elem.Namespace);
                }

                if (att.ElementName != null)
                {
                    elem.ElementName = att.ElementName;
                }
                else if (elem.MappedType != null)
                {
                    elem.ElementName = elem.MappedType.ElementName;
                }
                else
                {
                    elem.ElementName = TypeTranslator.GetTypeData(elemType).XmlType;
                }

                list.Add(elem);
            }

            if (list.Count == 0)
            {
                XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo(null, TypeTranslator.GetTypeData(itemType));
                if (isMultiArray)
                {
                    elem.MappedType = ImportListMapping(itemType, null, defaultNamespace, atts, nestingLevel + 1);
                }
                else if (elem.TypeData.IsComplexType)
                {
                    elem.MappedType = ImportTypeMapping(itemType, null, defaultNamespace);
                }

                if (elem.MappedType != null)
                {
                    elem.ElementName = elem.MappedType.XmlType;
                }
                else
                {
                    elem.ElementName = TypeTranslator.GetTypeData(itemType).XmlType;
                }

                elem.Namespace  = (defaultNamespace != null) ? defaultNamespace : "";
                elem.IsNullable = CanBeNull(elem.TypeData);
                list.Add(elem);
            }

            obmap.ItemInfo = list;

            // If there can be different element names (types) in the array, then its name cannot
            // be "ArrayOfXXX" it must be something like ArrayOfChoiceNNN

            string baseName;

            if (list.Count > 1)
            {
                baseName = "ArrayOfChoice" + (arrayChoiceCount++);
            }
            else
            {
                XmlTypeMapElementInfo elem = ((XmlTypeMapElementInfo)list[0]);
                if (elem.MappedType != null)
                {
                    baseName = TypeTranslator.GetArrayName(elem.MappedType.XmlType);
                }
                else
                {
                    baseName = TypeTranslator.GetArrayName(elem.ElementName);
                }
            }

            // Avoid name colisions

            int    nameCount = 1;
            string name      = baseName;

            do
            {
                XmlTypeMapping foundMap = helper.GetRegisteredSchemaType(name, defaultNamespace);
                if (foundMap == null)
                {
                    nameCount = -1;
                }
                else if (obmap.Equals(foundMap.ObjectMap) && typeData.Type == foundMap.TypeData.Type)
                {
                    return(foundMap);
                }
                else
                {
                    name = baseName + (nameCount++);
                }
            }while (nameCount != -1);

            XmlTypeMapping map = CreateTypeMapping(typeData, root, name, defaultNamespace);

            map.ObjectMap = obmap;

            // Register any of the including types as a derived class of object
            XmlIncludeAttribute[] includes = (XmlIncludeAttribute[])type.GetCustomAttributes(typeof(XmlIncludeAttribute), false);

            XmlTypeMapping objectMapping = ImportTypeMapping(typeof(object));

            for (int i = 0; i < includes.Length; i++)
            {
                Type includedType = includes[i].Type;
                objectMapping.DerivedTypes.Add(ImportTypeMapping(includedType, null, defaultNamespace));
            }

            // Register this map as a derived class of object

            helper.RegisterSchemaType(map, name, defaultNamespace);
            ImportTypeMapping(typeof(object)).DerivedTypes.Add(map);

            return(map);
        }
        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);
        }
		XmlTypeMapping GetTypeMapping (TypeData typeData)
		{
			XmlTypeMapping map = (XmlTypeMapping) dataMappedTypes [typeData];
			if (map != null) return map;
			
			if (map == null && typeData.IsListType)
			{
				// Create an array map for the type

				XmlTypeMapping itemMap = GetTypeMapping (typeData.ListItemTypeData);
				
				map = new XmlTypeMapping (typeData.XmlType, itemMap.Namespace, typeData, typeData.XmlType, itemMap.Namespace);
				map.IncludeInSchema = true;

				ListMap listMap = new ListMap ();
				listMap.ItemInfo = new XmlTypeMapElementInfoList();
				listMap.ItemInfo.Add (CreateElementInfo (itemMap.Namespace, null, typeData.ListItemTypeData.XmlType, typeData.ListItemTypeData, false, XmlSchemaForm.None));
				map.ObjectMap = listMap;
				
				mappedTypes [new XmlQualifiedName(map.ElementName, map.Namespace)] = map;
				dataMappedTypes [typeData] = map;
				return map;
			}
			else if (typeData.SchemaType == SchemaTypes.Primitive || typeData.Type == typeof(object) || typeof(XmlNode).IsAssignableFrom(typeData.Type))
			{
				map = new XmlTypeMapping (typeData.XmlType, XmlSchema.Namespace, typeData, typeData.XmlType, XmlSchema.Namespace);
				map.IncludeInSchema = false;
				map.ObjectMap = new ClassMap ();
				dataMappedTypes [typeData] = map;
				
				if (typeData.Type == typeof(object))
				{
					// All complex types are subtypes of anyType, so all of them 
					// must also be imported
					
					foreach (XmlSchema schema in schemas) {
						foreach (XmlSchemaObject sob in schema.Items) 
						{
							XmlSchemaComplexType sct = sob as XmlSchemaComplexType;
							if (sct != null)
								ImportType (new XmlQualifiedName (sct.Name, schema.TargetNamespace), sct, null);
						}
					}					
				}
				
				return map;
			}
			
			throw new InvalidOperationException ("Map for type " + typeData.TypeName + " not found");
		}
		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);
				}
			}

			if (listMap.IsMultiArray)
			{
				XmlTypeMapping nmap = listMap.NestedArrayMapping;
				AddArrayItemAttributes (attributes, (ListMap) nmap.ObjectMap, nmap.TypeData.ListItemTypeData, defaultNamespace, nestingLevel + 1);
			}
		}
示例#23
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("System.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);
            }
        }
示例#24
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");
     }
 }
		void GenerateGetArrayType (ListMap map, string itemCount, out string localName, out string ns)
		{
			string arrayDim;
			if (itemCount != "") arrayDim = "";
			else arrayDim = "[]";

			XmlTypeMapElementInfo info = (XmlTypeMapElementInfo) map.ItemInfo[0];
			if (info.TypeData.SchemaType == SchemaTypes.Array)
			{
				string nm;
				GenerateGetArrayType ((ListMap)info.MappedType.ObjectMap, "", out nm, out ns);
				localName = nm + arrayDim;
			}
			else 
			{
				if (info.MappedType != null)
				{
					localName = info.MappedType.XmlType + arrayDim;
					ns = info.MappedType.Namespace;
				}
				else 
				{
					localName = info.TypeData.XmlType + arrayDim;
					ns = info.DataTypeNamespace;
				}
			}
			if (itemCount != "") {
				localName = "\"" + localName + "[\" + " + itemCount + " + \"]\"";
				ns = GetLiteral (ns);
			}
		}
示例#27
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;
		}
		void GenerateListLoop (string container, ListMap map, string item, string index, TypeData itemTypeData, string targetString)
		{
			bool multichoice = (map.ItemInfo.Count > 1);

			if (map.ChoiceMember != null && container != null && index != null) {
				WriteLineInd ("if ((" + container + ".@" + map.ChoiceMember + " == null) || (" + index + " >= " + container + ".@" + map.ChoiceMember + ".Length))");
				WriteLine ("throw CreateInvalidChoiceIdentifierValueException (" + container + ".GetType().ToString(), \"" + map.ChoiceMember + "\");");
				Unindent ();
			}
			
			if (multichoice)
				WriteLine ("if (((object)" + item + ") == null) { }");
				
			foreach (XmlTypeMapElementInfo info in map.ItemInfo)
			{
				if (map.ChoiceMember != null && multichoice)
					WriteLineInd ("else if (" + container + ".@" + map.ChoiceMember + "[" + index + "] == " + GetLiteral (info.ChoiceValue) + ") {");
				else if (multichoice)
					WriteLineInd ("else if (" + item + ".GetType() == typeof(" + info.TypeData.CSharpFullName + ")) {");
				
				if (targetString == null) 
					GenerateWriteMemberElement (info, GetCast (info.TypeData, itemTypeData, item));
				else
				{
					string strVal = GenerateGetStringValue (info.MappedType, info.TypeData, GetCast (info.TypeData, itemTypeData, item), false);
					WriteLine (targetString + ".Append (" + strVal + ").Append (\" \");");
				}

				if (multichoice)
					WriteLineUni ("}");
			}
			
			if (multichoice)
				WriteLine ("else throw CreateUnknownTypeException (" + item + ");");
		}
		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");
		}
示例#30
0
		XmlTypeMapping ImportClassSimpleType (XmlQualifiedName typeQName, XmlSchemaSimpleType stype, XmlQualifiedName root)
		{
			if (CanBeEnum (stype))
			{
				// Create an enum map

				CodeIdentifiers codeIdents = new CodeIdentifiers ();
				XmlTypeMapping enumMap = CreateTypeMapping (typeQName, SchemaTypes.Enum, root);
				enumMap.Documentation = GetDocumentation (stype);
				
				bool isFlags = false;
				if (stype.Content is XmlSchemaSimpleTypeList) {
					stype = ((XmlSchemaSimpleTypeList)stype.Content).ItemType;
					isFlags = true;
				}
				XmlSchemaSimpleTypeRestriction rest = (XmlSchemaSimpleTypeRestriction)stype.Content;

				codeIdents.AddReserved (enumMap.TypeData.TypeName);

				EnumMap.EnumMapMember[] members = new EnumMap.EnumMapMember [rest.Facets.Count];
				for (int n=0; n<rest.Facets.Count; n++)
				{
					XmlSchemaEnumerationFacet enu = (XmlSchemaEnumerationFacet) rest.Facets[n];
					string enumName = codeIdents.AddUnique(CodeIdentifier.MakeValid (enu.Value), enu);
					members [n] = new EnumMap.EnumMapMember (enu.Value, enumName);
					members [n].Documentation = GetDocumentation (enu);
				}
				enumMap.ObjectMap = new EnumMap (members, isFlags);
				enumMap.IsSimpleType = true;
				return enumMap;
			}

			if (stype.Content is XmlSchemaSimpleTypeList)
			{
				XmlSchemaSimpleTypeList slist = (XmlSchemaSimpleTypeList)stype.Content;
				TypeData arrayTypeData = FindBuiltInType (slist.ItemTypeName, stype);

				ListMap listMap = new ListMap ();

				listMap.ItemInfo = new XmlTypeMapElementInfoList ();
				listMap.ItemInfo.Add (CreateElementInfo (typeQName.Namespace, null, "Item", arrayTypeData.ListItemTypeData, false, XmlSchemaForm.None, -1));

				XmlTypeMapping map = CreateArrayTypeMapping (typeQName, arrayTypeData);
				map.ObjectMap = listMap;
				map.IsSimpleType = true;
				return map;
			}

			// It is an extension of a primitive or known type
			
			TypeData typeData = FindBuiltInType (typeQName, stype);
			XmlTypeMapping rmap = GetTypeMapping (typeData);
			
			// The resulting map must be a simple type. It needs to be explicitely set for arrays
			rmap.IsSimpleType = true;
			return rmap;
		}
		void GenerateListLoop (ListMap map, string item, TypeData itemTypeData, string targetString)
		{
			bool multichoice = (map.ItemInfo.Count > 1);

			if (multichoice)
				WriteLine ("if (" + item + " == null) { }");
			
			foreach (XmlTypeMapElementInfo info in map.ItemInfo)
			{
				if (multichoice)
					WriteLineInd ("else if (" + item + ".GetType() == typeof(" + info.TypeData.FullTypeName + ")) {");

				if (targetString == null) 
					GenerateWriteMemberElement (info, GetCast (info.TypeData, itemTypeData, item));
				else
				{
					string strVal = GenerateGetStringValue (info.MappedType, info.TypeData, GetCast (info.TypeData, itemTypeData, item));
					WriteLine (targetString + ".Append (" + strVal + ").Append (\" \");");
				}

				if (multichoice)
					WriteLineUni ("}");
			}
			
			if (multichoice)
				WriteLine ("else throw CreateUnknownTypeException (" + item + ");");
		}
示例#32
0
		ListMap BuildEncodedArrayMap (string type, string ns, out TypeData arrayTypeData)
		{
			ListMap map = new ListMap ();
			
			int i = type.LastIndexOf ("[");
			if (i == -1) throw new InvalidOperationException ("Invalid arrayType value: " + type);
			if (type.IndexOf (",",i) != -1) throw new InvalidOperationException ("Multidimensional arrays are not supported");
			
			string itemType = type.Substring (0,i);
			
			TypeData itemTypeData;
			if (itemType.IndexOf ("[") != -1) 
			{
				ListMap innerListMap = BuildEncodedArrayMap (itemType, ns, out itemTypeData);
				
				int dims = itemType.Split ('[').Length - 1;
				string name = TypeTranslator.GetArrayName (type, dims);
				XmlQualifiedName qname = new XmlQualifiedName (name, ns);
				XmlTypeMapping tmap = CreateArrayTypeMapping (qname, itemTypeData);
				tmap.ObjectMap = innerListMap;
			}
			else
			{
				itemTypeData = GetTypeData (new XmlQualifiedName (itemType, ns), null, false);
			}
			
			arrayTypeData = itemTypeData.ListTypeData;
			
			map.ItemInfo = new XmlTypeMapElementInfoList();
			map.ItemInfo.Add (CreateElementInfo ("", null, "Item", itemTypeData, true, XmlSchemaForm.None, -1));
			return map;
		}
示例#33
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);
            }
        }
		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 ("System.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);
		}
		protected virtual void GenerateArrayItemAttributes (CodeAttributeDeclarationCollection attributes, ListMap listMap, TypeData type, XmlTypeMapElementInfo ainfo, string defaultName, string defaultNamespace, int nestingLevel)
		{
		}