XmlSchemaImporter.cs 63 KB

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