File: XmlSerializerOperationGenerator.cs

package info (click to toggle)
mono 4.6.2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (256 lines) | stat: -rw-r--r-- 12,021 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------

using System.Collections.Generic;
using System.ServiceModel.Channels;
using System.ServiceModel;
using System.ServiceModel.Dispatcher;
using System.CodeDom;
using System.Globalization;
using System.Text;
using System.Xml.Serialization;
using System.CodeDom.Compiler;
using System.Runtime.Serialization;

namespace System.ServiceModel.Description
{
    class XmlSerializerOperationGenerator : IOperationBehavior, IOperationContractGenerationExtension
    {
        OperationGenerator operationGenerator;
        Dictionary<MessagePartDescription, PartInfo> partInfoTable;
        Dictionary<OperationDescription, XmlSerializerFormatAttribute> operationAttributes = new Dictionary<OperationDescription, XmlSerializerFormatAttribute>();
        XmlCodeExporter xmlExporter;
        SoapCodeExporter soapExporter;

        XmlSerializerImportOptions options;
        CodeNamespace codeNamespace;

        internal XmlSerializerOperationGenerator(XmlSerializerImportOptions options)
        {
            operationGenerator = new OperationGenerator();
            this.options = options;
            this.codeNamespace = GetTargetCodeNamespace(options);
            partInfoTable = new Dictionary<MessagePartDescription, PartInfo>();
        }

        static CodeNamespace GetTargetCodeNamespace(XmlSerializerImportOptions options)
        {
            CodeNamespace targetCodeNamespace = null;
            string clrNamespace = options.ClrNamespace ?? string.Empty;
            foreach (CodeNamespace ns in options.CodeCompileUnit.Namespaces)
            {
                if (ns.Name == clrNamespace)
                {
                    targetCodeNamespace = ns;
                }
            }
            if (targetCodeNamespace == null)
            {
                targetCodeNamespace = new CodeNamespace(clrNamespace);
                options.CodeCompileUnit.Namespaces.Add(targetCodeNamespace);
            }
            return targetCodeNamespace;
        }

        internal void Add(MessagePartDescription part, XmlMemberMapping memberMapping, XmlMembersMapping membersMapping, bool isEncoded)
        {
            PartInfo partInfo = new PartInfo();
            partInfo.MemberMapping = memberMapping;
            partInfo.MembersMapping = membersMapping;
            partInfo.IsEncoded = isEncoded;
            partInfoTable[part] = partInfo;
        }

        public XmlCodeExporter XmlExporter
        {
            get
            {
                if (this.xmlExporter == null)
                {
                    this.xmlExporter = new XmlCodeExporter(this.codeNamespace, this.options.CodeCompileUnit, this.options.CodeProvider,
                        this.options.WebReferenceOptions.CodeGenerationOptions, null);
                }
                return xmlExporter;
            }
        }

        public SoapCodeExporter SoapExporter
        {
            get
            {
                if (this.soapExporter == null)
                {
                    this.soapExporter = new SoapCodeExporter(this.codeNamespace, this.options.CodeCompileUnit, this.options.CodeProvider,
                        this.options.WebReferenceOptions.CodeGenerationOptions, null);
                }
                return soapExporter;
            }
        }

        OperationGenerator OperationGenerator
        {
            get { return this.operationGenerator; }
        }

        internal Dictionary<OperationDescription, XmlSerializerFormatAttribute> OperationAttributes
        {
            get { return operationAttributes; }
        }


        void IOperationBehavior.Validate(OperationDescription description)
        {
        }

        void IOperationBehavior.AddBindingParameters(OperationDescription description, BindingParameterCollection parameters)
        {
        }

        void IOperationBehavior.ApplyDispatchBehavior(OperationDescription description, DispatchOperation dispatch) { }

        void IOperationBehavior.ApplyClientBehavior(OperationDescription description, ClientOperation proxy) { }

        static object contractMarker = new object();
        // Assumption: gets called exactly once per operation
        void IOperationContractGenerationExtension.GenerateOperation(OperationContractGenerationContext context)
        {
            if (context == null)
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
            if (partInfoTable != null && partInfoTable.Count > 0)
            {
                Dictionary<XmlMembersMapping, XmlMembersMapping> alreadyExported = new Dictionary<XmlMembersMapping, XmlMembersMapping>();
                foreach (MessageDescription message in context.Operation.Messages)
                {
                    foreach (MessageHeaderDescription header in message.Headers)
                        GeneratePartType(alreadyExported, header, header.Namespace);


                    MessageBodyDescription body = message.Body;
                    bool isWrapped = (body.WrapperName != null);
                    if (OperationFormatter.IsValidReturnValue(body.ReturnValue))
                        GeneratePartType(alreadyExported, body.ReturnValue, isWrapped ? body.WrapperNamespace : body.ReturnValue.Namespace);

                    foreach (MessagePartDescription part in body.Parts)
                        GeneratePartType(alreadyExported, part, isWrapped ? body.WrapperNamespace : part.Namespace);
                }
            }
            XmlSerializerOperationBehavior xmlSerializerOperationBehavior = context.Operation.Behaviors.Find<XmlSerializerOperationBehavior>() as XmlSerializerOperationBehavior;
            if (xmlSerializerOperationBehavior == null)
                return;

            XmlSerializerFormatAttribute xmlSerializerFormatAttribute = (xmlSerializerOperationBehavior == null) ? new XmlSerializerFormatAttribute() : xmlSerializerOperationBehavior.XmlSerializerFormatAttribute;
            OperationFormatStyle style = xmlSerializerFormatAttribute.Style;
            operationGenerator.GenerateOperation(context, ref style, xmlSerializerFormatAttribute.IsEncoded, new WrappedBodyTypeGenerator(context), new Dictionary<MessagePartDescription, ICollection<CodeTypeReference>>());
            context.ServiceContractGenerator.AddReferencedAssembly(typeof(System.Xml.Serialization.XmlTypeAttribute).Assembly);
            xmlSerializerFormatAttribute.Style = style;
            context.SyncMethod.CustomAttributes.Add(OperationGenerator.GenerateAttributeDeclaration(context.Contract.ServiceContractGenerator, xmlSerializerFormatAttribute));
            AddKnownTypes(context.SyncMethod.CustomAttributes, xmlSerializerFormatAttribute.IsEncoded ? SoapExporter.IncludeMetadata : XmlExporter.IncludeMetadata);
            DataContractSerializerOperationGenerator.UpdateTargetCompileUnit(context, this.options.CodeCompileUnit);
        }

        private void AddKnownTypes(CodeAttributeDeclarationCollection destination, CodeAttributeDeclarationCollection source)
        {
            foreach (CodeAttributeDeclaration attribute in source)
            {
                CodeAttributeDeclaration knownType = ToKnownType(attribute);
                if (knownType != null)
                {
                    destination.Add(knownType);
                }
            }
        }

        // Convert [XmlInclude] or [SoapInclude] attribute to [KnownType] attribute
        private CodeAttributeDeclaration ToKnownType(CodeAttributeDeclaration include)
        {
            if (include.Name == typeof(SoapIncludeAttribute).FullName || include.Name == typeof(XmlIncludeAttribute).FullName)
            {
                CodeAttributeDeclaration knownType = new CodeAttributeDeclaration(new CodeTypeReference(typeof(ServiceKnownTypeAttribute)));
                foreach (CodeAttributeArgument argument in include.Arguments)
                {
                    knownType.Arguments.Add(argument);
                }
                return knownType;
            }
            return null;
        }

        private void GeneratePartType(Dictionary<XmlMembersMapping, XmlMembersMapping> alreadyExported, MessagePartDescription part, string partNamespace)
        {
            if (!partInfoTable.ContainsKey(part))
                return;
            PartInfo partInfo = partInfoTable[part];
            XmlMembersMapping membersMapping = partInfo.MembersMapping;
            XmlMemberMapping memberMapping = partInfo.MemberMapping;
            if (!alreadyExported.ContainsKey(membersMapping))
            {
                if (partInfo.IsEncoded)
                    SoapExporter.ExportMembersMapping(membersMapping);
                else
                    XmlExporter.ExportMembersMapping(membersMapping);
                alreadyExported.Add(membersMapping, membersMapping);
            }
            CodeAttributeDeclarationCollection additionalAttributes = new CodeAttributeDeclarationCollection();
            if (partInfo.IsEncoded)
                SoapExporter.AddMappingMetadata(additionalAttributes, memberMapping, false/*forceUseMemberName*/);
            else
                XmlExporter.AddMappingMetadata(additionalAttributes, memberMapping, partNamespace, false/*forceUseMemberName*/);
            part.BaseType = GetTypeName(memberMapping);
            operationGenerator.ParameterTypes.Add(part, new CodeTypeReference(part.BaseType));
            operationGenerator.ParameterAttributes.Add(part, additionalAttributes);
        }

        internal string GetTypeName(XmlMemberMapping member)
        {
            string typeName = member.GenerateTypeName(options.CodeProvider);
            // If it is an array type, get the array element type name instead
            string comparableTypeName = typeName.Replace("[]", null);
            if (codeNamespace != null && !string.IsNullOrEmpty(codeNamespace.Name))
            {
                foreach (CodeTypeDeclaration typeDecl in codeNamespace.Types)
                {
                    if (typeDecl.Name == comparableTypeName)
                    {
                        typeName = codeNamespace.Name + "." + typeName;
                    }
                }
            }
            return typeName;
        }

        class PartInfo
        {
            internal XmlMemberMapping MemberMapping;
            internal XmlMembersMapping MembersMapping;
            internal bool IsEncoded;
        }

        internal class WrappedBodyTypeGenerator : IWrappedBodyTypeGenerator
        {
            OperationContractGenerationContext context;
            public WrappedBodyTypeGenerator(OperationContractGenerationContext context)
            {
                this.context = context;
            }
            public void ValidateForParameterMode(OperationDescription operation)
            {
            }

            public void AddMemberAttributes(XmlName messageName, MessagePartDescription part, CodeAttributeDeclarationCollection importedAttributes, CodeAttributeDeclarationCollection typeAttributes, CodeAttributeDeclarationCollection fieldAttributes)
            {
                if (importedAttributes != null)
                    fieldAttributes.AddRange(importedAttributes);
            }
            public void AddTypeAttributes(string messageName, string typeNS, CodeAttributeDeclarationCollection typeAttributes, bool isEncoded)
            {
                // we do not need top-level attibutes for the encoded SOAP
                if (isEncoded)
                    return;
                XmlTypeAttribute xmlType = new XmlTypeAttribute();
                xmlType.Namespace = typeNS;
                typeAttributes.Add(OperationGenerator.GenerateAttributeDeclaration(context.Contract.ServiceContractGenerator, xmlType));
            }
        }
    }
}