XmlSchemaExporter.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. //
  2. // System.Xml.Serialization.XmlSchemaExporter
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // Copyright (C) Tim Coleman, 2002
  9. //
  10. using System.Xml;
  11. using System.Xml.Schema;
  12. using System.Collections;
  13. namespace System.Xml.Serialization {
  14. public class XmlSchemaExporter {
  15. #region Fields
  16. XmlSchemas schemas;
  17. Hashtable exportedMaps = new Hashtable();
  18. #endregion
  19. #region Constructors
  20. public XmlSchemaExporter (XmlSchemas schemas)
  21. {
  22. this.schemas = schemas;
  23. }
  24. #endregion // Constructors
  25. #region Methods
  26. [MonoTODO]
  27. public string ExportAnyType (string ns)
  28. {
  29. throw new NotImplementedException ();
  30. }
  31. public void ExportMembersMapping (XmlMembersMapping xmlMembersMapping)
  32. {
  33. XmlSchema schema = GetSchema (xmlMembersMapping.Namespace);
  34. XmlSchemaElement selem = new XmlSchemaElement ();
  35. selem.Name = xmlMembersMapping.ElementName;
  36. schema.Items.Add (selem);
  37. XmlSchemaComplexType stype = new XmlSchemaComplexType ();
  38. XmlSchemaSequence particle;
  39. XmlSchemaAnyAttribute anyAttribute;
  40. ExportMembersMapSchema (schema, (ClassMap)xmlMembersMapping.ObjectMap, null, stype.Attributes, out particle, out anyAttribute);
  41. stype.Particle = particle;
  42. stype.AnyAttribute = anyAttribute;
  43. selem.SchemaType = stype;
  44. CompileSchemas ();
  45. }
  46. [MonoTODO]
  47. public XmlQualifiedName ExportTypeMapping (XmlMembersMapping xmlMembersMapping)
  48. {
  49. throw new NotImplementedException ();
  50. }
  51. public void ExportTypeMapping (XmlTypeMapping xmlTypeMapping)
  52. {
  53. XmlSchema schema = GetSchema (xmlTypeMapping.Namespace);
  54. XmlTypeMapElementInfo einfo = new XmlTypeMapElementInfo (null, xmlTypeMapping.TypeData);
  55. einfo.Namespace = xmlTypeMapping.Namespace;
  56. einfo.ElementName = xmlTypeMapping.ElementName;
  57. einfo.MappedType = xmlTypeMapping;
  58. einfo.IsNullable = false;
  59. AddSchemaElement (schema.Items, schema, einfo, false);
  60. CompileSchemas ();
  61. }
  62. void ExportClassSchema (XmlTypeMapping map)
  63. {
  64. if (IsMapExported (map)) return;
  65. SetMapExported (map);
  66. XmlSchema schema = GetSchema (map.XmlTypeNamespace);
  67. XmlSchemaComplexType stype = new XmlSchemaComplexType ();
  68. stype.Name = map.XmlType;
  69. schema.Items.Add (stype);
  70. ClassMap cmap = (ClassMap)map.ObjectMap;
  71. if (cmap.HasSimpleContent)
  72. {
  73. XmlSchemaSimpleContent simple = new XmlSchemaSimpleContent ();
  74. stype.ContentModel = simple;
  75. XmlSchemaSimpleContentExtension ext = new XmlSchemaSimpleContentExtension ();
  76. simple.Content = ext;
  77. XmlSchemaSequence particle;
  78. XmlSchemaAnyAttribute anyAttribute;
  79. ExportMembersMapSchema (schema, cmap, map.BaseMap, ext.Attributes, out particle, out anyAttribute);
  80. ext.AnyAttribute = anyAttribute;
  81. if (map.BaseMap == null)
  82. ext.BaseTypeName = cmap.SimpleContentBaseType;
  83. else
  84. ext.BaseTypeName = new XmlQualifiedName (map.BaseMap.XmlType, map.BaseMap.XmlTypeNamespace);
  85. }
  86. else if (map.BaseMap != null)
  87. {
  88. XmlSchemaComplexContent cstype = new XmlSchemaComplexContent ();
  89. XmlSchemaComplexContentExtension ext = new XmlSchemaComplexContentExtension ();
  90. ext.BaseTypeName = new XmlQualifiedName (map.BaseMap.XmlType, map.BaseMap.XmlTypeNamespace);
  91. cstype.Content = ext;
  92. stype.ContentModel = cstype;
  93. XmlSchemaSequence particle;
  94. XmlSchemaAnyAttribute anyAttribute;
  95. ExportMembersMapSchema (schema, cmap, map.BaseMap, ext.Attributes, out particle, out anyAttribute);
  96. ext.Particle = particle;
  97. ext.AnyAttribute = anyAttribute;
  98. ImportNamespace (schema, map.BaseMap.Namespace);
  99. ExportClassSchema (map.BaseMap);
  100. }
  101. else
  102. {
  103. XmlSchemaSequence particle;
  104. XmlSchemaAnyAttribute anyAttribute;
  105. ExportMembersMapSchema (schema, cmap, map.BaseMap, stype.Attributes, out particle, out anyAttribute);
  106. stype.Particle = particle;
  107. stype.AnyAttribute = anyAttribute;
  108. stype.IsMixed = cmap.XmlTextCollector != null;
  109. }
  110. }
  111. void ExportMembersMapSchema (XmlSchema schema, ClassMap map, XmlTypeMapping baseMap, XmlSchemaObjectCollection outAttributes, out XmlSchemaSequence particle, out XmlSchemaAnyAttribute anyAttribute)
  112. {
  113. particle = null;
  114. XmlSchemaSequence seq = new XmlSchemaSequence ();
  115. ICollection members = map.ElementMembers;
  116. if (members != null)
  117. {
  118. foreach (XmlTypeMapMemberElement member in members)
  119. {
  120. if (baseMap != null && DefinedInBaseMap (baseMap, member)) continue;
  121. Type memType = member.GetType();
  122. if (memType == typeof(XmlTypeMapMemberFlatList))
  123. {
  124. AddSchemaArrayElement (seq.Items, schema, member.ElementInfo);
  125. }
  126. else if (memType == typeof(XmlTypeMapMemberAnyElement))
  127. {
  128. AddSchemaArrayElement (seq.Items, schema, member.ElementInfo);
  129. }
  130. else if (memType == typeof(XmlTypeMapMemberAnyAttribute))
  131. {
  132. // Ignore
  133. }
  134. else if (memType == typeof(XmlTypeMapMemberElement))
  135. {
  136. XmlSchemaElement selem = (XmlSchemaElement) AddSchemaElement (seq.Items, schema, (XmlTypeMapElementInfo) member.ElementInfo [0], member.DefaultValue, true);
  137. }
  138. else
  139. {
  140. AddSchemaElement (seq.Items, schema, (XmlTypeMapElementInfo) member.ElementInfo [0], true);
  141. }
  142. }
  143. }
  144. if (seq.Items.Count > 0)
  145. particle = seq;
  146. // Write attributes
  147. ICollection attributes = map.AttributeMembers;
  148. if (attributes != null)
  149. {
  150. foreach (XmlTypeMapMemberAttribute attr in attributes) {
  151. if (baseMap != null && DefinedInBaseMap (baseMap, attr)) continue;
  152. outAttributes.Add (GetSchemaAttribute (schema, attr));
  153. }
  154. }
  155. XmlTypeMapMember anyAttrMember = map.DefaultAnyAttributeMember;
  156. if (anyAttrMember != null)
  157. anyAttribute = new XmlSchemaAnyAttribute ();
  158. else
  159. anyAttribute = null;
  160. }
  161. XmlSchemaAttribute GetSchemaAttribute (XmlSchema currentSchema, XmlTypeMapMemberAttribute attinfo)
  162. {
  163. XmlSchemaAttribute sat = new XmlSchemaAttribute ();
  164. if (attinfo.DefaultValue != System.DBNull.Value) sat.DefaultValue = XmlCustomFormatter.ToXmlString (attinfo.TypeData, attinfo.DefaultValue);
  165. ImportNamespace (currentSchema, attinfo.Namespace);
  166. XmlSchema memberSchema = GetSchema (attinfo.Namespace);
  167. if (currentSchema == memberSchema)
  168. {
  169. sat.Name = attinfo.AttributeName;
  170. if (attinfo.TypeData.SchemaType == SchemaTypes.Enum)
  171. {
  172. ImportNamespace (currentSchema, attinfo.DataTypeNamespace);
  173. ExportEnumSchema (attinfo.MappedType);
  174. sat.SchemaTypeName = new XmlQualifiedName (attinfo.TypeData.XmlType, attinfo.DataTypeNamespace);;
  175. }
  176. else if (attinfo.TypeData.SchemaType == SchemaTypes.Array && TypeTranslator.IsPrimitive (attinfo.TypeData.ListItemType))
  177. {
  178. sat.SchemaType = GetSchemaSimpleListType (attinfo.TypeData);
  179. }
  180. else
  181. sat.SchemaTypeName = new XmlQualifiedName (attinfo.TypeData.XmlType, attinfo.DataTypeNamespace);;
  182. }
  183. else
  184. {
  185. sat.RefName = new XmlQualifiedName (attinfo.AttributeName, attinfo.Namespace);
  186. memberSchema.Items.Add (GetSchemaAttribute (memberSchema, attinfo));
  187. }
  188. return sat;
  189. }
  190. XmlSchemaParticle AddSchemaElement (XmlSchemaObjectCollection destcol, XmlSchema currentSchema, XmlTypeMapElementInfo einfo, bool isTypeMember)
  191. {
  192. return AddSchemaElement (destcol, currentSchema, einfo, System.DBNull.Value, isTypeMember);
  193. }
  194. XmlSchemaParticle AddSchemaElement (XmlSchemaObjectCollection destcol, XmlSchema currentSchema, XmlTypeMapElementInfo einfo, object defaultValue, bool isTypeMember)
  195. {
  196. if (einfo.IsTextElement) return null;
  197. if (einfo.IsUnnamedAnyElement)
  198. {
  199. XmlSchemaAny any = new XmlSchemaAny ();
  200. any.MinOccurs = 0;
  201. any.MaxOccurs = 1;
  202. destcol.Add (any);
  203. return any;
  204. }
  205. XmlSchemaElement selem = new XmlSchemaElement ();
  206. destcol.Add (selem);
  207. if (isTypeMember)
  208. {
  209. selem.MaxOccurs = 1;
  210. selem.MinOccurs = einfo.IsNullable ? 1 : 0;
  211. if (einfo.TypeData.Type.IsPrimitive && einfo.TypeData.Type != typeof(string) ||
  212. einfo.TypeData.Type.IsEnum)
  213. selem.MinOccurs = 1;
  214. }
  215. XmlSchema memberSchema = GetSchema (einfo.Namespace);
  216. ImportNamespace (currentSchema, einfo.Namespace);
  217. if (currentSchema == memberSchema)
  218. {
  219. if (isTypeMember) selem.IsNillable = einfo.IsNullable;
  220. selem.Name = einfo.ElementName;
  221. XmlQualifiedName typeName = new XmlQualifiedName (einfo.TypeData.XmlType, einfo.DataTypeNamespace);
  222. if (defaultValue != System.DBNull.Value)
  223. selem.DefaultValue = XmlCustomFormatter.ToXmlString (einfo.TypeData, defaultValue);
  224. switch (einfo.TypeData.SchemaType)
  225. {
  226. case SchemaTypes.XmlNode:
  227. selem.SchemaType = GetSchemaXmlNodeType ();
  228. break;
  229. case SchemaTypes.XmlSerializable:
  230. selem.SchemaType = GetSchemaXmlSerializableType ();
  231. break;
  232. case SchemaTypes.Enum:
  233. selem.SchemaTypeName = new XmlQualifiedName (einfo.MappedType.XmlType, einfo.MappedType.XmlTypeNamespace);
  234. ImportNamespace (currentSchema, einfo.MappedType.XmlTypeNamespace);
  235. ExportEnumSchema (einfo.MappedType);
  236. break;
  237. case SchemaTypes.Array:
  238. selem.SchemaTypeName = new XmlQualifiedName (einfo.MappedType.XmlType, einfo.MappedType.XmlTypeNamespace);;
  239. ImportNamespace (currentSchema, einfo.MappedType.XmlTypeNamespace);
  240. ExportArraySchema (einfo.MappedType);
  241. break;
  242. case SchemaTypes.Class:
  243. if (einfo.MappedType.TypeData.Type != typeof(object)) {
  244. selem.SchemaTypeName = new XmlQualifiedName (einfo.MappedType.XmlType, einfo.MappedType.XmlTypeNamespace);;
  245. ImportNamespace (currentSchema, einfo.MappedType.XmlTypeNamespace);
  246. ExportClassSchema (einfo.MappedType);
  247. }
  248. break;
  249. case SchemaTypes.Primitive:
  250. selem.SchemaTypeName = new XmlQualifiedName (einfo.TypeData.XmlType, einfo.DataTypeNamespace);;
  251. break;
  252. }
  253. }
  254. else
  255. {
  256. selem.RefName = new XmlQualifiedName (einfo.ElementName, einfo.Namespace);
  257. AddSchemaElement (memberSchema.Items, memberSchema, einfo, defaultValue, false);
  258. }
  259. return selem;
  260. }
  261. void ImportNamespace (XmlSchema schema, string ns)
  262. {
  263. if (ns == "" || ns == schema.TargetNamespace) return;
  264. foreach (XmlSchemaObject sob in schema.Includes)
  265. if ((sob is XmlSchemaImport) && ((XmlSchemaImport)sob).Namespace == ns) return;
  266. XmlSchemaImport imp = new XmlSchemaImport ();
  267. imp.Namespace = ns;
  268. schema.Includes.Add (imp);
  269. }
  270. bool DefinedInBaseMap (XmlTypeMapping map, XmlTypeMapMember member)
  271. {
  272. if (((ClassMap)map.ObjectMap).FindMember (member.Name) != null)
  273. return true;
  274. else if (map.BaseMap != null)
  275. return DefinedInBaseMap (map.BaseMap, member);
  276. else
  277. return false;
  278. }
  279. XmlSchemaType GetSchemaXmlNodeType ()
  280. {
  281. XmlSchemaComplexType stype = new XmlSchemaComplexType ();
  282. stype.IsMixed = true;
  283. XmlSchemaSequence seq = new XmlSchemaSequence ();
  284. seq.Items.Add (new XmlSchemaAny ());
  285. stype.Particle = seq;
  286. return stype;
  287. }
  288. XmlSchemaType GetSchemaXmlSerializableType ()
  289. {
  290. XmlSchemaComplexType stype = new XmlSchemaComplexType ();
  291. XmlSchemaSequence seq = new XmlSchemaSequence ();
  292. XmlSchemaElement selem = new XmlSchemaElement ();
  293. selem.RefName = new XmlQualifiedName ("schema",XmlSchema.Namespace);
  294. seq.Items.Add (selem);
  295. seq.Items.Add (new XmlSchemaAny ());
  296. stype.Particle = seq;
  297. return stype;
  298. }
  299. XmlSchemaSimpleType GetSchemaSimpleListType (TypeData typeData)
  300. {
  301. XmlSchemaSimpleType stype = new XmlSchemaSimpleType ();
  302. XmlSchemaSimpleTypeList list = new XmlSchemaSimpleTypeList ();
  303. TypeData itemTypeData = TypeTranslator.GetTypeData (typeData.ListItemType);
  304. list.ItemTypeName = new XmlQualifiedName (itemTypeData.XmlType, XmlSchema.Namespace);
  305. stype.Content = list;
  306. return stype;
  307. }
  308. XmlSchemaParticle AddSchemaArrayElement (XmlSchemaObjectCollection destcol, XmlSchema currentSchema, XmlTypeMapElementInfoList infos)
  309. {
  310. int numInfos = infos.Count;
  311. if (numInfos > 0 && ((XmlTypeMapElementInfo)infos[0]).IsTextElement) numInfos--;
  312. if (numInfos == 0) return null;
  313. if (numInfos == 1)
  314. {
  315. XmlSchemaParticle selem = AddSchemaElement (destcol, currentSchema, (XmlTypeMapElementInfo) infos[infos.Count-1], true);
  316. selem.MinOccursString = "0";
  317. selem.MaxOccursString = "unbounded";
  318. return selem;
  319. }
  320. else
  321. {
  322. XmlSchemaChoice schoice = new XmlSchemaChoice ();
  323. destcol.Add (schoice);
  324. schoice.MinOccursString = "0";
  325. schoice.MaxOccursString = "unbounded";
  326. foreach (XmlTypeMapElementInfo einfo in infos)
  327. {
  328. if (einfo.IsTextElement) continue;
  329. AddSchemaElement (schoice.Items, currentSchema, einfo, true);
  330. }
  331. return schoice;
  332. }
  333. }
  334. void ExportEnumSchema (XmlTypeMapping map)
  335. {
  336. if (IsMapExported (map)) return;
  337. SetMapExported (map);
  338. XmlSchema schema = GetSchema (map.Namespace);
  339. XmlSchemaSimpleType stype = new XmlSchemaSimpleType ();
  340. stype.Name = map.ElementName;
  341. schema.Items.Add (stype);
  342. XmlSchemaSimpleTypeRestriction rest = new XmlSchemaSimpleTypeRestriction ();
  343. rest.BaseTypeName = new XmlQualifiedName ("string",XmlSchema.Namespace);
  344. EnumMap emap = (EnumMap) map.ObjectMap;
  345. foreach (EnumMap.EnumMapMember emem in emap.Members)
  346. {
  347. XmlSchemaEnumerationFacet ef = new XmlSchemaEnumerationFacet ();
  348. ef.Value = emem.XmlName;
  349. rest.Facets.Add (ef);
  350. }
  351. stype.Content = rest;
  352. }
  353. void ExportArraySchema (XmlTypeMapping map)
  354. {
  355. if (IsMapExported (map)) return;
  356. SetMapExported (map);
  357. XmlSchema schema = GetSchema (map.Namespace);
  358. XmlSchemaComplexType stype = new XmlSchemaComplexType ();
  359. stype.Name = map.ElementName;
  360. schema.Items.Add (stype);
  361. ListMap lmap = (ListMap) map.ObjectMap;
  362. XmlSchemaSequence seq = new XmlSchemaSequence ();
  363. XmlSchemaParticle spart = AddSchemaArrayElement (seq.Items, schema, lmap.ItemInfo);
  364. if (spart is XmlSchemaChoice)
  365. stype.Particle = spart;
  366. else
  367. stype.Particle = seq;
  368. }
  369. bool IsMapExported (XmlTypeMapping map)
  370. {
  371. if (exportedMaps.Contains (map)) return true;
  372. if (map.TypeData.Type == typeof(object)) return true;
  373. return false;
  374. }
  375. void SetMapExported (XmlTypeMapping map)
  376. {
  377. exportedMaps.Add (map,map);
  378. }
  379. void CompileSchemas ()
  380. {
  381. // foreach (XmlSchema sc in schemas)
  382. // sc.Compile (null);
  383. }
  384. XmlSchema GetSchema (string ns)
  385. {
  386. XmlSchema schema = schemas [ns];
  387. if (schema == null)
  388. {
  389. schema = new XmlSchema ();
  390. schema.TargetNamespace = ns;
  391. schema.ElementFormDefault = XmlSchemaForm.Qualified;
  392. schemas.Add (schema);
  393. }
  394. return schema;
  395. }
  396. #endregion // Methods
  397. }
  398. }