2
0

XmlSchemaImporter.cs 59 KB

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