XmlDictionaryWriter.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. //
  2. // XmlDictionaryWriter.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005 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. #if NET_2_0
  29. using System;
  30. using System.IO;
  31. using System.Text;
  32. namespace System.Xml
  33. {
  34. public abstract partial class XmlDictionaryWriter : XmlWriter
  35. {
  36. static readonly Encoding utf8_unmarked = new UTF8Encoding (false);
  37. int depth;
  38. protected XmlDictionaryWriter ()
  39. {
  40. }
  41. internal int Depth {
  42. get { return depth; }
  43. set { depth = value; }
  44. }
  45. public virtual bool CanCanonicalize {
  46. get { return false; }
  47. }
  48. public static XmlDictionaryWriter CreateBinaryWriter (
  49. Stream stream)
  50. {
  51. return CreateBinaryWriter (stream, null, null, false);
  52. }
  53. public static XmlDictionaryWriter CreateBinaryWriter (
  54. Stream stream, IXmlDictionary dictionary)
  55. {
  56. return CreateBinaryWriter (stream, dictionary, null, false);
  57. }
  58. public static XmlDictionaryWriter CreateBinaryWriter (
  59. Stream stream, IXmlDictionary dictionary,
  60. XmlBinaryWriterSession session)
  61. {
  62. return CreateBinaryWriter (stream, dictionary, session, false);
  63. }
  64. public static XmlDictionaryWriter CreateBinaryWriter (
  65. Stream stream, IXmlDictionary dictionary,
  66. XmlBinaryWriterSession session, bool ownsStream)
  67. {
  68. return new XmlBinaryDictionaryWriter (stream,
  69. dictionary, session, ownsStream);
  70. }
  71. public static XmlDictionaryWriter CreateDictionaryWriter (XmlWriter writer)
  72. {
  73. return new XmlSimpleDictionaryWriter (writer);
  74. }
  75. public static XmlDictionaryWriter CreateMtomWriter (
  76. Stream stream, Encoding encoding, int maxSizeInBytes,
  77. string startInfo)
  78. {
  79. return CreateMtomWriter (stream, encoding,
  80. maxSizeInBytes, startInfo, Guid.NewGuid () + "id=1", "http://tempuri.org/0/" + DateTime.Now.Ticks, true, false);
  81. }
  82. public static XmlDictionaryWriter CreateMtomWriter (
  83. Stream stream, Encoding encoding, int maxSizeInBytes,
  84. string startInfo, string boundary, string startUri,
  85. bool writeMessageHeaders, bool ownsStream)
  86. {
  87. return new XmlMtomDictionaryWriter (stream, encoding, maxSizeInBytes, startInfo, boundary, startUri, writeMessageHeaders, ownsStream);
  88. }
  89. public static XmlDictionaryWriter CreateTextWriter (
  90. Stream stream)
  91. {
  92. return CreateTextWriter (stream, Encoding.UTF8);
  93. }
  94. public static XmlDictionaryWriter CreateTextWriter (
  95. Stream stream, Encoding encoding)
  96. {
  97. return CreateTextWriter (stream, encoding, false);
  98. }
  99. // BTW looks like it creates an instance of different
  100. // implementation than those from XmlWriter.Create().
  101. public static XmlDictionaryWriter CreateTextWriter (
  102. Stream stream, Encoding encoding, bool ownsStream)
  103. {
  104. if (stream == null)
  105. throw new ArgumentNullException ("stream");
  106. if (encoding == null)
  107. throw new ArgumentNullException ("encoding");
  108. switch (encoding.CodePage) {
  109. case 1200:
  110. case 1201: // utf-16
  111. case 65001: // utf-8
  112. encoding = utf8_unmarked;
  113. break;
  114. default:
  115. throw new XmlException (String.Format ("XML declaration is required for encoding code page {0} but this XmlWriter does not support XML declaration.", encoding.CodePage));
  116. }
  117. XmlWriterSettings s = new XmlWriterSettings ();
  118. s.Encoding = encoding;
  119. s.CloseOutput = ownsStream;
  120. s.OmitXmlDeclaration = true;
  121. return CreateDictionaryWriter (XmlWriter.Create (stream, s));
  122. }
  123. public virtual void EndCanonicalization ()
  124. {
  125. throw new NotSupportedException ();
  126. }
  127. public virtual void StartCanonicalization (
  128. Stream stream, bool includeComments,
  129. string [] inclusivePrefixes)
  130. {
  131. throw new NotSupportedException ();
  132. }
  133. public void WriteAttributeString (
  134. XmlDictionaryString localName,
  135. XmlDictionaryString namespaceUri,
  136. string value)
  137. {
  138. WriteAttributeString (null, localName, namespaceUri, value);
  139. }
  140. public void WriteAttributeString (string prefix,
  141. XmlDictionaryString localName,
  142. XmlDictionaryString namespaceUri,
  143. string value)
  144. {
  145. WriteStartAttribute (prefix, localName, namespaceUri);
  146. WriteString (value);
  147. WriteEndAttribute ();
  148. }
  149. public void WriteElementString (
  150. XmlDictionaryString localName,
  151. XmlDictionaryString namespaceUri,
  152. string value)
  153. {
  154. WriteElementString (null, localName, namespaceUri, value);
  155. }
  156. public void WriteElementString (string prefix,
  157. XmlDictionaryString localName,
  158. XmlDictionaryString namespaceUri,
  159. string value)
  160. {
  161. WriteStartElement (prefix, localName, namespaceUri);
  162. WriteString (value);
  163. WriteEndElement ();
  164. }
  165. [MonoTODO ("make use of dictionary reader optimization")]
  166. public virtual void WriteNode (XmlDictionaryReader reader,
  167. bool defattr)
  168. {
  169. if (reader == null)
  170. throw new ArgumentNullException ("reader");
  171. switch (reader.NodeType) {
  172. case XmlNodeType.Element:
  173. // gratuitously copied from System.XML/System.Xml/XmlWriter.cs:WriteNode(XmlReader,bool)
  174. // as there doesn't seem to be a way to hook into attribute writing w/o handling Element.
  175. WriteStartElement (reader.Prefix, reader.LocalName, reader.NamespaceURI);
  176. // Well, I found that MS.NET took this way, since
  177. // there was a error-prone SgmlReader that fails
  178. // MoveToNextAttribute().
  179. if (reader.HasAttributes) {
  180. for (int i = 0; i < reader.AttributeCount; i++) {
  181. reader.MoveToAttribute (i);
  182. WriteAttribute (reader, defattr);
  183. }
  184. reader.MoveToElement ();
  185. }
  186. if (reader.IsEmptyElement)
  187. WriteEndElement ();
  188. else {
  189. int depth = reader.Depth;
  190. reader.Read ();
  191. if (reader.NodeType != XmlNodeType.EndElement) {
  192. do {
  193. WriteNode (reader, defattr);
  194. } while (depth < reader.Depth);
  195. }
  196. WriteFullEndElement ();
  197. }
  198. reader.Read ();
  199. break;
  200. case XmlNodeType.Attribute:
  201. case XmlNodeType.Text:
  202. WriteTextNode (reader, defattr);
  203. break;
  204. default:
  205. base.WriteNode (reader, defattr);
  206. break;
  207. }
  208. }
  209. private void WriteAttribute (XmlDictionaryReader reader, bool defattr)
  210. {
  211. if (!defattr && reader.IsDefault)
  212. return;
  213. WriteStartAttribute (reader.Prefix, reader.LocalName, reader.NamespaceURI);
  214. #if NET_2_1
  215. // no ReadAttributeValue() in 2.1 profile.
  216. WriteTextNode (reader, true);
  217. #else
  218. while (reader.ReadAttributeValue ()) {
  219. switch (reader.NodeType) {
  220. case XmlNodeType.Text:
  221. WriteTextNode (reader, true);
  222. break;
  223. case XmlNodeType.EntityReference:
  224. WriteEntityRef (reader.Name);
  225. break;
  226. }
  227. }
  228. #endif
  229. WriteEndAttribute ();
  230. }
  231. public override void WriteNode (XmlReader reader, bool defattr)
  232. {
  233. if (reader == null)
  234. throw new ArgumentNullException ("reader");
  235. XmlDictionaryReader dr = reader as XmlDictionaryReader;
  236. if (dr != null)
  237. WriteNode (dr, defattr);
  238. else
  239. base.WriteNode (reader, defattr);
  240. }
  241. public virtual void WriteQualifiedName (
  242. XmlDictionaryString localName,
  243. XmlDictionaryString namespaceUri)
  244. {
  245. WriteQualifiedName (localName.Value, namespaceUri.Value);
  246. }
  247. public void WriteStartAttribute (
  248. XmlDictionaryString localName,
  249. XmlDictionaryString namespaceUri)
  250. {
  251. WriteStartAttribute (localName.Value, namespaceUri.Value);
  252. }
  253. public virtual void WriteStartAttribute (string prefix,
  254. XmlDictionaryString localName,
  255. XmlDictionaryString namespaceUri)
  256. {
  257. WriteStartAttribute (prefix, localName.Value, namespaceUri.Value);
  258. }
  259. public void WriteStartElement (
  260. XmlDictionaryString localName,
  261. XmlDictionaryString namespaceUri)
  262. {
  263. WriteStartElement (null, localName, namespaceUri);
  264. }
  265. public virtual void WriteStartElement (string prefix,
  266. XmlDictionaryString localName,
  267. XmlDictionaryString namespaceUri)
  268. {
  269. if (localName == null)
  270. throw new ArgumentException ("localName must not be null.", "localName");
  271. WriteStartElement (prefix, localName.Value,
  272. namespaceUri != null ? namespaceUri.Value : null);
  273. }
  274. public virtual void WriteString (XmlDictionaryString value)
  275. {
  276. WriteString (value.Value);
  277. }
  278. protected virtual void WriteTextNode (XmlDictionaryReader reader, bool isAttribute)
  279. {
  280. WriteString (reader.Value);
  281. if (!isAttribute)
  282. reader.Read ();
  283. }
  284. public virtual void WriteValue (Guid guid)
  285. {
  286. WriteString (guid.ToString ());
  287. }
  288. public virtual void WriteValue (IStreamProvider value)
  289. {
  290. if (value == null)
  291. throw new ArgumentNullException ("value");
  292. Stream stream = value.GetStream ();
  293. byte[] buf = new byte [Math.Min (2048, stream.CanSeek ? stream.Length : 2048)];
  294. int read;
  295. while ((read = stream.Read (buf, 0, buf.Length)) > 0) {
  296. WriteBase64 (buf, 0, read);
  297. }
  298. value.ReleaseStream (stream);
  299. }
  300. public virtual void WriteValue (TimeSpan duration)
  301. {
  302. WriteString (XmlConvert.ToString (duration));
  303. }
  304. public virtual void WriteValue (UniqueId id)
  305. {
  306. if (id == null)
  307. throw new ArgumentNullException ("id");
  308. WriteString (id.ToString ());
  309. }
  310. public virtual void WriteValue (XmlDictionaryString value)
  311. {
  312. WriteValue (value.Value);
  313. }
  314. public virtual void WriteXmlAttribute (string localName, string value)
  315. {
  316. WriteAttributeString ("xml", localName, "http://www.w3.org/XML/1998/namespace", value);
  317. }
  318. public virtual void WriteXmlAttribute (XmlDictionaryString localName,
  319. XmlDictionaryString value)
  320. {
  321. WriteXmlAttribute (localName.Value, value.Value);
  322. }
  323. public virtual void WriteXmlnsAttribute (
  324. string prefix, string namespaceUri)
  325. {
  326. // BTW .NET 2.0 those XmlWriters from XmlWrite.Create()
  327. // rejects namespace overriding i.e.
  328. //
  329. // xw.WriteStartElement ("foo", "urn:foo");
  330. // xw.WriteXmlnsAttribute ("foo", "urn:bar");
  331. //
  332. // causes an XmlException. We need fix in sys.xml.dll
  333. // When the prefix is null, this writer must mock
  334. // a dummy namespace up. It is then up to the actual
  335. // writer how it is determined in the output. (When
  336. // there is a duplicate, then it will be further
  337. // modified.)
  338. if (prefix == null)
  339. prefix = "d" + Depth + "p1";
  340. if (prefix == String.Empty)
  341. WriteAttributeString ("xmlns", namespaceUri);
  342. else
  343. WriteAttributeString ("xmlns", prefix, "http://www.w3.org/2000/xmlns/", namespaceUri);
  344. }
  345. public virtual void WriteXmlnsAttribute (string prefix,
  346. XmlDictionaryString namespaceUri)
  347. {
  348. WriteXmlnsAttribute (prefix, namespaceUri.Value);
  349. }
  350. }
  351. }
  352. #endif