XmlSerializationReader.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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. private ArrayList EnsureArrayList (ArrayList list)
  69. {
  70. if (list == null)
  71. list = new ArrayList ();
  72. return list;
  73. }
  74. private Hashtable EnsureHashtable (Hashtable hash)
  75. {
  76. if (hash == null)
  77. hash = new Hashtable ();
  78. return hash;
  79. }
  80. protected XmlSerializationReader ()
  81. {
  82. }
  83. protected XmlDocument Document
  84. {
  85. get {
  86. if (document == null)
  87. document = new XmlDocument (reader.NameTable);
  88. return document;
  89. }
  90. }
  91. protected XmlReader Reader {
  92. get { return reader; }
  93. }
  94. #region Methods
  95. protected void AddFixup (CollectionFixup fixup)
  96. {
  97. collFixups = EnsureHashtable (collFixups);
  98. collFixups [fixup.Id] = fixup;
  99. if (delayedListFixups != null && delayedListFixups.ContainsKey (fixup.Id)) {
  100. fixup.CollectionItems = delayedListFixups [fixup.Id];
  101. delayedListFixups.Remove (fixup.Id);
  102. }
  103. }
  104. protected void AddFixup (Fixup fixup)
  105. {
  106. fixups = EnsureArrayList (fixups);
  107. fixups.Add (fixup);
  108. }
  109. void AddFixup (CollectionItemFixup fixup)
  110. {
  111. collItemFixups = EnsureArrayList (collItemFixups);
  112. collItemFixups.Add(fixup);
  113. }
  114. protected void AddReadCallback (string name, string ns, Type type, XmlSerializationReadCallback read)
  115. {
  116. WriteCallbackInfo info = new WriteCallbackInfo ();
  117. info.Type = type;
  118. info.TypeName = name;
  119. info.TypeNs = ns;
  120. info.Callback = read;
  121. typesCallbacks = EnsureHashtable (typesCallbacks);
  122. typesCallbacks.Add (new XmlQualifiedName (name, ns), info);
  123. }
  124. protected void AddTarget (string id, object o)
  125. {
  126. if (id != null) {
  127. targets = EnsureHashtable (targets);
  128. if (targets [id] == null)
  129. targets.Add (id, o);
  130. } else {
  131. if (o != null)
  132. return;
  133. noIDTargets = EnsureArrayList (noIDTargets);
  134. noIDTargets.Add (o);
  135. }
  136. }
  137. private string CurrentTag ()
  138. {
  139. switch (reader.NodeType) {
  140. case XmlNodeType.None:
  141. return String.Format ("<{0} xmlns='{1}'>", reader.LocalName,
  142. reader.NamespaceURI);
  143. case XmlNodeType.Attribute:
  144. return reader.Value;
  145. case XmlNodeType.Text:
  146. return "CDATA";
  147. case XmlNodeType.ProcessingInstruction:
  148. return "<--";
  149. case XmlNodeType.Entity:
  150. return "<?";
  151. case XmlNodeType.EndElement:
  152. return ">";
  153. default:
  154. return "(unknown)";
  155. }
  156. }
  157. protected Exception CreateAbstractTypeException (string name, string ns)
  158. {
  159. string message = "Error at " + name + " " + ns + ":" + CurrentTag ();
  160. return new InvalidOperationException (message);
  161. }
  162. protected Exception CreateInvalidCastException (Type type, object value)
  163. {
  164. string message = String.Format ("Cannot assign object of type {0} to an object of " +
  165. "type {1}.", value.GetType (), type);
  166. return new InvalidCastException (message);
  167. }
  168. protected Exception CreateReadOnlyCollectionException (string name)
  169. {
  170. string message = String.Format ("Could not serialize {0}. Default constructors are " +
  171. "required for collections and enumerators.", name);
  172. return new InvalidOperationException (message);
  173. }
  174. protected Exception CreateUnknownConstantException (string value, Type enumType)
  175. {
  176. string message = String.Format ("'{0}' is not a valid value for {1}.", value, enumType);
  177. return new InvalidOperationException (message);
  178. }
  179. protected Exception CreateUnknownNodeException ()
  180. {
  181. string message = "Unknown xml node -> " + CurrentTag ();
  182. return new InvalidOperationException (message);
  183. }
  184. protected Exception CreateUnknownTypeException (XmlQualifiedName type)
  185. {
  186. string message = "Unknown type " + type.Namespace + ":" + type.Name + " " + CurrentTag ();
  187. return new InvalidOperationException (message);
  188. }
  189. protected Array EnsureArrayIndex (Array a, int index, Type elementType)
  190. {
  191. if (a != null && index < a.Length)
  192. return a;
  193. int size;
  194. if (a == null) {
  195. size = 32;
  196. } else {
  197. size = a.Length * 2;
  198. }
  199. Array result = Array.CreateInstance (elementType, size);
  200. if (a != null)
  201. Array.Copy (a, result, index);
  202. return result;
  203. }
  204. [MonoTODO ("Implement")]
  205. protected void FixupArrayRefs (object fixup)
  206. {
  207. throw new NotImplementedException ();
  208. }
  209. [MonoTODO ("Implement")]
  210. protected int GetArrayLength (string name, string ns)
  211. {
  212. throw new NotImplementedException ();
  213. }
  214. protected bool GetNullAttr ()
  215. {
  216. string na = reader.GetAttribute (nullX, w3InstanceNS);
  217. if (na == null) {
  218. na = reader.GetAttribute (nil, w3InstanceNS);
  219. if (na == null) {
  220. na = reader.GetAttribute (nullX, w3InstanceNS2000);
  221. if (na == null)
  222. na = reader.GetAttribute (nullX, w3InstanceNS1999);
  223. }
  224. }
  225. return (na != null);
  226. }
  227. protected object GetTarget (string id)
  228. {
  229. if (targets == null) return null;
  230. return targets [id];
  231. }
  232. bool TargetReady (string id)
  233. {
  234. if (targets == null) return false;
  235. return targets.ContainsKey (id);
  236. }
  237. [MonoTODO ("Implement")]
  238. protected XmlQualifiedName GetXsiType ()
  239. {
  240. string typeName = Reader.GetAttribute ("xsi:type");
  241. if (typeName == string.Empty || typeName == null) return null;
  242. int i = typeName.IndexOf (":");
  243. if (i == -1) return new XmlQualifiedName (typeName, Reader.NamespaceURI);
  244. else
  245. {
  246. string prefix = typeName.Substring(0,i);
  247. string name = typeName.Substring (i+1);
  248. return new XmlQualifiedName (name, Reader.LookupNamespace (prefix));
  249. }
  250. }
  251. protected abstract void InitCallbacks ();
  252. protected abstract void InitIDs ();
  253. protected bool IsXmlnsAttribute (string name)
  254. {
  255. int length = name.Length;
  256. if (length < 5)
  257. return false;
  258. if (length == 5)
  259. return (name == "xmlns");
  260. return name.StartsWith ("xmlns:");
  261. }
  262. [MonoTODO ("Implement")]
  263. protected void ParseWsdlArrayType (XmlAttribute attr)
  264. {
  265. throw new NotImplementedException ();
  266. }
  267. protected XmlQualifiedName ReadElementQualifiedName ()
  268. {
  269. if (reader.IsEmptyElement) {
  270. reader.Skip();
  271. return ToXmlQualifiedName (String.Empty);
  272. }
  273. reader.ReadStartElement ();
  274. XmlQualifiedName xqn = ToXmlQualifiedName(reader.ReadString ());
  275. reader.ReadEndElement ();
  276. return xqn;
  277. }
  278. protected void ReadEndElement ()
  279. {
  280. while (reader.NodeType == XmlNodeType.Whitespace)
  281. reader.Skip ();
  282. if (reader.NodeType != XmlNodeType.None) {
  283. reader.ReadEndElement ();
  284. } else {
  285. reader.Skip ();
  286. }
  287. }
  288. protected bool ReadNull ()
  289. {
  290. if (!GetNullAttr ())
  291. return false;
  292. if (reader.IsEmptyElement) {
  293. reader.Skip();
  294. return true;
  295. }
  296. reader.ReadStartElement();
  297. while (reader.NodeType != XmlNodeType.EndElement)
  298. UnknownNode (null);
  299. ReadEndElement ();
  300. return true;
  301. }
  302. protected XmlQualifiedName ReadNullableQualifiedName ()
  303. {
  304. if (ReadNull ())
  305. return null;
  306. return ReadElementQualifiedName ();
  307. }
  308. protected string ReadNullableString ()
  309. {
  310. if (ReadNull ())
  311. return null;
  312. return reader.ReadElementString ();
  313. }
  314. protected bool ReadReference (out string fixupReference)
  315. {
  316. string href = reader.GetAttribute ("href");
  317. if (href == null) {
  318. fixupReference = null;
  319. return false;
  320. }
  321. if (href [0] != '#')
  322. throw new InvalidOperationException("href not found: " + href);
  323. fixupReference = href.Substring (1);
  324. if (!reader.IsEmptyElement) {
  325. reader.ReadStartElement ();
  326. ReadEndElement ();
  327. } else {
  328. reader.Skip ();
  329. }
  330. return true;
  331. }
  332. protected object ReadReferencedElement ()
  333. {
  334. return ReadReferencedElement (Reader.LocalName, Reader.NamespaceURI);
  335. }
  336. WriteCallbackInfo GetCallbackInfo (XmlQualifiedName qname)
  337. {
  338. if (typesCallbacks == null)
  339. {
  340. typesCallbacks = new Hashtable ();
  341. InitCallbacks ();
  342. }
  343. return (WriteCallbackInfo) typesCallbacks[qname];
  344. }
  345. protected object ReadReferencedElement (string name, string ns)
  346. {
  347. XmlQualifiedName qname = GetXsiType ();
  348. if (qname == null) qname = new XmlQualifiedName (name, ns);
  349. string id = Reader.GetAttribute ("id");
  350. object ob;
  351. if (qname == arrayQName)
  352. {
  353. CollectionFixup fixup = (collFixups != null) ? (CollectionFixup) collFixups[id] : null;
  354. if (ReadList (out ob))
  355. {
  356. // List complete (does not contain references)
  357. if (fixup != null)
  358. {
  359. fixup.Callback (fixup.Collection, ob);
  360. collFixups.Remove (id);
  361. ob = fixup.Collection;
  362. }
  363. }
  364. else if (fixup != null)
  365. {
  366. fixup.CollectionItems = (object[])ob;
  367. ob = fixup.Collection;
  368. }
  369. }
  370. else
  371. {
  372. WriteCallbackInfo info = GetCallbackInfo (qname);
  373. if (info == null) throw CreateUnknownTypeException (qname);
  374. ob = info.Callback ();
  375. }
  376. AddTarget (id, ob);
  377. return ob;
  378. }
  379. bool ReadList (out object resultList)
  380. {
  381. string arrayType = Reader.GetAttribute ("arrayType", soapNS);
  382. XmlQualifiedName qn = ToXmlQualifiedName (arrayType);
  383. int i = qn.Name.LastIndexOf ('[');
  384. string dim = qn.Name.Substring (i);
  385. string itemType = qn.Name.Substring (0,i);
  386. int count = Int32.Parse (dim.Substring (1, dim.Length - 2));
  387. Array list;
  388. i = itemType.IndexOf ('['); if (i == -1) i = itemType.Length;
  389. string baseType = itemType.Substring (0,i);
  390. string arrayTypeName;
  391. if (qn.Namespace == XmlSchema.Namespace)
  392. arrayTypeName = TypeTranslator.GetPrimitiveTypeData (baseType).Type.FullName + itemType.Substring (i);
  393. else
  394. {
  395. WriteCallbackInfo info = GetCallbackInfo (new XmlQualifiedName (baseType,qn.Namespace));
  396. arrayTypeName = info.Type.FullName + itemType.Substring (i) + ", " + info.Type.Assembly.FullName;
  397. }
  398. list = Array.CreateInstance (Type.GetType (arrayTypeName), count);
  399. bool listComplete = true;
  400. Reader.ReadStartElement ();
  401. for (int n=0; n<count; n++)
  402. {
  403. Reader.MoveToContent ();
  404. string refid = Reader.GetAttribute ("href");
  405. string id;
  406. object item = ReadReferencingElement (itemType, qn.Namespace, out id);
  407. if (id == null)
  408. list.SetValue (item,n);
  409. else
  410. {
  411. AddFixup (new CollectionItemFixup (list, n, id));
  412. listComplete = false;
  413. }
  414. }
  415. Reader.ReadEndElement ();
  416. resultList = list;
  417. return listComplete;
  418. }
  419. protected void ReadReferencedElements ()
  420. {
  421. reader.MoveToContent();
  422. XmlNodeType nt = reader.NodeType;
  423. while (nt != XmlNodeType.EndElement && nt != XmlNodeType.None) {
  424. ReadReferencedElement ();
  425. reader.MoveToContent ();
  426. nt = reader.NodeType;
  427. }
  428. // Registers delayed list
  429. if (delayedListFixups != null)
  430. {
  431. foreach (DictionaryEntry entry in delayedListFixups)
  432. AddTarget ((string)entry.Key, entry.Value);
  433. }
  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. if (ReadNull ()) return null;
  515. serializable.ReadXml (reader);
  516. Reader.MoveToContent ();
  517. Reader.ReadEndElement ();
  518. return serializable;
  519. }
  520. protected string ReadString (string value)
  521. {
  522. if (value == null || value == String.Empty)
  523. return reader.ReadString ();
  524. return (value + reader.ReadString ());
  525. }
  526. protected object ReadTypedPrimitive (XmlQualifiedName qname)
  527. {
  528. if (qname == null) qname = GetXsiType ();
  529. TypeData typeData = TypeTranslator.GetPrimitiveTypeData (qname.Name);
  530. if (typeData == null || typeData.SchemaType != SchemaTypes.Primitive) throw new InvalidOperationException ("Unknown type: " + qname.Name);
  531. if (typeData.Type == typeof (XmlQualifiedName)) return ReadNullableQualifiedName ();
  532. return XmlCustomFormatter.FromXmlString (typeData, Reader.ReadElementString ());
  533. }
  534. protected XmlNode ReadXmlNode (bool wrapped)
  535. {
  536. XmlNode node = Document.ReadNode (reader);
  537. if (wrapped)
  538. return node.FirstChild;
  539. else
  540. return node;
  541. }
  542. [MonoTODO ("Implement")]
  543. protected void Referenced (object o)
  544. {
  545. }
  546. protected Array ShrinkArray (Array a, int length, Type elementType, bool isNullable)
  547. {
  548. if (length == 0 && isNullable) return null;
  549. if (a == null) return Array.CreateInstance (elementType, length);
  550. if (a.Length == length) return a;
  551. Array result = Array.CreateInstance (elementType, length);
  552. Array.Copy (a, result, length);
  553. return result;
  554. }
  555. protected byte[] ToByteArrayBase64 (bool isNull)
  556. {
  557. return Convert.FromBase64String (Reader.ReadString());
  558. }
  559. [MonoTODO ("Implement")]
  560. protected static byte[] ToByteArrayBase64 (string value)
  561. {
  562. throw new NotImplementedException ();
  563. }
  564. [MonoTODO ("Implement")]
  565. protected byte[] ToByteArrayHex (bool isNull)
  566. {
  567. throw new NotImplementedException ();
  568. }
  569. [MonoTODO ("Implement")]
  570. protected static byte[] ToByteArrayHex (string value)
  571. {
  572. throw new NotImplementedException ();
  573. }
  574. protected static char ToChar (string value)
  575. {
  576. return XmlCustomFormatter.ToChar (value);
  577. }
  578. protected static DateTime ToDate (string value)
  579. {
  580. return XmlCustomFormatter.ToDate (value);
  581. }
  582. protected static DateTime ToDateTime (string value)
  583. {
  584. return XmlCustomFormatter.ToDateTime (value);
  585. }
  586. protected static long ToEnum (string value, Hashtable h, string typeName)
  587. {
  588. return XmlCustomFormatter.ToEnum (value, h, typeName, true);
  589. }
  590. protected static DateTime ToTime (string value)
  591. {
  592. return XmlCustomFormatter.ToTime (value);
  593. }
  594. protected static string ToXmlName (string value)
  595. {
  596. return XmlCustomFormatter.ToXmlName (value);
  597. }
  598. protected static string ToXmlNCName (string value)
  599. {
  600. return XmlCustomFormatter.ToXmlNCName (value);
  601. }
  602. protected static string ToXmlNmToken (string value)
  603. {
  604. return XmlCustomFormatter.ToXmlNmToken (value);
  605. }
  606. protected static string ToXmlNmTokens (string value)
  607. {
  608. return XmlCustomFormatter.ToXmlNmTokens (value);
  609. }
  610. protected XmlQualifiedName ToXmlQualifiedName (string value)
  611. {
  612. string name;
  613. string ns;
  614. int lastColon = value.LastIndexOf (':');
  615. string decodedValue = XmlConvert.DecodeName (value);
  616. if (lastColon < 0) {
  617. name = reader.NameTable.Add (decodedValue);
  618. ns = reader.LookupNamespace (String.Empty);
  619. } else {
  620. string prefix = value.Substring (0, lastColon);
  621. ns = reader.LookupNamespace (prefix);
  622. if (ns == null)
  623. throw new InvalidOperationException ("namespace " + prefix + " not defined");
  624. name = reader.NameTable.Add (value.Substring (lastColon + 1));
  625. }
  626. return new XmlQualifiedName (name, ns);
  627. }
  628. protected void UnknownAttribute (object o, XmlAttribute attr)
  629. {
  630. int line_number, line_position;
  631. if (Reader is XmlTextReader){
  632. line_number = ((XmlTextReader)Reader).LineNumber;
  633. line_position = ((XmlTextReader)Reader).LinePosition;
  634. } else {
  635. line_number = 0;
  636. line_position = 0;
  637. }
  638. if (eventSource != null)
  639. eventSource.OnUnknownAttribute (new XmlAttributeEventArgs (attr, line_number, line_position, o));
  640. }
  641. protected void UnknownElement (object o, XmlElement elem)
  642. {
  643. int line_number, line_position;
  644. if (Reader is XmlTextReader){
  645. line_number = ((XmlTextReader)Reader).LineNumber;
  646. line_position = ((XmlTextReader)Reader).LinePosition;
  647. } else {
  648. line_number = 0;
  649. line_position = 0;
  650. }
  651. if (eventSource != null)
  652. eventSource.OnUnknownElement (new XmlElementEventArgs (elem, line_number, line_position,o));
  653. }
  654. protected void UnknownNode (object o)
  655. {
  656. int line_number, line_position;
  657. if (Reader is XmlTextReader){
  658. line_number = ((XmlTextReader)Reader).LineNumber;
  659. line_position = ((XmlTextReader)Reader).LinePosition;
  660. } else {
  661. line_number = 0;
  662. line_position = 0;
  663. }
  664. if (eventSource != null)
  665. eventSource.OnUnknownNode (new XmlNodeEventArgs(line_number, line_position, Reader.LocalName, Reader.Name, Reader.NamespaceURI, Reader.NodeType, o, Reader.Value));
  666. if (Reader.NodeType == XmlNodeType.Attribute)
  667. {
  668. XmlAttribute att = (XmlAttribute) ReadXmlNode (false);
  669. UnknownAttribute (o, att);
  670. return;
  671. }
  672. else if (Reader.NodeType == XmlNodeType.Element)
  673. {
  674. XmlElement elem = (XmlElement) ReadXmlNode (false);
  675. UnknownElement (o, elem);
  676. return;
  677. }
  678. else
  679. {
  680. Reader.Skip();
  681. if (Reader.ReadState == ReadState.EndOfFile)
  682. throw new InvalidOperationException ("End of document found");
  683. }
  684. }
  685. protected void UnreferencedObject (string id, object o)
  686. {
  687. if (eventSource != null)
  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. }