XmlDictionaryWriter.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. reader.Read ();
  187. WriteNode (reader, defattr);
  188. break;
  189. case XmlNodeType.Attribute:
  190. case XmlNodeType.Text:
  191. WriteTextNode (reader, defattr);
  192. break;
  193. default:
  194. base.WriteNode (reader, defattr);
  195. break;
  196. }
  197. }
  198. private void WriteAttribute (XmlDictionaryReader reader, bool defattr)
  199. {
  200. if (!defattr && reader.IsDefault)
  201. return;
  202. WriteStartAttribute (reader.Prefix, reader.LocalName, reader.NamespaceURI);
  203. #if NET_2_1
  204. // no ReadAttributeValue() in 2.1 profile.
  205. WriteTextNode (reader, true);
  206. #else
  207. while (reader.ReadAttributeValue ()) {
  208. switch (reader.NodeType) {
  209. case XmlNodeType.Text:
  210. WriteTextNode (reader, true);
  211. break;
  212. case XmlNodeType.EntityReference:
  213. WriteEntityRef (reader.Name);
  214. break;
  215. }
  216. }
  217. #endif
  218. WriteEndAttribute ();
  219. }
  220. public override void WriteNode (XmlReader reader, bool defattr)
  221. {
  222. if (reader == null)
  223. throw new ArgumentNullException ("reader");
  224. XmlDictionaryReader dr = reader as XmlDictionaryReader;
  225. if (dr != null)
  226. WriteNode (dr, defattr);
  227. else
  228. base.WriteNode (reader, defattr);
  229. }
  230. public virtual void WriteQualifiedName (
  231. XmlDictionaryString localName,
  232. XmlDictionaryString namespaceUri)
  233. {
  234. WriteQualifiedName (localName.Value, namespaceUri.Value);
  235. }
  236. public void WriteStartAttribute (
  237. XmlDictionaryString localName,
  238. XmlDictionaryString namespaceUri)
  239. {
  240. WriteStartAttribute (localName.Value, namespaceUri.Value);
  241. }
  242. public virtual void WriteStartAttribute (string prefix,
  243. XmlDictionaryString localName,
  244. XmlDictionaryString namespaceUri)
  245. {
  246. WriteStartAttribute (prefix, localName.Value, namespaceUri.Value);
  247. }
  248. public void WriteStartElement (
  249. XmlDictionaryString localName,
  250. XmlDictionaryString namespaceUri)
  251. {
  252. WriteStartElement (null, localName, namespaceUri);
  253. }
  254. public virtual void WriteStartElement (string prefix,
  255. XmlDictionaryString localName,
  256. XmlDictionaryString namespaceUri)
  257. {
  258. if (localName == null)
  259. throw new ArgumentException ("localName must not be null.", "localName");
  260. WriteStartElement (prefix, localName.Value,
  261. namespaceUri != null ? namespaceUri.Value : null);
  262. }
  263. public virtual void WriteString (XmlDictionaryString value)
  264. {
  265. WriteString (value.Value);
  266. }
  267. protected virtual void WriteTextNode (XmlDictionaryReader reader, bool isAttribute)
  268. {
  269. WriteString (reader.Value);
  270. if (!isAttribute)
  271. reader.Read ();
  272. }
  273. public virtual void WriteValue (Guid guid)
  274. {
  275. WriteString (guid.ToString ());
  276. }
  277. public virtual void WriteValue (IStreamProvider value)
  278. {
  279. if (value == null)
  280. throw new ArgumentNullException ("value");
  281. Stream stream = value.GetStream ();
  282. byte[] buf = new byte [Math.Min (2048, stream.CanSeek ? stream.Length : 2048)];
  283. int read;
  284. while ((read = stream.Read (buf, 0, buf.Length)) > 0) {
  285. WriteBase64 (buf, 0, read);
  286. }
  287. value.ReleaseStream (stream);
  288. }
  289. public virtual void WriteValue (TimeSpan duration)
  290. {
  291. WriteString (XmlConvert.ToString (duration));
  292. }
  293. public virtual void WriteValue (UniqueId id)
  294. {
  295. if (id == null)
  296. throw new ArgumentNullException ("id");
  297. WriteString (id.ToString ());
  298. }
  299. public virtual void WriteValue (XmlDictionaryString value)
  300. {
  301. WriteValue (value.Value);
  302. }
  303. public virtual void WriteXmlAttribute (string localName, string value)
  304. {
  305. WriteAttributeString ("xml", localName, "http://www.w3.org/XML/1998/namespace", value);
  306. }
  307. public virtual void WriteXmlAttribute (XmlDictionaryString localName,
  308. XmlDictionaryString value)
  309. {
  310. WriteXmlAttribute (localName.Value, value.Value);
  311. }
  312. public virtual void WriteXmlnsAttribute (
  313. string prefix, string namespaceUri)
  314. {
  315. // BTW .NET 2.0 those XmlWriters from XmlWrite.Create()
  316. // rejects namespace overriding i.e.
  317. //
  318. // xw.WriteStartElement ("foo", "urn:foo");
  319. // xw.WriteXmlnsAttribute ("foo", "urn:bar");
  320. //
  321. // causes an XmlException. We need fix in sys.xml.dll
  322. // When the prefix is null, this writer must mock
  323. // a dummy namespace up. It is then up to the actual
  324. // writer how it is determined in the output. (When
  325. // there is a duplicate, then it will be further
  326. // modified.)
  327. if (prefix == null)
  328. prefix = "d" + Depth + "p1";
  329. if (prefix == String.Empty)
  330. WriteAttributeString ("xmlns", namespaceUri);
  331. else
  332. WriteAttributeString ("xmlns", prefix, "http://www.w3.org/2000/xmlns/", namespaceUri);
  333. }
  334. public virtual void WriteXmlnsAttribute (string prefix,
  335. XmlDictionaryString namespaceUri)
  336. {
  337. WriteXmlnsAttribute (prefix, namespaceUri.Value);
  338. }
  339. }
  340. }
  341. #endif