XmlMtomDictionaryWriter.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //
  2. // XmlMtomDictionaryWriter.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2009 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.IO;
  30. using System.Net.Mime;
  31. using System.Text;
  32. namespace System.Xml
  33. {
  34. internal class XmlMtomDictionaryWriter : XmlDictionaryWriter
  35. {
  36. public XmlMtomDictionaryWriter (Stream stream, Encoding encoding, int maxSizeInBytes, string startInfo, string boundary, string startUri, bool writeMessageHeaders, bool ownsStream)
  37. {
  38. writer = new StreamWriter (stream, encoding);
  39. max_bytes = maxSizeInBytes;
  40. write_headers = writeMessageHeaders;
  41. owns_stream = ownsStream;
  42. var settings = new XmlWriterSettings ();
  43. settings.Encoding = encoding;
  44. settings.OmitXmlDeclaration = true;
  45. xml_writer_settings = settings;
  46. // FIXME: actually it does not likely use ContentType.ToString() but writes those header items by own.
  47. // (so that it could generate "start" header dynamically)
  48. var c = new ContentType ("multipart/related");
  49. c.Parameters ["type"] = "application/xop+xml";
  50. c.Boundary = boundary;
  51. c.Parameters ["start"] = "<" + startUri + ">";
  52. c.Parameters ["start-info"] = startInfo;
  53. content_type = c;
  54. }
  55. // constructor arguments
  56. TextWriter writer;
  57. XmlWriterSettings xml_writer_settings;
  58. Encoding encoding;
  59. int max_bytes;
  60. bool write_headers;
  61. bool owns_stream;
  62. ContentType content_type;
  63. // state
  64. XmlWriter w;
  65. int depth;
  66. int section_count;
  67. XmlWriter CreateWriter ()
  68. {
  69. return XmlWriter.Create (writer, xml_writer_settings);
  70. }
  71. public override void Close ()
  72. {
  73. w.Close ();
  74. if (owns_stream)
  75. writer.Close ();
  76. }
  77. public override void Flush ()
  78. {
  79. w.Flush ();
  80. }
  81. public override string LookupPrefix (string namespaceUri)
  82. {
  83. return w.LookupPrefix (namespaceUri);
  84. }
  85. public override void WriteBase64 (byte [] bytes, int start, int length)
  86. {
  87. CheckState ();
  88. w.WriteBase64 (bytes, start, length);
  89. }
  90. public override void WriteCData (string text)
  91. {
  92. CheckState ();
  93. w.WriteCData (text);
  94. }
  95. public override void WriteCharEntity (char c)
  96. {
  97. CheckState ();
  98. w.WriteCharEntity (c);
  99. }
  100. public override void WriteChars (char [] buffer, int index, int count)
  101. {
  102. CheckState ();
  103. w.WriteChars (buffer, index, count);
  104. }
  105. public override void WriteComment (string comment)
  106. {
  107. CheckState ();
  108. w.WriteComment (comment);
  109. }
  110. public override void WriteDocType (string name, string pubid, string sysid, string intSubset)
  111. {
  112. throw new NotSupportedException (); // indeed
  113. }
  114. public override void WriteEndAttribute ()
  115. {
  116. w.WriteEndAttribute ();
  117. }
  118. public override void WriteEndDocument ()
  119. {
  120. w.WriteEndDocument ();
  121. }
  122. public override void WriteEndElement ()
  123. {
  124. w.WriteEndElement ();
  125. if (--depth == 0)
  126. WriteEndOfMimeSection ();
  127. }
  128. public override void WriteEntityRef (string name)
  129. {
  130. w.WriteEntityRef (name);
  131. }
  132. public override void WriteFullEndElement ()
  133. {
  134. w.WriteFullEndElement ();
  135. if (--depth == 0)
  136. WriteEndOfMimeSection ();
  137. }
  138. public override void WriteProcessingInstruction (string name, string data)
  139. {
  140. throw new NotSupportedException ();
  141. }
  142. public override void WriteRaw (string raw)
  143. {
  144. CheckState ();
  145. w.WriteRaw (raw);
  146. }
  147. public override void WriteRaw (char [] chars, int index, int count)
  148. {
  149. CheckState ();
  150. w.WriteRaw (chars, index, count);
  151. }
  152. public override void WriteStartAttribute (string prefix, string localName, string namespaceURI)
  153. {
  154. CheckState ();
  155. w.WriteStartAttribute (prefix, localName, namespaceURI);
  156. }
  157. public override void WriteStartDocument ()
  158. {
  159. CheckState ();
  160. w.WriteStartDocument ();
  161. }
  162. public override void WriteStartDocument (bool standalone)
  163. {
  164. CheckState ();
  165. w.WriteStartDocument (standalone);
  166. }
  167. public override void WriteStartElement (string prefix, string localName, string namespaceURI)
  168. {
  169. CheckState ();
  170. if (depth == 0)
  171. WriteStartOfMimeSection ();
  172. w.WriteStartElement (prefix, localName, namespaceURI);
  173. depth++;
  174. }
  175. public override WriteState WriteState {
  176. get { return w.WriteState; }
  177. }
  178. public override void WriteString (string text)
  179. {
  180. CheckState ();
  181. int i1, i2 = 0;
  182. do {
  183. i1 = text.IndexOf ('\r', i2);
  184. if (i1 >= 0) {
  185. w.WriteString (text.Substring (i2, i1 - i2));
  186. WriteCharEntity ('\r');
  187. i2 = i1 + 1;
  188. } else {
  189. w.WriteString (text.Substring (i2));
  190. break;
  191. }
  192. } while (true);
  193. }
  194. public override void WriteSurrogateCharEntity (char low, char high)
  195. {
  196. CheckState ();
  197. w.WriteSurrogateCharEntity (low, high);
  198. }
  199. public override void WriteWhitespace (string text)
  200. {
  201. CheckState ();
  202. w.WriteWhitespace (text);
  203. }
  204. public override string XmlLang {
  205. get { return w.XmlLang; }
  206. }
  207. public override XmlSpace XmlSpace {
  208. get { return w.XmlSpace; }
  209. }
  210. void CheckState ()
  211. {
  212. if (w == null && write_headers)
  213. WriteMimeHeaders ();
  214. if (w == null || w.WriteState == WriteState.Closed || w.WriteState == WriteState.Error)
  215. w = CreateWriter ();
  216. }
  217. void WriteMimeHeaders ()
  218. {
  219. writer.Write ("MIME-Version: 1.0\r\n");
  220. writer.Write ("Content-Type: ");
  221. writer.Write (content_type.ToString ());
  222. writer.Write ("\r\n\r\n\r\n");
  223. }
  224. void WriteStartOfMimeSection ()
  225. {
  226. section_count++;
  227. // I'm not sure what's the expected behavior of this
  228. // strange XmlWriter, but so far - it outputs only one
  229. // section.
  230. if (section_count > 1)
  231. return;
  232. writer.Write ("\r\n");
  233. writer.Write ("--");
  234. writer.Write (content_type.Boundary);
  235. writer.Write ("\r\n");
  236. writer.Write ("Content-ID: ");
  237. writer.Write (content_type.Parameters ["start"]);
  238. writer.Write ("\r\n");
  239. writer.Write ("Content-Transfer-Encoding: 8bit\r\n");
  240. writer.Write ("Content-Type: application/xop+xml;charset=");
  241. writer.Write (xml_writer_settings.Encoding.HeaderName);
  242. writer.Write (";type=\"");
  243. writer.Write (content_type.Parameters ["start-info"].Replace ("\"", "\\\""));
  244. writer.Write ("\"\r\n\r\n");
  245. }
  246. void WriteEndOfMimeSection ()
  247. {
  248. // I'm not sure what's the expected behavior of this
  249. // strange XmlWriter, but so far - it outputs only one
  250. // section.
  251. if (section_count > 1)
  252. return;
  253. writer.Write ("\r\n");
  254. writer.Write ("--");
  255. writer.Write (content_type.Boundary);
  256. writer.Write ("--\r\n");
  257. }
  258. }
  259. }