상속: TypeMapping
예제 #1
0
        XmlSchemaType ExportSpecialMapping(SpecialMapping mapping, string ns, bool isAny)
        {
            switch (mapping.TypeDesc.Kind)
            {
            case TypeKind.Node: {
                XmlSchemaComplexType type = new XmlSchemaComplexType();
                type.IsMixed = mapping.TypeDesc.IsMixed;
                XmlSchemaSequence seq = new XmlSchemaSequence();
                XmlSchemaAny      any = new XmlSchemaAny();
                if (isAny)
                {
                    type.AnyAttribute = new XmlSchemaAnyAttribute();
                    type.IsMixed      = true;
                    any.MaxOccurs     = decimal.MaxValue;
                }
                seq.Items.Add(any);
                type.Particle = seq;
                return(type);
            }

            case TypeKind.Serializable: {
                SerializableMapping  serializableMapping = (SerializableMapping)mapping;
                XmlSchemaComplexType type = new XmlSchemaComplexType();

                if (serializableMapping.Schema == null)
                {
                    XmlSchemaElement schemaElement = new XmlSchemaElement();
                    schemaElement.RefName = new XmlQualifiedName("schema", XmlSchema.Namespace);
                    XmlSchemaSequence seq = new XmlSchemaSequence();
                    seq.Items.Add(schemaElement);
                    seq.Items.Add(new XmlSchemaAny());
                    type.Particle = seq;
                    AddSchemaImport(XmlSchema.Namespace, ns);
                }
                else
                {
                    XmlSchemaAny      any = new XmlSchemaAny();
                    XmlSchemaSequence seq = new XmlSchemaSequence();
                    seq.Items.Add(any);
                    type.Particle = seq;
                    string anyNs = serializableMapping.Schema.TargetNamespace;
                    any.Namespace = anyNs == null ? "" : anyNs;
                    XmlSchema existingSchema = schemas[anyNs];
                    if (existingSchema == null)
                    {
                        schemas.Add(serializableMapping.Schema);
                        AddSchemaImport(anyNs, ns);
                    }
                    else if (existingSchema != serializableMapping.Schema)
                    {
                        throw new InvalidOperationException(Res.GetString(Res.XmlDuplicateNamespace, anyNs));
                    }
                }
                return(type);
            }

            default:
                throw new ArgumentException(Res.GetString(Res.XmlInternalError), "mapping");
            }
        }
예제 #2
0
 internal static bool IsWildcard(SpecialMapping mapping)
 {
     if (mapping is SerializableMapping)
     {
         return(((SerializableMapping)mapping).IsAny);
     }
     return(mapping.TypeDesc.CanBeElementValue);
 }
예제 #3
0
        private void WriteAttribute(object o, MemberMapping member, object attr = null)
        {
            AttributeAccessor attribute = member.Attribute;
            object            value     = null;

            if (attribute.Mapping is SpecialMapping)
            {
                SpecialMapping special = (SpecialMapping)attribute.Mapping;

                if (special.TypeDesc.Kind == TypeKind.Attribute)
                {
                    value = attr;
                }
                else if (special.TypeDesc.CanBeAttributeValue)
                {
                    // #10590: To Support special.TypeDesc.CanBeAttributeValue == true
                    throw new NotImplementedException("special.TypeDesc.CanBeAttributeValue");
                }
                else
                {
                    throw new InvalidOperationException(SR.Format(SR.XmlInternalError));
                }
            }
            else
            {
                if (attribute.IsList)
                {
                    string   listValues = Reader.Value;
                    string[] vals       = listValues.Split(null);
                    Array    arrayValue = Array.CreateInstance(member.TypeDesc.Type.GetElementType(), vals.Length);
                    for (int i = 0; i < vals.Length; i++)
                    {
                        arrayValue.SetValue(WritePrimitive(attribute.Mapping, () => vals[i]), i);
                    }

                    value = arrayValue;
                }
                else
                {
                    value = WritePrimitive(attribute.Mapping, () => Reader.Value);
                }
            }

            SetOrAddValueToMember(o, value, member.MemberInfo);

            if (member.CheckSpecified == SpecifiedAccessor.ReadWrite)
            {
                // #10591: we need to add tests for this block.
                string     specifiedMemberName = member.Name + "Specified";
                MethodInfo specifiedMethodInfo = o.GetType().GetMethod("set_" + specifiedMemberName);
                if (specifiedMethodInfo != null)
                {
                    specifiedMethodInfo.Invoke(o, new object[] { true });
                }
            }
        }
        private bool WriteMemberText(out object o, Member anyText)
        {
            MemberMapping anyTextMapping = anyText.Mapping;

            if ((Reader.NodeType == XmlNodeType.Text ||
                 Reader.NodeType == XmlNodeType.CDATA ||
                 Reader.NodeType == XmlNodeType.Whitespace ||
                 Reader.NodeType == XmlNodeType.SignificantWhitespace))
            {
                TextAccessor text = anyTextMapping.Text;

                if (text.Mapping is SpecialMapping)
                {
                    SpecialMapping special = (SpecialMapping)text.Mapping;
                    if (special.TypeDesc.Kind == TypeKind.Node)
                    {
                        o = Document.CreateTextNode(ReadString());
                    }
                    else
                    {
                        throw new InvalidOperationException(SR.Format(SR.XmlInternalError));
                    }
                }
                else
                {
                    if (anyTextMapping.TypeDesc.IsArrayLike)
                    {
                        if (text.Mapping.TypeDesc.CollapseWhitespace)
                        {
                            o = CollapseWhitespace(ReadString());
                        }
                        else
                        {
                            o = ReadString();
                        }
                    }
                    else
                    {
                        if (text.Mapping.TypeDesc == StringTypeDesc || text.Mapping.TypeDesc.FormatterName == "String")
                        {
                            o = ReadString(null, text.Mapping.TypeDesc.CollapseWhitespace);
                        }
                        else
                        {
                            o = WritePrimitive(text.Mapping, () => ReadString());
                        }
                    }
                }

                return(true);
            }

            o = null;
            return(false);
        }
예제 #5
0
        /// <include file='doc\XmlSchemaExporter.uex' path='docs/doc[@for="XmlSchemaExporter.ExportTypeMapping1"]/*' />
        public XmlQualifiedName ExportTypeMapping(XmlMembersMapping xmlMembersMapping)
        {
            CheckScope(xmlMembersMapping.Scope);
            MembersMapping mapping = (MembersMapping)xmlMembersMapping.Accessor.Mapping;

            if (mapping.Members.Length == 1 && mapping.Members[0].Elements[0].Mapping is SpecialMapping)
            {
                SpecialMapping special = (SpecialMapping)mapping.Members[0].Elements[0].Mapping;
                XmlSchemaType  type    = ExportSpecialMapping(special, xmlMembersMapping.Accessor.Namespace, false);
                type.Name = xmlMembersMapping.Accessor.Name;
                AddSchemaItem(type, xmlMembersMapping.Accessor.Namespace, null);
                ExportRootIfNecessary(xmlMembersMapping.Scope);
                return(new XmlQualifiedName(xmlMembersMapping.Accessor.Name, xmlMembersMapping.Accessor.Namespace));
            }
            return(null);
        }
예제 #6
0
        private void WriteText(object o, TextAccessor text)
        {
            if (text.Mapping is PrimitiveMapping)
            {
                PrimitiveMapping mapping = (PrimitiveMapping)text.Mapping;
                string           stringValue;
                if (text.Mapping is EnumMapping)
                {
                    stringValue = WriteEnumMethod((EnumMapping)mapping, o);
                }
                else
                {
                    if (!WritePrimitiveValue(mapping.TypeDesc, o, false, out stringValue))
                    {
                        // #10593: Add More Tests for Serialization Code
                        Debug.Assert(o is byte[]);
                    }
                }

                if (o is byte[])
                {
                    WriteValue((byte[])o);
                }
                else
                {
                    WriteValue(stringValue);
                }
            }
            else if (text.Mapping is SpecialMapping)
            {
                SpecialMapping mapping = (SpecialMapping)text.Mapping;
                switch (mapping.TypeDesc.Kind)
                {
                case TypeKind.Node:
                    ((XmlNode)o).WriteTo(Writer);
                    break;

                default:
                    throw new InvalidOperationException(SR.Format(SR.XmlInternalError));
                }
            }
        }
예제 #7
0
 private void WriteAttribute(object memberValue, AttributeAccessor attribute, object parent)
 {
     if (attribute.Mapping is SpecialMapping)
     {
         // TODO: this block is never hit by our tests.
         SpecialMapping special = (SpecialMapping)attribute.Mapping;
         if (special.TypeDesc.Kind == TypeKind.Attribute || special.TypeDesc.CanBeAttributeValue)
         {
             WriteXmlAttribute((XmlNode)memberValue, parent);
         }
         else
         {
             throw new InvalidOperationException(SR.XmlInternalError);
         }
     }
     else
     {
         string ns = attribute.Form == XmlSchemaForm.Qualified ? attribute.Namespace : "";
         WritePrimitive(WritePrimitiveMethodRequirement.WriteAttribute, attribute.Name, ns, attribute.Default, memberValue, attribute.Mapping, false, false, false);
     }
 }
예제 #8
0
        void ImportAnyMember(XmlSchemaAny any, string identifier, CodeIdentifiers members, string ns, bool mixed) {

            ElementAccessor accessor = ImportAny(any, !mixed);
            accessor.Name = identifier;
            accessor.Namespace = ns;

            MemberMapping member = new MemberMapping();
            member.Elements = new ElementAccessor[] { accessor };
            member.Name = members.MakeRightCase("Any");
            member.Name = members.AddUnique(member.Name, member);
            member.TypeDesc = ((TypeMapping)accessor.Mapping).TypeDesc;

            bool repeats = any.IsMultipleOccurrence;

            if (mixed) {
                SpecialMapping textMapping = new SpecialMapping();
                textMapping.TypeDesc = scope.GetTypeDesc(typeof(XmlNode));
                textMapping.TypeName = textMapping.TypeDesc.Name;
                member.TypeDesc = textMapping.TypeDesc;
                TextAccessor text = new TextAccessor();
                text.Mapping = textMapping;
                member.Text = text;
                repeats = true;
            }

            if (repeats) {
                member.TypeDesc = member.TypeDesc.CreateArrayTypeDesc();
            }
        }
예제 #9
0
        XmlSchemaType ExportSpecialMapping(SpecialMapping mapping, string ns, bool isAny, XmlSchemaElement element) {
            switch (mapping.TypeDesc.Kind) {
                case TypeKind.Node: {
                    XmlSchemaComplexType type = new XmlSchemaComplexType();
                    type.IsMixed = mapping.TypeDesc.IsMixed;
                    XmlSchemaSequence seq = new XmlSchemaSequence();
                    XmlSchemaAny any = new XmlSchemaAny();
                    if (isAny) {
                        type.AnyAttribute = new XmlSchemaAnyAttribute();
                        type.IsMixed = true;
                        any.MaxOccurs = decimal.MaxValue;
                    }
                    seq.Items.Add(any);
                    type.Particle = seq;
                    if (element != null)
                        element.SchemaType = type;
                    return type;
                }
                case TypeKind.Serializable: {
                    SerializableMapping serializableMapping = (SerializableMapping)mapping;
                    if (serializableMapping.IsAny) {
                        XmlSchemaComplexType type = new XmlSchemaComplexType();
                        type.IsMixed = mapping.TypeDesc.IsMixed;
                        XmlSchemaSequence seq = new XmlSchemaSequence();
                        XmlSchemaAny any = new XmlSchemaAny();
                        if (isAny) {
                            type.AnyAttribute = new XmlSchemaAnyAttribute();
                            type.IsMixed = true;
                            any.MaxOccurs = decimal.MaxValue;
                        }
                        if (serializableMapping.NamespaceList.Length > 0)
                            any.Namespace = serializableMapping.NamespaceList;
                        any.ProcessContents = XmlSchemaContentProcessing.Lax;

                        if (serializableMapping.Schemas != null) {
                            foreach (XmlSchema schema in serializableMapping.Schemas.Schemas()) {
                                if (schema.TargetNamespace != XmlSchema.Namespace) {
                                    schemas.Add(schema, true);
                                    AddSchemaImport(schema.TargetNamespace, ns);
                                }
                            }
                        }
                        seq.Items.Add(any);
                        type.Particle = seq;
                        if (element != null)
                            element.SchemaType = type;
                        return type;
                    }
                    else if (serializableMapping.XsiType != null || serializableMapping.XsdType != null) {
                        XmlSchemaType type = serializableMapping.XsdType;
                        // for performance reasons we need to postpone merging of the serializable schemas
                        foreach (XmlSchema schema in serializableMapping.Schemas.Schemas()) {
                            if (schema.TargetNamespace != XmlSchema.Namespace) {
                                schemas.Add(schema, true);
                                AddSchemaImport(schema.TargetNamespace, ns);
                                if (!serializableMapping.XsiType.IsEmpty && serializableMapping.XsiType.Namespace == schema.TargetNamespace)
                                    type = (XmlSchemaType)schema.SchemaTypes[serializableMapping.XsiType];
                            }
                        }
                        if (element != null) {
                            element.SchemaTypeName = serializableMapping.XsiType;
                            if (element.SchemaTypeName.IsEmpty)
                                element.SchemaType = type;
                        }
                        // check for duplicate top-level elements XmlAttributes
                        serializableMapping.CheckDuplicateElement(element, ns);
                        return type;
                    }
                    else if (serializableMapping.Schema != null) {
                        // this is the strongly-typed DataSet
                        XmlSchemaComplexType type = new XmlSchemaComplexType();
                        XmlSchemaAny any = new XmlSchemaAny();
                        XmlSchemaSequence seq = new XmlSchemaSequence();
                        seq.Items.Add(any);
                        type.Particle = seq;
                        string anyNs = serializableMapping.Schema.TargetNamespace;
                        any.Namespace = anyNs == null ? "" : anyNs;
                        XmlSchema existingSchema = schemas[anyNs];
                        if (existingSchema == null) {
                            schemas.Add(serializableMapping.Schema);
                        }
                        else if (existingSchema != serializableMapping.Schema) {
                            throw new InvalidOperationException(Res.GetString(Res.XmlDuplicateNamespace, anyNs));
                        }
                        if (element != null)
                            element.SchemaType = type;

                        // check for duplicate top-level elements XmlAttributes
                        serializableMapping.CheckDuplicateElement(element, ns);
                        return type;
                    }
                    else {
                        // DataSet
                        XmlSchemaComplexType type = new XmlSchemaComplexType();
                        XmlSchemaElement schemaElement = new XmlSchemaElement();
                        schemaElement.RefName = new XmlQualifiedName("schema", XmlSchema.Namespace);
                        XmlSchemaSequence seq = new XmlSchemaSequence();
                        seq.Items.Add(schemaElement);
                        seq.Items.Add(new XmlSchemaAny());
                        type.Particle = seq;
                        AddSchemaImport(XmlSchema.Namespace, ns);
                        if (element != null)
                            element.SchemaType = type;
                        return type;
                    }
                }
                default:
                    throw new ArgumentException(Res.GetString(Res.XmlInternalError), "mapping");
            }
        }
 private SpecialMapping ImportAnyMapping(XmlSchemaType type, string identifier, string ns, bool repeats)
 {
     if (type == null)
     {
         return null;
     }
     if (!type.DerivedFrom.IsEmpty)
     {
         return null;
     }
     bool flag = IsMixed(type);
     TypeItems typeItems = this.GetTypeItems(type);
     if (typeItems.Particle == null)
     {
         return null;
     }
     if (!(typeItems.Particle is XmlSchemaAll) && !(typeItems.Particle is XmlSchemaSequence))
     {
         return null;
     }
     if ((typeItems.Attributes != null) && (typeItems.Attributes.Count > 0))
     {
         return null;
     }
     XmlSchemaGroupBase particle = typeItems.Particle;
     if ((particle.Items.Count != 1) || !(particle.Items[0] is XmlSchemaAny))
     {
         return null;
     }
     XmlSchemaAny context = (XmlSchemaAny) particle.Items[0];
     SpecialMapping mapping = new SpecialMapping();
     if (((typeItems.AnyAttribute != null) && context.IsMultipleOccurrence) && flag)
     {
         mapping.NamedAny = true;
         mapping.TypeDesc = base.Scope.GetTypeDesc(typeof(XmlElement));
     }
     else
     {
         if ((typeItems.AnyAttribute != null) || context.IsMultipleOccurrence)
         {
             return null;
         }
         mapping.TypeDesc = base.Scope.GetTypeDesc(flag ? typeof(XmlNode) : typeof(XmlElement));
     }
     TypeFlags canBeElementValue = TypeFlags.CanBeElementValue;
     if ((typeItems.AnyAttribute != null) || flag)
     {
         canBeElementValue |= TypeFlags.CanBeTextValue;
     }
     this.RunSchemaExtensions(mapping, XmlQualifiedName.Empty, null, context, canBeElementValue);
     mapping.TypeName = mapping.TypeDesc.Name;
     if (repeats)
     {
         mapping.TypeDesc = mapping.TypeDesc.CreateArrayTypeDesc();
     }
     return mapping;
 }
 private MemberMapping ImportAnyMember(XmlSchemaAny any, string identifier, CodeIdentifiers members, CodeIdentifiers membersScope, INameScope elementsScope, string ns, ref bool mixed, ref bool needExplicitOrder, bool allowDuplicates)
 {
     MemberMapping mapping;
     ElementAccessor[] elements = this.ImportAny(any, !mixed, ns);
     this.AddScopeElements(elementsScope, elements, ref needExplicitOrder, allowDuplicates);
     mapping = new MemberMapping {
         Elements = elements,
         Name = membersScope.MakeRightCase("Any"),
         Name = membersScope.AddUnique(mapping.Name, mapping)
     };
     members.Add(mapping.Name, mapping);
     mapping.TypeDesc = elements[0].Mapping.TypeDesc;
     bool isMultipleOccurrence = any.IsMultipleOccurrence;
     if (mixed)
     {
         SpecialMapping mapping2;
         mapping2 = new SpecialMapping {
             TypeDesc = base.Scope.GetTypeDesc(typeof(XmlNode)),
             TypeName = mapping2.TypeDesc.Name
         };
         mapping.TypeDesc = mapping2.TypeDesc;
         TextAccessor accessor = new TextAccessor {
             Mapping = mapping2
         };
         mapping.Text = accessor;
         isMultipleOccurrence = true;
         mixed = false;
     }
     if (isMultipleOccurrence)
     {
         mapping.TypeDesc = mapping.TypeDesc.CreateArrayTypeDesc();
     }
     return mapping;
 }
        SpecialMapping ImportSpecialMapping(Type type, TypeDesc typeDesc, string ns, ImportContext context) {
            if (specials == null)
                specials = new Hashtable();
            SpecialMapping mapping = (SpecialMapping)specials[type];
            if (mapping != null) {
                CheckContext(mapping.TypeDesc, context);
                return mapping;
            }
            if (typeDesc.Kind == TypeKind.Serializable) {
                SerializableMapping serializableMapping = null;

                // get the schema method info
                object[] attrs = type.GetCustomAttributes(typeof(XmlSchemaProviderAttribute), false);

                if (attrs.Length > 0) {
                    // new IXmlSerializable
                    XmlSchemaProviderAttribute provider = (XmlSchemaProviderAttribute)attrs[0];
                    MethodInfo method = GetMethodFromSchemaProvider(provider, type);
                    serializableMapping = new SerializableMapping(method, provider.IsAny, ns);
                    XmlQualifiedName qname = serializableMapping.XsiType;
                    if (qname != null && !qname.IsEmpty) {
                        if (serializables == null)
                            serializables = new NameTable();
                        SerializableMapping existingMapping = (SerializableMapping)serializables[qname];
                        if (existingMapping != null) {
                            if (existingMapping.Type == null) {
                                serializableMapping = existingMapping;
                            }
                            else if (existingMapping.Type != type) {
                                SerializableMapping next = existingMapping.Next;
                                existingMapping.Next = serializableMapping;
                                serializableMapping.Next = next;
                            }
                        }
                        else {
                            XmlSchemaType xsdType = serializableMapping.XsdType;
                            if (xsdType != null)
                                SetBase(serializableMapping, xsdType.DerivedFrom);
                            serializables[qname] = serializableMapping;
                        }
                        serializableMapping.TypeName = qname.Name;
                        serializableMapping.Namespace = qname.Namespace;
                    }
                    serializableMapping.TypeDesc = typeDesc;
                    serializableMapping.Type = type;
                    IncludeTypes(type);
                }
                else {
                    // old IXmlSerializable
                    serializableMapping = new SerializableMapping();
                    serializableMapping.TypeDesc = typeDesc;
                    serializableMapping.Type = type;
                }
                mapping = serializableMapping;
            }
            else {
                mapping = new SpecialMapping();
                mapping.TypeDesc = typeDesc;
            }
            CheckContext(typeDesc, context);
            specials.Add(type, mapping);
            typeScope.AddTypeMapping(mapping);
            return mapping;
        }
예제 #13
0
 SpecialMapping ImportSpecialMapping(Type type, TypeDesc typeDesc, string ns, ImportContext context) {
     SpecialMapping mapping = (SpecialMapping)specials[type];
     if (mapping != null) {
         CheckContext(mapping.TypeDesc, context);
         return mapping;
     }
     if (typeDesc.Kind == TypeKind.Serializable) {
         SerializableMapping serializableMapping = new SerializableMapping();
         serializableMapping.Type = type;
         mapping = serializableMapping;
     }
     else {
         mapping = new SpecialMapping();
     }
     mapping.TypeDesc = typeDesc;
     mapping.TypeName = typeDesc.Name;
     CheckContext(typeDesc, context);
     specials.Add(type, mapping);
     return mapping;
 }
 private void ImportAnyAttributeMember(XmlSchemaAnyAttribute any, CodeIdentifiers members, CodeIdentifiers membersScope)
 {
     SpecialMapping mapping;
     MemberMapping mapping2;
     mapping = new SpecialMapping {
         TypeDesc = base.Scope.GetTypeDesc(typeof(XmlAttribute)),
         TypeName = mapping.TypeDesc.Name
     };
     AttributeAccessor accessor = new AttributeAccessor {
         Any = true,
         Mapping = mapping
     };
     mapping2 = new MemberMapping {
         Elements = new ElementAccessor[0],
         Attribute = accessor,
         Name = membersScope.MakeRightCase("AnyAttr"),
         Name = membersScope.AddUnique(mapping2.Name, mapping2)
     };
     members.Add(mapping2.Name, mapping2);
     mapping2.TypeDesc = accessor.Mapping.TypeDesc;
     mapping2.TypeDesc = mapping2.TypeDesc.CreateArrayTypeDesc();
 }
 private SpecialMapping ImportSpecialMapping(Type type, TypeDesc typeDesc, string ns, ImportContext context, RecursionLimiter limiter)
 {
     if (this.specials == null)
     {
         this.specials = new Hashtable();
     }
     SpecialMapping mapping = (SpecialMapping) this.specials[type];
     if (mapping != null)
     {
         this.CheckContext(mapping.TypeDesc, context);
         return mapping;
     }
     if (typeDesc.Kind == TypeKind.Serializable)
     {
         SerializableMapping mapping2 = null;
         object[] customAttributes = type.GetCustomAttributes(typeof(XmlSchemaProviderAttribute), false);
         if (customAttributes.Length > 0)
         {
             XmlSchemaProviderAttribute provider = (XmlSchemaProviderAttribute) customAttributes[0];
             mapping2 = new SerializableMapping(GetMethodFromSchemaProvider(provider, type), provider.IsAny, ns);
             XmlQualifiedName xsiType = mapping2.XsiType;
             if ((xsiType != null) && !xsiType.IsEmpty)
             {
                 if (this.serializables == null)
                 {
                     this.serializables = new System.Xml.Serialization.NameTable();
                 }
                 SerializableMapping mapping3 = (SerializableMapping) this.serializables[xsiType];
                 if (mapping3 != null)
                 {
                     if (mapping3.Type == null)
                     {
                         mapping2 = mapping3;
                     }
                     else if (mapping3.Type != type)
                     {
                         SerializableMapping next = mapping3.Next;
                         mapping3.Next = mapping2;
                         mapping2.Next = next;
                     }
                 }
                 else
                 {
                     XmlSchemaType xsdType = mapping2.XsdType;
                     if (xsdType != null)
                     {
                         this.SetBase(mapping2, xsdType.DerivedFrom);
                     }
                     this.serializables[xsiType] = mapping2;
                 }
                 mapping2.TypeName = xsiType.Name;
                 mapping2.Namespace = xsiType.Namespace;
             }
             mapping2.TypeDesc = typeDesc;
             mapping2.Type = type;
             this.IncludeTypes(type);
         }
         else
         {
             mapping2 = new SerializableMapping {
                 TypeDesc = typeDesc,
                 Type = type
             };
         }
         mapping = mapping2;
     }
     else
     {
         mapping = new SpecialMapping {
             TypeDesc = typeDesc
         };
     }
     this.CheckContext(typeDesc, context);
     this.specials.Add(type, mapping);
     this.typeScope.AddTypeMapping(mapping);
     return mapping;
 }
예제 #16
0
        private SpecialMapping ImportSpecialMapping(Type type, TypeDesc typeDesc, string ns, ImportContext context, RecursionLimiter limiter)
        {
            if (_specials == null)
                _specials = new Hashtable();
            SpecialMapping mapping = (SpecialMapping)_specials[type];
            if (mapping != null)
            {
                CheckContext(mapping.TypeDesc, context);
                return mapping;
            }
            if (typeDesc.Kind == TypeKind.Serializable)
            {
                SerializableMapping serializableMapping = null;

                // get the schema method info
                object[] attrs = type.GetTypeInfo().GetCustomAttributes(typeof(XmlSchemaProviderAttribute), false).ToArray();

                if (attrs.Length > 0)
                {
                    // new IXmlSerializable
                    XmlSchemaProviderAttribute provider = (XmlSchemaProviderAttribute)attrs[0];
                    MethodInfo method = GetMethodFromSchemaProvider(provider, type);
                    serializableMapping = new SerializableMapping(method, provider.IsAny, ns);
                    XmlQualifiedName qname = serializableMapping.XsiType;
                    if (qname != null && !qname.IsEmpty)
                    {
                        serializableMapping.TypeName = qname.Name;
                        serializableMapping.Namespace = qname.Namespace;
                    }
                    serializableMapping.TypeDesc = typeDesc;
                    serializableMapping.Type = type;
                    IncludeTypes(type.GetTypeInfo());
                }
                else
                {
                    // old IXmlSerializable
                    serializableMapping = new SerializableMapping();
                    serializableMapping.TypeDesc = typeDesc;
                    serializableMapping.Type = type;
                }
                mapping = serializableMapping;
            }
            else
            {
                mapping = new SpecialMapping();
                mapping.TypeDesc = typeDesc;
            }
            CheckContext(typeDesc, context);
            _specials.Add(type, mapping);
            _typeScope.AddTypeMapping(mapping);
            return mapping;
        }
예제 #17
0
        void ImportAnyAttributeMember(XmlSchemaAnyAttribute any, CodeIdentifiers members) {
            SpecialMapping mapping = new SpecialMapping();
            mapping.TypeDesc = scope.GetTypeDesc(typeof(XmlAttribute));
            mapping.TypeName = mapping.TypeDesc.Name;

            AttributeAccessor accessor = new AttributeAccessor();
            accessor.Any = true;
            accessor.Mapping = mapping;

            MemberMapping member = new MemberMapping();
            member.Elements = new ElementAccessor[0];
            member.Attribute = accessor;
            member.Name = members.MakeRightCase("AnyAttr");
            member.Name = members.AddUnique(member.Name, member);
            member.TypeDesc = ((TypeMapping)accessor.Mapping).TypeDesc;
            member.TypeDesc = member.TypeDesc.CreateArrayTypeDesc();
        }
예제 #18
0
        private SpecialMapping ImportAnyMapping(XmlSchemaType type, string identifier, string ns, bool repeats)
        {
            if (type == null) return null;
            if (!type.DerivedFrom.IsEmpty) return null;

            bool mixed = IsMixed(type);
            TypeItems items = GetTypeItems(type);
            if (items.Particle == null) return null;
            if (!(items.Particle is XmlSchemaAll || items.Particle is XmlSchemaSequence)) return null;
            if (items.Attributes != null && items.Attributes.Count > 0) return null;
            XmlSchemaGroupBase group = (XmlSchemaGroupBase)items.Particle;

            if (group.Items.Count != 1 || !(group.Items[0] is XmlSchemaAny)) return null;
            XmlSchemaAny any = (XmlSchemaAny)group.Items[0];

            SpecialMapping mapping = new SpecialMapping();
            // check for special named any case
            if (items.AnyAttribute != null && any.IsMultipleOccurrence && mixed)
            {
                mapping.NamedAny = true;
                mapping.TypeDesc = Scope.GetTypeDesc(typeof(XmlElement));
            }
            else if (items.AnyAttribute != null || any.IsMultipleOccurrence)
            {
                // these only work for named any case -- otherwise import as struct
                return null;
            }
            else
            {
                mapping.TypeDesc = Scope.GetTypeDesc(mixed ? typeof(XmlNode) : typeof(XmlElement));
            }

            TypeFlags flags = TypeFlags.CanBeElementValue;
            if (items.AnyAttribute != null || mixed)
                flags |= TypeFlags.CanBeTextValue;

            // let the extensions to run
            RunSchemaExtensions(mapping, XmlQualifiedName.Empty, null, any, flags);

            mapping.TypeName = mapping.TypeDesc.Name;
            if (repeats)
                mapping.TypeDesc = mapping.TypeDesc.CreateArrayTypeDesc();

            return mapping;
        }
예제 #19
0
        private ElementAccessor[] ImportAny(XmlSchemaAny any, bool makeElement, string targetNamespace)
        {
            SpecialMapping mapping = new SpecialMapping();

            mapping.TypeDesc = Scope.GetTypeDesc(makeElement ? typeof(XmlElement) : typeof(XmlNode));
            mapping.TypeName = mapping.TypeDesc.Name;


            TypeFlags flags = TypeFlags.CanBeElementValue;
            if (makeElement)
                flags |= TypeFlags.CanBeTextValue;

            // let the extensions to run
            RunSchemaExtensions(mapping, XmlQualifiedName.Empty, null, any, flags);

            if (GenerateOrder && any.Namespace != null)
            {
                NamespaceList list = new NamespaceList(any.Namespace, targetNamespace);

                if (list.Type == NamespaceList.ListType.Set)
                {
                    ICollection namespaces = list.Enumerate;
                    ElementAccessor[] accessors = new ElementAccessor[namespaces.Count == 0 ? 1 : namespaces.Count];
                    int count = 0;
                    foreach (string ns in list.Enumerate)
                    {
                        ElementAccessor accessor = new ElementAccessor();
                        accessor.Mapping = mapping;
                        accessor.Any = true;
                        accessor.Namespace = ns;
                        accessors[count++] = accessor;
                    }
                    if (count > 0)
                    {
                        return accessors;
                    }
                }
            }

            ElementAccessor anyAccessor = new ElementAccessor();
            anyAccessor.Mapping = mapping;
            anyAccessor.Any = true;
            return new ElementAccessor[] { anyAccessor };
        }
예제 #20
0
        private MemberMapping ImportAnyMember(XmlSchemaAny any, string identifier, CodeIdentifiers members, CodeIdentifiers membersScope, INameScope elementsScope, string ns, ref bool mixed, ref bool needExplicitOrder, bool allowDuplicates)
        {
            ElementAccessor[] accessors = ImportAny(any, !mixed, ns);
            AddScopeElements(elementsScope, accessors, ref needExplicitOrder, allowDuplicates);
            MemberMapping member = new MemberMapping();
            member.Elements = accessors;
            member.Name = membersScope.MakeRightCase("Any");
            member.Name = membersScope.AddUnique(member.Name, member);
            members.Add(member.Name, member);
            member.TypeDesc = ((TypeMapping)accessors[0].Mapping).TypeDesc;

            bool repeats = any.IsMultipleOccurrence;

            if (mixed)
            {
                SpecialMapping textMapping = new SpecialMapping();
                textMapping.TypeDesc = Scope.GetTypeDesc(typeof(XmlNode));
                textMapping.TypeName = textMapping.TypeDesc.Name;
                member.TypeDesc = textMapping.TypeDesc;
                TextAccessor text = new TextAccessor();
                text.Mapping = textMapping;
                member.Text = text;
                repeats = true;
                mixed = false;
            }

            if (repeats)
            {
                member.TypeDesc = member.TypeDesc.CreateArrayTypeDesc();
            }
            return member;
        }
 private XmlSchemaType ExportSpecialMapping(SpecialMapping mapping, string ns, bool isAny, XmlSchemaElement element)
 {
     switch (mapping.TypeDesc.Kind)
     {
         case TypeKind.Node:
         {
             XmlSchemaComplexType type = new XmlSchemaComplexType {
                 IsMixed = mapping.TypeDesc.IsMixed
             };
             XmlSchemaSequence sequence = new XmlSchemaSequence();
             XmlSchemaAny item = new XmlSchemaAny();
             if (isAny)
             {
                 type.AnyAttribute = new XmlSchemaAnyAttribute();
                 type.IsMixed = true;
                 item.MaxOccurs = 79228162514264337593543950335M;
             }
             sequence.Items.Add(item);
             type.Particle = sequence;
             if (element != null)
             {
                 element.SchemaType = type;
             }
             return type;
         }
         case TypeKind.Serializable:
         {
             SerializableMapping mapping2 = (SerializableMapping) mapping;
             if (!mapping2.IsAny)
             {
                 if ((mapping2.XsiType != null) || (mapping2.XsdType != null))
                 {
                     XmlSchemaType xsdType = mapping2.XsdType;
                     foreach (XmlSchema schema2 in mapping2.Schemas.Schemas())
                     {
                         if (schema2.TargetNamespace != "http://www.w3.org/2001/XMLSchema")
                         {
                             this.schemas.Add(schema2, true);
                             this.AddSchemaImport(schema2.TargetNamespace, ns);
                             if (!mapping2.XsiType.IsEmpty && (mapping2.XsiType.Namespace == schema2.TargetNamespace))
                             {
                                 xsdType = (XmlSchemaType) schema2.SchemaTypes[mapping2.XsiType];
                             }
                         }
                     }
                     if (element != null)
                     {
                         element.SchemaTypeName = mapping2.XsiType;
                         if (element.SchemaTypeName.IsEmpty)
                         {
                             element.SchemaType = xsdType;
                         }
                     }
                     mapping2.CheckDuplicateElement(element, ns);
                     return xsdType;
                 }
                 if (mapping2.Schema != null)
                 {
                     XmlSchemaComplexType type4 = new XmlSchemaComplexType();
                     XmlSchemaAny any3 = new XmlSchemaAny();
                     XmlSchemaSequence sequence3 = new XmlSchemaSequence();
                     sequence3.Items.Add(any3);
                     type4.Particle = sequence3;
                     string targetNamespace = mapping2.Schema.TargetNamespace;
                     any3.Namespace = (targetNamespace == null) ? "" : targetNamespace;
                     XmlSchema schema3 = this.schemas[targetNamespace];
                     if (schema3 == null)
                     {
                         this.schemas.Add(mapping2.Schema);
                     }
                     else if (schema3 != mapping2.Schema)
                     {
                         throw new InvalidOperationException(Res.GetString("XmlDuplicateNamespace", new object[] { targetNamespace }));
                     }
                     if (element != null)
                     {
                         element.SchemaType = type4;
                     }
                     mapping2.CheckDuplicateElement(element, ns);
                     return type4;
                 }
                 XmlSchemaComplexType type5 = new XmlSchemaComplexType();
                 XmlSchemaElement element2 = new XmlSchemaElement {
                     RefName = new XmlQualifiedName("schema", "http://www.w3.org/2001/XMLSchema")
                 };
                 XmlSchemaSequence sequence4 = new XmlSchemaSequence();
                 sequence4.Items.Add(element2);
                 sequence4.Items.Add(new XmlSchemaAny());
                 type5.Particle = sequence4;
                 this.AddSchemaImport("http://www.w3.org/2001/XMLSchema", ns);
                 if (element != null)
                 {
                     element.SchemaType = type5;
                 }
                 return type5;
             }
             XmlSchemaComplexType type2 = new XmlSchemaComplexType {
                 IsMixed = mapping.TypeDesc.IsMixed
             };
             XmlSchemaSequence sequence2 = new XmlSchemaSequence();
             XmlSchemaAny any2 = new XmlSchemaAny();
             if (isAny)
             {
                 type2.AnyAttribute = new XmlSchemaAnyAttribute();
                 type2.IsMixed = true;
                 any2.MaxOccurs = 79228162514264337593543950335M;
             }
             if (mapping2.NamespaceList.Length > 0)
             {
                 any2.Namespace = mapping2.NamespaceList;
             }
             any2.ProcessContents = XmlSchemaContentProcessing.Lax;
             if (mapping2.Schemas != null)
             {
                 foreach (XmlSchema schema in mapping2.Schemas.Schemas())
                 {
                     if (schema.TargetNamespace != "http://www.w3.org/2001/XMLSchema")
                     {
                         this.schemas.Add(schema, true);
                         this.AddSchemaImport(schema.TargetNamespace, ns);
                     }
                 }
             }
             sequence2.Items.Add(any2);
             type2.Particle = sequence2;
             if (element != null)
             {
                 element.SchemaType = type2;
             }
             return type2;
         }
     }
     throw new ArgumentException(Res.GetString("XmlInternalError"), "mapping");
 }
예제 #22
0
        ElementAccessor ImportAny(XmlSchemaAny any, bool makeElement) {
            SpecialMapping mapping = new SpecialMapping();
            mapping.TypeDesc = scope.GetTypeDesc(makeElement ? typeof(XmlElement) : typeof(XmlNode));
            mapping.TypeName = mapping.TypeDesc.Name;

            ElementAccessor accessor = new ElementAccessor();
            accessor.Mapping = mapping;
            accessor.Any = true;
            return accessor;
        }
예제 #23
0
        private void WriteElement(ref object o, CollectionMember collectionMember, ElementAccessor element, ChoiceIdentifierAccessor choice, bool checkSpecified, bool checkForNull, bool readOnly, string defaultNamespace, object masterObject = null)
        {
            object value = null;

            if (element.Mapping is ArrayMapping)
            {
                WriteArray(ref value, (ArrayMapping)element.Mapping, readOnly, element.IsNullable, defaultNamespace);
            }
            else if (element.Mapping is NullableMapping)
            {
                value = WriteNullableMethod((NullableMapping)element.Mapping, true, defaultNamespace);
            }
            else if (!element.Mapping.IsSoap && (element.Mapping is PrimitiveMapping))
            {
                if (element.IsNullable && ReadNull())
                {
                    if (element.Mapping.TypeDesc.IsValueType)
                    {
                        value = ReflectionCreateObject(element.Mapping.TypeDesc.Type);
                    }
                    else
                    {
                        value = null;
                    }
                }
                else if ((element.Default != null && !Globals.IsDBNullValue(element.Default) && element.Mapping.TypeDesc.IsValueType) &&
                         (Reader.IsEmptyElement))
                {
                    Reader.Skip();
                }
                else
                {
                    if (element.Mapping.TypeDesc == QnameTypeDesc)
                    {
                        value = ReadElementQualifiedName();
                    }
                    else
                    {
                        if (element.Mapping.TypeDesc.FormatterName == "ByteArrayBase64")
                        {
                            value = ToByteArrayBase64(false);
                        }
                        else if (element.Mapping.TypeDesc.FormatterName == "ByteArrayHex")
                        {
                            value = ToByteArrayHex(false);
                        }
                        else
                        {
                            Func <string> readFunc = () => Reader.ReadElementContentAsString();

                            value = WritePrimitive(element.Mapping, readFunc);
                        }
                    }
                }
            }
            else if (element.Mapping is StructMapping || (element.Mapping.IsSoap && element.Mapping is PrimitiveMapping))
            {
                TypeMapping mapping = element.Mapping;
                if (mapping.IsSoap)
                {
                    throw new PlatformNotSupportedException();
                }
                else
                {
                    if (checkForNull && o == null)
                    {
                        Reader.Skip();
                    }
                    else
                    {
                        value = WriteStructMethod(
                            mapping: (StructMapping)mapping,
                            isNullable: mapping.TypeDesc.IsNullable && element.IsNullable,
                            checkType: true,
                            defaultNamespace: defaultNamespace
                            );
                    }
                }
            }
            else if (element.Mapping is SpecialMapping)
            {
                SpecialMapping special = (SpecialMapping)element.Mapping;
                switch (special.TypeDesc.Kind)
                {
                case TypeKind.Node:
                    bool isDoc = special.TypeDesc.FullName == typeof(XmlDocument).FullName;
                    if (isDoc)
                    {
                        value = ReadXmlDocument(!element.Any);
                    }
                    else
                    {
                        value = ReadXmlNode(!element.Any);
                    }

                    break;

                case TypeKind.Serializable:
                    SerializableMapping sm = (SerializableMapping)element.Mapping;
                    // check to see if we need to do the derivation
                    bool flag = true;
                    if (sm.DerivedMappings != null)
                    {
                        XmlQualifiedName tser = GetXsiType();
                        if (tser == null || QNameEqual(tser, sm.XsiType.Name, sm.XsiType.Namespace, defaultNamespace))
                        {
                        }
                        else
                        {
                            flag = false;
                        }
                    }

                    if (flag)
                    {
                        bool isWrappedAny = !element.Any && IsWildcard(sm);
                        value = ReadSerializable((IXmlSerializable)ReflectionCreateObject(sm.TypeDesc.Type), isWrappedAny);
                    }

                    if (sm.DerivedMappings != null)
                    {
                        // #10587: To Support SpecialMapping Types Having DerivedMappings
                        throw new NotImplementedException("sm.DerivedMappings != null");
                        //WriteDerivedSerializable(sm, sm, source, isWrappedAny);
                        //WriteUnknownNode("UnknownNode", "null", null, true);
                    }
                    break;

                default:
                    throw new InvalidOperationException(SR.Format(SR.XmlInternalError));
                }
            }
            else
            {
                throw new InvalidOperationException(SR.Format(SR.XmlInternalError));
            }

            if (choice != null && masterObject != null)
            {
                foreach (var name in choice.MemberIds)
                {
                    if (name == element.Name)
                    {
                        object choiceValue = Enum.Parse(choice.Mapping.TypeDesc.Type, name);
                        SetOrAddValueToMember(masterObject, choiceValue, choice.MemberInfo);

                        break;
                    }
                }
            }

            if (collectionMember != null)
            {
                collectionMember.Add(value);
            }
            else
            {
                o = value;
            }
        }
예제 #24
0
        SpecialMapping ImportAnyMapping(XmlSchemaType type, string identifier, string ns, bool repeats) {
            if (type == null) return null;
            if (!type.DerivedFrom.IsEmpty) return null;

            bool mixed = IsMixed(type);
            TypeItems items = GetTypeItems(type);
            if (items.Particle == null) return null;
            if (!(items.Particle is XmlSchemaAll || items.Particle is XmlSchemaSequence)) return null;
            if (items.AnyAttribute != null) return null;
            if (items.Attributes != null && items.Attributes.Count > 0) return null;
            XmlSchemaGroupBase group = (XmlSchemaGroupBase) items.Particle;

            if (group.Items.Count != 1 || !(group.Items[0] is XmlSchemaAny)) return null;
            XmlSchemaAny any = (XmlSchemaAny)group.Items[0];

            SpecialMapping mapping = new SpecialMapping();
            mapping.TypeDesc = scope.GetTypeDesc(mixed ? typeof(XmlNode) : typeof(XmlElement));
            mapping.TypeName = mapping.TypeDesc.Name;
            if (any.IsMultipleOccurrence || repeats)
                mapping.TypeDesc = mapping.TypeDesc.CreateArrayTypeDesc();

            return mapping;
        }
 internal static bool IsWildcard(SpecialMapping mapping) {
     if (mapping is SerializableMapping)
         return ((SerializableMapping)mapping).IsAny;
     return mapping.TypeDesc.CanBeElementValue;
 }
 private ElementAccessor[] ImportAny(XmlSchemaAny any, bool makeElement, string targetNamespace)
 {
     SpecialMapping mapping;
     mapping = new SpecialMapping {
         TypeDesc = base.Scope.GetTypeDesc(makeElement ? typeof(XmlElement) : typeof(XmlNode)),
         TypeName = mapping.TypeDesc.Name
     };
     TypeFlags canBeElementValue = TypeFlags.CanBeElementValue;
     if (makeElement)
     {
         canBeElementValue |= TypeFlags.CanBeTextValue;
     }
     this.RunSchemaExtensions(mapping, XmlQualifiedName.Empty, null, any, canBeElementValue);
     if (this.GenerateOrder && (any.Namespace != null))
     {
         NamespaceList list = new NamespaceList(any.Namespace, targetNamespace);
         if (list.Type == NamespaceList.ListType.Set)
         {
             ICollection enumerate = list.Enumerate;
             ElementAccessor[] accessorArray = new ElementAccessor[(enumerate.Count == 0) ? 1 : enumerate.Count];
             int num = 0;
             foreach (string str in list.Enumerate)
             {
                 ElementAccessor accessor = new ElementAccessor {
                     Mapping = mapping,
                     Any = true,
                     Namespace = str
                 };
                 accessorArray[num++] = accessor;
             }
             if (num > 0)
             {
                 return accessorArray;
             }
         }
     }
     ElementAccessor accessor2 = new ElementAccessor {
         Mapping = mapping,
         Any = true
     };
     return new ElementAccessor[] { accessor2 };
 }