Merge pull request #3381 from krytarowski/netbsd-support-20
[mono.git] / mcs / class / System.XML / System.Xml.Serialization / XmlCodeExporter.cs
1 // 
2 // System.Xml.Serialization.XmlCodeExporter 
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.Collections;
34 using System.Xml.Schema;
35 using System.CodeDom.Compiler;
36
37 namespace System.Xml.Serialization 
38 {
39
40         public class XmlCodeExporter 
41                 : CodeExporter
42         {
43                 #region Fields
44
45                 // CodeGenerationOptions options;
46
47                 #endregion
48
49                 #region Constructors
50
51                 public XmlCodeExporter (CodeNamespace codeNamespace): this (codeNamespace, null)
52                 {
53                 }
54
55                 public XmlCodeExporter (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit)
56                 {
57                         codeGenerator = new XmlMapCodeGenerator (codeNamespace, codeCompileUnit, CodeGenerationOptions.GenerateProperties);
58                 }
59
60                 public XmlCodeExporter (CodeNamespace codeNamespace, 
61                                                                 CodeCompileUnit codeCompileUnit, 
62                                                                 CodeGenerationOptions options)
63                 : this (codeNamespace, codeCompileUnit, null, options, null)
64                 {
65                 }
66                 
67                 public XmlCodeExporter (CodeNamespace codeNamespace, 
68                                                                 CodeCompileUnit codeCompileUnit, 
69                                                                 CodeGenerationOptions options, 
70                                                                 Hashtable mappings)
71                 : this (codeNamespace, codeCompileUnit, null, options, mappings)
72                 {
73                         
74                 }
75                 
76                 [MonoTODO]// FIXME: mappings?
77                 public XmlCodeExporter (CodeNamespace codeNamespace, 
78                                                                 CodeCompileUnit codeCompileUnit, 
79                                                                 CodeDomProvider codeProvider, 
80                                                                 CodeGenerationOptions options, 
81                                                                 Hashtable mappings)
82                 {
83                         codeGenerator = new XmlMapCodeGenerator (codeNamespace, codeCompileUnit, codeProvider, options, mappings);
84                 }
85
86                 #endregion // Constructors
87
88                 #region Properties
89
90
91                 #endregion Properties
92
93                 #region Methods
94
95                 public void AddMappingMetadata (CodeAttributeDeclarationCollection metadata, XmlMemberMapping member, string ns)
96                 {
97                         AddMappingMetadata (metadata, member, ns, false);
98                 }
99
100                 public void AddMappingMetadata (CodeAttributeDeclarationCollection metadata, XmlTypeMapping mapping, string ns)
101                 {
102                         if ( (mapping.TypeData.SchemaType == SchemaTypes.Primitive ||
103                               mapping.TypeData.SchemaType == SchemaTypes.Array) 
104                                 && mapping.Namespace != XmlSchema.Namespace)
105                         {
106                                 CodeAttributeDeclaration ratt = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlRoot");
107                                 ratt.Arguments.Add (MapCodeGenerator.GetArg (mapping.ElementName));
108                                 ratt.Arguments.Add (MapCodeGenerator.GetArg ("Namespace", mapping.Namespace));
109                                 ratt.Arguments.Add (MapCodeGenerator.GetArg ("IsNullable", mapping.IsNullable));
110                                 metadata.Add (ratt);
111                         }
112                 }
113
114                 public void AddMappingMetadata (CodeAttributeDeclarationCollection metadata, XmlMemberMapping member, string ns, bool forceUseMemberName)
115                 {
116                         CodeAttributeDeclaration att;
117                         TypeData memType = member.TypeMapMember.TypeData;
118                         
119                         if (member.Any)
120                         {
121                                 XmlTypeMapElementInfoList list = (XmlTypeMapElementInfoList)((XmlTypeMapMemberElement)member.TypeMapMember).ElementInfo;
122                                 foreach (XmlTypeMapElementInfo info in list)
123                                 {
124                                         if (info.IsTextElement)
125                                                 metadata.Add (new CodeAttributeDeclaration ("System.Xml.Serialization.XmlText"));
126                                         else {
127                                                 att = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlAnyElement");
128                                                 if (!info.IsUnnamedAnyElement) {
129                                                         att.Arguments.Add (MapCodeGenerator.GetArg ("Name", info.ElementName));
130                                                         if (info.Namespace != ns) att.Arguments.Add (MapCodeGenerator.GetArg ("Namespace", member.Namespace));
131                                                 }
132                                                 metadata.Add (att);
133                                         }
134                                 }
135                         }
136                         else if (member.TypeMapMember is XmlTypeMapMemberList)
137                         {
138                                 // Array parameter
139                                 XmlTypeMapMemberList list = member.TypeMapMember as XmlTypeMapMemberList;
140                                 ListMap listMap = (ListMap) list.ListTypeMapping.ObjectMap;
141                                 
142                                 codeGenerator.AddArrayAttributes (metadata, list, ns, forceUseMemberName);
143                                 codeGenerator.AddArrayItemAttributes (metadata, listMap, memType.ListItemTypeData, list.Namespace, 0);
144                         }
145                         else if (member.TypeMapMember is XmlTypeMapMemberElement) {
146                                 codeGenerator.AddElementMemberAttributes ((XmlTypeMapMemberElement) member.TypeMapMember, ns, metadata, forceUseMemberName);
147                         }
148                         else if (member.TypeMapMember is XmlTypeMapMemberAttribute) {
149                                 codeGenerator.AddAttributeMemberAttributes ((XmlTypeMapMemberAttribute) member.TypeMapMember, ns, metadata, forceUseMemberName);
150                         }
151                         else
152                                 throw new NotSupportedException ("Schema type not supported");
153                 }
154
155                 public void ExportMembersMapping (XmlMembersMapping xmlMembersMapping)
156                 {
157                         codeGenerator.ExportMembersMapping (xmlMembersMapping);
158                 }
159
160                 public void ExportTypeMapping (XmlTypeMapping xmlTypeMapping)
161                 {
162                         codeGenerator.ExportTypeMapping (xmlTypeMapping, true);
163                 }
164
165                 #endregion // Methods
166         }
167         
168         class XmlMapCodeGenerator : MapCodeGenerator
169         {
170                 public XmlMapCodeGenerator (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit, CodeGenerationOptions options)
171                 : base (codeNamespace, codeCompileUnit, options)
172                 {
173                 }
174
175                 public XmlMapCodeGenerator (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit, CodeDomProvider codeProvider, CodeGenerationOptions options, Hashtable mappings)
176                 : base (codeNamespace, codeCompileUnit, codeProvider, options, mappings)
177                 {
178                 }
179                 
180                 protected override void GenerateClass (XmlTypeMapping map, CodeTypeDeclaration codeClass, bool isTopLevel)
181                 {
182                         CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlTypeAttribute");
183                         if (map.XmlType != map.TypeData.TypeName) att.Arguments.Add (GetArg (map.XmlType));
184                         if (map.XmlTypeNamespace != "") att.Arguments.Add (GetArg ("Namespace", map.XmlTypeNamespace));
185                         if (!map.IncludeInSchema) att.Arguments.Add (GetArg ("IncludeInSchema", false));
186                         AddCustomAttribute (codeClass, att, false);
187
188                         CodeAttributeDeclaration ratt = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlRootAttribute");
189                         if (map.ElementName != map.XmlType) ratt.Arguments.Add (GetArg (map.ElementName));
190                         if (isTopLevel) {
191                                 ratt.Arguments.Add (GetArg ("Namespace", map.Namespace));
192                                 ratt.Arguments.Add (GetArg ("IsNullable", map.IsNullable));
193                         } else {
194                                 if (map.Namespace != "") 
195                                         ratt.Arguments.Add (GetArg ("Namespace", map.Namespace));
196                         }
197                         AddCustomAttribute (codeClass, ratt, isTopLevel);
198                 }
199                 
200                 protected override void GenerateClassInclude (CodeAttributeDeclarationCollection attributes, XmlTypeMapping map)
201                 {
202                         CodeAttributeDeclaration iatt = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlIncludeAttribute");
203                         iatt.Arguments.Add (new CodeAttributeArgument (new CodeTypeOfExpression(map.TypeData.FullTypeName)));
204                         attributes.Add (iatt);
205                 }
206                 
207                 protected override void GenerateAnyAttribute (CodeTypeMember codeField)
208                 {
209                         AddCustomAttribute (codeField, "System.Xml.Serialization.XmlAnyAttribute");
210                 }
211                 
212                 protected override void GenerateAttributeMember (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberAttribute attinfo, string defaultNamespace, bool forceUseMemberName)
213                 {
214                         CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlAttributeAttribute");
215                         if (forceUseMemberName || attinfo.Name != attinfo.AttributeName) att.Arguments.Add (GetArg (attinfo.AttributeName));
216                         if (attinfo.Namespace != defaultNamespace) att.Arguments.Add (GetArg ("Namespace", attinfo.Namespace));
217                         if (attinfo.Form == XmlSchemaForm.Qualified) att.Arguments.Add (GetEnumArg ("Form","System.Xml.Schema.XmlSchemaForm",attinfo.Form.ToString()));
218                         if (!TypeTranslator.IsDefaultPrimitiveTpeData(attinfo.TypeData)) att.Arguments.Add (GetArg ("DataType",attinfo.TypeData.XmlType));
219                         attributes.Add (att);
220                         
221                         if (attinfo.Ignore)
222                                 attributes.Add (new CodeAttributeDeclaration ("System.Xml.Serialization.XmlIgnoreAttribute"));
223                 }
224                 
225                 protected override void GenerateElementInfoMember (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member, XmlTypeMapElementInfo einfo, TypeData defaultType, string defaultNamespace, bool addAlwaysAttr, bool forceUseMemberName)
226                 {
227                         CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlElementAttribute");
228                         if (forceUseMemberName || einfo.ElementName != member.Name) att.Arguments.Add (GetArg (einfo.ElementName));
229                         if (einfo.TypeData.FullTypeName != defaultType.FullTypeName) att.Arguments.Add (GetTypeArg ("Type", einfo.TypeData.FullTypeName));
230                         if (einfo.Namespace != defaultNamespace) att.Arguments.Add (GetArg ("Namespace", einfo.Namespace));
231                         if (einfo.Form == XmlSchemaForm.Unqualified) att.Arguments.Add (GetEnumArg ("Form", "System.Xml.Schema.XmlSchemaForm", einfo.Form.ToString()));
232                         if (einfo.IsNullable) att.Arguments.Add (GetArg ("IsNullable", true));
233                         if (!TypeTranslator.IsDefaultPrimitiveTpeData(einfo.TypeData)) att.Arguments.Add (GetArg ("DataType",einfo.TypeData.XmlType));
234                         if (addAlwaysAttr || att.Arguments.Count > 0) attributes.Add (att);
235                 }
236                 
237                 protected override void GenerateElementMember (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member)
238                 {
239                         if (member.ChoiceMember != null) {
240                                 CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlChoiceIdentifier");
241                                 att.Arguments.Add (GetArg(member.ChoiceMember));
242                                 attributes.Add (att);
243                         }
244
245                         if (member.Ignore)
246                                 attributes.Add (new CodeAttributeDeclaration ("System.Xml.Serialization.XmlIgnoreAttribute"));
247                 }
248                 
249                 protected override void GenerateArrayElement (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member, string defaultNamespace, bool forceUseMemberName)
250                 {
251                         XmlTypeMapElementInfo einfo = (XmlTypeMapElementInfo) member.ElementInfo[0];
252                         CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlArray");
253                         if (forceUseMemberName || (einfo.ElementName != member.Name)) att.Arguments.Add (GetArg ("ElementName", einfo.ElementName));
254                         if (einfo.Namespace != defaultNamespace) att.Arguments.Add (GetArg ("Namespace", einfo.Namespace));
255                         if (einfo.Form == XmlSchemaForm.Unqualified) att.Arguments.Add (MapCodeGenerator.GetEnumArg ("Form", "System.Xml.Schema.XmlSchemaForm", einfo.Form.ToString()));
256                         if (einfo.IsNullable) att.Arguments.Add (GetArg ("IsNullable", true));
257                         if (att.Arguments.Count > 0) attributes.Add (att);
258                 }
259                 
260                 protected override void GenerateArrayItemAttributes (CodeAttributeDeclarationCollection attributes, ListMap listMap, TypeData type, XmlTypeMapElementInfo ainfo, string defaultName, string defaultNamespace, int nestingLevel)
261                 {
262                         bool needsType = (listMap.ItemInfo.Count > 1) ||
263                                                          (ainfo.TypeData.FullTypeName != type.FullTypeName && !listMap.IsMultiArray);
264
265                         CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlArrayItem");
266                         if (ainfo.ElementName != defaultName) att.Arguments.Add (GetArg ("ElementName", ainfo.ElementName));
267                         if (ainfo.Namespace != defaultNamespace && ainfo.Namespace != XmlSchema.Namespace) att.Arguments.Add (GetArg ("Namespace", ainfo.Namespace));
268                         if (needsType) att.Arguments.Add (GetTypeArg ("Type", ainfo.TypeData.FullTypeName));
269                         if (!ainfo.IsNullable) att.Arguments.Add (GetArg ("IsNullable", false));
270                         if (ainfo.Form == XmlSchemaForm.Unqualified) att.Arguments.Add (MapCodeGenerator.GetEnumArg ("Form", "System.Xml.Schema.XmlSchemaForm", ainfo.Form.ToString()));
271                         if (att.Arguments.Count > 0 && nestingLevel > 0) att.Arguments.Add (GetArg ("NestingLevel", nestingLevel));
272                         
273                         if (att.Arguments.Count > 0) attributes.Add (att);
274                 }
275
276                 protected override void GenerateTextElementAttribute (CodeAttributeDeclarationCollection attributes, XmlTypeMapElementInfo einfo, TypeData defaultType)
277                 {
278                         CodeAttributeDeclaration uatt = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlTextAttribute");
279                         if (einfo.TypeData.FullTypeName != defaultType.FullTypeName) uatt.Arguments.Add (GetTypeArg ("Type", einfo.TypeData.FullTypeName));
280                         attributes.Add (uatt);
281                 }
282                 
283                 protected override void GenerateUnnamedAnyElementAttribute (CodeAttributeDeclarationCollection attributes, XmlTypeMapElementInfo einfo, string defaultNamespace)
284                 {
285                         CodeAttributeDeclaration uatt = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlAnyElement");
286                         if (!einfo.IsUnnamedAnyElement) uatt.Arguments.Add (GetArg ("Name", einfo.ElementName));
287                         if (einfo.Namespace != defaultNamespace) uatt.Arguments.Add (GetArg ("Namespace", einfo.Namespace));
288                         attributes.Add (uatt);
289                 }
290
291                 protected override void GenerateEnum (XmlTypeMapping map, CodeTypeDeclaration codeEnum, bool isTopLevel)
292                 {
293                         GenerateClass (map, codeEnum, isTopLevel);
294                 }
295                 
296                 protected override void GenerateEnumItem (CodeMemberField codeField, EnumMap.EnumMapMember emem)
297                 {
298                         if (emem.EnumName != emem.XmlName)
299                         {
300                                 CodeAttributeDeclaration xatt = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlEnumAttribute");
301                                 xatt.Arguments.Add (GetArg (emem.XmlName));
302
303                                 AddCustomAttribute (codeField, xatt, true);
304                         }
305                 }
306                 
307                 protected override void GenerateSpecifierMember (CodeTypeMember codeField)
308                 {
309                         AddCustomAttribute (codeField, "System.Xml.Serialization.XmlIgnore");
310                 }
311
312         }
313 }