XmlSchemaImporter.cs 65 KB

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