XmlDictionaryWriter.cs 11 KB

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