XmlMtomDictionaryWriter.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. settings.ConformanceLevel = ConformanceLevel.Fragment;
  46. settings.NewLineChars = "\r\n";
  47. xml_writer_settings = settings;
  48. // FIXME: actually it does not likely use ContentType.ToString() but writes those header items by own.
  49. // (so that it could generate "start" header dynamically)
  50. var c = new ContentType ("multipart/related");
  51. c.Parameters ["type"] = "application/xop+xml";
  52. c.Boundary = boundary;
  53. c.Parameters ["start"] = "<" + startUri + ">";
  54. c.Parameters ["start-info"] = startInfo;
  55. content_type = c;
  56. }
  57. // constructor arguments
  58. TextWriter writer;
  59. XmlWriterSettings xml_writer_settings;
  60. Encoding encoding;
  61. int max_bytes;
  62. bool write_headers;
  63. bool owns_stream;
  64. ContentType content_type;
  65. // state
  66. XmlWriter w;
  67. int depth;
  68. int section_count;
  69. XmlWriter CreateWriter ()
  70. {
  71. return XmlWriter.Create (writer, xml_writer_settings);
  72. }
  73. public override void Close ()
  74. {
  75. w.Close ();
  76. if (owns_stream)
  77. writer.Close ();
  78. }
  79. public override void Flush ()
  80. {
  81. w.Flush ();
  82. }
  83. public override string LookupPrefix (string namespaceUri)
  84. {
  85. return w.LookupPrefix (namespaceUri);
  86. }
  87. public override void WriteBase64 (byte [] bytes, int start, int length)
  88. {
  89. CheckState ();
  90. w.WriteBase64 (bytes, start, length);
  91. }
  92. public override void WriteCData (string text)
  93. {
  94. CheckState ();
  95. w.WriteCData (text);
  96. }
  97. public override void WriteCharEntity (char c)
  98. {
  99. CheckState ();
  100. w.WriteCharEntity (c);
  101. }
  102. public override void WriteChars (char [] buffer, int index, int count)
  103. {
  104. CheckState ();
  105. w.WriteChars (buffer, index, count);
  106. }
  107. public override void WriteComment (string comment)
  108. {
  109. CheckState ();
  110. w.WriteComment (comment);
  111. }
  112. public override void WriteDocType (string name, string pubid, string sysid, string intSubset)
  113. {
  114. throw new NotSupportedException (); // indeed
  115. }
  116. public override void WriteEndAttribute ()
  117. {
  118. w.WriteEndAttribute ();
  119. }
  120. public override void WriteEndDocument ()
  121. {
  122. // We don't call w.WriteEndElement() because that causes state error
  123. // (which is correct; MTOM writer just does not expect it).
  124. }
  125. public override void WriteEndElement ()
  126. {
  127. w.WriteEndElement ();
  128. if (--depth == 0)
  129. WriteEndOfMimeSection ();
  130. }
  131. public override void WriteEntityRef (string name)
  132. {
  133. w.WriteEntityRef (name);
  134. }
  135. public override void WriteFullEndElement ()
  136. {
  137. w.WriteFullEndElement ();
  138. if (--depth == 0)
  139. WriteEndOfMimeSection ();
  140. }
  141. public override void WriteProcessingInstruction (string name, string data)
  142. {
  143. throw new NotSupportedException ();
  144. }
  145. public override void WriteRaw (string raw)
  146. {
  147. CheckState ();
  148. w.WriteRaw (raw);
  149. }
  150. public override void WriteRaw (char [] chars, int index, int count)
  151. {
  152. CheckState ();
  153. w.WriteRaw (chars, index, count);
  154. }
  155. public override void WriteStartAttribute (string prefix, string localName, string namespaceURI)
  156. {
  157. CheckState ();
  158. w.WriteStartAttribute (prefix, localName, namespaceURI);
  159. }
  160. public override void WriteStartDocument ()
  161. {
  162. CheckState ();
  163. // We don't call w.WriteStartDocument() because that causes state error
  164. // (which is correct; MTOM writer just does not expect it).
  165. }
  166. public override void WriteStartDocument (bool standalone)
  167. {
  168. CheckState ();
  169. // We don't call w.WriteStartDocument() because that causes state error
  170. // (which is correct; MTOM writer just does not expect it).
  171. }
  172. public override void WriteStartElement (string prefix, string localName, string namespaceURI)
  173. {
  174. CheckState ();
  175. if (depth == 0)
  176. WriteStartOfMimeSection ();
  177. w.WriteStartElement (prefix, localName, namespaceURI);
  178. depth++;
  179. }
  180. public override WriteState WriteState {
  181. get { return w.WriteState; }
  182. }
  183. static readonly char [] eol_chars = "\r\n".ToCharArray ();
  184. public override void WriteString (string text)
  185. {
  186. CheckState ();
  187. int i1, i2 = 0;
  188. do {
  189. i1 = text.IndexOfAny (eol_chars, i2);
  190. if (i1 >= 0) {
  191. w.WriteString (text.Substring (i2, i1 - i2));
  192. if (text [i1] == '\r')
  193. w.WriteCharEntity ('\r');
  194. else
  195. w.WriteRaw ("\r\n");
  196. i2 = i1 + 1;
  197. } else {
  198. w.WriteString (text.Substring (i2));
  199. break;
  200. }
  201. } while (true);
  202. }
  203. public override void WriteSurrogateCharEntity (char low, char high)
  204. {
  205. CheckState ();
  206. w.WriteSurrogateCharEntity (low, high);
  207. }
  208. public override void WriteWhitespace (string text)
  209. {
  210. CheckState ();
  211. w.WriteWhitespace (text);
  212. }
  213. public override string XmlLang {
  214. get { return w.XmlLang; }
  215. }
  216. public override XmlSpace XmlSpace {
  217. get { return w.XmlSpace; }
  218. }
  219. void CheckState ()
  220. {
  221. if (w == null && write_headers)
  222. WriteMimeHeaders ();
  223. if (w == null || w.WriteState == WriteState.Closed || w.WriteState == WriteState.Error)
  224. w = CreateWriter ();
  225. }
  226. void WriteMimeHeaders ()
  227. {
  228. w.Flush ();
  229. writer.Write ("MIME-Version: 1.0\r\n");
  230. writer.Write ("Content-Type: ");
  231. writer.Write (content_type.ToString ());
  232. writer.Write ("\r\n\r\n\r\n");
  233. }
  234. void WriteStartOfMimeSection ()
  235. {
  236. section_count++;
  237. // I'm not sure what's the expected behavior of this
  238. // strange XmlWriter, but so far - it outputs only one
  239. // section.
  240. if (section_count > 1)
  241. return;
  242. writer.Write ("\r\n");
  243. writer.Write ("--");
  244. writer.Write (content_type.Boundary);
  245. writer.Write ("\r\n");
  246. writer.Write ("Content-ID: ");
  247. writer.Write (content_type.Parameters ["start"]);
  248. writer.Write ("\r\n");
  249. writer.Write ("Content-Transfer-Encoding: 8bit\r\n");
  250. writer.Write ("Content-Type: application/xop+xml;charset=");
  251. writer.Write (xml_writer_settings.Encoding.HeaderName);
  252. writer.Write (";type=\"");
  253. writer.Write (content_type.Parameters ["start-info"].Replace ("\"", "\\\""));
  254. writer.Write ("\"\r\n\r\n");
  255. }
  256. void WriteEndOfMimeSection ()
  257. {
  258. // I'm not sure what's the expected behavior of this
  259. // strange XmlWriter, but so far - it outputs only one
  260. // section.
  261. if (section_count > 1)
  262. return;
  263. w.Flush ();
  264. writer.Write ("\r\n");
  265. writer.Write ("--");
  266. writer.Write (content_type.Boundary);
  267. writer.Write ("--\r\n");
  268. }
  269. }
  270. }