XmlSchemaImporter.cs 64 KB

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