XmlSchemaImporter.cs 61 KB

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