XmlSchemaImporter.cs 60 KB

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