XmlSchemaImporter.cs 65 KB

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