XmlSerializationReader.cs 22 KB

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