XmlSerializationReader.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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. using System;
  13. using System.Collections;
  14. using System.Xml;
  15. using System.Xml.Schema;
  16. namespace System.Xml.Serialization {
  17. public abstract class XmlSerializationReader {
  18. #region Fields
  19. XmlDocument document;
  20. XmlReader reader;
  21. ArrayList fixups;
  22. Hashtable collFixups;
  23. ArrayList collItemFixups;
  24. Hashtable typesCallbacks;
  25. ArrayList noIDTargets;
  26. Hashtable targets;
  27. Hashtable delayedListFixups;
  28. XmlSerializer eventSource;
  29. string w3SchemaNS;
  30. string w3SchemaNS2000;
  31. string w3SchemaNS1999;
  32. string w3InstanceNS;
  33. string w3InstanceNS2000;
  34. string w3InstanceNS1999;
  35. string soapNS;
  36. string schema;
  37. string wsdlNS;
  38. string wsdlArrayType;
  39. string nullX;
  40. string nil;
  41. string typeX;
  42. string arrayType;
  43. string anyType;
  44. XmlQualifiedName arrayQName;
  45. #endregion
  46. internal void Initialize (XmlReader reader, XmlSerializer eventSource)
  47. {
  48. w3SchemaNS = reader.NameTable.Add ("http://www.w3.org/2001/XMLSchema");
  49. w3SchemaNS2000 = reader.NameTable.Add ("http://www.w3.org/2000/10/XMLSchema");
  50. w3SchemaNS1999 = reader.NameTable.Add ("http://www.w3.org/1999/XMLSchema");
  51. w3InstanceNS = reader.NameTable.Add ("http://www.w3.org/2001/XMLSchema-instance");
  52. w3InstanceNS2000 = reader.NameTable.Add ("http://www.w3.org/2000/10/XMLSchema-instance");
  53. w3InstanceNS1999 = reader.NameTable.Add ("http://www.w3.org/1999/XMLSchema-instance");
  54. soapNS = reader.NameTable.Add ("http://schemas.xmlsoap.org/soap/encoding/");
  55. schema = reader.NameTable.Add ("schema");
  56. wsdlNS = reader.NameTable.Add ("http://schemas.xmlsoap.org/wsdl/");
  57. wsdlArrayType = reader.NameTable.Add ("arrayType");
  58. nullX = reader.NameTable.Add ("null");
  59. nil = reader.NameTable.Add ("nil");
  60. typeX = reader.NameTable.Add ("type");
  61. arrayType = reader.NameTable.Add ("arrayType");
  62. anyType = reader.NameTable.Add ("anyType");
  63. this.reader = reader;
  64. this.eventSource = eventSource;
  65. arrayQName = new XmlQualifiedName ("Array", soapNS);
  66. InitIDs ();
  67. }
  68. internal virtual object ReadObject ()
  69. {
  70. throw new NotImplementedException ();
  71. }
  72. private ArrayList EnsureArrayList (ArrayList list)
  73. {
  74. if (list == null)
  75. list = new ArrayList ();
  76. return list;
  77. }
  78. private Hashtable EnsureHashtable (Hashtable hash)
  79. {
  80. if (hash == null)
  81. hash = new Hashtable ();
  82. return hash;
  83. }
  84. protected XmlSerializationReader ()
  85. {
  86. }
  87. protected XmlDocument Document
  88. {
  89. get {
  90. if (document == null)
  91. document = new XmlDocument (reader.NameTable);
  92. return document;
  93. }
  94. }
  95. protected XmlReader Reader {
  96. get { return reader; }
  97. }
  98. #region Methods
  99. protected void AddFixup (CollectionFixup fixup)
  100. {
  101. collFixups = EnsureHashtable (collFixups);
  102. collFixups [fixup.Id] = fixup;
  103. if (delayedListFixups != null && delayedListFixups.ContainsKey (fixup.Id))
  104. fixup.CollectionItems = delayedListFixups [fixup.Id];
  105. }
  106. protected void AddFixup (Fixup fixup)
  107. {
  108. fixups = EnsureArrayList (fixups);
  109. fixups.Add (fixup);
  110. }
  111. void AddFixup (CollectionItemFixup fixup)
  112. {
  113. collItemFixups = EnsureArrayList (collItemFixups);
  114. collItemFixups.Add(fixup);
  115. }
  116. protected void AddReadCallback (string name, string ns, Type type, XmlSerializationReadCallback read)
  117. {
  118. WriteCallbackInfo info = new WriteCallbackInfo ();
  119. info.Type = type;
  120. info.TypeName = name;
  121. info.TypeNs = ns;
  122. info.Callback = read;
  123. typesCallbacks = EnsureHashtable (typesCallbacks);
  124. typesCallbacks.Add (new XmlQualifiedName (name, ns), info);
  125. }
  126. protected void AddTarget (string id, object o)
  127. {
  128. if (id != null) {
  129. targets = EnsureHashtable (targets);
  130. if (targets [id] == null)
  131. targets.Add (id, o);
  132. } else {
  133. if (o != null)
  134. return;
  135. noIDTargets = EnsureArrayList (noIDTargets);
  136. noIDTargets.Add (o);
  137. }
  138. }
  139. private string CurrentTag ()
  140. {
  141. switch (reader.NodeType) {
  142. case XmlNodeType.None:
  143. return String.Format ("<{0} xmlns='{1}'>", reader.LocalName,
  144. reader.NamespaceURI);
  145. case XmlNodeType.Attribute:
  146. return reader.Value;
  147. case XmlNodeType.Text:
  148. return "CDATA";
  149. case XmlNodeType.ProcessingInstruction:
  150. return "<--";
  151. case XmlNodeType.Entity:
  152. return "<?";
  153. case XmlNodeType.EndElement:
  154. return ">";
  155. default:
  156. return "(unknown)";
  157. }
  158. }
  159. protected Exception CreateAbstractTypeException (string name, string ns)
  160. {
  161. string message = "Error at " + name + " " + ns + ":" + CurrentTag ();
  162. return new InvalidOperationException (message);
  163. }
  164. protected Exception CreateInvalidCastException (Type type, object value)
  165. {
  166. string message = String.Format ("Cannot assign object of type {0} to an object of " +
  167. "type {1}.", value.GetType (), type);
  168. return new InvalidCastException (message);
  169. }
  170. protected Exception CreateReadOnlyCollectionException (string name)
  171. {
  172. string message = String.Format ("Could not serialize {0}. Default constructors are " +
  173. "required for collections and enumerators.", name);
  174. return new InvalidOperationException (message);
  175. }
  176. protected Exception CreateUnknownConstantException (string value, Type enumType)
  177. {
  178. string message = String.Format ("'{0}' is not a valid value for {1}.", value, enumType);
  179. return new InvalidOperationException (message);
  180. }
  181. protected Exception CreateUnknownNodeException ()
  182. {
  183. string message = "Unknown xml node -> " + CurrentTag ();
  184. return new InvalidOperationException (message);
  185. }
  186. protected Exception CreateUnknownTypeException (XmlQualifiedName type)
  187. {
  188. string message = "Unknown type " + type.Namespace + ":" + type.Name + " " + CurrentTag ();
  189. return new InvalidOperationException (message);
  190. }
  191. protected Array EnsureArrayIndex (Array a, int index, Type elementType)
  192. {
  193. if (a != null && index < a.Length)
  194. return a;
  195. int size;
  196. if (a == null) {
  197. size = 32;
  198. } else {
  199. size = a.Length * 2;
  200. }
  201. Array result = Array.CreateInstance (elementType, size);
  202. if (a != null)
  203. Array.Copy (a, result, index);
  204. return result;
  205. }
  206. [MonoTODO ("Implement")]
  207. protected void FixupArrayRefs (object fixup)
  208. {
  209. throw new NotImplementedException ();
  210. }
  211. [MonoTODO ("Implement")]
  212. protected int GetArrayLength (string name, string ns)
  213. {
  214. throw new NotImplementedException ();
  215. }
  216. protected bool GetNullAttr ()
  217. {
  218. string na = reader.GetAttribute (nullX, w3InstanceNS);
  219. if (na == string.Empty) {
  220. na = reader.GetAttribute (nil, w3InstanceNS);
  221. if (na == string.Empty) {
  222. na = reader.GetAttribute (nullX, w3InstanceNS2000);
  223. if (na == string.Empty)
  224. na = reader.GetAttribute (nullX, w3InstanceNS1999);
  225. }
  226. }
  227. return (na != string.Empty);
  228. }
  229. protected object GetTarget (string id)
  230. {
  231. if (targets == null) return null;
  232. return targets [id];
  233. }
  234. bool TargetReady (string id)
  235. {
  236. if (targets == null) return false;
  237. return targets.ContainsKey (id);
  238. }
  239. [MonoTODO ("Implement")]
  240. protected XmlQualifiedName GetXsiType ()
  241. {
  242. string typeName = Reader.GetAttribute ("xsi:type");
  243. if (typeName == string.Empty || typeName == null) return null;
  244. int i = typeName.IndexOf (":");
  245. if (i == -1) return new XmlQualifiedName (typeName, "");
  246. else
  247. {
  248. string prefix = typeName.Substring(0,i);
  249. string name = typeName.Substring (i+1);
  250. return new XmlQualifiedName (name, Reader.LookupNamespace (prefix));
  251. }
  252. }
  253. protected abstract void InitCallbacks ();
  254. protected abstract void InitIDs ();
  255. protected bool IsXmlnsAttribute (string name)
  256. {
  257. int length = name.Length;
  258. if (length < 5)
  259. return false;
  260. if (length == 5)
  261. return (name == "xmlns");
  262. return name.StartsWith ("xmlns:");
  263. }
  264. [MonoTODO ("Implement")]
  265. protected void ParseWsdlArrayType (XmlAttribute attr)
  266. {
  267. throw new NotImplementedException ();
  268. }
  269. protected XmlQualifiedName ReadElementQualifiedName ()
  270. {
  271. if (reader.IsEmptyElement) {
  272. reader.Skip();
  273. return ToXmlQualifiedName (String.Empty);
  274. }
  275. XmlQualifiedName xqn = ToXmlQualifiedName(reader.ReadString ());
  276. reader.ReadEndElement ();
  277. return xqn;
  278. }
  279. protected void ReadEndElement ()
  280. {
  281. while (reader.NodeType == XmlNodeType.Whitespace)
  282. reader.Skip ();
  283. if (reader.NodeType != XmlNodeType.None) {
  284. reader.ReadEndElement ();
  285. } else {
  286. reader.Skip ();
  287. }
  288. }
  289. protected bool ReadNull ()
  290. {
  291. if (!GetNullAttr ())
  292. return false;
  293. if (reader.IsEmptyElement) {
  294. reader.Skip();
  295. return true;
  296. }
  297. reader.ReadStartElement();
  298. while (reader.NodeType != XmlNodeType.EndElement)
  299. UnknownNode (null);
  300. ReadEndElement ();
  301. return true;
  302. }
  303. protected XmlQualifiedName ReadNullableQualifiedName ()
  304. {
  305. if (ReadNull ())
  306. return null;
  307. return ReadElementQualifiedName ();
  308. }
  309. protected string ReadNullableString ()
  310. {
  311. if (ReadNull ())
  312. return null;
  313. return reader.ReadElementString ();
  314. }
  315. protected bool ReadReference (out string fixupReference)
  316. {
  317. string href = reader.GetAttribute ("href");
  318. if (href == null) {
  319. fixupReference = null;
  320. return false;
  321. }
  322. if (href [0] != '#')
  323. throw new InvalidOperationException("href not found: " + href);
  324. fixupReference = href.Substring (1);
  325. if (!reader.IsEmptyElement) {
  326. reader.ReadStartElement ();
  327. ReadEndElement ();
  328. } else {
  329. reader.Skip ();
  330. }
  331. return true;
  332. }
  333. protected object ReadReferencedElement ()
  334. {
  335. return ReadReferencedElement (Reader.LocalName, Reader.NamespaceURI);
  336. }
  337. WriteCallbackInfo GetCallbackInfo (XmlQualifiedName qname)
  338. {
  339. if (typesCallbacks == null)
  340. {
  341. typesCallbacks = new Hashtable ();
  342. InitCallbacks ();
  343. }
  344. return (WriteCallbackInfo) typesCallbacks[qname];
  345. }
  346. protected object ReadReferencedElement (string name, string ns)
  347. {
  348. XmlQualifiedName qname = GetXsiType ();
  349. if (qname == null) qname = new XmlQualifiedName (name, ns);
  350. string id = Reader.GetAttribute ("id");
  351. object ob;
  352. if (qname == arrayQName)
  353. {
  354. CollectionFixup fixup = (collFixups != null) ? (CollectionFixup) collFixups[id] : null;
  355. if (ReadList (out ob))
  356. {
  357. // List complete (does not contain references)
  358. if (fixup != null)
  359. {
  360. fixup.Callback (fixup.Collection, ob);
  361. collFixups.Remove (id);
  362. ob = fixup.Collection;
  363. }
  364. }
  365. else if (fixup != null)
  366. {
  367. fixup.CollectionItems = (object[])ob;
  368. ob = fixup.Collection;
  369. }
  370. }
  371. else
  372. {
  373. WriteCallbackInfo info = GetCallbackInfo (qname);
  374. if (info == null) throw CreateUnknownTypeException (qname);
  375. ob = info.Callback ();
  376. }
  377. AddTarget (id, ob);
  378. return ob;
  379. }
  380. bool ReadList (out object resultList)
  381. {
  382. string arrayType = Reader.GetAttribute ("arrayType", soapNS);
  383. XmlQualifiedName qn = ToXmlQualifiedName (arrayType);
  384. int i = qn.Name.LastIndexOf ('[');
  385. string dim = qn.Name.Substring (i);
  386. string itemType = qn.Name.Substring (0,i);
  387. int count = Int32.Parse (dim.Substring (1, dim.Length - 2));
  388. Array list;
  389. i = itemType.IndexOf ('['); if (i == -1) i = itemType.Length;
  390. string baseType = itemType.Substring (0,i);
  391. string arrayTypeName;
  392. if (qn.Namespace == XmlSchema.Namespace)
  393. arrayTypeName = TypeTranslator.GetPrimitiveTypeData (baseType).Type.FullName + itemType.Substring (i);
  394. else
  395. {
  396. WriteCallbackInfo info = GetCallbackInfo (new XmlQualifiedName (baseType,qn.Namespace));
  397. arrayTypeName = info.Type.FullName + itemType.Substring (i) + ", " + info.Type.Assembly.FullName;
  398. }
  399. list = Array.CreateInstance (Type.GetType (arrayTypeName), count);
  400. bool listComplete = true;
  401. Reader.ReadStartElement ();
  402. for (int n=0; n<count; n++)
  403. {
  404. Reader.MoveToContent ();
  405. string refid = Reader.GetAttribute ("href");
  406. string id;
  407. object item = ReadReferencingElement (itemType, qn.Namespace, out id);
  408. if (id == null)
  409. list.SetValue (item,n);
  410. else
  411. {
  412. AddFixup (new CollectionItemFixup (list, n, id));
  413. listComplete = false;
  414. }
  415. }
  416. Reader.ReadEndElement ();
  417. resultList = list;
  418. return listComplete;
  419. }
  420. protected void ReadReferencedElements ()
  421. {
  422. reader.MoveToContent();
  423. XmlNodeType nt = reader.NodeType;
  424. while (nt != XmlNodeType.EndElement && nt != XmlNodeType.None) {
  425. ReadReferencedElement ();
  426. reader.MoveToContent ();
  427. nt = reader.NodeType;
  428. }
  429. // Fix arrays
  430. if (collItemFixups != null)
  431. {
  432. foreach (CollectionItemFixup itemFixup in collItemFixups)
  433. itemFixup.Collection.SetValue (GetTarget (itemFixup.Id), itemFixup.Index);
  434. }
  435. // Fills collections
  436. if (collFixups != null)
  437. {
  438. ICollection cfixups = collFixups.Values;
  439. foreach (CollectionFixup fixup in cfixups)
  440. fixup.Callback (fixup.Collection, fixup.CollectionItems);
  441. }
  442. // Fills class instances
  443. if (fixups != null)
  444. {
  445. foreach (Fixup fixup in fixups)
  446. fixup.Callback (fixup);
  447. }
  448. }
  449. protected object ReadReferencingElement (out string fixupReference)
  450. {
  451. return ReadReferencingElement (Reader.LocalName, Reader.NamespaceURI, false, out fixupReference);
  452. }
  453. protected object ReadReferencingElement (string name, string ns, out string fixupReference)
  454. {
  455. return ReadReferencingElement (name, ns, false, out fixupReference);
  456. }
  457. protected object ReadReferencingElement (string name,
  458. string ns,
  459. bool elementCanBeType,
  460. out string fixupReference)
  461. {
  462. if (ReadNull ())
  463. {
  464. fixupReference = null;
  465. return null;
  466. }
  467. string refid = Reader.GetAttribute ("href");
  468. if (refid == string.Empty || refid == null)
  469. {
  470. fixupReference = null;
  471. XmlQualifiedName qname = GetXsiType ();
  472. if (qname == null) qname = new XmlQualifiedName (name, ns);
  473. if (qname == arrayQName)
  474. {
  475. delayedListFixups = EnsureHashtable (delayedListFixups);
  476. fixupReference = "__<" + delayedListFixups.Count + ">";
  477. object items;
  478. ReadList (out items);
  479. delayedListFixups [fixupReference] = items;
  480. return null;
  481. }
  482. else
  483. {
  484. WriteCallbackInfo info = GetCallbackInfo (qname);
  485. if (info == null)
  486. return ReadTypedPrimitive (qname);
  487. else
  488. return info.Callback();
  489. }
  490. }
  491. else
  492. {
  493. if (refid.StartsWith ("#")) refid = refid.Substring (1);
  494. Reader.ReadStartElement ();
  495. if (TargetReady (refid))
  496. {
  497. fixupReference = null;
  498. return GetTarget (refid);
  499. }
  500. else
  501. {
  502. fixupReference = refid;
  503. return null;
  504. }
  505. }
  506. }
  507. protected IXmlSerializable ReadSerializable (IXmlSerializable serializable)
  508. {
  509. serializable.ReadXml (reader);
  510. return serializable;
  511. }
  512. protected string ReadString (string value)
  513. {
  514. if (value == null || value == String.Empty)
  515. return reader.ReadString ();
  516. return (value + reader.ReadString ());
  517. }
  518. protected object ReadTypedPrimitive (XmlQualifiedName qname)
  519. {
  520. if (qname == null) qname = GetXsiType ();
  521. TypeData typeData = TypeTranslator.GetPrimitiveTypeData (qname.Name);
  522. if (typeData == null || typeData.SchemaType != SchemaTypes.Primitive) throw new InvalidOperationException ("Unknown type: " + qname.Name);
  523. return XmlCustomFormatter.FromXmlString (typeData.Type, Reader.ReadElementString ());
  524. }
  525. protected XmlNode ReadXmlNode (bool wrapped)
  526. {
  527. XmlNode node = Document.ReadNode (reader);
  528. if (wrapped)
  529. return node.FirstChild;
  530. else
  531. return node;
  532. }
  533. [MonoTODO ("Implement")]
  534. protected void Referenced (object o)
  535. {
  536. }
  537. protected Array ShrinkArray (Array a, int length, Type elementType, bool isNullable)
  538. {
  539. if (length == 0 && isNullable) return null;
  540. if (a == null) return Array.CreateInstance (elementType, length);
  541. if (a.Length == length) return a;
  542. Array result = Array.CreateInstance (elementType, length);
  543. Array.Copy (a, result, length);
  544. return result;
  545. }
  546. protected byte[] ToByteArrayBase64 (bool isNull)
  547. {
  548. return Convert.FromBase64String (Reader.ReadString());
  549. }
  550. [MonoTODO ("Implement")]
  551. protected static byte[] ToByteArrayBase64 (string value)
  552. {
  553. throw new NotImplementedException ();
  554. }
  555. [MonoTODO ("Implement")]
  556. protected byte[] ToByteArrayHex (bool isNull)
  557. {
  558. throw new NotImplementedException ();
  559. }
  560. [MonoTODO ("Implement")]
  561. protected static byte[] ToByteArrayHex (string value)
  562. {
  563. throw new NotImplementedException ();
  564. }
  565. protected static char ToChar (string value)
  566. {
  567. return XmlCustomFormatter.ToChar (value);
  568. }
  569. protected static DateTime ToDate (string value)
  570. {
  571. return XmlCustomFormatter.ToDate (value);
  572. }
  573. protected static DateTime ToDateTime (string value)
  574. {
  575. return XmlCustomFormatter.ToDateTime (value);
  576. }
  577. protected static long ToEnum (string value, Hashtable h, string typeName)
  578. {
  579. return XmlCustomFormatter.ToEnum (value, h, typeName, true);
  580. }
  581. protected static DateTime ToTime (string value)
  582. {
  583. return XmlCustomFormatter.ToTime (value);
  584. }
  585. protected static string ToXmlName (string value)
  586. {
  587. return XmlCustomFormatter.ToXmlName (value);
  588. }
  589. protected static string ToXmlNCName (string value)
  590. {
  591. return XmlCustomFormatter.ToXmlNCName (value);
  592. }
  593. protected static string ToXmlNmToken (string value)
  594. {
  595. return XmlCustomFormatter.ToXmlNmToken (value);
  596. }
  597. protected static string ToXmlNmTokens (string value)
  598. {
  599. return XmlCustomFormatter.ToXmlNmTokens (value);
  600. }
  601. protected XmlQualifiedName ToXmlQualifiedName (string value)
  602. {
  603. string name;
  604. string ns;
  605. int lastColon = value.LastIndexOf (':');
  606. string decodedValue = XmlConvert.DecodeName (value);
  607. if (lastColon < 0) {
  608. name = reader.NameTable.Add (decodedValue);
  609. ns = reader.LookupNamespace (String.Empty);
  610. } else {
  611. string prefix = value.Substring (0, lastColon);
  612. ns = reader.LookupNamespace (prefix);
  613. if (ns == null)
  614. throw new InvalidOperationException ("namespace " + prefix + "not defined");
  615. name = reader.NameTable.Add (value.Substring (lastColon + 1));
  616. }
  617. return new XmlQualifiedName (name, ns);
  618. }
  619. protected void UnknownAttribute (object o, XmlAttribute attr)
  620. {
  621. int line_number, line_position;
  622. if (Reader is XmlTextReader){
  623. line_number = ((XmlTextReader)Reader).LineNumber;
  624. line_position = ((XmlTextReader)Reader).LinePosition;
  625. } else {
  626. line_number = 0;
  627. line_position = 0;
  628. }
  629. eventSource.OnUnknownAttribute (new XmlAttributeEventArgs (attr, line_number, line_position, o));
  630. }
  631. protected void UnknownElement (object o, XmlElement elem)
  632. {
  633. int line_number, line_position;
  634. if (Reader is XmlTextReader){
  635. line_number = ((XmlTextReader)Reader).LineNumber;
  636. line_position = ((XmlTextReader)Reader).LinePosition;
  637. } else {
  638. line_number = 0;
  639. line_position = 0;
  640. }
  641. eventSource.OnUnknownElement (new XmlElementEventArgs (elem, line_number, line_position,o));
  642. }
  643. protected void UnknownNode (object o)
  644. {
  645. int line_number, line_position;
  646. if (Reader is XmlTextReader){
  647. line_number = ((XmlTextReader)Reader).LineNumber;
  648. line_position = ((XmlTextReader)Reader).LinePosition;
  649. } else {
  650. line_number = 0;
  651. line_position = 0;
  652. }
  653. eventSource.OnUnknownNode (new XmlNodeEventArgs(line_number, line_position, Reader.LocalName, Reader.Name, Reader.NamespaceURI, Reader.NodeType, o, Reader.Value));
  654. if (Reader.NodeType == XmlNodeType.Attribute)
  655. {
  656. XmlAttribute att = (XmlAttribute) ReadXmlNode (false);
  657. UnknownAttribute (o, att);
  658. return;
  659. }
  660. else if (Reader.NodeType == XmlNodeType.Element)
  661. {
  662. XmlElement elem = (XmlElement) ReadXmlNode (false);
  663. UnknownElement (o, elem);
  664. return;
  665. }
  666. else
  667. {
  668. Reader.Skip();
  669. if (Reader.ReadState == ReadState.EndOfFile)
  670. throw new InvalidOperationException ("End of document found");
  671. }
  672. }
  673. protected void UnreferencedObject (string id, object o)
  674. {
  675. eventSource.OnUnreferencedObject (new UnreferencedObjectEventArgs (o,id));
  676. }
  677. #endregion // Methods
  678. class WriteCallbackInfo
  679. {
  680. public Type Type;
  681. public string TypeName;
  682. public string TypeNs;
  683. public XmlSerializationReadCallback Callback;
  684. }
  685. protected class CollectionFixup {
  686. XmlSerializationCollectionFixupCallback callback;
  687. object collection;
  688. object collectionItems;
  689. string id;
  690. public CollectionFixup (object collection, XmlSerializationCollectionFixupCallback callback, string id)
  691. {
  692. this.callback = callback;
  693. this.collection = collection;
  694. this.id = id;
  695. }
  696. public XmlSerializationCollectionFixupCallback Callback {
  697. get { return callback; }
  698. }
  699. public object Collection {
  700. get { return collection; }
  701. }
  702. public object Id {
  703. get { return id; }
  704. }
  705. internal object CollectionItems
  706. {
  707. get { return collectionItems; }
  708. set { collectionItems = value; }
  709. }
  710. }
  711. protected class Fixup {
  712. object source;
  713. string[] ids;
  714. XmlSerializationFixupCallback callback;
  715. public Fixup (object o, XmlSerializationFixupCallback callback, int count)
  716. {
  717. this.source = o;
  718. this.callback = callback;
  719. this.ids = new string[count];
  720. }
  721. public Fixup (object o, XmlSerializationFixupCallback callback, string[] ids)
  722. {
  723. this.source = o;
  724. this.ids = ids;
  725. this.callback = callback;
  726. }
  727. public XmlSerializationFixupCallback Callback {
  728. get { return callback; }
  729. }
  730. public string[] Ids {
  731. get { return ids; }
  732. }
  733. public object Source {
  734. get { return source; }
  735. set { source = value; }
  736. }
  737. }
  738. protected class CollectionItemFixup
  739. {
  740. Array list;
  741. int index;
  742. string id;
  743. public CollectionItemFixup (Array list, int index, string id)
  744. {
  745. this.list = list;
  746. this.index = index;
  747. this.id = id;
  748. }
  749. public Array Collection
  750. {
  751. get { return list; }
  752. }
  753. public int Index
  754. {
  755. get { return index; }
  756. }
  757. public string Id
  758. {
  759. get { return id; }
  760. }
  761. }
  762. }
  763. }