コード例 #1
0
        void AddAttributeFieldMember(CodeTypeDeclaration codeClass, XmlTypeMapMemberAttribute attinfo, string defaultNamespace)
        {
            CodeTypeMember codeField = CreateFieldMember(codeClass, attinfo);

            CodeAttributeDeclarationCollection attributes = codeField.CustomAttributes;

            if (attributes == null)
            {
                attributes = new CodeAttributeDeclarationCollection();
            }

            GenerateAttributeMember(attributes, attinfo, defaultNamespace, false);
            if (attributes.Count > 0)
            {
                codeField.CustomAttributes = attributes;
            }

            if (attinfo.MappedType != null)
            {
                ExportMapCode(attinfo.MappedType, false);
                RemoveInclude(attinfo.MappedType);
            }

            if (attinfo.TypeData.IsValueType && attinfo.IsOptionalValueType)
            {
                codeField            = CreateFieldMember(codeClass, typeof(bool), identifiers.MakeUnique(attinfo.Name + "Specified"));
                codeField.Attributes = MemberAttributes.Public;
                GenerateSpecifierMember(codeField);
            }
        }
コード例 #2
0
        void ReadAttributeMembers(ClassMap map, object ob, bool isValueList)
        {
            XmlTypeMapMember anyAttrMember = map.DefaultAnyAttributeMember;
            int    anyAttributeIndex       = 0;
            object anyAttributeArray       = null;

            while (Reader.MoveToNextAttribute())
            {
                XmlTypeMapMemberAttribute member = map.GetAttribute(Reader.LocalName, Reader.NamespaceURI);

                if (member != null)
                {
                    SetMemberValue(member, ob, GetValueFromXmlString(Reader.Value, member.TypeData, member.MappedType), isValueList);
                }
                else if (IsXmlnsAttribute(Reader.Name))
                {
                    // If the map has NamespaceDeclarations,
                    // then store this xmlns to the given member.
                    // If the instance doesn't exist, then create.
                    if (map.NamespaceDeclarations != null)
                    {
                        XmlSerializerNamespaces nss = this.GetMemberValue(map.NamespaceDeclarations, ob, isValueList) as XmlSerializerNamespaces;
                        if (nss == null)
                        {
                            nss = new XmlSerializerNamespaces();
                            SetMemberValue(map.NamespaceDeclarations, ob, nss, isValueList);
                        }
                        if (Reader.Prefix == "xmlns")
                        {
                            nss.Add(Reader.LocalName, Reader.Value);
                        }
                        else
                        {
                            nss.Add("", Reader.Value);
                        }
                    }
                }
                else if (anyAttrMember != null)
                {
                    XmlAttribute attr = (XmlAttribute)Document.ReadNode(Reader);
                    ParseWsdlArrayType(attr);
                    AddListValue(anyAttrMember.TypeData, ref anyAttributeArray, anyAttributeIndex++, attr, true);
                }
                else
                {
                    ProcessUnknownAttribute(ob);
                }
            }

            if (anyAttrMember != null)
            {
                anyAttributeArray = ShrinkArray((Array)anyAttributeArray, anyAttributeIndex, anyAttrMember.TypeData.Type.GetElementType(), true);
                SetMemberValue(anyAttrMember, ob, anyAttributeArray, isValueList);
            }
            Reader.MoveToElement();
        }
コード例 #3
0
 protected virtual void GenerateAttributeMember(CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberAttribute attinfo, string defaultNamespace, bool forceUseMemberName)
 {
 }
コード例 #4
0
 public void AddAttributeMemberAttributes(XmlTypeMapMemberAttribute attinfo, string defaultNamespace, CodeAttributeDeclarationCollection attributes, bool forceUseMemberName)
 {
     GenerateAttributeMember(attributes, attinfo, defaultNamespace, forceUseMemberName);
 }
コード例 #5
0
        XmlSchemaAttribute GetSchemaAttribute(XmlSchema currentSchema, XmlTypeMapMemberAttribute attinfo, bool isTypeMember)
        {
            XmlSchemaAttribute sat = new XmlSchemaAttribute();

            if (attinfo.DefaultValue != global::System.DBNull.Value)
            {
                sat.DefaultValue = ExportDefaultValue(attinfo.TypeData,
                                                      attinfo.MappedType, attinfo.DefaultValue);
            }
            else
            {
                if (!attinfo.IsOptionalValueType && attinfo.TypeData.IsValueType)
                {
                    sat.Use = XmlSchemaUse.Required;
                }
            }

            ImportNamespace(currentSchema, attinfo.Namespace);

            XmlSchema memberSchema;

            if (attinfo.Namespace.Length == 0 && attinfo.Form != XmlSchemaForm.Qualified)
            {
                memberSchema = currentSchema;
            }
            else
            {
                memberSchema = GetSchema(attinfo.Namespace);
            }

            if (currentSchema == memberSchema || encodedFormat)
            {
                sat.Name = attinfo.AttributeName;
                if (isTypeMember)
                {
                    sat.Form = attinfo.Form;
                }
                if (attinfo.TypeData.SchemaType == SchemaTypes.Enum)
                {
                    ImportNamespace(currentSchema, attinfo.DataTypeNamespace);
                    ExportEnumSchema(attinfo.MappedType);
                    sat.SchemaTypeName = new XmlQualifiedName(attinfo.TypeData.XmlType, attinfo.DataTypeNamespace);
                }
                else if (attinfo.TypeData.SchemaType == SchemaTypes.Array && TypeTranslator.IsPrimitive(attinfo.TypeData.ListItemType))
                {
                    sat.SchemaType = GetSchemaSimpleListType(attinfo.TypeData);
                }
                else
                {
                    sat.SchemaTypeName = new XmlQualifiedName(attinfo.TypeData.XmlType, attinfo.DataTypeNamespace);
                };
            }
            else
            {
                sat.RefName = new XmlQualifiedName(attinfo.AttributeName, attinfo.Namespace);
                foreach (XmlSchemaObject ob in memberSchema.Items)
                {
                    if (ob is XmlSchemaAttribute && ((XmlSchemaAttribute)ob).Name == attinfo.AttributeName)
                    {
                        return(sat);
                    }
                }

                memberSchema.Items.Add(GetSchemaAttribute(memberSchema, attinfo, false));
            }
            return(sat);
        }
コード例 #6
0
        protected override void GenerateAttributeMember(CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberAttribute attinfo, string defaultNamespace, bool forceUseMemberName)
        {
            CodeAttributeDeclaration att = new CodeAttributeDeclaration("Mono.System.Xml.Serialization.SoapAttribute");

            if (attinfo.Name != attinfo.AttributeName)
            {
                att.Arguments.Add(GetArg(attinfo.AttributeName));
            }
            if (attinfo.Namespace != defaultNamespace)
            {
                att.Arguments.Add(GetArg("Namespace", attinfo.Namespace));
            }
            if (!TypeTranslator.IsDefaultPrimitiveTpeData(attinfo.TypeData))
            {
                att.Arguments.Add(GetArg("DataType", attinfo.TypeData.XmlType));
            }
            attributes.Add(att);
        }
コード例 #7
0
        protected override void GenerateAttributeMember(CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberAttribute attinfo, string defaultNamespace, bool forceUseMemberName)
        {
            CodeAttributeDeclaration att = new CodeAttributeDeclaration("Mono.System.Xml.Serialization.XmlAttributeAttribute");

            if (forceUseMemberName || attinfo.Name != attinfo.AttributeName)
            {
                att.Arguments.Add(GetArg(attinfo.AttributeName));
            }
            if (attinfo.Namespace != defaultNamespace)
            {
                att.Arguments.Add(GetArg("Namespace", attinfo.Namespace));
            }
            if (attinfo.Form == XmlSchemaForm.Qualified)
            {
                att.Arguments.Add(GetEnumArg("Form", "Mono.System.Xml.Schema.XmlSchemaForm", attinfo.Form.ToString()));
            }
            if (!TypeTranslator.IsDefaultPrimitiveTpeData(attinfo.TypeData))
            {
                att.Arguments.Add(GetArg("DataType", attinfo.TypeData.XmlType));
            }
            attributes.Add(att);

            if (attinfo.Ignore)
            {
                attributes.Add(new CodeAttributeDeclaration("Mono.System.Xml.Serialization.XmlIgnoreAttribute"));
            }
        }
コード例 #8
0
        public void AddMember(XmlTypeMapMember member)
        {
            // If GlobalIndex has not been set, set it now
            if (member.GlobalIndex == -1)
            {
                member.GlobalIndex = _allMembers.Count;
            }

            _allMembers.Add(member);

            if (!(member.DefaultValue is DBNull) && member.DefaultValue != null)
            {
                if (_membersWithDefault == null)
                {
                    _membersWithDefault = new ArrayList();
                }
                _membersWithDefault.Add(member);
            }

            if (member.IsReturnValue)
            {
                _returnMember = member;
            }

            if (member is XmlTypeMapMemberAttribute)
            {
                XmlTypeMapMemberAttribute atm = (XmlTypeMapMemberAttribute)member;
                if (_attributeMembers == null)
                {
                    _attributeMembers = new Hashtable();
                }
                string key = BuildKey(atm.AttributeName, atm.Namespace, -1);
                if (_attributeMembers.ContainsKey(key))
                {
                    throw new InvalidOperationException("The XML attribute named '" + atm.AttributeName + "' from namespace '" + atm.Namespace + "' is already present in the current scope. Use XML attributes to specify another XML name or namespace for the attribute.");
                }
                member.Index = _attributeMembers.Count;
                _attributeMembers.Add(key, member);
                return;
            }
            else if (member is XmlTypeMapMemberFlatList)
            {
                RegisterFlatList((XmlTypeMapMemberFlatList)member);
            }
            else if (member is XmlTypeMapMemberAnyElement)
            {
                XmlTypeMapMemberAnyElement mem = (XmlTypeMapMemberAnyElement)member;
                if (mem.IsDefaultAny)
                {
                    _defaultAnyElement = mem;
                }
                if (mem.TypeData.IsListType)
                {
                    RegisterFlatList(mem);
                }
            }
            else if (member is XmlTypeMapMemberAnyAttribute)
            {
                _defaultAnyAttribute = (XmlTypeMapMemberAnyAttribute)member;
                return;
            }
            else if (member is XmlTypeMapMemberNamespaces)
            {
                _namespaceDeclarations = (XmlTypeMapMemberNamespaces)member;
                return;
            }

            if (member is XmlTypeMapMemberElement && ((XmlTypeMapMemberElement)member).IsXmlTextCollector)
            {
                if (_xmlTextCollector != null)
                {
                    throw new InvalidOperationException("XmlTextAttribute can only be applied once in a class");
                }
                _xmlTextCollector = member;
            }

            if (_elementMembers == null)
            {
                _elementMembers = new ArrayList();
                _elements       = new Hashtable();
            }

            member.Index = _elementMembers.Count;
            _elementMembers.Add(member);

            ICollection elemsInfo = ((XmlTypeMapMemberElement)member).ElementInfo;

            foreach (XmlTypeMapElementInfo elem in elemsInfo)
            {
                string key = BuildKey(elem.ElementName, elem.Namespace, elem.ExplicitOrder);
                if (_elements.ContainsKey(key))
                {
                    throw new InvalidOperationException("The XML element named '" + elem.ElementName + "' from namespace '" + elem.Namespace + "' is already present in the current scope. Use XML attributes to specify another XML name or namespace for the element.");
                }
                _elements.Add(key, elem);
            }

            if (member.TypeData.IsListType && member.TypeData.Type != null && !member.TypeData.Type.IsArray)
            {
                if (_listMembers == null)
                {
                    _listMembers = new ArrayList();
                }
                _listMembers.Add(member);
            }
        }
コード例 #9
0
        private XmlTypeMapMember CreateMapMember(XmlReflectionMember rmember, string defaultNamespace)
        {
            XmlTypeMapMember mapMember;
            SoapAttributes   atts     = rmember.SoapAttributes;
            TypeData         typeData = TypeTranslator.GetTypeData(rmember.MemberType);

            if (atts.SoapAttribute != null)
            {
                // An attribute

                if (typeData.SchemaType != SchemaTypes.Enum && typeData.SchemaType != SchemaTypes.Primitive)
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
                                                                      "Cannot serialize member '{0}' of type {1}. " +
                                                                      "SoapAttribute cannot be used to encode complex types.",
                                                                      rmember.MemberName, typeData.FullTypeName));
                }

                if (atts.SoapElement != null)
                {
                    throw new Exception("SoapAttributeAttribute and SoapElementAttribute cannot be applied to the same member");
                }

                XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute();
                if (atts.SoapAttribute.AttributeName.Length == 0)
                {
                    mapAttribute.AttributeName = XmlConvert.EncodeLocalName(rmember.MemberName);
                }
                else
                {
                    mapAttribute.AttributeName = XmlConvert.EncodeLocalName(atts.SoapAttribute.AttributeName);
                }

                mapAttribute.Namespace = (atts.SoapAttribute.Namespace != null) ? atts.SoapAttribute.Namespace : "";
                if (typeData.IsComplexType)
                {
                    mapAttribute.MappedType = ImportTypeMapping(typeData.Type, defaultNamespace);
                }

                typeData  = TypeTranslator.GetTypeData(rmember.MemberType, atts.SoapAttribute.DataType);
                mapMember = mapAttribute;
                mapMember.DefaultValue = GetDefaultValue(typeData, atts.SoapDefaultValue);
            }
            else
            {
                if (typeData.SchemaType == SchemaTypes.Array)
                {
                    mapMember = new XmlTypeMapMemberList();
                }
                else
                {
                    mapMember = new XmlTypeMapMemberElement();
                }

                if (atts.SoapElement != null && atts.SoapElement.DataType.Length != 0)
                {
                    typeData = TypeTranslator.GetTypeData(rmember.MemberType, atts.SoapElement.DataType);
                }

                // Creates an ElementInfo that identifies the element
                XmlTypeMapElementInfoList infoList = new XmlTypeMapElementInfoList();
                XmlTypeMapElementInfo     elem     = new XmlTypeMapElementInfo(mapMember, typeData);

                elem.ElementName = XmlConvert.EncodeLocalName((atts.SoapElement != null && atts.SoapElement.ElementName.Length != 0) ? atts.SoapElement.ElementName : rmember.MemberName);
                elem.Namespace   = string.Empty;
                elem.IsNullable  = (atts.SoapElement != null) ? atts.SoapElement.IsNullable : false;
                if (typeData.IsComplexType)
                {
                    elem.MappedType = ImportTypeMapping(typeData.Type, defaultNamespace);
                }

                infoList.Add(elem);
                ((XmlTypeMapMemberElement)mapMember).ElementInfo = infoList;
            }

            mapMember.TypeData      = typeData;
            mapMember.Name          = rmember.MemberName;
            mapMember.IsReturnValue = rmember.IsReturnValue;
            return(mapMember);
        }