XmlSchemaImporter.cs 57 KB

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