XmlSchemaImporter.cs 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571
  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) {
  312. XmlSchemaComplexType ct = stype as XmlSchemaComplexType;
  313. if (map.TypeData.SchemaType != SchemaTypes.Class || ct == null || !CanBeArray (name, ct))
  314. return map;
  315. // The map was initially imported as a class, but it turns out that it is an
  316. // array. It has to be imported now as array.
  317. }
  318. if (stype is XmlSchemaComplexType)
  319. return ImportClassComplexType (name, (XmlSchemaComplexType) stype, root);
  320. else if (stype is XmlSchemaSimpleType)
  321. return ImportClassSimpleType (name, (XmlSchemaSimpleType) stype, root);
  322. throw new NotSupportedException ("Schema type not supported: " + stype.GetType ());
  323. }
  324. XmlTypeMapping ImportClassComplexType (XmlQualifiedName typeQName, XmlSchemaComplexType stype, XmlQualifiedName root)
  325. {
  326. XmlTypeMapping map;
  327. // The need for fixups: If the complex type is an array, then to get the type of the
  328. // array we need first to get the type of the items of the array.
  329. // But if one of the item types or its children has a referece to this type array,
  330. // then we enter in an infinite loop. This does not happen with class types because
  331. // the class map is registered before parsing the children. We can't do the same
  332. // with the array type because to register the array map we need the type of the array.
  333. if (CanBeArray (typeQName, stype))
  334. {
  335. TypeData typeData;
  336. ListMap listMap = BuildArrayMap (typeQName, stype, out typeData);
  337. if (listMap != null)
  338. {
  339. map = CreateArrayTypeMapping (typeQName, typeData);
  340. map.ObjectMap = listMap;
  341. return map;
  342. }
  343. // After all, it is not an array. Create a class map then.
  344. }
  345. else if (CanBeAnyElement (stype))
  346. {
  347. if (stype.IsMixed)
  348. return GetTypeMapping (TypeTranslator.GetTypeData(typeof(XmlNode)));
  349. else
  350. return GetTypeMapping (TypeTranslator.GetTypeData(typeof(XmlElement)));
  351. }
  352. else if (CanBeIXmlSerializable (stype))
  353. {
  354. return ImportXmlSerializableMapping (typeQName.Namespace);
  355. }
  356. // Register the map right now but do not build it,
  357. // This will avoid loops.
  358. map = CreateTypeMapping (typeQName, SchemaTypes.Class, root);
  359. map.Documentation = GetDocumentation (stype);
  360. RegisterMapFixup (map, typeQName, stype);
  361. return map;
  362. }
  363. void RegisterMapFixup (XmlTypeMapping map, XmlQualifiedName typeQName, XmlSchemaComplexType stype)
  364. {
  365. MapFixup fixup = new MapFixup ();
  366. fixup.Map = map;
  367. fixup.SchemaType = stype;
  368. fixup.TypeName = typeQName;
  369. pendingMaps.Enqueue (fixup);
  370. }
  371. void BuildPendingMaps ()
  372. {
  373. while (pendingMaps.Count > 0) {
  374. MapFixup fixup = (MapFixup) pendingMaps.Dequeue ();
  375. if (fixup.Map.ObjectMap == null) {
  376. BuildClassMap (fixup.Map, fixup.TypeName, fixup.SchemaType);
  377. if (fixup.Map.ObjectMap == null) pendingMaps.Enqueue (fixup);
  378. }
  379. }
  380. }
  381. void BuildPendingMap (XmlTypeMapping map)
  382. {
  383. if (map.ObjectMap != null) return;
  384. foreach (MapFixup fixup in pendingMaps)
  385. {
  386. if (fixup.Map == map) {
  387. BuildClassMap (fixup.Map, fixup.TypeName, fixup.SchemaType);
  388. return;
  389. }
  390. }
  391. throw new InvalidOperationException ("Can't complete map of type " + map.XmlType + " : " + map.Namespace);
  392. }
  393. void BuildClassMap (XmlTypeMapping map, XmlQualifiedName typeQName, XmlSchemaComplexType stype)
  394. {
  395. CodeIdentifiers classIds = new CodeIdentifiers();
  396. classIds.AddReserved (map.TypeData.TypeName);
  397. ClassMap cmap = new ClassMap ();
  398. map.ObjectMap = cmap;
  399. bool isMixed = stype.IsMixed;
  400. if (stype.Particle != null)
  401. ImportParticleComplexContent (typeQName, cmap, stype.Particle, classIds, isMixed);
  402. else
  403. {
  404. if (stype.ContentModel is XmlSchemaSimpleContent) {
  405. ImportSimpleContent (typeQName, map, (XmlSchemaSimpleContent)stype.ContentModel, classIds, isMixed);
  406. }
  407. else if (stype.ContentModel is XmlSchemaComplexContent) {
  408. ImportComplexContent (typeQName, map, (XmlSchemaComplexContent)stype.ContentModel, classIds, isMixed);
  409. }
  410. }
  411. ImportAttributes (typeQName, cmap, stype.Attributes, stype.AnyAttribute, classIds);
  412. ImportExtensionTypes (typeQName);
  413. GetTypeMapping (TypeTranslator.GetTypeData (typeof(object))).DerivedTypes.Add (map);
  414. }
  415. void ImportAttributes (XmlQualifiedName typeQName, ClassMap cmap, XmlSchemaObjectCollection atts, XmlSchemaAnyAttribute anyat, CodeIdentifiers classIds)
  416. {
  417. if (anyat != null)
  418. {
  419. XmlTypeMapMemberAnyAttribute member = new XmlTypeMapMemberAnyAttribute ();
  420. member.Name = classIds.AddUnique ("AnyAttribute", member);
  421. member.TypeData = TypeTranslator.GetTypeData (typeof(XmlAttribute[]));
  422. cmap.AddMember (member);
  423. }
  424. foreach (XmlSchemaObject at in atts)
  425. {
  426. if (at is XmlSchemaAttribute)
  427. {
  428. string ns;
  429. XmlSchemaAttribute attr = (XmlSchemaAttribute)at;
  430. XmlSchemaAttribute refAttr = GetRefAttribute (typeQName, attr, out ns);
  431. XmlTypeMapMemberAttribute member = new XmlTypeMapMemberAttribute ();
  432. member.Name = classIds.AddUnique (CodeIdentifier.MakeValid (refAttr.Name), member);
  433. member.Documentation = GetDocumentation (attr);
  434. member.AttributeName = refAttr.Name;
  435. member.Namespace = ns;
  436. member.Form = refAttr.Form;
  437. member.TypeData = GetAttributeTypeData (typeQName, attr);
  438. if (refAttr.DefaultValue != null)
  439. member.DefaultValue = XmlCustomFormatter.FromXmlString (member.TypeData, refAttr.DefaultValue);
  440. else if (member.TypeData.IsValueType)
  441. member.IsOptionalValueType = (refAttr.ValidatedUse != XmlSchemaUse.Required);
  442. if (member.TypeData.IsComplexType)
  443. member.MappedType = GetTypeMapping (member.TypeData);
  444. cmap.AddMember (member);
  445. }
  446. else if (at is XmlSchemaAttributeGroupRef)
  447. {
  448. XmlSchemaAttributeGroupRef gref = (XmlSchemaAttributeGroupRef)at;
  449. XmlSchemaAttributeGroup grp = FindRefAttributeGroup (gref.RefName);
  450. ImportAttributes (typeQName, cmap, grp.Attributes, grp.AnyAttribute, classIds);
  451. }
  452. }
  453. }
  454. ListMap BuildArrayMap (XmlQualifiedName typeQName, XmlSchemaComplexType stype, out TypeData arrayTypeData)
  455. {
  456. if (encodedFormat)
  457. {
  458. XmlSchemaComplexContent content = stype.ContentModel as XmlSchemaComplexContent;
  459. XmlSchemaComplexContentRestriction rest = content.Content as XmlSchemaComplexContentRestriction;
  460. XmlSchemaAttribute arrayTypeAt = FindArrayAttribute (rest.Attributes);
  461. if (arrayTypeAt != null)
  462. {
  463. XmlAttribute[] uatts = arrayTypeAt.UnhandledAttributes;
  464. if (uatts == null || uatts.Length == 0) throw new InvalidOperationException ("arrayType attribute not specified in array declaration: " + typeQName);
  465. XmlAttribute xat = null;
  466. foreach (XmlAttribute at in uatts)
  467. if (at.LocalName == "arrayType" && at.NamespaceURI == XmlSerializer.WsdlNamespace)
  468. { xat = at; break; }
  469. if (xat == null)
  470. throw new InvalidOperationException ("arrayType attribute not specified in array declaration: " + typeQName);
  471. string name, ns, dims;
  472. TypeTranslator.ParseArrayType (xat.Value, out name, out ns, out dims);
  473. return BuildEncodedArrayMap (name + dims, ns, out arrayTypeData);
  474. }
  475. else
  476. {
  477. XmlSchemaElement elem = null;
  478. XmlSchemaSequence seq = rest.Particle as XmlSchemaSequence;
  479. if (seq != null && seq.Items.Count == 1)
  480. elem = seq.Items[0] as XmlSchemaElement;
  481. else {
  482. XmlSchemaAll all = rest.Particle as XmlSchemaAll;
  483. if (all != null && all.Items.Count == 1)
  484. elem = all.Items[0] as XmlSchemaElement;
  485. }
  486. if (elem == null)
  487. throw new InvalidOperationException ("Unknown array format");
  488. return BuildEncodedArrayMap (elem.SchemaTypeName.Name + "[]", elem.SchemaTypeName.Namespace, out arrayTypeData);
  489. }
  490. }
  491. else
  492. {
  493. ClassMap cmap = new ClassMap ();
  494. CodeIdentifiers classIds = new CodeIdentifiers();
  495. ImportParticleComplexContent (typeQName, cmap, stype.Particle, classIds, stype.IsMixed);
  496. XmlTypeMapMemberFlatList list = (cmap.AllMembers.Count == 1) ? cmap.AllMembers[0] as XmlTypeMapMemberFlatList : null;
  497. if (list != null && list.ChoiceMember == null)
  498. {
  499. arrayTypeData = list.TypeData;
  500. return list.ListMap;
  501. }
  502. else
  503. {
  504. arrayTypeData = null;
  505. return null;
  506. }
  507. }
  508. }
  509. ListMap BuildEncodedArrayMap (string type, string ns, out TypeData arrayTypeData)
  510. {
  511. ListMap map = new ListMap ();
  512. int i = type.LastIndexOf ("[");
  513. if (i == -1) throw new InvalidOperationException ("Invalid arrayType value: " + type);
  514. if (type.IndexOf (",",i) != -1) throw new InvalidOperationException ("Multidimensional arrays are not supported");
  515. string itemType = type.Substring (0,i);
  516. TypeData itemTypeData;
  517. if (itemType.IndexOf ("[") != -1)
  518. {
  519. ListMap innerListMap = BuildEncodedArrayMap (itemType, ns, out itemTypeData);
  520. int dims = itemType.Split ('[').Length - 1;
  521. string name = TypeTranslator.GetArrayName (type, dims);
  522. XmlQualifiedName qname = new XmlQualifiedName (name, ns);
  523. XmlTypeMapping tmap = CreateArrayTypeMapping (qname, itemTypeData);
  524. tmap.ObjectMap = innerListMap;
  525. }
  526. else
  527. {
  528. itemTypeData = GetTypeData (new XmlQualifiedName (itemType, ns), null);
  529. }
  530. arrayTypeData = itemTypeData.ListTypeData;
  531. map.ItemInfo = new XmlTypeMapElementInfoList();
  532. map.ItemInfo.Add (CreateElementInfo ("", null, "Item", itemTypeData, true, XmlSchemaForm.None));
  533. return map;
  534. }
  535. XmlSchemaAttribute FindArrayAttribute (XmlSchemaObjectCollection atts)
  536. {
  537. foreach (object ob in atts)
  538. {
  539. XmlSchemaAttribute att = ob as XmlSchemaAttribute;
  540. if (att != null && att.RefName == arrayTypeRefName) return att;
  541. XmlSchemaAttributeGroupRef gref = ob as XmlSchemaAttributeGroupRef;
  542. if (gref != null)
  543. {
  544. XmlSchemaAttributeGroup grp = FindRefAttributeGroup (gref.RefName);
  545. att = FindArrayAttribute (grp.Attributes);
  546. if (att != null) return att;
  547. }
  548. }
  549. return null;
  550. }
  551. void ImportParticleComplexContent (XmlQualifiedName typeQName, ClassMap cmap, XmlSchemaParticle particle, CodeIdentifiers classIds, bool isMixed)
  552. {
  553. ImportParticleContent (typeQName, cmap, particle, classIds, false, ref isMixed);
  554. if (isMixed && cmap.XmlTextCollector == null)
  555. {
  556. XmlTypeMapMemberFlatList member = new XmlTypeMapMemberFlatList ();
  557. member.Name = classIds.AddUnique ("Text", member);
  558. member.TypeData = TypeTranslator.GetTypeData (typeof(string[]));
  559. member.ElementInfo.Add (CreateTextElementInfo (typeQName.Namespace, member, member.TypeData.ListItemTypeData));
  560. member.IsXmlTextCollector = true;
  561. member.ListMap = new ListMap ();
  562. member.ListMap.ItemInfo = member.ElementInfo;
  563. cmap.AddMember (member);
  564. }
  565. }
  566. void ImportParticleContent (XmlQualifiedName typeQName, ClassMap cmap, XmlSchemaParticle particle, CodeIdentifiers classIds, bool multiValue, ref bool isMixed)
  567. {
  568. if (particle is XmlSchemaGroupRef)
  569. particle = GetRefGroupParticle ((XmlSchemaGroupRef)particle);
  570. if (particle.MaxOccurs > 1) multiValue = true;
  571. if (particle is XmlSchemaSequence) {
  572. ImportSequenceContent (typeQName, cmap, ((XmlSchemaSequence)particle).Items, classIds, multiValue, ref isMixed);
  573. }
  574. else if (particle is XmlSchemaChoice) {
  575. if (((XmlSchemaChoice)particle).Items.Count == 1)
  576. ImportSequenceContent (typeQName, cmap, ((XmlSchemaChoice)particle).Items, classIds, multiValue, ref isMixed);
  577. else
  578. ImportChoiceContent (typeQName, cmap, (XmlSchemaChoice)particle, classIds, multiValue);
  579. }
  580. else if (particle is XmlSchemaAll) {
  581. ImportSequenceContent (typeQName, cmap, ((XmlSchemaAll)particle).Items, classIds, multiValue, ref isMixed);
  582. }
  583. }
  584. void ImportSequenceContent (XmlQualifiedName typeQName, ClassMap cmap, XmlSchemaObjectCollection items, CodeIdentifiers classIds, bool multiValue, ref bool isMixed)
  585. {
  586. foreach (XmlSchemaObject item in items)
  587. {
  588. if (item is XmlSchemaElement)
  589. {
  590. string ns;
  591. XmlSchemaElement elem = (XmlSchemaElement) item;
  592. XmlTypeMapping emap;
  593. TypeData typeData = GetElementTypeData (typeQName, elem, out emap);
  594. XmlSchemaElement refElem = GetRefElement (typeQName, elem, out ns);
  595. if (elem.MaxOccurs == 1 && !multiValue)
  596. {
  597. XmlTypeMapMemberElement member = null;
  598. if (typeData.SchemaType != SchemaTypes.Array)
  599. {
  600. member = new XmlTypeMapMemberElement ();
  601. if (refElem.DefaultValue != null) member.DefaultValue = XmlCustomFormatter.FromXmlString (typeData, refElem.DefaultValue);
  602. }
  603. else if (GetTypeMapping (typeData).IsSimpleType)
  604. {
  605. // It is a simple list (space separated list).
  606. // Since this is not supported, map as a single item value
  607. // TODO: improve this
  608. member = new XmlTypeMapMemberElement ();
  609. typeData = typeData.ListItemTypeData;
  610. }
  611. else
  612. member = new XmlTypeMapMemberList ();
  613. if (elem.MinOccurs == 0 && typeData.IsValueType)
  614. member.IsOptionalValueType = true;
  615. member.Name = classIds.AddUnique(CodeIdentifier.MakeValid(refElem.Name), member);
  616. member.Documentation = GetDocumentation (elem);
  617. member.TypeData = typeData;
  618. member.ElementInfo.Add (CreateElementInfo (ns, member, refElem.Name, typeData, refElem.IsNillable, refElem.Form, emap));
  619. cmap.AddMember (member);
  620. }
  621. else
  622. {
  623. XmlTypeMapMemberFlatList member = new XmlTypeMapMemberFlatList ();
  624. member.ListMap = new ListMap ();
  625. member.Name = classIds.AddUnique(CodeIdentifier.MakeValid(refElem.Name), member);
  626. member.Documentation = GetDocumentation (elem);
  627. member.TypeData = typeData.ListTypeData;
  628. member.ElementInfo.Add (CreateElementInfo (ns, member, refElem.Name, typeData, refElem.IsNillable, refElem.Form, emap));
  629. member.ListMap.ItemInfo = member.ElementInfo;
  630. cmap.AddMember (member);
  631. }
  632. }
  633. else if (item is XmlSchemaAny)
  634. {
  635. XmlSchemaAny elem = (XmlSchemaAny) item;
  636. XmlTypeMapMemberAnyElement member = new XmlTypeMapMemberAnyElement ();
  637. member.Name = classIds.AddUnique ("Any", member);
  638. member.Documentation = GetDocumentation (elem);
  639. Type ctype;
  640. if (elem.MaxOccurs != 1 || multiValue)
  641. ctype = isMixed ? typeof(XmlNode[]) : typeof(XmlElement[]);
  642. else
  643. ctype = isMixed ? typeof(XmlNode) : typeof(XmlElement);
  644. member.TypeData = TypeTranslator.GetTypeData (ctype);
  645. XmlTypeMapElementInfo einfo = new XmlTypeMapElementInfo (member, member.TypeData);
  646. einfo.IsUnnamedAnyElement = true;
  647. member.ElementInfo.Add (einfo);
  648. if (isMixed)
  649. {
  650. einfo = CreateTextElementInfo (typeQName.Namespace, member, member.TypeData);
  651. member.ElementInfo.Add (einfo);
  652. member.IsXmlTextCollector = true;
  653. isMixed = false; //Allow only one XmlTextAttribute
  654. }
  655. cmap.AddMember (member);
  656. }
  657. else if (item is XmlSchemaParticle) {
  658. ImportParticleContent (typeQName, cmap, (XmlSchemaParticle)item, classIds, multiValue, ref isMixed);
  659. }
  660. }
  661. }
  662. void ImportChoiceContent (XmlQualifiedName typeQName, ClassMap cmap, XmlSchemaChoice choice, CodeIdentifiers classIds, bool multiValue)
  663. {
  664. XmlTypeMapElementInfoList choices = new XmlTypeMapElementInfoList ();
  665. multiValue = ImportChoices (typeQName, null, choices, choice.Items) || multiValue;
  666. if (choices.Count == 0) return;
  667. if (choice.MaxOccurs > 1) multiValue = true;
  668. XmlTypeMapMemberElement member;
  669. if (multiValue)
  670. {
  671. member = new XmlTypeMapMemberFlatList ();
  672. member.Name = classIds.AddUnique ("Items", member);
  673. ListMap listMap = new ListMap ();
  674. listMap.ItemInfo = choices;
  675. ((XmlTypeMapMemberFlatList)member).ListMap = listMap;
  676. }
  677. else
  678. {
  679. member = new XmlTypeMapMemberElement ();
  680. member.Name = classIds.AddUnique ("Item", member);
  681. }
  682. // If all choices have the same type, use that type for the member.
  683. // If not use System.Object.
  684. // If there are at least two choices with the same type, use a choice
  685. // identifier attribute
  686. TypeData typeData = null;
  687. bool twoEqual = false;
  688. bool allEqual = true;
  689. Hashtable types = new Hashtable ();
  690. foreach (XmlTypeMapElementInfo einfo in choices)
  691. {
  692. if (types.ContainsKey (einfo.TypeData)) twoEqual = true;
  693. else types.Add (einfo.TypeData, einfo);
  694. TypeData choiceType = einfo.TypeData;
  695. if (choiceType.SchemaType == SchemaTypes.Class)
  696. {
  697. // When comparing class types, use the most generic class in the
  698. // inheritance hierarchy
  699. XmlTypeMapping choiceMap = GetTypeMapping (choiceType);
  700. BuildPendingMap (choiceMap);
  701. while (choiceMap.BaseMap != null) {
  702. choiceMap = choiceMap.BaseMap;
  703. BuildPendingMap (choiceMap);
  704. choiceType = choiceMap.TypeData;
  705. }
  706. }
  707. if (typeData == null) typeData = choiceType;
  708. else if (typeData != choiceType) allEqual = false;
  709. }
  710. if (!allEqual)
  711. typeData = TypeTranslator.GetTypeData (typeof(object));
  712. if (twoEqual)
  713. {
  714. // Create the choice member
  715. XmlTypeMapMemberElement choiceMember = new XmlTypeMapMemberElement ();
  716. choiceMember.Name = classIds.AddUnique (member.Name + "ElementName", choiceMember);
  717. member.ChoiceMember = choiceMember.Name;
  718. // Create the choice enum
  719. XmlTypeMapping enumMap = CreateTypeMapping (new XmlQualifiedName (member.Name + "ChoiceType", typeQName.Namespace), SchemaTypes.Enum, null);
  720. CodeIdentifiers codeIdents = new CodeIdentifiers ();
  721. EnumMap.EnumMapMember[] members = new EnumMap.EnumMapMember [choices.Count];
  722. for (int n=0; n<choices.Count; n++)
  723. {
  724. XmlTypeMapElementInfo it =(XmlTypeMapElementInfo) choices[n];
  725. string xmlName = (it.Namespace != null && it.Namespace != "") ? it.Namespace + ":" + it.ElementName : it.ElementName;
  726. string enumName = codeIdents.AddUnique (CodeIdentifier.MakeValid (it.ElementName), it);
  727. members [n] = new EnumMap.EnumMapMember (xmlName, enumName);
  728. }
  729. enumMap.ObjectMap = new EnumMap (members, false);
  730. choiceMember.TypeData = multiValue ? enumMap.TypeData.ListTypeData : enumMap.TypeData;
  731. choiceMember.ElementInfo.Add (CreateElementInfo (typeQName.Namespace, choiceMember, choiceMember.Name, choiceMember.TypeData, false, XmlSchemaForm.None));
  732. cmap.AddMember (choiceMember);
  733. }
  734. if (multiValue)
  735. typeData = typeData.ListTypeData;
  736. member.ElementInfo = choices;
  737. member.Documentation = GetDocumentation (choice);
  738. member.TypeData = typeData;
  739. cmap.AddMember (member);
  740. }
  741. bool ImportChoices (XmlQualifiedName typeQName, XmlTypeMapMember member, XmlTypeMapElementInfoList choices, XmlSchemaObjectCollection items)
  742. {
  743. bool multiValue = false;
  744. foreach (XmlSchemaObject titem in items)
  745. {
  746. XmlSchemaObject item = titem;
  747. if (item is XmlSchemaGroupRef)
  748. item = GetRefGroupParticle ((XmlSchemaGroupRef)item);
  749. if (item is XmlSchemaElement)
  750. {
  751. string ns;
  752. XmlSchemaElement elem = (XmlSchemaElement) item;
  753. XmlTypeMapping emap;
  754. TypeData typeData = GetElementTypeData (typeQName, elem, out emap);
  755. XmlSchemaElement refElem = GetRefElement (typeQName, elem, out ns);
  756. choices.Add (CreateElementInfo (ns, member, refElem.Name, typeData, refElem.IsNillable, refElem.Form, emap));
  757. if (elem.MaxOccurs > 1) multiValue = true;
  758. }
  759. else if (item is XmlSchemaAny)
  760. {
  761. XmlTypeMapElementInfo einfo = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(typeof(XmlElement)));
  762. einfo.IsUnnamedAnyElement = true;
  763. choices.Add (einfo);
  764. }
  765. else if (item is XmlSchemaChoice) {
  766. multiValue = ImportChoices (typeQName, member, choices, ((XmlSchemaChoice)item).Items) || multiValue;
  767. }
  768. else if (item is XmlSchemaSequence) {
  769. multiValue = ImportChoices (typeQName, member, choices, ((XmlSchemaSequence)item).Items) || multiValue;
  770. }
  771. }
  772. return multiValue;
  773. }
  774. void ImportSimpleContent (XmlQualifiedName typeQName, XmlTypeMapping map, XmlSchemaSimpleContent content, CodeIdentifiers classIds, bool isMixed)
  775. {
  776. ClassMap cmap = (ClassMap)map.ObjectMap;
  777. XmlQualifiedName qname = GetContentBaseType (content.Content);
  778. XmlTypeMapMemberElement member = new XmlTypeMapMemberElement ();
  779. member.Name = classIds.AddUnique("Value", member);
  780. member.TypeData = FindBuiltInType (qname);
  781. member.ElementInfo.Add (CreateTextElementInfo (typeQName.Namespace, member, member.TypeData));
  782. member.IsXmlTextCollector = true;
  783. cmap.AddMember (member);
  784. XmlSchemaSimpleContentExtension ext = content.Content as XmlSchemaSimpleContentExtension;
  785. if (ext != null)
  786. ImportAttributes (typeQName, cmap, ext.Attributes, ext.AnyAttribute, classIds);
  787. }
  788. TypeData FindBuiltInType (XmlQualifiedName qname)
  789. {
  790. if (qname.Namespace == XmlSchema.Namespace)
  791. return TypeTranslator.GetPrimitiveTypeData (qname.Name);
  792. XmlSchemaComplexType ct = (XmlSchemaComplexType) schemas.Find (qname, typeof(XmlSchemaComplexType));
  793. if (ct != null)
  794. {
  795. XmlSchemaSimpleContent sc = ct.ContentModel as XmlSchemaSimpleContent;
  796. if (sc == null) throw new InvalidOperationException ("Invalid schema");
  797. return FindBuiltInType (GetContentBaseType (sc.Content));
  798. }
  799. XmlSchemaSimpleType st = (XmlSchemaSimpleType) schemas.Find (qname, typeof(XmlSchemaSimpleType));
  800. if (st != null)
  801. return FindBuiltInType (qname, st);
  802. throw new InvalidOperationException ("Definition of type " + qname + " not found");
  803. }
  804. TypeData FindBuiltInType (XmlQualifiedName qname, XmlSchemaSimpleType st)
  805. {
  806. if (CanBeEnum (st))
  807. return ImportType (qname, null).TypeData;
  808. if (st.Content is XmlSchemaSimpleTypeRestriction) {
  809. return FindBuiltInType (GetContentBaseType (st.Content));
  810. }
  811. else if (st.Content is XmlSchemaSimpleTypeList) {
  812. return FindBuiltInType (GetContentBaseType (st.Content)).ListTypeData;
  813. }
  814. else if (st.Content is XmlSchemaSimpleTypeUnion)
  815. {
  816. // Check if all types of the union are equal. If not, then will use anyType.
  817. XmlSchemaSimpleTypeUnion uni = (XmlSchemaSimpleTypeUnion) st.Content;
  818. TypeData utype = null;
  819. // Anonymous types are unique
  820. if (uni.BaseTypes.Count != 0 && uni.MemberTypes.Length != 0)
  821. return FindBuiltInType (anyType);
  822. foreach (XmlQualifiedName mt in uni.MemberTypes)
  823. {
  824. TypeData qn = FindBuiltInType (mt);
  825. if (utype != null && qn != utype) return FindBuiltInType (anyType);
  826. else utype = qn;
  827. }
  828. return utype;
  829. }
  830. else
  831. return null;
  832. }
  833. XmlQualifiedName GetContentBaseType (XmlSchemaObject ob)
  834. {
  835. if (ob is XmlSchemaSimpleContentExtension)
  836. return ((XmlSchemaSimpleContentExtension)ob).BaseTypeName;
  837. else if (ob is XmlSchemaSimpleContentRestriction)
  838. return ((XmlSchemaSimpleContentRestriction)ob).BaseTypeName;
  839. else if (ob is XmlSchemaSimpleTypeRestriction)
  840. return ((XmlSchemaSimpleTypeRestriction)ob).BaseTypeName;
  841. else if (ob is XmlSchemaSimpleTypeList)
  842. return ((XmlSchemaSimpleTypeList)ob).ItemTypeName;
  843. else
  844. return null;
  845. }
  846. void ImportComplexContent (XmlQualifiedName typeQName, XmlTypeMapping map, XmlSchemaComplexContent content, CodeIdentifiers classIds, bool isMixed)
  847. {
  848. ClassMap cmap = (ClassMap)map.ObjectMap;
  849. XmlQualifiedName qname;
  850. XmlSchemaComplexContentExtension ext = content.Content as XmlSchemaComplexContentExtension;
  851. if (ext != null) qname = ext.BaseTypeName;
  852. else qname = ((XmlSchemaComplexContentRestriction)content.Content).BaseTypeName;
  853. if (qname == typeQName)
  854. throw new InvalidOperationException ("Cannot import schema for type '" + typeQName.Name + "' from namespace '" + typeQName.Namespace + "'. Redefine not supported");
  855. // Add base map members to this map
  856. XmlTypeMapping baseMap = ImportType (qname, null);
  857. BuildPendingMap (baseMap);
  858. ClassMap baseClassMap = (ClassMap)baseMap.ObjectMap;
  859. foreach (XmlTypeMapMember member in baseClassMap.AllMembers)
  860. cmap.AddMember (member);
  861. if (baseClassMap.XmlTextCollector != null) isMixed = false;
  862. else if (content.IsMixed) isMixed = true;
  863. map.BaseMap = baseMap;
  864. baseMap.DerivedTypes.Add (map);
  865. if (ext != null) {
  866. // Add the members of this map
  867. if (ext.Particle != null)
  868. ImportParticleComplexContent (typeQName, cmap, ext.Particle, classIds, isMixed);
  869. ImportAttributes (typeQName, cmap, ext.Attributes, ext.AnyAttribute, classIds);
  870. }
  871. else {
  872. if (isMixed) ImportParticleComplexContent (typeQName, cmap, null, classIds, true);
  873. }
  874. }
  875. void ImportExtensionTypes (XmlQualifiedName qname)
  876. {
  877. foreach (XmlSchema schema in schemas) {
  878. foreach (XmlSchemaObject sob in schema.Items)
  879. {
  880. XmlSchemaComplexType sct = sob as XmlSchemaComplexType;
  881. if (sct != null && sct.ContentModel is XmlSchemaComplexContent) {
  882. XmlQualifiedName exqname;
  883. XmlSchemaComplexContentExtension ext = sct.ContentModel.Content as XmlSchemaComplexContentExtension;
  884. if (ext != null) exqname = ext.BaseTypeName;
  885. else exqname = ((XmlSchemaComplexContentRestriction)sct.ContentModel.Content).BaseTypeName;
  886. if (exqname == qname)
  887. ImportType (new XmlQualifiedName (sct.Name, schema.TargetNamespace), sct, null);
  888. }
  889. }
  890. }
  891. }
  892. XmlTypeMapping ImportClassSimpleType (XmlQualifiedName typeQName, XmlSchemaSimpleType stype, XmlQualifiedName root)
  893. {
  894. if (CanBeEnum (stype))
  895. {
  896. // Create an enum map
  897. CodeIdentifiers codeIdents = new CodeIdentifiers ();
  898. XmlTypeMapping enumMap = CreateTypeMapping (typeQName, SchemaTypes.Enum, null);
  899. enumMap.Documentation = GetDocumentation (stype);
  900. bool isFlags = false;
  901. if (stype.Content is XmlSchemaSimpleTypeList) {
  902. stype = ((XmlSchemaSimpleTypeList)stype.Content).ItemType;
  903. isFlags = true;
  904. }
  905. XmlSchemaSimpleTypeRestriction rest = (XmlSchemaSimpleTypeRestriction)stype.Content;
  906. codeIdents.AddReserved (enumMap.TypeData.TypeName);
  907. EnumMap.EnumMapMember[] members = new EnumMap.EnumMapMember [rest.Facets.Count];
  908. for (int n=0; n<rest.Facets.Count; n++)
  909. {
  910. XmlSchemaEnumerationFacet enu = (XmlSchemaEnumerationFacet) rest.Facets[n];
  911. string enumName = codeIdents.AddUnique(CodeIdentifier.MakeValid (enu.Value), enu);
  912. members [n] = new EnumMap.EnumMapMember (enu.Value, enumName);
  913. members [n].Documentation = GetDocumentation (enu);
  914. }
  915. enumMap.ObjectMap = new EnumMap (members, isFlags);
  916. enumMap.IsSimpleType = true;
  917. return enumMap;
  918. }
  919. if (stype.Content is XmlSchemaSimpleTypeList)
  920. {
  921. XmlSchemaSimpleTypeList slist = (XmlSchemaSimpleTypeList)stype.Content;
  922. TypeData arrayTypeData = FindBuiltInType (slist.ItemTypeName, stype);
  923. ListMap listMap = new ListMap ();
  924. listMap.ItemInfo = new XmlTypeMapElementInfoList ();
  925. listMap.ItemInfo.Add (CreateElementInfo (typeQName.Namespace, null, "Item", arrayTypeData.ListItemTypeData, false, XmlSchemaForm.None));
  926. XmlTypeMapping map = CreateArrayTypeMapping (typeQName, arrayTypeData);
  927. map.ObjectMap = listMap;
  928. map.IsSimpleType = true;
  929. return map;
  930. }
  931. // It is an extension of a primitive or known type
  932. TypeData typeData = FindBuiltInType (typeQName, stype);
  933. return GetTypeMapping (typeData);
  934. }
  935. bool CanBeEnum (XmlSchemaSimpleType stype)
  936. {
  937. if (stype.Content is XmlSchemaSimpleTypeRestriction)
  938. {
  939. XmlSchemaSimpleTypeRestriction rest = (XmlSchemaSimpleTypeRestriction)stype.Content;
  940. if (rest.Facets.Count == 0) return false;
  941. foreach (object ob in rest.Facets)
  942. if (!(ob is XmlSchemaEnumerationFacet)) return false;
  943. return true;
  944. }
  945. else if (stype.Content is XmlSchemaSimpleTypeList)
  946. {
  947. XmlSchemaSimpleTypeList list = (XmlSchemaSimpleTypeList) stype.Content;
  948. return (list.ItemType != null && CanBeEnum (list.ItemType));
  949. }
  950. return false;
  951. }
  952. bool CanBeArray (XmlQualifiedName typeQName, XmlSchemaComplexType stype)
  953. {
  954. if (encodedFormat)
  955. {
  956. XmlSchemaComplexContent content = stype.ContentModel as XmlSchemaComplexContent;
  957. if (content == null) return false;
  958. XmlSchemaComplexContentRestriction rest = content.Content as XmlSchemaComplexContentRestriction;
  959. if (rest == null) return false;
  960. return rest.BaseTypeName == arrayType;
  961. }
  962. else
  963. {
  964. if (stype.Attributes.Count > 0 || stype.AnyAttribute != null) return false;
  965. else return !stype.IsMixed && CanBeArray (typeQName, stype.Particle, false);
  966. }
  967. }
  968. bool CanBeArray (XmlQualifiedName typeQName, XmlSchemaParticle particle, bool multiValue)
  969. {
  970. // To be an array, there can't be a direct child of type typeQName
  971. if (particle == null) return false;
  972. multiValue = multiValue || particle.MaxOccurs > 1;
  973. if (particle is XmlSchemaGroupRef)
  974. return CanBeArray (typeQName, GetRefGroupParticle ((XmlSchemaGroupRef)particle), multiValue);
  975. if (particle is XmlSchemaElement)
  976. {
  977. XmlSchemaElement elem = (XmlSchemaElement)particle;
  978. if (!elem.RefName.IsEmpty)
  979. return CanBeArray (typeQName, FindRefElement (elem), multiValue);
  980. else
  981. return multiValue && !typeQName.Equals (((XmlSchemaElement)particle).SchemaTypeName);
  982. }
  983. if (particle is XmlSchemaAny)
  984. return multiValue;
  985. if (particle is XmlSchemaSequence)
  986. {
  987. XmlSchemaSequence seq = particle as XmlSchemaSequence;
  988. if (seq.Items.Count != 1) return false;
  989. return CanBeArray (typeQName, (XmlSchemaParticle)seq.Items[0], multiValue);
  990. }
  991. if (particle is XmlSchemaChoice)
  992. {
  993. // Can be array if all choices have different types
  994. ArrayList types = new ArrayList ();
  995. if(!CheckChoiceType (typeQName, particle, types, ref multiValue)) return false;
  996. return multiValue;
  997. }
  998. return false;
  999. }
  1000. bool CheckChoiceType (XmlQualifiedName typeQName, XmlSchemaParticle particle, ArrayList types, ref bool multiValue)
  1001. {
  1002. XmlQualifiedName type = null;
  1003. multiValue = multiValue || particle.MaxOccurs > 1;
  1004. if (particle is XmlSchemaGroupRef)
  1005. return CheckChoiceType (typeQName, GetRefGroupParticle ((XmlSchemaGroupRef)particle), types, ref multiValue);
  1006. if (particle is XmlSchemaElement) {
  1007. string ns;
  1008. XmlSchemaElement elem = (XmlSchemaElement)particle;
  1009. XmlSchemaElement refElem = GetRefElement (typeQName, elem, out ns);
  1010. if (refElem.SchemaType != null) return true;
  1011. type = refElem.SchemaTypeName;
  1012. }
  1013. else if (particle is XmlSchemaAny) {
  1014. type = anyType;
  1015. }
  1016. else if (particle is XmlSchemaSequence)
  1017. {
  1018. XmlSchemaSequence seq = particle as XmlSchemaSequence;
  1019. foreach (XmlSchemaParticle par in seq.Items)
  1020. if (!CheckChoiceType (typeQName, par, types, ref multiValue)) return false;
  1021. return true;
  1022. }
  1023. else if (particle is XmlSchemaChoice)
  1024. {
  1025. foreach (XmlSchemaParticle choice in ((XmlSchemaChoice)particle).Items)
  1026. if (!CheckChoiceType (typeQName, choice, types, ref multiValue)) return false;
  1027. return true;
  1028. }
  1029. if (typeQName.Equals (type)) return false;
  1030. // For primitive types, compare using CLR types, since several
  1031. // xml types can be mapped to a single CLR type
  1032. string t;
  1033. if (type.Namespace == XmlSchema.Namespace)
  1034. t = TypeTranslator.GetPrimitiveTypeData (type.Name).FullTypeName + ":" + type.Namespace;
  1035. else
  1036. t = type.Name + ":" + type.Namespace;
  1037. if (types.Contains (t)) return false;
  1038. types.Add (t);
  1039. return true;
  1040. }
  1041. bool CanBeAnyElement (XmlSchemaComplexType stype)
  1042. {
  1043. XmlSchemaSequence seq = stype.Particle as XmlSchemaSequence;
  1044. return (seq != null) && (seq.Items.Count == 1) && (seq.Items[0] is XmlSchemaAny);
  1045. }
  1046. bool CanBeIXmlSerializable (XmlSchemaComplexType stype)
  1047. {
  1048. XmlSchemaSequence seq = stype.Particle as XmlSchemaSequence;
  1049. if (seq == null) return false;
  1050. if (seq.Items.Count != 2) return false;
  1051. XmlSchemaElement elem = seq.Items[0] as XmlSchemaElement;
  1052. if (elem == null) return false;
  1053. if (elem.RefName != new XmlQualifiedName ("schema",XmlSchema.Namespace)) return false;
  1054. return (seq.Items[1] is XmlSchemaAny);
  1055. }
  1056. XmlTypeMapping ImportXmlSerializableMapping (string ns)
  1057. {
  1058. XmlQualifiedName qname = new XmlQualifiedName ("System.Data.DataSet",ns);
  1059. XmlTypeMapping map = mappedTypes [qname] as XmlTypeMapping;
  1060. if (map != null) return map;
  1061. TypeData typeData = new TypeData ("System.Data.DataSet", "System.Data.DataSet", "System.Data.DataSet", SchemaTypes.XmlSerializable, null);
  1062. map = new XmlTypeMapping ("System.Data.DataSet", "", typeData, "System.Data.DataSet", ns);
  1063. map.IncludeInSchema = true;
  1064. mappedTypes [qname] = map;
  1065. dataMappedTypes [typeData] = map;
  1066. return map;
  1067. }
  1068. XmlTypeMapElementInfo CreateElementInfo (string ns, XmlTypeMapMember member, string name, TypeData typeData, bool isNillable, XmlSchemaForm form)
  1069. {
  1070. if (typeData.IsComplexType)
  1071. return CreateElementInfo (ns, member, name, typeData, isNillable, form, GetTypeMapping (typeData));
  1072. else
  1073. return CreateElementInfo (ns, member, name, typeData, isNillable, form, null);
  1074. }
  1075. XmlTypeMapElementInfo CreateElementInfo (string ns, XmlTypeMapMember member, string name, TypeData typeData, bool isNillable, XmlSchemaForm form, XmlTypeMapping emap)
  1076. {
  1077. XmlTypeMapElementInfo einfo = new XmlTypeMapElementInfo (member, typeData);
  1078. einfo.ElementName = name;
  1079. einfo.Namespace = ns;
  1080. einfo.IsNullable = isNillable;
  1081. einfo.Form = form;
  1082. if (typeData.IsComplexType)
  1083. einfo.MappedType = emap;
  1084. return einfo;
  1085. }
  1086. XmlTypeMapElementInfo CreateTextElementInfo (string ns, XmlTypeMapMember member, TypeData typeData)
  1087. {
  1088. XmlTypeMapElementInfo einfo = new XmlTypeMapElementInfo (member, typeData);
  1089. einfo.IsTextElement = true;
  1090. einfo.WrappedElement = false;
  1091. if (typeData.IsComplexType)
  1092. einfo.MappedType = GetTypeMapping (typeData);
  1093. return einfo;
  1094. }
  1095. XmlTypeMapping CreateTypeMapping (XmlQualifiedName typeQName, SchemaTypes schemaType, XmlQualifiedName root)
  1096. {
  1097. string typeName = CodeIdentifier.MakeValid (typeQName.Name);
  1098. typeName = typeIdentifiers.AddUnique (typeName, null);
  1099. TypeData typeData = new TypeData (typeName, typeName, typeName, schemaType, null);
  1100. string rootElem;
  1101. string rootNs;
  1102. if (root != null) {
  1103. rootElem = root.Name;
  1104. rootNs = root.Namespace;
  1105. }
  1106. else {
  1107. rootElem = typeQName.Name;
  1108. rootNs = "";
  1109. }
  1110. XmlTypeMapping map = new XmlTypeMapping (rootElem, rootNs, typeData, typeQName.Name, typeQName.Namespace);
  1111. map.IncludeInSchema = true;
  1112. mappedTypes [typeQName] = map;
  1113. dataMappedTypes [typeData] = map;
  1114. return map;
  1115. }
  1116. XmlTypeMapping CreateArrayTypeMapping (XmlQualifiedName typeQName, TypeData arrayTypeData)
  1117. {
  1118. XmlTypeMapping map;
  1119. if (encodedFormat) map = new XmlTypeMapping ("Array", XmlSerializer.EncodingNamespace, arrayTypeData, "Array", XmlSerializer.EncodingNamespace);
  1120. else map = new XmlTypeMapping (arrayTypeData.XmlType, typeQName.Namespace, arrayTypeData, arrayTypeData.XmlType, typeQName.Namespace);
  1121. map.IncludeInSchema = true;
  1122. mappedTypes [typeQName] = map;
  1123. dataMappedTypes [arrayTypeData] = map;
  1124. return map;
  1125. }
  1126. XmlSchemaElement GetRefElement (XmlQualifiedName typeQName, XmlSchemaElement elem, out string ns)
  1127. {
  1128. if (!elem.RefName.IsEmpty)
  1129. {
  1130. ns = elem.RefName.Namespace;
  1131. return FindRefElement (elem);
  1132. }
  1133. else
  1134. {
  1135. ns = typeQName.Namespace;
  1136. return elem;
  1137. }
  1138. }
  1139. XmlSchemaAttribute GetRefAttribute (XmlQualifiedName typeQName, XmlSchemaAttribute attr, out string ns)
  1140. {
  1141. if (!attr.RefName.IsEmpty)
  1142. {
  1143. ns = attr.RefName.Namespace;
  1144. XmlSchemaAttribute at = FindRefAttribute (attr.RefName);
  1145. if (at == null) throw new InvalidOperationException ("The attribute " + attr.RefName + " is missing");
  1146. return at;
  1147. }
  1148. else
  1149. {
  1150. ns = typeQName.Namespace;
  1151. return attr;
  1152. }
  1153. }
  1154. TypeData GetElementTypeData (XmlQualifiedName typeQName, XmlSchemaElement elem, out XmlTypeMapping map)
  1155. {
  1156. bool sharedAnnType = false;
  1157. XmlQualifiedName root = null;
  1158. map = null;
  1159. if (!elem.RefName.IsEmpty) {
  1160. XmlSchemaElement refElem = FindRefElement (elem);
  1161. if (refElem == null) throw new InvalidOperationException ("Global element not found: " + elem.RefName);
  1162. root = elem.RefName;
  1163. elem = refElem;
  1164. sharedAnnType = true;
  1165. }
  1166. TypeData td;
  1167. if (!elem.SchemaTypeName.IsEmpty) {
  1168. td = GetTypeData (elem.SchemaTypeName, root);
  1169. map = GetRegisteredTypeMapping (elem.SchemaTypeName);
  1170. }
  1171. else if (elem.SchemaType == null)
  1172. td = TypeTranslator.GetTypeData (typeof(object));
  1173. else
  1174. td = GetTypeData (elem.SchemaType, typeQName, elem.Name, sharedAnnType, root);
  1175. if (map == null && td.IsComplexType)
  1176. map = GetTypeMapping (td);
  1177. return td;
  1178. }
  1179. TypeData GetAttributeTypeData (XmlQualifiedName typeQName, XmlSchemaAttribute attr)
  1180. {
  1181. bool sharedAnnType = false;
  1182. if (!attr.RefName.IsEmpty) {
  1183. XmlSchemaAttribute refAtt = FindRefAttribute (attr.RefName);
  1184. if (refAtt == null) throw new InvalidOperationException ("Global attribute not found: " + attr.RefName);
  1185. attr = refAtt;
  1186. sharedAnnType = true;
  1187. }
  1188. if (!attr.SchemaTypeName.IsEmpty) return GetTypeData (attr.SchemaTypeName, null);
  1189. if (attr.SchemaType == null) return TypeTranslator.GetTypeData (typeof(string));
  1190. else return GetTypeData (attr.SchemaType, typeQName, attr.Name, sharedAnnType, null);
  1191. }
  1192. TypeData GetTypeData (XmlQualifiedName typeQName, XmlQualifiedName root)
  1193. {
  1194. if (typeQName.Namespace == XmlSchema.Namespace || (encodedFormat && typeQName.Namespace == ""))
  1195. return TypeTranslator.GetPrimitiveTypeData (typeQName.Name);
  1196. return ImportType (typeQName, root).TypeData;
  1197. }
  1198. TypeData GetTypeData (XmlSchemaType stype, XmlQualifiedName typeQNname, string propertyName, bool sharedAnnType, XmlQualifiedName root)
  1199. {
  1200. string baseName;
  1201. if (sharedAnnType)
  1202. {
  1203. // Anonymous types defined in root elements or attributes can be shared among all elements that
  1204. // reference this root element or attribute
  1205. TypeData std = sharedAnonymousTypes [stype] as TypeData;
  1206. if (std != null) return std;
  1207. baseName = propertyName;
  1208. }
  1209. else
  1210. baseName = typeQNname.Name + typeIdentifiers.MakeRightCase (propertyName);
  1211. baseName = elemIdentifiers.AddUnique (baseName, stype);
  1212. XmlQualifiedName newName;
  1213. newName = new XmlQualifiedName (baseName, typeQNname.Namespace);
  1214. XmlTypeMapping map = ImportType (newName, stype, root);
  1215. if (sharedAnnType) sharedAnonymousTypes [stype] = map.TypeData;
  1216. return map.TypeData;
  1217. }
  1218. XmlTypeMapping GetTypeMapping (TypeData typeData)
  1219. {
  1220. XmlTypeMapping map = (XmlTypeMapping) dataMappedTypes [typeData];
  1221. if (map != null) return map;
  1222. if (map == null && typeData.IsListType)
  1223. {
  1224. // Create an array map for the type
  1225. XmlTypeMapping itemMap = GetTypeMapping (typeData.ListItemTypeData);
  1226. map = new XmlTypeMapping (typeData.XmlType, itemMap.Namespace, typeData, typeData.XmlType, itemMap.Namespace);
  1227. map.IncludeInSchema = true;
  1228. ListMap listMap = new ListMap ();
  1229. listMap.ItemInfo = new XmlTypeMapElementInfoList();
  1230. listMap.ItemInfo.Add (CreateElementInfo (itemMap.Namespace, null, typeData.ListItemTypeData.XmlType, typeData.ListItemTypeData, false, XmlSchemaForm.None));
  1231. map.ObjectMap = listMap;
  1232. mappedTypes [new XmlQualifiedName(map.ElementName, map.Namespace)] = map;
  1233. dataMappedTypes [typeData] = map;
  1234. return map;
  1235. }
  1236. else if (typeData.SchemaType == SchemaTypes.Primitive || typeData.Type == typeof(object) || typeof(XmlNode).IsAssignableFrom(typeData.Type))
  1237. {
  1238. map = new XmlTypeMapping (typeData.XmlType, XmlSchema.Namespace, typeData, typeData.XmlType, XmlSchema.Namespace);
  1239. map.IncludeInSchema = false;
  1240. map.ObjectMap = new ClassMap ();
  1241. dataMappedTypes [typeData] = map;
  1242. if (typeData.Type == typeof(object))
  1243. {
  1244. // All complex types are subtypes of anyType, so all of them
  1245. // must also be imported
  1246. foreach (XmlSchema schema in schemas) {
  1247. foreach (XmlSchemaObject sob in schema.Items)
  1248. {
  1249. XmlSchemaComplexType sct = sob as XmlSchemaComplexType;
  1250. if (sct != null)
  1251. ImportType (new XmlQualifiedName (sct.Name, schema.TargetNamespace), sct, null);
  1252. }
  1253. }
  1254. }
  1255. return map;
  1256. }
  1257. throw new InvalidOperationException ("Map for type " + typeData.TypeName + " not found");
  1258. }
  1259. XmlTypeMapping GetRegisteredTypeMapping (XmlQualifiedName typeQName)
  1260. {
  1261. return (XmlTypeMapping) mappedTypes [typeQName];
  1262. }
  1263. XmlSchemaParticle GetRefGroupParticle (XmlSchemaGroupRef refGroup)
  1264. {
  1265. XmlSchemaGroup grp = (XmlSchemaGroup) schemas.Find (refGroup.RefName, typeof (XmlSchemaGroup));
  1266. return grp.Particle;
  1267. }
  1268. XmlSchemaElement FindRefElement (XmlSchemaElement elem)
  1269. {
  1270. if (elem.RefName.Namespace == XmlSchema.Namespace)
  1271. {
  1272. if (anyElement != null) return anyElement;
  1273. anyElement = new XmlSchemaElement ();
  1274. anyElement.Name = "any";
  1275. anyElement.SchemaTypeName = anyType;
  1276. return anyElement;
  1277. }
  1278. return (XmlSchemaElement) schemas.Find (elem.RefName, typeof(XmlSchemaElement));
  1279. }
  1280. XmlSchemaAttribute FindRefAttribute (XmlQualifiedName refName)
  1281. {
  1282. if (refName.Namespace == XmlNamespace)
  1283. {
  1284. XmlSchemaAttribute at = new XmlSchemaAttribute ();
  1285. at.Name = refName.Name;
  1286. at.SchemaTypeName = new XmlQualifiedName ("string",XmlSchema.Namespace);
  1287. return at;
  1288. }
  1289. return (XmlSchemaAttribute) schemas.Find (refName, typeof(XmlSchemaAttribute));
  1290. }
  1291. XmlSchemaAttributeGroup FindRefAttributeGroup (XmlQualifiedName refName)
  1292. {
  1293. XmlSchemaAttributeGroup grp = (XmlSchemaAttributeGroup) schemas.Find (refName, typeof(XmlSchemaAttributeGroup));
  1294. foreach (XmlSchemaObject at in grp.Attributes)
  1295. {
  1296. if (at is XmlSchemaAttributeGroupRef && ((XmlSchemaAttributeGroupRef)at).RefName == refName)
  1297. throw new InvalidOperationException ("Cannot import attribute group '" + refName.Name + "' from namespace '" + refName.Namespace + "'. Redefine not supported");
  1298. }
  1299. return grp;
  1300. }
  1301. string GetDocumentation (XmlSchemaAnnotated elem)
  1302. {
  1303. string res = "";
  1304. XmlSchemaAnnotation anot = elem.Annotation;
  1305. if (anot == null || anot.Items == null) return null;
  1306. foreach (object ob in anot.Items)
  1307. {
  1308. XmlSchemaDocumentation doc = ob as XmlSchemaDocumentation;
  1309. if (doc != null && doc.Markup != null && doc.Markup.Length > 0) {
  1310. if (res != string.Empty) res += "\n";
  1311. foreach (XmlNode node in doc.Markup)
  1312. res += node.Value;
  1313. }
  1314. }
  1315. return res;
  1316. }
  1317. #endregion // Methods
  1318. }
  1319. }