XmlSchemaImporter.cs 60 KB

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