XmlSchemaExporter.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Xml;
  31. using System.Xml.Schema;
  32. using System.Collections;
  33. namespace System.Xml.Serialization {
  34. public class XmlSchemaExporter {
  35. #region Fields
  36. XmlSchemas schemas;
  37. Hashtable exportedMaps = new Hashtable();
  38. Hashtable exportedElements = new Hashtable();
  39. bool encodedFormat = false;
  40. XmlDocument xmlDoc;
  41. #endregion
  42. #region Constructors
  43. public XmlSchemaExporter (XmlSchemas schemas)
  44. {
  45. this.schemas = schemas;
  46. }
  47. internal XmlSchemaExporter (XmlSchemas schemas, bool encodedFormat)
  48. {
  49. this.encodedFormat = encodedFormat;
  50. this.schemas = schemas;
  51. }
  52. #endregion // Constructors
  53. #region Methods
  54. [MonoTODO]
  55. public string ExportAnyType (string ns)
  56. {
  57. throw new NotImplementedException ();
  58. }
  59. public void ExportMembersMapping (XmlMembersMapping xmlMembersMapping)
  60. {
  61. ExportMembersMapping (xmlMembersMapping, true);
  62. }
  63. #if NET_2_0
  64. public
  65. #else
  66. internal
  67. #endif
  68. void ExportMembersMapping (XmlMembersMapping xmlMembersMapping, bool exportEnclosingType)
  69. {
  70. ClassMap cmap = (ClassMap) xmlMembersMapping.ObjectMap;
  71. if (xmlMembersMapping.HasWrapperElement && exportEnclosingType)
  72. {
  73. XmlSchema schema = GetSchema (xmlMembersMapping.Namespace);
  74. XmlSchemaComplexType stype = new XmlSchemaComplexType ();
  75. XmlSchemaSequence particle;
  76. XmlSchemaAnyAttribute anyAttribute;
  77. ExportMembersMapSchema (schema, cmap, null, stype.Attributes, out particle, out anyAttribute);
  78. stype.Particle = particle;
  79. stype.AnyAttribute = anyAttribute;
  80. if (encodedFormat)
  81. {
  82. stype.Name = xmlMembersMapping.ElementName;
  83. schema.Items.Add (stype);
  84. }
  85. else
  86. {
  87. XmlSchemaElement selem = new XmlSchemaElement ();
  88. selem.Name = xmlMembersMapping.ElementName;
  89. selem.SchemaType = stype;
  90. schema.Items.Add (selem);
  91. }
  92. }
  93. else
  94. {
  95. ICollection members = cmap.ElementMembers;
  96. if (members != null)
  97. {
  98. foreach (XmlTypeMapMemberElement member in members)
  99. {
  100. if (member is XmlTypeMapMemberAnyElement && member.TypeData.IsListType)
  101. {
  102. XmlSchema mschema = GetSchema (xmlMembersMapping.Namespace);
  103. XmlSchemaParticle par = GetSchemaArrayElement (mschema, member.ElementInfo);
  104. if (par is XmlSchemaAny)
  105. {
  106. XmlSchemaComplexType ct = FindComplexType (mschema.Items, "any");
  107. if (ct != null) continue;
  108. ct = new XmlSchemaComplexType ();
  109. ct.Name = "any";
  110. ct.IsMixed = true;
  111. XmlSchemaSequence seq = new XmlSchemaSequence ();
  112. ct.Particle = seq;
  113. seq.Items.Add (par);
  114. mschema.Items.Add (ct);
  115. continue;
  116. }
  117. }
  118. XmlTypeMapElementInfo einfo = (XmlTypeMapElementInfo) member.ElementInfo [0];
  119. XmlSchema schema;
  120. if (encodedFormat)
  121. {
  122. schema = GetSchema (xmlMembersMapping.Namespace);
  123. ImportNamespace (schema, XmlSerializer.EncodingNamespace);
  124. }
  125. else
  126. schema = GetSchema (einfo.Namespace);
  127. XmlSchemaElement exe = FindElement (schema.Items, einfo.ElementName);
  128. XmlSchemaElement elem;
  129. XmlSchemaObjectContainer container = null;
  130. // In encoded format, the schema elements are not needed
  131. if (!encodedFormat)
  132. container = new XmlSchemaObjectContainer (schema);
  133. Type memType = member.GetType();
  134. if (member is XmlTypeMapMemberFlatList)
  135. throw new InvalidOperationException ("Unwrapped arrays not supported as parameters");
  136. else if (memType == typeof(XmlTypeMapMemberElement))
  137. elem = (XmlSchemaElement) GetSchemaElement (schema,
  138. einfo, member.DefaultValue, false, container);
  139. else
  140. elem = (XmlSchemaElement) GetSchemaElement (schema,
  141. einfo, false, container);
  142. if (exe != null)
  143. {
  144. if (exe.SchemaTypeName.Equals (elem.SchemaTypeName))
  145. schema.Items.Remove (elem);
  146. else
  147. {
  148. string s = "The XML element named '" + einfo.ElementName + "' ";
  149. s += "from namespace '" + schema.TargetNamespace + "' references distinct types " + elem.SchemaTypeName.Name + " and " + exe.SchemaTypeName.Name + ". ";
  150. s += "Use XML attributes to specify another XML name or namespace for the element or types.";
  151. throw new InvalidOperationException (s);
  152. }
  153. }
  154. }
  155. }
  156. }
  157. CompileSchemas ();
  158. }
  159. [MonoTODO]
  160. public XmlQualifiedName ExportTypeMapping (XmlMembersMapping xmlMembersMapping)
  161. {
  162. throw new NotImplementedException ();
  163. }
  164. public void ExportTypeMapping (XmlTypeMapping xmlTypeMapping)
  165. {
  166. if (!xmlTypeMapping.IncludeInSchema) return;
  167. if (IsElementExported (xmlTypeMapping)) return;
  168. if (encodedFormat)
  169. {
  170. ExportClassSchema (xmlTypeMapping);
  171. XmlSchema schema = GetSchema (xmlTypeMapping.XmlTypeNamespace);
  172. ImportNamespace (schema, XmlSerializer.EncodingNamespace);
  173. }
  174. else
  175. {
  176. XmlSchema schema = GetSchema (xmlTypeMapping.Namespace);
  177. XmlTypeMapElementInfo einfo = new XmlTypeMapElementInfo (null, xmlTypeMapping.TypeData);
  178. einfo.Namespace = xmlTypeMapping.Namespace;
  179. einfo.ElementName = xmlTypeMapping.ElementName;
  180. if (xmlTypeMapping.TypeData.IsComplexType)
  181. einfo.MappedType = xmlTypeMapping;
  182. einfo.IsNullable = false;
  183. GetSchemaElement (schema, einfo, false, new XmlSchemaObjectContainer (schema));
  184. SetElementExported (xmlTypeMapping);
  185. }
  186. CompileSchemas ();
  187. }
  188. void ExportXmlSerializableSchema (XmlSchema currentSchema, XmlSerializableMapping map)
  189. {
  190. if (IsMapExported (map)) return;
  191. SetMapExported (map);
  192. if (map.Schema == null) return;
  193. string targetNs = map.Schema.TargetNamespace;
  194. XmlSchema existingSchema = schemas [targetNs];
  195. if (existingSchema == null)
  196. {
  197. schemas.Add (map.Schema);
  198. ImportNamespace (currentSchema, targetNs);
  199. }
  200. else if (existingSchema != map.Schema)
  201. {
  202. throw new InvalidOperationException("The namespace '" + targetNs +"' defined by the class '" + map.TypeFullName + "' is a duplicate.");
  203. }
  204. }
  205. void ExportClassSchema (XmlTypeMapping map)
  206. {
  207. if (IsMapExported (map)) return;
  208. SetMapExported (map);
  209. if (map.TypeData.Type == typeof(object))
  210. {
  211. foreach (XmlTypeMapping dmap in map.DerivedTypes)
  212. if (dmap.TypeData.SchemaType == SchemaTypes.Class) ExportClassSchema (dmap);
  213. return;
  214. }
  215. XmlSchema schema = GetSchema (map.XmlTypeNamespace);
  216. XmlSchemaComplexType stype = new XmlSchemaComplexType ();
  217. stype.Name = map.XmlType;
  218. schema.Items.Add (stype);
  219. ClassMap cmap = (ClassMap)map.ObjectMap;
  220. if (cmap.HasSimpleContent)
  221. {
  222. XmlSchemaSimpleContent simple = new XmlSchemaSimpleContent ();
  223. stype.ContentModel = simple;
  224. XmlSchemaSimpleContentExtension ext = new XmlSchemaSimpleContentExtension ();
  225. simple.Content = ext;
  226. XmlSchemaSequence particle;
  227. XmlSchemaAnyAttribute anyAttribute;
  228. ExportMembersMapSchema (schema, cmap, map.BaseMap, ext.Attributes, out particle, out anyAttribute);
  229. ext.AnyAttribute = anyAttribute;
  230. if (map.BaseMap == null)
  231. ext.BaseTypeName = cmap.SimpleContentBaseType;
  232. else {
  233. ext.BaseTypeName = new XmlQualifiedName (map.BaseMap.XmlType, map.BaseMap.XmlTypeNamespace);
  234. ImportNamespace (schema, map.BaseMap.XmlTypeNamespace);
  235. ExportClassSchema (map.BaseMap);
  236. }
  237. }
  238. else if (map.BaseMap != null && map.BaseMap.IncludeInSchema)
  239. {
  240. XmlSchemaComplexContent cstype = new XmlSchemaComplexContent ();
  241. XmlSchemaComplexContentExtension ext = new XmlSchemaComplexContentExtension ();
  242. ext.BaseTypeName = new XmlQualifiedName (map.BaseMap.XmlType, map.BaseMap.XmlTypeNamespace);
  243. cstype.Content = ext;
  244. stype.ContentModel = cstype;
  245. XmlSchemaSequence particle;
  246. XmlSchemaAnyAttribute anyAttribute;
  247. ExportMembersMapSchema (schema, cmap, map.BaseMap, ext.Attributes, out particle, out anyAttribute);
  248. ext.Particle = particle;
  249. ext.AnyAttribute = anyAttribute;
  250. stype.IsMixed = HasMixedContent (map);
  251. cstype.IsMixed = BaseHasMixedContent (map);
  252. ImportNamespace (schema, map.BaseMap.XmlTypeNamespace);
  253. ExportClassSchema (map.BaseMap);
  254. }
  255. else
  256. {
  257. XmlSchemaSequence particle;
  258. XmlSchemaAnyAttribute anyAttribute;
  259. ExportMembersMapSchema (schema, cmap, map.BaseMap, stype.Attributes, out particle, out anyAttribute);
  260. stype.Particle = particle;
  261. stype.AnyAttribute = anyAttribute;
  262. stype.IsMixed = cmap.XmlTextCollector != null;
  263. }
  264. foreach (XmlTypeMapping dmap in map.DerivedTypes)
  265. if (dmap.TypeData.SchemaType == SchemaTypes.Class) ExportClassSchema (dmap);
  266. }
  267. bool BaseHasMixedContent (XmlTypeMapping map)
  268. {
  269. ClassMap cmap = (ClassMap)map.ObjectMap;
  270. return (cmap.XmlTextCollector != null && (map.BaseMap != null && DefinedInBaseMap (map.BaseMap, cmap.XmlTextCollector)));
  271. }
  272. bool HasMixedContent (XmlTypeMapping map)
  273. {
  274. ClassMap cmap = (ClassMap)map.ObjectMap;
  275. return (cmap.XmlTextCollector != null && (map.BaseMap == null || !DefinedInBaseMap (map.BaseMap, cmap.XmlTextCollector)));
  276. }
  277. void ExportMembersMapSchema (XmlSchema schema, ClassMap map, XmlTypeMapping baseMap, XmlSchemaObjectCollection outAttributes, out XmlSchemaSequence particle, out XmlSchemaAnyAttribute anyAttribute)
  278. {
  279. particle = null;
  280. XmlSchemaSequence seq = new XmlSchemaSequence ();
  281. ICollection members = map.ElementMembers;
  282. if (members != null && !map.HasSimpleContent)
  283. {
  284. foreach (XmlTypeMapMemberElement member in members)
  285. {
  286. if (baseMap != null && DefinedInBaseMap (baseMap, member)) continue;
  287. Type memType = member.GetType();
  288. if (memType == typeof(XmlTypeMapMemberFlatList))
  289. {
  290. XmlSchemaParticle part = GetSchemaArrayElement (schema, member.ElementInfo);
  291. if (part != null) seq.Items.Add (part);
  292. }
  293. else if (memType == typeof(XmlTypeMapMemberAnyElement))
  294. {
  295. seq.Items.Add (GetSchemaArrayElement (schema, member.ElementInfo));
  296. }
  297. else if (memType == typeof(XmlTypeMapMemberElement))
  298. {
  299. GetSchemaElement (schema, (XmlTypeMapElementInfo) member.ElementInfo [0],
  300. member.DefaultValue, true, new XmlSchemaObjectContainer (seq));
  301. }
  302. else
  303. {
  304. GetSchemaElement (schema, (XmlTypeMapElementInfo) member.ElementInfo[0],
  305. true, new XmlSchemaObjectContainer (seq));
  306. }
  307. }
  308. }
  309. if (seq.Items.Count > 0)
  310. particle = seq;
  311. // Write attributes
  312. ICollection attributes = map.AttributeMembers;
  313. if (attributes != null)
  314. {
  315. foreach (XmlTypeMapMemberAttribute attr in attributes) {
  316. if (baseMap != null && DefinedInBaseMap (baseMap, attr)) continue;
  317. outAttributes.Add (GetSchemaAttribute (schema, attr, true));
  318. }
  319. }
  320. XmlTypeMapMember anyAttrMember = map.DefaultAnyAttributeMember;
  321. if (anyAttrMember != null)
  322. anyAttribute = new XmlSchemaAnyAttribute ();
  323. else
  324. anyAttribute = null;
  325. }
  326. XmlSchemaElement FindElement (XmlSchemaObjectCollection col, string name)
  327. {
  328. foreach (XmlSchemaObject ob in col)
  329. {
  330. XmlSchemaElement elem = ob as XmlSchemaElement;
  331. if (elem != null && elem.Name == name) return elem;
  332. }
  333. return null;
  334. }
  335. XmlSchemaComplexType FindComplexType (XmlSchemaObjectCollection col, string name)
  336. {
  337. foreach (XmlSchemaObject ob in col)
  338. {
  339. XmlSchemaComplexType ctype = ob as XmlSchemaComplexType;
  340. if (ctype != null && ctype.Name == name) return ctype;
  341. }
  342. return null;
  343. }
  344. XmlSchemaAttribute GetSchemaAttribute (XmlSchema currentSchema, XmlTypeMapMemberAttribute attinfo, bool isTypeMember)
  345. {
  346. XmlSchemaAttribute sat = new XmlSchemaAttribute ();
  347. if (attinfo.DefaultValue != System.DBNull.Value) sat.DefaultValue = XmlCustomFormatter.ToXmlString (attinfo.TypeData, attinfo.DefaultValue);
  348. ImportNamespace (currentSchema, attinfo.Namespace);
  349. XmlSchema memberSchema;
  350. if (attinfo.Namespace.Length == 0 && attinfo.Form != XmlSchemaForm.Qualified)
  351. memberSchema = currentSchema;
  352. else
  353. memberSchema = GetSchema (attinfo.Namespace);
  354. if (currentSchema == memberSchema || encodedFormat)
  355. {
  356. sat.Name = attinfo.AttributeName;
  357. if (isTypeMember) sat.Form = attinfo.Form;
  358. if (attinfo.TypeData.SchemaType == SchemaTypes.Enum)
  359. {
  360. ImportNamespace (currentSchema, attinfo.DataTypeNamespace);
  361. ExportEnumSchema (attinfo.MappedType);
  362. sat.SchemaTypeName = new XmlQualifiedName (attinfo.TypeData.XmlType, attinfo.DataTypeNamespace);;
  363. }
  364. else if (attinfo.TypeData.SchemaType == SchemaTypes.Array && TypeTranslator.IsPrimitive (attinfo.TypeData.ListItemType))
  365. {
  366. sat.SchemaType = GetSchemaSimpleListType (attinfo.TypeData);
  367. }
  368. else
  369. sat.SchemaTypeName = new XmlQualifiedName (attinfo.TypeData.XmlType, attinfo.DataTypeNamespace);;
  370. }
  371. else
  372. {
  373. sat.RefName = new XmlQualifiedName (attinfo.AttributeName, attinfo.Namespace);
  374. foreach (XmlSchemaObject ob in memberSchema.Items)
  375. if (ob is XmlSchemaAttribute && ((XmlSchemaAttribute)ob).Name == attinfo.AttributeName)
  376. return sat;
  377. memberSchema.Items.Add (GetSchemaAttribute (memberSchema, attinfo, false));
  378. }
  379. return sat;
  380. }
  381. XmlSchemaParticle GetSchemaElement (XmlSchema currentSchema, XmlTypeMapElementInfo einfo, bool isTypeMember)
  382. {
  383. return GetSchemaElement (currentSchema, einfo, System.DBNull.Value,
  384. isTypeMember, (XmlSchemaObjectContainer) null);
  385. }
  386. XmlSchemaParticle GetSchemaElement (XmlSchema currentSchema, XmlTypeMapElementInfo einfo, bool isTypeMember, XmlSchemaObjectContainer container)
  387. {
  388. return GetSchemaElement (currentSchema, einfo, System.DBNull.Value, isTypeMember, container);
  389. }
  390. XmlSchemaParticle GetSchemaElement (XmlSchema currentSchema, XmlTypeMapElementInfo einfo, object defaultValue, bool isTypeMember, XmlSchemaObjectContainer container)
  391. {
  392. if (einfo.IsTextElement) return null;
  393. if (einfo.IsUnnamedAnyElement)
  394. {
  395. XmlSchemaAny any = new XmlSchemaAny ();
  396. any.MinOccurs = 0;
  397. any.MaxOccurs = 1;
  398. if (container != null)
  399. container.Items.Add (any);
  400. return any;
  401. }
  402. XmlSchemaElement selem = new XmlSchemaElement ();
  403. if (container != null)
  404. container.Items.Add (selem);
  405. if (isTypeMember)
  406. {
  407. selem.MaxOccurs = 1;
  408. selem.MinOccurs = einfo.IsNullable ? 1 : 0;
  409. if ((einfo.TypeData.IsValueType && einfo.Member != null && !einfo.Member.IsOptionalValueType) || encodedFormat)
  410. selem.MinOccurs = 1;
  411. }
  412. XmlSchema memberSchema = null;
  413. if (!encodedFormat)
  414. {
  415. memberSchema = GetSchema (einfo.Namespace);
  416. ImportNamespace (currentSchema, einfo.Namespace);
  417. }
  418. if (currentSchema == memberSchema || encodedFormat || !isTypeMember)
  419. {
  420. if (isTypeMember) selem.IsNillable = einfo.IsNullable;
  421. selem.Name = einfo.ElementName;
  422. if (defaultValue != System.DBNull.Value)
  423. selem.DefaultValue = XmlCustomFormatter.ToXmlString (einfo.TypeData, defaultValue);
  424. if (einfo.Form != XmlSchemaForm.Qualified)
  425. selem.Form = einfo.Form;
  426. switch (einfo.TypeData.SchemaType)
  427. {
  428. case SchemaTypes.XmlNode:
  429. selem.SchemaType = GetSchemaXmlNodeType ();
  430. break;
  431. case SchemaTypes.XmlSerializable:
  432. selem.SchemaType = GetSchemaXmlSerializableType (einfo.MappedType as XmlSerializableMapping);
  433. ExportXmlSerializableSchema (currentSchema, einfo.MappedType as XmlSerializableMapping);
  434. break;
  435. case SchemaTypes.Enum:
  436. selem.SchemaTypeName = new XmlQualifiedName (einfo.MappedType.XmlType, einfo.MappedType.XmlTypeNamespace);
  437. ImportNamespace (currentSchema, einfo.MappedType.XmlTypeNamespace);
  438. ExportEnumSchema (einfo.MappedType);
  439. break;
  440. case SchemaTypes.Array:
  441. XmlQualifiedName atypeName = ExportArraySchema (einfo.MappedType, currentSchema.TargetNamespace);
  442. selem.SchemaTypeName = atypeName;
  443. ImportNamespace (currentSchema, atypeName.Namespace);
  444. break;
  445. case SchemaTypes.Class:
  446. if (einfo.MappedType.TypeData.Type != typeof(object)) {
  447. selem.SchemaTypeName = new XmlQualifiedName (einfo.MappedType.XmlType, einfo.MappedType.XmlTypeNamespace);
  448. ImportNamespace (currentSchema, einfo.MappedType.XmlTypeNamespace);
  449. }
  450. else if (encodedFormat)
  451. selem.SchemaTypeName = new XmlQualifiedName (einfo.MappedType.XmlType, einfo.MappedType.XmlTypeNamespace);
  452. ExportClassSchema (einfo.MappedType);
  453. break;
  454. case SchemaTypes.Primitive:
  455. selem.SchemaTypeName = new XmlQualifiedName (einfo.TypeData.XmlType, einfo.DataTypeNamespace);
  456. if (!einfo.TypeData.IsXsdType) {
  457. ImportNamespace (currentSchema, einfo.MappedType.XmlTypeNamespace);
  458. ExportDerivedSchema (einfo.MappedType);
  459. }
  460. break;
  461. }
  462. }
  463. else
  464. {
  465. selem.RefName = new XmlQualifiedName (einfo.ElementName, einfo.Namespace);
  466. foreach (XmlSchemaObject ob in memberSchema.Items)
  467. if (ob is XmlSchemaElement && ((XmlSchemaElement)ob).Name == einfo.ElementName)
  468. return selem;
  469. GetSchemaElement (memberSchema, einfo, defaultValue, false,
  470. new XmlSchemaObjectContainer (memberSchema));
  471. }
  472. return selem;
  473. }
  474. void ImportNamespace (XmlSchema schema, string ns)
  475. {
  476. if (ns == null || ns.Length == 0 ||
  477. ns == schema.TargetNamespace || ns == XmlSchema.Namespace) return;
  478. foreach (XmlSchemaObject sob in schema.Includes)
  479. if ((sob is XmlSchemaImport) && ((XmlSchemaImport)sob).Namespace == ns) return;
  480. XmlSchemaImport imp = new XmlSchemaImport ();
  481. imp.Namespace = ns;
  482. schema.Includes.Add (imp);
  483. }
  484. bool DefinedInBaseMap (XmlTypeMapping map, XmlTypeMapMember member)
  485. {
  486. if (((ClassMap)map.ObjectMap).FindMember (member.Name) != null)
  487. return true;
  488. else if (map.BaseMap != null)
  489. return DefinedInBaseMap (map.BaseMap, member);
  490. else
  491. return false;
  492. }
  493. XmlSchemaType GetSchemaXmlNodeType ()
  494. {
  495. XmlSchemaComplexType stype = new XmlSchemaComplexType ();
  496. stype.IsMixed = true;
  497. XmlSchemaSequence seq = new XmlSchemaSequence ();
  498. seq.Items.Add (new XmlSchemaAny ());
  499. stype.Particle = seq;
  500. return stype;
  501. }
  502. XmlSchemaType GetSchemaXmlSerializableType (XmlSerializableMapping map)
  503. {
  504. XmlSchemaComplexType stype = new XmlSchemaComplexType ();
  505. XmlSchemaSequence seq = new XmlSchemaSequence ();
  506. if (map.Schema == null) {
  507. XmlSchemaElement selem = new XmlSchemaElement ();
  508. selem.RefName = new XmlQualifiedName ("schema",XmlSchema.Namespace);
  509. seq.Items.Add (selem);
  510. seq.Items.Add (new XmlSchemaAny ());
  511. } else {
  512. XmlSchemaAny any = new XmlSchemaAny ();
  513. any.Namespace = map.Schema.TargetNamespace;
  514. seq.Items.Add (any);
  515. }
  516. stype.Particle = seq;
  517. return stype;
  518. }
  519. XmlSchemaSimpleType GetSchemaSimpleListType (TypeData typeData)
  520. {
  521. XmlSchemaSimpleType stype = new XmlSchemaSimpleType ();
  522. XmlSchemaSimpleTypeList list = new XmlSchemaSimpleTypeList ();
  523. TypeData itemTypeData = TypeTranslator.GetTypeData (typeData.ListItemType);
  524. list.ItemTypeName = new XmlQualifiedName (itemTypeData.XmlType, XmlSchema.Namespace);
  525. stype.Content = list;
  526. return stype;
  527. }
  528. XmlSchemaParticle GetSchemaArrayElement (XmlSchema currentSchema, XmlTypeMapElementInfoList infos)
  529. {
  530. int numInfos = infos.Count;
  531. if (numInfos > 0 && ((XmlTypeMapElementInfo)infos[0]).IsTextElement) numInfos--;
  532. if (numInfos == 0) return null;
  533. if (numInfos == 1)
  534. {
  535. XmlSchemaParticle selem = GetSchemaElement (currentSchema, (XmlTypeMapElementInfo) infos[infos.Count-1], true);
  536. selem.MinOccursString = "0";
  537. selem.MaxOccursString = "unbounded";
  538. return selem;
  539. }
  540. else
  541. {
  542. XmlSchemaChoice schoice = new XmlSchemaChoice ();
  543. schoice.MinOccursString = "0";
  544. schoice.MaxOccursString = "unbounded";
  545. foreach (XmlTypeMapElementInfo einfo in infos)
  546. {
  547. if (einfo.IsTextElement) continue;
  548. schoice.Items.Add (GetSchemaElement (currentSchema, einfo, true));
  549. }
  550. return schoice;
  551. }
  552. }
  553. void ExportDerivedSchema(XmlTypeMapping map) {
  554. if (IsMapExported (map)) return;
  555. SetMapExported (map);
  556. XmlSchema schema = GetSchema (map.XmlTypeNamespace);
  557. XmlSchemaSimpleType stype = new XmlSchemaSimpleType ();
  558. stype.Name = map.ElementName;
  559. schema.Items.Add (stype);
  560. XmlSchemaSimpleTypeRestriction rest = new XmlSchemaSimpleTypeRestriction ();
  561. rest.BaseTypeName = new XmlQualifiedName (map.TypeData.MappedType.XmlType, XmlSchema.Namespace);
  562. XmlSchemaPatternFacet facet = map.TypeData.XmlSchemaPatternFacet;
  563. if (facet != null)
  564. rest.Facets.Add(facet);
  565. stype.Content = rest;
  566. }
  567. void ExportEnumSchema (XmlTypeMapping map)
  568. {
  569. if (IsMapExported (map)) return;
  570. SetMapExported (map);
  571. XmlSchema schema = GetSchema (map.XmlTypeNamespace);
  572. XmlSchemaSimpleType stype = new XmlSchemaSimpleType ();
  573. stype.Name = map.ElementName;
  574. schema.Items.Add (stype);
  575. XmlSchemaSimpleTypeRestriction rest = new XmlSchemaSimpleTypeRestriction ();
  576. rest.BaseTypeName = new XmlQualifiedName ("string",XmlSchema.Namespace);
  577. EnumMap emap = (EnumMap) map.ObjectMap;
  578. foreach (EnumMap.EnumMapMember emem in emap.Members)
  579. {
  580. XmlSchemaEnumerationFacet ef = new XmlSchemaEnumerationFacet ();
  581. ef.Value = emem.XmlName;
  582. rest.Facets.Add (ef);
  583. }
  584. stype.Content = rest;
  585. }
  586. XmlQualifiedName ExportArraySchema (XmlTypeMapping map, string defaultNamespace)
  587. {
  588. ListMap lmap = (ListMap) map.ObjectMap;
  589. if (encodedFormat)
  590. {
  591. string name, ns, schemaNs;
  592. lmap.GetArrayType (-1, out name, out ns);
  593. if (ns == XmlSchema.Namespace) schemaNs = defaultNamespace;
  594. else schemaNs = ns;
  595. if (IsMapExported (map)) return new XmlQualifiedName (lmap.GetSchemaArrayName (), schemaNs);
  596. SetMapExported (map);
  597. XmlSchema schema = GetSchema (schemaNs);
  598. XmlSchemaComplexType stype = new XmlSchemaComplexType ();
  599. stype.Name = lmap.GetSchemaArrayName ();
  600. schema.Items.Add (stype);
  601. XmlSchemaComplexContent content = new XmlSchemaComplexContent();
  602. content.IsMixed = false;
  603. stype.ContentModel = content;
  604. XmlSchemaComplexContentRestriction rest = new XmlSchemaComplexContentRestriction ();
  605. content.Content = rest;
  606. rest.BaseTypeName = new XmlQualifiedName ("Array", XmlSerializer.EncodingNamespace);
  607. XmlSchemaAttribute at = new XmlSchemaAttribute ();
  608. rest.Attributes.Add (at);
  609. at.RefName = new XmlQualifiedName ("arrayType", XmlSerializer.EncodingNamespace);
  610. XmlAttribute arrayType = Document.CreateAttribute ("arrayType", XmlSerializer.WsdlNamespace);
  611. arrayType.Value = ns + (ns != "" ? ":" : "") + name;
  612. at.UnhandledAttributes = new XmlAttribute [] { arrayType };
  613. ImportNamespace (schema, XmlSerializer.WsdlNamespace);
  614. XmlTypeMapElementInfo einfo = (XmlTypeMapElementInfo) lmap.ItemInfo[0];
  615. if (einfo.MappedType != null)
  616. {
  617. switch (einfo.TypeData.SchemaType)
  618. {
  619. case SchemaTypes.Enum:
  620. ExportEnumSchema (einfo.MappedType);
  621. break;
  622. case SchemaTypes.Array:
  623. ExportArraySchema (einfo.MappedType, schemaNs);
  624. break;
  625. case SchemaTypes.Class:
  626. ExportClassSchema (einfo.MappedType);
  627. break;
  628. }
  629. }
  630. return new XmlQualifiedName (lmap.GetSchemaArrayName (), schemaNs);
  631. }
  632. else
  633. {
  634. if (IsMapExported (map)) return new XmlQualifiedName (map.XmlType, map.XmlTypeNamespace);
  635. SetMapExported (map);
  636. XmlSchema schema = GetSchema (map.XmlTypeNamespace);
  637. XmlSchemaComplexType stype = new XmlSchemaComplexType ();
  638. stype.Name = map.ElementName;
  639. schema.Items.Add (stype);
  640. XmlSchemaParticle spart = GetSchemaArrayElement (schema, lmap.ItemInfo);
  641. if (spart is XmlSchemaChoice)
  642. stype.Particle = spart;
  643. else
  644. {
  645. XmlSchemaSequence seq = new XmlSchemaSequence ();
  646. seq.Items.Add (spart);
  647. stype.Particle = seq;
  648. }
  649. return new XmlQualifiedName (map.XmlType, map.XmlTypeNamespace);
  650. }
  651. }
  652. XmlDocument Document
  653. {
  654. get
  655. {
  656. if (xmlDoc == null) xmlDoc = new XmlDocument ();
  657. return xmlDoc;
  658. }
  659. }
  660. bool IsMapExported (XmlTypeMapping map)
  661. {
  662. if (exportedMaps.ContainsKey (GetMapKey(map))) return true;
  663. return false;
  664. }
  665. void SetMapExported (XmlTypeMapping map)
  666. {
  667. exportedMaps [GetMapKey(map)] = map;
  668. }
  669. bool IsElementExported (XmlTypeMapping map)
  670. {
  671. if (exportedElements.ContainsKey (GetMapKey(map))) return true;
  672. if (map.TypeData.Type == typeof(object)) return true;
  673. return false;
  674. }
  675. void SetElementExported (XmlTypeMapping map)
  676. {
  677. exportedElements [GetMapKey(map)] = map;
  678. }
  679. string GetMapKey (XmlTypeMapping map)
  680. {
  681. // Don't use type name for array types, since we can have different
  682. // classes that represent the same array type (for example
  683. // StringCollection and string[]).
  684. if (map.TypeData.IsListType)
  685. return GetArrayKeyName (map.TypeData) + " " + map.XmlType + " " + map.XmlTypeNamespace;
  686. else
  687. return map.TypeData.FullTypeName + " " + map.XmlType + " " + map.XmlTypeNamespace;
  688. }
  689. string GetArrayKeyName (TypeData td)
  690. {
  691. TypeData etd = td.ListItemTypeData;
  692. return "*arrayof*" + (etd.IsListType ? GetArrayKeyName (etd) : etd.FullTypeName);
  693. }
  694. void CompileSchemas ()
  695. {
  696. // foreach (XmlSchema sc in schemas)
  697. // sc.Compile (null);
  698. }
  699. XmlSchema GetSchema (string ns)
  700. {
  701. XmlSchema schema = schemas [ns];
  702. if (schema == null)
  703. {
  704. schema = new XmlSchema ();
  705. if (ns != null && ns.Length > 0)
  706. schema.TargetNamespace = ns;
  707. if (!encodedFormat)
  708. schema.ElementFormDefault = XmlSchemaForm.Qualified;
  709. schemas.Add (schema);
  710. }
  711. return schema;
  712. }
  713. #endregion // Methods
  714. private class XmlSchemaObjectContainer
  715. {
  716. private readonly XmlSchemaObject _xmlSchemaObject;
  717. public XmlSchemaObjectContainer (XmlSchema schema)
  718. {
  719. _xmlSchemaObject = schema;
  720. }
  721. public XmlSchemaObjectContainer (XmlSchemaGroupBase group)
  722. {
  723. _xmlSchemaObject = group;
  724. }
  725. public XmlSchemaObjectCollection Items {
  726. get {
  727. if (_xmlSchemaObject is XmlSchema) {
  728. return ((XmlSchema) _xmlSchemaObject).Items;
  729. } else {
  730. return ((XmlSchemaGroupBase) _xmlSchemaObject).Items;
  731. }
  732. }
  733. }
  734. }
  735. }
  736. }