XmlSchemaImporter.cs 61 KB

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