MapCodeGenerator.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. //
  2. // System.Xml.Serialization.MapCodeGenerator
  3. //
  4. // Author:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // Copyright (C) Ximian, Inc., 2003
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.CodeDom;
  30. using System.CodeDom.Compiler;
  31. using System.Collections;
  32. #if NET_2_0
  33. using System.ComponentModel;
  34. using System.Diagnostics;
  35. #endif
  36. using System.Globalization;
  37. using System.Xml.Schema;
  38. using Microsoft.CSharp;
  39. namespace System.Xml.Serialization {
  40. internal class MapCodeGenerator {
  41. CodeNamespace codeNamespace;
  42. CodeCompileUnit codeCompileUnit;
  43. CodeAttributeDeclarationCollection includeMetadata;
  44. XmlTypeMapping exportedAnyType = null;
  45. protected bool includeArrayTypes;
  46. CodeDomProvider codeProvider;
  47. CodeGenerationOptions options;
  48. CodeIdentifiers identifiers;
  49. Hashtable exportedMaps = new Hashtable ();
  50. Hashtable includeMaps = new Hashtable ();
  51. public MapCodeGenerator (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit, CodeGenerationOptions options)
  52. {
  53. this.codeCompileUnit = codeCompileUnit;
  54. this.codeNamespace = codeNamespace;
  55. this.options = options;
  56. this.identifiers = new CodeIdentifiers ();
  57. }
  58. public MapCodeGenerator (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit, CodeDomProvider codeProvider, CodeGenerationOptions options, Hashtable mappings)
  59. {
  60. this.codeCompileUnit = codeCompileUnit;
  61. this.codeNamespace = codeNamespace;
  62. this.codeProvider = codeProvider;
  63. this.options = options;
  64. #if NET_2_0
  65. this.identifiers = new CodeIdentifiers ((codeProvider.LanguageOptions & LanguageOptions.CaseInsensitive) == 0);
  66. #else
  67. this.identifiers = new CodeIdentifiers ();
  68. #endif
  69. // this.mappings = mappings;
  70. }
  71. public CodeAttributeDeclarationCollection IncludeMetadata
  72. {
  73. get
  74. {
  75. if (includeMetadata != null) return includeMetadata;
  76. includeMetadata = new CodeAttributeDeclarationCollection ();
  77. foreach (XmlTypeMapping map in includeMaps.Values)
  78. GenerateClassInclude (includeMetadata, map);
  79. return includeMetadata;
  80. }
  81. }
  82. #region Code generation methods
  83. public void ExportMembersMapping (XmlMembersMapping xmlMembersMapping)
  84. {
  85. CodeTypeDeclaration dummyClass = new CodeTypeDeclaration ();
  86. ExportMembersMapCode (dummyClass, (ClassMap)xmlMembersMapping.ObjectMap, xmlMembersMapping.Namespace, null);
  87. }
  88. public void ExportTypeMapping (XmlTypeMapping xmlTypeMapping, bool isTopLevel)
  89. {
  90. ExportMapCode (xmlTypeMapping, isTopLevel);
  91. RemoveInclude (xmlTypeMapping);
  92. }
  93. void ExportMapCode (XmlTypeMapping map, bool isTopLevel)
  94. {
  95. switch (map.TypeData.SchemaType)
  96. {
  97. case SchemaTypes.Enum:
  98. ExportEnumCode (map, isTopLevel);
  99. break;
  100. case SchemaTypes.Array:
  101. ExportArrayCode (map);
  102. break;
  103. case SchemaTypes.Class:
  104. ExportClassCode (map, isTopLevel);
  105. break;
  106. case SchemaTypes.XmlSerializable:
  107. case SchemaTypes.XmlNode:
  108. case SchemaTypes.Primitive:
  109. // Ignore
  110. break;
  111. }
  112. }
  113. void ExportClassCode (XmlTypeMapping map, bool isTopLevel)
  114. {
  115. CodeTypeDeclaration codeClass;
  116. if (IsMapExported (map)) {
  117. codeClass = GetMapDeclaration (map);
  118. if (codeClass != null) {
  119. // Regenerate attributes, since things may have changed
  120. codeClass.CustomAttributes.Clear ();
  121. #if NET_2_0
  122. AddClassAttributes (codeClass);
  123. #endif
  124. GenerateClass (map, codeClass, isTopLevel);
  125. ExportDerivedTypeAttributes (map, codeClass);
  126. }
  127. return;
  128. }
  129. if (map.TypeData.Type == typeof(object))
  130. {
  131. exportedAnyType = map;
  132. SetMapExported (map, null);
  133. foreach (XmlTypeMapping dmap in exportedAnyType.DerivedTypes) {
  134. if (IsMapExported (dmap) || !dmap.IncludeInSchema) continue;
  135. ExportTypeMapping (dmap, false);
  136. AddInclude (dmap);
  137. }
  138. return;
  139. }
  140. codeClass = new CodeTypeDeclaration (map.TypeData.TypeName);
  141. SetMapExported (map, codeClass);
  142. AddCodeType (codeClass, map.Documentation);
  143. codeClass.Attributes = MemberAttributes.Public;
  144. #if NET_2_0
  145. codeClass.IsPartial = CodeProvider.Supports(GeneratorSupport.PartialTypes);
  146. AddClassAttributes (codeClass);
  147. #endif
  148. GenerateClass (map, codeClass, isTopLevel);
  149. ExportDerivedTypeAttributes (map, codeClass);
  150. ExportMembersMapCode (codeClass, (ClassMap)map.ObjectMap, map.XmlTypeNamespace, map.BaseMap);
  151. if (map.BaseMap != null && map.BaseMap.TypeData.SchemaType != SchemaTypes.XmlNode)
  152. {
  153. CodeTypeReference ctr = GetDomType (map.BaseMap.TypeData);
  154. codeClass.BaseTypes.Add (ctr);
  155. if (map.BaseMap.IncludeInSchema) {
  156. ExportMapCode (map.BaseMap, false);
  157. AddInclude (map.BaseMap);
  158. }
  159. }
  160. ExportDerivedTypes (map, codeClass);
  161. }
  162. void ExportDerivedTypeAttributes (XmlTypeMapping map, CodeTypeDeclaration codeClass)
  163. {
  164. foreach (XmlTypeMapping tm in map.DerivedTypes)
  165. {
  166. GenerateClassInclude (codeClass.CustomAttributes, tm);
  167. ExportDerivedTypeAttributes (tm, codeClass);
  168. }
  169. }
  170. void ExportDerivedTypes (XmlTypeMapping map, CodeTypeDeclaration codeClass)
  171. {
  172. foreach (XmlTypeMapping tm in map.DerivedTypes)
  173. {
  174. if (codeClass.CustomAttributes == null)
  175. codeClass.CustomAttributes = new CodeAttributeDeclarationCollection ();
  176. ExportMapCode (tm, false);
  177. ExportDerivedTypes (tm, codeClass);
  178. }
  179. }
  180. void ExportMembersMapCode (CodeTypeDeclaration codeClass, ClassMap map, string defaultNamespace, XmlTypeMapping baseMap)
  181. {
  182. ICollection attributes = map.AttributeMembers;
  183. ICollection members = map.ElementMembers;
  184. // collect names
  185. if (attributes != null)
  186. foreach (XmlTypeMapMemberAttribute attr in attributes)
  187. identifiers.AddUnique (attr.Name, attr);
  188. if (members != null)
  189. foreach (XmlTypeMapMemberElement member in members)
  190. identifiers.AddUnique (member.Name, member);
  191. // Write attributes
  192. if (attributes != null) {
  193. foreach (XmlTypeMapMemberAttribute attr in attributes) {
  194. if (baseMap != null && DefinedInBaseMap (baseMap, attr)) continue;
  195. AddAttributeFieldMember (codeClass, attr, defaultNamespace);
  196. }
  197. }
  198. members = map.ElementMembers;
  199. if (members != null)
  200. {
  201. foreach (XmlTypeMapMemberElement member in members)
  202. {
  203. if (baseMap != null && DefinedInBaseMap (baseMap, member)) continue;
  204. Type memType = member.GetType();
  205. if (memType == typeof(XmlTypeMapMemberList))
  206. {
  207. AddArrayElementFieldMember (codeClass, (XmlTypeMapMemberList) member, defaultNamespace);
  208. }
  209. else if (memType == typeof(XmlTypeMapMemberFlatList))
  210. {
  211. AddElementFieldMember (codeClass, member, defaultNamespace);
  212. }
  213. else if (memType == typeof(XmlTypeMapMemberAnyElement))
  214. {
  215. AddAnyElementFieldMember (codeClass, member, defaultNamespace);
  216. }
  217. else if (memType == typeof(XmlTypeMapMemberElement))
  218. {
  219. AddElementFieldMember (codeClass, member, defaultNamespace);
  220. }
  221. else
  222. {
  223. throw new InvalidOperationException ("Member type " + memType + " not supported");
  224. }
  225. }
  226. }
  227. XmlTypeMapMember anyAttrMember = map.DefaultAnyAttributeMember;
  228. if (anyAttrMember != null)
  229. {
  230. CodeTypeMember codeField = CreateFieldMember (codeClass, anyAttrMember.TypeData, anyAttrMember.Name);
  231. AddComments (codeField, anyAttrMember.Documentation);
  232. codeField.Attributes = MemberAttributes.Public;
  233. GenerateAnyAttribute (codeField);
  234. }
  235. }
  236. CodeTypeMember CreateFieldMember (CodeTypeDeclaration codeClass, Type type, string name)
  237. {
  238. return CreateFieldMember (codeClass, new CodeTypeReference(type), name, System.DBNull.Value, null, null);
  239. }
  240. CodeTypeMember CreateFieldMember (CodeTypeDeclaration codeClass, TypeData type, string name)
  241. {
  242. return CreateFieldMember (codeClass, GetDomType (type), name, System.DBNull.Value, null, null);
  243. }
  244. CodeTypeMember CreateFieldMember (CodeTypeDeclaration codeClass, XmlTypeMapMember member)
  245. {
  246. return CreateFieldMember (codeClass, GetDomType (member.TypeData), member.Name, member.DefaultValue, member.TypeData, member.Documentation);
  247. }
  248. CodeTypeMember CreateFieldMember (CodeTypeDeclaration codeClass, CodeTypeReference type, string name, object defaultValue, TypeData defaultType, string documentation)
  249. {
  250. CodeMemberField codeField = null;
  251. CodeTypeMember codeProp = null;
  252. if ((options & CodeGenerationOptions.GenerateProperties) > 0) {
  253. string field = identifiers.MakeUnique (CodeIdentifier.MakeCamel (name + "Field"));
  254. codeField = new CodeMemberField (type, field);
  255. codeField.Attributes = MemberAttributes.Private;
  256. codeClass.Members.Add (codeField);
  257. CodeMemberProperty prop = new CodeMemberProperty ();
  258. prop.Name = name;
  259. prop.Type = type;
  260. prop.Attributes = MemberAttributes.Public | MemberAttributes.Final;
  261. codeProp = prop;
  262. prop.HasGet = prop.HasSet = true;
  263. CodeExpression ce = new CodeFieldReferenceExpression (new CodeThisReferenceExpression(), field);
  264. prop.SetStatements.Add (new CodeAssignStatement (ce, new CodePropertySetValueReferenceExpression()));
  265. prop.GetStatements.Add (new CodeMethodReturnStatement (ce));
  266. }
  267. else {
  268. codeField = new CodeMemberField (type, name);
  269. codeField.Attributes = MemberAttributes.Public;
  270. codeProp = codeField;
  271. }
  272. if (defaultValue != System.DBNull.Value)
  273. GenerateDefaultAttribute (codeField, codeProp, defaultType, defaultValue);
  274. AddComments (codeProp, documentation);
  275. codeClass.Members.Add (codeProp);
  276. return codeProp;
  277. }
  278. void AddAttributeFieldMember (CodeTypeDeclaration codeClass, XmlTypeMapMemberAttribute attinfo, string defaultNamespace)
  279. {
  280. CodeTypeMember codeField = CreateFieldMember (codeClass, attinfo);
  281. CodeAttributeDeclarationCollection attributes = codeField.CustomAttributes;
  282. if (attributes == null) attributes = new CodeAttributeDeclarationCollection ();
  283. GenerateAttributeMember (attributes, attinfo, defaultNamespace, false);
  284. if (attributes.Count > 0) codeField.CustomAttributes = attributes;
  285. if (attinfo.MappedType != null) {
  286. ExportMapCode (attinfo.MappedType, false);
  287. RemoveInclude (attinfo.MappedType);
  288. }
  289. if (attinfo.TypeData.IsValueType && attinfo.IsOptionalValueType)
  290. {
  291. codeField = CreateFieldMember (codeClass, typeof(bool), identifiers.MakeUnique (attinfo.Name + "Specified"));
  292. codeField.Attributes = MemberAttributes.Public;
  293. GenerateSpecifierMember (codeField);
  294. }
  295. }
  296. public void AddAttributeMemberAttributes (XmlTypeMapMemberAttribute attinfo, string defaultNamespace, CodeAttributeDeclarationCollection attributes, bool forceUseMemberName)
  297. {
  298. GenerateAttributeMember (attributes, attinfo, defaultNamespace, forceUseMemberName);
  299. }
  300. void AddElementFieldMember (CodeTypeDeclaration codeClass, XmlTypeMapMemberElement member, string defaultNamespace)
  301. {
  302. CodeTypeMember codeField = CreateFieldMember (codeClass, member);
  303. CodeAttributeDeclarationCollection attributes = codeField.CustomAttributes;
  304. if (attributes == null) attributes = new CodeAttributeDeclarationCollection ();
  305. AddElementMemberAttributes (member, defaultNamespace, attributes, false);
  306. if (attributes.Count > 0) codeField.CustomAttributes = attributes;
  307. if (member.TypeData.IsValueType && member.IsOptionalValueType)
  308. {
  309. codeField = CreateFieldMember (codeClass, typeof(bool), identifiers.MakeUnique (member.Name + "Specified"));
  310. codeField.Attributes = MemberAttributes.Public;
  311. GenerateSpecifierMember (codeField);
  312. }
  313. }
  314. public void AddElementMemberAttributes (XmlTypeMapMemberElement member, string defaultNamespace, CodeAttributeDeclarationCollection attributes, bool forceUseMemberName)
  315. {
  316. TypeData defaultType = member.TypeData;
  317. bool addAlwaysAttr = false;
  318. if (member is XmlTypeMapMemberFlatList)
  319. {
  320. defaultType = defaultType.ListItemTypeData;
  321. addAlwaysAttr = true;
  322. }
  323. foreach (XmlTypeMapElementInfo einfo in member.ElementInfo)
  324. {
  325. if (ExportExtraElementAttributes (attributes, einfo, defaultNamespace, defaultType))
  326. continue;
  327. GenerateElementInfoMember (attributes, member, einfo, defaultType, defaultNamespace, addAlwaysAttr, forceUseMemberName);
  328. if (einfo.MappedType != null) {
  329. ExportMapCode (einfo.MappedType, false);
  330. RemoveInclude (einfo.MappedType);
  331. }
  332. }
  333. GenerateElementMember (attributes, member);
  334. }
  335. void AddAnyElementFieldMember (CodeTypeDeclaration codeClass, XmlTypeMapMemberElement member, string defaultNamespace)
  336. {
  337. CodeTypeMember codeField = CreateFieldMember (codeClass, member);
  338. CodeAttributeDeclarationCollection attributes = new CodeAttributeDeclarationCollection ();
  339. foreach (XmlTypeMapElementInfo einfo in member.ElementInfo)
  340. ExportExtraElementAttributes (attributes, einfo, defaultNamespace, einfo.TypeData);
  341. if (attributes.Count > 0) codeField.CustomAttributes = attributes;
  342. }
  343. bool DefinedInBaseMap (XmlTypeMapping map, XmlTypeMapMember member)
  344. {
  345. if (((ClassMap)map.ObjectMap).FindMember (member.Name) != null)
  346. return true;
  347. else if (map.BaseMap != null)
  348. return DefinedInBaseMap (map.BaseMap, member);
  349. else
  350. return false;
  351. }
  352. void AddArrayElementFieldMember (CodeTypeDeclaration codeClass, XmlTypeMapMemberList member, string defaultNamespace)
  353. {
  354. CodeTypeMember codeField = CreateFieldMember (codeClass, member.TypeData, member.Name);
  355. CodeAttributeDeclarationCollection attributes = new CodeAttributeDeclarationCollection ();
  356. AddArrayAttributes (attributes, member, defaultNamespace, false);
  357. ListMap listMap = (ListMap) member.ListTypeMapping.ObjectMap;
  358. AddArrayItemAttributes (attributes, listMap, member.TypeData.ListItemTypeData, defaultNamespace, 0);
  359. if (attributes.Count > 0) codeField.CustomAttributes = attributes;
  360. }
  361. public void AddArrayAttributes (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member, string defaultNamespace, bool forceUseMemberName)
  362. {
  363. GenerateArrayElement (attributes, member, defaultNamespace, forceUseMemberName);
  364. }
  365. public void AddArrayItemAttributes (CodeAttributeDeclarationCollection attributes, ListMap listMap, TypeData type, string defaultNamespace, int nestingLevel)
  366. {
  367. foreach (XmlTypeMapElementInfo ainfo in listMap.ItemInfo)
  368. {
  369. string defaultName;
  370. if (ainfo.MappedType != null) defaultName = ainfo.MappedType.ElementName;
  371. else defaultName = ainfo.TypeData.XmlType;
  372. GenerateArrayItemAttributes (attributes, listMap, type, ainfo, defaultName, defaultNamespace, nestingLevel);
  373. if (ainfo.MappedType != null) {
  374. if (!IsMapExported (ainfo.MappedType) && includeArrayTypes)
  375. AddInclude (ainfo.MappedType);
  376. ExportMapCode (ainfo.MappedType, false);
  377. }
  378. }
  379. if (listMap.IsMultiArray)
  380. {
  381. XmlTypeMapping nmap = listMap.NestedArrayMapping;
  382. AddArrayItemAttributes (attributes, (ListMap) nmap.ObjectMap, nmap.TypeData.ListItemTypeData, defaultNamespace, nestingLevel + 1);
  383. }
  384. }
  385. void ExportArrayCode (XmlTypeMapping map)
  386. {
  387. ListMap listMap = (ListMap) map.ObjectMap;
  388. foreach (XmlTypeMapElementInfo ainfo in listMap.ItemInfo)
  389. {
  390. if (ainfo.MappedType != null) {
  391. if (!IsMapExported (ainfo.MappedType) && includeArrayTypes)
  392. AddInclude (ainfo.MappedType);
  393. ExportMapCode (ainfo.MappedType, false);
  394. }
  395. }
  396. }
  397. bool ExportExtraElementAttributes (CodeAttributeDeclarationCollection attributes, XmlTypeMapElementInfo einfo, string defaultNamespace, TypeData defaultType)
  398. {
  399. if (einfo.IsTextElement) {
  400. GenerateTextElementAttribute (attributes, einfo, defaultType);
  401. return true;
  402. }
  403. else if (einfo.IsUnnamedAnyElement) {
  404. GenerateUnnamedAnyElementAttribute (attributes, einfo, defaultNamespace);
  405. return true;
  406. }
  407. return false;
  408. }
  409. void ExportEnumCode (XmlTypeMapping map, bool isTopLevel)
  410. {
  411. if (IsMapExported (map)) return;
  412. CodeTypeDeclaration codeEnum = new CodeTypeDeclaration (map.TypeData.TypeName);
  413. SetMapExported (map, codeEnum);
  414. codeEnum.Attributes = MemberAttributes.Public;
  415. codeEnum.IsEnum = true;
  416. AddCodeType (codeEnum, map.Documentation);
  417. EnumMap emap = (EnumMap) map.ObjectMap;
  418. if (emap.IsFlags)
  419. codeEnum.CustomAttributes.Add (new CodeAttributeDeclaration ("System.FlagsAttribute"));
  420. #if NET_2_0
  421. CodeAttributeDeclaration generatedCodeAttribute = new CodeAttributeDeclaration (
  422. new CodeTypeReference (typeof(GeneratedCodeAttribute)));
  423. generatedCodeAttribute.Arguments.Add (new CodeAttributeArgument (
  424. new CodePrimitiveExpression ("System.Xml")));
  425. generatedCodeAttribute.Arguments.Add (new CodeAttributeArgument (
  426. new CodePrimitiveExpression (Consts.FxFileVersion)));
  427. codeEnum.CustomAttributes.Add (generatedCodeAttribute);
  428. codeEnum.CustomAttributes.Add (new CodeAttributeDeclaration (
  429. new CodeTypeReference (typeof (SerializableAttribute))));
  430. #endif
  431. GenerateEnum (map, codeEnum, isTopLevel);
  432. int flag = 1;
  433. foreach (EnumMap.EnumMapMember emem in emap.Members)
  434. {
  435. CodeMemberField codeField = new CodeMemberField ("", emem.EnumName);
  436. if (emap.IsFlags) {
  437. codeField.InitExpression = new CodePrimitiveExpression (flag);
  438. flag *= 2;
  439. }
  440. AddComments (codeField, emem.Documentation);
  441. GenerateEnumItem (codeField, emem);
  442. codeEnum.Members.Add (codeField);
  443. }
  444. }
  445. void AddInclude (XmlTypeMapping map)
  446. {
  447. if (!includeMaps.ContainsKey (map.TypeData.FullTypeName))
  448. includeMaps [map.TypeData.FullTypeName] = map;
  449. }
  450. void RemoveInclude (XmlTypeMapping map)
  451. {
  452. includeMaps.Remove (map.TypeData.FullTypeName);
  453. }
  454. #endregion
  455. #region Helper methods
  456. bool IsMapExported (XmlTypeMapping map)
  457. {
  458. if (exportedMaps.Contains (map.TypeData.FullTypeName)) return true;
  459. return false;
  460. }
  461. void SetMapExported (XmlTypeMapping map, CodeTypeDeclaration declaration)
  462. {
  463. exportedMaps.Add (map.TypeData.FullTypeName, declaration);
  464. }
  465. CodeTypeDeclaration GetMapDeclaration (XmlTypeMapping map)
  466. {
  467. return exportedMaps [map.TypeData.FullTypeName] as CodeTypeDeclaration;
  468. }
  469. public static void AddCustomAttribute (CodeTypeMember ctm, CodeAttributeDeclaration att, bool addIfNoParams)
  470. {
  471. if (att.Arguments.Count == 0 && !addIfNoParams) return;
  472. if (ctm.CustomAttributes == null) ctm.CustomAttributes = new CodeAttributeDeclarationCollection ();
  473. ctm.CustomAttributes.Add (att);
  474. }
  475. public static void AddCustomAttribute (CodeTypeMember ctm, string name, params CodeAttributeArgument[] args)
  476. {
  477. if (ctm.CustomAttributes == null) ctm.CustomAttributes = new CodeAttributeDeclarationCollection ();
  478. ctm.CustomAttributes.Add (new CodeAttributeDeclaration (name, args));
  479. }
  480. public static CodeAttributeArgument GetArg (string name, object value)
  481. {
  482. return new CodeAttributeArgument (name, new CodePrimitiveExpression(value));
  483. }
  484. public static CodeAttributeArgument GetArg (object value)
  485. {
  486. return new CodeAttributeArgument (new CodePrimitiveExpression(value));
  487. }
  488. public static CodeAttributeArgument GetTypeArg (string name, string typeName)
  489. {
  490. return new CodeAttributeArgument (name, new CodeTypeOfExpression(typeName));
  491. }
  492. public static CodeAttributeArgument GetEnumArg (string name, string enumType, string enumValue)
  493. {
  494. return new CodeAttributeArgument (name, new CodeFieldReferenceExpression (new CodeTypeReferenceExpression(enumType), enumValue));
  495. }
  496. public static void AddComments (CodeTypeMember member, string comments)
  497. {
  498. if (comments == null || comments == "") member.Comments.Add (new CodeCommentStatement ("<remarks/>", true));
  499. else member.Comments.Add (new CodeCommentStatement ("<remarks>\n" + comments + "\n</remarks>", true));
  500. }
  501. void AddCodeType (CodeTypeDeclaration type, string comments)
  502. {
  503. AddComments (type, comments);
  504. codeNamespace.Types.Add (type);
  505. }
  506. #if NET_2_0
  507. void AddClassAttributes (CodeTypeDeclaration codeClass)
  508. {
  509. CodeAttributeDeclaration generatedCodeAttribute = new CodeAttributeDeclaration (
  510. new CodeTypeReference (typeof (GeneratedCodeAttribute)));
  511. generatedCodeAttribute.Arguments.Add (new CodeAttributeArgument (
  512. new CodePrimitiveExpression ("System.Xml")));
  513. generatedCodeAttribute.Arguments.Add (new CodeAttributeArgument (
  514. new CodePrimitiveExpression (Consts.FxFileVersion)));
  515. codeClass.CustomAttributes.Add (generatedCodeAttribute);
  516. codeClass.CustomAttributes.Add (new CodeAttributeDeclaration (
  517. new CodeTypeReference (typeof (SerializableAttribute))));
  518. codeClass.CustomAttributes.Add (new CodeAttributeDeclaration (
  519. new CodeTypeReference (typeof (DebuggerStepThroughAttribute))));
  520. CodeAttributeDeclaration designerCategoryAttribute = new CodeAttributeDeclaration (
  521. new CodeTypeReference (typeof (DesignerCategoryAttribute)));
  522. designerCategoryAttribute.Arguments.Add (new CodeAttributeArgument (
  523. new CodePrimitiveExpression ("code")));
  524. codeClass.CustomAttributes.Add (designerCategoryAttribute);
  525. }
  526. #endif
  527. CodeTypeReference GetDomType (TypeData data)
  528. {
  529. #if NET_2_0
  530. if (data.IsValueType && data.IsNullable)
  531. return new CodeTypeReference ("System.Nullable", new CodeTypeReference (data.FullTypeName));
  532. #endif
  533. if (data.SchemaType == SchemaTypes.Array)
  534. return new CodeTypeReference (GetDomType (data.ListItemTypeData),1);
  535. else
  536. return new CodeTypeReference (data.FullTypeName);
  537. }
  538. #endregion
  539. #region Private Properties
  540. #if NET_2_0
  541. private CodeDomProvider CodeProvider {
  542. get {
  543. if (codeProvider == null) {
  544. codeProvider = new CSharpCodeProvider ();
  545. }
  546. return codeProvider;
  547. }
  548. }
  549. #endif
  550. #endregion
  551. #region Overridable methods
  552. protected virtual void GenerateClass (XmlTypeMapping map, CodeTypeDeclaration codeClass, bool isTopLevel)
  553. {
  554. }
  555. protected virtual void GenerateClassInclude (CodeAttributeDeclarationCollection attributes, XmlTypeMapping map)
  556. {
  557. }
  558. protected virtual void GenerateAnyAttribute (CodeTypeMember codeField)
  559. {
  560. }
  561. protected virtual void GenerateDefaultAttribute (CodeMemberField internalField, CodeTypeMember externalField, TypeData typeData, object defaultValue)
  562. {
  563. if (typeData.Type == null)
  564. {
  565. // It must be an enumeration defined in the schema.
  566. if (typeData.SchemaType != SchemaTypes.Enum)
  567. throw new InvalidOperationException ("Type " + typeData.TypeName + " not supported");
  568. IFormattable defaultValueFormattable = defaultValue as IFormattable;
  569. CodeFieldReferenceExpression fref = new CodeFieldReferenceExpression (new CodeTypeReferenceExpression (GetDomType (typeData)), defaultValueFormattable != null ? defaultValueFormattable.ToString(null, CultureInfo.InvariantCulture) : defaultValue.ToString ());
  570. CodeAttributeArgument arg = new CodeAttributeArgument (fref);
  571. AddCustomAttribute (externalField, "System.ComponentModel.DefaultValue", arg);
  572. internalField.InitExpression = fref;
  573. }
  574. else
  575. {
  576. AddCustomAttribute (externalField, "System.ComponentModel.DefaultValue", GetArg (defaultValue));
  577. internalField.InitExpression = new CodePrimitiveExpression (defaultValue);
  578. }
  579. }
  580. protected virtual void GenerateAttributeMember (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberAttribute attinfo, string defaultNamespace, bool forceUseMemberName)
  581. {
  582. }
  583. protected virtual void GenerateElementInfoMember (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member, XmlTypeMapElementInfo einfo, TypeData defaultType, string defaultNamespace, bool addAlwaysAttr, bool forceUseMemberName)
  584. {
  585. }
  586. protected virtual void GenerateElementMember (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member)
  587. {
  588. }
  589. protected virtual void GenerateArrayElement (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member, string defaultNamespace, bool forceUseMemberName)
  590. {
  591. }
  592. protected virtual void GenerateArrayItemAttributes (CodeAttributeDeclarationCollection attributes, ListMap listMap, TypeData type, XmlTypeMapElementInfo ainfo, string defaultName, string defaultNamespace, int nestingLevel)
  593. {
  594. }
  595. protected virtual void GenerateTextElementAttribute (CodeAttributeDeclarationCollection attributes, XmlTypeMapElementInfo einfo, TypeData defaultType)
  596. {
  597. }
  598. protected virtual void GenerateUnnamedAnyElementAttribute (CodeAttributeDeclarationCollection attributes, XmlTypeMapElementInfo einfo, string defaultNamespace)
  599. {
  600. }
  601. protected virtual void GenerateEnum (XmlTypeMapping map, CodeTypeDeclaration codeEnum, bool isTopLevel)
  602. {
  603. }
  604. protected virtual void GenerateEnumItem (CodeMemberField codeField, EnumMap.EnumMapMember emem)
  605. {
  606. }
  607. protected virtual void GenerateSpecifierMember (CodeTypeMember codeField)
  608. {
  609. }
  610. #endregion
  611. }
  612. }