XmlSchemaImporter.cs 68 KB

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