XmlSerializationWriter.cs 13 KB

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