XmlSerializationReader.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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. if (delayedListFixups != null)
  433. {
  434. foreach (DictionaryEntry entry in delayedListFixups)
  435. AddTarget ((string)entry.Key, entry.Value);
  436. }
  437. // Fix arrays
  438. if (collItemFixups != null)
  439. {
  440. foreach (CollectionItemFixup itemFixup in collItemFixups)
  441. itemFixup.Collection.SetValue (GetTarget (itemFixup.Id), itemFixup.Index);
  442. }
  443. // Fills collections
  444. if (collFixups != null)
  445. {
  446. ICollection cfixups = collFixups.Values;
  447. foreach (CollectionFixup fixup in cfixups)
  448. fixup.Callback (fixup.Collection, fixup.CollectionItems);
  449. }
  450. // Fills class instances
  451. if (fixups != null)
  452. {
  453. foreach (Fixup fixup in fixups)
  454. fixup.Callback (fixup);
  455. }
  456. }
  457. protected object ReadReferencingElement (out string fixupReference)
  458. {
  459. return ReadReferencingElement (Reader.LocalName, Reader.NamespaceURI, false, out fixupReference);
  460. }
  461. protected object ReadReferencingElement (string name, string ns, out string fixupReference)
  462. {
  463. return ReadReferencingElement (name, ns, false, out fixupReference);
  464. }
  465. protected object ReadReferencingElement (string name,
  466. string ns,
  467. bool elementCanBeType,
  468. out string fixupReference)
  469. {
  470. if (ReadNull ())
  471. {
  472. fixupReference = null;
  473. return null;
  474. }
  475. string refid = Reader.GetAttribute ("href");
  476. if (refid == string.Empty || refid == null)
  477. {
  478. fixupReference = null;
  479. XmlQualifiedName qname = GetXsiType ();
  480. if (qname == null) qname = new XmlQualifiedName (name, ns);
  481. if (qname == arrayQName)
  482. {
  483. delayedListFixups = EnsureHashtable (delayedListFixups);
  484. fixupReference = "__<" + delayedListFixups.Count + ">";
  485. object items;
  486. ReadList (out items);
  487. delayedListFixups [fixupReference] = items;
  488. return null;
  489. }
  490. else
  491. {
  492. WriteCallbackInfo info = GetCallbackInfo (qname);
  493. if (info == null)
  494. return ReadTypedPrimitive (qname);
  495. else
  496. return info.Callback();
  497. }
  498. }
  499. else
  500. {
  501. if (refid.StartsWith ("#")) refid = refid.Substring (1);
  502. Reader.ReadStartElement ();
  503. if (TargetReady (refid))
  504. {
  505. fixupReference = null;
  506. return GetTarget (refid);
  507. }
  508. else
  509. {
  510. fixupReference = refid;
  511. return null;
  512. }
  513. }
  514. }
  515. protected IXmlSerializable ReadSerializable (IXmlSerializable serializable)
  516. {
  517. serializable.ReadXml (reader);
  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. return XmlCustomFormatter.FromXmlString (typeData.Type, Reader.ReadElementString ());
  532. }
  533. protected XmlNode ReadXmlNode (bool wrapped)
  534. {
  535. XmlNode node = Document.ReadNode (reader);
  536. if (wrapped)
  537. return node.FirstChild;
  538. else
  539. return node;
  540. }
  541. [MonoTODO ("Implement")]
  542. protected void Referenced (object o)
  543. {
  544. }
  545. protected Array ShrinkArray (Array a, int length, Type elementType, bool isNullable)
  546. {
  547. if (length == 0 && isNullable) return null;
  548. if (a == null) return Array.CreateInstance (elementType, length);
  549. if (a.Length == length) return a;
  550. Array result = Array.CreateInstance (elementType, length);
  551. Array.Copy (a, result, length);
  552. return result;
  553. }
  554. protected byte[] ToByteArrayBase64 (bool isNull)
  555. {
  556. return Convert.FromBase64String (Reader.ReadString());
  557. }
  558. [MonoTODO ("Implement")]
  559. protected static byte[] ToByteArrayBase64 (string value)
  560. {
  561. throw new NotImplementedException ();
  562. }
  563. [MonoTODO ("Implement")]
  564. protected byte[] ToByteArrayHex (bool isNull)
  565. {
  566. throw new NotImplementedException ();
  567. }
  568. [MonoTODO ("Implement")]
  569. protected static byte[] ToByteArrayHex (string value)
  570. {
  571. throw new NotImplementedException ();
  572. }
  573. protected static char ToChar (string value)
  574. {
  575. return XmlCustomFormatter.ToChar (value);
  576. }
  577. protected static DateTime ToDate (string value)
  578. {
  579. return XmlCustomFormatter.ToDate (value);
  580. }
  581. protected static DateTime ToDateTime (string value)
  582. {
  583. return XmlCustomFormatter.ToDateTime (value);
  584. }
  585. protected static long ToEnum (string value, Hashtable h, string typeName)
  586. {
  587. return XmlCustomFormatter.ToEnum (value, h, typeName, true);
  588. }
  589. protected static DateTime ToTime (string value)
  590. {
  591. return XmlCustomFormatter.ToTime (value);
  592. }
  593. protected static string ToXmlName (string value)
  594. {
  595. return XmlCustomFormatter.ToXmlName (value);
  596. }
  597. protected static string ToXmlNCName (string value)
  598. {
  599. return XmlCustomFormatter.ToXmlNCName (value);
  600. }
  601. protected static string ToXmlNmToken (string value)
  602. {
  603. return XmlCustomFormatter.ToXmlNmToken (value);
  604. }
  605. protected static string ToXmlNmTokens (string value)
  606. {
  607. return XmlCustomFormatter.ToXmlNmTokens (value);
  608. }
  609. protected XmlQualifiedName ToXmlQualifiedName (string value)
  610. {
  611. string name;
  612. string ns;
  613. int lastColon = value.LastIndexOf (':');
  614. string decodedValue = XmlConvert.DecodeName (value);
  615. if (lastColon < 0) {
  616. name = reader.NameTable.Add (decodedValue);
  617. ns = reader.LookupNamespace (String.Empty);
  618. } else {
  619. string prefix = value.Substring (0, lastColon);
  620. ns = reader.LookupNamespace (prefix);
  621. if (ns == null)
  622. throw new InvalidOperationException ("namespace " + prefix + "not defined");
  623. name = reader.NameTable.Add (value.Substring (lastColon + 1));
  624. }
  625. return new XmlQualifiedName (name, ns);
  626. }
  627. protected void UnknownAttribute (object o, XmlAttribute attr)
  628. {
  629. int line_number, line_position;
  630. if (Reader is XmlTextReader){
  631. line_number = ((XmlTextReader)Reader).LineNumber;
  632. line_position = ((XmlTextReader)Reader).LinePosition;
  633. } else {
  634. line_number = 0;
  635. line_position = 0;
  636. }
  637. eventSource.OnUnknownAttribute (new XmlAttributeEventArgs (attr, line_number, line_position, o));
  638. }
  639. protected void UnknownElement (object o, XmlElement elem)
  640. {
  641. int line_number, line_position;
  642. if (Reader is XmlTextReader){
  643. line_number = ((XmlTextReader)Reader).LineNumber;
  644. line_position = ((XmlTextReader)Reader).LinePosition;
  645. } else {
  646. line_number = 0;
  647. line_position = 0;
  648. }
  649. eventSource.OnUnknownElement (new XmlElementEventArgs (elem, line_number, line_position,o));
  650. }
  651. protected void UnknownNode (object o)
  652. {
  653. int line_number, line_position;
  654. if (Reader is XmlTextReader){
  655. line_number = ((XmlTextReader)Reader).LineNumber;
  656. line_position = ((XmlTextReader)Reader).LinePosition;
  657. } else {
  658. line_number = 0;
  659. line_position = 0;
  660. }
  661. eventSource.OnUnknownNode (new XmlNodeEventArgs(line_number, line_position, Reader.LocalName, Reader.Name, Reader.NamespaceURI, Reader.NodeType, o, Reader.Value));
  662. if (Reader.NodeType == XmlNodeType.Attribute)
  663. {
  664. XmlAttribute att = (XmlAttribute) ReadXmlNode (false);
  665. UnknownAttribute (o, att);
  666. return;
  667. }
  668. else if (Reader.NodeType == XmlNodeType.Element)
  669. {
  670. XmlElement elem = (XmlElement) ReadXmlNode (false);
  671. UnknownElement (o, elem);
  672. return;
  673. }
  674. else
  675. {
  676. Reader.Skip();
  677. if (Reader.ReadState == ReadState.EndOfFile)
  678. throw new InvalidOperationException ("End of document found");
  679. }
  680. }
  681. protected void UnreferencedObject (string id, object o)
  682. {
  683. eventSource.OnUnreferencedObject (new UnreferencedObjectEventArgs (o,id));
  684. }
  685. #endregion // Methods
  686. class WriteCallbackInfo
  687. {
  688. public Type Type;
  689. public string TypeName;
  690. public string TypeNs;
  691. public XmlSerializationReadCallback Callback;
  692. }
  693. protected class CollectionFixup {
  694. XmlSerializationCollectionFixupCallback callback;
  695. object collection;
  696. object collectionItems;
  697. string id;
  698. public CollectionFixup (object collection, XmlSerializationCollectionFixupCallback callback, string id)
  699. {
  700. this.callback = callback;
  701. this.collection = collection;
  702. this.id = id;
  703. }
  704. public XmlSerializationCollectionFixupCallback Callback {
  705. get { return callback; }
  706. }
  707. public object Collection {
  708. get { return collection; }
  709. }
  710. public object Id {
  711. get { return id; }
  712. }
  713. internal object CollectionItems
  714. {
  715. get { return collectionItems; }
  716. set { collectionItems = value; }
  717. }
  718. }
  719. protected class Fixup {
  720. object source;
  721. string[] ids;
  722. XmlSerializationFixupCallback callback;
  723. public Fixup (object o, XmlSerializationFixupCallback callback, int count)
  724. {
  725. this.source = o;
  726. this.callback = callback;
  727. this.ids = new string[count];
  728. }
  729. public Fixup (object o, XmlSerializationFixupCallback callback, string[] ids)
  730. {
  731. this.source = o;
  732. this.ids = ids;
  733. this.callback = callback;
  734. }
  735. public XmlSerializationFixupCallback Callback {
  736. get { return callback; }
  737. }
  738. public string[] Ids {
  739. get { return ids; }
  740. }
  741. public object Source {
  742. get { return source; }
  743. set { source = value; }
  744. }
  745. }
  746. protected class CollectionItemFixup
  747. {
  748. Array list;
  749. int index;
  750. string id;
  751. public CollectionItemFixup (Array list, int index, string id)
  752. {
  753. this.list = list;
  754. this.index = index;
  755. this.id = id;
  756. }
  757. public Array Collection
  758. {
  759. get { return list; }
  760. }
  761. public int Index
  762. {
  763. get { return index; }
  764. }
  765. public string Id
  766. {
  767. get { return id; }
  768. }
  769. }
  770. }
  771. }