2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.XML / System.Xml.Serialization / SoapCodeExporter.cs
blob58ee7ca7453b94b9de4f1531f0ad2b0d108ceb29
1 //
2 // System.Xml.Serialization.SoapCodeExporter
3 //
4 // Author:
5 // Tim Coleman (tim@timcoleman.com)
6 // Lluis Sanchez Gual (lluis@ximian.com)
7 //
8 // Copyright (C) Tim Coleman, 2002
9 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.CodeDom;
33 using System.CodeDom.Compiler;
34 using System.Collections;
36 namespace System.Xml.Serialization
38 public class SoapCodeExporter
39 #if NET_2_0
40 : CodeExporter
41 #endif
43 #region Fields
45 #if !NET_2_0
46 SoapMapCodeGenerator codeGenerator;
47 #endif
49 #endregion
51 #region Constructors
53 public SoapCodeExporter (CodeNamespace codeNamespace): this (codeNamespace, null)
57 public SoapCodeExporter (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit)
59 codeGenerator = new SoapMapCodeGenerator (codeNamespace, codeCompileUnit);
62 #if NET_2_0
64 public SoapCodeExporter (CodeNamespace codeNamespace,
65 CodeCompileUnit codeCompileUnit,
66 CodeGenerationOptions options)
67 : this (codeNamespace, codeCompileUnit, null, options, null)
71 public SoapCodeExporter (CodeNamespace codeNamespace,
72 CodeCompileUnit codeCompileUnit,
73 CodeGenerationOptions options,
74 Hashtable mappings)
75 : this (codeNamespace, codeCompileUnit, null, options, mappings)
80 [MonoTODO]// FIXME: mappings?
81 public SoapCodeExporter (CodeNamespace codeNamespace,
82 CodeCompileUnit codeCompileUnit,
83 CodeDomProvider codeProvider,
84 CodeGenerationOptions options,
85 Hashtable mappings)
87 codeGenerator = new SoapMapCodeGenerator (codeNamespace, codeCompileUnit, codeProvider, options, mappings);
90 #endif
92 #endregion // Constructors
94 #region Properties
96 #if !NET_2_0
97 public CodeAttributeDeclarationCollection IncludeMetadata {
98 get { return codeGenerator.IncludeMetadata; }
100 #endif
102 #endregion // Properties
104 #region Methods
106 public void AddMappingMetadata (CodeAttributeDeclarationCollection metadata, XmlMemberMapping member)
108 AddMappingMetadata (metadata, member, false);
111 public void AddMappingMetadata (CodeAttributeDeclarationCollection metadata, XmlMemberMapping member, bool forceUseMemberName)
113 TypeData memType = member.TypeMapMember.TypeData;
115 CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.SoapElement");
116 if (forceUseMemberName || (member.ElementName != member.MemberName))
117 att.Arguments.Add (new CodeAttributeArgument (new CodePrimitiveExpression(member.ElementName)));
118 if (!TypeTranslator.IsDefaultPrimitiveTpeData (memType))
119 att.Arguments.Add (new CodeAttributeArgument ("DataType", new CodePrimitiveExpression(member.TypeName)));
121 if (att.Arguments.Count > 0)
122 metadata.Add (att);
125 public void ExportMembersMapping (XmlMembersMapping xmlMembersMapping)
127 codeGenerator.ExportMembersMapping (xmlMembersMapping);
130 public void ExportTypeMapping (XmlTypeMapping xmlTypeMapping)
132 codeGenerator.ExportTypeMapping (xmlTypeMapping, true);
136 #endregion // Methods
139 class SoapMapCodeGenerator : MapCodeGenerator
141 public SoapMapCodeGenerator (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit)
142 : base (codeNamespace, codeCompileUnit, CodeGenerationOptions.None)
144 includeArrayTypes = true;
147 public SoapMapCodeGenerator (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit, CodeDomProvider codeProvider, CodeGenerationOptions options, Hashtable mappings)
148 : base (codeNamespace, codeCompileUnit, codeProvider, options, mappings)
152 protected override void GenerateClass (XmlTypeMapping map, CodeTypeDeclaration codeClass, bool isTopLevel)
154 CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.SoapType");
155 if (map.XmlType != map.TypeData.TypeName) att.Arguments.Add (GetArg (map.XmlType));
156 if (map.XmlTypeNamespace != "") att.Arguments.Add (GetArg ("Namespace", map.XmlTypeNamespace));
157 AddCustomAttribute (codeClass, att, false);
160 protected override void GenerateClassInclude (CodeAttributeDeclarationCollection attributes, XmlTypeMapping map)
162 CodeAttributeDeclaration iatt = new CodeAttributeDeclaration ("System.Xml.Serialization.SoapInclude");
163 iatt.Arguments.Add (new CodeAttributeArgument (new CodeTypeOfExpression(map.TypeData.FullTypeName)));
164 attributes.Add (iatt);
167 protected override void GenerateAttributeMember (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberAttribute attinfo, string defaultNamespace, bool forceUseMemberName)
169 CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.SoapAttribute");
170 if (attinfo.Name != attinfo.AttributeName) att.Arguments.Add (GetArg (attinfo.AttributeName));
171 if (attinfo.Namespace != defaultNamespace) att.Arguments.Add (GetArg ("Namespace", attinfo.Namespace));
172 if (!TypeTranslator.IsDefaultPrimitiveTpeData(attinfo.TypeData)) att.Arguments.Add (GetArg ("DataType",attinfo.TypeData.XmlType));
173 attributes.Add (att);
176 protected override void GenerateElementInfoMember (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member, XmlTypeMapElementInfo einfo, TypeData defaultType, string defaultNamespace, bool addAlwaysAttr, bool forceUseMemberName)
178 CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.SoapElement");
179 if (forceUseMemberName || einfo.ElementName != member.Name) att.Arguments.Add (GetArg (einfo.ElementName));
180 // if (einfo.IsNullable) att.Arguments.Add (GetArg ("IsNullable", true)); MS seems to ignore this
181 if (!TypeTranslator.IsDefaultPrimitiveTpeData(einfo.TypeData)) att.Arguments.Add (GetArg ("DataType",einfo.TypeData.XmlType));
182 if (addAlwaysAttr || att.Arguments.Count > 0) attributes.Add (att);
185 protected override void GenerateEnum (XmlTypeMapping map, CodeTypeDeclaration codeEnum, bool isTopLevel)
187 CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.SoapType");
188 if (map.XmlType != map.TypeData.TypeName) att.Arguments.Add (GetArg (map.XmlType));
189 if (map.XmlTypeNamespace != "") att.Arguments.Add (GetArg ("Namespace", map.XmlTypeNamespace));
190 AddCustomAttribute (codeEnum, att, false);
193 protected override void GenerateEnumItem (CodeMemberField codeField, EnumMap.EnumMapMember emem)
195 if (emem.EnumName != emem.XmlName)
197 CodeAttributeDeclaration xatt = new CodeAttributeDeclaration ("System.Xml.Serialization.SoapEnum");
198 xatt.Arguments.Add (GetArg ("Name", emem.XmlName));
199 AddCustomAttribute (codeField, xatt, true);
203 protected override void GenerateSpecifierMember (CodeTypeMember codeField)
205 AddCustomAttribute (codeField, "System.Xml.Serialization.SoapIgnore");