XmlSerializationWriter.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. //
  2. // System.Xml.Serialization.XmlSerializationWriter.cs
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. //
  7. // Copyright (C) Tim Coleman, 2002
  8. //
  9. using System;
  10. using System.Collections;
  11. using System.Text;
  12. using System.Xml;
  13. using System.Xml.Schema;
  14. namespace System.Xml.Serialization {
  15. public abstract class XmlSerializationWriter {
  16. #region Fields
  17. Hashtable references;
  18. int referenceCount;
  19. int qnameCount;
  20. ArrayList namespaces;
  21. XmlWriter writer;
  22. #endregion // Fields
  23. #region Constructors
  24. [MonoTODO]
  25. protected XmlSerializationWriter ()
  26. {
  27. qnameCount = 0;
  28. references = new Hashtable ();
  29. referenceCount = 0;
  30. }
  31. #endregion // Constructors
  32. #region Properties
  33. protected ArrayList Namespaces {
  34. get { return namespaces; }
  35. set { namespaces = value; }
  36. }
  37. protected XmlWriter Writer {
  38. get { return writer; }
  39. set { writer = value; }
  40. }
  41. #endregion // Properties
  42. #region Methods
  43. [MonoTODO ("Implement")]
  44. protected void AddWriteCallback (Type type, string typeName, string typeNs, XmlSerializationWriteCallback callback)
  45. {
  46. throw new NotImplementedException ();
  47. }
  48. protected Exception CreateMismatchChoiceException (string value, string elementName, string enumValue)
  49. {
  50. string message = String.Format ("Value of {0} mismatches the type of {1}, you need to set it to {2}.", elementName, value, enumValue);
  51. return new InvalidOperationException (message);
  52. }
  53. protected Exception CreateUnknownAnyElementException (string name, string ns)
  54. {
  55. string message = String.Format ("The XML element named '{0}' from namespace '{1}' was not expected. The XML element name and namespace must match those provided via XmlAnyElementAttribute(s).", name, ns);
  56. return new InvalidOperationException (message);
  57. }
  58. protected Exception CreateUnknownTypeException (object o)
  59. {
  60. return CreateUnknownTypeException (o.GetType ());
  61. }
  62. protected Exception CreateUnknownTypeException (Type type)
  63. {
  64. string message = String.Format ("The type {0} may not be used in this context.", type);
  65. return new InvalidOperationException (message);
  66. }
  67. protected static string FromByteArrayBase64 (byte[] value)
  68. {
  69. return XmlCustomFormatter.FromByteArrayBase64 (value);
  70. }
  71. protected static string FromByteArrayHex (byte[] value)
  72. {
  73. return XmlCustomFormatter.FromByteArrayHex (value);
  74. }
  75. protected static string FromChar (char value)
  76. {
  77. return XmlCustomFormatter.FromChar (value);
  78. }
  79. protected static string FromDate (DateTime value)
  80. {
  81. return XmlCustomFormatter.FromDate (value);
  82. }
  83. protected static string FromDateTime (DateTime value)
  84. {
  85. return XmlCustomFormatter.FromDateTime (value);
  86. }
  87. protected static string FromEnum (long value, string[] values, long[] ids)
  88. {
  89. return XmlCustomFormatter.FromEnum (value, values, ids);
  90. }
  91. protected static string FromTime (DateTime value)
  92. {
  93. return XmlCustomFormatter.FromTime (value);
  94. }
  95. protected static string FromXmlName (string name)
  96. {
  97. return XmlCustomFormatter.FromXmlName (name);
  98. }
  99. protected static string FromXmlNCName (string ncName)
  100. {
  101. return XmlCustomFormatter.FromXmlNCName (ncName);
  102. }
  103. protected static string FromXmlNmToken (string nmToken)
  104. {
  105. return XmlCustomFormatter.FromXmlNmToken (nmToken);
  106. }
  107. protected static string FromXmlNmTokens (string nmTokens)
  108. {
  109. return XmlCustomFormatter.FromXmlNmTokens (nmTokens);
  110. }
  111. [MonoTODO ("Implement")]
  112. protected string FromXmlQualifiedName (XmlQualifiedName xmlQualifiedName)
  113. {
  114. return GetQualifiedName (xmlQualifiedName.Name, xmlQualifiedName.Namespace);
  115. }
  116. private string GetId (object o, bool addToReferencesList)
  117. {
  118. referenceCount += 1;
  119. string id = String.Format ("id{0}", referenceCount);
  120. if (addToReferencesList)
  121. references[o] = id;
  122. return id;
  123. }
  124. [MonoTODO ("Complete this list.")]
  125. private string GetPrimitiveTypeName (Type type)
  126. {
  127. if (type == typeof (string))
  128. return "string";
  129. if (type == typeof (bool))
  130. return "Boolean";
  131. if (type == typeof (float))
  132. return "float";
  133. if (type == typeof (double))
  134. return "double";
  135. if (type == typeof (XmlQualifiedName))
  136. return "QName";
  137. return String.Empty;
  138. }
  139. [MonoTODO ("Need to check for namespace conflicts before blindly allocating qN")]
  140. private string GetQualifiedName (string name, string ns)
  141. {
  142. qnameCount += 1;
  143. string prefix = String.Format ("q{0}", qnameCount);
  144. WriteAttribute ("xmlns", prefix, null, ns);
  145. return String.Format ("{0}:{1}", prefix, name);
  146. }
  147. protected abstract void InitCallbacks ();
  148. [MonoTODO ("Implement")]
  149. protected void TopLevelElement ()
  150. {
  151. throw new NotImplementedException ();
  152. }
  153. protected void WriteAttribute (string localName, byte[] value)
  154. {
  155. WriteAttribute (localName, String.Empty, value);
  156. }
  157. protected void WriteAttribute (string localName, string value)
  158. {
  159. WriteAttribute (String.Empty, localName, String.Empty, value);
  160. }
  161. protected void WriteAttribute (string localName, string ns, byte[] value)
  162. {
  163. Writer.WriteStartAttribute (localName, ns);
  164. WriteValue (value);
  165. Writer.WriteEndAttribute ();
  166. }
  167. protected void WriteAttribute (string localName, string ns, string value)
  168. {
  169. WriteAttribute (String.Empty, localName, ns, value);
  170. }
  171. protected void WriteAttribute (string prefix, string localName, string ns, string value)
  172. {
  173. Writer.WriteStartAttribute (prefix, localName, ns);
  174. WriteValue (value);
  175. Writer.WriteEndAttribute ();
  176. }
  177. [MonoTODO ("Implement")]
  178. protected void WriteElementEncoded (XmlNode node, string name, string ns, bool isNullable, bool any)
  179. {
  180. throw new NotImplementedException ();
  181. }
  182. [MonoTODO ("Implement")]
  183. protected void WriteElementLiteral (XmlNode node, string name, string ns, bool isNullable, bool any)
  184. {
  185. WriteStartElement (name, ns);
  186. node.WriteTo (Writer);
  187. WriteEndElement ();
  188. }
  189. protected void WriteElementQualifiedName (string localName, XmlQualifiedName value)
  190. {
  191. WriteElementQualifiedName (localName, String.Empty, value, null);
  192. }
  193. protected void WriteElementQualifiedName (string localName, string ns, XmlQualifiedName value)
  194. {
  195. WriteElementQualifiedName (localName, ns, value, null);
  196. }
  197. protected void WriteElementQualifiedName (string localName, XmlQualifiedName value, XmlQualifiedName xsiType)
  198. {
  199. WriteElementQualifiedName (localName, String.Empty, value, xsiType);
  200. }
  201. protected void WriteElementQualifiedName (string localName, string ns, XmlQualifiedName value, XmlQualifiedName xsiType)
  202. {
  203. WriteStartElement (localName, ns);
  204. Writer.WriteString (FromXmlQualifiedName (value));
  205. WriteEndElement ();
  206. }
  207. protected void WriteElementString (string localName, string value)
  208. {
  209. WriteElementString (localName, String.Empty, value, null);
  210. }
  211. protected void WriteElementString (string localName, string ns, string value)
  212. {
  213. WriteElementString (localName, ns, value, null);
  214. }
  215. protected void WriteElementString (string localName, string value, XmlQualifiedName xsiType)
  216. {
  217. WriteElementString (localName, String.Empty, value, xsiType);
  218. }
  219. [MonoTODO ("Implement")]
  220. protected void WriteElementString (string localName, string ns, string value, XmlQualifiedName xsiType)
  221. {
  222. if (xsiType != null) {
  223. WriteStartElement (localName, ns);
  224. WriteXsiType (xsiType.Name, xsiType.Namespace);
  225. Writer.WriteString (value);
  226. WriteEndElement ();
  227. }
  228. else
  229. Writer.WriteElementString (localName, ns, value);
  230. }
  231. protected void WriteElementStringRaw (string localName, byte[] value)
  232. {
  233. WriteElementStringRaw (localName, String.Empty, value, null);
  234. }
  235. protected void WriteElementStringRaw (string localName, string value)
  236. {
  237. WriteElementStringRaw (localName, String.Empty, value, null);
  238. }
  239. protected void WriteElementStringRaw (string localName, byte[] value, XmlQualifiedName xsiType)
  240. {
  241. WriteElementStringRaw (localName, String.Empty, value, xsiType);
  242. }
  243. protected void WriteElementStringRaw (string localName, string ns, byte[] value)
  244. {
  245. WriteElementStringRaw (localName, ns, value, null);
  246. }
  247. protected void WriteElementStringRaw (string localName, string ns, string value)
  248. {
  249. WriteElementStringRaw (localName, ns, value, null);
  250. }
  251. protected void WriteElementStringRaw (string localName, string value, XmlQualifiedName xsiType)
  252. {
  253. WriteElementStringRaw (localName, String.Empty, value, null);
  254. }
  255. [MonoTODO ("Implement")]
  256. protected void WriteElementStringRaw (string localName, string ns, byte[] value, XmlQualifiedName xsiType)
  257. {
  258. throw new NotImplementedException ();
  259. }
  260. [MonoTODO ("Implement")]
  261. protected void WriteElementStringRaw (string localName, string ns, string value, XmlQualifiedName xsiType)
  262. {
  263. WriteStartElement (localName, ns);
  264. if (xsiType != null)
  265. WriteXsiType (xsiType.Name, xsiType.Namespace);
  266. Writer.WriteRaw (value);
  267. WriteEndElement ();
  268. }
  269. protected void WriteEmptyTag (string name)
  270. {
  271. WriteEmptyTag (name, String.Empty);
  272. }
  273. [MonoTODO ("Verify")]
  274. protected void WriteEmptyTag (string name, string ns)
  275. {
  276. Writer.WriteStartElement (name, ns);
  277. Writer.WriteEndElement ();
  278. }
  279. protected void WriteEndElement ()
  280. {
  281. WriteEndElement (null);
  282. }
  283. [MonoTODO ("Implement")]
  284. protected void WriteEndElement (object o)
  285. {
  286. Writer.WriteEndElement ();
  287. }
  288. protected void WriteId (object o)
  289. {
  290. WriteAttribute ("id", GetId (o, true));
  291. }
  292. protected void WriteNamespaceDeclarations (XmlSerializerNamespaces ns)
  293. {
  294. if (ns == null)
  295. return;
  296. Hashtable tbl = ns.Namespaces;
  297. foreach (string key in tbl.Keys) {
  298. string val = tbl [key] as string;
  299. if (val == null)
  300. val = String.Empty;
  301. WriteAttribute ("xmlns", key, null, val);
  302. }
  303. }
  304. [MonoTODO ("Implement")]
  305. protected void WriteNullableQualifiedNameEncoded (string name, string ns, XmlQualifiedName value, XmlQualifiedName xsiType)
  306. {
  307. throw new NotImplementedException ();
  308. }
  309. [MonoTODO ("Implement")]
  310. protected void WriteNullableQualifiedNameLiteral (string name, string ns, XmlQualifiedName value)
  311. {
  312. throw new NotImplementedException ();
  313. }
  314. [MonoTODO ("Implement")]
  315. protected void WriteNullableStringEncoded (string name, string ns, string value, XmlQualifiedName xsiType)
  316. {
  317. throw new NotImplementedException ();
  318. }
  319. [MonoTODO ("Implement")]
  320. protected void WriteNullableStringEncodedRaw (string name, string ns, byte[] value, XmlQualifiedName xsiType)
  321. {
  322. throw new NotImplementedException ();
  323. }
  324. [MonoTODO ("Implement")]
  325. protected void WriteNullableStringEncodedRaw (string name, string ns, string value, XmlQualifiedName xsiType)
  326. {
  327. throw new NotImplementedException ();
  328. }
  329. [MonoTODO ("Implement")]
  330. protected void WriteNullableStringLiteral (string name, string ns, string value)
  331. {
  332. throw new NotImplementedException ();
  333. }
  334. [MonoTODO ("Implement")]
  335. protected void WriteNullableStringLiteralRaw (string name, string ns, byte[] value)
  336. {
  337. throw new NotImplementedException ();
  338. }
  339. [MonoTODO ("Implement")]
  340. protected void WriteNullableStringLiteralRaw (string name, string ns, string value)
  341. {
  342. throw new NotImplementedException ();
  343. }
  344. protected void WriteNullTagEncoded (string name)
  345. {
  346. WriteNullTagEncoded (name, String.Empty);
  347. }
  348. [MonoTODO ("Implement")]
  349. protected void WriteNullTagEncoded (string name, string ns)
  350. {
  351. throw new NotImplementedException ();
  352. }
  353. protected void WriteNullTagLiteral (string name)
  354. {
  355. WriteNullTagLiteral (name, String.Empty);
  356. }
  357. [MonoTODO ("Implement")]
  358. protected void WriteNullTagLiteral (string name, string ns)
  359. {
  360. throw new NotImplementedException ();
  361. }
  362. protected void WritePotentiallyReferencingElement (string n, string ns, object o)
  363. {
  364. WritePotentiallyReferencingElement (n, ns, o, null, false, false);
  365. }
  366. protected void WritePotentiallyReferencingElement (string n, string ns, object o, Type ambientType)
  367. {
  368. WritePotentiallyReferencingElement (n, ns, o, ambientType, false, false);
  369. }
  370. protected void WritePotentiallyReferencingElement (string n, string ns, object o, Type ambientType, bool suppressReference)
  371. {
  372. WritePotentiallyReferencingElement (n, ns, o, ambientType, suppressReference, false);
  373. }
  374. [MonoTODO ("Implement")]
  375. protected void WritePotentiallyReferencingElement (string n, string ns, object o, Type ambientType, bool suppressReference, bool isNullable)
  376. {
  377. throw new NotImplementedException ();
  378. }
  379. [MonoTODO ("Implement")]
  380. protected void WriteReferencedElements ()
  381. {
  382. throw new NotImplementedException ();
  383. }
  384. protected void WriteReferencingElement (string n, string ns, object o)
  385. {
  386. WriteReferencingElement (n, ns, o, false);
  387. }
  388. [MonoTODO ("Implement")]
  389. protected void WriteReferencingElement (string n, string ns, object o, bool isNullable)
  390. {
  391. throw new NotImplementedException ();
  392. }
  393. [MonoTODO ("Implement")]
  394. protected void WriteSerializable (IXmlSerializable serializable, string name, string ns, bool isNullable)
  395. {
  396. throw new NotImplementedException ();
  397. }
  398. protected void WriteStartDocument ()
  399. {
  400. Writer.WriteStartDocument ();
  401. }
  402. protected void WriteStartElement (string name)
  403. {
  404. WriteStartElement (name, String.Empty, null, false);
  405. }
  406. protected void WriteStartElement (string name, string ns)
  407. {
  408. WriteStartElement (name, ns, null, false);
  409. }
  410. protected void WriteStartElement (string name, string ns, bool writePrefixed)
  411. {
  412. WriteStartElement (name, ns, null, writePrefixed);
  413. }
  414. protected void WriteStartElement (string name, string ns, object o)
  415. {
  416. WriteStartElement (name, ns, o, false);
  417. }
  418. [MonoTODO]
  419. protected void WriteStartElement (string name, string ns, object o, bool writePrefixed)
  420. {
  421. if (writePrefixed)
  422. Writer.WriteStartElement (String.Empty, name, ns);
  423. else
  424. Writer.WriteStartElement (name, ns);
  425. }
  426. protected void WriteTypedPrimitive (string name, string ns, object o, bool xsiType)
  427. {
  428. string value;
  429. WriteStartElement (name, ns);
  430. if (o is XmlQualifiedName)
  431. value = FromXmlQualifiedName ((XmlQualifiedName) o);
  432. else
  433. value = o.ToString ();
  434. if (xsiType)
  435. WriteXsiType (GetPrimitiveTypeName (o.GetType ()), XmlSchema.Namespace);
  436. WriteValue (value);
  437. WriteEndElement ();
  438. }
  439. protected void WriteValue (byte[] value)
  440. {
  441. Writer.WriteBase64 (value, 0, value.Length);
  442. }
  443. protected void WriteValue (string value)
  444. {
  445. Writer.WriteString (value);
  446. }
  447. protected void WriteXmlAttribute (XmlNode node)
  448. {
  449. WriteXmlAttribute (node, null);
  450. }
  451. [MonoTODO ("Implement")]
  452. protected void WriteXmlAttribute (XmlNode node, object container)
  453. {
  454. if (!(node is XmlAttribute))
  455. throw new InvalidOperationException ("The node must be either type XmlAttribute or a derived type.");
  456. throw new NotImplementedException ();
  457. }
  458. protected void WriteXsiType (string name, string ns)
  459. {
  460. WriteAttribute ("type", XmlSchema.InstanceNamespace, GetQualifiedName (name, ns));
  461. }
  462. #endregion
  463. }
  464. }