XmlSchemaImporter.cs 60 KB

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