XmlSerializationReader.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  1. //
  2. // System.Xml.Serialization.XmlSerializationReader.cs
  3. //
  4. // Authors:
  5. // Tim Coleman ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. // Lluis Sanchez Gual ([email protected])
  8. //
  9. // Copyright (C) Tim Coleman, 2002
  10. // (c) 2002 Ximian, Inc. (http://www.ximian.com)
  11. //
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System;
  33. using System.Collections;
  34. using System.Globalization;
  35. using System.Xml;
  36. using System.Xml.Schema;
  37. using System.Reflection;
  38. namespace System.Xml.Serialization
  39. {
  40. public abstract class XmlSerializationReader
  41. #if NET_2_0
  42. : XmlSerializationGeneratedCode
  43. #endif
  44. {
  45. #region Fields
  46. XmlDocument document;
  47. XmlReader reader;
  48. ArrayList fixups;
  49. Hashtable collFixups;
  50. ArrayList collItemFixups;
  51. Hashtable typesCallbacks;
  52. ArrayList noIDTargets;
  53. Hashtable targets;
  54. Hashtable delayedListFixups;
  55. XmlSerializer eventSource;
  56. int delayedFixupId = 0;
  57. Hashtable referencedObjects;
  58. string w3SchemaNS;
  59. string w3InstanceNS;
  60. string w3InstanceNS2000;
  61. string w3InstanceNS1999;
  62. string soapNS;
  63. string wsdlNS;
  64. string nullX;
  65. string nil;
  66. string typeX;
  67. string arrayType;
  68. XmlQualifiedName arrayQName;
  69. #endregion
  70. internal void Initialize (XmlReader reader, XmlSerializer eventSource)
  71. {
  72. w3SchemaNS = reader.NameTable.Add (XmlSchema.Namespace);
  73. w3InstanceNS = reader.NameTable.Add (XmlSchema.InstanceNamespace);
  74. w3InstanceNS2000 = reader.NameTable.Add ("http://www.w3.org/2000/10/XMLSchema-instance");
  75. w3InstanceNS1999 = reader.NameTable.Add ("http://www.w3.org/1999/XMLSchema-instance");
  76. soapNS = reader.NameTable.Add (XmlSerializer.EncodingNamespace);
  77. wsdlNS = reader.NameTable.Add (XmlSerializer.WsdlNamespace);
  78. nullX = reader.NameTable.Add ("null");
  79. nil = reader.NameTable.Add ("nil");
  80. typeX = reader.NameTable.Add ("type");
  81. arrayType = reader.NameTable.Add ("arrayType");
  82. this.reader = reader;
  83. this.eventSource = eventSource;
  84. arrayQName = new XmlQualifiedName ("Array", soapNS);
  85. InitIDs ();
  86. }
  87. private ArrayList EnsureArrayList (ArrayList list)
  88. {
  89. if (list == null)
  90. list = new ArrayList ();
  91. return list;
  92. }
  93. private Hashtable EnsureHashtable (Hashtable hash)
  94. {
  95. if (hash == null)
  96. hash = new Hashtable ();
  97. return hash;
  98. }
  99. protected XmlSerializationReader ()
  100. {
  101. }
  102. protected XmlDocument Document
  103. {
  104. get {
  105. if (document == null)
  106. document = new XmlDocument (reader.NameTable);
  107. return document;
  108. }
  109. }
  110. protected XmlReader Reader {
  111. get { return reader; }
  112. }
  113. [MonoTODO]
  114. protected bool IsReturnValue
  115. {
  116. get {
  117. throw new NotImplementedException ();
  118. }
  119. set {
  120. throw new NotImplementedException ();
  121. }
  122. }
  123. #region Methods
  124. protected void AddFixup (CollectionFixup fixup)
  125. {
  126. collFixups = EnsureHashtable (collFixups);
  127. collFixups [fixup.Id] = fixup;
  128. if (delayedListFixups != null && delayedListFixups.ContainsKey (fixup.Id)) {
  129. fixup.CollectionItems = delayedListFixups [fixup.Id];
  130. delayedListFixups.Remove (fixup.Id);
  131. }
  132. }
  133. protected void AddFixup (Fixup fixup)
  134. {
  135. fixups = EnsureArrayList (fixups);
  136. fixups.Add (fixup);
  137. }
  138. void AddFixup (CollectionItemFixup fixup)
  139. {
  140. collItemFixups = EnsureArrayList (collItemFixups);
  141. collItemFixups.Add(fixup);
  142. }
  143. protected void AddReadCallback (string name, string ns, Type type, XmlSerializationReadCallback read)
  144. {
  145. WriteCallbackInfo info = new WriteCallbackInfo ();
  146. info.Type = type;
  147. info.TypeName = name;
  148. info.TypeNs = ns;
  149. info.Callback = read;
  150. typesCallbacks = EnsureHashtable (typesCallbacks);
  151. typesCallbacks.Add (new XmlQualifiedName (name, ns), info);
  152. }
  153. protected void AddTarget (string id, object o)
  154. {
  155. if (id != null) {
  156. targets = EnsureHashtable (targets);
  157. if (targets [id] == null)
  158. targets.Add (id, o);
  159. } else {
  160. if (o != null)
  161. return;
  162. noIDTargets = EnsureArrayList (noIDTargets);
  163. noIDTargets.Add (o);
  164. }
  165. }
  166. private string CurrentTag ()
  167. {
  168. switch (reader.NodeType) {
  169. case XmlNodeType.Element:
  170. return String.Format ("<{0} xmlns='{1}'>", reader.LocalName,
  171. reader.NamespaceURI);
  172. case XmlNodeType.Attribute:
  173. return reader.Value;
  174. case XmlNodeType.Text:
  175. return "CDATA";
  176. case XmlNodeType.ProcessingInstruction:
  177. return "<--";
  178. case XmlNodeType.Entity:
  179. return "<?";
  180. case XmlNodeType.EndElement:
  181. return ">";
  182. default:
  183. return "(unknown)";
  184. }
  185. }
  186. protected Exception CreateCtorHasSecurityException (string typeName)
  187. {
  188. string message = string.Format ("The type '{0}' cannot"
  189. + " be serialized because its parameterless"
  190. + " constructor is decorated with declarative"
  191. + " security permission attributes."
  192. + " Consider using imperative asserts or demands"
  193. + " in the constructor.", typeName);
  194. return new InvalidOperationException (message);
  195. }
  196. protected Exception CreateInaccessibleConstructorException (string typeName)
  197. {
  198. string message = string.Format ("{0} cannot be serialized"
  199. + " because it does not have a default public"
  200. + " constructor.", typeName);
  201. return new InvalidOperationException (message);
  202. }
  203. protected Exception CreateAbstractTypeException (string name, string ns)
  204. {
  205. string message = "The specified type is abstrace: name='" + name + "' namespace='" + ns + "', at " + CurrentTag ();
  206. return new InvalidOperationException (message);
  207. }
  208. protected Exception CreateInvalidCastException (Type type, object value)
  209. {
  210. string message = String.Format (CultureInfo.InvariantCulture, "Cannot assign object of type {0} to an object of " +
  211. "type {1}.", value.GetType (), type);
  212. return new InvalidCastException (message);
  213. }
  214. protected Exception CreateReadOnlyCollectionException (string name)
  215. {
  216. string message = String.Format ("Could not serialize {0}. Default constructors are " +
  217. "required for collections and enumerators.", name);
  218. return new InvalidOperationException (message);
  219. }
  220. protected Exception CreateUnknownConstantException (string value, Type enumType)
  221. {
  222. string message = String.Format ("'{0}' is not a valid value for {1}.", value, enumType);
  223. return new InvalidOperationException (message);
  224. }
  225. protected Exception CreateUnknownNodeException ()
  226. {
  227. string message = CurrentTag () + " was not expected";
  228. return new InvalidOperationException (message);
  229. }
  230. protected Exception CreateUnknownTypeException (XmlQualifiedName type)
  231. {
  232. string message = "The specified type was not recognized: name='" + type.Name + "' namespace='" + type.Namespace + "', at " + CurrentTag ();
  233. return new InvalidOperationException (message);
  234. }
  235. protected Array EnsureArrayIndex (Array a, int index, Type elementType)
  236. {
  237. if (a != null && index < a.Length)
  238. return a;
  239. int size;
  240. if (a == null) {
  241. size = 32;
  242. } else {
  243. size = a.Length * 2;
  244. }
  245. Array result = Array.CreateInstance (elementType, size);
  246. if (a != null)
  247. Array.Copy (a, result, index);
  248. return result;
  249. }
  250. [MonoTODO ("Implement")]
  251. protected void FixupArrayRefs (object fixup)
  252. {
  253. throw new NotImplementedException ();
  254. }
  255. [MonoTODO ("Implement")]
  256. protected int GetArrayLength (string name, string ns)
  257. {
  258. throw new NotImplementedException ();
  259. }
  260. protected bool GetNullAttr ()
  261. {
  262. string na = reader.GetAttribute (nullX, w3InstanceNS);
  263. if (na == null) {
  264. na = reader.GetAttribute (nil, w3InstanceNS);
  265. if (na == null) {
  266. na = reader.GetAttribute (nullX, w3InstanceNS2000);
  267. if (na == null)
  268. na = reader.GetAttribute (nullX, w3InstanceNS1999);
  269. }
  270. }
  271. return (na != null);
  272. }
  273. protected object GetTarget (string id)
  274. {
  275. if (targets == null) return null;
  276. object ob = targets [id];
  277. if (ob != null) {
  278. if (referencedObjects == null) referencedObjects = new Hashtable ();
  279. referencedObjects [ob] = ob;
  280. }
  281. return ob;
  282. }
  283. bool TargetReady (string id)
  284. {
  285. if (targets == null) return false;
  286. return targets.ContainsKey (id);
  287. }
  288. protected XmlQualifiedName GetXsiType ()
  289. {
  290. string typeName = Reader.GetAttribute (typeX, XmlSchema.InstanceNamespace);
  291. if (typeName == string.Empty || typeName == null) {
  292. typeName = Reader.GetAttribute (typeX, w3InstanceNS1999);
  293. if (typeName == string.Empty || typeName == null) {
  294. typeName = Reader.GetAttribute (typeX, w3InstanceNS2000);
  295. if (typeName == string.Empty || typeName == null)
  296. return null;
  297. }
  298. }
  299. int i = typeName.IndexOf (":");
  300. if (i == -1) return new XmlQualifiedName (typeName, Reader.NamespaceURI);
  301. else
  302. {
  303. string prefix = typeName.Substring(0,i);
  304. string name = typeName.Substring (i+1);
  305. return new XmlQualifiedName (name, Reader.LookupNamespace (prefix));
  306. }
  307. }
  308. protected abstract void InitCallbacks ();
  309. protected abstract void InitIDs ();
  310. protected bool IsXmlnsAttribute (string name)
  311. {
  312. int length = name.Length;
  313. if (length < 5)
  314. return false;
  315. if (length == 5)
  316. return (name == "xmlns");
  317. return name.StartsWith ("xmlns:");
  318. }
  319. protected void ParseWsdlArrayType (XmlAttribute attr)
  320. {
  321. if (attr.NamespaceURI == wsdlNS && attr.LocalName == arrayType)
  322. {
  323. string ns = "", type, dimensions;
  324. TypeTranslator.ParseArrayType (attr.Value, out type, out ns, out dimensions);
  325. if (ns != "") ns = Reader.LookupNamespace (ns) + ":";
  326. attr.Value = ns + type + dimensions;
  327. }
  328. }
  329. protected XmlQualifiedName ReadElementQualifiedName ()
  330. {
  331. if (reader.IsEmptyElement) {
  332. reader.Skip();
  333. return ToXmlQualifiedName (String.Empty);
  334. }
  335. reader.ReadStartElement ();
  336. XmlQualifiedName xqn = ToXmlQualifiedName(reader.ReadString ());
  337. reader.ReadEndElement ();
  338. return xqn;
  339. }
  340. protected void ReadEndElement ()
  341. {
  342. while (reader.NodeType == XmlNodeType.Whitespace)
  343. reader.Skip ();
  344. if (reader.NodeType != XmlNodeType.None) {
  345. reader.ReadEndElement ();
  346. } else {
  347. reader.Skip ();
  348. }
  349. }
  350. protected bool ReadNull ()
  351. {
  352. if (!GetNullAttr ())
  353. return false;
  354. if (reader.IsEmptyElement) {
  355. reader.Skip();
  356. return true;
  357. }
  358. reader.ReadStartElement();
  359. while (reader.NodeType != XmlNodeType.EndElement)
  360. UnknownNode (null);
  361. ReadEndElement ();
  362. return true;
  363. }
  364. protected XmlQualifiedName ReadNullableQualifiedName ()
  365. {
  366. if (ReadNull ())
  367. return null;
  368. return ReadElementQualifiedName ();
  369. }
  370. protected string ReadNullableString ()
  371. {
  372. if (ReadNull ())
  373. return null;
  374. return reader.ReadElementString ();
  375. }
  376. protected bool ReadReference (out string fixupReference)
  377. {
  378. string href = reader.GetAttribute ("href");
  379. if (href == null) {
  380. fixupReference = null;
  381. return false;
  382. }
  383. if (href [0] != '#')
  384. throw new InvalidOperationException("href not found: " + href);
  385. fixupReference = href.Substring (1);
  386. if (!reader.IsEmptyElement) {
  387. reader.ReadStartElement ();
  388. ReadEndElement ();
  389. } else {
  390. reader.Skip ();
  391. }
  392. return true;
  393. }
  394. protected object ReadReferencedElement ()
  395. {
  396. return ReadReferencedElement (Reader.LocalName, Reader.NamespaceURI);
  397. }
  398. WriteCallbackInfo GetCallbackInfo (XmlQualifiedName qname)
  399. {
  400. if (typesCallbacks == null)
  401. {
  402. typesCallbacks = new Hashtable ();
  403. InitCallbacks ();
  404. }
  405. return (WriteCallbackInfo) typesCallbacks[qname];
  406. }
  407. protected object ReadReferencedElement (string name, string ns)
  408. {
  409. XmlQualifiedName qname = GetXsiType ();
  410. if (qname == null) qname = new XmlQualifiedName (name, ns);
  411. string id = Reader.GetAttribute ("id");
  412. object ob;
  413. if (qname == arrayQName)
  414. {
  415. CollectionFixup fixup = (collFixups != null) ? (CollectionFixup) collFixups[id] : null;
  416. if (ReadList (out ob))
  417. {
  418. // List complete (does not contain references)
  419. if (fixup != null)
  420. {
  421. fixup.Callback (fixup.Collection, ob);
  422. collFixups.Remove (id);
  423. ob = fixup.Collection;
  424. }
  425. }
  426. else if (fixup != null)
  427. {
  428. fixup.CollectionItems = (object[])ob;
  429. ob = fixup.Collection;
  430. }
  431. }
  432. else
  433. {
  434. WriteCallbackInfo info = GetCallbackInfo (qname);
  435. if (info == null)
  436. ob = ReadTypedPrimitive (qname, id != null);
  437. else
  438. ob = info.Callback();
  439. }
  440. AddTarget (id, ob);
  441. return ob;
  442. }
  443. bool ReadList (out object resultList)
  444. {
  445. string arrayTypeAttr = Reader.GetAttribute (arrayType, soapNS);
  446. if (arrayTypeAttr == null) arrayTypeAttr = Reader.GetAttribute (arrayType, wsdlNS);
  447. XmlQualifiedName qn = ToXmlQualifiedName (arrayTypeAttr);
  448. int i = qn.Name.LastIndexOf ('[');
  449. string dim = qn.Name.Substring (i);
  450. string itemType = qn.Name.Substring (0,i);
  451. int count = Int32.Parse (dim.Substring (1, dim.Length - 2), CultureInfo.InvariantCulture);
  452. Array list;
  453. i = itemType.IndexOf ('['); if (i == -1) i = itemType.Length;
  454. string baseType = itemType.Substring (0,i);
  455. string arrayTypeName;
  456. if (qn.Namespace == w3SchemaNS)
  457. arrayTypeName = TypeTranslator.GetPrimitiveTypeData (baseType).Type.FullName + itemType.Substring (i);
  458. else
  459. {
  460. WriteCallbackInfo info = GetCallbackInfo (new XmlQualifiedName (baseType,qn.Namespace));
  461. arrayTypeName = info.Type.FullName + itemType.Substring (i) + ", " + info.Type.Assembly.FullName;
  462. }
  463. list = Array.CreateInstance (Type.GetType (arrayTypeName), count);
  464. bool listComplete = true;
  465. if (Reader.IsEmptyElement)
  466. Reader.Skip ();
  467. else {
  468. Reader.ReadStartElement ();
  469. for (int n=0; n<count; n++)
  470. {
  471. Reader.MoveToContent ();
  472. string id;
  473. object item = ReadReferencingElement (itemType, qn.Namespace, out id);
  474. if (id == null)
  475. list.SetValue (item,n);
  476. else
  477. {
  478. AddFixup (new CollectionItemFixup (list, n, id));
  479. listComplete = false;
  480. }
  481. }
  482. Reader.ReadEndElement ();
  483. }
  484. resultList = list;
  485. return listComplete;
  486. }
  487. protected void ReadReferencedElements ()
  488. {
  489. reader.MoveToContent();
  490. XmlNodeType nt = reader.NodeType;
  491. while (nt != XmlNodeType.EndElement && nt != XmlNodeType.None) {
  492. ReadReferencedElement ();
  493. reader.MoveToContent ();
  494. nt = reader.NodeType;
  495. }
  496. // Registers delayed list
  497. if (delayedListFixups != null)
  498. {
  499. foreach (DictionaryEntry entry in delayedListFixups)
  500. AddTarget ((string)entry.Key, entry.Value);
  501. }
  502. // Fix arrays
  503. if (collItemFixups != null)
  504. {
  505. foreach (CollectionItemFixup itemFixup in collItemFixups)
  506. itemFixup.Collection.SetValue (GetTarget (itemFixup.Id), itemFixup.Index);
  507. }
  508. // Fills collections
  509. if (collFixups != null)
  510. {
  511. ICollection cfixups = collFixups.Values;
  512. foreach (CollectionFixup fixup in cfixups)
  513. fixup.Callback (fixup.Collection, fixup.CollectionItems);
  514. }
  515. // Fills class instances
  516. if (fixups != null)
  517. {
  518. foreach (Fixup fixup in fixups)
  519. fixup.Callback (fixup);
  520. }
  521. if (targets != null) {
  522. foreach (DictionaryEntry e in targets) {
  523. if (e.Value != null && (referencedObjects == null || !referencedObjects.Contains (e.Value)))
  524. UnreferencedObject ((string)e.Key, e.Value);
  525. }
  526. }
  527. }
  528. protected object ReadReferencingElement (out string fixupReference)
  529. {
  530. return ReadReferencingElement (Reader.LocalName, Reader.NamespaceURI, false, out fixupReference);
  531. }
  532. protected object ReadReferencingElement (string name, string ns, out string fixupReference)
  533. {
  534. return ReadReferencingElement (name, ns, false, out fixupReference);
  535. }
  536. protected object ReadReferencingElement (string name,
  537. string ns,
  538. bool elementCanBeType,
  539. out string fixupReference)
  540. {
  541. if (ReadNull ())
  542. {
  543. fixupReference = null;
  544. return null;
  545. }
  546. string refid = Reader.GetAttribute ("href");
  547. if (refid == string.Empty || refid == null)
  548. {
  549. fixupReference = null;
  550. XmlQualifiedName qname = GetXsiType ();
  551. if (qname == null) qname = new XmlQualifiedName (name, ns);
  552. string arrayTypeAttr = Reader.GetAttribute (arrayType, soapNS);
  553. if (qname == arrayQName || arrayTypeAttr != null)
  554. {
  555. delayedListFixups = EnsureHashtable (delayedListFixups);
  556. fixupReference = "__<" + (delayedFixupId++) + ">";
  557. object items;
  558. ReadList (out items);
  559. delayedListFixups [fixupReference] = items;
  560. return null;
  561. }
  562. else
  563. {
  564. WriteCallbackInfo info = GetCallbackInfo (qname);
  565. if (info == null)
  566. return ReadTypedPrimitive (qname, true);
  567. else
  568. return info.Callback();
  569. }
  570. }
  571. else
  572. {
  573. if (refid.StartsWith ("#")) refid = refid.Substring (1);
  574. Reader.Skip ();
  575. if (TargetReady (refid))
  576. {
  577. fixupReference = null;
  578. return GetTarget (refid);
  579. }
  580. else
  581. {
  582. fixupReference = refid;
  583. return null;
  584. }
  585. }
  586. }
  587. protected IXmlSerializable ReadSerializable (IXmlSerializable serializable)
  588. {
  589. if (ReadNull ()) return null;
  590. int depth = reader.Depth;
  591. serializable.ReadXml (reader);
  592. Reader.MoveToContent ();
  593. while (reader.Depth > depth)
  594. reader.Skip ();
  595. if (reader.Depth == depth && reader.NodeType == XmlNodeType.EndElement)
  596. reader.ReadEndElement ();
  597. return serializable;
  598. }
  599. protected string ReadString (string value)
  600. {
  601. if (value == null || value == String.Empty)
  602. return reader.ReadString ();
  603. return (value + reader.ReadString ());
  604. }
  605. protected object ReadTypedPrimitive (XmlQualifiedName qname)
  606. {
  607. return ReadTypedPrimitive (qname, false);
  608. }
  609. object ReadTypedPrimitive (XmlQualifiedName qname, bool reportUnknown)
  610. {
  611. if (qname == null) qname = GetXsiType ();
  612. TypeData typeData = TypeTranslator.FindPrimitiveTypeData (qname.Name);
  613. if (typeData == null || typeData.SchemaType != SchemaTypes.Primitive)
  614. {
  615. // Put everything into a node array
  616. XmlNode node = Document.ReadNode (reader);
  617. if (reportUnknown)
  618. OnUnknownNode (node, null);
  619. if (node.ChildNodes.Count == 0 && node.Attributes.Count == 0)
  620. return new Object ();
  621. XmlElement elem = node as XmlElement;
  622. if (elem == null)
  623. return new XmlNode[] {node};
  624. else {
  625. XmlNode[] nodes = new XmlNode[elem.Attributes.Count + elem.ChildNodes.Count];
  626. int n = 0;
  627. foreach (XmlNode no in elem.Attributes)
  628. nodes[n++] = no;
  629. foreach (XmlNode no in elem.ChildNodes)
  630. nodes[n++] = no;
  631. return nodes;
  632. }
  633. }
  634. if (typeData.Type == typeof (XmlQualifiedName)) return ReadNullableQualifiedName ();
  635. return XmlCustomFormatter.FromXmlString (typeData, Reader.ReadElementString ());
  636. }
  637. protected XmlNode ReadXmlNode (bool wrapped)
  638. {
  639. XmlNode node = Document.ReadNode (reader);
  640. if (wrapped)
  641. return node.FirstChild;
  642. else
  643. return node;
  644. }
  645. protected XmlDocument ReadXmlDocument (bool wrapped)
  646. {
  647. if (wrapped)
  648. reader.ReadStartElement ();
  649. XmlDocument doc = new XmlDocument ();
  650. XmlNode node = doc.ReadNode (reader);
  651. doc.AppendChild (node);
  652. if (wrapped)
  653. reader.ReadEndElement ();
  654. return doc;
  655. }
  656. protected void Referenced (object o)
  657. {
  658. if (o != null) {
  659. if (referencedObjects == null) referencedObjects = new Hashtable ();
  660. referencedObjects [o] = o;
  661. }
  662. }
  663. protected Array ShrinkArray (Array a, int length, Type elementType, bool isNullable)
  664. {
  665. if (length == 0 && isNullable) return null;
  666. if (a == null) return Array.CreateInstance (elementType, length);
  667. if (a.Length == length) return a;
  668. Array result = Array.CreateInstance (elementType, length);
  669. Array.Copy (a, result, length);
  670. return result;
  671. }
  672. protected byte[] ToByteArrayBase64 (bool isNull)
  673. {
  674. return Convert.FromBase64String (Reader.ReadString());
  675. }
  676. [MonoTODO ("Implement")]
  677. protected static byte[] ToByteArrayBase64 (string value)
  678. {
  679. throw new NotImplementedException ();
  680. }
  681. [MonoTODO ("Implement")]
  682. protected byte[] ToByteArrayHex (bool isNull)
  683. {
  684. throw new NotImplementedException ();
  685. }
  686. [MonoTODO ("Implement")]
  687. protected static byte[] ToByteArrayHex (string value)
  688. {
  689. throw new NotImplementedException ();
  690. }
  691. protected static char ToChar (string value)
  692. {
  693. return XmlCustomFormatter.ToChar (value);
  694. }
  695. protected static DateTime ToDate (string value)
  696. {
  697. return XmlCustomFormatter.ToDate (value);
  698. }
  699. protected static DateTime ToDateTime (string value)
  700. {
  701. return XmlCustomFormatter.ToDateTime (value);
  702. }
  703. protected static long ToEnum (string value, Hashtable h, string typeName)
  704. {
  705. return XmlCustomFormatter.ToEnum (value, h, typeName, true);
  706. }
  707. protected static DateTime ToTime (string value)
  708. {
  709. return XmlCustomFormatter.ToTime (value);
  710. }
  711. protected static string ToXmlName (string value)
  712. {
  713. return XmlCustomFormatter.ToXmlName (value);
  714. }
  715. protected static string ToXmlNCName (string value)
  716. {
  717. return XmlCustomFormatter.ToXmlNCName (value);
  718. }
  719. protected static string ToXmlNmToken (string value)
  720. {
  721. return XmlCustomFormatter.ToXmlNmToken (value);
  722. }
  723. protected static string ToXmlNmTokens (string value)
  724. {
  725. return XmlCustomFormatter.ToXmlNmTokens (value);
  726. }
  727. protected XmlQualifiedName ToXmlQualifiedName (string value)
  728. {
  729. string name;
  730. string ns;
  731. int lastColon = value.LastIndexOf (':');
  732. string decodedValue = XmlConvert.DecodeName (value);
  733. if (lastColon < 0) {
  734. name = reader.NameTable.Add (decodedValue);
  735. ns = reader.LookupNamespace (String.Empty);
  736. } else {
  737. string prefix = value.Substring (0, lastColon);
  738. ns = reader.LookupNamespace (prefix);
  739. if (ns == null)
  740. throw new InvalidOperationException ("namespace " + prefix + " not defined");
  741. name = reader.NameTable.Add (value.Substring (lastColon + 1));
  742. }
  743. return new XmlQualifiedName (name, ns);
  744. }
  745. protected void UnknownAttribute (object o, XmlAttribute attr)
  746. {
  747. int line_number, line_position;
  748. if (Reader is XmlTextReader){
  749. line_number = ((XmlTextReader)Reader).LineNumber;
  750. line_position = ((XmlTextReader)Reader).LinePosition;
  751. } else {
  752. line_number = 0;
  753. line_position = 0;
  754. }
  755. if (eventSource != null)
  756. eventSource.OnUnknownAttribute (new XmlAttributeEventArgs (attr, line_number, line_position, o));
  757. }
  758. protected void UnknownElement (object o, XmlElement elem)
  759. {
  760. int line_number, line_position;
  761. if (Reader is XmlTextReader){
  762. line_number = ((XmlTextReader)Reader).LineNumber;
  763. line_position = ((XmlTextReader)Reader).LinePosition;
  764. } else {
  765. line_number = 0;
  766. line_position = 0;
  767. }
  768. if (eventSource != null)
  769. eventSource.OnUnknownElement (new XmlElementEventArgs (elem, line_number, line_position,o));
  770. }
  771. protected void UnknownNode (object o)
  772. {
  773. OnUnknownNode (ReadXmlNode (false), o);
  774. }
  775. void OnUnknownNode (XmlNode node, object o)
  776. {
  777. int line_number, line_position;
  778. if (Reader is XmlTextReader){
  779. line_number = ((XmlTextReader)Reader).LineNumber;
  780. line_position = ((XmlTextReader)Reader).LinePosition;
  781. } else {
  782. line_number = 0;
  783. line_position = 0;
  784. }
  785. if (node is XmlAttribute)
  786. {
  787. UnknownAttribute (o, (XmlAttribute)node);
  788. return;
  789. }
  790. else if (node is XmlElement)
  791. {
  792. UnknownElement (o, (XmlElement) node);
  793. return;
  794. }
  795. else
  796. {
  797. if (eventSource != null)
  798. eventSource.OnUnknownNode (new XmlNodeEventArgs(line_number, line_position, node.LocalName, node.Name, node.NamespaceURI, node.NodeType, o, node.Value));
  799. if (Reader.ReadState == ReadState.EndOfFile)
  800. throw new InvalidOperationException ("End of document found");
  801. }
  802. }
  803. protected void UnreferencedObject (string id, object o)
  804. {
  805. if (eventSource != null)
  806. eventSource.OnUnreferencedObject (new UnreferencedObjectEventArgs (o,id));
  807. }
  808. #endregion // Methods
  809. class WriteCallbackInfo
  810. {
  811. public Type Type;
  812. public string TypeName;
  813. public string TypeNs;
  814. public XmlSerializationReadCallback Callback;
  815. }
  816. protected class CollectionFixup {
  817. XmlSerializationCollectionFixupCallback callback;
  818. object collection;
  819. object collectionItems;
  820. string id;
  821. public CollectionFixup (object collection, XmlSerializationCollectionFixupCallback callback, string id)
  822. {
  823. this.callback = callback;
  824. this.collection = collection;
  825. this.id = id;
  826. }
  827. public XmlSerializationCollectionFixupCallback Callback {
  828. get { return callback; }
  829. }
  830. public object Collection {
  831. get { return collection; }
  832. }
  833. public object Id {
  834. get { return id; }
  835. }
  836. internal object CollectionItems
  837. {
  838. get { return collectionItems; }
  839. set { collectionItems = value; }
  840. }
  841. }
  842. protected class Fixup {
  843. object source;
  844. string[] ids;
  845. XmlSerializationFixupCallback callback;
  846. public Fixup (object o, XmlSerializationFixupCallback callback, int count)
  847. {
  848. this.source = o;
  849. this.callback = callback;
  850. this.ids = new string[count];
  851. }
  852. public Fixup (object o, XmlSerializationFixupCallback callback, string[] ids)
  853. {
  854. this.source = o;
  855. this.ids = ids;
  856. this.callback = callback;
  857. }
  858. public XmlSerializationFixupCallback Callback {
  859. get { return callback; }
  860. }
  861. public string[] Ids {
  862. get { return ids; }
  863. }
  864. public object Source {
  865. get { return source; }
  866. set { source = value; }
  867. }
  868. }
  869. protected class CollectionItemFixup
  870. {
  871. Array list;
  872. int index;
  873. string id;
  874. public CollectionItemFixup (Array list, int index, string id)
  875. {
  876. this.list = list;
  877. this.index = index;
  878. this.id = id;
  879. }
  880. public Array Collection
  881. {
  882. get { return list; }
  883. }
  884. public int Index
  885. {
  886. get { return index; }
  887. }
  888. public string Id
  889. {
  890. get { return id; }
  891. }
  892. }
  893. #if NET_2_0
  894. [MonoTODO]
  895. protected bool DecodeName
  896. {
  897. get { throw new NotImplementedException(); }
  898. set { throw new NotImplementedException(); }
  899. }
  900. [MonoTODO]
  901. protected string CollapseWhitespace (string value)
  902. {
  903. throw new NotImplementedException ();
  904. }
  905. [MonoTODO]
  906. protected Exception CreateBadDeriveationException (
  907. string xsdDerived,
  908. string nsDerived,
  909. string xsdBase,
  910. string nsBase,
  911. string clrDerived,
  912. string clrBase)
  913. {
  914. throw new NotImplementedException ();
  915. }
  916. [MonoTODO]
  917. protected Exception CreateInvalidCastException (Type type, object value, string id)
  918. {
  919. throw new NotImplementedException ();
  920. }
  921. [MonoTODO]
  922. protected Exception CreateMissingIXmlSerializableType (string name, string ns, string clrType)
  923. {
  924. throw new NotImplementedException ();
  925. }
  926. [MonoTODO]
  927. protected IXmlSerializable ReadSerializable (IXmlSerializable serializable, bool wrapped)
  928. {
  929. throw new NotImplementedException ();
  930. }
  931. [MonoTODO]
  932. protected string ReadString (string value, bool trim)
  933. {
  934. throw new NotImplementedException ();
  935. }
  936. [MonoTODO]
  937. protected object ReadTypedNull (XmlQualifiedName type)
  938. {
  939. throw new NotImplementedException ();
  940. }
  941. [MonoTODO]
  942. protected static Assembly ResolveDynamicAssembly (string assemblyFullName)
  943. {
  944. throw new NotImplementedException ();
  945. }
  946. #endif
  947. }
  948. }