XmlSchemaImporter.cs 61 KB

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