XmlDictionaryWriter.cs 12 KB

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