XmlSerializationReader.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  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 == null) {
  222. na = reader.GetAttribute (nil, w3InstanceNS);
  223. if (na == null) {
  224. na = reader.GetAttribute (nullX, w3InstanceNS2000);
  225. if (na == null)
  226. na = reader.GetAttribute (nullX, w3InstanceNS1999);
  227. }
  228. }
  229. return (na != null);
  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, Reader.NamespaceURI);
  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. reader.ReadStartElement ();
  278. XmlQualifiedName xqn = ToXmlQualifiedName(reader.ReadString ());
  279. reader.ReadEndElement ();
  280. return xqn;
  281. }
  282. protected void ReadEndElement ()
  283. {
  284. while (reader.NodeType == XmlNodeType.Whitespace)
  285. reader.Skip ();
  286. if (reader.NodeType != XmlNodeType.None) {
  287. reader.ReadEndElement ();
  288. } else {
  289. reader.Skip ();
  290. }
  291. }
  292. protected bool ReadNull ()
  293. {
  294. if (!GetNullAttr ())
  295. return false;
  296. if (reader.IsEmptyElement) {
  297. reader.Skip();
  298. return true;
  299. }
  300. reader.ReadStartElement();
  301. while (reader.NodeType != XmlNodeType.EndElement)
  302. UnknownNode (null);
  303. ReadEndElement ();
  304. return true;
  305. }
  306. protected XmlQualifiedName ReadNullableQualifiedName ()
  307. {
  308. if (ReadNull ())
  309. return null;
  310. return ReadElementQualifiedName ();
  311. }
  312. protected string ReadNullableString ()
  313. {
  314. if (ReadNull ())
  315. return null;
  316. return reader.ReadElementString ();
  317. }
  318. protected bool ReadReference (out string fixupReference)
  319. {
  320. string href = reader.GetAttribute ("href");
  321. if (href == null) {
  322. fixupReference = null;
  323. return false;
  324. }
  325. if (href [0] != '#')
  326. throw new InvalidOperationException("href not found: " + href);
  327. fixupReference = href.Substring (1);
  328. if (!reader.IsEmptyElement) {
  329. reader.ReadStartElement ();
  330. ReadEndElement ();
  331. } else {
  332. reader.Skip ();
  333. }
  334. return true;
  335. }
  336. protected object ReadReferencedElement ()
  337. {
  338. return ReadReferencedElement (Reader.LocalName, Reader.NamespaceURI);
  339. }
  340. WriteCallbackInfo GetCallbackInfo (XmlQualifiedName qname)
  341. {
  342. if (typesCallbacks == null)
  343. {
  344. typesCallbacks = new Hashtable ();
  345. InitCallbacks ();
  346. }
  347. return (WriteCallbackInfo) typesCallbacks[qname];
  348. }
  349. protected object ReadReferencedElement (string name, string ns)
  350. {
  351. XmlQualifiedName qname = GetXsiType ();
  352. if (qname == null) qname = new XmlQualifiedName (name, ns);
  353. string id = Reader.GetAttribute ("id");
  354. object ob;
  355. if (qname == arrayQName)
  356. {
  357. CollectionFixup fixup = (collFixups != null) ? (CollectionFixup) collFixups[id] : null;
  358. if (ReadList (out ob))
  359. {
  360. // List complete (does not contain references)
  361. if (fixup != null)
  362. {
  363. fixup.Callback (fixup.Collection, ob);
  364. collFixups.Remove (id);
  365. ob = fixup.Collection;
  366. }
  367. }
  368. else if (fixup != null)
  369. {
  370. fixup.CollectionItems = (object[])ob;
  371. ob = fixup.Collection;
  372. }
  373. }
  374. else
  375. {
  376. WriteCallbackInfo info = GetCallbackInfo (qname);
  377. if (info == null) throw CreateUnknownTypeException (qname);
  378. ob = info.Callback ();
  379. }
  380. AddTarget (id, ob);
  381. return ob;
  382. }
  383. bool ReadList (out object resultList)
  384. {
  385. string arrayType = Reader.GetAttribute ("arrayType", soapNS);
  386. XmlQualifiedName qn = ToXmlQualifiedName (arrayType);
  387. int i = qn.Name.LastIndexOf ('[');
  388. string dim = qn.Name.Substring (i);
  389. string itemType = qn.Name.Substring (0,i);
  390. int count = Int32.Parse (dim.Substring (1, dim.Length - 2));
  391. Array list;
  392. i = itemType.IndexOf ('['); if (i == -1) i = itemType.Length;
  393. string baseType = itemType.Substring (0,i);
  394. string arrayTypeName;
  395. if (qn.Namespace == XmlSchema.Namespace)
  396. arrayTypeName = TypeTranslator.GetPrimitiveTypeData (baseType).Type.FullName + itemType.Substring (i);
  397. else
  398. {
  399. WriteCallbackInfo info = GetCallbackInfo (new XmlQualifiedName (baseType,qn.Namespace));
  400. arrayTypeName = info.Type.FullName + itemType.Substring (i) + ", " + info.Type.Assembly.FullName;
  401. }
  402. list = Array.CreateInstance (Type.GetType (arrayTypeName), count);
  403. bool listComplete = true;
  404. Reader.ReadStartElement ();
  405. for (int n=0; n<count; n++)
  406. {
  407. Reader.MoveToContent ();
  408. string refid = Reader.GetAttribute ("href");
  409. string id;
  410. object item = ReadReferencingElement (itemType, qn.Namespace, out id);
  411. if (id == null)
  412. list.SetValue (item,n);
  413. else
  414. {
  415. AddFixup (new CollectionItemFixup (list, n, id));
  416. listComplete = false;
  417. }
  418. }
  419. Reader.ReadEndElement ();
  420. resultList = list;
  421. return listComplete;
  422. }
  423. protected void ReadReferencedElements ()
  424. {
  425. reader.MoveToContent();
  426. XmlNodeType nt = reader.NodeType;
  427. while (nt != XmlNodeType.EndElement && nt != XmlNodeType.None) {
  428. ReadReferencedElement ();
  429. reader.MoveToContent ();
  430. nt = reader.NodeType;
  431. }
  432. // Registers delayed list
  433. if (delayedListFixups != null)
  434. {
  435. foreach (DictionaryEntry entry in delayedListFixups)
  436. AddTarget ((string)entry.Key, entry.Value);
  437. }
  438. // Fix arrays
  439. if (collItemFixups != null)
  440. {
  441. foreach (CollectionItemFixup itemFixup in collItemFixups)
  442. itemFixup.Collection.SetValue (GetTarget (itemFixup.Id), itemFixup.Index);
  443. }
  444. // Fills collections
  445. if (collFixups != null)
  446. {
  447. ICollection cfixups = collFixups.Values;
  448. foreach (CollectionFixup fixup in cfixups)
  449. fixup.Callback (fixup.Collection, fixup.CollectionItems);
  450. }
  451. // Fills class instances
  452. if (fixups != null)
  453. {
  454. foreach (Fixup fixup in fixups)
  455. fixup.Callback (fixup);
  456. }
  457. }
  458. protected object ReadReferencingElement (out string fixupReference)
  459. {
  460. return ReadReferencingElement (Reader.LocalName, Reader.NamespaceURI, false, out fixupReference);
  461. }
  462. protected object ReadReferencingElement (string name, string ns, out string fixupReference)
  463. {
  464. return ReadReferencingElement (name, ns, false, out fixupReference);
  465. }
  466. protected object ReadReferencingElement (string name,
  467. string ns,
  468. bool elementCanBeType,
  469. out string fixupReference)
  470. {
  471. if (ReadNull ())
  472. {
  473. fixupReference = null;
  474. return null;
  475. }
  476. string refid = Reader.GetAttribute ("href");
  477. if (refid == string.Empty || refid == null)
  478. {
  479. fixupReference = null;
  480. XmlQualifiedName qname = GetXsiType ();
  481. if (qname == null) qname = new XmlQualifiedName (name, ns);
  482. if (qname == arrayQName)
  483. {
  484. delayedListFixups = EnsureHashtable (delayedListFixups);
  485. fixupReference = "__<" + delayedListFixups.Count + ">";
  486. object items;
  487. ReadList (out items);
  488. delayedListFixups [fixupReference] = items;
  489. return null;
  490. }
  491. else
  492. {
  493. WriteCallbackInfo info = GetCallbackInfo (qname);
  494. if (info == null)
  495. return ReadTypedPrimitive (qname);
  496. else
  497. return info.Callback();
  498. }
  499. }
  500. else
  501. {
  502. if (refid.StartsWith ("#")) refid = refid.Substring (1);
  503. Reader.ReadStartElement ();
  504. if (TargetReady (refid))
  505. {
  506. fixupReference = null;
  507. return GetTarget (refid);
  508. }
  509. else
  510. {
  511. fixupReference = refid;
  512. return null;
  513. }
  514. }
  515. }
  516. protected IXmlSerializable ReadSerializable (IXmlSerializable serializable)
  517. {
  518. if (ReadNull ()) return null;
  519. serializable.ReadXml (reader);
  520. Reader.MoveToContent ();
  521. Reader.ReadEndElement ();
  522. return serializable;
  523. }
  524. protected string ReadString (string value)
  525. {
  526. if (value == null || value == String.Empty)
  527. return reader.ReadString ();
  528. return (value + reader.ReadString ());
  529. }
  530. protected object ReadTypedPrimitive (XmlQualifiedName qname)
  531. {
  532. if (qname == null) qname = GetXsiType ();
  533. TypeData typeData = TypeTranslator.GetPrimitiveTypeData (qname.Name);
  534. if (typeData == null || typeData.SchemaType != SchemaTypes.Primitive) throw new InvalidOperationException ("Unknown type: " + qname.Name);
  535. if (typeData.Type == typeof (XmlQualifiedName)) return ReadNullableQualifiedName ();
  536. return XmlCustomFormatter.FromXmlString (typeData.Type, Reader.ReadElementString ());
  537. }
  538. protected XmlNode ReadXmlNode (bool wrapped)
  539. {
  540. XmlNode node = Document.ReadNode (reader);
  541. if (wrapped)
  542. return node.FirstChild;
  543. else
  544. return node;
  545. }
  546. [MonoTODO ("Implement")]
  547. protected void Referenced (object o)
  548. {
  549. }
  550. protected Array ShrinkArray (Array a, int length, Type elementType, bool isNullable)
  551. {
  552. if (length == 0 && isNullable) return null;
  553. if (a == null) return Array.CreateInstance (elementType, length);
  554. if (a.Length == length) return a;
  555. Array result = Array.CreateInstance (elementType, length);
  556. Array.Copy (a, result, length);
  557. return result;
  558. }
  559. protected byte[] ToByteArrayBase64 (bool isNull)
  560. {
  561. return Convert.FromBase64String (Reader.ReadString());
  562. }
  563. [MonoTODO ("Implement")]
  564. protected static byte[] ToByteArrayBase64 (string value)
  565. {
  566. throw new NotImplementedException ();
  567. }
  568. [MonoTODO ("Implement")]
  569. protected byte[] ToByteArrayHex (bool isNull)
  570. {
  571. throw new NotImplementedException ();
  572. }
  573. [MonoTODO ("Implement")]
  574. protected static byte[] ToByteArrayHex (string value)
  575. {
  576. throw new NotImplementedException ();
  577. }
  578. protected static char ToChar (string value)
  579. {
  580. return XmlCustomFormatter.ToChar (value);
  581. }
  582. protected static DateTime ToDate (string value)
  583. {
  584. return XmlCustomFormatter.ToDate (value);
  585. }
  586. protected static DateTime ToDateTime (string value)
  587. {
  588. return XmlCustomFormatter.ToDateTime (value);
  589. }
  590. protected static long ToEnum (string value, Hashtable h, string typeName)
  591. {
  592. return XmlCustomFormatter.ToEnum (value, h, typeName, true);
  593. }
  594. protected static DateTime ToTime (string value)
  595. {
  596. return XmlCustomFormatter.ToTime (value);
  597. }
  598. protected static string ToXmlName (string value)
  599. {
  600. return XmlCustomFormatter.ToXmlName (value);
  601. }
  602. protected static string ToXmlNCName (string value)
  603. {
  604. return XmlCustomFormatter.ToXmlNCName (value);
  605. }
  606. protected static string ToXmlNmToken (string value)
  607. {
  608. return XmlCustomFormatter.ToXmlNmToken (value);
  609. }
  610. protected static string ToXmlNmTokens (string value)
  611. {
  612. return XmlCustomFormatter.ToXmlNmTokens (value);
  613. }
  614. protected XmlQualifiedName ToXmlQualifiedName (string value)
  615. {
  616. string name;
  617. string ns;
  618. int lastColon = value.LastIndexOf (':');
  619. string decodedValue = XmlConvert.DecodeName (value);
  620. if (lastColon < 0) {
  621. name = reader.NameTable.Add (decodedValue);
  622. ns = reader.LookupNamespace (String.Empty);
  623. } else {
  624. string prefix = value.Substring (0, lastColon);
  625. ns = reader.LookupNamespace (prefix);
  626. if (ns == null)
  627. throw new InvalidOperationException ("namespace " + prefix + " not defined");
  628. name = reader.NameTable.Add (value.Substring (lastColon + 1));
  629. }
  630. return new XmlQualifiedName (name, ns);
  631. }
  632. protected void UnknownAttribute (object o, XmlAttribute attr)
  633. {
  634. int line_number, line_position;
  635. if (Reader is XmlTextReader){
  636. line_number = ((XmlTextReader)Reader).LineNumber;
  637. line_position = ((XmlTextReader)Reader).LinePosition;
  638. } else {
  639. line_number = 0;
  640. line_position = 0;
  641. }
  642. eventSource.OnUnknownAttribute (new XmlAttributeEventArgs (attr, line_number, line_position, o));
  643. }
  644. protected void UnknownElement (object o, XmlElement elem)
  645. {
  646. int line_number, line_position;
  647. if (Reader is XmlTextReader){
  648. line_number = ((XmlTextReader)Reader).LineNumber;
  649. line_position = ((XmlTextReader)Reader).LinePosition;
  650. } else {
  651. line_number = 0;
  652. line_position = 0;
  653. }
  654. eventSource.OnUnknownElement (new XmlElementEventArgs (elem, line_number, line_position,o));
  655. }
  656. protected void UnknownNode (object o)
  657. {
  658. int line_number, line_position;
  659. if (Reader is XmlTextReader){
  660. line_number = ((XmlTextReader)Reader).LineNumber;
  661. line_position = ((XmlTextReader)Reader).LinePosition;
  662. } else {
  663. line_number = 0;
  664. line_position = 0;
  665. }
  666. eventSource.OnUnknownNode (new XmlNodeEventArgs(line_number, line_position, Reader.LocalName, Reader.Name, Reader.NamespaceURI, Reader.NodeType, o, Reader.Value));
  667. if (Reader.NodeType == XmlNodeType.Attribute)
  668. {
  669. XmlAttribute att = (XmlAttribute) ReadXmlNode (false);
  670. UnknownAttribute (o, att);
  671. return;
  672. }
  673. else if (Reader.NodeType == XmlNodeType.Element)
  674. {
  675. XmlElement elem = (XmlElement) ReadXmlNode (false);
  676. UnknownElement (o, elem);
  677. return;
  678. }
  679. else
  680. {
  681. Reader.Skip();
  682. if (Reader.ReadState == ReadState.EndOfFile)
  683. throw new InvalidOperationException ("End of document found");
  684. }
  685. }
  686. protected void UnreferencedObject (string id, object o)
  687. {
  688. eventSource.OnUnreferencedObject (new UnreferencedObjectEventArgs (o,id));
  689. }
  690. #endregion // Methods
  691. class WriteCallbackInfo
  692. {
  693. public Type Type;
  694. public string TypeName;
  695. public string TypeNs;
  696. public XmlSerializationReadCallback Callback;
  697. }
  698. protected class CollectionFixup {
  699. XmlSerializationCollectionFixupCallback callback;
  700. object collection;
  701. object collectionItems;
  702. string id;
  703. public CollectionFixup (object collection, XmlSerializationCollectionFixupCallback callback, string id)
  704. {
  705. this.callback = callback;
  706. this.collection = collection;
  707. this.id = id;
  708. }
  709. public XmlSerializationCollectionFixupCallback Callback {
  710. get { return callback; }
  711. }
  712. public object Collection {
  713. get { return collection; }
  714. }
  715. public object Id {
  716. get { return id; }
  717. }
  718. internal object CollectionItems
  719. {
  720. get { return collectionItems; }
  721. set { collectionItems = value; }
  722. }
  723. }
  724. protected class Fixup {
  725. object source;
  726. string[] ids;
  727. XmlSerializationFixupCallback callback;
  728. public Fixup (object o, XmlSerializationFixupCallback callback, int count)
  729. {
  730. this.source = o;
  731. this.callback = callback;
  732. this.ids = new string[count];
  733. }
  734. public Fixup (object o, XmlSerializationFixupCallback callback, string[] ids)
  735. {
  736. this.source = o;
  737. this.ids = ids;
  738. this.callback = callback;
  739. }
  740. public XmlSerializationFixupCallback Callback {
  741. get { return callback; }
  742. }
  743. public string[] Ids {
  744. get { return ids; }
  745. }
  746. public object Source {
  747. get { return source; }
  748. set { source = value; }
  749. }
  750. }
  751. protected class CollectionItemFixup
  752. {
  753. Array list;
  754. int index;
  755. string id;
  756. public CollectionItemFixup (Array list, int index, string id)
  757. {
  758. this.list = list;
  759. this.index = index;
  760. this.id = id;
  761. }
  762. public Array Collection
  763. {
  764. get { return list; }
  765. }
  766. public int Index
  767. {
  768. get { return index; }
  769. }
  770. public string Id
  771. {
  772. get { return id; }
  773. }
  774. }
  775. }
  776. }