Пример #1
0
 XmlQualifiedName ExportTypeMapping(TypeMapping mapping, string ns)
 {
     if (mapping is ArrayMapping)
     {
         return(ExportArrayMapping((ArrayMapping)mapping, ns));
     }
     else if (mapping is EnumMapping)
     {
         return(ExportEnumMapping((EnumMapping)mapping, ns));
     }
     else if (mapping is PrimitiveMapping)
     {
         PrimitiveMapping pm = (PrimitiveMapping)mapping;
         if (pm.TypeDesc.IsXsdType)
         {
             return(ExportPrimitiveMapping(pm));
         }
         else
         {
             return(ExportNonXsdPrimitiveMapping(pm, ns));
         }
     }
     else if (mapping is StructMapping)
     {
         return(ExportStructMapping((StructMapping)mapping, ns));
     }
     else if (mapping is MembersMapping)
     {
         return(ExportMembersMapping((MembersMapping)mapping, ns));
     }
     else
     {
         throw new ArgumentException(Res.GetString(Res.XmlInternalError), "mapping");
     }
 }
Пример #2
0
 internal TypeMapping GetDefaultMapping(TypeFlags flags) {
     PrimitiveMapping mapping = new PrimitiveMapping();
     mapping.TypeDesc = Scope.GetTypeDesc("string", XmlSchema.Namespace, flags);
     mapping.TypeName = mapping.TypeDesc.DataType.Name;
     mapping.Namespace = XmlSchema.Namespace;
     return mapping;
 }
 private XmlQualifiedName ExportTypeMapping(TypeMapping mapping, string ns)
 {
     if (mapping is ArrayMapping)
     {
         return(this.ExportArrayMapping((ArrayMapping)mapping, ns));
     }
     if (mapping is EnumMapping)
     {
         return(this.ExportEnumMapping((EnumMapping)mapping, ns));
     }
     if (mapping is PrimitiveMapping)
     {
         PrimitiveMapping mapping2 = (PrimitiveMapping)mapping;
         if (mapping2.TypeDesc.IsXsdType)
         {
             return(this.ExportPrimitiveMapping(mapping2));
         }
         return(this.ExportNonXsdPrimitiveMapping(mapping2, ns));
     }
     if (mapping is StructMapping)
     {
         return(this.ExportStructMapping((StructMapping)mapping, ns));
     }
     if (mapping is NullableMapping)
     {
         return(this.ExportTypeMapping(((NullableMapping)mapping).BaseMapping, ns));
     }
     if (!(mapping is MembersMapping))
     {
         throw new ArgumentException(Res.GetString("XmlInternalError"), "mapping");
     }
     return(this.ExportMembersMapping((MembersMapping)mapping, ns));
 }
Пример #4
0
        private PrimitiveMapping ImportPrimitiveMapping(PrimitiveModel model, string dataType)
        {
            PrimitiveMapping mapping = new PrimitiveMapping {
                IsSoap = true
            };

            if (dataType.Length > 0)
            {
                mapping.TypeDesc = this.typeScope.GetTypeDesc(dataType, "http://www.w3.org/2001/XMLSchema");
                if (mapping.TypeDesc == null)
                {
                    mapping.TypeDesc = this.typeScope.GetTypeDesc(dataType, "http://microsoft.com/wsdl/types/");
                    if (mapping.TypeDesc == null)
                    {
                        throw new InvalidOperationException(Res.GetString("XmlUdeclaredXsdType", new object[] { dataType }));
                    }
                }
            }
            else
            {
                mapping.TypeDesc = model.TypeDesc;
            }
            mapping.TypeName  = mapping.TypeDesc.DataType.Name;
            mapping.Namespace = mapping.TypeDesc.IsXsdType ? "http://www.w3.org/2001/XMLSchema" : "http://microsoft.com/wsdl/types/";
            return(mapping);
        }
Пример #5
0
        private PrimitiveMapping ImportPrimitiveMapping(PrimitiveModel model, string dataType)
        {
            PrimitiveMapping mapping = new PrimitiveMapping();

            mapping.IsSoap = true;
            if (dataType.Length > 0)
            {
                mapping.TypeDesc = _typeScope.GetTypeDesc(dataType, XmlSchema.Namespace);
                if (mapping.TypeDesc == null)
                {
                    // try it as a non-Xsd type
                    mapping.TypeDesc = _typeScope.GetTypeDesc(dataType, UrtTypes.Namespace);
                    if (mapping.TypeDesc == null)
                    {
                        throw new InvalidOperationException(SR.Format(SR.XmlUdeclaredXsdType, dataType));
                    }
                }
            }
            else
            {
                mapping.TypeDesc = model.TypeDesc;
            }
            mapping.TypeName  = mapping.TypeDesc.DataType.Name;
            mapping.Namespace = mapping.TypeDesc.IsXsdType ? XmlSchema.Namespace : UrtTypes.Namespace;
            return(mapping);
        }
Пример #6
0
        PrimitiveMapping ImportPrimitiveMapping(PrimitiveModel model, string dataType)
        {
            PrimitiveMapping mapping = new PrimitiveMapping();

            mapping.IsSoap = true;
            if (dataType.Length > 0)
            {
                mapping.TypeDesc = typeScope.GetTypeDesc(new XmlQualifiedName(dataType, XmlSchema.Namespace));
                if (mapping.TypeDesc == null)
                {
                    // try it as a non-Xsd type
                    mapping.TypeDesc = typeScope.GetTypeDesc(new XmlQualifiedName(dataType, UrtTypes.Namespace));
                    if (mapping.TypeDesc == null)
                    {
                        throw new InvalidOperationException(Res.GetString(Res.XmlUdeclaredXsdType, dataType));
                    }
                }
            }
            else
            {
                mapping.TypeDesc = model.TypeDesc;
            }
            mapping.TypeName  = mapping.TypeDesc.DataType.Name;
            mapping.Namespace = mapping.TypeDesc.IsXsdType ? XmlSchema.Namespace : UrtTypes.Namespace;
            if (mapping.TypeDesc.IsXsdType && mapping.TypeName == "hexBinary")
            {
                throw new NotSupportedException(Res.GetString(Res.XmlUnsupportedSoapTypeKind, mapping.TypeName));
            }
            return(mapping);
        }
 private object ImportDefaultValue(TypeMapping mapping, string defaultValue)
 {
     if (defaultValue == null)
     {
         return(null);
     }
     if (mapping is PrimitiveMapping)
     {
         if (mapping is EnumMapping)
         {
             EnumMapping       mapping2  = (EnumMapping)mapping;
             ConstantMapping[] constants = mapping2.Constants;
             if (mapping2.IsFlags)
             {
                 Hashtable vals     = new Hashtable();
                 string[]  strArray = new string[constants.Length];
                 long[]    ids      = new long[constants.Length];
                 for (int j = 0; j < constants.Length; j++)
                 {
                     ids[j]      = mapping2.IsFlags ? (((long)1L) << j) : ((long)j);
                     strArray[j] = constants[j].Name;
                     vals.Add(constants[j].Name, ids[j]);
                 }
                 return(XmlCustomFormatter.FromEnum(XmlCustomFormatter.ToEnum(defaultValue, vals, mapping2.TypeName, true), strArray, ids, mapping2.TypeDesc.FullName));
             }
             for (int i = 0; i < constants.Length; i++)
             {
                 if (constants[i].XmlName == defaultValue)
                 {
                     return(constants[i].Name);
                 }
             }
             throw new InvalidOperationException(Res.GetString("XmlInvalidDefaultValue", new object[] { defaultValue, mapping2.TypeDesc.FullName }));
         }
         PrimitiveMapping mapping3 = (PrimitiveMapping)mapping;
         if (!mapping3.TypeDesc.HasCustomFormatter)
         {
             if (mapping3.TypeDesc.FormatterName == "String")
             {
                 return(defaultValue);
             }
             if (mapping3.TypeDesc.FormatterName == "DateTime")
             {
                 return(XmlCustomFormatter.ToDateTime(defaultValue));
             }
             Type       type   = typeof(XmlConvert);
             MethodInfo method = type.GetMethod("To" + mapping3.TypeDesc.FormatterName, new Type[] { typeof(string) });
             if (method != null)
             {
                 return(method.Invoke(type, new object[] { defaultValue }));
             }
         }
         else if (mapping3.TypeDesc.HasDefaultSupport)
         {
             return(XmlCustomFormatter.ToDefaultValue(defaultValue, mapping3.TypeDesc.FormatterName));
         }
     }
     return(DBNull.Value);
 }
Пример #8
0
        private TypeMapping ImportEnumeratedDataType(XmlSchemaSimpleType dataType, string typeNs, string identifier, bool isList)
        {
            TypeMapping mapping = (TypeMapping)base.ImportedMappings[dataType];

            if (mapping != null)
            {
                return(mapping);
            }
            XmlSchemaSimpleType type     = this.FindDataType(dataType.DerivedFrom);
            TypeDesc            typeDesc = base.Scope.GetTypeDesc(type);

            if ((typeDesc != null) && (typeDesc != base.Scope.GetTypeDesc(typeof(string))))
            {
                return(this.ImportPrimitiveDataType(dataType));
            }
            identifier = Accessor.UnescapeName(identifier);
            string      name     = base.GenerateUniqueTypeName(identifier);
            EnumMapping mapping2 = new EnumMapping {
                IsReference = base.Schemas.IsReference(dataType),
                TypeDesc    = new TypeDesc(name, name, TypeKind.Enum, null, TypeFlags.None),
                TypeName    = identifier,
                Namespace   = typeNs,
                IsFlags     = isList
            };
            CodeIdentifiers identifiers = new CodeIdentifiers();

            if (!(dataType.Content is XmlSchemaSimpleTypeRestriction))
            {
                throw new InvalidOperationException(Res.GetString("XmlInvalidEnumContent", new object[] { dataType.Content.GetType().Name, identifier }));
            }
            XmlSchemaSimpleTypeRestriction content = (XmlSchemaSimpleTypeRestriction)dataType.Content;

            for (int i = 0; i < content.Facets.Count; i++)
            {
                object obj2 = content.Facets[i];
                if (obj2 is XmlSchemaEnumerationFacet)
                {
                    XmlSchemaEnumerationFacet facet    = (XmlSchemaEnumerationFacet)obj2;
                    ConstantMapping           mapping3 = new ConstantMapping();
                    string str2 = CodeIdentifier.MakeValid(facet.Value);
                    mapping3.Name    = identifiers.AddUnique(str2, mapping3);
                    mapping3.XmlName = facet.Value;
                    mapping3.Value   = i;
                }
            }
            mapping2.Constants = (ConstantMapping[])identifiers.ToArray(typeof(ConstantMapping));
            if (isList && (mapping2.Constants.Length > 0x3f))
            {
                mapping = new PrimitiveMapping {
                    TypeDesc = base.Scope.GetTypeDesc(typeof(string)),
                    TypeName = mapping.TypeDesc.DataType.Name
                };
                base.ImportedMappings.Add(dataType, mapping);
                return(mapping);
            }
            base.ImportedMappings.Add(dataType, mapping2);
            base.Scope.AddTypeMapping(mapping2);
            return(mapping2);
        }
Пример #9
0
        private PrimitiveMapping ImportPrimitiveDataType(XmlSchemaSimpleType dataType)
        {
            TypeDesc         sourceTypeDesc = GetDataTypeSource(dataType);
            PrimitiveMapping mapping        = new PrimitiveMapping();

            mapping.TypeDesc = sourceTypeDesc;
            mapping.TypeName = sourceTypeDesc.DataType.Name;
            return(mapping);
        }
Пример #10
0
        XmlQualifiedName ExportNonXsdPrimitiveMapping(PrimitiveMapping mapping, string ns)
        {
            XmlSchemaSimpleType type = mapping.TypeDesc.DataType;

            if (!SchemaContainsItem(type, UrtTypes.Namespace))
            {
                AddSchemaItem(type, UrtTypes.Namespace, ns);
            }
            return(new XmlQualifiedName(mapping.TypeDesc.DataType.Name, UrtTypes.Namespace));
        }
        private XmlQualifiedName ExportNonXsdPrimitiveMapping(PrimitiveMapping mapping, string ns)
        {
            XmlSchemaType dataType = mapping.TypeDesc.DataType;

            if (!this.SchemaContainsItem(dataType, "http://microsoft.com/wsdl/types/"))
            {
                this.AddSchemaItem(dataType, "http://microsoft.com/wsdl/types/", ns);
            }
            else
            {
                this.AddSchemaImport("http://microsoft.com/wsdl/types/", ns);
            }
            return(new XmlQualifiedName(mapping.TypeDesc.DataType.Name, "http://microsoft.com/wsdl/types/"));
        }
Пример #12
0
        PrimitiveMapping ImportNonXsdPrimitiveDataType(XmlSchemaSimpleType dataType)
        {
            PrimitiveMapping mapping  = null;
            TypeDesc         typeDesc = null;

            if (dataType.Name != null && dataType.Name.Length != 0)
            {
                typeDesc = scope.GetTypeDesc(new XmlQualifiedName(dataType.Name, UrtTypes.Namespace));
                if (typeDesc != null)
                {
                    mapping          = new PrimitiveMapping();
                    mapping.TypeDesc = typeDesc;
                    mapping.TypeName = typeDesc.DataType.Name;
                }
            }
            return(mapping);
        }
Пример #13
0
        private PrimitiveMapping ImportNonXsdPrimitiveDataType(XmlSchemaSimpleType dataType, string ns)
        {
            PrimitiveMapping mapping  = null;
            TypeDesc         typeDesc = null;

            if (dataType.Name != null && dataType.Name.Length != 0)
            {
                typeDesc = Scope.GetTypeDesc(dataType.Name, ns);
                if (typeDesc != null)
                {
                    mapping          = new PrimitiveMapping();
                    mapping.TypeDesc = typeDesc;
                    mapping.TypeName = typeDesc.DataType.Name;
                }
            }
            return(mapping);
        }
Пример #14
0
        private PrimitiveMapping ImportNonXsdPrimitiveDataType(XmlSchemaSimpleType dataType, string ns)
        {
            PrimitiveMapping mapping  = null;
            TypeDesc         typeDesc = null;

            if ((dataType.Name != null) && (dataType.Name.Length != 0))
            {
                typeDesc = base.Scope.GetTypeDesc(dataType.Name, ns);
                if (typeDesc != null)
                {
                    mapping = new PrimitiveMapping {
                        TypeDesc = typeDesc,
                        TypeName = typeDesc.DataType.Name
                    };
                }
            }
            return(mapping);
        }
Пример #15
0
        TypeMapping ImportDataType(XmlSchemaSimpleType dataType, string typeNs, string identifier)
        {
            TypeMapping mapping = ImportNonXsdPrimitiveDataType(dataType);

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

            if (dataType.Content is XmlSchemaSimpleTypeRestriction)
            {
                XmlSchemaSimpleTypeRestriction restriction = (XmlSchemaSimpleTypeRestriction)dataType.Content;
                foreach (object o in restriction.Facets)
                {
                    if (o is XmlSchemaEnumerationFacet)
                    {
                        return(ImportEnumeratedDataType(dataType, typeNs, identifier));
                    }
                }
            }
            else if (dataType.Content is XmlSchemaSimpleTypeList || dataType.Content is XmlSchemaSimpleTypeUnion)
            {
                if (dataType.Content is XmlSchemaSimpleTypeList)
                {
                    // check if we have enumeration list
                    XmlSchemaSimpleTypeList list = (XmlSchemaSimpleTypeList)dataType.Content;
                    if (list.ItemType != null)
                    {
                        mapping = ImportDataType(list.ItemType, typeNs, identifier);
                        if (mapping != null && mapping is EnumMapping)
                        {
                            ((EnumMapping)mapping).IsFlags = true;
                            return(mapping);
                        }
                    }
                }
                mapping          = new PrimitiveMapping();
                mapping.TypeDesc = scope.GetTypeDesc(typeof(string));
                mapping.TypeName = mapping.TypeDesc.DataType.Name;
                return(mapping);
            }
            return(ImportPrimitiveDataType(dataType));
        }
Пример #16
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));
                }
            }
        }
Пример #17
0
        XmlQualifiedName ExportPrimitiveMapping(PrimitiveMapping mapping, string ns)
        {
            XmlQualifiedName qname;

            if (mapping is EnumMapping)
            {
                qname = ExportEnumMapping((EnumMapping)mapping, ns);
            }
            else
            {
                if (mapping.TypeDesc.IsXsdType)
                {
                    qname = new XmlQualifiedName(mapping.TypeDesc.DataType.Name, XmlSchema.Namespace);
                }
                else
                {
                    qname = ExportNonXsdPrimitiveMapping(mapping, ns);
                }
            }
            return(qname);
        }
Пример #18
0
        private ArrayMapping ImportArrayMapping(XmlSchemaType type, string ns)
        {
            ArrayMapping mapping;

            if ((type.Name == "Array") && (ns == "http://schemas.xmlsoap.org/soap/encoding/"))
            {
                mapping = new ArrayMapping();
                TypeMapping     rootMapping = base.GetRootMapping();
                ElementAccessor accessor    = new ElementAccessor {
                    IsSoap     = true,
                    Name       = "anyType",
                    Namespace  = ns,
                    Mapping    = rootMapping,
                    IsNullable = true,
                    Form       = XmlSchemaForm.None
                };
                mapping.Elements = new ElementAccessor[] { accessor };
                mapping.TypeDesc = accessor.Mapping.TypeDesc.CreateArrayTypeDesc();
                mapping.TypeName = "ArrayOf" + CodeIdentifier.MakePascal(accessor.Mapping.TypeName);
                return(mapping);
            }
            if ((type.DerivedFrom.Name != "Array") || (type.DerivedFrom.Namespace != "http://schemas.xmlsoap.org/soap/encoding/"))
            {
                return(null);
            }
            XmlSchemaContentModel contentModel = ((XmlSchemaComplexType)type).ContentModel;

            if (!(contentModel.Content is XmlSchemaComplexContentRestriction))
            {
                return(null);
            }
            mapping = new ArrayMapping();
            XmlSchemaComplexContentRestriction content = (XmlSchemaComplexContentRestriction)contentModel.Content;

            for (int i = 0; i < content.Attributes.Count; i++)
            {
                XmlSchemaAttribute parent = content.Attributes[i] as XmlSchemaAttribute;
                if (((parent != null) && (parent.RefName.Name == "arrayType")) && (parent.RefName.Namespace == "http://schemas.xmlsoap.org/soap/encoding/"))
                {
                    string str = null;
                    if (parent.UnhandledAttributes != null)
                    {
                        foreach (XmlAttribute attribute2 in parent.UnhandledAttributes)
                        {
                            if ((attribute2.LocalName == "arrayType") && (attribute2.NamespaceURI == "http://schemas.xmlsoap.org/wsdl/"))
                            {
                                str = attribute2.Value;
                                break;
                            }
                        }
                    }
                    if (str != null)
                    {
                        string           str2;
                        TypeMapping      mapping3;
                        XmlQualifiedName name     = TypeScope.ParseWsdlArrayType(str, out str2, parent);
                        TypeDesc         typeDesc = base.Scope.GetTypeDesc(name.Name, name.Namespace);
                        if ((typeDesc != null) && typeDesc.IsPrimitive)
                        {
                            mapping3 = new PrimitiveMapping {
                                TypeDesc = typeDesc,
                                TypeName = typeDesc.DataType.Name
                            };
                        }
                        else
                        {
                            mapping3 = this.ImportType(name, false);
                        }
                        ElementAccessor accessor2 = new ElementAccessor {
                            IsSoap     = true,
                            Name       = name.Name,
                            Namespace  = ns,
                            Mapping    = mapping3,
                            IsNullable = true,
                            Form       = XmlSchemaForm.None
                        };
                        mapping.Elements = new ElementAccessor[] { accessor2 };
                        mapping.TypeDesc = accessor2.Mapping.TypeDesc.CreateArrayTypeDesc();
                        mapping.TypeName = "ArrayOf" + CodeIdentifier.MakePascal(accessor2.Mapping.TypeName);
                        return(mapping);
                    }
                }
            }
            XmlSchemaParticle particle = content.Particle;

            if (!(particle is XmlSchemaAll) && !(particle is XmlSchemaSequence))
            {
                return(null);
            }
            XmlSchemaGroupBase base2 = (XmlSchemaGroupBase)particle;

            if ((base2.Items.Count != 1) || !(base2.Items[0] is XmlSchemaElement))
            {
                return(null);
            }
            XmlSchemaElement element = (XmlSchemaElement)base2.Items[0];

            if (!element.IsMultipleOccurrence)
            {
                return(null);
            }
            ElementAccessor accessor3 = this.ImportElement(element, ns);

            mapping.Elements = new ElementAccessor[] { accessor3 };
            mapping.TypeDesc = accessor3.Mapping.TypeDesc.CreateArrayTypeDesc();
            return(mapping);
        }
 private AttributeAccessor ImportSpecialAttribute(XmlQualifiedName name, string identifier)
 {
     PrimitiveMapping mapping;
     mapping = new PrimitiveMapping {
         TypeDesc = base.Scope.GetTypeDesc(typeof(string)),
         TypeName = mapping.TypeDesc.DataType.Name
     };
     AttributeAccessor accessor = new AttributeAccessor {
         Name = name.Name,
         Namespace = "http://www.w3.org/XML/1998/namespace"
     };
     accessor.CheckSpecial();
     accessor.Mapping = mapping;
     return accessor;
 }
 XmlQualifiedName ExportPrimitiveMapping(PrimitiveMapping mapping)
 {
     return(new XmlQualifiedName(mapping.TypeDesc.DataType.Name, XmlSchema.Namespace));
 }
Пример #21
0
 XmlQualifiedName ExportPrimitiveMapping(PrimitiveMapping mapping, string ns) {
     XmlQualifiedName qname;
     if (mapping is EnumMapping) {
         XmlSchemaType type = ExportEnumMapping((EnumMapping)mapping, ns);
         qname = new XmlQualifiedName(type.Name, mapping.Namespace);
     }
     else {
         if (mapping.TypeDesc.IsXsdType) {
             qname = new XmlQualifiedName(mapping.TypeDesc.DataType.Name, XmlSchema.Namespace);
         }
         else {
             qname = ExportNonXsdPrimitiveMapping(mapping, ns);
         }
     }
     return qname;
 }
Пример #22
0
 XmlQualifiedName ExportNonXsdPrimitiveMapping(PrimitiveMapping mapping, string ns) {
     XmlSchemaSimpleType type = (XmlSchemaSimpleType)mapping.TypeDesc.DataType;
     if (!SchemaContainsItem(type, UrtTypes.Namespace)) {
         AddSchemaItem(type, UrtTypes.Namespace, ns);
     }
     else {
         AddSchemaImport(mapping.Namespace, ns);
     }
     return new XmlQualifiedName(mapping.TypeDesc.DataType.Name, UrtTypes.Namespace);
 }
Пример #23
0
        static internal string ExportDefaultValue(TypeMapping mapping, object value)
        {
            #if DEBUG
            // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
            if (!(mapping is PrimitiveMapping))
            {
                throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Mapping " + mapping.GetType() + ", should not have Default"));
            }
            else if (mapping.IsList)
            {
                throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Mapping " + mapping.GetType() + ", should not have Default"));
            }
            #endif


            if (mapping is EnumMapping)
            {
                EnumMapping em = (EnumMapping)mapping;

                #if DEBUG
                // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
                if (value.GetType() != typeof(string))
                {
                    throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, Res.GetString(Res.XmlInvalidDefaultValue, value.ToString(), value.GetType().FullName)));
                }
                #endif

                // check the validity of the value
                ConstantMapping[] c = em.Constants;
                if (em.IsFlags)
                {
                    string[]  names  = new string[c.Length];
                    long[]    ids    = new long[c.Length];
                    Hashtable values = new Hashtable();
                    for (int i = 0; i < c.Length; i++)
                    {
                        names[i] = c[i].XmlName;
                        ids[i]   = 1 << i;
                        values.Add(c[i].Name, ids[i]);
                    }
                    long val = XmlCustomFormatter.ToEnum((string)value, values, em.TypeName, false);
                    return(val != 0 ? XmlCustomFormatter.FromEnum(val, names, ids) : null);
                }
                else
                {
                    for (int i = 0; i < c.Length; i++)
                    {
                        if (c[i].Name == (string)value)
                        {
                            return(c[i].XmlName);
                        }
                    }
                    return(null); // unknown value
                }
            }

            PrimitiveMapping pm = (PrimitiveMapping)mapping;

            if (!pm.TypeDesc.HasCustomFormatter)
            {
                #if DEBUG
                // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
                if (pm.TypeDesc.Type == null)
                {
                    throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Mapping for " + pm.TypeDesc.Name + " missing type property"));
                }
                #endif

                if (pm.TypeDesc.FormatterName == "String")
                {
                    return((string)value);
                }

                Type formatter = typeof(XmlConvert);
                System.Reflection.MethodInfo format = formatter.GetMethod("ToString", new Type[] { pm.TypeDesc.Type });
                if (format != null)
                {
                    return((string)format.Invoke(formatter, new Object[] { value }));
                }
            }
            else
            {
                string defaultValue = XmlCustomFormatter.FromDefaultValue(value, pm.TypeDesc.FormatterName);
                if (defaultValue == null)
                {
                    throw new InvalidOperationException(Res.GetString(Res.XmlInvalidDefaultValue, value.ToString(), pm.TypeDesc.Name));
                }
                return(defaultValue);
            }
            throw new InvalidOperationException(Res.GetString(Res.XmlInvalidDefaultValue, value.ToString(), pm.TypeDesc.Name));
        }
        private void AddDefaultValueAttribute(CodeMemberField field, CodeAttributeDeclarationCollection metadata, object defaultValue, TypeMapping mapping, CodeCommentStatementCollection comments, TypeDesc memberTypeDesc, Accessor accessor, CodeConstructor ctor)
        {
            string str = accessor.IsFixed ? "fixed" : "default";

            if (!memberTypeDesc.HasDefaultSupport)
            {
                if ((comments != null) && (defaultValue is string))
                {
                    DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName);
                    CodeExporter.AddWarningComment(comments, Res.GetString("XmlDropAttributeValue", new object[] { str, mapping.TypeName, defaultValue.ToString() }));
                }
            }
            else if (memberTypeDesc.IsArrayLike && (accessor is ElementAccessor))
            {
                if ((comments != null) && (defaultValue is string))
                {
                    DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName);
                    CodeExporter.AddWarningComment(comments, Res.GetString("XmlDropArrayAttributeValue", new object[] { str, defaultValue.ToString(), ((ElementAccessor)accessor).Name }));
                }
            }
            else if ((mapping.TypeDesc.IsMappedType && (field != null)) && (defaultValue is string))
            {
                SchemaImporterExtension extension = mapping.TypeDesc.ExtendedType.Extension;
                CodeExpression          init      = extension.ImportDefaultValue((string)defaultValue, mapping.TypeDesc.FullName);
                if (init != null)
                {
                    if (ctor != null)
                    {
                        AddInitializationStatement(ctor, field, init);
                    }
                    else
                    {
                        field.InitExpression = extension.ImportDefaultValue((string)defaultValue, mapping.TypeDesc.FullName);
                    }
                }
                if (comments != null)
                {
                    DropDefaultAttribute(accessor, comments, mapping.TypeDesc.FullName);
                    if (init == null)
                    {
                        CodeExporter.AddWarningComment(comments, Res.GetString("XmlNotKnownDefaultValue", new object[] { extension.GetType().FullName, str, (string)defaultValue, mapping.TypeName, mapping.Namespace }));
                    }
                }
            }
            else
            {
                object obj2 = null;
                if ((defaultValue is string) || (defaultValue == null))
                {
                    obj2 = this.ImportDefault(mapping, (string)defaultValue);
                }
                if (obj2 != null)
                {
                    if (!(mapping is PrimitiveMapping))
                    {
                        DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName);
                        CodeExporter.AddWarningComment(comments, Res.GetString("XmlDropNonPrimitiveAttributeValue", new object[] { str, defaultValue.ToString() }));
                    }
                    else
                    {
                        PrimitiveMapping mapping2 = (PrimitiveMapping)mapping;
                        if (((comments != null) && !mapping2.TypeDesc.HasDefaultSupport) && mapping2.TypeDesc.IsMappedType)
                        {
                            DropDefaultAttribute(accessor, comments, mapping2.TypeDesc.FullName);
                        }
                        else if (obj2 == DBNull.Value)
                        {
                            if (comments != null)
                            {
                                CodeExporter.AddWarningComment(comments, Res.GetString("XmlDropAttributeValue", new object[] { str, mapping2.TypeName, defaultValue.ToString() }));
                            }
                        }
                        else
                        {
                            CodeAttributeArgument[] arguments      = null;
                            CodeExpression          initExpression = null;
                            if (mapping2.IsList)
                            {
                                object[]         objArray     = (object[])obj2;
                                CodeExpression[] initializers = new CodeExpression[objArray.Length];
                                for (int i = 0; i < objArray.Length; i++)
                                {
                                    this.GetDefaultValueArguments(mapping2, objArray[i], out initializers[i]);
                                }
                                initExpression = new CodeArrayCreateExpression(field.Type, initializers);
                            }
                            else
                            {
                                arguments = this.GetDefaultValueArguments(mapping2, obj2, out initExpression);
                            }
                            if (field != null)
                            {
                                if (ctor != null)
                                {
                                    AddInitializationStatement(ctor, field, initExpression);
                                }
                                else
                                {
                                    field.InitExpression = initExpression;
                                }
                            }
                            if (((arguments != null) && mapping2.TypeDesc.HasDefaultSupport) && (accessor.IsOptional && !accessor.IsFixed))
                            {
                                CodeAttributeDeclaration declaration = new CodeAttributeDeclaration(typeof(DefaultValueAttribute).FullName, arguments);
                                metadata.Add(declaration);
                            }
                            else if (comments != null)
                            {
                                DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName);
                            }
                        }
                    }
                }
            }
        }
 private XmlQualifiedName ExportPrimitiveMapping(PrimitiveMapping mapping)
 {
     return(new XmlQualifiedName(mapping.TypeDesc.DataType.Name, "http://www.w3.org/2001/XMLSchema"));
 }
Пример #26
0
 private PrimitiveMapping ImportNonXsdPrimitiveDataType(XmlSchemaSimpleType dataType, string ns, TypeFlags flags)
 {
     PrimitiveMapping mapping = null;
     TypeDesc typeDesc = null;
     if (dataType.Name != null && dataType.Name.Length != 0)
     {
         typeDesc = Scope.GetTypeDesc(dataType.Name, ns, flags);
         if (typeDesc != null)
         {
             mapping = new PrimitiveMapping();
             mapping.TypeDesc = typeDesc;
             mapping.TypeName = typeDesc.DataType.Name;
             mapping.Namespace = mapping.TypeDesc.IsXsdType ? XmlSchema.Namespace : ns;
         }
     }
     return mapping;
 }
Пример #27
0
 private PrimitiveMapping ImportPrimitiveDataType(XmlSchemaSimpleType dataType, TypeFlags flags)
 {
     TypeDesc sourceTypeDesc = GetDataTypeSource(dataType, flags);
     PrimitiveMapping mapping = new PrimitiveMapping();
     mapping.TypeDesc = sourceTypeDesc;
     mapping.TypeName = sourceTypeDesc.DataType.Name;
     mapping.Namespace = mapping.TypeDesc.IsXsdType ? XmlSchema.Namespace : UrtTypes.Namespace;
     return mapping;
 }
Пример #28
0
 XmlQualifiedName ExportPrimitiveMapping(PrimitiveMapping mapping) {
     return new XmlQualifiedName(mapping.TypeDesc.DataType.Name, XmlSchema.Namespace);
 }
 private PrimitiveMapping ImportPrimitiveMapping(PrimitiveModel model, ImportContext context, string dataType, bool repeats)
 {
     PrimitiveMapping mapping = new PrimitiveMapping();
     if (dataType.Length > 0)
     {
         mapping.TypeDesc = this.typeScope.GetTypeDesc(dataType, "http://www.w3.org/2001/XMLSchema");
         if (mapping.TypeDesc == null)
         {
             mapping.TypeDesc = this.typeScope.GetTypeDesc(dataType, "http://microsoft.com/wsdl/types/");
             if (mapping.TypeDesc == null)
             {
                 throw new InvalidOperationException(Res.GetString("XmlUdeclaredXsdType", new object[] { dataType }));
             }
         }
     }
     else
     {
         mapping.TypeDesc = model.TypeDesc;
     }
     mapping.TypeName = mapping.TypeDesc.DataType.Name;
     mapping.Namespace = mapping.TypeDesc.IsXsdType ? "http://www.w3.org/2001/XMLSchema" : "http://microsoft.com/wsdl/types/";
     mapping.IsList = repeats;
     this.CheckContext(mapping.TypeDesc, context);
     return mapping;
 }
Пример #30
0
 PrimitiveMapping ImportPrimitiveMapping(PrimitiveModel model, ImportContext context, string dataType, bool repeats) {
     PrimitiveMapping mapping = new PrimitiveMapping();
     if (dataType.Length > 0) {
         mapping.TypeDesc = typeScope.GetTypeDesc(new XmlQualifiedName(dataType, XmlSchema.Namespace));
         if (mapping.TypeDesc == null) {
             // try it as a non-Xsd type
             mapping.TypeDesc = typeScope.GetTypeDesc(new XmlQualifiedName(dataType, UrtTypes.Namespace));
             if (mapping.TypeDesc == null) {
                 throw new InvalidOperationException(Res.GetString(Res.XmlUdeclaredXsdType, dataType));
             }
         }
     }
     else {
         mapping.TypeDesc = model.TypeDesc;
     }
     mapping.TypeName = mapping.TypeDesc.DataType.Name;
     mapping.IsList = repeats;
     CheckContext(mapping.TypeDesc, context);
     return mapping;
 }
        private CodeAttributeArgument[] GetDefaultValueArguments(PrimitiveMapping mapping, object value, out CodeExpression initExpression)
        {
            initExpression = null;
            if (value == null)
            {
                return(null);
            }
            CodeExpression left        = null;
            CodeExpression expression2 = null;
            Type           type        = value.GetType();

            CodeAttributeArgument[] argumentArray = null;
            if (mapping is EnumMapping)
            {
                if (((EnumMapping)mapping).IsFlags)
                {
                    string[] strArray = ((string)value).Split(null);
                    for (int i = 0; i < strArray.Length; i++)
                    {
                        if (strArray[i].Length != 0)
                        {
                            CodeExpression right = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(mapping.TypeDesc.FullName), strArray[i]);
                            if (left != null)
                            {
                                left = new CodeBinaryOperatorExpression(left, CodeBinaryOperatorType.BitwiseOr, right);
                            }
                            else
                            {
                                left = right;
                            }
                        }
                    }
                }
                else
                {
                    left = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(mapping.TypeDesc.FullName), (string)value);
                }
                initExpression = left;
                argumentArray  = new CodeAttributeArgument[] { new CodeAttributeArgument(left) };
            }
            else if (((type == typeof(bool)) || (type == typeof(int))) || ((type == typeof(string)) || (type == typeof(double))))
            {
                initExpression = left = new CodePrimitiveExpression(value);
                argumentArray  = new CodeAttributeArgument[] { new CodeAttributeArgument(left) };
            }
            else if (((type == typeof(short)) || (type == typeof(long))) || (((type == typeof(float)) || (type == typeof(byte))) || (type == typeof(decimal))))
            {
                left           = new CodePrimitiveExpression(Convert.ToString(value, NumberFormatInfo.InvariantInfo));
                expression2    = new CodeTypeOfExpression(type.FullName);
                argumentArray  = new CodeAttributeArgument[] { new CodeAttributeArgument(expression2), new CodeAttributeArgument(left) };
                initExpression = new CodeCastExpression(type.FullName, new CodePrimitiveExpression(value));
            }
            else if (((type == typeof(sbyte)) || (type == typeof(ushort))) || ((type == typeof(uint)) || (type == typeof(ulong))))
            {
                value          = CodeExporter.PromoteType(type, value);
                left           = new CodePrimitiveExpression(Convert.ToString(value, NumberFormatInfo.InvariantInfo));
                expression2    = new CodeTypeOfExpression(type.FullName);
                argumentArray  = new CodeAttributeArgument[] { new CodeAttributeArgument(expression2), new CodeAttributeArgument(left) };
                initExpression = new CodeCastExpression(type.FullName, new CodePrimitiveExpression(value));
            }
            else if (type == typeof(DateTime))
            {
                string   str;
                long     ticks;
                DateTime time = (DateTime)value;
                if (mapping.TypeDesc.FormatterName == "Date")
                {
                    str = XmlCustomFormatter.FromDate(time);
                    DateTime time2 = new DateTime(time.Year, time.Month, time.Day);
                    ticks = time2.Ticks;
                }
                else if (mapping.TypeDesc.FormatterName == "Time")
                {
                    str   = XmlCustomFormatter.FromDateTime(time);
                    ticks = time.Ticks;
                }
                else
                {
                    str   = XmlCustomFormatter.FromDateTime(time);
                    ticks = time.Ticks;
                }
                left           = new CodePrimitiveExpression(str);
                expression2    = new CodeTypeOfExpression(type.FullName);
                argumentArray  = new CodeAttributeArgument[] { new CodeAttributeArgument(expression2), new CodeAttributeArgument(left) };
                initExpression = new CodeObjectCreateExpression(new CodeTypeReference(typeof(DateTime)), new CodeExpression[] { new CodePrimitiveExpression(ticks) });
            }
            else if (type == typeof(Guid))
            {
                left           = new CodePrimitiveExpression(Convert.ToString(value, NumberFormatInfo.InvariantInfo));
                expression2    = new CodeTypeOfExpression(type.FullName);
                argumentArray  = new CodeAttributeArgument[] { new CodeAttributeArgument(expression2), new CodeAttributeArgument(left) };
                initExpression = new CodeObjectCreateExpression(new CodeTypeReference(typeof(Guid)), new CodeExpression[] { left });
            }
            if ((mapping.TypeDesc.FullName != type.ToString()) && !(mapping is EnumMapping))
            {
                initExpression = new CodeCastExpression(mapping.TypeDesc.FullName, initExpression);
            }
            return(argumentArray);
        }
Пример #32
0
 AttributeAccessor ImportSpecialAttribute(XmlQualifiedName name, string identifier) {
     PrimitiveMapping mapping = new PrimitiveMapping();
     mapping.TypeDesc = scope.GetTypeDesc(typeof(string));
     mapping.TypeName = mapping.TypeDesc.DataType.Name;
     AttributeAccessor accessor = new AttributeAccessor();
     accessor.Name = name.Name;
     accessor.Namespace = XmlReservedNs.NsXml;
     accessor.CheckSpecial();
     accessor.Mapping = mapping;
     return accessor;
 }
Пример #33
0
        void ExportAttributeAccessor(XmlSchemaComplexType type, AttributeAccessor accessor, bool valueTypeOptional, string ns)
        {
            if (accessor == null)
            {
                return;
            }
            XmlSchemaObjectCollection attributes;

            if (type.ContentModel != null)
            {
                if (type.ContentModel.Content is XmlSchemaComplexContentRestriction)
                {
                    attributes = ((XmlSchemaComplexContentRestriction)type.ContentModel.Content).Attributes;
                }
                else if (type.ContentModel.Content is XmlSchemaComplexContentExtension)
                {
                    attributes = ((XmlSchemaComplexContentExtension)type.ContentModel.Content).Attributes;
                }
                else if (type.ContentModel.Content is XmlSchemaSimpleContentExtension)
                {
                    attributes = ((XmlSchemaSimpleContentExtension)type.ContentModel.Content).Attributes;
                }
                else
                {
                    throw new InvalidOperationException(Res.GetString(Res.XmlInvalidContent, type.ContentModel.Content.GetType().Name));
                }
            }
            else
            {
                attributes = type.Attributes;
            }

            if (accessor.IsSpecialXmlNamespace)
            {
                // add <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
                AddSchemaImport(XmlReservedNs.NsXml, ns);

                // generate <xsd:attribute ref="xml:lang" use="optional" />
                XmlSchemaAttribute attribute = new XmlSchemaAttribute();
                attribute.Use     = XmlSchemaUse.Optional;
                attribute.RefName = new XmlQualifiedName(accessor.Name, XmlReservedNs.NsXml);
                attributes.Add(attribute);
            }
            else if (accessor.Any)
            {
                if (type.ContentModel == null)
                {
                    type.AnyAttribute = new XmlSchemaAnyAttribute();
                }
                else
                {
                    XmlSchemaContent content = type.ContentModel.Content;
                    if (content is XmlSchemaComplexContentExtension)
                    {
                        XmlSchemaComplexContentExtension extension = (XmlSchemaComplexContentExtension)content;
                        extension.AnyAttribute = new XmlSchemaAnyAttribute();
                    }
                    else if (content is XmlSchemaComplexContentRestriction)
                    {
                        XmlSchemaComplexContentRestriction restriction = (XmlSchemaComplexContentRestriction)content;
                        restriction.AnyAttribute = new XmlSchemaAnyAttribute();
                    }
                }
            }
            else
            {
                XmlSchemaAttribute attribute = new XmlSchemaAttribute();
                attribute.Use = XmlSchemaUse.None;
                if (!accessor.HasDefault && !valueTypeOptional && accessor.Mapping.TypeDesc.IsValueType)
                {
                    attribute.Use = XmlSchemaUse.Required;
                }
                attribute.Name = accessor.Name;
                if (accessor.Namespace == null || accessor.Namespace == ns)
                {
                    // determine the form attribute value
                    XmlSchema schema = schemas[ns];
                    if (schema == null)
                    {
                        attribute.Form = accessor.Form == attributeFormDefault ? XmlSchemaForm.None : accessor.Form;
                    }
                    else
                    {
                        attribute.Form = accessor.Form == schema.AttributeFormDefault ? XmlSchemaForm.None : accessor.Form;
                    }
                    attributes.Add(attribute);
                }
                else
                {
                    // we are going to add the attribute to the top-level items. "use" attribute should not be set
                    attribute.Use  = XmlSchemaUse.None;
                    attribute.Form = accessor.Form;
                    AddSchemaItem(attribute, accessor.Namespace, ns);
                    XmlSchemaAttribute refAttribute = new XmlSchemaAttribute();
                    refAttribute.Use     = XmlSchemaUse.None;
                    refAttribute.RefName = new XmlQualifiedName(accessor.Name, accessor.Namespace);
                    attributes.Add(refAttribute);
                }
                if (accessor.Mapping is PrimitiveMapping)
                {
                    PrimitiveMapping pm = (PrimitiveMapping)accessor.Mapping;
                    if (pm.IsList)
                    {
                        // create local simple type for the list-like attributes
                        XmlSchemaSimpleType     dataType = new XmlSchemaSimpleType();
                        XmlSchemaSimpleTypeList list     = new XmlSchemaSimpleTypeList();
                        list.ItemTypeName    = ExportPrimitiveMapping(pm, accessor.Namespace == null ? ns : accessor.Namespace);
                        dataType.Content     = list;
                        attribute.SchemaType = dataType;
                    }
                    else
                    {
                        attribute.SchemaTypeName = ExportPrimitiveMapping(pm, accessor.Namespace == null ? ns : accessor.Namespace);
                    }
                }
                else if (!(accessor.Mapping is SpecialMapping))
                {
                    throw new InvalidOperationException(Res.GetString(Res.XmlInternalError));
                }

                if (accessor.HasDefault && accessor.Mapping.TypeDesc.HasDefaultSupport)
                {
                    attribute.DefaultValue = ExportDefaultValue(accessor.Mapping, accessor.Default);
                }
            }
        }
Пример #34
0
        AttributeAccessor ImportAttribute(XmlSchemaAttribute attribute, string identifier, string ns) {
            if (attribute.Use == XmlSchemaUse.Prohibited) return null;
            if (!attribute.RefName.IsEmpty) {
                if (attribute.RefName.Namespace == XmlReservedNs.NsXml)
                    return ImportSpecialAttribute(attribute.RefName, identifier);
                else
                    return ImportAttribute(FindAttribute(attribute.RefName), identifier, attribute.RefName.Namespace);
            }
            TypeMapping mapping;
            if (attribute.Name.Length == 0) throw new InvalidOperationException(Res.GetString(Res.XmlAttributeHasNoName));
            if (identifier.Length == 0)
                identifier = CodeIdentifier.MakeValid(attribute.Name);
            else
                identifier += CodeIdentifier.MakePascal(attribute.Name);
            if (!attribute.SchemaTypeName.IsEmpty)
                mapping = (TypeMapping)ImportType(attribute.SchemaTypeName, typeof(TypeMapping), null);
            else if (attribute.SchemaType != null)
                mapping = ImportDataType((XmlSchemaSimpleType)attribute.SchemaType, ns, identifier, null);
            else {
                mapping = new PrimitiveMapping();
                mapping.TypeDesc = scope.GetTypeDesc(typeof(string));
                mapping.TypeName = mapping.TypeDesc.DataType.Name;
            }
            AttributeAccessor accessor = new AttributeAccessor();
            accessor.Name = attribute.Name;
            accessor.Namespace = ns;
            if (attribute.Form == XmlSchemaForm.None) {
                XmlSchema schema = schemas[ns];
                if (schema != null) accessor.Form = schema.AttributeFormDefault;
            }
            else {
                accessor.Form = attribute.Form;
            }
            accessor.CheckSpecial();
            accessor.Mapping = mapping;
            accessor.IsList = mapping.IsList;

            if (attribute.DefaultValue != null) {
                accessor.Default = ImportDefaultValue(mapping, attribute.DefaultValue);
            }
            else if (attribute.FixedValue != null) {
                accessor.Default = ImportDefaultValue(mapping, attribute.FixedValue);
            }
            return accessor;
        }
Пример #35
0
        void ExportTypeMembers(XmlSchemaComplexType type, MemberMapping[] members, string name, string ns, bool hasSimpleContent)
        {
            XmlSchemaGroupBase seq         = new XmlSchemaSequence();
            TypeMapping        textMapping = null;

            for (int i = 0; i < members.Length; i++)
            {
                MemberMapping member = members[i];
                if (member.Ignore)
                {
                    continue;
                }
                if (member.Text != null)
                {
                    if (textMapping != null)
                    {
                        throw new InvalidOperationException(Res.GetString(Res.XmlIllegalMultipleText, name));
                    }
                    textMapping = member.Text.Mapping;
                }
                if (member.Elements.Length > 0)
                {
                    bool repeats = member.TypeDesc.IsArrayLike &&
                                   !(member.Elements.Length == 1 && member.Elements[0].Mapping is ArrayMapping);
                    bool valueTypeOptional = member.CheckSpecified || member.CheckShouldPersist;
                    ExportElementAccessors(seq, member.Elements, repeats, valueTypeOptional, ns);
                }
            }

            if (seq.Items.Count > 0)
            {
                if (type.ContentModel != null)
                {
                    if (type.ContentModel.Content is XmlSchemaComplexContentRestriction)
                    {
                        ((XmlSchemaComplexContentRestriction)type.ContentModel.Content).Particle = seq;
                    }
                    else if (type.ContentModel.Content is XmlSchemaComplexContentExtension)
                    {
                        ((XmlSchemaComplexContentExtension)type.ContentModel.Content).Particle = seq;
                    }
                    else
                    {
                        throw new InvalidOperationException(Res.GetString(Res.XmlInvalidContent, type.ContentModel.Content.GetType().Name));
                    }
                }
                else
                {
                    type.Particle = seq;
                }
            }
            if (textMapping != null)
            {
                if (hasSimpleContent)
                {
                    if (textMapping is PrimitiveMapping && seq.Items.Count == 0)
                    {
                        PrimitiveMapping pm = (PrimitiveMapping)textMapping;
                        if (pm.IsList)
                        {
                            type.IsMixed = true;
                        }
                        else
                        {
                            // Create simpleContent
                            XmlSchemaSimpleContent          model = new XmlSchemaSimpleContent();
                            XmlSchemaSimpleContentExtension ex    = new XmlSchemaSimpleContentExtension();
                            model.Content     = ex;
                            type.ContentModel = model;
                            ex.BaseTypeName   = ExportPrimitiveMapping(pm, ns);
                        }
                    }
                }
                else
                {
                    type.IsMixed = true;
                }
            }
            for (int i = 0; i < members.Length; i++)
            {
                ExportAttributeAccessor(type, members[i].Attribute, members[i].CheckSpecified || members[i].CheckShouldPersist, ns);
            }
        }
Пример #36
0
        private void WriteElement(object o, ElementAccessor element, string arrayName, bool writeAccessor, XmlMapping parentMapping = null)
        {
            string name = writeAccessor ? element.Name : element.Mapping.TypeName;
            string ns   = element.Any && element.Name.Length == 0 ? null : (element.Form == XmlSchemaForm.Qualified ? (writeAccessor ? element.Namespace : element.Mapping.Namespace) : "");

            if (element.Mapping is NullableMapping)
            {
                if (o != null)
                {
                    ElementAccessor e = element.Clone();
                    e.Mapping = ((NullableMapping)element.Mapping).BaseMapping;
                    WriteElement(o, e, arrayName, writeAccessor);
                }
                else if (element.IsNullable)
                {
                    WriteNullTagLiteral(element.Name, ns);
                }
            }
            else if (element.Mapping is ArrayMapping)
            {
                var mapping = (ArrayMapping)element.Mapping;
                if (mapping.IsSoap)
                {
                    throw new PlatformNotSupportedException();
                }

                if (element.IsNullable && o == null)
                {
                    WriteNullTagLiteral(element.Name, element.Form == XmlSchemaForm.Qualified ? element.Namespace : "");
                }
                else
                {
                    if (element.IsUnbounded)
                    {
                        TypeDesc arrayTypeDesc = mapping.TypeDesc.CreateArrayTypeDesc();

                        var enumerable = (IEnumerable)o;
                        foreach (var e in enumerable)
                        {
                            element.IsUnbounded = false;
                            WriteElement(e, element, arrayName, writeAccessor);
                            element.IsUnbounded = true;
                        }
                    }
                    else
                    {
                        if (o != null)
                        {
                            WriteStartElement(name, ns, false);
                            WriteArrayItems(mapping.ElementsSortedByDerivation, null, null, mapping.TypeDesc, o);
                            WriteEndElement();
                        }
                    }
                }
            }
            else if (element.Mapping is EnumMapping)
            {
                if (element.Mapping.IsSoap)
                {
                    Writer.WriteStartElement(name, ns);
                    WriteEnumMethod((EnumMapping)element.Mapping, o);
                    WriteEndElement();
                }
                else
                {
                    WritePrimitive(WritePrimitiveMethodRequirement.WriteElementString, name, ns, element.Default, o, element.Mapping, false, true, element.IsNullable);
                }
            }
            else if (element.Mapping is PrimitiveMapping)
            {
                PrimitiveMapping mapping = (PrimitiveMapping)element.Mapping;
                if (mapping.TypeDesc == QnameTypeDesc)
                {
                    WriteQualifiedNameElement(name, ns, element.Default, (XmlQualifiedName)o, element.IsNullable, mapping.IsSoap, mapping);
                }
                else
                {
                    WritePrimitiveMethodRequirement suffixNullable = mapping.IsSoap ? WritePrimitiveMethodRequirement.Encoded : WritePrimitiveMethodRequirement.None;
                    WritePrimitiveMethodRequirement suffixRaw      = mapping.TypeDesc.XmlEncodingNotRequired ? WritePrimitiveMethodRequirement.Raw : WritePrimitiveMethodRequirement.None;
                    WritePrimitive(element.IsNullable
                        ? WritePrimitiveMethodRequirement.WriteNullableStringLiteral | suffixNullable | suffixRaw
                        : WritePrimitiveMethodRequirement.WriteElementString | suffixRaw,
                                   name, ns, element.Default, o, mapping, mapping.IsSoap, true, element.IsNullable);
                }
            }
            else if (element.Mapping is StructMapping)
            {
                var mapping = (StructMapping)element.Mapping;
                if (mapping.IsSoap)
                {
                    EnsureXmlSerializationWriteCallbackForMapping(mapping, name, ns, element.IsNullable, needType: false, parentMapping: parentMapping);
                    WritePotentiallyReferencingElement(name, ns, o, !writeAccessor ? mapping.TypeDesc.Type : null, !writeAccessor, element.IsNullable);
                }
                else
                {
                    WriteStructMethod(mapping, name, ns, o, element.IsNullable, needType: false, parentMapping: parentMapping);
                }
            }
            else if (element.Mapping is SpecialMapping)
            {
                if (element.Mapping is SerializableMapping)
                {
                    WriteSerializable((IXmlSerializable)o, name, ns, element.IsNullable, !element.Any);
                }
                else
                {
                    // XmlNode, XmlElement
                    var node = o as XmlNode;
                    if (node != null)
                    {
                        WriteElementLiteral(node, name, ns, element.IsNullable, element.Any);
                    }
                    else
                    {
                        throw CreateInvalidAnyTypeException(o);
                    }
                }
            }
            else
            {
                throw new InvalidOperationException(SR.XmlInternalError);
            }
        }
Пример #37
0
 XmlSchemaType ExportAnonymousPrimitiveMapping(PrimitiveMapping mapping) {
     if (mapping is EnumMapping) {
         return ExportEnumMapping((EnumMapping)mapping, null);
     }
     else {
         throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Unsuported anonymous mapping type: " + mapping.ToString()));
     }
 }
 private XmlSchemaType ExportAnonymousPrimitiveMapping(PrimitiveMapping mapping)
 {
     if (!(mapping is EnumMapping))
     {
         throw new InvalidOperationException(Res.GetString("XmlInternalErrorDetails", new object[] { "Unsuported anonymous mapping type: " + mapping.ToString() }));
     }
     return this.ExportEnumMapping((EnumMapping) mapping, null);
 }
        private void WriteQualifiedNameElement(string name, string ns, object defaultValue, XmlQualifiedName o, bool nullable, bool isSoap, PrimitiveMapping mapping)
        {
            bool hasDefault = defaultValue != null && !Globals.IsDBNullValue(defaultValue) && mapping.TypeDesc.HasDefaultSupport;
            if (hasDefault && IsDefaultValue(mapping, o, defaultValue, nullable))
                return;

            if (isSoap)
            {
                throw new PlatformNotSupportedException();
            }

            if (nullable)
            {
                WriteNullableQualifiedNameLiteral(name, ns, o);
            }
            else
            {
                WriteElementQualifiedName(name, ns, o);
            }
        }
 private XmlQualifiedName ExportPrimitiveMapping(PrimitiveMapping mapping, string ns)
 {
     if (mapping is EnumMapping)
     {
         return new XmlQualifiedName(this.ExportEnumMapping((EnumMapping) mapping, ns).Name, mapping.Namespace);
     }
     if (mapping.TypeDesc.IsXsdType)
     {
         return new XmlQualifiedName(mapping.TypeDesc.DataType.Name, "http://www.w3.org/2001/XMLSchema");
     }
     return this.ExportNonXsdPrimitiveMapping(mapping, ns);
 }
Пример #41
0
        private TypeMapping ImportEnumeratedDataType(XmlSchemaSimpleType dataType, string typeNs, string identifier, bool isList)
        {
            TypeMapping mapping = (TypeMapping)ImportedMappings[dataType];

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

            XmlSchemaSimpleType sourceDataType = FindDataType(dataType.DerivedFrom);
            TypeDesc            sourceTypeDesc = Scope.GetTypeDesc(sourceDataType);

            if (sourceTypeDesc != null && sourceTypeDesc != Scope.GetTypeDesc(typeof(string)))
            {
                return(ImportPrimitiveDataType(dataType));
            }
            identifier = Accessor.UnescapeName(identifier);
            string      typeName    = GenerateUniqueTypeName(identifier);
            EnumMapping enumMapping = new EnumMapping();

            enumMapping.IsReference = Schemas.IsReference(dataType);
            enumMapping.TypeDesc    = new TypeDesc(typeName, typeName, TypeKind.Enum, null, 0);
            enumMapping.TypeName    = identifier;
            enumMapping.Namespace   = typeNs;
            enumMapping.IsFlags     = isList;
            CodeIdentifiers constants = new CodeIdentifiers();

            if (!(dataType.Content is XmlSchemaSimpleTypeRestriction))
            {
                throw new InvalidOperationException(SR.Format(SR.XmlInvalidEnumContent, dataType.Content.GetType().Name, identifier));
            }

            XmlSchemaSimpleTypeRestriction restriction = (XmlSchemaSimpleTypeRestriction)dataType.Content;

            for (int i = 0; i < restriction.Facets.Count; i++)
            {
                object facet = restriction.Facets[i];
                if (!(facet is XmlSchemaEnumerationFacet))
                {
                    continue;
                }
                XmlSchemaEnumerationFacet enumeration = (XmlSchemaEnumerationFacet)facet;
                ConstantMapping           constant    = new ConstantMapping();
                string constantName = CodeIdentifier.MakeValid(enumeration.Value);
                constant.Name    = constants.AddUnique(constantName, constant);
                constant.XmlName = enumeration.Value;
                constant.Value   = i;
            }
            enumMapping.Constants = (ConstantMapping[])constants.ToArray(typeof(ConstantMapping));
            if (isList && enumMapping.Constants.Length > 63)
            {
                // if we have 64+ flag constants we cannot map the type to long enum, we will use string mapping instead.
                mapping          = new PrimitiveMapping();
                mapping.TypeDesc = Scope.GetTypeDesc(typeof(string));
                mapping.TypeName = mapping.TypeDesc.DataType.Name;
                ImportedMappings.Add(dataType, mapping);
                return(mapping);
            }
            ImportedMappings.Add(dataType, enumMapping);
            Scope.AddTypeMapping(enumMapping);
            return(enumMapping);
        }
 private PrimitiveMapping ImportNonXsdPrimitiveDataType(XmlSchemaSimpleType dataType, string ns, TypeFlags flags)
 {
     PrimitiveMapping mapping = null;
     TypeDesc desc = null;
     if ((dataType.Name != null) && (dataType.Name.Length != 0))
     {
         desc = base.Scope.GetTypeDesc(dataType.Name, ns, flags);
         if (desc != null)
         {
             mapping = new PrimitiveMapping {
                 TypeDesc = desc,
                 TypeName = desc.DataType.Name,
                 Namespace = mapping.TypeDesc.IsXsdType ? "http://www.w3.org/2001/XMLSchema" : ns
             };
         }
     }
     return mapping;
 }
Пример #43
0
        private void WriteQualifiedNameElement(string name, string ns, object defaultValue, XmlQualifiedName o, bool nullable, bool isSoap, PrimitiveMapping mapping)
        {
            bool hasDefault = defaultValue != null && !Globals.IsDBNullValue(defaultValue) && mapping.TypeDesc.HasDefaultSupport;

            if (hasDefault && IsDefaultValue(mapping, o, defaultValue, nullable))
            {
                return;
            }

            if (isSoap)
            {
                throw new PlatformNotSupportedException();
            }

            if (nullable)
            {
                WriteNullableQualifiedNameLiteral(name, ns, o);
            }
            else
            {
                WriteElementQualifiedName(name, ns, o);
            }
        }
        TypeMapping ImportDataType(XmlSchemaSimpleType dataType, string typeNs, string identifier, bool isList) {
            TypeMapping mapping = ImportNonXsdPrimitiveDataType(dataType, typeNs);
            if (mapping != null)
                return mapping;

            if (dataType.Content is XmlSchemaSimpleTypeRestriction) {
                XmlSchemaSimpleTypeRestriction restriction = (XmlSchemaSimpleTypeRestriction)dataType.Content;
                foreach (object o in restriction.Facets) {
                    if (o is XmlSchemaEnumerationFacet) {
                        return ImportEnumeratedDataType(dataType, typeNs, identifier, isList);
                    }
                }
            }
            else if (dataType.Content is XmlSchemaSimpleTypeList || dataType.Content is XmlSchemaSimpleTypeUnion) {
                if (dataType.Content is XmlSchemaSimpleTypeList) {
                    // check if we have enumeration list
                    XmlSchemaSimpleTypeList list = (XmlSchemaSimpleTypeList)dataType.Content;
                    if (list.ItemType != null) {
                        mapping = ImportDataType(list.ItemType, typeNs, identifier, true);
                        if (mapping != null) {
                            return mapping;
                        }
                    }
                }
                mapping = new PrimitiveMapping();
                mapping.TypeDesc = Scope.GetTypeDesc(typeof(string));
                mapping.TypeName = mapping.TypeDesc.DataType.Name;
                return mapping;
            }
            return ImportPrimitiveDataType(dataType);
        }
 private XmlQualifiedName ExportNonXsdPrimitiveMapping(PrimitiveMapping mapping, string ns)
 {
     XmlSchemaSimpleType dataType = (XmlSchemaSimpleType) mapping.TypeDesc.DataType;
     if (!this.SchemaContainsItem(dataType, "http://microsoft.com/wsdl/types/"))
     {
         this.AddSchemaItem(dataType, "http://microsoft.com/wsdl/types/", ns);
     }
     else
     {
         this.AddSchemaImport(mapping.Namespace, ns);
     }
     return new XmlQualifiedName(mapping.TypeDesc.DataType.Name, "http://microsoft.com/wsdl/types/");
 }
 PrimitiveMapping ImportPrimitiveDataType(XmlSchemaSimpleType dataType) {
     TypeDesc sourceTypeDesc = GetDataTypeSource(dataType);
     PrimitiveMapping mapping = new PrimitiveMapping();
     mapping.TypeDesc = sourceTypeDesc;
     mapping.TypeName = sourceTypeDesc.DataType.Name;
     return mapping;
 }
Пример #47
0
        private ArrayMapping ImportArrayMapping(XmlSchemaType type, string ns)
        {
            ArrayMapping arrayMapping;

            if (type.Name == Soap.Array && ns == Soap.Encoding)
            {
                arrayMapping = new ArrayMapping();
                TypeMapping     mapping      = GetRootMapping();
                ElementAccessor itemAccessor = new ElementAccessor();
                itemAccessor.IsSoap     = true;
                itemAccessor.Name       = Soap.UrType;
                itemAccessor.Namespace  = ns;
                itemAccessor.Mapping    = mapping;
                itemAccessor.IsNullable = true;
                itemAccessor.Form       = XmlSchemaForm.None;

                arrayMapping.Elements = new ElementAccessor[] { itemAccessor };
                arrayMapping.TypeDesc = itemAccessor.Mapping.TypeDesc.CreateArrayTypeDesc();
                arrayMapping.TypeName = "ArrayOf" + CodeIdentifier.MakePascal(itemAccessor.Mapping.TypeName);
                return(arrayMapping);
            }
            if (!(type.DerivedFrom.Name == Soap.Array && type.DerivedFrom.Namespace == Soap.Encoding))
            {
                return(null);
            }

            // the type should be a XmlSchemaComplexType
            XmlSchemaContentModel model = ((XmlSchemaComplexType)type).ContentModel;

            // the Content  should be an restriction
            if (!(model.Content is XmlSchemaComplexContentRestriction))
            {
                return(null);
            }

            arrayMapping = new ArrayMapping();

            XmlSchemaComplexContentRestriction restriction = (XmlSchemaComplexContentRestriction)model.Content;

            for (int i = 0; i < restriction.Attributes.Count; i++)
            {
                XmlSchemaAttribute attribute = restriction.Attributes[i] as XmlSchemaAttribute;
                if (attribute != null && attribute.RefName.Name == Soap.ArrayType && attribute.RefName.Namespace == Soap.Encoding)
                {
                    // read the value of the wsdl:arrayType attribute
                    string arrayType = null;

                    if (attribute.UnhandledAttributes != null)
                    {
                        foreach (XmlAttribute a in attribute.UnhandledAttributes)
                        {
                            if (a.LocalName == Wsdl.ArrayType && a.NamespaceURI == Wsdl.Namespace)
                            {
                                arrayType = a.Value;
                                break;
                            }
                        }
                    }
                    if (arrayType != null)
                    {
                        string           dims;
                        XmlQualifiedName typeName = TypeScope.ParseWsdlArrayType(arrayType, out dims, attribute);

                        TypeMapping mapping;
                        TypeDesc    td = Scope.GetTypeDesc(typeName.Name, typeName.Namespace);
                        if (td != null && td.IsPrimitive)
                        {
                            mapping          = new PrimitiveMapping();
                            mapping.TypeDesc = td;
                            mapping.TypeName = td.DataType.Name;
                        }
                        else
                        {
                            mapping = ImportType(typeName, false);
                        }
                        ElementAccessor itemAccessor = new ElementAccessor();
                        itemAccessor.IsSoap     = true;
                        itemAccessor.Name       = typeName.Name;
                        itemAccessor.Namespace  = ns;
                        itemAccessor.Mapping    = mapping;
                        itemAccessor.IsNullable = true;
                        itemAccessor.Form       = XmlSchemaForm.None;

                        arrayMapping.Elements = new ElementAccessor[] { itemAccessor };
                        arrayMapping.TypeDesc = itemAccessor.Mapping.TypeDesc.CreateArrayTypeDesc();
                        arrayMapping.TypeName = "ArrayOf" + CodeIdentifier.MakePascal(itemAccessor.Mapping.TypeName);

                        return(arrayMapping);
                    }
                }
            }

            XmlSchemaParticle particle = restriction.Particle;

            if (particle is XmlSchemaAll || particle is XmlSchemaSequence)
            {
                XmlSchemaGroupBase group = (XmlSchemaGroupBase)particle;
                if (group.Items.Count != 1 || !(group.Items[0] is XmlSchemaElement))
                {
                    return(null);
                }
                XmlSchemaElement itemElement = (XmlSchemaElement)group.Items[0];
                if (!itemElement.IsMultipleOccurrence)
                {
                    return(null);
                }
                ElementAccessor itemAccessor = ImportElement(itemElement, ns);
                arrayMapping.Elements = new ElementAccessor[] { itemAccessor };
                arrayMapping.TypeDesc = ((TypeMapping)itemAccessor.Mapping).TypeDesc.CreateArrayTypeDesc();
            }
            else
            {
                return(null);
            }
            return(arrayMapping);
        }
 private CodeAttributeArgument[] GetDefaultValueArguments(PrimitiveMapping mapping, object value, out CodeExpression initExpression)
 {
     initExpression = null;
     if (value == null)
     {
         return null;
     }
     CodeExpression left = null;
     CodeExpression expression2 = null;
     Type type = value.GetType();
     CodeAttributeArgument[] argumentArray = null;
     if (mapping is EnumMapping)
     {
         if (((EnumMapping) mapping).IsFlags)
         {
             string[] strArray = ((string) value).Split(null);
             for (int i = 0; i < strArray.Length; i++)
             {
                 if (strArray[i].Length != 0)
                 {
                     CodeExpression right = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(mapping.TypeDesc.FullName), strArray[i]);
                     if (left != null)
                     {
                         left = new CodeBinaryOperatorExpression(left, CodeBinaryOperatorType.BitwiseOr, right);
                     }
                     else
                     {
                         left = right;
                     }
                 }
             }
         }
         else
         {
             left = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(mapping.TypeDesc.FullName), (string) value);
         }
         initExpression = left;
         argumentArray = new CodeAttributeArgument[] { new CodeAttributeArgument(left) };
     }
     else if (((type == typeof(bool)) || (type == typeof(int))) || ((type == typeof(string)) || (type == typeof(double))))
     {
         initExpression = left = new CodePrimitiveExpression(value);
         argumentArray = new CodeAttributeArgument[] { new CodeAttributeArgument(left) };
     }
     else if (((type == typeof(short)) || (type == typeof(long))) || (((type == typeof(float)) || (type == typeof(byte))) || (type == typeof(decimal))))
     {
         left = new CodePrimitiveExpression(Convert.ToString(value, NumberFormatInfo.InvariantInfo));
         expression2 = new CodeTypeOfExpression(type.FullName);
         argumentArray = new CodeAttributeArgument[] { new CodeAttributeArgument(expression2), new CodeAttributeArgument(left) };
         initExpression = new CodeCastExpression(type.FullName, new CodePrimitiveExpression(value));
     }
     else if (((type == typeof(sbyte)) || (type == typeof(ushort))) || ((type == typeof(uint)) || (type == typeof(ulong))))
     {
         value = CodeExporter.PromoteType(type, value);
         left = new CodePrimitiveExpression(Convert.ToString(value, NumberFormatInfo.InvariantInfo));
         expression2 = new CodeTypeOfExpression(type.FullName);
         argumentArray = new CodeAttributeArgument[] { new CodeAttributeArgument(expression2), new CodeAttributeArgument(left) };
         initExpression = new CodeCastExpression(type.FullName, new CodePrimitiveExpression(value));
     }
     else if (type == typeof(DateTime))
     {
         string str;
         long ticks;
         DateTime time = (DateTime) value;
         if (mapping.TypeDesc.FormatterName == "Date")
         {
             str = XmlCustomFormatter.FromDate(time);
             DateTime time2 = new DateTime(time.Year, time.Month, time.Day);
             ticks = time2.Ticks;
         }
         else if (mapping.TypeDesc.FormatterName == "Time")
         {
             str = XmlCustomFormatter.FromDateTime(time);
             ticks = time.Ticks;
         }
         else
         {
             str = XmlCustomFormatter.FromDateTime(time);
             ticks = time.Ticks;
         }
         left = new CodePrimitiveExpression(str);
         expression2 = new CodeTypeOfExpression(type.FullName);
         argumentArray = new CodeAttributeArgument[] { new CodeAttributeArgument(expression2), new CodeAttributeArgument(left) };
         initExpression = new CodeObjectCreateExpression(new CodeTypeReference(typeof(DateTime)), new CodeExpression[] { new CodePrimitiveExpression(ticks) });
     }
     else if (type == typeof(Guid))
     {
         left = new CodePrimitiveExpression(Convert.ToString(value, NumberFormatInfo.InvariantInfo));
         expression2 = new CodeTypeOfExpression(type.FullName);
         argumentArray = new CodeAttributeArgument[] { new CodeAttributeArgument(expression2), new CodeAttributeArgument(left) };
         initExpression = new CodeObjectCreateExpression(new CodeTypeReference(typeof(Guid)), new CodeExpression[] { left });
     }
     if ((mapping.TypeDesc.FullName != type.ToString()) && !(mapping is EnumMapping))
     {
         initExpression = new CodeCastExpression(mapping.TypeDesc.FullName, initExpression);
     }
     return argumentArray;
 }
Пример #49
0
        TypeMapping ImportDataType(XmlSchemaSimpleType dataType, string typeNs, string identifier, Type baseType) {
            if (baseType != null)
                return ImportStructDataType(dataType, typeNs, identifier, baseType);

            TypeMapping mapping = ImportNonXsdPrimitiveDataType(dataType);
            if (mapping != null)
                return mapping;

            if (dataType.Content is XmlSchemaSimpleTypeRestriction) {
                XmlSchemaSimpleTypeRestriction restriction = (XmlSchemaSimpleTypeRestriction)dataType.Content;
                foreach (object o in restriction.Facets) {
                    if (o is XmlSchemaEnumerationFacet) {
                        return ImportEnumeratedDataType(dataType, typeNs, identifier);
                    }
                }
            }
            else if (dataType.Content is XmlSchemaSimpleTypeList || dataType.Content is XmlSchemaSimpleTypeUnion) {
                if (dataType.Content is XmlSchemaSimpleTypeList) {
                    // check if we have enumeration list
                    XmlSchemaSimpleTypeList list = (XmlSchemaSimpleTypeList)dataType.Content;
                    if (list.ItemType != null) {
                        mapping = ImportDataType(list.ItemType, typeNs, identifier, null);
                        if (mapping != null && mapping is EnumMapping) {
                            ((EnumMapping)mapping).IsFlags = true;
                            return mapping;
                        }
                    }
                    else if (list.ItemTypeName != null && !list.ItemTypeName.IsEmpty) {
                        mapping = ImportType(list.ItemTypeName, typeof(TypeMapping), null);
                        if (mapping != null && mapping is PrimitiveMapping) {
                            ((PrimitiveMapping)mapping).IsList = true;
                            return mapping;
                        }
                    }
                }
                mapping = new PrimitiveMapping();
                mapping.TypeDesc = scope.GetTypeDesc(typeof(string));
                mapping.TypeName = mapping.TypeDesc.DataType.Name;
                return mapping;
            }
            return ImportPrimitiveDataType(dataType);
        }
 private XmlQualifiedName ExportPrimitiveMapping(PrimitiveMapping mapping)
 {
     return new XmlQualifiedName(mapping.TypeDesc.DataType.Name, "http://www.w3.org/2001/XMLSchema");
 }
        ArrayMapping ImportArrayMapping(XmlSchemaType type, string ns) {
            ArrayMapping arrayMapping;
            if (type.Name == Soap.Array && ns == Soap.Encoding) {
                arrayMapping = new ArrayMapping();
                TypeMapping mapping = GetRootMapping();
                ElementAccessor itemAccessor = new ElementAccessor();
                itemAccessor.IsSoap = true;
                itemAccessor.Name = Soap.UrType;
                itemAccessor.Namespace = ns;
                itemAccessor.Mapping = mapping;
                itemAccessor.IsNullable = true;
                itemAccessor.Form = XmlSchemaForm.None;

                arrayMapping.Elements = new ElementAccessor[] { itemAccessor };
                arrayMapping.TypeDesc = itemAccessor.Mapping.TypeDesc.CreateArrayTypeDesc();
                arrayMapping.TypeName = "ArrayOf" + CodeIdentifier.MakePascal(itemAccessor.Mapping.TypeName);
                return arrayMapping;
            }
            if (!(type.DerivedFrom.Name == Soap.Array && type.DerivedFrom.Namespace == Soap.Encoding)) return null;

            // the type should be a XmlSchemaComplexType
            XmlSchemaContentModel model = ((XmlSchemaComplexType)type).ContentModel;

            // the Content  should be an restriction
            if (!(model.Content is XmlSchemaComplexContentRestriction)) return null;

            arrayMapping = new ArrayMapping();

            XmlSchemaComplexContentRestriction restriction = (XmlSchemaComplexContentRestriction)model.Content;

            for (int i = 0; i < restriction.Attributes.Count; i++) {
                XmlSchemaAttribute attribute = restriction.Attributes[i] as XmlSchemaAttribute;
                if (attribute != null && attribute.RefName.Name == Soap.ArrayType && attribute.RefName.Namespace == Soap.Encoding) {
                    // read the value of the wsdl:arrayType attribute
                    string arrayType = null;

                    if (attribute.UnhandledAttributes != null) {
                        foreach (XmlAttribute a in attribute.UnhandledAttributes) {
                            if (a.LocalName == Wsdl.ArrayType && a.NamespaceURI == Wsdl.Namespace) {
                                arrayType = a.Value;
                                break;
                            }
                        }
                    }
                    if (arrayType != null) {
                        string dims;
                        XmlQualifiedName typeName = TypeScope.ParseWsdlArrayType(arrayType, out dims, attribute);

                        TypeMapping mapping;
                        TypeDesc td = Scope.GetTypeDesc(typeName.Name, typeName.Namespace);
                        if (td != null && td.IsPrimitive) {
                            mapping = new PrimitiveMapping();
                            mapping.TypeDesc = td;
                            mapping.TypeName = td.DataType.Name;
                        }
                        else {
                            mapping = ImportType(typeName, false);
                        }
                        ElementAccessor itemAccessor = new ElementAccessor();
                        itemAccessor.IsSoap = true;
                        itemAccessor.Name = typeName.Name;
                        itemAccessor.Namespace = ns;
                        itemAccessor.Mapping = mapping;
                        itemAccessor.IsNullable = true;
                        itemAccessor.Form = XmlSchemaForm.None;

                        arrayMapping.Elements = new ElementAccessor[] { itemAccessor };
                        arrayMapping.TypeDesc = itemAccessor.Mapping.TypeDesc.CreateArrayTypeDesc();
                        arrayMapping.TypeName = "ArrayOf" + CodeIdentifier.MakePascal(itemAccessor.Mapping.TypeName);

                        return arrayMapping;
                    }
                }
            }

            XmlSchemaParticle particle = restriction.Particle;
            if (particle is XmlSchemaAll || particle is XmlSchemaSequence) {
                XmlSchemaGroupBase group = (XmlSchemaGroupBase)particle;
                if (group.Items.Count != 1 || !(group.Items[0] is XmlSchemaElement))
                    return null;
                XmlSchemaElement itemElement = (XmlSchemaElement)group.Items[0];
                if (!itemElement.IsMultipleOccurrence) return null;
                ElementAccessor itemAccessor = ImportElement(itemElement, ns);
                arrayMapping.Elements = new ElementAccessor[] { itemAccessor };
                arrayMapping.TypeDesc = ((TypeMapping)itemAccessor.Mapping).TypeDesc.CreateArrayTypeDesc();
            }
            else {
                return null;
            }
            return arrayMapping;
        }
Пример #52
0
 PrimitiveMapping ImportNonXsdPrimitiveDataType(XmlSchemaSimpleType dataType) {
     PrimitiveMapping mapping = null;
     TypeDesc typeDesc = null;
     if (dataType.Name != null && dataType.Name.Length != 0) {
         typeDesc = scope.GetTypeDesc(new XmlQualifiedName(dataType.Name, UrtTypes.Namespace));
         if (typeDesc != null) {
             mapping = new PrimitiveMapping();
             mapping.TypeDesc = typeDesc;
             mapping.TypeName = typeDesc.DataType.Name;
         }
     }
     return mapping;
 }
        TypeMapping ImportEnumeratedDataType(XmlSchemaSimpleType dataType, string typeNs, string identifier, bool isList) {
            TypeMapping mapping = (TypeMapping)ImportedMappings[dataType];
            if (mapping != null)
                return mapping;

            XmlSchemaSimpleType sourceDataType = FindDataType(dataType.DerivedFrom);
            TypeDesc sourceTypeDesc = Scope.GetTypeDesc(sourceDataType);
            if (sourceTypeDesc != null && sourceTypeDesc != Scope.GetTypeDesc(typeof(string)))
                return ImportPrimitiveDataType(dataType);
            identifier = Accessor.UnescapeName(identifier);
            string typeName = GenerateUniqueTypeName(identifier);
            EnumMapping enumMapping = new EnumMapping();
            enumMapping.IsReference = Schemas.IsReference(dataType);
            enumMapping.TypeDesc = new TypeDesc(typeName, typeName, TypeKind.Enum, null, 0);
            enumMapping.TypeName = identifier;
            enumMapping.Namespace = typeNs;
            enumMapping.IsFlags = isList;
            CodeIdentifiers constants = new CodeIdentifiers();

            if (!(dataType.Content is XmlSchemaSimpleTypeRestriction))
                throw new InvalidOperationException(Res.GetString(Res.XmlInvalidEnumContent, dataType.Content.GetType().Name, identifier));

            XmlSchemaSimpleTypeRestriction restriction = (XmlSchemaSimpleTypeRestriction)dataType.Content;

            for (int i = 0; i < restriction.Facets.Count; i++) {
                object facet = restriction.Facets[i];
                if (!(facet is XmlSchemaEnumerationFacet)) continue;
                XmlSchemaEnumerationFacet enumeration = (XmlSchemaEnumerationFacet)facet;
                ConstantMapping constant = new ConstantMapping();
                string constantName = CodeIdentifier.MakeValid(enumeration.Value);
                constant.Name = constants.AddUnique(constantName, constant);
                constant.XmlName = enumeration.Value;
                constant.Value = i;
            }
            enumMapping.Constants = (ConstantMapping[])constants.ToArray(typeof(ConstantMapping));
            if (isList && enumMapping.Constants.Length > 63) {
                // if we have 64+ flag constants we cannot map the type to long enum, we will use string mapping instead.
                mapping = new PrimitiveMapping();
                mapping.TypeDesc = Scope.GetTypeDesc(typeof(string));
                mapping.TypeName = mapping.TypeDesc.DataType.Name;
                ImportedMappings.Add(dataType, mapping);
                return mapping;
            }
            ImportedMappings.Add(dataType, enumMapping);
            Scope.AddTypeMapping(enumMapping);
            return enumMapping;
        }
Пример #54
0
        void ImportTextMember(CodeIdentifiers members, XmlQualifiedName simpleContentType) {
            TypeMapping mapping;
            bool isMixed = false;

            if (simpleContentType != null) {
                mapping = ImportType(simpleContentType, typeof(TypeMapping), null);
                if (!(mapping is PrimitiveMapping || mapping.TypeDesc.CanBeTextValue)) {
                    return;
                }
            }
            else {
                // this is a case of the mixed content type, just generate string typeDesc
                isMixed = true;
                mapping = new PrimitiveMapping();
                mapping.TypeDesc = scope.GetTypeDesc(typeof(string));
                mapping.TypeName = mapping.TypeDesc.DataType.Name;
            }

            TextAccessor accessor = new TextAccessor();
            accessor.Mapping = mapping;

            MemberMapping member = new MemberMapping();
            member.Elements = new ElementAccessor[0];
            member.Text = accessor;
            if (isMixed) {
                // just generate code for the standard mixed case (string[] text)
                member.TypeDesc = accessor.Mapping.TypeDesc.CreateArrayTypeDesc();
                member.Name = members.MakeRightCase("Text");
            }
            else {
                // import mapping for the simpleContent
                PrimitiveMapping pm = (PrimitiveMapping)accessor.Mapping;
                if (pm.IsList) {
                    member.TypeDesc = accessor.Mapping.TypeDesc.CreateArrayTypeDesc();
                    member.Name = members.MakeRightCase("Text");
                }
                else {
                    member.TypeDesc = accessor.Mapping.TypeDesc;
                    member.Name = members.MakeRightCase("Value");
                }
            }
            member.Name = members.AddUnique(member.Name, member);
        }
 PrimitiveMapping ImportNonXsdPrimitiveDataType(XmlSchemaSimpleType dataType, string ns) {
     PrimitiveMapping mapping = null;
     TypeDesc typeDesc = null;
     if (dataType.Name != null && dataType.Name.Length != 0) {
         typeDesc = Scope.GetTypeDesc(dataType.Name, ns);
         if (typeDesc != null) {
             mapping = new PrimitiveMapping();
             mapping.TypeDesc = typeDesc;
             mapping.TypeName = typeDesc.DataType.Name;
         }
     }
     return mapping;
 }
        CodeAttributeArgument[] GetDefaultValueArguments(PrimitiveMapping mapping, object value, out CodeExpression initExpression) {
            initExpression = null;
            if (value == null) return null;

            CodeExpression valueExpression = null;
            CodeExpression typeofValue = null;
            Type type = value.GetType();
            CodeAttributeArgument[] arguments = null;

            if (mapping is EnumMapping) {
                #if DEBUG
                    // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
                    if (value.GetType() != typeof(string)) throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Invalid enumeration type " + value.GetType().Name));
                #endif

                if (((EnumMapping)mapping).IsFlags) {
                    string[] values = ((string)value).Split(null);
                    for (int i = 0; i < values.Length; i++) {
                        if (values[i].Length == 0) continue;
                        CodeExpression enumRef = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(mapping.TypeDesc.FullName), values[i]);
                        if (valueExpression != null)
                            valueExpression = new CodeBinaryOperatorExpression(valueExpression, CodeBinaryOperatorType.BitwiseOr, enumRef);
                        else
                            valueExpression = enumRef;
                    }
                }
                else {
                    valueExpression = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(mapping.TypeDesc.FullName), (string)value);
                }
                initExpression = valueExpression;
                arguments  = new CodeAttributeArgument[] {new CodeAttributeArgument(valueExpression)};
            }
            else if (type == typeof(bool) ||
                type == typeof(Int32)     ||
                type == typeof(string)    ||
                type == typeof(double)) {

                initExpression = valueExpression = new CodePrimitiveExpression(value);
                arguments = new CodeAttributeArgument[] {new CodeAttributeArgument(valueExpression)};
            }
            else if (type == typeof(Int16) ||
                type == typeof(Int64)      ||
                type == typeof(float)      ||
                type == typeof(byte)       ||
                type == typeof(decimal)) {
                valueExpression = new CodePrimitiveExpression(Convert.ToString(value, NumberFormatInfo.InvariantInfo));
                typeofValue = new CodeTypeOfExpression(type.FullName);
                arguments  = new CodeAttributeArgument[] {new CodeAttributeArgument(typeofValue), new CodeAttributeArgument(valueExpression)};
                initExpression = new CodeCastExpression(type.FullName, new CodePrimitiveExpression(value));
            }
            else if (type == typeof(sbyte) ||
                type == typeof(UInt16)     ||
                type == typeof(UInt32)     ||
                type == typeof(UInt64)) {
                // need to promote the non-CLS complient types

                value = PromoteType(type, value);

                valueExpression = new CodePrimitiveExpression(Convert.ToString(value, NumberFormatInfo.InvariantInfo));
                typeofValue = new CodeTypeOfExpression(type.FullName);
                arguments  = new CodeAttributeArgument[] {new CodeAttributeArgument(typeofValue), new CodeAttributeArgument(valueExpression)};
                initExpression = new CodeCastExpression(type.FullName, new CodePrimitiveExpression(value));
            }
            else if (type == typeof(DateTime)) {
                DateTime dt = (DateTime)value;
                string dtString;
                long ticks;
                if (mapping.TypeDesc.FormatterName == "Date") {
                    dtString = XmlCustomFormatter.FromDate(dt);
                    ticks = (new DateTime(dt.Year, dt.Month, dt.Day)).Ticks;
                }
                else if (mapping.TypeDesc.FormatterName == "Time") {
                    dtString = XmlCustomFormatter.FromDateTime(dt);
                    ticks = dt.Ticks;
                }
                else {
                    dtString = XmlCustomFormatter.FromDateTime(dt);
                    ticks = dt.Ticks;
                }
                valueExpression = new CodePrimitiveExpression(dtString);
                typeofValue = new CodeTypeOfExpression(type.FullName);
                arguments  = new CodeAttributeArgument[] {new CodeAttributeArgument(typeofValue), new CodeAttributeArgument(valueExpression)};
                initExpression = new CodeObjectCreateExpression(new CodeTypeReference(typeof(DateTime)), new CodeExpression[] {new CodePrimitiveExpression(ticks)});
            }
            else if (type == typeof(Guid)) {
                valueExpression = new CodePrimitiveExpression(Convert.ToString(value, NumberFormatInfo.InvariantInfo));
                typeofValue = new CodeTypeOfExpression(type.FullName);
                arguments  = new CodeAttributeArgument[] {new CodeAttributeArgument(typeofValue), new CodeAttributeArgument(valueExpression)};
                initExpression = new CodeObjectCreateExpression(new CodeTypeReference(typeof(Guid)), new CodeExpression[] {valueExpression});

            }
            if (mapping.TypeDesc.FullName != type.ToString() && !(mapping is EnumMapping)) {
                // generate cast
                initExpression = new CodeCastExpression(mapping.TypeDesc.FullName, initExpression);
            }
            return arguments;
        }
 PrimitiveMapping ImportPrimitiveMapping(PrimitiveModel model, string dataType) {
     PrimitiveMapping mapping = new PrimitiveMapping();
     mapping.IsSoap = true;
     if (dataType.Length > 0) {
         mapping.TypeDesc = typeScope.GetTypeDesc(dataType, XmlSchema.Namespace);
         if (mapping.TypeDesc == null) {
             // try it as a non-Xsd type
             mapping.TypeDesc = typeScope.GetTypeDesc(dataType, UrtTypes.Namespace);
             if (mapping.TypeDesc == null) {
                 throw new InvalidOperationException(Res.GetString(Res.XmlUdeclaredXsdType, dataType));
             }
         }
     }
     else {
         mapping.TypeDesc = model.TypeDesc;
     }
     mapping.TypeName = mapping.TypeDesc.DataType.Name;
     mapping.Namespace = mapping.TypeDesc.IsXsdType ? XmlSchema.Namespace : UrtTypes.Namespace;
     return mapping;
 }
 private PrimitiveMapping ImportNonXsdPrimitiveDataType(XmlSchemaSimpleType dataType, string ns)
 {
     PrimitiveMapping mapping = null;
     TypeDesc typeDesc = null;
     if ((dataType.Name != null) && (dataType.Name.Length != 0))
     {
         typeDesc = base.Scope.GetTypeDesc(dataType.Name, ns);
         if (typeDesc != null)
         {
             mapping = new PrimitiveMapping {
                 TypeDesc = typeDesc,
                 TypeName = typeDesc.DataType.Name
             };
         }
     }
     return mapping;
 }