XmlSerializationReader.cs 22 KB

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