XmlSchemaImporter.cs 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  1. //
  2. // System.Xml.Serialization.XmlSchemaImporter
  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 XmlSchemaImporter {
  15. #region Fields
  16. XmlSchemas schemas;
  17. CodeIdentifiers typeIdentifiers;
  18. CodeIdentifiers elemIdentifiers = new CodeIdentifiers ();
  19. Hashtable mappedTypes = new Hashtable ();
  20. Hashtable dataMappedTypes = new Hashtable ();
  21. Queue pendingMaps = new Queue ();
  22. Hashtable sharedAnonymousTypes = new Hashtable ();
  23. bool encodedFormat = false;
  24. XmlReflectionImporter auxXmlRefImporter;
  25. SoapReflectionImporter auxSoapRefImporter;
  26. static readonly XmlQualifiedName anyType = new XmlQualifiedName ("anyType",XmlSchema.Namespace);
  27. static readonly XmlQualifiedName arrayType = new XmlQualifiedName ("Array",XmlSerializer.EncodingNamespace);
  28. static readonly XmlQualifiedName arrayTypeRefName = new XmlQualifiedName ("arrayType",XmlSerializer.EncodingNamespace);
  29. const string XmlNamespace = "http://www.w3.org/XML/1998/namespace";
  30. XmlSchemaElement anyElement = null;
  31. class MapFixup
  32. {
  33. public XmlTypeMapping Map;
  34. public XmlSchemaComplexType SchemaType;
  35. public XmlQualifiedName TypeName;
  36. }
  37. #endregion
  38. #region Constructors
  39. public XmlSchemaImporter (XmlSchemas schemas)
  40. {
  41. this.schemas = schemas;
  42. typeIdentifiers = new CodeIdentifiers ();
  43. }
  44. public XmlSchemaImporter (XmlSchemas schemas, CodeIdentifiers typeIdentifiers)
  45. : this (schemas)
  46. {
  47. this.typeIdentifiers = typeIdentifiers;
  48. }
  49. internal bool UseEncodedFormat
  50. {
  51. get { return encodedFormat; }
  52. set { encodedFormat = value; }
  53. }
  54. #endregion // Constructors
  55. #region Methods
  56. public XmlMembersMapping ImportAnyType (XmlQualifiedName typeName, string elementName)
  57. {
  58. if (typeName == XmlQualifiedName.Empty)
  59. {
  60. XmlTypeMapMemberAnyElement mapMem = new XmlTypeMapMemberAnyElement ();
  61. mapMem.Name = typeName.Name;
  62. mapMem.TypeData = TypeTranslator.GetTypeData(typeof(XmlNode));
  63. mapMem.ElementInfo.Add (CreateElementInfo (typeName.Namespace, mapMem, typeName.Name, mapMem.TypeData, true, XmlSchemaForm.None));
  64. XmlMemberMapping[] mm = new XmlMemberMapping [1];
  65. mm[0] = new XmlMemberMapping (typeName.Name, typeName.Namespace, mapMem, encodedFormat);
  66. return new XmlMembersMapping (mm);
  67. }
  68. else
  69. {
  70. XmlSchemaComplexType stype = (XmlSchemaComplexType) schemas.Find (typeName, typeof (XmlSchemaComplexType));
  71. if (stype == null)
  72. throw new InvalidOperationException ("Referenced type '" + typeName + "' not found");
  73. if (!CanBeAnyElement (stype))
  74. throw new InvalidOperationException ("The type '" + typeName + "' is not valid for a collection of any elements");
  75. ClassMap cmap = new ClassMap ();
  76. CodeIdentifiers classIds = new CodeIdentifiers ();
  77. bool isMixed = stype.IsMixed;
  78. ImportSequenceContent (typeName, cmap, ((XmlSchemaSequence) stype.Particle).Items, classIds, false, ref isMixed);
  79. XmlTypeMapMemberAnyElement mapMem = (XmlTypeMapMemberAnyElement) cmap.AllMembers[0];
  80. mapMem.Name = typeName.Name;
  81. XmlMemberMapping[] mm = new XmlMemberMapping [1];
  82. mm[0] = new XmlMemberMapping (typeName.Name, typeName.Namespace, mapMem, encodedFormat);
  83. return new XmlMembersMapping (mm);
  84. }
  85. }
  86. public XmlTypeMapping ImportDerivedTypeMapping (XmlQualifiedName name, Type baseType)
  87. {
  88. return ImportDerivedTypeMapping (name, baseType, true);
  89. }
  90. public XmlTypeMapping ImportDerivedTypeMapping (XmlQualifiedName name, Type baseType, bool baseTypeCanBeIndirect)
  91. {
  92. XmlQualifiedName qname;
  93. XmlSchemaType stype;
  94. if (encodedFormat)
  95. {
  96. qname = name;
  97. stype = schemas.Find (name, typeof (XmlSchemaComplexType)) as XmlSchemaComplexType;
  98. if (stype == null) throw new InvalidOperationException ("Schema type '" + name + "' not found or not valid");
  99. }
  100. else
  101. {
  102. if (!LocateElement (name, out qname, out stype)) return null;
  103. }
  104. XmlTypeMapping map = GetRegisteredTypeMapping (qname);
  105. if (map != null)
  106. {
  107. // If the type has already been imported, make sure that the map
  108. // has the requested base type
  109. SetMapBaseType (map, baseType);
  110. map.SetRoot (name);
  111. return map;
  112. }
  113. map = CreateTypeMapping (qname, SchemaTypes.Class, name);
  114. map.Documentation = GetDocumentation (stype);
  115. RegisterMapFixup (map, qname, (XmlSchemaComplexType)stype);
  116. BuildPendingMaps ();
  117. SetMapBaseType (map, baseType);
  118. return map;
  119. }
  120. void SetMapBaseType (XmlTypeMapping map, Type baseType)
  121. {
  122. // This method sets the base type for a given map.
  123. // If the map already inherits from this type, it does nothing.
  124. // Fiirst of all, check if the map already inherits from baseType
  125. XmlTypeMapping topMap = null;
  126. while (map != null)
  127. {
  128. if (map.TypeData.Type == baseType)
  129. return;
  130. topMap = map;
  131. map = map.BaseMap;
  132. }
  133. // Does not have the requested base type.
  134. // Then, get/create a map for that base type.
  135. XmlTypeMapping baseMap;
  136. if (!encodedFormat)
  137. {
  138. if (auxXmlRefImporter == null) auxXmlRefImporter = new XmlReflectionImporter ();
  139. baseMap = auxXmlRefImporter.ImportTypeMapping (baseType);
  140. }
  141. else
  142. {
  143. if (auxSoapRefImporter == null) auxSoapRefImporter = new SoapReflectionImporter ();
  144. baseMap = auxSoapRefImporter.ImportTypeMapping (baseType);
  145. }
  146. // Add this map as a derived map of the base map
  147. topMap.BaseMap = baseMap;
  148. baseMap.DerivedTypes.Add (topMap);
  149. baseMap.DerivedTypes.AddRange (topMap.DerivedTypes);
  150. // Now add the base type fields to all derived maps
  151. ClassMap baseClassMap = (ClassMap)baseMap.ObjectMap;
  152. ClassMap cmap = (ClassMap)topMap.ObjectMap;
  153. foreach (XmlTypeMapMember member in baseClassMap.AllMembers)
  154. cmap.AddMember (member);
  155. foreach (XmlTypeMapping derivedMap in topMap.DerivedTypes)
  156. {
  157. cmap = (ClassMap)derivedMap.ObjectMap;
  158. foreach (XmlTypeMapMember member in baseClassMap.AllMembers)
  159. cmap.AddMember (member);
  160. }
  161. }
  162. public XmlMembersMapping ImportMembersMapping (XmlQualifiedName name)
  163. {
  164. XmlSchemaElement elem = (XmlSchemaElement) schemas.Find (name, typeof (XmlSchemaElement));
  165. if (elem == null) throw new InvalidOperationException ("Schema element '" + name + "' not found or not valid");
  166. XmlSchemaComplexType stype;
  167. if (elem.SchemaType != null)
  168. {
  169. stype = elem.SchemaType as XmlSchemaComplexType;
  170. }
  171. else
  172. {
  173. if (elem.SchemaTypeName.IsEmpty) return null;
  174. if (elem.SchemaTypeName.Namespace == XmlSchema.Namespace) return null;
  175. object type = schemas.Find (elem.SchemaTypeName, typeof (XmlSchemaComplexType));
  176. if (type == null) throw new InvalidOperationException ("Schema type '" + elem.SchemaTypeName + "' not found");
  177. stype = type as XmlSchemaComplexType;
  178. }
  179. if (stype == null)
  180. throw new InvalidOperationException ("Schema element '" + name + "' not found or not valid");
  181. XmlMemberMapping[] mapping = ImportMembersMappingComposite (stype, name);
  182. return new XmlMembersMapping (name.Name, name.Namespace, mapping);
  183. }
  184. public XmlMembersMapping ImportMembersMapping (XmlQualifiedName[] names)
  185. {
  186. XmlMemberMapping[] mapping = new XmlMemberMapping [names.Length];
  187. for (int n=0; n<names.Length; n++)
  188. {
  189. XmlSchemaElement elem = (XmlSchemaElement) schemas.Find (names[n], typeof (XmlSchemaElement));
  190. if (elem == null) throw new InvalidOperationException ("Schema element '" + names[n] + "' not found");
  191. XmlQualifiedName typeQName = new XmlQualifiedName ("Message", names[n].Namespace);
  192. XmlTypeMapping tmap;
  193. TypeData td = GetElementTypeData (typeQName, elem, out tmap);
  194. mapping[n] = ImportMemberMapping (elem.Name, typeQName.Namespace, elem.IsNillable, td, tmap);
  195. }
  196. BuildPendingMaps ();
  197. return new XmlMembersMapping (mapping);
  198. }
  199. internal XmlMembersMapping ImportEncodedMembersMapping (string name, string ns, SoapSchemaMember[] members, bool hasWrapperElement)
  200. {
  201. XmlMemberMapping[] mapping = new XmlMemberMapping [members.Length];
  202. for (int n=0; n<members.Length; n++)
  203. {
  204. TypeData td = GetTypeData (members[n].MemberType, null);
  205. XmlTypeMapping tmap = GetTypeMapping (td);
  206. mapping[n] = ImportMemberMapping (members[n].MemberName, members[n].MemberType.Namespace, true, td, tmap);
  207. }
  208. BuildPendingMaps ();
  209. return new XmlMembersMapping (name, ns, hasWrapperElement, false, mapping);
  210. }
  211. internal XmlMembersMapping ImportEncodedMembersMapping (string name, string ns, SoapSchemaMember member)
  212. {
  213. XmlSchemaComplexType stype = schemas.Find (member.MemberType, typeof (XmlSchemaComplexType)) as XmlSchemaComplexType;
  214. if (stype == null) throw new InvalidOperationException ("Schema type '" + member.MemberType + "' not found or not valid");
  215. XmlMemberMapping[] mapping = ImportMembersMappingComposite (stype, member.MemberType);
  216. return new XmlMembersMapping (name, ns, mapping);
  217. }
  218. XmlMemberMapping[] ImportMembersMappingComposite (XmlSchemaComplexType stype, XmlQualifiedName refer)
  219. {
  220. if (stype.Particle == null)
  221. return new XmlMemberMapping [0];
  222. ClassMap cmap = new ClassMap ();
  223. XmlSchemaSequence seq = stype.Particle as XmlSchemaSequence;
  224. if (seq == null) throw new InvalidOperationException ("Schema element '" + refer + "' cannot be imported as XmlMembersMapping");
  225. CodeIdentifiers classIds = new CodeIdentifiers ();
  226. ImportParticleComplexContent (refer, cmap, seq, classIds, false);
  227. ImportAttributes (refer, cmap, stype.Attributes, stype.AnyAttribute, classIds);
  228. BuildPendingMaps ();
  229. int n = 0;
  230. XmlMemberMapping[] mapping = new XmlMemberMapping [cmap.AllMembers.Count];
  231. foreach (XmlTypeMapMember mapMem in cmap.AllMembers)
  232. mapping[n++] = new XmlMemberMapping (mapMem.Name, refer.Namespace, mapMem, encodedFormat);
  233. return mapping;
  234. }
  235. XmlMemberMapping ImportMemberMapping (string name, string ns, bool isNullable, TypeData type, XmlTypeMapping emap)
  236. {
  237. XmlTypeMapMemberElement mapMem;
  238. if (type.IsListType)
  239. mapMem = new XmlTypeMapMemberList ();
  240. else
  241. mapMem = new XmlTypeMapMemberElement ();
  242. mapMem.Name = name;
  243. mapMem.TypeData = type;
  244. mapMem.ElementInfo.Add (CreateElementInfo (ns, mapMem, name, type, isNullable, XmlSchemaForm.None, emap));
  245. return new XmlMemberMapping (name, ns, mapMem, encodedFormat);
  246. }
  247. [MonoTODO]
  248. public XmlMembersMapping ImportMembersMapping (XmlQualifiedName[] names, Type baseType, bool baseTypeCanBeIndirect)
  249. {
  250. throw new NotImplementedException ();
  251. }
  252. public XmlTypeMapping ImportTypeMapping (XmlQualifiedName name)
  253. {
  254. XmlQualifiedName qname;
  255. XmlSchemaType stype;
  256. if (!LocateElement (name, out qname, out stype)) return null;
  257. XmlTypeMapping map = GetRegisteredTypeMapping (qname);
  258. if (map != null) return map;
  259. map = CreateTypeMapping (qname, SchemaTypes.Class, name);
  260. map.Documentation = GetDocumentation (stype);
  261. RegisterMapFixup (map, qname, (XmlSchemaComplexType)stype);
  262. BuildPendingMaps ();
  263. return map;
  264. }
  265. bool LocateElement (XmlQualifiedName name, out XmlQualifiedName qname, out XmlSchemaType stype)
  266. {
  267. qname = null;
  268. stype = null;
  269. XmlSchemaElement elem = (XmlSchemaElement) schemas.Find (name, typeof (XmlSchemaElement));
  270. if (elem == null) return false;
  271. // The root element must be an element with complex type
  272. if (elem.SchemaType != null)
  273. {
  274. stype = elem.SchemaType;
  275. qname = name;
  276. }
  277. else
  278. {
  279. if (elem.SchemaTypeName.IsEmpty) return false;
  280. if (elem.SchemaTypeName.Namespace == XmlSchema.Namespace) return false;
  281. object type = schemas.Find (elem.SchemaTypeName, typeof (XmlSchemaComplexType));
  282. if (type == null) type = schemas.Find (elem.SchemaTypeName, typeof (XmlSchemaSimpleType));
  283. if (type == null) throw new InvalidOperationException ("Schema type '" + elem.SchemaTypeName + "' not found");
  284. stype = (XmlSchemaType) type;
  285. qname = stype.QualifiedName;
  286. XmlSchemaType btype = stype.BaseSchemaType as XmlSchemaType;
  287. if (btype != null && btype.QualifiedName == elem.SchemaTypeName)
  288. throw new InvalidOperationException ("Cannot import schema for type '" + elem.SchemaTypeName.Name + "' from namespace '" + elem.SchemaTypeName.Namespace + "'. Redefine not supported");
  289. }
  290. if (stype is XmlSchemaSimpleType) return false;
  291. return true;
  292. }
  293. XmlTypeMapping ImportType (XmlQualifiedName name, XmlQualifiedName root)
  294. {
  295. XmlTypeMapping map = GetRegisteredTypeMapping (name);
  296. if (map != null) return map;
  297. XmlSchemaType type = (XmlSchemaType) schemas.Find (name, typeof (XmlSchemaComplexType));
  298. if (type == null) type = (XmlSchemaType) schemas.Find (name, typeof (XmlSchemaSimpleType));
  299. if (type == null)
  300. {
  301. if (name.Namespace == XmlSerializer.EncodingNamespace)
  302. throw new InvalidOperationException ("Referenced type '" + name + "' valid only for encoded SOAP");
  303. else
  304. throw new InvalidOperationException ("Referenced type '" + name + "' not found");
  305. }
  306. return ImportType (name, type, root);
  307. }
  308. XmlTypeMapping ImportType (XmlQualifiedName name, XmlSchemaType stype, XmlQualifiedName root)
  309. {
  310. XmlTypeMapping map = GetRegisteredTypeMapping (name);
  311. if (map != null) return map;
  312. if (stype is XmlSchemaComplexType)
  313. return ImportClassComplexType (name, (XmlSchemaComplexType) stype, root);
  314. else if (stype is XmlSchemaSimpleType)
  315. return ImportClassSimpleType (name, (XmlSchemaSimpleType) stype, root);
  316. throw new NotSupportedException ("Schema type not supported: " + stype.GetType ());
  317. }
  318. XmlTypeMapping ImportClassComplexType (XmlQualifiedName typeQName, XmlSchemaComplexType stype, XmlQualifiedName root)
  319. {
  320. XmlTypeMapping map;
  321. // The need for fixups: If the complex type is an array, then to get the type of the
  322. // array we need first to get the type of the items of the array.
  323. // But if one of the item types or its children has a referece to this type array,
  324. // then we enter in an infinite loop. This does not happen with class types because
  325. // the class map is registered before parsing the children. We can't do the same
  326. // with the array type because to register the array map we need the type of the array.
  327. if (CanBeArray (typeQName, stype))
  328. {
  329. TypeData typeData;
  330. ListMap listMap = BuildArrayMap (typeQName, stype, out typeData);
  331. if (listMap != null)
  332. {
  333. map = CreateArrayTypeMapping (typeQName, typeData);
  334. map.ObjectMap = listMap;
  335. return map;
  336. }
  337. // After all, it is not an array. Create a class map then.
  338. }
  339. else if (CanBeAnyElement (stype))
  340. {
  341. if (stype.IsMixed)
  342. return GetTypeMapping (TypeTranslator.GetTypeData(typeof(XmlNode)));
  343. else
  344. return GetTypeMapping (TypeTranslator.GetTypeData(typeof(XmlElement)));
  345. }
  346. else if (CanBeIXmlSerializable (stype))
  347. {
  348. return ImportXmlSerializableMapping (typeQName.Namespace);
  349. }
  350. // Register the map right now but do not build it,
  351. // This will avoid loops.
  352. map = CreateTypeMapping (typeQName, SchemaTypes.Class, root);
  353. map.Documentation = GetDocumentation (stype);
  354. RegisterMapFixup (map, typeQName, stype);
  355. return map;
  356. }
  357. void RegisterMapFixup (XmlTypeMapping map, XmlQualifiedName typeQName, XmlSchemaComplexType stype)
  358. {
  359. MapFixup fixup = new MapFixup ();
  360. fixup.Map = map;
  361. fixup.SchemaType = stype;
  362. fixup.TypeName = typeQName;
  363. pendingMaps.Enqueue (fixup);
  364. }
  365. void BuildPendingMaps ()
  366. {
  367. while (pendingMaps.Count > 0) {
  368. MapFixup fixup = (MapFixup) pendingMaps.Dequeue ();
  369. if (fixup.Map.ObjectMap == null) {
  370. BuildClassMap (fixup.Map, fixup.TypeName, fixup.SchemaType);
  371. if (fixup.Map.ObjectMap == null) pendingMaps.Enqueue (fixup);
  372. }
  373. }
  374. }
  375. void BuildPendingMap (XmlTypeMapping map)
  376. {
  377. if (map.ObjectMap != null) return;
  378. foreach (MapFixup fixup in pendingMaps)
  379. {
  380. if (fixup.Map == map) {
  381. BuildClassMap (fixup.Map, fixup.TypeName, fixup.SchemaType);
  382. return;
  383. }
  384. }
  385. throw new InvalidOperationException ("Can't complete map of type " + map.XmlType + " : " + map.Namespace);
  386. }
  387. void BuildClassMap (XmlTypeMapping map, XmlQualifiedName typeQName, XmlSchemaComplexType stype)
  388. {
  389. CodeIdentifiers classIds = new CodeIdentifiers();
  390. classIds.AddReserved (map.TypeData.TypeName);
  391. ClassMap cmap = new ClassMap ();
  392. map.ObjectMap = cmap;
  393. bool isMixed = stype.IsMixed;
  394. if (stype.Particle != null)
  395. ImportParticleComplexContent (typeQName, cmap, stype.Particle, classIds, isMixed);
  396. else
  397. {
  398. if (stype.ContentModel is XmlSchemaSimpleContent) {
  399. ImportSimpleContent (typeQName, map, (XmlSchemaSimpleContent)stype.ContentModel, classIds, isMixed);
  400. }
  401. else if (stype.ContentModel is XmlSchemaComplexContent) {
  402. ImportComplexContent (typeQName, map, (XmlSchemaComplexContent)stype.ContentModel, classIds, isMixed);
  403. }
  404. }
  405. ImportAttributes (typeQName, cmap, stype.Attributes, stype.AnyAttribute, classIds);
  406. ImportExtensionTypes (typeQName);
  407. GetTypeMapping (TypeTranslator.GetTypeData (typeof(object))).DerivedTypes.Add (map);
  408. }
  409. void ImportAttributes (XmlQualifiedName typeQName, ClassMap cmap, XmlSchemaObjectCollection atts, XmlSchemaAnyAttribute anyat, CodeIdentifiers classIds)
  410. {
  411. if (anyat != null)
  412. {
  413. XmlTypeMapMemberAnyAttribute member = new XmlTypeMapMemberAnyAttribute ();
  414. member.Name = classIds.AddUnique ("AnyAttribute", member);
  415. member.TypeData = TypeTranslator.GetTypeData (typeof(XmlAttribute[]));
  416. cmap.AddMember (member);
  417. }
  418. foreach (XmlSchemaObject at in atts)
  419. {
  420. if (at is XmlSchemaAttribute)
  421. {
  422. string ns;
  423. XmlSchemaAttribute attr = (XmlSchemaAttribute)at;
  424. XmlSchemaAttribute refAttr = GetRefAttribute (typeQName, attr, out ns);
  425. XmlTypeMapMemberAttribute member = new XmlTypeMapMemberAttribute ();
  426. member.Name = classIds.AddUnique (CodeIdentifier.MakeValid (refAttr.Name), member);
  427. member.Documentation = GetDocumentation (attr);
  428. member.AttributeName = refAttr.Name;
  429. member.Namespace = ns;
  430. member.Form = refAttr.Form;
  431. member.TypeData = GetAttributeTypeData (typeQName, attr);
  432. if (refAttr.DefaultValue != null)
  433. member.DefaultValue = XmlCustomFormatter.FromXmlString (member.TypeData, refAttr.DefaultValue);
  434. else if (member.TypeData.IsValueType)
  435. member.IsOptionalValueType = (refAttr.ValidatedUse != XmlSchemaUse.Required);
  436. if (member.TypeData.IsComplexType)
  437. member.MappedType = GetTypeMapping (member.TypeData);
  438. cmap.AddMember (member);
  439. }
  440. else if (at is XmlSchemaAttributeGroupRef)
  441. {
  442. XmlSchemaAttributeGroupRef gref = (XmlSchemaAttributeGroupRef)at;
  443. XmlSchemaAttributeGroup grp = FindRefAttributeGroup (gref.RefName);
  444. ImportAttributes (typeQName, cmap, grp.Attributes, grp.AnyAttribute, classIds);
  445. }
  446. }
  447. }
  448. ListMap BuildArrayMap (XmlQualifiedName typeQName, XmlSchemaComplexType stype, out TypeData arrayTypeData)
  449. {
  450. if (encodedFormat)
  451. {
  452. XmlSchemaComplexContent content = stype.ContentModel as XmlSchemaComplexContent;
  453. XmlSchemaComplexContentRestriction rest = content.Content as XmlSchemaComplexContentRestriction;
  454. XmlSchemaAttribute arrayTypeAt = FindArrayAttribute (rest.Attributes);
  455. if (arrayTypeAt != null)
  456. {
  457. XmlAttribute[] uatts = arrayTypeAt.UnhandledAttributes;
  458. if (uatts == null || uatts.Length == 0) throw new InvalidOperationException ("arrayType attribute not specified in array declaration: " + typeQName);
  459. XmlAttribute xat = null;
  460. foreach (XmlAttribute at in uatts)
  461. if (at.LocalName == "arrayType" && at.NamespaceURI == XmlSerializer.WsdlNamespace)
  462. { xat = at; break; }
  463. if (xat == null)
  464. throw new InvalidOperationException ("arrayType attribute not specified in array declaration: " + typeQName);
  465. string name, ns, dims;
  466. TypeTranslator.ParseArrayType (xat.Value, out name, out ns, out dims);
  467. return BuildEncodedArrayMap (name + dims, ns, out arrayTypeData);
  468. }
  469. else
  470. {
  471. XmlSchemaElement elem = null;
  472. XmlSchemaSequence seq = rest.Particle as XmlSchemaSequence;
  473. if (seq != null && seq.Items.Count == 1)
  474. elem = seq.Items[0] as XmlSchemaElement;
  475. else {
  476. XmlSchemaAll all = rest.Particle as XmlSchemaAll;
  477. if (all != null && all.Items.Count == 1)
  478. elem = all.Items[0] as XmlSchemaElement;
  479. }
  480. if (elem == null)
  481. throw new InvalidOperationException ("Unknown array format");
  482. return BuildEncodedArrayMap (elem.SchemaTypeName.Name + "[]", elem.SchemaTypeName.Namespace, out arrayTypeData);
  483. }
  484. }
  485. else
  486. {
  487. ClassMap cmap = new ClassMap ();
  488. CodeIdentifiers classIds = new CodeIdentifiers();
  489. ImportParticleComplexContent (typeQName, cmap, stype.Particle, classIds, stype.IsMixed);
  490. XmlTypeMapMemberFlatList list = (cmap.AllMembers.Count == 1) ? cmap.AllMembers[0] as XmlTypeMapMemberFlatList : null;
  491. if (list != null && list.ChoiceMember == null)
  492. {
  493. arrayTypeData = list.TypeData;
  494. return list.ListMap;
  495. }
  496. else
  497. {
  498. arrayTypeData = null;
  499. return null;
  500. }
  501. }
  502. }
  503. ListMap BuildEncodedArrayMap (string type, string ns, out TypeData arrayTypeData)
  504. {
  505. ListMap map = new ListMap ();
  506. int i = type.LastIndexOf ("[");
  507. if (i == -1) throw new InvalidOperationException ("Invalid arrayType value: " + type);
  508. if (type.IndexOf (",",i) != -1) throw new InvalidOperationException ("Multidimensional arrays are not supported");
  509. string itemType = type.Substring (0,i);
  510. TypeData itemTypeData;
  511. if (itemType.IndexOf ("[") != -1)
  512. {
  513. ListMap innerListMap = BuildEncodedArrayMap (itemType, ns, out itemTypeData);
  514. int dims = itemType.Split ('[').Length - 1;
  515. string name = TypeTranslator.GetArrayName (type, dims);
  516. XmlQualifiedName qname = new XmlQualifiedName (name, ns);
  517. XmlTypeMapping tmap = CreateArrayTypeMapping (qname, itemTypeData);
  518. tmap.ObjectMap = innerListMap;
  519. }
  520. else
  521. {
  522. itemTypeData = GetTypeData (new XmlQualifiedName (itemType, ns), null);
  523. }
  524. arrayTypeData = itemTypeData.ListTypeData;
  525. map.ItemInfo = new XmlTypeMapElementInfoList();
  526. map.ItemInfo.Add (CreateElementInfo ("", null, "Item", itemTypeData, true, XmlSchemaForm.None));
  527. return map;
  528. }
  529. XmlSchemaAttribute FindArrayAttribute (XmlSchemaObjectCollection atts)
  530. {
  531. foreach (object ob in atts)
  532. {
  533. XmlSchemaAttribute att = ob as XmlSchemaAttribute;
  534. if (att != null && att.RefName == arrayTypeRefName) return att;
  535. XmlSchemaAttributeGroupRef gref = ob as XmlSchemaAttributeGroupRef;
  536. if (gref != null)
  537. {
  538. XmlSchemaAttributeGroup grp = FindRefAttributeGroup (gref.RefName);
  539. att = FindArrayAttribute (grp.Attributes);
  540. if (att != null) return att;
  541. }
  542. }
  543. return null;
  544. }
  545. void ImportParticleComplexContent (XmlQualifiedName typeQName, ClassMap cmap, XmlSchemaParticle particle, CodeIdentifiers classIds, bool isMixed)
  546. {
  547. ImportParticleContent (typeQName, cmap, particle, classIds, false, ref isMixed);
  548. if (isMixed && cmap.XmlTextCollector == null)
  549. {
  550. XmlTypeMapMemberFlatList member = new XmlTypeMapMemberFlatList ();
  551. member.Name = classIds.AddUnique ("Text", member);
  552. member.TypeData = TypeTranslator.GetTypeData (typeof(string[]));
  553. member.ElementInfo.Add (CreateTextElementInfo (typeQName.Namespace, member, member.TypeData.ListItemTypeData));
  554. member.IsXmlTextCollector = true;
  555. member.ListMap = new ListMap ();
  556. member.ListMap.ItemInfo = member.ElementInfo;
  557. cmap.AddMember (member);
  558. }
  559. }
  560. void ImportParticleContent (XmlQualifiedName typeQName, ClassMap cmap, XmlSchemaParticle particle, CodeIdentifiers classIds, bool multiValue, ref bool isMixed)
  561. {
  562. if (particle is XmlSchemaGroupRef)
  563. particle = GetRefGroupParticle ((XmlSchemaGroupRef)particle);
  564. if (particle.MaxOccurs > 1) multiValue = true;
  565. if (particle is XmlSchemaSequence) {
  566. ImportSequenceContent (typeQName, cmap, ((XmlSchemaSequence)particle).Items, classIds, multiValue, ref isMixed);
  567. }
  568. else if (particle is XmlSchemaChoice) {
  569. if (((XmlSchemaChoice)particle).Items.Count == 1)
  570. ImportSequenceContent (typeQName, cmap, ((XmlSchemaChoice)particle).Items, classIds, multiValue, ref isMixed);
  571. else
  572. ImportChoiceContent (typeQName, cmap, (XmlSchemaChoice)particle, classIds, multiValue);
  573. }
  574. else if (particle is XmlSchemaAll) {
  575. ImportSequenceContent (typeQName, cmap, ((XmlSchemaAll)particle).Items, classIds, multiValue, ref isMixed);
  576. }
  577. }
  578. void ImportSequenceContent (XmlQualifiedName typeQName, ClassMap cmap, XmlSchemaObjectCollection items, CodeIdentifiers classIds, bool multiValue, ref bool isMixed)
  579. {
  580. foreach (XmlSchemaObject item in items)
  581. {
  582. if (item is XmlSchemaElement)
  583. {
  584. string ns;
  585. XmlSchemaElement elem = (XmlSchemaElement) item;
  586. XmlTypeMapping emap;
  587. TypeData typeData = GetElementTypeData (typeQName, elem, out emap);
  588. XmlSchemaElement refElem = GetRefElement (typeQName, elem, out ns);
  589. if (elem.MaxOccurs == 1 && !multiValue)
  590. {
  591. XmlTypeMapMemberElement member = null;
  592. if (typeData.SchemaType != SchemaTypes.Array)
  593. {
  594. member = new XmlTypeMapMemberElement ();
  595. if (refElem.DefaultValue != null) member.DefaultValue = XmlCustomFormatter.FromXmlString (typeData, refElem.DefaultValue);
  596. }
  597. else if (GetTypeMapping (typeData).IsSimpleType)
  598. {
  599. // It is a simple list (space separated list).
  600. // Since this is not supported, map as a single item value
  601. // TODO: improve this
  602. member = new XmlTypeMapMemberElement ();
  603. typeData = typeData.ListItemTypeData;
  604. }
  605. else
  606. member = new XmlTypeMapMemberList ();
  607. if (elem.MinOccurs == 0 && typeData.IsValueType)
  608. member.IsOptionalValueType = true;
  609. member.Name = classIds.AddUnique(CodeIdentifier.MakeValid(refElem.Name), member);
  610. member.Documentation = GetDocumentation (elem);
  611. member.TypeData = typeData;
  612. member.ElementInfo.Add (CreateElementInfo (ns, member, refElem.Name, typeData, refElem.IsNillable, refElem.Form, emap));
  613. cmap.AddMember (member);
  614. }
  615. else
  616. {
  617. XmlTypeMapMemberFlatList member = new XmlTypeMapMemberFlatList ();
  618. member.ListMap = new ListMap ();
  619. member.Name = classIds.AddUnique(CodeIdentifier.MakeValid(refElem.Name), member);
  620. member.Documentation = GetDocumentation (elem);
  621. member.TypeData = typeData.ListTypeData;
  622. member.ElementInfo.Add (CreateElementInfo (ns, member, refElem.Name, typeData, refElem.IsNillable, refElem.Form, emap));
  623. member.ListMap.ItemInfo = member.ElementInfo;
  624. cmap.AddMember (member);
  625. }
  626. }
  627. else if (item is XmlSchemaAny)
  628. {
  629. XmlSchemaAny elem = (XmlSchemaAny) item;
  630. XmlTypeMapMemberAnyElement member = new XmlTypeMapMemberAnyElement ();
  631. member.Name = classIds.AddUnique ("Any", member);
  632. member.Documentation = GetDocumentation (elem);
  633. Type ctype;
  634. if (elem.MaxOccurs != 1 || multiValue)
  635. ctype = isMixed ? typeof(XmlNode[]) : typeof(XmlElement[]);
  636. else
  637. ctype = isMixed ? typeof(XmlNode) : typeof(XmlElement);
  638. member.TypeData = TypeTranslator.GetTypeData (ctype);
  639. XmlTypeMapElementInfo einfo = new XmlTypeMapElementInfo (member, member.TypeData);
  640. einfo.IsUnnamedAnyElement = true;
  641. member.ElementInfo.Add (einfo);
  642. if (isMixed)
  643. {
  644. einfo = CreateTextElementInfo (typeQName.Namespace, member, member.TypeData);
  645. member.ElementInfo.Add (einfo);
  646. member.IsXmlTextCollector = true;
  647. isMixed = false; //Allow only one XmlTextAttribute
  648. }
  649. cmap.AddMember (member);
  650. }
  651. else if (item is XmlSchemaParticle) {
  652. ImportParticleContent (typeQName, cmap, (XmlSchemaParticle)item, classIds, multiValue, ref isMixed);
  653. }
  654. }
  655. }
  656. void ImportChoiceContent (XmlQualifiedName typeQName, ClassMap cmap, XmlSchemaChoice choice, CodeIdentifiers classIds, bool multiValue)
  657. {
  658. XmlTypeMapElementInfoList choices = new XmlTypeMapElementInfoList ();
  659. multiValue = ImportChoices (typeQName, null, choices, choice.Items) || multiValue;
  660. if (choices.Count == 0) return;
  661. if (choice.MaxOccurs > 1) multiValue = true;
  662. XmlTypeMapMemberElement member;
  663. if (multiValue)
  664. {
  665. member = new XmlTypeMapMemberFlatList ();
  666. member.Name = classIds.AddUnique ("Items", member);
  667. ListMap listMap = new ListMap ();
  668. listMap.ItemInfo = choices;
  669. ((XmlTypeMapMemberFlatList)member).ListMap = listMap;
  670. }
  671. else
  672. {
  673. member = new XmlTypeMapMemberElement ();
  674. member.Name = classIds.AddUnique ("Item", member);
  675. }
  676. // If all choices have the same type, use that type for the member.
  677. // If not use System.Object.
  678. // If there are at least two choices with the same type, use a choice
  679. // identifier attribute
  680. TypeData typeData = null;
  681. bool twoEqual = false;
  682. bool allEqual = true;
  683. Hashtable types = new Hashtable ();
  684. foreach (XmlTypeMapElementInfo einfo in choices)
  685. {
  686. if (types.ContainsKey (einfo.TypeData)) twoEqual = true;
  687. else types.Add (einfo.TypeData, einfo);
  688. TypeData choiceType = einfo.TypeData;
  689. if (choiceType.SchemaType == SchemaTypes.Class)
  690. {
  691. // When comparing class types, use the most generic class in the
  692. // inheritance hierarchy
  693. XmlTypeMapping choiceMap = GetTypeMapping (choiceType);
  694. BuildPendingMap (choiceMap);
  695. while (choiceMap.BaseMap != null) {
  696. choiceMap = choiceMap.BaseMap;
  697. BuildPendingMap (choiceMap);
  698. choiceType = choiceMap.TypeData;
  699. }
  700. }
  701. if (typeData == null) typeData = choiceType;
  702. else if (typeData != choiceType) allEqual = false;
  703. }
  704. if (!allEqual)
  705. typeData = TypeTranslator.GetTypeData (typeof(object));
  706. if (twoEqual)
  707. {
  708. // Create the choice member
  709. XmlTypeMapMemberElement choiceMember = new XmlTypeMapMemberElement ();
  710. choiceMember.Name = classIds.AddUnique (member.Name + "ElementName", choiceMember);
  711. member.ChoiceMember = choiceMember.Name;
  712. // Create the choice enum
  713. XmlTypeMapping enumMap = CreateTypeMapping (new XmlQualifiedName (member.Name + "ChoiceType", typeQName.Namespace), SchemaTypes.Enum, null);
  714. CodeIdentifiers codeIdents = new CodeIdentifiers ();
  715. EnumMap.EnumMapMember[] members = new EnumMap.EnumMapMember [choices.Count];
  716. for (int n=0; n<choices.Count; n++)
  717. {
  718. XmlTypeMapElementInfo it =(XmlTypeMapElementInfo) choices[n];
  719. string xmlName = (it.Namespace != null && it.Namespace != "") ? it.Namespace + ":" + it.ElementName : it.ElementName;
  720. string enumName = codeIdents.AddUnique (CodeIdentifier.MakeValid (it.ElementName), it);
  721. members [n] = new EnumMap.EnumMapMember (xmlName, enumName);
  722. }
  723. enumMap.ObjectMap = new EnumMap (members, false);
  724. choiceMember.TypeData = multiValue ? enumMap.TypeData.ListTypeData : enumMap.TypeData;
  725. choiceMember.ElementInfo.Add (CreateElementInfo (typeQName.Namespace, choiceMember, choiceMember.Name, choiceMember.TypeData, false, XmlSchemaForm.None));
  726. cmap.AddMember (choiceMember);
  727. }
  728. if (multiValue)
  729. typeData = typeData.ListTypeData;
  730. member.ElementInfo = choices;
  731. member.Documentation = GetDocumentation (choice);
  732. member.TypeData = typeData;
  733. cmap.AddMember (member);
  734. }
  735. bool ImportChoices (XmlQualifiedName typeQName, XmlTypeMapMember member, XmlTypeMapElementInfoList choices, XmlSchemaObjectCollection items)
  736. {
  737. bool multiValue = false;
  738. foreach (XmlSchemaObject titem in items)
  739. {
  740. XmlSchemaObject item = titem;
  741. if (item is XmlSchemaGroupRef)
  742. item = GetRefGroupParticle ((XmlSchemaGroupRef)item);
  743. if (item is XmlSchemaElement)
  744. {
  745. string ns;
  746. XmlSchemaElement elem = (XmlSchemaElement) item;
  747. XmlTypeMapping emap;
  748. TypeData typeData = GetElementTypeData (typeQName, elem, out emap);
  749. XmlSchemaElement refElem = GetRefElement (typeQName, elem, out ns);
  750. choices.Add (CreateElementInfo (ns, member, refElem.Name, typeData, refElem.IsNillable, refElem.Form, emap));
  751. if (elem.MaxOccurs > 1) multiValue = true;
  752. }
  753. else if (item is XmlSchemaAny)
  754. {
  755. XmlTypeMapElementInfo einfo = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(typeof(XmlElement)));
  756. einfo.IsUnnamedAnyElement = true;
  757. choices.Add (einfo);
  758. }
  759. else if (item is XmlSchemaChoice) {
  760. multiValue = ImportChoices (typeQName, member, choices, ((XmlSchemaChoice)item).Items) || multiValue;
  761. }
  762. else if (item is XmlSchemaSequence) {
  763. multiValue = ImportChoices (typeQName, member, choices, ((XmlSchemaSequence)item).Items) || multiValue;
  764. }
  765. }
  766. return multiValue;
  767. }
  768. void ImportSimpleContent (XmlQualifiedName typeQName, XmlTypeMapping map, XmlSchemaSimpleContent content, CodeIdentifiers classIds, bool isMixed)
  769. {
  770. ClassMap cmap = (ClassMap)map.ObjectMap;
  771. XmlQualifiedName qname = GetContentBaseType (content.Content);
  772. XmlTypeMapMemberElement member = new XmlTypeMapMemberElement ();
  773. member.Name = classIds.AddUnique("Value", member);
  774. member.TypeData = FindBuiltInType (qname);
  775. member.ElementInfo.Add (CreateTextElementInfo (typeQName.Namespace, member, member.TypeData));
  776. member.IsXmlTextCollector = true;
  777. cmap.AddMember (member);
  778. XmlSchemaSimpleContentExtension ext = content.Content as XmlSchemaSimpleContentExtension;
  779. if (ext != null)
  780. ImportAttributes (typeQName, cmap, ext.Attributes, ext.AnyAttribute, classIds);
  781. }
  782. TypeData FindBuiltInType (XmlQualifiedName qname)
  783. {
  784. if (qname.Namespace == XmlSchema.Namespace)
  785. return TypeTranslator.GetPrimitiveTypeData (qname.Name);
  786. XmlSchemaComplexType ct = (XmlSchemaComplexType) schemas.Find (qname, typeof(XmlSchemaComplexType));
  787. if (ct != null)
  788. {
  789. XmlSchemaSimpleContent sc = ct.ContentModel as XmlSchemaSimpleContent;
  790. if (sc == null) throw new InvalidOperationException ("Invalid schema");
  791. return FindBuiltInType (GetContentBaseType (sc.Content));
  792. }
  793. XmlSchemaSimpleType st = (XmlSchemaSimpleType) schemas.Find (qname, typeof(XmlSchemaSimpleType));
  794. if (st != null)
  795. return FindBuiltInType (qname, st);
  796. throw new InvalidOperationException ("Definition of type " + qname + " not found");
  797. }
  798. TypeData FindBuiltInType (XmlQualifiedName qname, XmlSchemaSimpleType st)
  799. {
  800. if (CanBeEnum (st))
  801. return ImportType (qname, null).TypeData;
  802. if (st.Content is XmlSchemaSimpleTypeRestriction) {
  803. return FindBuiltInType (GetContentBaseType (st.Content));
  804. }
  805. else if (st.Content is XmlSchemaSimpleTypeList) {
  806. return FindBuiltInType (GetContentBaseType (st.Content)).ListTypeData;
  807. }
  808. else if (st.Content is XmlSchemaSimpleTypeUnion)
  809. {
  810. // Check if all types of the union are equal. If not, then will use anyType.
  811. XmlSchemaSimpleTypeUnion uni = (XmlSchemaSimpleTypeUnion) st.Content;
  812. TypeData utype = null;
  813. // Anonymous types are unique
  814. if (uni.BaseTypes.Count != 0 && uni.MemberTypes.Length != 0)
  815. return FindBuiltInType (anyType);
  816. foreach (XmlQualifiedName mt in uni.MemberTypes)
  817. {
  818. TypeData qn = FindBuiltInType (mt);
  819. if (utype != null && qn != utype) return FindBuiltInType (anyType);
  820. else utype = qn;
  821. }
  822. return utype;
  823. }
  824. else
  825. return null;
  826. }
  827. XmlQualifiedName GetContentBaseType (XmlSchemaObject ob)
  828. {
  829. if (ob is XmlSchemaSimpleContentExtension)
  830. return ((XmlSchemaSimpleContentExtension)ob).BaseTypeName;
  831. else if (ob is XmlSchemaSimpleContentRestriction)
  832. return ((XmlSchemaSimpleContentRestriction)ob).BaseTypeName;
  833. else if (ob is XmlSchemaSimpleTypeRestriction)
  834. return ((XmlSchemaSimpleTypeRestriction)ob).BaseTypeName;
  835. else if (ob is XmlSchemaSimpleTypeList)
  836. return ((XmlSchemaSimpleTypeList)ob).ItemTypeName;
  837. else
  838. return null;
  839. }
  840. void ImportComplexContent (XmlQualifiedName typeQName, XmlTypeMapping map, XmlSchemaComplexContent content, CodeIdentifiers classIds, bool isMixed)
  841. {
  842. ClassMap cmap = (ClassMap)map.ObjectMap;
  843. XmlQualifiedName qname;
  844. XmlSchemaComplexContentExtension ext = content.Content as XmlSchemaComplexContentExtension;
  845. if (ext != null) qname = ext.BaseTypeName;
  846. else qname = ((XmlSchemaComplexContentRestriction)content.Content).BaseTypeName;
  847. if (qname == typeQName)
  848. throw new InvalidOperationException ("Cannot import schema for type '" + typeQName.Name + "' from namespace '" + typeQName.Namespace + "'. Redefine not supported");
  849. // Add base map members to this map
  850. XmlTypeMapping baseMap = ImportType (qname, null);
  851. BuildPendingMap (baseMap);
  852. ClassMap baseClassMap = (ClassMap)baseMap.ObjectMap;
  853. foreach (XmlTypeMapMember member in baseClassMap.AllMembers)
  854. cmap.AddMember (member);
  855. if (baseClassMap.XmlTextCollector != null) isMixed = false;
  856. else if (content.IsMixed) isMixed = true;
  857. map.BaseMap = baseMap;
  858. baseMap.DerivedTypes.Add (map);
  859. if (ext != null) {
  860. // Add the members of this map
  861. if (ext.Particle != null)
  862. ImportParticleComplexContent (typeQName, cmap, ext.Particle, classIds, isMixed);
  863. ImportAttributes (typeQName, cmap, ext.Attributes, ext.AnyAttribute, classIds);
  864. }
  865. else {
  866. if (isMixed) ImportParticleComplexContent (typeQName, cmap, null, classIds, true);
  867. }
  868. }
  869. void ImportExtensionTypes (XmlQualifiedName qname)
  870. {
  871. foreach (XmlSchema schema in schemas) {
  872. foreach (XmlSchemaObject sob in schema.Items)
  873. {
  874. XmlSchemaComplexType sct = sob as XmlSchemaComplexType;
  875. if (sct != null && sct.ContentModel is XmlSchemaComplexContent) {
  876. XmlQualifiedName exqname;
  877. XmlSchemaComplexContentExtension ext = sct.ContentModel.Content as XmlSchemaComplexContentExtension;
  878. if (ext != null) exqname = ext.BaseTypeName;
  879. else exqname = ((XmlSchemaComplexContentRestriction)sct.ContentModel.Content).BaseTypeName;
  880. if (exqname == qname)
  881. ImportType (new XmlQualifiedName (sct.Name, schema.TargetNamespace), sct, null);
  882. }
  883. }
  884. }
  885. }
  886. XmlTypeMapping ImportClassSimpleType (XmlQualifiedName typeQName, XmlSchemaSimpleType stype, XmlQualifiedName root)
  887. {
  888. if (CanBeEnum (stype))
  889. {
  890. // Create an enum map
  891. CodeIdentifiers codeIdents = new CodeIdentifiers ();
  892. XmlTypeMapping enumMap = CreateTypeMapping (typeQName, SchemaTypes.Enum, null);
  893. enumMap.Documentation = GetDocumentation (stype);
  894. bool isFlags = false;
  895. if (stype.Content is XmlSchemaSimpleTypeList) {
  896. stype = ((XmlSchemaSimpleTypeList)stype.Content).ItemType;
  897. isFlags = true;
  898. }
  899. XmlSchemaSimpleTypeRestriction rest = (XmlSchemaSimpleTypeRestriction)stype.Content;
  900. codeIdents.AddReserved (enumMap.TypeData.TypeName);
  901. EnumMap.EnumMapMember[] members = new EnumMap.EnumMapMember [rest.Facets.Count];
  902. for (int n=0; n<rest.Facets.Count; n++)
  903. {
  904. XmlSchemaEnumerationFacet enu = (XmlSchemaEnumerationFacet) rest.Facets[n];
  905. string enumName = codeIdents.AddUnique(CodeIdentifier.MakeValid (enu.Value), enu);
  906. members [n] = new EnumMap.EnumMapMember (enu.Value, enumName);
  907. members [n].Documentation = GetDocumentation (enu);
  908. }
  909. enumMap.ObjectMap = new EnumMap (members, isFlags);
  910. enumMap.IsSimpleType = true;
  911. return enumMap;
  912. }
  913. if (stype.Content is XmlSchemaSimpleTypeList)
  914. {
  915. XmlSchemaSimpleTypeList slist = (XmlSchemaSimpleTypeList)stype.Content;
  916. TypeData arrayTypeData = FindBuiltInType (slist.ItemTypeName, stype);
  917. ListMap listMap = new ListMap ();
  918. listMap.ItemInfo = new XmlTypeMapElementInfoList ();
  919. listMap.ItemInfo.Add (CreateElementInfo (typeQName.Namespace, null, "Item", arrayTypeData.ListItemTypeData, false, XmlSchemaForm.None));
  920. XmlTypeMapping map = CreateArrayTypeMapping (typeQName, arrayTypeData);
  921. map.ObjectMap = listMap;
  922. map.IsSimpleType = true;
  923. return map;
  924. }
  925. // It is an extension of a primitive or known type
  926. TypeData typeData = FindBuiltInType (typeQName, stype);
  927. return GetTypeMapping (typeData);
  928. }
  929. bool CanBeEnum (XmlSchemaSimpleType stype)
  930. {
  931. if (stype.Content is XmlSchemaSimpleTypeRestriction)
  932. {
  933. XmlSchemaSimpleTypeRestriction rest = (XmlSchemaSimpleTypeRestriction)stype.Content;
  934. if (rest.Facets.Count == 0) return false;
  935. foreach (object ob in rest.Facets)
  936. if (!(ob is XmlSchemaEnumerationFacet)) return false;
  937. return true;
  938. }
  939. else if (stype.Content is XmlSchemaSimpleTypeList)
  940. {
  941. XmlSchemaSimpleTypeList list = (XmlSchemaSimpleTypeList) stype.Content;
  942. return (list.ItemType != null && CanBeEnum (list.ItemType));
  943. }
  944. return false;
  945. }
  946. bool CanBeArray (XmlQualifiedName typeQName, XmlSchemaComplexType stype)
  947. {
  948. if (encodedFormat)
  949. {
  950. XmlSchemaComplexContent content = stype.ContentModel as XmlSchemaComplexContent;
  951. if (content == null) return false;
  952. XmlSchemaComplexContentRestriction rest = content.Content as XmlSchemaComplexContentRestriction;
  953. if (rest == null) return false;
  954. return rest.BaseTypeName == arrayType;
  955. }
  956. else
  957. {
  958. if (stype.Attributes.Count > 0 || stype.AnyAttribute != null) return false;
  959. else return !stype.IsMixed && CanBeArray (typeQName, stype.Particle, false);
  960. }
  961. }
  962. bool CanBeArray (XmlQualifiedName typeQName, XmlSchemaParticle particle, bool multiValue)
  963. {
  964. // To be an array, there can't be a direct child of type typeQName
  965. if (particle == null) return false;
  966. multiValue = multiValue || particle.MaxOccurs > 1;
  967. if (particle is XmlSchemaGroupRef)
  968. return CanBeArray (typeQName, GetRefGroupParticle ((XmlSchemaGroupRef)particle), multiValue);
  969. if (particle is XmlSchemaElement)
  970. {
  971. XmlSchemaElement elem = (XmlSchemaElement)particle;
  972. if (!elem.RefName.IsEmpty)
  973. return CanBeArray (typeQName, FindRefElement (elem), multiValue);
  974. else
  975. return multiValue && !typeQName.Equals (((XmlSchemaElement)particle).SchemaTypeName);
  976. }
  977. if (particle is XmlSchemaAny)
  978. return multiValue;
  979. if (particle is XmlSchemaSequence)
  980. {
  981. XmlSchemaSequence seq = particle as XmlSchemaSequence;
  982. if (seq.Items.Count != 1) return false;
  983. return CanBeArray (typeQName, (XmlSchemaParticle)seq.Items[0], multiValue);
  984. }
  985. if (particle is XmlSchemaChoice)
  986. {
  987. // Can be array if all choices have different types
  988. ArrayList types = new ArrayList ();
  989. if(!CheckChoiceType (typeQName, particle, types, ref multiValue)) return false;
  990. return multiValue;
  991. }
  992. return false;
  993. }
  994. bool CheckChoiceType (XmlQualifiedName typeQName, XmlSchemaParticle particle, ArrayList types, ref bool multiValue)
  995. {
  996. XmlQualifiedName type = null;
  997. multiValue = multiValue || particle.MaxOccurs > 1;
  998. if (particle is XmlSchemaGroupRef)
  999. return CheckChoiceType (typeQName, GetRefGroupParticle ((XmlSchemaGroupRef)particle), types, ref multiValue);
  1000. if (particle is XmlSchemaElement) {
  1001. string ns;
  1002. XmlSchemaElement elem = (XmlSchemaElement)particle;
  1003. XmlSchemaElement refElem = GetRefElement (typeQName, elem, out ns);
  1004. if (refElem.SchemaType != null) return true;
  1005. type = refElem.SchemaTypeName;
  1006. }
  1007. else if (particle is XmlSchemaAny) {
  1008. type = anyType;
  1009. }
  1010. else if (particle is XmlSchemaSequence)
  1011. {
  1012. XmlSchemaSequence seq = particle as XmlSchemaSequence;
  1013. foreach (XmlSchemaParticle par in seq.Items)
  1014. if (!CheckChoiceType (typeQName, par, types, ref multiValue)) return false;
  1015. return true;
  1016. }
  1017. else if (particle is XmlSchemaChoice)
  1018. {
  1019. foreach (XmlSchemaParticle choice in ((XmlSchemaChoice)particle).Items)
  1020. if (!CheckChoiceType (typeQName, choice, types, ref multiValue)) return false;
  1021. return true;
  1022. }
  1023. if (typeQName.Equals (type)) return false;
  1024. // For primitive types, compare using CLR types, since several
  1025. // xml types can be mapped to a single CLR type
  1026. string t;
  1027. if (type.Namespace == XmlSchema.Namespace)
  1028. t = TypeTranslator.GetPrimitiveTypeData (type.Name).FullTypeName + ":" + type.Namespace;
  1029. else
  1030. t = type.Name + ":" + type.Namespace;
  1031. if (types.Contains (t)) return false;
  1032. types.Add (t);
  1033. return true;
  1034. }
  1035. bool CanBeAnyElement (XmlSchemaComplexType stype)
  1036. {
  1037. XmlSchemaSequence seq = stype.Particle as XmlSchemaSequence;
  1038. return (seq != null) && (seq.Items.Count == 1) && (seq.Items[0] is XmlSchemaAny);
  1039. }
  1040. bool CanBeIXmlSerializable (XmlSchemaComplexType stype)
  1041. {
  1042. XmlSchemaSequence seq = stype.Particle as XmlSchemaSequence;
  1043. if (seq == null) return false;
  1044. if (seq.Items.Count != 2) return false;
  1045. XmlSchemaElement elem = seq.Items[0] as XmlSchemaElement;
  1046. if (elem == null) return false;
  1047. if (elem.RefName != new XmlQualifiedName ("schema",XmlSchema.Namespace)) return false;
  1048. return (seq.Items[1] is XmlSchemaAny);
  1049. }
  1050. XmlTypeMapping ImportXmlSerializableMapping (string ns)
  1051. {
  1052. XmlQualifiedName qname = new XmlQualifiedName ("System.Data.DataSet",ns);
  1053. XmlTypeMapping map = mappedTypes [qname] as XmlTypeMapping;
  1054. if (map != null) return map;
  1055. TypeData typeData = new TypeData ("System.Data.DataSet", "System.Data.DataSet", "System.Data.DataSet", SchemaTypes.XmlSerializable, null);
  1056. map = new XmlTypeMapping ("System.Data.DataSet", "", typeData, "System.Data.DataSet", ns);
  1057. map.IncludeInSchema = true;
  1058. mappedTypes [qname] = map;
  1059. dataMappedTypes [typeData] = map;
  1060. return map;
  1061. }
  1062. XmlTypeMapElementInfo CreateElementInfo (string ns, XmlTypeMapMember member, string name, TypeData typeData, bool isNillable, XmlSchemaForm form)
  1063. {
  1064. if (typeData.IsComplexType)
  1065. return CreateElementInfo (ns, member, name, typeData, isNillable, form, GetTypeMapping (typeData));
  1066. else
  1067. return CreateElementInfo (ns, member, name, typeData, isNillable, form, null);
  1068. }
  1069. XmlTypeMapElementInfo CreateElementInfo (string ns, XmlTypeMapMember member, string name, TypeData typeData, bool isNillable, XmlSchemaForm form, XmlTypeMapping emap)
  1070. {
  1071. XmlTypeMapElementInfo einfo = new XmlTypeMapElementInfo (member, typeData);
  1072. einfo.ElementName = name;
  1073. einfo.Namespace = ns;
  1074. einfo.IsNullable = isNillable;
  1075. einfo.Form = form;
  1076. if (typeData.IsComplexType)
  1077. einfo.MappedType = emap;
  1078. return einfo;
  1079. }
  1080. XmlTypeMapElementInfo CreateTextElementInfo (string ns, XmlTypeMapMember member, TypeData typeData)
  1081. {
  1082. XmlTypeMapElementInfo einfo = new XmlTypeMapElementInfo (member, typeData);
  1083. einfo.IsTextElement = true;
  1084. einfo.WrappedElement = false;
  1085. if (typeData.IsComplexType)
  1086. einfo.MappedType = GetTypeMapping (typeData);
  1087. return einfo;
  1088. }
  1089. XmlTypeMapping CreateTypeMapping (XmlQualifiedName typeQName, SchemaTypes schemaType, XmlQualifiedName root)
  1090. {
  1091. string typeName = CodeIdentifier.MakeValid (typeQName.Name);
  1092. typeName = typeIdentifiers.AddUnique (typeName, null);
  1093. TypeData typeData = new TypeData (typeName, typeName, typeName, schemaType, null);
  1094. string rootElem;
  1095. string rootNs;
  1096. if (root != null) {
  1097. rootElem = root.Name;
  1098. rootNs = root.Namespace;
  1099. }
  1100. else {
  1101. rootElem = typeQName.Name;
  1102. rootNs = "";
  1103. }
  1104. XmlTypeMapping map = new XmlTypeMapping (rootElem, rootNs, typeData, typeQName.Name, typeQName.Namespace);
  1105. map.IncludeInSchema = true;
  1106. mappedTypes [typeQName] = map;
  1107. dataMappedTypes [typeData] = map;
  1108. return map;
  1109. }
  1110. XmlTypeMapping CreateArrayTypeMapping (XmlQualifiedName typeQName, TypeData arrayTypeData)
  1111. {
  1112. XmlTypeMapping map;
  1113. if (encodedFormat) map = new XmlTypeMapping ("Array", XmlSerializer.EncodingNamespace, arrayTypeData, "Array", XmlSerializer.EncodingNamespace);
  1114. else map = new XmlTypeMapping (arrayTypeData.XmlType, typeQName.Namespace, arrayTypeData, arrayTypeData.XmlType, typeQName.Namespace);
  1115. map.IncludeInSchema = true;
  1116. mappedTypes [typeQName] = map;
  1117. dataMappedTypes [arrayTypeData] = map;
  1118. return map;
  1119. }
  1120. XmlSchemaElement GetRefElement (XmlQualifiedName typeQName, XmlSchemaElement elem, out string ns)
  1121. {
  1122. if (!elem.RefName.IsEmpty)
  1123. {
  1124. ns = elem.RefName.Namespace;
  1125. return FindRefElement (elem);
  1126. }
  1127. else
  1128. {
  1129. ns = typeQName.Namespace;
  1130. return elem;
  1131. }
  1132. }
  1133. XmlSchemaAttribute GetRefAttribute (XmlQualifiedName typeQName, XmlSchemaAttribute attr, out string ns)
  1134. {
  1135. if (!attr.RefName.IsEmpty)
  1136. {
  1137. ns = attr.RefName.Namespace;
  1138. XmlSchemaAttribute at = FindRefAttribute (attr.RefName);
  1139. if (at == null) throw new InvalidOperationException ("The attribute " + attr.RefName + " is missing");
  1140. return at;
  1141. }
  1142. else
  1143. {
  1144. ns = typeQName.Namespace;
  1145. return attr;
  1146. }
  1147. }
  1148. TypeData GetElementTypeData (XmlQualifiedName typeQName, XmlSchemaElement elem, out XmlTypeMapping map)
  1149. {
  1150. bool sharedAnnType = false;
  1151. XmlQualifiedName root = null;
  1152. map = null;
  1153. if (!elem.RefName.IsEmpty) {
  1154. XmlSchemaElement refElem = FindRefElement (elem);
  1155. if (refElem == null) throw new InvalidOperationException ("Global element not found: " + elem.RefName);
  1156. root = elem.RefName;
  1157. elem = refElem;
  1158. sharedAnnType = true;
  1159. }
  1160. TypeData td;
  1161. if (!elem.SchemaTypeName.IsEmpty) {
  1162. td = GetTypeData (elem.SchemaTypeName, root);
  1163. map = GetRegisteredTypeMapping (elem.SchemaTypeName);
  1164. }
  1165. else if (elem.SchemaType == null)
  1166. td = TypeTranslator.GetTypeData (typeof(object));
  1167. else
  1168. td = GetTypeData (elem.SchemaType, typeQName, elem.Name, sharedAnnType, root);
  1169. if (map == null && td.IsComplexType)
  1170. map = GetTypeMapping (td);
  1171. return td;
  1172. }
  1173. TypeData GetAttributeTypeData (XmlQualifiedName typeQName, XmlSchemaAttribute attr)
  1174. {
  1175. bool sharedAnnType = false;
  1176. if (!attr.RefName.IsEmpty) {
  1177. XmlSchemaAttribute refAtt = FindRefAttribute (attr.RefName);
  1178. if (refAtt == null) throw new InvalidOperationException ("Global attribute not found: " + attr.RefName);
  1179. attr = refAtt;
  1180. sharedAnnType = true;
  1181. }
  1182. if (!attr.SchemaTypeName.IsEmpty) return GetTypeData (attr.SchemaTypeName, null);
  1183. if (attr.SchemaType == null) return TypeTranslator.GetTypeData (typeof(string));
  1184. else return GetTypeData (attr.SchemaType, typeQName, attr.Name, sharedAnnType, null);
  1185. }
  1186. TypeData GetTypeData (XmlQualifiedName typeQName, XmlQualifiedName root)
  1187. {
  1188. if (typeQName.Namespace == XmlSchema.Namespace || (encodedFormat && typeQName.Namespace == ""))
  1189. return TypeTranslator.GetPrimitiveTypeData (typeQName.Name);
  1190. return ImportType (typeQName, root).TypeData;
  1191. }
  1192. TypeData GetTypeData (XmlSchemaType stype, XmlQualifiedName typeQNname, string propertyName, bool sharedAnnType, XmlQualifiedName root)
  1193. {
  1194. string baseName;
  1195. if (sharedAnnType)
  1196. {
  1197. // Anonymous types defined in root elements or attributes can be shared among all elements that
  1198. // reference this root element or attribute
  1199. TypeData std = sharedAnonymousTypes [stype] as TypeData;
  1200. if (std != null) return std;
  1201. baseName = propertyName;
  1202. }
  1203. else
  1204. baseName = typeQNname.Name + typeIdentifiers.MakeRightCase (propertyName);
  1205. baseName = elemIdentifiers.AddUnique (baseName, stype);
  1206. XmlQualifiedName newName;
  1207. newName = new XmlQualifiedName (baseName, typeQNname.Namespace);
  1208. XmlTypeMapping map = ImportType (newName, stype, root);
  1209. if (sharedAnnType) sharedAnonymousTypes [stype] = map.TypeData;
  1210. return map.TypeData;
  1211. }
  1212. XmlTypeMapping GetTypeMapping (TypeData typeData)
  1213. {
  1214. XmlTypeMapping map = (XmlTypeMapping) dataMappedTypes [typeData];
  1215. if (map != null) return map;
  1216. if (map == null && typeData.IsListType)
  1217. {
  1218. // Create an array map for the type
  1219. XmlTypeMapping itemMap = GetTypeMapping (typeData.ListItemTypeData);
  1220. map = new XmlTypeMapping (typeData.XmlType, itemMap.Namespace, typeData, typeData.XmlType, itemMap.Namespace);
  1221. map.IncludeInSchema = true;
  1222. ListMap listMap = new ListMap ();
  1223. listMap.ItemInfo = new XmlTypeMapElementInfoList();
  1224. listMap.ItemInfo.Add (CreateElementInfo (itemMap.Namespace, null, typeData.ListItemTypeData.XmlType, typeData.ListItemTypeData, false, XmlSchemaForm.None));
  1225. map.ObjectMap = listMap;
  1226. mappedTypes [new XmlQualifiedName(map.ElementName, map.Namespace)] = map;
  1227. dataMappedTypes [typeData] = map;
  1228. return map;
  1229. }
  1230. else if (typeData.SchemaType == SchemaTypes.Primitive || typeData.Type == typeof(object) || typeof(XmlNode).IsAssignableFrom(typeData.Type))
  1231. {
  1232. map = new XmlTypeMapping (typeData.XmlType, XmlSchema.Namespace, typeData, typeData.XmlType, XmlSchema.Namespace);
  1233. map.IncludeInSchema = false;
  1234. map.ObjectMap = new ClassMap ();
  1235. dataMappedTypes [typeData] = map;
  1236. if (typeData.Type == typeof(object))
  1237. {
  1238. // All complex types are subtypes of anyType, so all of them
  1239. // must also be imported
  1240. foreach (XmlSchema schema in schemas) {
  1241. foreach (XmlSchemaObject sob in schema.Items)
  1242. {
  1243. XmlSchemaComplexType sct = sob as XmlSchemaComplexType;
  1244. if (sct != null)
  1245. ImportType (new XmlQualifiedName (sct.Name, schema.TargetNamespace), sct, null);
  1246. }
  1247. }
  1248. }
  1249. return map;
  1250. }
  1251. throw new InvalidOperationException ("Map for type " + typeData.TypeName + " not found");
  1252. }
  1253. XmlTypeMapping GetRegisteredTypeMapping (XmlQualifiedName typeQName)
  1254. {
  1255. return (XmlTypeMapping) mappedTypes [typeQName];
  1256. }
  1257. XmlSchemaParticle GetRefGroupParticle (XmlSchemaGroupRef refGroup)
  1258. {
  1259. XmlSchemaGroup grp = (XmlSchemaGroup) schemas.Find (refGroup.RefName, typeof (XmlSchemaGroup));
  1260. return grp.Particle;
  1261. }
  1262. XmlSchemaElement FindRefElement (XmlSchemaElement elem)
  1263. {
  1264. if (elem.RefName.Namespace == XmlSchema.Namespace)
  1265. {
  1266. if (anyElement != null) return anyElement;
  1267. anyElement = new XmlSchemaElement ();
  1268. anyElement.Name = "any";
  1269. anyElement.SchemaTypeName = anyType;
  1270. return anyElement;
  1271. }
  1272. return (XmlSchemaElement) schemas.Find (elem.RefName, typeof(XmlSchemaElement));
  1273. }
  1274. XmlSchemaAttribute FindRefAttribute (XmlQualifiedName refName)
  1275. {
  1276. if (refName.Namespace == XmlNamespace)
  1277. {
  1278. XmlSchemaAttribute at = new XmlSchemaAttribute ();
  1279. at.Name = refName.Name;
  1280. at.SchemaTypeName = new XmlQualifiedName ("string",XmlSchema.Namespace);
  1281. return at;
  1282. }
  1283. return (XmlSchemaAttribute) schemas.Find (refName, typeof(XmlSchemaAttribute));
  1284. }
  1285. XmlSchemaAttributeGroup FindRefAttributeGroup (XmlQualifiedName refName)
  1286. {
  1287. XmlSchemaAttributeGroup grp = (XmlSchemaAttributeGroup) schemas.Find (refName, typeof(XmlSchemaAttributeGroup));
  1288. foreach (XmlSchemaObject at in grp.Attributes)
  1289. {
  1290. if (at is XmlSchemaAttributeGroupRef && ((XmlSchemaAttributeGroupRef)at).RefName == refName)
  1291. throw new InvalidOperationException ("Cannot import attribute group '" + refName.Name + "' from namespace '" + refName.Namespace + "'. Redefine not supported");
  1292. }
  1293. return grp;
  1294. }
  1295. string GetDocumentation (XmlSchemaAnnotated elem)
  1296. {
  1297. string res = "";
  1298. XmlSchemaAnnotation anot = elem.Annotation;
  1299. if (anot == null || anot.Items == null) return null;
  1300. foreach (object ob in anot.Items)
  1301. {
  1302. XmlSchemaDocumentation doc = ob as XmlSchemaDocumentation;
  1303. if (doc != null && doc.Markup != null && doc.Markup.Length > 0) {
  1304. if (res != string.Empty) res += "\n";
  1305. foreach (XmlNode node in doc.Markup)
  1306. res += node.Value;
  1307. }
  1308. }
  1309. return res;
  1310. }
  1311. #endregion // Methods
  1312. }
  1313. }