New test.
[mono.git] / mcs / class / System.XML / System.Xml.Serialization / SoapCodeExporter.cs
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 //
10
11 //
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.
30 //
31
32 using System.CodeDom;
33 using System.CodeDom.Compiler;
34 using System.Collections;
35
36 namespace System.Xml.Serialization 
37 {
38         public class SoapCodeExporter
39 #if NET_2_0
40                 : CodeExporter
41 #endif
42         {
43                 #region Fields
44
45 #if !NET_2_0
46                 SoapMapCodeGenerator codeGenerator;
47 #endif
48
49                 #endregion
50
51                 #region Constructors
52
53                 public SoapCodeExporter (CodeNamespace codeNamespace): this (codeNamespace, null)
54                 {
55                 }
56
57                 public SoapCodeExporter (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit)
58                 {
59                         codeGenerator = new SoapMapCodeGenerator (codeNamespace, codeCompileUnit);
60                 }
61
62 #if NET_2_0
63
64                 public SoapCodeExporter (CodeNamespace codeNamespace, 
65                                                                 CodeCompileUnit codeCompileUnit, 
66                                                                 CodeGenerationOptions options)
67                 : this (codeNamespace, codeCompileUnit, null, options, null)
68                 {
69                 }
70                 
71                 public SoapCodeExporter (CodeNamespace codeNamespace, 
72                                                                 CodeCompileUnit codeCompileUnit, 
73                                                                 CodeGenerationOptions options, 
74                                                                 Hashtable mappings)
75                 : this (codeNamespace, codeCompileUnit, null, options, mappings)
76                 {
77                         
78                 }
79                 
80                 [MonoTODO]// FIXME: mappings?
81                 public SoapCodeExporter (CodeNamespace codeNamespace, 
82                                                                 CodeCompileUnit codeCompileUnit, 
83                                                                 CodeDomProvider codeProvider, 
84                                                                 CodeGenerationOptions options, 
85                                                                 Hashtable mappings)
86                 {
87                         codeGenerator = new SoapMapCodeGenerator (codeNamespace, codeCompileUnit, codeProvider, options, mappings);
88                 }
89
90 #endif
91
92 #endregion // Constructors
93
94                 #region Properties
95
96 #if !NET_2_0
97                 public CodeAttributeDeclarationCollection IncludeMetadata {
98                         get { return codeGenerator.IncludeMetadata; }
99                 }
100 #endif
101                 
102                 #endregion // Properties
103
104                 #region Methods
105
106                 public void AddMappingMetadata (CodeAttributeDeclarationCollection metadata, XmlMemberMapping member)
107                 {
108                         AddMappingMetadata (metadata, member, false);
109                 }
110
111                 public void AddMappingMetadata (CodeAttributeDeclarationCollection metadata, XmlMemberMapping member, bool forceUseMemberName)
112                 {
113                         TypeData memType = member.TypeMapMember.TypeData;
114                         
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)));
120                                 
121                         if (att.Arguments.Count > 0) 
122                                 metadata.Add (att);
123                 }
124
125                 public void ExportMembersMapping (XmlMembersMapping xmlMembersMapping)
126                 {
127                         codeGenerator.ExportMembersMapping (xmlMembersMapping);
128                 }
129
130                 public void ExportTypeMapping (XmlTypeMapping xmlTypeMapping)
131                 {
132                         codeGenerator.ExportTypeMapping (xmlTypeMapping, true);
133                 }
134
135
136                 #endregion // Methods
137         }
138
139         class SoapMapCodeGenerator : MapCodeGenerator
140         {
141                 public SoapMapCodeGenerator (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit)
142                 : base (codeNamespace, codeCompileUnit, CodeGenerationOptions.None)
143                 {
144                         includeArrayTypes = true;
145                 }
146
147                 public SoapMapCodeGenerator (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit, CodeDomProvider codeProvider, CodeGenerationOptions options, Hashtable mappings)
148                 : base (codeNamespace, codeCompileUnit, codeProvider, options, mappings)
149                 {
150                 }
151
152                 protected override void GenerateClass (XmlTypeMapping map, CodeTypeDeclaration codeClass, bool isTopLevel)
153                 {
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);
158                 }
159                 
160                 protected override void GenerateClassInclude (CodeAttributeDeclarationCollection attributes, XmlTypeMapping map)
161                 {
162                         CodeAttributeDeclaration iatt = new CodeAttributeDeclaration ("System.Xml.Serialization.SoapInclude");
163                         iatt.Arguments.Add (new CodeAttributeArgument (new CodeTypeOfExpression(map.TypeData.FullTypeName)));
164                         attributes.Add (iatt);
165                 }
166         
167                 protected override void GenerateAttributeMember (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberAttribute attinfo, string defaultNamespace, bool forceUseMemberName)
168                 {
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);
174                 }
175                 
176                 protected override void GenerateElementInfoMember (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member, XmlTypeMapElementInfo einfo, TypeData defaultType, string defaultNamespace, bool addAlwaysAttr, bool forceUseMemberName)
177                 {
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);
183                 }
184
185                 protected override void GenerateEnum (XmlTypeMapping map, CodeTypeDeclaration codeEnum, bool isTopLevel)
186                 {
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);
191                 }               
192                 
193                 protected override void GenerateEnumItem (CodeMemberField codeField, EnumMap.EnumMapMember emem)
194                 {
195                         if (emem.EnumName != emem.XmlName)
196                         {
197                                 CodeAttributeDeclaration xatt = new CodeAttributeDeclaration ("System.Xml.Serialization.SoapEnum");
198                                 xatt.Arguments.Add (GetArg ("Name", emem.XmlName));
199                                 AddCustomAttribute (codeField, xatt, true);
200                         }
201                 }               
202                 
203                 protected override void GenerateSpecifierMember (CodeTypeMember codeField)
204                 {
205                         AddCustomAttribute (codeField, "System.Xml.Serialization.SoapIgnore");
206                 }
207         }
208 }